Create dwarf2/leb.[ch]
[deliverable/binutils-gdb.git] / gdb / dwarf2read.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"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
f4382c45 35#include "dwarf2/leb.h"
4de283e4
TT
36#include "bfd.h"
37#include "elf-bfd.h"
38#include "symtab.h"
39#include "gdbtypes.h"
40#include "objfiles.h"
d55e5aa6 41#include "dwarf2.h"
4de283e4
TT
42#include "buildsym.h"
43#include "demangle.h"
44#include "gdb-demangle.h"
4de283e4
TT
45#include "filenames.h" /* for DOSish file names */
46#include "macrotab.h"
47#include "language.h"
48#include "complaints.h"
d55e5aa6
TT
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
4de283e4
TT
51#include "cp-support.h"
52#include "hashtab.h"
53#include "command.h"
d55e5aa6 54#include "gdbcmd.h"
4de283e4
TT
55#include "block.h"
56#include "addrmap.h"
57#include "typeprint.h"
58#include "psympriv.h"
4de283e4 59#include "c-lang.h"
d55e5aa6 60#include "go-lang.h"
4de283e4
TT
61#include "valprint.h"
62#include "gdbcore.h" /* for gnutarget */
63#include "gdb/gdb-index.h"
4de283e4
TT
64#include "gdb_bfd.h"
65#include "f-lang.h"
66#include "source.h"
4de283e4 67#include "build-id.h"
d55e5aa6 68#include "namespace.h"
268a13a5
TT
69#include "gdbsupport/function-view.h"
70#include "gdbsupport/gdb_optional.h"
71#include "gdbsupport/underlying.h"
268a13a5 72#include "gdbsupport/hash_enum.h"
4de283e4 73#include "filename-seen-cache.h"
b32b108a 74#include "producer.h"
4de283e4 75#include <fcntl.h>
4de283e4 76#include <algorithm>
4de283e4 77#include <unordered_map>
268a13a5 78#include "gdbsupport/selftest.h"
c9317f21 79#include "rust-lang.h"
268a13a5 80#include "gdbsupport/pathstuff.h"
437afbb8 81
73be47f5
DE
82/* When == 1, print basic high level tracing messages.
83 When > 1, be more verbose.
b4f54984
DE
84 This is in contrast to the low level DIE reading of dwarf_die_debug. */
85static unsigned int dwarf_read_debug = 0;
45cfd468 86
d97bc12b 87/* When non-zero, dump DIEs after they are read in. */
b4f54984 88static unsigned int dwarf_die_debug = 0;
d97bc12b 89
27e0867f
DE
90/* When non-zero, dump line number entries as they are read in. */
91static unsigned int dwarf_line_debug = 0;
92
491144b5
CB
93/* When true, cross-check physname against demangler. */
94static bool check_physname = false;
900e11f9 95
491144b5
CB
96/* When true, do not reject deprecated .gdb_index sections. */
97static bool use_deprecated_index_sections = false;
481860b3 98
5bfd760d 99static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 100
f1e6e072
TT
101/* The "aclass" indices for various kinds of computed DWARF symbols. */
102
103static int dwarf2_locexpr_index;
104static int dwarf2_loclist_index;
105static int dwarf2_locexpr_block_index;
106static int dwarf2_loclist_block_index;
107
3f563c84
PA
108/* An index into a (C++) symbol name component in a symbol name as
109 recorded in the mapped_index's symbol table. For each C++ symbol
110 in the symbol table, we record one entry for the start of each
111 component in the symbol in a table of name components, and then
112 sort the table, in order to be able to binary search symbol names,
113 ignoring leading namespaces, both completion and regular look up.
114 For example, for symbol "A::B::C", we'll have an entry that points
115 to "A::B::C", another that points to "B::C", and another for "C".
116 Note that function symbols in GDB index have no parameter
117 information, just the function/method names. You can convert a
118 name_component to a "const char *" using the
119 'mapped_index::symbol_name_at(offset_type)' method. */
120
121struct name_component
122{
123 /* Offset in the symbol name where the component starts. Stored as
124 a (32-bit) offset instead of a pointer to save memory and improve
125 locality on 64-bit architectures. */
126 offset_type name_offset;
127
128 /* The symbol's index in the symbol and constant pool tables of a
129 mapped_index. */
130 offset_type idx;
131};
132
44ed8f3e
PA
133/* Base class containing bits shared by both .gdb_index and
134 .debug_name indexes. */
135
136struct mapped_index_base
137{
22ca247e
TT
138 mapped_index_base () = default;
139 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
140
44ed8f3e
PA
141 /* The name_component table (a sorted vector). See name_component's
142 description above. */
143 std::vector<name_component> name_components;
144
145 /* How NAME_COMPONENTS is sorted. */
146 enum case_sensitivity name_components_casing;
147
148 /* Return the number of names in the symbol table. */
149 virtual size_t symbol_name_count () const = 0;
150
151 /* Get the name of the symbol at IDX in the symbol table. */
152 virtual const char *symbol_name_at (offset_type idx) const = 0;
153
154 /* Return whether the name at IDX in the symbol table should be
155 ignored. */
156 virtual bool symbol_name_slot_invalid (offset_type idx) const
157 {
158 return false;
159 }
160
161 /* Build the symbol name component sorted vector, if we haven't
162 yet. */
163 void build_name_components ();
164
165 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
166 possible matches for LN_NO_PARAMS in the name component
167 vector. */
168 std::pair<std::vector<name_component>::const_iterator,
169 std::vector<name_component>::const_iterator>
3b00ef10
TT
170 find_name_components_bounds (const lookup_name_info &ln_no_params,
171 enum language lang) const;
44ed8f3e
PA
172
173 /* Prevent deleting/destroying via a base class pointer. */
174protected:
175 ~mapped_index_base() = default;
176};
177
9291a0cd
TT
178/* A description of the mapped index. The file format is described in
179 a comment by the code that writes the index. */
fc898b42 180struct mapped_index final : public mapped_index_base
9291a0cd 181{
f00a2de2
PA
182 /* A slot/bucket in the symbol table hash. */
183 struct symbol_table_slot
184 {
185 const offset_type name;
186 const offset_type vec;
187 };
188
559a7a62 189 /* Index data format version. */
3063847f 190 int version = 0;
559a7a62 191
f00a2de2
PA
192 /* The address table data. */
193 gdb::array_view<const gdb_byte> address_table;
b11b1f88 194
3876f04e 195 /* The symbol table, implemented as a hash table. */
f00a2de2 196 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 197
9291a0cd 198 /* A pointer to the constant pool. */
3063847f 199 const char *constant_pool = nullptr;
3f563c84 200
44ed8f3e
PA
201 bool symbol_name_slot_invalid (offset_type idx) const override
202 {
203 const auto &bucket = this->symbol_table[idx];
9ab08412 204 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 205 }
5c58de74 206
3f563c84
PA
207 /* Convenience method to get at the name of the symbol at IDX in the
208 symbol table. */
44ed8f3e 209 const char *symbol_name_at (offset_type idx) const override
f00a2de2 210 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 211
44ed8f3e
PA
212 size_t symbol_name_count () const override
213 { return this->symbol_table.size (); }
9291a0cd
TT
214};
215
927aa2e7
JK
216/* A description of the mapped .debug_names.
217 Uninitialized map has CU_COUNT 0. */
fc898b42 218struct mapped_debug_names final : public mapped_index_base
927aa2e7 219{
ed2dc618
SM
220 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
221 : dwarf2_per_objfile (dwarf2_per_objfile_)
222 {}
223
224 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
225 bfd_endian dwarf5_byte_order;
226 bool dwarf5_is_dwarf64;
227 bool augmentation_is_gdb;
228 uint8_t offset_size;
229 uint32_t cu_count = 0;
230 uint32_t tu_count, bucket_count, name_count;
231 const gdb_byte *cu_table_reordered, *tu_table_reordered;
232 const uint32_t *bucket_table_reordered, *hash_table_reordered;
233 const gdb_byte *name_table_string_offs_reordered;
234 const gdb_byte *name_table_entry_offs_reordered;
235 const gdb_byte *entry_pool;
236
237 struct index_val
238 {
239 ULONGEST dwarf_tag;
240 struct attr
241 {
242 /* Attribute name DW_IDX_*. */
243 ULONGEST dw_idx;
244
245 /* Attribute form DW_FORM_*. */
246 ULONGEST form;
247
248 /* Value if FORM is DW_FORM_implicit_const. */
249 LONGEST implicit_const;
250 };
251 std::vector<attr> attr_vec;
252 };
253
254 std::unordered_map<ULONGEST, index_val> abbrev_map;
255
256 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
257
258 /* Implementation of the mapped_index_base virtual interface, for
259 the name_components cache. */
260
261 const char *symbol_name_at (offset_type idx) const override
262 { return namei_to_name (idx); }
263
264 size_t symbol_name_count () const override
265 { return this->name_count; }
927aa2e7
JK
266};
267
cd4fb1b2 268/* See dwarf2read.h. */
ed2dc618 269
cd4fb1b2 270dwarf2_per_objfile *
ed2dc618
SM
271get_dwarf2_per_objfile (struct objfile *objfile)
272{
5bfd760d 273 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 274}
c906108c 275
251d32d9 276/* Default names of the debugging sections. */
c906108c 277
233a11ab
CS
278/* Note that if the debugging section has been compressed, it might
279 have a name like .zdebug_info. */
280
9cdd5dbd
DE
281static const struct dwarf2_debug_sections dwarf2_elf_names =
282{
251d32d9
TG
283 { ".debug_info", ".zdebug_info" },
284 { ".debug_abbrev", ".zdebug_abbrev" },
285 { ".debug_line", ".zdebug_line" },
286 { ".debug_loc", ".zdebug_loc" },
43988095 287 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 288 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 289 { ".debug_macro", ".zdebug_macro" },
251d32d9 290 { ".debug_str", ".zdebug_str" },
18a8505e 291 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 292 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 293 { ".debug_ranges", ".zdebug_ranges" },
43988095 294 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 295 { ".debug_types", ".zdebug_types" },
3019eac3 296 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
297 { ".debug_frame", ".zdebug_frame" },
298 { ".eh_frame", NULL },
24d3216f 299 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
300 { ".debug_names", ".zdebug_names" },
301 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 302 23
251d32d9 303};
c906108c 304
80626a55 305/* List of DWO/DWP sections. */
3019eac3 306
80626a55 307static const struct dwop_section_names
3019eac3
DE
308{
309 struct dwarf2_section_names abbrev_dwo;
310 struct dwarf2_section_names info_dwo;
311 struct dwarf2_section_names line_dwo;
312 struct dwarf2_section_names loc_dwo;
43988095 313 struct dwarf2_section_names loclists_dwo;
09262596
DE
314 struct dwarf2_section_names macinfo_dwo;
315 struct dwarf2_section_names macro_dwo;
3019eac3
DE
316 struct dwarf2_section_names str_dwo;
317 struct dwarf2_section_names str_offsets_dwo;
318 struct dwarf2_section_names types_dwo;
80626a55
DE
319 struct dwarf2_section_names cu_index;
320 struct dwarf2_section_names tu_index;
3019eac3 321}
80626a55 322dwop_section_names =
3019eac3
DE
323{
324 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
325 { ".debug_info.dwo", ".zdebug_info.dwo" },
326 { ".debug_line.dwo", ".zdebug_line.dwo" },
327 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 328 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
329 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
330 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
331 { ".debug_str.dwo", ".zdebug_str.dwo" },
332 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
333 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
334 { ".debug_cu_index", ".zdebug_cu_index" },
335 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
336};
337
c906108c
SS
338/* local data types */
339
107d2387
AC
340/* The data in a compilation unit header, after target2host
341 translation, looks like this. */
c906108c 342struct comp_unit_head
a738430d 343{
c764a876 344 unsigned int length;
a738430d 345 short version;
a738430d
MK
346 unsigned char addr_size;
347 unsigned char signed_addr_p;
9c541725 348 sect_offset abbrev_sect_off;
57349743 349
a738430d
MK
350 /* Size of file offsets; either 4 or 8. */
351 unsigned int offset_size;
57349743 352
a738430d
MK
353 /* Size of the length field; either 4 or 12. */
354 unsigned int initial_length_size;
57349743 355
43988095
JK
356 enum dwarf_unit_type unit_type;
357
a738430d
MK
358 /* Offset to the first byte of this compilation unit header in the
359 .debug_info section, for resolving relative reference dies. */
9c541725 360 sect_offset sect_off;
57349743 361
d00adf39
DE
362 /* Offset to first die in this cu from the start of the cu.
363 This will be the first byte following the compilation unit header. */
9c541725 364 cu_offset first_die_cu_offset;
43988095 365
a084a2a6
AT
366
367 /* 64-bit signature of this unit. For type units, it denotes the signature of
368 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
369 Also used in DWARF 5, to denote the dwo id when the unit type is
370 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
371 ULONGEST signature;
372
373 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 374 cu_offset type_cu_offset_in_tu;
a738430d 375};
c906108c 376
3da10d80
KS
377/* Type used for delaying computation of method physnames.
378 See comments for compute_delayed_physnames. */
379struct delayed_method_info
380{
381 /* The type to which the method is attached, i.e., its parent class. */
382 struct type *type;
383
384 /* The index of the method in the type's function fieldlists. */
385 int fnfield_index;
386
387 /* The index of the method in the fieldlist. */
388 int index;
389
390 /* The name of the DIE. */
391 const char *name;
392
393 /* The DIE associated with this method. */
394 struct die_info *die;
395};
396
e7c27a73
DJ
397/* Internal state when decoding a particular compilation unit. */
398struct dwarf2_cu
399{
fcd3b13d
SM
400 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
401 ~dwarf2_cu ();
402
403 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
404
c24bdb02
KS
405 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
406 Create the set of symtabs used by this TU, or if this TU is sharing
407 symtabs with another TU and the symtabs have already been created
408 then restore those symtabs in the line header.
409 We don't need the pc/line-number mapping for type units. */
410 void setup_type_unit_groups (struct die_info *die);
411
412 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
413 buildsym_compunit constructor. */
414 struct compunit_symtab *start_symtab (const char *name,
415 const char *comp_dir,
416 CORE_ADDR low_pc);
417
418 /* Reset the builder. */
419 void reset_builder () { m_builder.reset (); }
420
d00adf39 421 /* The header of the compilation unit. */
fcd3b13d 422 struct comp_unit_head header {};
e142c38c 423
d00adf39 424 /* Base address of this compilation unit. */
fcd3b13d 425 CORE_ADDR base_address = 0;
d00adf39
DE
426
427 /* Non-zero if base_address has been set. */
fcd3b13d 428 int base_known = 0;
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
470 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 471 int last_used = 0;
ae038cb0 472
b64f50a1
JK
473 /* A hash table of DIE cu_offset for following references with
474 die_info->offset.sect_off as hash. */
fcd3b13d 475 htab_t die_hash = nullptr;
10b3939b
DJ
476
477 /* Full DIEs if read in. */
fcd3b13d 478 struct die_info *dies = nullptr;
10b3939b
DJ
479
480 /* A set of pointers to dwarf2_per_cu_data objects for compilation
481 units referenced by this one. Only set during full symbol processing;
482 partial symbol tables do not have dependencies. */
fcd3b13d 483 htab_t dependencies = nullptr;
10b3939b 484
cb1df416 485 /* Header data from the line table, during full symbol processing. */
fcd3b13d 486 struct line_header *line_header = nullptr;
4c8aa72d
PA
487 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
488 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
489 this is the DW_TAG_compile_unit die for this CU. We'll hold on
490 to the line header as long as this DIE is being processed. See
491 process_die_scope. */
fcd3b13d 492 die_info *line_header_die_owner = nullptr;
cb1df416 493
3da10d80
KS
494 /* A list of methods which need to have physnames computed
495 after all type information has been read. */
c89b44cd 496 std::vector<delayed_method_info> method_list;
3da10d80 497
96408a79 498 /* To be copied to symtab->call_site_htab. */
fcd3b13d 499 htab_t call_site_htab = nullptr;
96408a79 500
034e5797
DE
501 /* Non-NULL if this CU came from a DWO file.
502 There is an invariant here that is important to remember:
503 Except for attributes copied from the top level DIE in the "main"
504 (or "stub") file in preparation for reading the DWO file
18a8505e 505 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
506 Either there isn't a DWO file (in which case this is NULL and the point
507 is moot), or there is and either we're not going to read it (in which
508 case this is NULL) or there is and we are reading it (in which case this
509 is non-NULL). */
fcd3b13d 510 struct dwo_unit *dwo_unit = nullptr;
3019eac3 511
18a8505e 512 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 513 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 514 gdb::optional<ULONGEST> addr_base;
3019eac3 515
18a8505e 516 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 517 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 518 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 523 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 525 ULONGEST ranges_base = 0;
2e3cf129 526
c9317f21
TT
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
18a8505e
AT
535 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
536 files, the value is implicitly zero. For DWARF 5 version DWO files, the
537 value is often implicit and is the size of the header of
538 .debug_str_offsets section (8 or 4, depending on the address size). */
539 gdb::optional<ULONGEST> str_offsets_base;
540
ae038cb0 541 /* Mark used when releasing cached dies. */
9068261f 542 bool mark : 1;
ae038cb0 543
8be455d7
JK
544 /* This CU references .debug_loc. See the symtab->locations_valid field.
545 This test is imperfect as there may exist optimized debug code not using
546 any location list and still facing inlining issues if handled as
547 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 548 bool has_loclist : 1;
ba919b58 549
9068261f 550 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
551 if all the producer_is_* fields are valid. This information is cached
552 because profiling CU expansion showed excessive time spent in
553 producer_is_gxx_lt_4_6. */
9068261f
AB
554 bool checked_producer : 1;
555 bool producer_is_gxx_lt_4_6 : 1;
556 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 557 bool producer_is_icc : 1;
9068261f 558 bool producer_is_icc_lt_14 : 1;
c258c396 559 bool producer_is_codewarrior : 1;
4d4ec4e5 560
9068261f 561 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
562 debugging info for C++ namespaces. GCC 3.3.x did not produce
563 this information, but later versions do. */
564
9068261f 565 bool processing_has_namespace_info : 1;
d590ff25
YQ
566
567 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
568
569 /* If this CU was inherited by another CU (via specification,
570 abstract_origin, etc), this is the ancestor CU. */
571 dwarf2_cu *ancestor;
572
573 /* Get the buildsym_compunit for this CU. */
574 buildsym_compunit *get_builder ()
575 {
576 /* If this CU has a builder associated with it, use that. */
577 if (m_builder != nullptr)
578 return m_builder.get ();
579
580 /* Otherwise, search ancestors for a valid builder. */
581 if (ancestor != nullptr)
582 return ancestor->get_builder ();
583
584 return nullptr;
585 }
e7c27a73
DJ
586};
587
094b34ac
DE
588/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
589 This includes type_unit_group and quick_file_names. */
590
591struct stmt_list_hash
592{
593 /* The DWO unit this table is from or NULL if there is none. */
594 struct dwo_unit *dwo_unit;
595
596 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 597 sect_offset line_sect_off;
094b34ac
DE
598};
599
f4dc4d17
DE
600/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
601 an object of this type. */
602
603struct type_unit_group
604{
0186c6a7 605 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
606 To simplify things we create an artificial CU that "includes" all the
607 type units using this stmt_list so that the rest of the code still has
608 a "per_cu" handle on the symtab.
609 This PER_CU is recognized by having no section. */
8a0459fd 610#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17
DE
625
626 /* The number of symtabs from the line header.
627 The value here must match line_header.num_file_names. */
628 unsigned int num_symtabs;
629
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). */
51ac9db5 728 htab_t cus {};
3019eac3
DE
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
51ac9db5 732 htab_t 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 *. */
400174b1
TT
869 htab_t loaded_cus {};
870 htab_t 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
c0ab21c2
TT
878struct abbrev_table;
879typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
880
0963b4bd
MS
881/* Struct used to pass misc. parameters to read_die_and_children, et
882 al. which are used for both .debug_info and .debug_types dies.
883 All parameters here are unchanging for the life of the call. This
dee91e82 884 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
885
886struct die_reader_specs
887{
a32a8923 888 /* The bfd of die_section. */
93311388
DE
889 bfd* abfd;
890
891 /* The CU of the DIE we are parsing. */
892 struct dwarf2_cu *cu;
893
80626a55 894 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
895 struct dwo_file *dwo_file;
896
dee91e82 897 /* The section the die comes from.
3019eac3 898 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
899 struct dwarf2_section_info *die_section;
900
901 /* die_section->buffer. */
d521ce57 902 const gdb_byte *buffer;
f664829e
DE
903
904 /* The end of the buffer. */
905 const gdb_byte *buffer_end;
a2ce51a0
DE
906
907 /* The value of the DW_AT_comp_dir attribute. */
908 const char *comp_dir;
685af9cd
TT
909
910 /* The abbreviation table to use when reading the DIEs. */
911 struct abbrev_table *abbrev_table;
93311388
DE
912};
913
c0ab21c2
TT
914/* A subclass of die_reader_specs that holds storage and has complex
915 constructor and destructor behavior. */
916
917class cutu_reader : public die_reader_specs
918{
919public:
920
921 cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 struct abbrev_table *abbrev_table,
923 int use_existing_cu, int keep,
924 bool skip_partial);
925
926 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
927 struct dwarf2_cu *parent_cu = nullptr,
928 struct dwo_file *dwo_file = nullptr);
929
930 ~cutu_reader ();
931
932 DISABLE_COPY_AND_ASSIGN (cutu_reader);
933
934 const gdb_byte *info_ptr = nullptr;
935 struct die_info *comp_unit_die = nullptr;
936 int has_children = 0;
937 bool dummy_p = false;
938
939private:
940 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
941 int use_existing_cu, int keep);
942
943 struct dwarf2_per_cu_data *m_this_cu;
944 int m_keep = 0;
945 std::unique_ptr<dwarf2_cu> m_new_cu;
946
947 /* The ordinary abbreviation table. */
948 abbrev_table_up m_abbrev_table_holder;
949
950 /* The DWO abbreviation table. */
951 abbrev_table_up m_dwo_abbrev_table;
952};
dee91e82 953
7ba99d21
AT
954/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
955 later. */
956typedef int dir_index;
ecfb656c 957
7ba99d21
AT
958/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
959 and later. */
960typedef int file_name_index;
ecfb656c 961
52059ffd
TT
962struct file_entry
963{
fff8551c
PA
964 file_entry () = default;
965
ecfb656c 966 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
967 unsigned int mod_time_, unsigned int length_)
968 : name (name_),
ecfb656c 969 d_index (d_index_),
fff8551c
PA
970 mod_time (mod_time_),
971 length (length_)
972 {}
973
ecfb656c
PA
974 /* Return the include directory at D_INDEX stored in LH. Returns
975 NULL if D_INDEX is out of bounds. */
8c43009f
PA
976 const char *include_dir (const line_header *lh) const;
977
fff8551c
PA
978 /* The file name. Note this is an observing pointer. The memory is
979 owned by debug_line_buffer. */
980 const char *name {};
981
8c43009f 982 /* The directory index (1-based). */
ecfb656c 983 dir_index d_index {};
fff8551c
PA
984
985 unsigned int mod_time {};
986
987 unsigned int length {};
988
989 /* True if referenced by the Line Number Program. */
990 bool included_p {};
991
83769d0b 992 /* The associated symbol table, if any. */
fff8551c 993 struct symtab *symtab {};
52059ffd
TT
994};
995
debd256d
JB
996/* The line number information for a compilation unit (found in the
997 .debug_line section) begins with a "statement program header",
998 which contains the following information. */
999struct line_header
1000{
fff8551c
PA
1001 line_header ()
1002 : offset_in_dwz {}
1003 {}
1004
1005 /* Add an entry to the include directory table. */
1006 void add_include_dir (const char *include_dir);
1007
1008 /* Add an entry to the file name table. */
ecfb656c 1009 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
1010 unsigned int mod_time, unsigned int length);
1011
7ba99d21
AT
1012 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1013 Returns NULL if INDEX is out of bounds. */
ecfb656c 1014 const char *include_dir_at (dir_index index) const
8c43009f 1015 {
7ba99d21
AT
1016 int vec_index;
1017 if (version >= 5)
1018 vec_index = index;
1019 else
1020 vec_index = index - 1;
1021 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 1022 return NULL;
7ba99d21 1023 return m_include_dirs[vec_index];
8c43009f
PA
1024 }
1025
7ba99d21 1026 bool is_valid_file_index (int file_index)
8c43009f 1027 {
7ba99d21
AT
1028 if (version >= 5)
1029 return 0 <= file_index && file_index < file_names_size ();
1030 return 1 <= file_index && file_index <= file_names_size ();
1031 }
ecfb656c 1032
7ba99d21
AT
1033 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1034 Returns NULL if INDEX is out of bounds. */
1035 file_entry *file_name_at (file_name_index index)
1036 {
1037 int vec_index;
1038 if (version >= 5)
1039 vec_index = index;
1040 else
1041 vec_index = index - 1;
1042 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1043 return NULL;
7ba99d21 1044 return &m_file_names[vec_index];
fff8551c
PA
1045 }
1046
7ba99d21
AT
1047 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1048 this method should only be used to iterate through all file entries in an
1049 index-agnostic manner. */
1050 std::vector<file_entry> &file_names ()
1051 { return m_file_names; }
1052
527f3840 1053 /* Offset of line number information in .debug_line section. */
9c541725 1054 sect_offset sect_off {};
527f3840
JK
1055
1056 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1057 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1058
1059 unsigned int total_length {};
1060 unsigned short version {};
1061 unsigned int header_length {};
1062 unsigned char minimum_instruction_length {};
1063 unsigned char maximum_ops_per_instruction {};
1064 unsigned char default_is_stmt {};
1065 int line_base {};
1066 unsigned char line_range {};
1067 unsigned char opcode_base {};
debd256d
JB
1068
1069 /* standard_opcode_lengths[i] is the number of operands for the
1070 standard opcode whose value is i. This means that
1071 standard_opcode_lengths[0] is unused, and the last meaningful
1072 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1073 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1074
7ba99d21
AT
1075 int file_names_size ()
1076 { return m_file_names.size(); }
debd256d
JB
1077
1078 /* The start and end of the statement program following this
6502dd73 1079 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1080 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1081
1082 private:
1083 /* The include_directories table. Note these are observing
1084 pointers. The memory is owned by debug_line_buffer. */
1085 std::vector<const char *> m_include_dirs;
1086
1087 /* The file_names table. This is private because the meaning of indexes
1088 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1089 before, and is 0 in DWARF 5 and later). So the client should use
1090 file_name_at method for access. */
1091 std::vector<file_entry> m_file_names;
debd256d 1092};
c906108c 1093
fff8551c
PA
1094typedef std::unique_ptr<line_header> line_header_up;
1095
8c43009f
PA
1096const char *
1097file_entry::include_dir (const line_header *lh) const
1098{
ecfb656c 1099 return lh->include_dir_at (d_index);
8c43009f
PA
1100}
1101
c906108c 1102/* When we construct a partial symbol table entry we only
0963b4bd 1103 need this much information. */
6f06d47b 1104struct partial_die_info : public allocate_on_obstack
c906108c 1105 {
6f06d47b
YQ
1106 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1107
1108 /* Disable assign but still keep copy ctor, which is needed
1109 load_partial_dies. */
1110 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1111
52356b79
YQ
1112 /* Adjust the partial die before generating a symbol for it. This
1113 function may set the is_external flag or change the DIE's
1114 name. */
1115 void fixup (struct dwarf2_cu *cu);
1116
48fbe735
YQ
1117 /* Read a minimal amount of information into the minimal die
1118 structure. */
1119 const gdb_byte *read (const struct die_reader_specs *reader,
1120 const struct abbrev_info &abbrev,
1121 const gdb_byte *info_ptr);
1122
72bf9492 1123 /* Offset of this DIE. */
6f06d47b 1124 const sect_offset sect_off;
72bf9492
DJ
1125
1126 /* DWARF-2 tag for this DIE. */
6f06d47b 1127 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1128
72bf9492 1129 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1130 const unsigned int has_children : 1;
1131
72bf9492
DJ
1132 unsigned int is_external : 1;
1133 unsigned int is_declaration : 1;
1134 unsigned int has_type : 1;
1135 unsigned int has_specification : 1;
1136 unsigned int has_pc_info : 1;
481860b3 1137 unsigned int may_be_inlined : 1;
72bf9492 1138
0c1b455e
TT
1139 /* This DIE has been marked DW_AT_main_subprogram. */
1140 unsigned int main_subprogram : 1;
1141
72bf9492
DJ
1142 /* Flag set if the SCOPE field of this structure has been
1143 computed. */
1144 unsigned int scope_set : 1;
1145
fa4028e9
JB
1146 /* Flag set if the DIE has a byte_size attribute. */
1147 unsigned int has_byte_size : 1;
1148
ff908ebf
AW
1149 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1150 unsigned int has_const_value : 1;
1151
98bfdba5
PA
1152 /* Flag set if any of the DIE's children are template arguments. */
1153 unsigned int has_template_arguments : 1;
1154
52356b79 1155 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1156 unsigned int fixup_called : 1;
1157
36586728
TT
1158 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1159 unsigned int is_dwz : 1;
1160
1161 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1162 unsigned int spec_is_dwz : 1;
1163
72bf9492 1164 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1165 sometimes a default name for unnamed DIEs. */
6f06d47b 1166 const char *name = nullptr;
72bf9492 1167
abc72ce4 1168 /* The linkage name, if present. */
6f06d47b 1169 const char *linkage_name = nullptr;
abc72ce4 1170
72bf9492
DJ
1171 /* The scope to prepend to our children. This is generally
1172 allocated on the comp_unit_obstack, so will disappear
1173 when this compilation unit leaves the cache. */
6f06d47b 1174 const char *scope = nullptr;
72bf9492 1175
95554aad
TT
1176 /* Some data associated with the partial DIE. The tag determines
1177 which field is live. */
1178 union
1179 {
1180 /* The location description associated with this DIE, if any. */
1181 struct dwarf_block *locdesc;
1182 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1183 sect_offset sect_off;
6f06d47b 1184 } d {};
72bf9492
DJ
1185
1186 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1187 CORE_ADDR lowpc = 0;
1188 CORE_ADDR highpc = 0;
72bf9492 1189
93311388 1190 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1191 DW_AT_sibling, if any. */
48fbe735
YQ
1192 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1193 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1194 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1195
1196 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1197 DW_AT_specification (or DW_AT_abstract_origin or
1198 DW_AT_extension). */
6f06d47b 1199 sect_offset spec_offset {};
72bf9492
DJ
1200
1201 /* Pointers to this DIE's parent, first child, and next sibling,
1202 if any. */
6f06d47b
YQ
1203 struct partial_die_info *die_parent = nullptr;
1204 struct partial_die_info *die_child = nullptr;
1205 struct partial_die_info *die_sibling = nullptr;
1206
1207 friend struct partial_die_info *
1208 dwarf2_cu::find_partial_die (sect_offset sect_off);
1209
1210 private:
1211 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1212 partial_die_info (sect_offset sect_off)
1213 : partial_die_info (sect_off, DW_TAG_padding, 0)
1214 {
1215 }
1216
1217 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1218 int has_children_)
1219 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1220 {
1221 is_external = 0;
1222 is_declaration = 0;
1223 has_type = 0;
1224 has_specification = 0;
1225 has_pc_info = 0;
1226 may_be_inlined = 0;
1227 main_subprogram = 0;
1228 scope_set = 0;
1229 has_byte_size = 0;
1230 has_const_value = 0;
1231 has_template_arguments = 0;
1232 fixup_called = 0;
1233 is_dwz = 0;
1234 spec_is_dwz = 0;
1235 }
c906108c
SS
1236 };
1237
0963b4bd 1238/* This data structure holds the information of an abbrev. */
c906108c
SS
1239struct abbrev_info
1240 {
1241 unsigned int number; /* number identifying abbrev */
1242 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1243 unsigned short has_children; /* boolean */
1244 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1245 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1246 struct abbrev_info *next; /* next in chain */
1247 };
1248
1249struct attr_abbrev
1250 {
9d25dd43
DE
1251 ENUM_BITFIELD(dwarf_attribute) name : 16;
1252 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1253
1254 /* It is valid only if FORM is DW_FORM_implicit_const. */
1255 LONGEST implicit_const;
c906108c
SS
1256 };
1257
433df2d4
DE
1258/* Size of abbrev_table.abbrev_hash_table. */
1259#define ABBREV_HASH_SIZE 121
1260
1261/* Top level data structure to contain an abbreviation table. */
1262
1263struct abbrev_table
1264{
685af9cd
TT
1265 explicit abbrev_table (sect_offset off)
1266 : sect_off (off)
1267 {
4a17f768 1268 m_abbrevs =
685af9cd 1269 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1270 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1271 }
1272
1273 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1274
1275 /* Allocate space for a struct abbrev_info object in
1276 ABBREV_TABLE. */
1277 struct abbrev_info *alloc_abbrev ();
1278
1279 /* Add an abbreviation to the table. */
1280 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1281
1282 /* Look up an abbrev in the table.
1283 Returns NULL if the abbrev is not found. */
1284
1285 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1286
1287
f4dc4d17
DE
1288 /* Where the abbrev table came from.
1289 This is used as a sanity check when the table is used. */
685af9cd 1290 const sect_offset sect_off;
433df2d4
DE
1291
1292 /* Storage for the abbrev table. */
685af9cd 1293 auto_obstack abbrev_obstack;
433df2d4 1294
4a17f768
YQ
1295private:
1296
433df2d4
DE
1297 /* Hash table of abbrevs.
1298 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1299 It could be statically allocated, but the previous code didn't so we
1300 don't either. */
4a17f768 1301 struct abbrev_info **m_abbrevs;
433df2d4
DE
1302};
1303
0963b4bd 1304/* Attributes have a name and a value. */
b60c80d6
DJ
1305struct attribute
1306 {
9d25dd43 1307 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1308 ENUM_BITFIELD(dwarf_form) form : 15;
1309
1310 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1311 field should be in u.str (existing only for DW_STRING) but it is kept
1312 here for better struct attribute alignment. */
1313 unsigned int string_is_canonical : 1;
1314
b60c80d6
DJ
1315 union
1316 {
15d034d0 1317 const char *str;
b60c80d6 1318 struct dwarf_block *blk;
43bbcdc2
PH
1319 ULONGEST unsnd;
1320 LONGEST snd;
b60c80d6 1321 CORE_ADDR addr;
ac9ec31b 1322 ULONGEST signature;
b60c80d6
DJ
1323 }
1324 u;
1325 };
1326
0963b4bd 1327/* This data structure holds a complete die structure. */
c906108c
SS
1328struct die_info
1329 {
76815b17
DE
1330 /* DWARF-2 tag for this DIE. */
1331 ENUM_BITFIELD(dwarf_tag) tag : 16;
1332
1333 /* Number of attributes */
98bfdba5
PA
1334 unsigned char num_attrs;
1335
1336 /* True if we're presently building the full type name for the
1337 type derived from this DIE. */
1338 unsigned char building_fullname : 1;
76815b17 1339
adde2bff
DE
1340 /* True if this die is in process. PR 16581. */
1341 unsigned char in_process : 1;
1342
76815b17
DE
1343 /* Abbrev number */
1344 unsigned int abbrev;
1345
93311388 1346 /* Offset in .debug_info or .debug_types section. */
9c541725 1347 sect_offset sect_off;
78ba4af6
JB
1348
1349 /* The dies in a compilation unit form an n-ary tree. PARENT
1350 points to this die's parent; CHILD points to the first child of
1351 this node; and all the children of a given node are chained
4950bc1c 1352 together via their SIBLING fields. */
639d11d3
DC
1353 struct die_info *child; /* Its first child, if any. */
1354 struct die_info *sibling; /* Its next sibling, if any. */
1355 struct die_info *parent; /* Its parent, if any. */
c906108c 1356
b60c80d6
DJ
1357 /* An array of attributes, with NUM_ATTRS elements. There may be
1358 zero, but it's not common and zero-sized arrays are not
1359 sufficiently portable C. */
1360 struct attribute attrs[1];
c906108c
SS
1361 };
1362
0963b4bd 1363/* Get at parts of an attribute structure. */
c906108c
SS
1364
1365#define DW_STRING(attr) ((attr)->u.str)
8285870a 1366#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1367#define DW_UNSND(attr) ((attr)->u.unsnd)
1368#define DW_BLOCK(attr) ((attr)->u.blk)
1369#define DW_SND(attr) ((attr)->u.snd)
1370#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1371#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1372
0963b4bd 1373/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1374struct dwarf_block
1375 {
56eb65bd 1376 size_t size;
1d6edc3c
JK
1377
1378 /* Valid only if SIZE is not zero. */
d521ce57 1379 const gdb_byte *data;
c906108c
SS
1380 };
1381
c906108c
SS
1382/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1383 but this would require a corresponding change in unpack_field_as_long
1384 and friends. */
1385static int bits_per_byte = 8;
1386
2ddeaf8a
TT
1387/* When reading a variant or variant part, we track a bit more
1388 information about the field, and store it in an object of this
1389 type. */
1390
1391struct variant_field
1392{
1393 /* If we see a DW_TAG_variant, then this will be the discriminant
1394 value. */
1395 ULONGEST discriminant_value;
1396 /* If we see a DW_TAG_variant, then this will be set if this is the
1397 default branch. */
1398 bool default_branch;
1399 /* While reading a DW_TAG_variant_part, this will be set if this
1400 field is the discriminant. */
1401 bool is_discriminant;
1402};
1403
52059ffd
TT
1404struct nextfield
1405{
be2daae6
TT
1406 int accessibility = 0;
1407 int virtuality = 0;
2ddeaf8a 1408 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1409 struct variant_field variant {};
1410 struct field field {};
52059ffd
TT
1411};
1412
1413struct fnfieldlist
1414{
be2daae6
TT
1415 const char *name = nullptr;
1416 std::vector<struct fn_field> fnfields;
52059ffd
TT
1417};
1418
c906108c
SS
1419/* The routines that read and process dies for a C struct or C++ class
1420 pass lists of data member fields and lists of member function fields
1421 in an instance of a field_info structure, as defined below. */
1422struct field_info
c5aa993b 1423 {
0963b4bd 1424 /* List of data member and baseclasses fields. */
be2daae6
TT
1425 std::vector<struct nextfield> fields;
1426 std::vector<struct nextfield> baseclasses;
c906108c 1427
7d0ccb61 1428 /* Number of fields (including baseclasses). */
be2daae6 1429 int nfields = 0;
c906108c 1430
85102364 1431 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1432 int non_public_fields = 0;
c906108c 1433
c5aa993b
JM
1434 /* Member function fieldlist array, contains name of possibly overloaded
1435 member function, number of overloaded member functions and a pointer
1436 to the head of the member function field chain. */
be2daae6 1437 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1438
1439 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1440 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1441 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1442
1443 /* Nested types defined by this class and the number of elements in this
1444 list. */
be2daae6 1445 std::vector<struct decl_field> nested_types_list;
c5aa993b 1446 };
c906108c 1447
10b3939b
DJ
1448/* One item on the queue of compilation units to read in full symbols
1449 for. */
1450struct dwarf2_queue_item
1451{
1452 struct dwarf2_per_cu_data *per_cu;
95554aad 1453 enum language pretend_language;
10b3939b
DJ
1454 struct dwarf2_queue_item *next;
1455};
1456
1457/* The current queue. */
1458static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1459
ae038cb0
DJ
1460/* Loaded secondary compilation units are kept in memory until they
1461 have not been referenced for the processing of this many
1462 compilation units. Set this to zero to disable caching. Cache
1463 sizes of up to at least twenty will improve startup time for
1464 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1465static int dwarf_max_cache_age = 5;
920d2a44 1466static void
b4f54984
DE
1467show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1468 struct cmd_list_element *c, const char *value)
920d2a44 1469{
3e43a32a 1470 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1471 "DWARF compilation units is %s.\n"),
920d2a44
AC
1472 value);
1473}
4390d890 1474\f
c906108c
SS
1475/* local function prototypes */
1476
a32a8923
DE
1477static const char *get_section_name (const struct dwarf2_section_info *);
1478
1479static const char *get_section_file_name (const struct dwarf2_section_info *);
1480
918dd910
JK
1481static void dwarf2_find_base_address (struct die_info *die,
1482 struct dwarf2_cu *cu);
1483
891813be 1484static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1485 (struct dwarf2_per_cu_data *per_cu, const char *name);
1486
f1902523
JK
1487static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1488 const gdb_byte *info_ptr,
1489 struct die_info *type_unit_die,
c0ab21c2 1490 int has_children);
f1902523 1491
ed2dc618
SM
1492static void dwarf2_build_psymtabs_hard
1493 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1494
72bf9492
DJ
1495static void scan_partial_symbols (struct partial_die_info *,
1496 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1497 int, struct dwarf2_cu *);
c906108c 1498
72bf9492
DJ
1499static void add_partial_symbol (struct partial_die_info *,
1500 struct dwarf2_cu *);
63d06c5c 1501
72bf9492
DJ
1502static void add_partial_namespace (struct partial_die_info *pdi,
1503 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1504 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1505
5d7cb8df 1506static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1507 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1508 struct dwarf2_cu *cu);
1509
72bf9492
DJ
1510static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1511 struct dwarf2_cu *cu);
91c24f0a 1512
bc30ff58
JB
1513static void add_partial_subprogram (struct partial_die_info *pdi,
1514 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1515 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1516
685af9cd 1517static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1518 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1519 sect_offset);
433df2d4 1520
d521ce57 1521static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1522
dee91e82 1523static struct partial_die_info *load_partial_dies
d521ce57 1524 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1525
fb816e8b
TV
1526/* A pair of partial_die_info and compilation unit. */
1527struct cu_partial_die_info
1528{
1529 /* The compilation unit of the partial_die_info. */
1530 struct dwarf2_cu *cu;
1531 /* A partial_die_info. */
1532 struct partial_die_info *pdi;
122cf0f2
AB
1533
1534 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1535 : cu (cu),
1536 pdi (pdi)
405feb71 1537 { /* Nothing. */ }
122cf0f2
AB
1538
1539private:
1540 cu_partial_die_info () = delete;
fb816e8b
TV
1541};
1542
122cf0f2
AB
1543static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1544 struct dwarf2_cu *);
72bf9492 1545
d521ce57
TT
1546static const gdb_byte *read_attribute (const struct die_reader_specs *,
1547 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1548 const gdb_byte *, bool *need_reprocess);
1549
1550static void read_attribute_reprocess (const struct die_reader_specs *reader,
1551 struct attribute *attr);
1552
1553static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1554
d521ce57 1555static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1556 unsigned int *);
c906108c 1557
d521ce57 1558static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1559
1560static LONGEST read_checked_initial_length_and_offset
d521ce57 1561 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1562 unsigned int *, unsigned int *);
613e1657 1563
d521ce57
TT
1564static LONGEST read_offset (bfd *, const gdb_byte *,
1565 const struct comp_unit_head *,
c764a876
DE
1566 unsigned int *);
1567
d521ce57 1568static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1569
ed2dc618
SM
1570static sect_offset read_abbrev_offset
1571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1572 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1573
d521ce57 1574static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1575
d521ce57 1576static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1577
ed2dc618
SM
1578static const char *read_indirect_string
1579 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1580 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1581
ed2dc618
SM
1582static const char *read_indirect_line_string
1583 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1584 const struct comp_unit_head *, unsigned int *);
36586728 1585
ed2dc618
SM
1586static const char *read_indirect_string_at_offset
1587 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1588 LONGEST str_offset);
927aa2e7 1589
ed2dc618
SM
1590static const char *read_indirect_string_from_dwz
1591 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1592
d521ce57
TT
1593static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1594 const gdb_byte *,
3019eac3
DE
1595 unsigned int *);
1596
18a8505e
AT
1597static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1598 ULONGEST str_index);
1599
1600static const char *read_stub_str_index (struct dwarf2_cu *cu,
1601 ULONGEST str_index);
3019eac3 1602
e142c38c 1603static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1604
e142c38c
DJ
1605static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1606 struct dwarf2_cu *);
c906108c 1607
348e048f 1608static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1609 unsigned int);
348e048f 1610
7d45c7c3
KB
1611static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1612 struct dwarf2_cu *cu);
1613
a084a2a6
AT
1614static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1615
05cf31d1
JB
1616static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1617 struct dwarf2_cu *cu);
1618
e142c38c 1619static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1620
e142c38c 1621static struct die_info *die_specification (struct die_info *die,
f2f0e013 1622 struct dwarf2_cu **);
63d06c5c 1623
9c541725 1624static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1625 struct dwarf2_cu *cu);
debd256d 1626
f3f5162e 1627static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1628 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1629 CORE_ADDR, int decode_mapping);
c906108c 1630
804d2729
TT
1631static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1632 const char *);
c906108c 1633
a14ed312 1634static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1635 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1636
ff39bb5e 1637static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1638 struct dwarf2_cu *);
c906108c 1639
ff39bb5e 1640static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1641 struct type *type,
1642 const char *name,
1643 struct obstack *obstack,
12df843f 1644 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1645 const gdb_byte **bytes,
98bfdba5 1646 struct dwarf2_locexpr_baton **baton);
2df3850c 1647
e7c27a73 1648static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1649
b4ba55a1
JB
1650static int need_gnat_info (struct dwarf2_cu *);
1651
3e43a32a
MS
1652static struct type *die_descriptive_type (struct die_info *,
1653 struct dwarf2_cu *);
b4ba55a1
JB
1654
1655static void set_descriptive_type (struct type *, struct die_info *,
1656 struct dwarf2_cu *);
1657
e7c27a73
DJ
1658static struct type *die_containing_type (struct die_info *,
1659 struct dwarf2_cu *);
c906108c 1660
ff39bb5e 1661static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1662 struct dwarf2_cu *);
c906108c 1663
f792889a 1664static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1665
673bfd45
DE
1666static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1667
0d5cff50 1668static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1669
6e70227d 1670static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1671 const char *suffix, int physname,
1672 struct dwarf2_cu *cu);
63d06c5c 1673
e7c27a73 1674static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1675
348e048f
DE
1676static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1677
e7c27a73 1678static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1679
e7c27a73 1680static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1681
96408a79
SA
1682static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1683
71a3c369
TT
1684static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1685
ff013f42 1686static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1687 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1688
3a2b436a 1689/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1690 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1691enum pc_bounds_kind
1692{
e385593e 1693 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1694 PC_BOUNDS_NOT_PRESENT,
1695
e385593e
JK
1696 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1697 were present but they do not form a valid range of PC addresses. */
1698 PC_BOUNDS_INVALID,
1699
3a2b436a
JK
1700 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1701 PC_BOUNDS_RANGES,
1702
1703 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1704 PC_BOUNDS_HIGH_LOW,
1705};
1706
1707static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1708 CORE_ADDR *, CORE_ADDR *,
1709 struct dwarf2_cu *,
891813be 1710 dwarf2_psymtab *);
c906108c 1711
fae299cd
DC
1712static void get_scope_pc_bounds (struct die_info *,
1713 CORE_ADDR *, CORE_ADDR *,
1714 struct dwarf2_cu *);
1715
801e3a5b
JB
1716static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1717 CORE_ADDR, struct dwarf2_cu *);
1718
a14ed312 1719static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1720 struct dwarf2_cu *);
c906108c 1721
a14ed312 1722static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1723 struct type *, struct dwarf2_cu *);
c906108c 1724
a14ed312 1725static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1726 struct die_info *, struct type *,
e7c27a73 1727 struct dwarf2_cu *);
c906108c 1728
a14ed312 1729static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1730 struct type *,
1731 struct dwarf2_cu *);
c906108c 1732
134d01f1 1733static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1734
e7c27a73 1735static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1736
e7c27a73 1737static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1738
5d7cb8df
JK
1739static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1740
804d2729 1741static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1742
27aa8d6a
SW
1743static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1744
74921315
KS
1745static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1746
f55ee35c
JK
1747static struct type *read_module_type (struct die_info *die,
1748 struct dwarf2_cu *cu);
1749
38d518c9 1750static const char *namespace_name (struct die_info *die,
e142c38c 1751 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1752
134d01f1 1753static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1754
e7c27a73 1755static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1756
6e70227d 1757static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1758 struct dwarf2_cu *);
1759
bf6af496 1760static struct die_info *read_die_and_siblings_1
d521ce57 1761 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1762 struct die_info *);
639d11d3 1763
dee91e82 1764static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1765 const gdb_byte *info_ptr,
1766 const gdb_byte **new_info_ptr,
639d11d3
DC
1767 struct die_info *parent);
1768
d521ce57
TT
1769static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1770 struct die_info **, const gdb_byte *,
1771 int *, int);
3019eac3 1772
d521ce57
TT
1773static const gdb_byte *read_full_die (const struct die_reader_specs *,
1774 struct die_info **, const gdb_byte *,
1775 int *);
93311388 1776
e7c27a73 1777static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1778
15d034d0
TT
1779static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1780 struct obstack *);
71c25dea 1781
15d034d0 1782static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1783
15d034d0 1784static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1785 struct die_info *die,
1786 struct dwarf2_cu *cu);
1787
ca69b9e6
DE
1788static const char *dwarf2_physname (const char *name, struct die_info *die,
1789 struct dwarf2_cu *cu);
1790
e142c38c 1791static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1792 struct dwarf2_cu **);
9219021c 1793
f39c6ffd 1794static const char *dwarf_tag_name (unsigned int);
c906108c 1795
f39c6ffd 1796static const char *dwarf_attr_name (unsigned int);
c906108c 1797
a084a2a6
AT
1798static const char *dwarf_unit_type_name (int unit_type);
1799
f39c6ffd 1800static const char *dwarf_form_name (unsigned int);
c906108c 1801
a121b7c1 1802static const char *dwarf_bool_name (unsigned int);
c906108c 1803
f39c6ffd 1804static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1805
f9aca02d 1806static struct die_info *sibling_die (struct die_info *);
c906108c 1807
d97bc12b
DE
1808static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1809
1810static void dump_die_for_error (struct die_info *);
1811
1812static void dump_die_1 (struct ui_file *, int level, int max_level,
1813 struct die_info *);
c906108c 1814
d97bc12b 1815/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1816
51545339 1817static void store_in_ref_table (struct die_info *,
10b3939b 1818 struct dwarf2_cu *);
c906108c 1819
ff39bb5e 1820static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1821
ff39bb5e 1822static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1823
348e048f 1824static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1825 const struct attribute *,
348e048f
DE
1826 struct dwarf2_cu **);
1827
10b3939b 1828static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1829 const struct attribute *,
f2f0e013 1830 struct dwarf2_cu **);
c906108c 1831
348e048f 1832static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1833 const struct attribute *,
348e048f
DE
1834 struct dwarf2_cu **);
1835
ac9ec31b
DE
1836static struct type *get_signatured_type (struct die_info *, ULONGEST,
1837 struct dwarf2_cu *);
1838
1839static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1840 const struct attribute *,
ac9ec31b
DE
1841 struct dwarf2_cu *);
1842
e5fe5e75 1843static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1844
52dc124a 1845static void read_signatured_type (struct signatured_type *);
348e048f 1846
63e43d3a
PMR
1847static int attr_to_dynamic_prop (const struct attribute *attr,
1848 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1849 struct dynamic_prop *prop, struct type *type);
63e43d3a 1850
c906108c
SS
1851/* memory allocation interface */
1852
7b5a2f43 1853static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1854
b60c80d6 1855static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1856
43f3e411 1857static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1858
6e5a29e1 1859static int attr_form_is_block (const struct attribute *);
8e19ed76 1860
6e5a29e1 1861static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1862
6e5a29e1 1863static int attr_form_is_constant (const struct attribute *);
3690dd37 1864
6e5a29e1 1865static int attr_form_is_ref (const struct attribute *);
7771576e 1866
8cf6f0b1
TT
1867static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1868 struct dwarf2_loclist_baton *baton,
ff39bb5e 1869 const struct attribute *attr);
8cf6f0b1 1870
ff39bb5e 1871static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1872 struct symbol *sym,
f1e6e072
TT
1873 struct dwarf2_cu *cu,
1874 int is_block);
4c2df51b 1875
d521ce57
TT
1876static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1877 const gdb_byte *info_ptr,
1878 struct abbrev_info *abbrev);
4bb7a0a7 1879
72bf9492
DJ
1880static hashval_t partial_die_hash (const void *item);
1881
1882static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1883
ae038cb0 1884static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1885 (sect_offset sect_off, unsigned int offset_in_dwz,
1886 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1887
9816fde3 1888static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1889 struct die_info *comp_unit_die,
1890 enum language pretend_language);
93311388 1891
ed2dc618 1892static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1893
dee91e82 1894static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1895
f792889a
DJ
1896static struct type *set_die_type (struct die_info *, struct type *,
1897 struct dwarf2_cu *);
1c379e20 1898
ed2dc618 1899static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1900
ed2dc618 1901static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1902
58f0c718 1903static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1904 enum language);
10b3939b 1905
95554aad
TT
1906static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1907 enum language);
10b3939b 1908
f4dc4d17
DE
1909static void process_full_type_unit (struct dwarf2_per_cu_data *,
1910 enum language);
1911
10b3939b
DJ
1912static void dwarf2_add_dependence (struct dwarf2_cu *,
1913 struct dwarf2_per_cu_data *);
1914
ae038cb0
DJ
1915static void dwarf2_mark (struct dwarf2_cu *);
1916
1917static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1918
b64f50a1 1919static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1920 struct dwarf2_per_cu_data *);
673bfd45 1921
f792889a 1922static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1923
95554aad
TT
1924static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1925 enum language pretend_language);
1926
ed2dc618 1927static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1928
9a49df9d
AB
1929static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1930static struct type *dwarf2_per_cu_addr_sized_int_type
1931 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1932static struct type *dwarf2_per_cu_int_type
1933 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1934 bool unsigned_p);
9a49df9d 1935
b303c6f6
AB
1936/* Class, the destructor of which frees all allocated queue entries. This
1937 will only have work to do if an error was thrown while processing the
1938 dwarf. If no error was thrown then the queue entries should have all
1939 been processed, and freed, as we went along. */
1940
1941class dwarf2_queue_guard
1942{
1943public:
1944 dwarf2_queue_guard () = default;
1945
1946 /* Free any entries remaining on the queue. There should only be
1947 entries left if we hit an error while processing the dwarf. */
1948 ~dwarf2_queue_guard ()
1949 {
1950 struct dwarf2_queue_item *item, *last;
1951
1952 item = dwarf2_queue;
1953 while (item)
1954 {
1955 /* Anything still marked queued is likely to be in an
1956 inconsistent state, so discard it. */
1957 if (item->per_cu->queued)
1958 {
1959 if (item->per_cu->cu != NULL)
1960 free_one_cached_comp_unit (item->per_cu);
1961 item->per_cu->queued = 0;
1962 }
1963
1964 last = item;
1965 item = item->next;
1966 xfree (last);
1967 }
1968
1969 dwarf2_queue = dwarf2_queue_tail = NULL;
1970 }
1971};
1972
d721ba37
PA
1973/* The return type of find_file_and_directory. Note, the enclosed
1974 string pointers are only valid while this object is valid. */
1975
1976struct file_and_directory
1977{
1978 /* The filename. This is never NULL. */
1979 const char *name;
1980
1981 /* The compilation directory. NULL if not known. If we needed to
1982 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1983 points directly to the DW_AT_comp_dir string attribute owned by
1984 the obstack that owns the DIE. */
1985 const char *comp_dir;
1986
1987 /* If we needed to build a new string for comp_dir, this is what
1988 owns the storage. */
1989 std::string comp_dir_storage;
1990};
1991
1992static file_and_directory find_file_and_directory (struct die_info *die,
1993 struct dwarf2_cu *cu);
9291a0cd
TT
1994
1995static char *file_full_name (int file, struct line_header *lh,
1996 const char *comp_dir);
1997
43988095
JK
1998/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1999enum class rcuh_kind { COMPILE, TYPE };
2000
d521ce57 2001static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
2002 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2003 struct comp_unit_head *header,
36586728 2004 struct dwarf2_section_info *section,
d521ce57 2005 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 2006 rcuh_kind section_kind);
36586728 2007
673bfd45 2008static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2009
3019eac3
DE
2010static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2011
57d63ce2 2012static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2013 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2014 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2015 ULONGEST signature, int is_debug_types);
a2ce51a0 2016
ed2dc618
SM
2017static struct dwp_file *get_dwp_file
2018 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2019
3019eac3 2020static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2021 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2022
2023static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2024 (struct signatured_type *, const char *, const char *);
3019eac3 2025
89e63ee4
DE
2026static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2027
263db9a1
TT
2028/* A unique pointer to a dwo_file. */
2029
51ac9db5 2030typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2031
ed2dc618 2032static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2033
1b80a9fa 2034static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2035
2036static void free_line_header_voidp (void *arg);
4390d890
DE
2037\f
2038/* Various complaints about symbol reading that don't abort the process. */
2039
2040static void
2041dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2042{
b98664d3 2043 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2044}
2045
2046static void
2047dwarf2_debug_line_missing_file_complaint (void)
2048{
b98664d3 2049 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2050}
2051
2052static void
2053dwarf2_debug_line_missing_end_sequence_complaint (void)
2054{
b98664d3 2055 complaint (_(".debug_line section has line "
4390d890
DE
2056 "program sequence without an end"));
2057}
2058
2059static void
2060dwarf2_complex_location_expr_complaint (void)
2061{
b98664d3 2062 complaint (_("location expression too complex"));
4390d890
DE
2063}
2064
2065static void
2066dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2067 int arg3)
2068{
b98664d3 2069 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2070 arg1, arg2, arg3);
2071}
2072
2073static void
2074dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2075{
b98664d3 2076 complaint (_("debug info runs off end of %s section"
4390d890 2077 " [in module %s]"),
a32a8923
DE
2078 get_section_name (section),
2079 get_section_file_name (section));
4390d890 2080}
1b80a9fa 2081
4390d890
DE
2082static void
2083dwarf2_macro_malformed_definition_complaint (const char *arg1)
2084{
b98664d3 2085 complaint (_("macro debug info contains a "
4390d890
DE
2086 "malformed macro definition:\n`%s'"),
2087 arg1);
2088}
2089
2090static void
2091dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2092{
b98664d3 2093 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2094 arg1, arg2);
2095}
527f3840
JK
2096
2097/* Hash function for line_header_hash. */
2098
2099static hashval_t
2100line_header_hash (const struct line_header *ofs)
2101{
9c541725 2102 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2103}
2104
2105/* Hash function for htab_create_alloc_ex for line_header_hash. */
2106
2107static hashval_t
2108line_header_hash_voidp (const void *item)
2109{
9a3c8263 2110 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2111
2112 return line_header_hash (ofs);
2113}
2114
2115/* Equality function for line_header_hash. */
2116
2117static int
2118line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2119{
9a3c8263
SM
2120 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2121 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2122
9c541725 2123 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2124 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2125}
2126
4390d890 2127\f
9291a0cd 2128
31aa7e4e
JB
2129/* Read the given attribute value as an address, taking the attribute's
2130 form into account. */
2131
2132static CORE_ADDR
2133attr_value_as_address (struct attribute *attr)
2134{
2135 CORE_ADDR addr;
2136
336d760d
AT
2137 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2138 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2139 {
2140 /* Aside from a few clearly defined exceptions, attributes that
2141 contain an address must always be in DW_FORM_addr form.
2142 Unfortunately, some compilers happen to be violating this
2143 requirement by encoding addresses using other forms, such
2144 as DW_FORM_data4 for example. For those broken compilers,
2145 we try to do our best, without any guarantee of success,
2146 to interpret the address correctly. It would also be nice
2147 to generate a complaint, but that would require us to maintain
2148 a list of legitimate cases where a non-address form is allowed,
2149 as well as update callers to pass in at least the CU's DWARF
2150 version. This is more overhead than what we're willing to
2151 expand for a pretty rare case. */
2152 addr = DW_UNSND (attr);
2153 }
2154 else
2155 addr = DW_ADDR (attr);
2156
2157 return addr;
2158}
2159
330cdd98
PA
2160/* See declaration. */
2161
2162dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2163 const dwarf2_debug_sections *names,
2164 bool can_copy_)
2165 : objfile (objfile_),
2166 can_copy (can_copy_)
330cdd98
PA
2167{
2168 if (names == NULL)
2169 names = &dwarf2_elf_names;
2170
2171 bfd *obfd = objfile->obfd;
2172
2173 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2174 locate_sections (obfd, sec, *names);
2175}
2176
2177dwarf2_per_objfile::~dwarf2_per_objfile ()
2178{
2179 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2180 free_cached_comp_units ();
2181
2182 if (quick_file_names_table)
2183 htab_delete (quick_file_names_table);
2184
2185 if (line_header_hash)
2186 htab_delete (line_header_hash);
2187
b76e467d 2188 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2189 per_cu->imported_symtabs_free ();
fc8e7e75 2190
b2bdb8cf 2191 for (signatured_type *sig_type : all_type_units)
ae640021 2192 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2193
330cdd98
PA
2194 /* Everything else should be on the objfile obstack. */
2195}
2196
2197/* See declaration. */
2198
2199void
2200dwarf2_per_objfile::free_cached_comp_units ()
2201{
2202 dwarf2_per_cu_data *per_cu = read_in_chain;
2203 dwarf2_per_cu_data **last_chain = &read_in_chain;
2204 while (per_cu != NULL)
2205 {
2206 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2207
fcd3b13d 2208 delete per_cu->cu;
330cdd98
PA
2209 *last_chain = next_cu;
2210 per_cu = next_cu;
2211 }
2212}
2213
11ed8cad
TT
2214/* A helper class that calls free_cached_comp_units on
2215 destruction. */
2216
2217class free_cached_comp_units
2218{
2219public:
2220
2221 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2222 : m_per_objfile (per_objfile)
2223 {
2224 }
2225
2226 ~free_cached_comp_units ()
2227 {
2228 m_per_objfile->free_cached_comp_units ();
2229 }
2230
2231 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2232
2233private:
2234
2235 dwarf2_per_objfile *m_per_objfile;
2236};
2237
c906108c 2238/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2239 information and return true if we have enough to do something.
2240 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2241 ELF names are used. CAN_COPY is true for formats where symbol
2242 interposition is possible and so symbol values must follow copy
2243 relocation rules. */
c906108c
SS
2244
2245int
251d32d9 2246dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2247 const struct dwarf2_debug_sections *names,
2248 bool can_copy)
c906108c 2249{
97cbe998
SDJ
2250 if (objfile->flags & OBJF_READNEVER)
2251 return 0;
2252
ed2dc618
SM
2253 struct dwarf2_per_objfile *dwarf2_per_objfile
2254 = get_dwarf2_per_objfile (objfile);
2255
2256 if (dwarf2_per_objfile == NULL)
5bfd760d 2257 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2258 names,
2259 can_copy);
5bfd760d 2260
73869dc2 2261 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2262 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2263 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2264 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2265}
2266
2267/* Return the containing section of virtual section SECTION. */
2268
2269static struct dwarf2_section_info *
2270get_containing_section (const struct dwarf2_section_info *section)
2271{
2272 gdb_assert (section->is_virtual);
2273 return section->s.containing_section;
c906108c
SS
2274}
2275
a32a8923
DE
2276/* Return the bfd owner of SECTION. */
2277
2278static struct bfd *
2279get_section_bfd_owner (const struct dwarf2_section_info *section)
2280{
73869dc2
DE
2281 if (section->is_virtual)
2282 {
2283 section = get_containing_section (section);
2284 gdb_assert (!section->is_virtual);
2285 }
049412e3 2286 return section->s.section->owner;
a32a8923
DE
2287}
2288
2289/* Return the bfd section of SECTION.
2290 Returns NULL if the section is not present. */
2291
2292static asection *
2293get_section_bfd_section (const struct dwarf2_section_info *section)
2294{
73869dc2
DE
2295 if (section->is_virtual)
2296 {
2297 section = get_containing_section (section);
2298 gdb_assert (!section->is_virtual);
2299 }
049412e3 2300 return section->s.section;
a32a8923
DE
2301}
2302
2303/* Return the name of SECTION. */
2304
2305static const char *
2306get_section_name (const struct dwarf2_section_info *section)
2307{
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
fd361982 2311 return bfd_section_name (sectp);
a32a8923
DE
2312}
2313
2314/* Return the name of the file SECTION is in. */
2315
2316static const char *
2317get_section_file_name (const struct dwarf2_section_info *section)
2318{
2319 bfd *abfd = get_section_bfd_owner (section);
2320
2321 return bfd_get_filename (abfd);
2322}
2323
2324/* Return the id of SECTION.
2325 Returns 0 if SECTION doesn't exist. */
2326
2327static int
2328get_section_id (const struct dwarf2_section_info *section)
2329{
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 if (sectp == NULL)
2333 return 0;
2334 return sectp->id;
2335}
2336
2337/* Return the flags of SECTION.
73869dc2 2338 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2339
2340static int
2341get_section_flags (const struct dwarf2_section_info *section)
2342{
2343 asection *sectp = get_section_bfd_section (section);
2344
2345 gdb_assert (sectp != NULL);
fd361982 2346 return bfd_section_flags (sectp);
a32a8923
DE
2347}
2348
251d32d9
TG
2349/* When loading sections, we look either for uncompressed section or for
2350 compressed section names. */
233a11ab
CS
2351
2352static int
251d32d9
TG
2353section_is_p (const char *section_name,
2354 const struct dwarf2_section_names *names)
233a11ab 2355{
251d32d9
TG
2356 if (names->normal != NULL
2357 && strcmp (section_name, names->normal) == 0)
2358 return 1;
2359 if (names->compressed != NULL
2360 && strcmp (section_name, names->compressed) == 0)
2361 return 1;
2362 return 0;
233a11ab
CS
2363}
2364
330cdd98 2365/* See declaration. */
c906108c 2366
330cdd98
PA
2367void
2368dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2369 const dwarf2_debug_sections &names)
c906108c 2370{
fd361982 2371 flagword aflag = bfd_section_flags (sectp);
251d32d9 2372
dc7650b8
JK
2373 if ((aflag & SEC_HAS_CONTENTS) == 0)
2374 {
2375 }
950b7495
KS
2376 else if (elf_section_data (sectp)->this_hdr.sh_size
2377 > bfd_get_file_size (abfd))
2378 {
2379 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2380 warning (_("Discarding section %s which has a section size (%s"
2381 ") larger than the file size [in module %s]"),
2382 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2383 bfd_get_filename (abfd));
2384 }
330cdd98 2385 else if (section_is_p (sectp->name, &names.info))
c906108c 2386 {
330cdd98 2387 this->info.s.section = sectp;
fd361982 2388 this->info.size = bfd_section_size (sectp);
c906108c 2389 }
330cdd98 2390 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2391 {
330cdd98 2392 this->abbrev.s.section = sectp;
fd361982 2393 this->abbrev.size = bfd_section_size (sectp);
c906108c 2394 }
330cdd98 2395 else if (section_is_p (sectp->name, &names.line))
c906108c 2396 {
330cdd98 2397 this->line.s.section = sectp;
fd361982 2398 this->line.size = bfd_section_size (sectp);
c906108c 2399 }
330cdd98 2400 else if (section_is_p (sectp->name, &names.loc))
c906108c 2401 {
330cdd98 2402 this->loc.s.section = sectp;
fd361982 2403 this->loc.size = bfd_section_size (sectp);
c906108c 2404 }
330cdd98 2405 else if (section_is_p (sectp->name, &names.loclists))
43988095 2406 {
330cdd98 2407 this->loclists.s.section = sectp;
fd361982 2408 this->loclists.size = bfd_section_size (sectp);
43988095 2409 }
330cdd98 2410 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2411 {
330cdd98 2412 this->macinfo.s.section = sectp;
fd361982 2413 this->macinfo.size = bfd_section_size (sectp);
c906108c 2414 }
330cdd98 2415 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2416 {
330cdd98 2417 this->macro.s.section = sectp;
fd361982 2418 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2419 }
330cdd98 2420 else if (section_is_p (sectp->name, &names.str))
c906108c 2421 {
330cdd98 2422 this->str.s.section = sectp;
fd361982 2423 this->str.size = bfd_section_size (sectp);
c906108c 2424 }
18a8505e
AT
2425 else if (section_is_p (sectp->name, &names.str_offsets))
2426 {
2427 this->str_offsets.s.section = sectp;
2428 this->str_offsets.size = bfd_section_size (sectp);
2429 }
330cdd98 2430 else if (section_is_p (sectp->name, &names.line_str))
43988095 2431 {
330cdd98 2432 this->line_str.s.section = sectp;
fd361982 2433 this->line_str.size = bfd_section_size (sectp);
43988095 2434 }
330cdd98 2435 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2436 {
330cdd98 2437 this->addr.s.section = sectp;
fd361982 2438 this->addr.size = bfd_section_size (sectp);
3019eac3 2439 }
330cdd98 2440 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2441 {
330cdd98 2442 this->frame.s.section = sectp;
fd361982 2443 this->frame.size = bfd_section_size (sectp);
b6af0555 2444 }
330cdd98 2445 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2446 {
330cdd98 2447 this->eh_frame.s.section = sectp;
fd361982 2448 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2449 }
330cdd98 2450 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2451 {
330cdd98 2452 this->ranges.s.section = sectp;
fd361982 2453 this->ranges.size = bfd_section_size (sectp);
af34e669 2454 }
330cdd98 2455 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2456 {
330cdd98 2457 this->rnglists.s.section = sectp;
fd361982 2458 this->rnglists.size = bfd_section_size (sectp);
43988095 2459 }
330cdd98 2460 else if (section_is_p (sectp->name, &names.types))
348e048f 2461 {
8b70b953
TT
2462 struct dwarf2_section_info type_section;
2463
2464 memset (&type_section, 0, sizeof (type_section));
049412e3 2465 type_section.s.section = sectp;
fd361982 2466 type_section.size = bfd_section_size (sectp);
8b70b953 2467
fd5866f6 2468 this->types.push_back (type_section);
348e048f 2469 }
330cdd98 2470 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2471 {
330cdd98 2472 this->gdb_index.s.section = sectp;
fd361982 2473 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2474 }
927aa2e7
JK
2475 else if (section_is_p (sectp->name, &names.debug_names))
2476 {
2477 this->debug_names.s.section = sectp;
fd361982 2478 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2479 }
2480 else if (section_is_p (sectp->name, &names.debug_aranges))
2481 {
2482 this->debug_aranges.s.section = sectp;
fd361982 2483 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2484 }
dce234bc 2485
fd361982
AM
2486 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2487 && bfd_section_vma (sectp) == 0)
330cdd98 2488 this->has_section_at_zero = true;
c906108c
SS
2489}
2490
fceca515
DE
2491/* A helper function that decides whether a section is empty,
2492 or not present. */
9e0ac564
TT
2493
2494static int
19ac8c2e 2495dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2496{
73869dc2
DE
2497 if (section->is_virtual)
2498 return section->size == 0;
049412e3 2499 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2500}
2501
cd4fb1b2 2502/* See dwarf2read.h. */
c906108c 2503
cd4fb1b2
SM
2504void
2505dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2506{
a32a8923 2507 asection *sectp;
3019eac3 2508 bfd *abfd;
dce234bc 2509 gdb_byte *buf, *retbuf;
c906108c 2510
be391dca
TT
2511 if (info->readin)
2512 return;
dce234bc 2513 info->buffer = NULL;
dc4ccb6f 2514 info->readin = true;
188dd5d6 2515
9e0ac564 2516 if (dwarf2_section_empty_p (info))
dce234bc 2517 return;
c906108c 2518
a32a8923 2519 sectp = get_section_bfd_section (info);
3019eac3 2520
73869dc2
DE
2521 /* If this is a virtual section we need to read in the real one first. */
2522 if (info->is_virtual)
2523 {
2524 struct dwarf2_section_info *containing_section =
2525 get_containing_section (info);
2526
2527 gdb_assert (sectp != NULL);
2528 if ((sectp->flags & SEC_RELOC) != 0)
2529 {
2530 error (_("Dwarf Error: DWP format V2 with relocations is not"
2531 " supported in section %s [in module %s]"),
2532 get_section_name (info), get_section_file_name (info));
2533 }
2534 dwarf2_read_section (objfile, containing_section);
2535 /* Other code should have already caught virtual sections that don't
2536 fit. */
2537 gdb_assert (info->virtual_offset + info->size
2538 <= containing_section->size);
2539 /* If the real section is empty or there was a problem reading the
2540 section we shouldn't get here. */
2541 gdb_assert (containing_section->buffer != NULL);
2542 info->buffer = containing_section->buffer + info->virtual_offset;
2543 return;
2544 }
2545
4bf44c1c
TT
2546 /* If the section has relocations, we must read it ourselves.
2547 Otherwise we attach it to the BFD. */
2548 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2549 {
d521ce57 2550 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2551 return;
dce234bc 2552 }
dce234bc 2553
224c3ddb 2554 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2555 info->buffer = buf;
dce234bc
PP
2556
2557 /* When debugging .o files, we may need to apply relocations; see
2558 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2559 We never compress sections in .o files, so we only need to
2560 try this when the section is not compressed. */
ac8035ab 2561 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2562 if (retbuf != NULL)
2563 {
2564 info->buffer = retbuf;
2565 return;
2566 }
2567
a32a8923
DE
2568 abfd = get_section_bfd_owner (info);
2569 gdb_assert (abfd != NULL);
2570
dce234bc
PP
2571 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2572 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2573 {
2574 error (_("Dwarf Error: Can't read DWARF data"
2575 " in section %s [in module %s]"),
fd361982 2576 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2577 }
dce234bc
PP
2578}
2579
9e0ac564
TT
2580/* A helper function that returns the size of a section in a safe way.
2581 If you are positive that the section has been read before using the
2582 size, then it is safe to refer to the dwarf2_section_info object's
2583 "size" field directly. In other cases, you must call this
2584 function, because for compressed sections the size field is not set
2585 correctly until the section has been read. */
2586
2587static bfd_size_type
2588dwarf2_section_size (struct objfile *objfile,
2589 struct dwarf2_section_info *info)
2590{
2591 if (!info->readin)
2592 dwarf2_read_section (objfile, info);
2593 return info->size;
2594}
2595
dce234bc 2596/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2597 SECTION_NAME. */
af34e669 2598
dce234bc 2599void
3017a003
TG
2600dwarf2_get_section_info (struct objfile *objfile,
2601 enum dwarf2_section_enum sect,
d521ce57 2602 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2603 bfd_size_type *sizep)
2604{
5bfd760d 2605 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2606 struct dwarf2_section_info *info;
a3b2a86b
TT
2607
2608 /* We may see an objfile without any DWARF, in which case we just
2609 return nothing. */
2610 if (data == NULL)
2611 {
2612 *sectp = NULL;
2613 *bufp = NULL;
2614 *sizep = 0;
2615 return;
2616 }
3017a003
TG
2617 switch (sect)
2618 {
2619 case DWARF2_DEBUG_FRAME:
2620 info = &data->frame;
2621 break;
2622 case DWARF2_EH_FRAME:
2623 info = &data->eh_frame;
2624 break;
2625 default:
2626 gdb_assert_not_reached ("unexpected section");
2627 }
dce234bc 2628
9e0ac564 2629 dwarf2_read_section (objfile, info);
dce234bc 2630
a32a8923 2631 *sectp = get_section_bfd_section (info);
dce234bc
PP
2632 *bufp = info->buffer;
2633 *sizep = info->size;
2634}
2635
36586728
TT
2636/* A helper function to find the sections for a .dwz file. */
2637
2638static void
2639locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2640{
9a3c8263 2641 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2642
2643 /* Note that we only support the standard ELF names, because .dwz
2644 is ELF-only (at the time of writing). */
2645 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2646 {
049412e3 2647 dwz_file->abbrev.s.section = sectp;
fd361982 2648 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2649 }
2650 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2651 {
049412e3 2652 dwz_file->info.s.section = sectp;
fd361982 2653 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2654 }
2655 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2656 {
049412e3 2657 dwz_file->str.s.section = sectp;
fd361982 2658 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2659 }
2660 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2661 {
049412e3 2662 dwz_file->line.s.section = sectp;
fd361982 2663 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2664 }
2665 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2666 {
049412e3 2667 dwz_file->macro.s.section = sectp;
fd361982 2668 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2669 }
2ec9a5e0
TT
2670 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2671 {
049412e3 2672 dwz_file->gdb_index.s.section = sectp;
fd361982 2673 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2674 }
927aa2e7
JK
2675 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2676 {
2677 dwz_file->debug_names.s.section = sectp;
fd361982 2678 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2679 }
36586728
TT
2680}
2681
c4973306 2682/* See dwarf2read.h. */
36586728 2683
c4973306 2684struct dwz_file *
ed2dc618 2685dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2686{
36586728 2687 const char *filename;
acd13123 2688 bfd_size_type buildid_len_arg;
dc294be5
TT
2689 size_t buildid_len;
2690 bfd_byte *buildid;
36586728
TT
2691
2692 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2693 return dwarf2_per_objfile->dwz_file.get ();
36586728 2694
4db1a1dc 2695 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2696 gdb::unique_xmalloc_ptr<char> data
2697 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2698 &buildid_len_arg, &buildid));
4db1a1dc
TT
2699 if (data == NULL)
2700 {
2701 if (bfd_get_error () == bfd_error_no_error)
2702 return NULL;
2703 error (_("could not read '.gnu_debugaltlink' section: %s"),
2704 bfd_errmsg (bfd_get_error ()));
2705 }
791afaa2
TT
2706
2707 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2708
acd13123
TT
2709 buildid_len = (size_t) buildid_len_arg;
2710
791afaa2 2711 filename = data.get ();
d721ba37
PA
2712
2713 std::string abs_storage;
36586728
TT
2714 if (!IS_ABSOLUTE_PATH (filename))
2715 {
14278e1f
TT
2716 gdb::unique_xmalloc_ptr<char> abs
2717 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2718
14278e1f 2719 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2720 filename = abs_storage.c_str ();
36586728
TT
2721 }
2722
dc294be5
TT
2723 /* First try the file name given in the section. If that doesn't
2724 work, try to use the build-id instead. */
192b62ce 2725 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2726 if (dwz_bfd != NULL)
36586728 2727 {
192b62ce 2728 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2729 dwz_bfd.reset (nullptr);
36586728
TT
2730 }
2731
dc294be5
TT
2732 if (dwz_bfd == NULL)
2733 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2734
2735 if (dwz_bfd == NULL)
2736 error (_("could not find '.gnu_debugaltlink' file for %s"),
2737 objfile_name (dwarf2_per_objfile->objfile));
2738
7ff8cb8c
TT
2739 std::unique_ptr<struct dwz_file> result
2740 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2741
7ff8cb8c
TT
2742 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2743 result.get ());
36586728 2744
7ff8cb8c
TT
2745 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2746 result->dwz_bfd.get ());
2747 dwarf2_per_objfile->dwz_file = std::move (result);
2748 return dwarf2_per_objfile->dwz_file.get ();
36586728 2749}
9291a0cd 2750\f
7b9f3c50
DE
2751/* DWARF quick_symbols_functions support. */
2752
2753/* TUs can share .debug_line entries, and there can be a lot more TUs than
2754 unique line tables, so we maintain a separate table of all .debug_line
2755 derived entries to support the sharing.
2756 All the quick functions need is the list of file names. We discard the
2757 line_header when we're done and don't need to record it here. */
2758struct quick_file_names
2759{
094b34ac
DE
2760 /* The data used to construct the hash key. */
2761 struct stmt_list_hash hash;
7b9f3c50
DE
2762
2763 /* The number of entries in file_names, real_names. */
2764 unsigned int num_file_names;
2765
2766 /* The file names from the line table, after being run through
2767 file_full_name. */
2768 const char **file_names;
2769
2770 /* The file names from the line table after being run through
2771 gdb_realpath. These are computed lazily. */
2772 const char **real_names;
2773};
2774
2775/* When using the index (and thus not using psymtabs), each CU has an
2776 object of this type. This is used to hold information needed by
2777 the various "quick" methods. */
2778struct dwarf2_per_cu_quick_data
2779{
2780 /* The file table. This can be NULL if there was no file table
2781 or it's currently not read in.
2782 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2783 struct quick_file_names *file_names;
2784
2785 /* The corresponding symbol table. This is NULL if symbols for this
2786 CU have not yet been read. */
43f3e411 2787 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2788
2789 /* A temporary mark bit used when iterating over all CUs in
2790 expand_symtabs_matching. */
2791 unsigned int mark : 1;
2792
2793 /* True if we've tried to read the file table and found there isn't one.
2794 There will be no point in trying to read it again next time. */
2795 unsigned int no_file_data : 1;
2796};
2797
094b34ac
DE
2798/* Utility hash function for a stmt_list_hash. */
2799
2800static hashval_t
2801hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2802{
2803 hashval_t v = 0;
2804
2805 if (stmt_list_hash->dwo_unit != NULL)
2806 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2807 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2808 return v;
2809}
2810
2811/* Utility equality function for a stmt_list_hash. */
2812
2813static int
2814eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2815 const struct stmt_list_hash *rhs)
2816{
2817 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2818 return 0;
2819 if (lhs->dwo_unit != NULL
2820 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2821 return 0;
2822
9c541725 2823 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2824}
2825
7b9f3c50
DE
2826/* Hash function for a quick_file_names. */
2827
2828static hashval_t
2829hash_file_name_entry (const void *e)
2830{
9a3c8263
SM
2831 const struct quick_file_names *file_data
2832 = (const struct quick_file_names *) e;
7b9f3c50 2833
094b34ac 2834 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2835}
2836
2837/* Equality function for a quick_file_names. */
2838
2839static int
2840eq_file_name_entry (const void *a, const void *b)
2841{
9a3c8263
SM
2842 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2843 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2844
094b34ac 2845 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2846}
2847
2848/* Delete function for a quick_file_names. */
2849
2850static void
2851delete_file_name_entry (void *e)
2852{
9a3c8263 2853 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2854 int i;
2855
2856 for (i = 0; i < file_data->num_file_names; ++i)
2857 {
2858 xfree ((void*) file_data->file_names[i]);
2859 if (file_data->real_names)
2860 xfree ((void*) file_data->real_names[i]);
2861 }
2862
2863 /* The space for the struct itself lives on objfile_obstack,
2864 so we don't free it here. */
2865}
2866
2867/* Create a quick_file_names hash table. */
2868
2869static htab_t
2870create_quick_file_names_table (unsigned int nr_initial_entries)
2871{
2872 return htab_create_alloc (nr_initial_entries,
2873 hash_file_name_entry, eq_file_name_entry,
2874 delete_file_name_entry, xcalloc, xfree);
2875}
9291a0cd 2876
918dd910
JK
2877/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2878 have to be created afterwards. You should call age_cached_comp_units after
2879 processing PER_CU->CU. dw2_setup must have been already called. */
2880
2881static void
58f0c718 2882load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2883{
3019eac3 2884 if (per_cu->is_debug_types)
e5fe5e75 2885 load_full_type_unit (per_cu);
918dd910 2886 else
58f0c718 2887 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2888
cc12ce38
DE
2889 if (per_cu->cu == NULL)
2890 return; /* Dummy CU. */
2dc860c0
DE
2891
2892 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2893}
2894
a0f42c21 2895/* Read in the symbols for PER_CU. */
2fdf6df6 2896
9291a0cd 2897static void
58f0c718 2898dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2899{
ed2dc618 2900 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2901
f4dc4d17
DE
2902 /* Skip type_unit_groups, reading the type units they contain
2903 is handled elsewhere. */
2904 if (IS_TYPE_UNIT_GROUP (per_cu))
2905 return;
2906
b303c6f6
AB
2907 /* The destructor of dwarf2_queue_guard frees any entries left on
2908 the queue. After this point we're guaranteed to leave this function
2909 with the dwarf queue empty. */
2910 dwarf2_queue_guard q_guard;
9291a0cd 2911
95554aad 2912 if (dwarf2_per_objfile->using_index
43f3e411 2913 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2914 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2915 {
2916 queue_comp_unit (per_cu, language_minimal);
58f0c718 2917 load_cu (per_cu, skip_partial);
89e63ee4
DE
2918
2919 /* If we just loaded a CU from a DWO, and we're working with an index
2920 that may badly handle TUs, load all the TUs in that DWO as well.
2921 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2922 if (!per_cu->is_debug_types
cc12ce38 2923 && per_cu->cu != NULL
89e63ee4
DE
2924 && per_cu->cu->dwo_unit != NULL
2925 && dwarf2_per_objfile->index_table != NULL
2926 && dwarf2_per_objfile->index_table->version <= 7
2927 /* DWP files aren't supported yet. */
ed2dc618 2928 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2929 queue_and_load_all_dwo_tus (per_cu);
95554aad 2930 }
9291a0cd 2931
ed2dc618 2932 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2933
2934 /* Age the cache, releasing compilation units that have not
2935 been used recently. */
ed2dc618 2936 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2937}
2938
2939/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2940 the objfile from which this CU came. Returns the resulting symbol
2941 table. */
2fdf6df6 2942
43f3e411 2943static struct compunit_symtab *
58f0c718 2944dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2945{
ed2dc618
SM
2946 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2947
95554aad 2948 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2949 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2950 {
11ed8cad 2951 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2952 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2953 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2954 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2955 }
f194fefb 2956
43f3e411 2957 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2958}
2959
ff4c9fec 2960/* See declaration. */
f4dc4d17 2961
ff4c9fec
SM
2962dwarf2_per_cu_data *
2963dwarf2_per_objfile::get_cutu (int index)
2964{
b76e467d 2965 if (index >= this->all_comp_units.size ())
ff4c9fec 2966 {
b76e467d 2967 index -= this->all_comp_units.size ();
b2bdb8cf 2968 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2969 return &this->all_type_units[index]->per_cu;
2970 }
f4dc4d17 2971
ff4c9fec
SM
2972 return this->all_comp_units[index];
2973}
f4dc4d17 2974
ff4c9fec 2975/* See declaration. */
2fdf6df6 2976
ff4c9fec
SM
2977dwarf2_per_cu_data *
2978dwarf2_per_objfile::get_cu (int index)
1fd400ff 2979{
b76e467d 2980 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2981
ff4c9fec 2982 return this->all_comp_units[index];
f4dc4d17
DE
2983}
2984
ff4c9fec 2985/* See declaration. */
f4dc4d17 2986
ff4c9fec
SM
2987signatured_type *
2988dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2989{
b2bdb8cf 2990 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2991
ff4c9fec 2992 return this->all_type_units[index];
1fd400ff
TT
2993}
2994
4b514bc8
JK
2995/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2996 objfile_obstack, and constructed with the specified field
2997 values. */
2998
2999static dwarf2_per_cu_data *
ed2dc618 3000create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
3001 struct dwarf2_section_info *section,
3002 int is_dwz,
3003 sect_offset sect_off, ULONGEST length)
3004{
ed2dc618 3005 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
3006 dwarf2_per_cu_data *the_cu
3007 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3008 struct dwarf2_per_cu_data);
3009 the_cu->sect_off = sect_off;
3010 the_cu->length = length;
e3b94546 3011 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
3012 the_cu->section = section;
3013 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3014 struct dwarf2_per_cu_quick_data);
3015 the_cu->is_dwz = is_dwz;
3016 return the_cu;
3017}
3018
2ec9a5e0
TT
3019/* A helper for create_cus_from_index that handles a given list of
3020 CUs. */
2fdf6df6 3021
74a0d9f6 3022static void
12359b5e 3023create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3024 const gdb_byte *cu_list, offset_type n_elements,
3025 struct dwarf2_section_info *section,
b76e467d 3026 int is_dwz)
9291a0cd 3027{
12359b5e 3028 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3029 {
74a0d9f6 3030 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3031
3032 sect_offset sect_off
3033 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3034 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3035 cu_list += 2 * 8;
3036
b76e467d 3037 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3038 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3039 sect_off, length);
b76e467d 3040 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3041 }
9291a0cd
TT
3042}
3043
2ec9a5e0 3044/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3045 the CU objects for this objfile. */
2ec9a5e0 3046
74a0d9f6 3047static void
12359b5e 3048create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3049 const gdb_byte *cu_list, offset_type cu_list_elements,
3050 const gdb_byte *dwz_list, offset_type dwz_elements)
3051{
b76e467d
SM
3052 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3053 dwarf2_per_objfile->all_comp_units.reserve
3054 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3055
12359b5e 3056 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3057 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3058
3059 if (dwz_elements == 0)
74a0d9f6 3060 return;
2ec9a5e0 3061
12359b5e
SM
3062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3063 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3064 &dwz->info, 1);
2ec9a5e0
TT
3065}
3066
1fd400ff 3067/* Create the signatured type hash table from the index. */
673bfd45 3068
74a0d9f6 3069static void
12359b5e
SM
3070create_signatured_type_table_from_index
3071 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3072 struct dwarf2_section_info *section,
3073 const gdb_byte *bytes,
3074 offset_type elements)
1fd400ff 3075{
12359b5e 3076 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3077
b2bdb8cf
SM
3078 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3079 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3080
12359b5e 3081 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3082
12359b5e 3083 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3084 {
52dc124a 3085 struct signatured_type *sig_type;
9c541725 3086 ULONGEST signature;
1fd400ff 3087 void **slot;
9c541725 3088 cu_offset type_offset_in_tu;
1fd400ff 3089
74a0d9f6 3090 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3091 sect_offset sect_off
3092 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3093 type_offset_in_tu
3094 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3095 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3096 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3097 bytes += 3 * 8;
3098
52dc124a 3099 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3100 struct signatured_type);
52dc124a 3101 sig_type->signature = signature;
9c541725 3102 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3103 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3104 sig_type->per_cu.section = section;
9c541725 3105 sig_type->per_cu.sect_off = sect_off;
e3b94546 3106 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3107 sig_type->per_cu.v.quick
1fd400ff
TT
3108 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3109 struct dwarf2_per_cu_quick_data);
3110
52dc124a
DE
3111 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3112 *slot = sig_type;
1fd400ff 3113
b2bdb8cf 3114 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3115 }
3116
673bfd45 3117 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3118}
3119
927aa2e7
JK
3120/* Create the signatured type hash table from .debug_names. */
3121
3122static void
3123create_signatured_type_table_from_debug_names
ed2dc618 3124 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3125 const mapped_debug_names &map,
3126 struct dwarf2_section_info *section,
3127 struct dwarf2_section_info *abbrev_section)
3128{
ed2dc618
SM
3129 struct objfile *objfile = dwarf2_per_objfile->objfile;
3130
927aa2e7
JK
3131 dwarf2_read_section (objfile, section);
3132 dwarf2_read_section (objfile, abbrev_section);
3133
b2bdb8cf
SM
3134 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3135 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3136
3137 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3138
3139 for (uint32_t i = 0; i < map.tu_count; ++i)
3140 {
3141 struct signatured_type *sig_type;
927aa2e7 3142 void **slot;
927aa2e7
JK
3143
3144 sect_offset sect_off
3145 = (sect_offset) (extract_unsigned_integer
3146 (map.tu_table_reordered + i * map.offset_size,
3147 map.offset_size,
3148 map.dwarf5_byte_order));
3149
3150 comp_unit_head cu_header;
ed2dc618
SM
3151 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3152 abbrev_section,
927aa2e7
JK
3153 section->buffer + to_underlying (sect_off),
3154 rcuh_kind::TYPE);
3155
3156 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3157 struct signatured_type);
3158 sig_type->signature = cu_header.signature;
3159 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3160 sig_type->per_cu.is_debug_types = 1;
3161 sig_type->per_cu.section = section;
3162 sig_type->per_cu.sect_off = sect_off;
e3b94546 3163 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3164 sig_type->per_cu.v.quick
3165 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3166 struct dwarf2_per_cu_quick_data);
3167
3168 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3169 *slot = sig_type;
3170
b2bdb8cf 3171 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3172 }
3173
3174 dwarf2_per_objfile->signatured_types = sig_types_hash;
3175}
3176
9291a0cd
TT
3177/* Read the address map data from the mapped index, and use it to
3178 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3179
9291a0cd 3180static void
ed2dc618
SM
3181create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3182 struct mapped_index *index)
9291a0cd 3183{
ed2dc618 3184 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3186 const gdb_byte *iter, *end;
9291a0cd 3187 struct addrmap *mutable_map;
9291a0cd
TT
3188 CORE_ADDR baseaddr;
3189
8268c778
PA
3190 auto_obstack temp_obstack;
3191
9291a0cd
TT
3192 mutable_map = addrmap_create_mutable (&temp_obstack);
3193
f00a2de2
PA
3194 iter = index->address_table.data ();
3195 end = iter + index->address_table.size ();
9291a0cd 3196
b3b3bada 3197 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
3198
3199 while (iter < end)
3200 {
3201 ULONGEST hi, lo, cu_index;
3202 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3203 iter += 8;
3204 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3205 iter += 8;
3206 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3207 iter += 4;
f652bce2 3208
24a55014 3209 if (lo > hi)
f652bce2 3210 {
b98664d3 3211 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3212 hex_string (lo), hex_string (hi));
24a55014 3213 continue;
f652bce2 3214 }
24a55014 3215
b76e467d 3216 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3217 {
b98664d3 3218 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3219 (unsigned) cu_index);
24a55014 3220 continue;
f652bce2 3221 }
24a55014 3222
79748972
TT
3223 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3224 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3225 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3226 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3227 }
3228
d320c2b5 3229 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3230 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3231}
3232
927aa2e7
JK
3233/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3234 populate the objfile's psymtabs_addrmap. */
3235
3236static void
ed2dc618 3237create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3238 struct dwarf2_section_info *section)
3239{
ed2dc618 3240 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3241 bfd *abfd = objfile->obfd;
3242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 3243 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
3244
3245 auto_obstack temp_obstack;
3246 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3247
3248 std::unordered_map<sect_offset,
3249 dwarf2_per_cu_data *,
3250 gdb::hash_enum<sect_offset>>
3251 debug_info_offset_to_per_cu;
b76e467d 3252 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3253 {
927aa2e7
JK
3254 const auto insertpair
3255 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3256 if (!insertpair.second)
3257 {
3258 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3259 "debug_info_offset %s, ignoring .debug_aranges."),
3260 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3261 return;
3262 }
3263 }
3264
3265 dwarf2_read_section (objfile, section);
3266
3267 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3268
3269 const gdb_byte *addr = section->buffer;
3270
3271 while (addr < section->buffer + section->size)
3272 {
3273 const gdb_byte *const entry_addr = addr;
3274 unsigned int bytes_read;
3275
3276 const LONGEST entry_length = read_initial_length (abfd, addr,
3277 &bytes_read);
3278 addr += bytes_read;
3279
3280 const gdb_byte *const entry_end = addr + entry_length;
3281 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3282 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3283 if (addr + entry_length > section->buffer + section->size)
3284 {
47e3f474 3285 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3286 "length %s exceeds section length %s, "
3287 "ignoring .debug_aranges."),
47e3f474
TV
3288 objfile_name (objfile),
3289 plongest (entry_addr - section->buffer),
927aa2e7
JK
3290 plongest (bytes_read + entry_length),
3291 pulongest (section->size));
3292 return;
3293 }
3294
3295 /* The version number. */
3296 const uint16_t version = read_2_bytes (abfd, addr);
3297 addr += 2;
3298 if (version != 2)
3299 {
47e3f474 3300 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3301 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3302 objfile_name (objfile),
3303 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3304 return;
3305 }
3306
3307 const uint64_t debug_info_offset
3308 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3309 addr += offset_size;
3310 const auto per_cu_it
3311 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3312 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3313 {
47e3f474 3314 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3315 "debug_info_offset %s does not exists, "
3316 "ignoring .debug_aranges."),
47e3f474
TV
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer),
927aa2e7
JK
3319 pulongest (debug_info_offset));
3320 return;
3321 }
3322 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3323
3324 const uint8_t address_size = *addr++;
3325 if (address_size < 1 || address_size > 8)
3326 {
47e3f474 3327 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3328 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3329 objfile_name (objfile),
3330 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3331 return;
3332 }
3333
3334 const uint8_t segment_selector_size = *addr++;
3335 if (segment_selector_size != 0)
3336 {
47e3f474 3337 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3338 "segment_selector_size %u is not supported, "
3339 "ignoring .debug_aranges."),
47e3f474
TV
3340 objfile_name (objfile),
3341 plongest (entry_addr - section->buffer),
927aa2e7
JK
3342 segment_selector_size);
3343 return;
3344 }
3345
3346 /* Must pad to an alignment boundary that is twice the address
3347 size. It is undocumented by the DWARF standard but GCC does
3348 use it. */
3349 for (size_t padding = ((-(addr - section->buffer))
3350 & (2 * address_size - 1));
3351 padding > 0; padding--)
3352 if (*addr++ != 0)
3353 {
47e3f474 3354 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3355 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3356 objfile_name (objfile),
3357 plongest (entry_addr - section->buffer));
927aa2e7
JK
3358 return;
3359 }
3360
3361 for (;;)
3362 {
3363 if (addr + 2 * address_size > entry_end)
3364 {
47e3f474 3365 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3366 "address list is not properly terminated, "
3367 "ignoring .debug_aranges."),
47e3f474
TV
3368 objfile_name (objfile),
3369 plongest (entry_addr - section->buffer));
927aa2e7
JK
3370 return;
3371 }
3372 ULONGEST start = extract_unsigned_integer (addr, address_size,
3373 dwarf5_byte_order);
3374 addr += address_size;
3375 ULONGEST length = extract_unsigned_integer (addr, address_size,
3376 dwarf5_byte_order);
3377 addr += address_size;
3378 if (start == 0 && length == 0)
3379 break;
3380 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3381 {
3382 /* Symbol was eliminated due to a COMDAT group. */
3383 continue;
3384 }
3385 ULONGEST end = start + length;
79748972
TT
3386 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3387 - baseaddr);
3388 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3389 - baseaddr);
927aa2e7
JK
3390 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3391 }
3392 }
3393
d320c2b5 3394 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3395 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3396}
3397
9291a0cd
TT
3398/* Find a slot in the mapped index INDEX for the object named NAME.
3399 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3400 constant pool and return true. If NAME cannot be found, return
3401 false. */
2fdf6df6 3402
109483d9 3403static bool
9291a0cd
TT
3404find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3405 offset_type **vec_out)
3406{
0cf03b49 3407 offset_type hash;
9291a0cd 3408 offset_type slot, step;
559a7a62 3409 int (*cmp) (const char *, const char *);
9291a0cd 3410
791afaa2 3411 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3412 if (current_language->la_language == language_cplus
45280282
IB
3413 || current_language->la_language == language_fortran
3414 || current_language->la_language == language_d)
0cf03b49
JK
3415 {
3416 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3417 not contain any. */
a8719064 3418
72998fb3 3419 if (strchr (name, '(') != NULL)
0cf03b49 3420 {
109483d9 3421 without_params = cp_remove_params (name);
0cf03b49 3422
72998fb3 3423 if (without_params != NULL)
791afaa2 3424 name = without_params.get ();
0cf03b49
JK
3425 }
3426 }
3427
559a7a62 3428 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3429 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3430 simulate our NAME being searched is also lowercased. */
3431 hash = mapped_index_string_hash ((index->version == 4
3432 && case_sensitivity == case_sensitive_off
3433 ? 5 : index->version),
3434 name);
3435
f00a2de2
PA
3436 slot = hash & (index->symbol_table.size () - 1);
3437 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3438 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3439
3440 for (;;)
3441 {
9291a0cd 3442 const char *str;
f00a2de2
PA
3443
3444 const auto &bucket = index->symbol_table[slot];
3445 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3446 return false;
9291a0cd 3447
f00a2de2 3448 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3449 if (!cmp (name, str))
9291a0cd
TT
3450 {
3451 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3452 + MAYBE_SWAP (bucket.vec));
109483d9 3453 return true;
9291a0cd
TT
3454 }
3455
f00a2de2 3456 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3457 }
3458}
3459
4485a1c1
SM
3460/* A helper function that reads the .gdb_index from BUFFER and fills
3461 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3462 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3463 ok to use deprecated sections.
3464
3465 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3466 out parameters that are filled in with information about the CU and
3467 TU lists in the section.
3468
4485a1c1 3469 Returns true if all went well, false otherwise. */
2fdf6df6 3470
d33bc52e 3471static bool
4485a1c1
SM
3472read_gdb_index_from_buffer (struct objfile *objfile,
3473 const char *filename,
3474 bool deprecated_ok,
3475 gdb::array_view<const gdb_byte> buffer,
3476 struct mapped_index *map,
3477 const gdb_byte **cu_list,
3478 offset_type *cu_list_elements,
3479 const gdb_byte **types_list,
3480 offset_type *types_list_elements)
3481{
3482 const gdb_byte *addr = &buffer[0];
82430852 3483
9291a0cd 3484 /* Version check. */
4485a1c1 3485 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3486 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3487 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3488 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3489 indices. */
831adc1f 3490 if (version < 4)
481860b3
GB
3491 {
3492 static int warning_printed = 0;
3493 if (!warning_printed)
3494 {
3495 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3496 filename);
481860b3
GB
3497 warning_printed = 1;
3498 }
3499 return 0;
3500 }
3501 /* Index version 4 uses a different hash function than index version
3502 5 and later.
3503
3504 Versions earlier than 6 did not emit psymbols for inlined
3505 functions. Using these files will cause GDB not to be able to
3506 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3507 indices unless the user has done
3508 "set use-deprecated-index-sections on". */
2ec9a5e0 3509 if (version < 6 && !deprecated_ok)
481860b3
GB
3510 {
3511 static int warning_printed = 0;
3512 if (!warning_printed)
3513 {
e615022a
DE
3514 warning (_("\
3515Skipping deprecated .gdb_index section in %s.\n\
3516Do \"set use-deprecated-index-sections on\" before the file is read\n\
3517to use the section anyway."),
2ec9a5e0 3518 filename);
481860b3
GB
3519 warning_printed = 1;
3520 }
3521 return 0;
3522 }
796a7ff8 3523 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3524 of the TU (for symbols coming from TUs),
3525 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3526 Plus gold-generated indices can have duplicate entries for global symbols,
3527 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3528 These are just performance bugs, and we can't distinguish gdb-generated
3529 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3530
481860b3 3531 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3532 longer backward compatible. */
796a7ff8 3533 if (version > 8)
594e8718 3534 return 0;
9291a0cd 3535
559a7a62 3536 map->version = version;
9291a0cd 3537
4485a1c1 3538 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3539
4485a1c1 3540 int i = 0;
2ec9a5e0
TT
3541 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3542 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3543 / 8);
1fd400ff
TT
3544 ++i;
3545
2ec9a5e0
TT
3546 *types_list = addr + MAYBE_SWAP (metadata[i]);
3547 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3548 - MAYBE_SWAP (metadata[i]))
3549 / 8);
987d643c 3550 ++i;
1fd400ff 3551
f00a2de2
PA
3552 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3553 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3554 map->address_table
3555 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3556 ++i;
3557
f00a2de2
PA
3558 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3559 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3560 map->symbol_table
3561 = gdb::array_view<mapped_index::symbol_table_slot>
3562 ((mapped_index::symbol_table_slot *) symbol_table,
3563 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3564
f00a2de2 3565 ++i;
f9d83a0b 3566 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3567
2ec9a5e0
TT
3568 return 1;
3569}
3570
4485a1c1
SM
3571/* Callback types for dwarf2_read_gdb_index. */
3572
3573typedef gdb::function_view
3574 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3575 get_gdb_index_contents_ftype;
3576typedef gdb::function_view
3577 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3578 get_gdb_index_contents_dwz_ftype;
3579
927aa2e7 3580/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3581 elements of all the CUs and return 1. Otherwise, return 0. */
3582
3583static int
4485a1c1
SM
3584dwarf2_read_gdb_index
3585 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3586 get_gdb_index_contents_ftype get_gdb_index_contents,
3587 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3588{
2ec9a5e0
TT
3589 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3590 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3591 struct dwz_file *dwz;
12359b5e 3592 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3593
4485a1c1
SM
3594 gdb::array_view<const gdb_byte> main_index_contents
3595 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3596
3597 if (main_index_contents.empty ())
3598 return 0;
3599
3063847f 3600 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3601 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3602 use_deprecated_index_sections,
3603 main_index_contents, map.get (), &cu_list,
3604 &cu_list_elements, &types_list,
3605 &types_list_elements))
2ec9a5e0
TT
3606 return 0;
3607
0fefef59 3608 /* Don't use the index if it's empty. */
3063847f 3609 if (map->symbol_table.empty ())
0fefef59
DE
3610 return 0;
3611
2ec9a5e0
TT
3612 /* If there is a .dwz file, read it so we can get its CU list as
3613 well. */
ed2dc618 3614 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3615 if (dwz != NULL)
2ec9a5e0 3616 {
2ec9a5e0
TT
3617 struct mapped_index dwz_map;
3618 const gdb_byte *dwz_types_ignore;
3619 offset_type dwz_types_elements_ignore;
3620
4485a1c1
SM
3621 gdb::array_view<const gdb_byte> dwz_index_content
3622 = get_gdb_index_contents_dwz (objfile, dwz);
3623
3624 if (dwz_index_content.empty ())
3625 return 0;
3626
3627 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3628 bfd_get_filename (dwz->dwz_bfd.get ()),
3629 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3630 &dwz_list, &dwz_list_elements,
3631 &dwz_types_ignore,
3632 &dwz_types_elements_ignore))
2ec9a5e0
TT
3633 {
3634 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3635 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3636 return 0;
3637 }
3638 }
3639
12359b5e
SM
3640 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3641 dwz_list, dwz_list_elements);
1fd400ff 3642
8b70b953
TT
3643 if (types_list_elements)
3644 {
8b70b953
TT
3645 /* We can only handle a single .debug_types when we have an
3646 index. */
fd5866f6 3647 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3648 return 0;
3649
fd5866f6 3650 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3651
12359b5e
SM
3652 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3653 types_list, types_list_elements);
8b70b953 3654 }
9291a0cd 3655
3063847f 3656 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3657
3063847f 3658 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3659 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3660 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3661 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3662
3663 return 1;
3664}
3665
dee91e82 3666/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3667
dee91e82
DE
3668static void
3669dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3670 const gdb_byte *info_ptr,
dee91e82 3671 struct die_info *comp_unit_die,
c0ab21c2 3672 int has_children)
9291a0cd 3673{
dee91e82 3674 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3675 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3676 struct dwarf2_per_objfile *dwarf2_per_objfile
3677 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3678 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3679 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3680 struct attribute *attr;
7b9f3c50
DE
3681 void **slot;
3682 struct quick_file_names *qfn;
9291a0cd 3683
0186c6a7
DE
3684 gdb_assert (! this_cu->is_debug_types);
3685
07261596
TT
3686 /* Our callers never want to match partial units -- instead they
3687 will match the enclosing full CU. */
3688 if (comp_unit_die->tag == DW_TAG_partial_unit)
3689 {
3690 this_cu->v.quick->no_file_data = 1;
3691 return;
3692 }
3693
0186c6a7 3694 lh_cu = this_cu;
7b9f3c50 3695 slot = NULL;
dee91e82 3696
fff8551c 3697 line_header_up lh;
9c541725 3698 sect_offset line_offset {};
fff8551c 3699
dee91e82 3700 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3701 if (attr != nullptr)
9291a0cd 3702 {
7b9f3c50
DE
3703 struct quick_file_names find_entry;
3704
9c541725 3705 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3706
3707 /* We may have already read in this line header (TU line header sharing).
3708 If we have we're done. */
094b34ac 3709 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3710 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3711 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3712 &find_entry, INSERT);
3713 if (*slot != NULL)
3714 {
9a3c8263 3715 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3716 return;
7b9f3c50
DE
3717 }
3718
3019eac3 3719 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3720 }
3721 if (lh == NULL)
3722 {
094b34ac 3723 lh_cu->v.quick->no_file_data = 1;
dee91e82 3724 return;
9291a0cd
TT
3725 }
3726
8d749320 3727 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3728 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3729 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3730 gdb_assert (slot != NULL);
3731 *slot = qfn;
9291a0cd 3732
d721ba37 3733 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3734
aa391654
TT
3735 int offset = 0;
3736 if (strcmp (fnd.name, "<unknown>") != 0)
3737 ++offset;
3738
7ba99d21 3739 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3740 qfn->file_names =
aa391654
TT
3741 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3742 if (offset != 0)
3743 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3744 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3745 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3746 qfn->real_names = NULL;
9291a0cd 3747
094b34ac 3748 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3749}
3750
3751/* A helper for the "quick" functions which attempts to read the line
3752 table for THIS_CU. */
3753
3754static struct quick_file_names *
e4a48d9d 3755dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3756{
0186c6a7
DE
3757 /* This should never be called for TUs. */
3758 gdb_assert (! this_cu->is_debug_types);
3759 /* Nor type unit groups. */
3760 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3761
dee91e82
DE
3762 if (this_cu->v.quick->file_names != NULL)
3763 return this_cu->v.quick->file_names;
3764 /* If we know there is no line data, no point in looking again. */
3765 if (this_cu->v.quick->no_file_data)
3766 return NULL;
3767
c0ab21c2
TT
3768 cutu_reader reader (this_cu);
3769 if (!reader.dummy_p)
3770 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3771 reader.has_children);
dee91e82
DE
3772
3773 if (this_cu->v.quick->no_file_data)
3774 return NULL;
3775 return this_cu->v.quick->file_names;
9291a0cd
TT
3776}
3777
3778/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3779 real path for a given file name from the line table. */
2fdf6df6 3780
9291a0cd 3781static const char *
7b9f3c50
DE
3782dw2_get_real_path (struct objfile *objfile,
3783 struct quick_file_names *qfn, int index)
9291a0cd 3784{
7b9f3c50
DE
3785 if (qfn->real_names == NULL)
3786 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3787 qfn->num_file_names, const char *);
9291a0cd 3788
7b9f3c50 3789 if (qfn->real_names[index] == NULL)
14278e1f 3790 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3791
7b9f3c50 3792 return qfn->real_names[index];
9291a0cd
TT
3793}
3794
3795static struct symtab *
3796dw2_find_last_source_symtab (struct objfile *objfile)
3797{
ed2dc618
SM
3798 struct dwarf2_per_objfile *dwarf2_per_objfile
3799 = get_dwarf2_per_objfile (objfile);
b76e467d 3800 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3801 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3802
43f3e411
DE
3803 if (cust == NULL)
3804 return NULL;
ed2dc618 3805
43f3e411 3806 return compunit_primary_filetab (cust);
9291a0cd
TT
3807}
3808
7b9f3c50
DE
3809/* Traversal function for dw2_forget_cached_source_info. */
3810
3811static int
3812dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3813{
7b9f3c50 3814 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3815
7b9f3c50 3816 if (file_data->real_names)
9291a0cd 3817 {
7b9f3c50 3818 int i;
9291a0cd 3819
7b9f3c50 3820 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3821 {
7b9f3c50
DE
3822 xfree ((void*) file_data->real_names[i]);
3823 file_data->real_names[i] = NULL;
9291a0cd
TT
3824 }
3825 }
7b9f3c50
DE
3826
3827 return 1;
3828}
3829
3830static void
3831dw2_forget_cached_source_info (struct objfile *objfile)
3832{
ed2dc618
SM
3833 struct dwarf2_per_objfile *dwarf2_per_objfile
3834 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3835
3836 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3837 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3838}
3839
f8eba3c6
TT
3840/* Helper function for dw2_map_symtabs_matching_filename that expands
3841 the symtabs and calls the iterator. */
3842
3843static int
3844dw2_map_expand_apply (struct objfile *objfile,
3845 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3846 const char *name, const char *real_path,
14bc53a8 3847 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3848{
43f3e411 3849 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3850
3851 /* Don't visit already-expanded CUs. */
43f3e411 3852 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3853 return 0;
3854
3855 /* This may expand more than one symtab, and we want to iterate over
3856 all of them. */
58f0c718 3857 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3858
14bc53a8
PA
3859 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3860 last_made, callback);
f8eba3c6
TT
3861}
3862
3863/* Implementation of the map_symtabs_matching_filename method. */
3864
14bc53a8
PA
3865static bool
3866dw2_map_symtabs_matching_filename
3867 (struct objfile *objfile, const char *name, const char *real_path,
3868 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3869{
c011a4f4 3870 const char *name_basename = lbasename (name);
ed2dc618
SM
3871 struct dwarf2_per_objfile *dwarf2_per_objfile
3872 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3873
848e3e78
DE
3874 /* The rule is CUs specify all the files, including those used by
3875 any TU, so there's no need to scan TUs here. */
f4dc4d17 3876
b76e467d 3877 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3878 {
3d7bb9d9 3879 /* We only need to look at symtabs not already expanded. */
43f3e411 3880 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3881 continue;
3882
b76e467d 3883 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3884 if (file_data == NULL)
9291a0cd
TT
3885 continue;
3886
b76e467d 3887 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3888 {
7b9f3c50 3889 const char *this_name = file_data->file_names[j];
da235a7c 3890 const char *this_real_name;
9291a0cd 3891
af529f8f 3892 if (compare_filenames_for_search (this_name, name))
9291a0cd 3893 {
f5b95b50 3894 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3895 callback))
3896 return true;
288e77a7 3897 continue;
4aac40c8 3898 }
9291a0cd 3899
c011a4f4
DE
3900 /* Before we invoke realpath, which can get expensive when many
3901 files are involved, do a quick comparison of the basenames. */
3902 if (! basenames_may_differ
3903 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3904 continue;
3905
da235a7c
JK
3906 this_real_name = dw2_get_real_path (objfile, file_data, j);
3907 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3908 {
da235a7c 3909 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3910 callback))
3911 return true;
288e77a7 3912 continue;
da235a7c 3913 }
9291a0cd 3914
da235a7c
JK
3915 if (real_path != NULL)
3916 {
af529f8f
JK
3917 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3918 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3919 if (this_real_name != NULL
af529f8f 3920 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3921 {
f5b95b50 3922 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3923 callback))
3924 return true;
288e77a7 3925 continue;
9291a0cd
TT
3926 }
3927 }
3928 }
3929 }
3930
14bc53a8 3931 return false;
9291a0cd
TT
3932}
3933
da51c347
DE
3934/* Struct used to manage iterating over all CUs looking for a symbol. */
3935
3936struct dw2_symtab_iterator
9291a0cd 3937{
ed2dc618
SM
3938 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3939 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3940 /* If set, only look for symbols that match that block. Valid values are
3941 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3942 gdb::optional<block_enum> block_index;
da51c347
DE
3943 /* The kind of symbol we're looking for. */
3944 domain_enum domain;
3945 /* The list of CUs from the index entry of the symbol,
3946 or NULL if not found. */
3947 offset_type *vec;
3948 /* The next element in VEC to look at. */
3949 int next;
3950 /* The number of elements in VEC, or zero if there is no match. */
3951 int length;
8943b874
DE
3952 /* Have we seen a global version of the symbol?
3953 If so we can ignore all further global instances.
3954 This is to work around gold/15646, inefficient gold-generated
3955 indices. */
3956 int global_seen;
da51c347 3957};
9291a0cd 3958
2b79f376 3959/* Initialize the index symtab iterator ITER. */
2fdf6df6 3960
9291a0cd 3961static void
da51c347 3962dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3963 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3964 gdb::optional<block_enum> block_index,
da51c347
DE
3965 domain_enum domain,
3966 const char *name)
3967{
ed2dc618 3968 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3969 iter->block_index = block_index;
3970 iter->domain = domain;
3971 iter->next = 0;
8943b874 3972 iter->global_seen = 0;
da51c347 3973
3063847f 3974 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3975
3976 /* index is NULL if OBJF_READNOW. */
3977 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3978 iter->length = MAYBE_SWAP (*iter->vec);
3979 else
3980 {
3981 iter->vec = NULL;
3982 iter->length = 0;
3983 }
3984}
3985
3986/* Return the next matching CU or NULL if there are no more. */
3987
3988static struct dwarf2_per_cu_data *
3989dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3990{
ed2dc618
SM
3991 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3992
da51c347
DE
3993 for ( ; iter->next < iter->length; ++iter->next)
3994 {
3995 offset_type cu_index_and_attrs =
3996 MAYBE_SWAP (iter->vec[iter->next + 1]);
3997 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3998 gdb_index_symbol_kind symbol_kind =
3999 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4000 /* Only check the symbol attributes if they're present.
4001 Indices prior to version 7 don't record them,
4002 and indices >= 7 may elide them for certain symbols
4003 (gold does this). */
4004 int attrs_valid =
ed2dc618 4005 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
4006 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4007
3190f0c6 4008 /* Don't crash on bad data. */
b76e467d 4009 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4010 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 4011 {
b98664d3 4012 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
4013 " [in module %s]"),
4014 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4015 continue;
4016 }
4017
ff4c9fec 4018 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4019
da51c347 4020 /* Skip if already read in. */
43f3e411 4021 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4022 continue;
4023
8943b874
DE
4024 /* Check static vs global. */
4025 if (attrs_valid)
4026 {
2b79f376
SM
4027 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4028
4029 if (iter->block_index.has_value ())
4030 {
4031 bool want_static = *iter->block_index == STATIC_BLOCK;
4032
4033 if (is_static != want_static)
4034 continue;
4035 }
4036
8943b874
DE
4037 /* Work around gold/15646. */
4038 if (!is_static && iter->global_seen)
4039 continue;
4040 if (!is_static)
4041 iter->global_seen = 1;
4042 }
da51c347
DE
4043
4044 /* Only check the symbol's kind if it has one. */
4045 if (attrs_valid)
4046 {
4047 switch (iter->domain)
4048 {
4049 case VAR_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4051 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4052 /* Some types are also in VAR_DOMAIN. */
4053 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4054 continue;
4055 break;
4056 case STRUCT_DOMAIN:
4057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4058 continue;
4059 break;
4060 case LABEL_DOMAIN:
4061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4062 continue;
4063 break;
59c35742
AB
4064 case MODULE_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4066 continue;
4067 break;
da51c347
DE
4068 default:
4069 break;
4070 }
4071 }
4072
4073 ++iter->next;
4074 return per_cu;
4075 }
4076
4077 return NULL;
4078}
4079
43f3e411 4080static struct compunit_symtab *
c7f839cb 4081dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4082 const char *name, domain_enum domain)
9291a0cd 4083{
43f3e411 4084 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4085 struct dwarf2_per_objfile *dwarf2_per_objfile
4086 = get_dwarf2_per_objfile (objfile);
9291a0cd 4087
b5ec771e
PA
4088 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4089
ed2dc618
SM
4090 struct dw2_symtab_iterator iter;
4091 struct dwarf2_per_cu_data *per_cu;
da51c347 4092
2b79f376 4093 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4094
ed2dc618
SM
4095 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4096 {
4097 struct symbol *sym, *with_opaque = NULL;
58f0c718 4098 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4099 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4100 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4101
ed2dc618
SM
4102 sym = block_find_symbol (block, name, domain,
4103 block_find_non_opaque_type_preferred,
4104 &with_opaque);
b2e2f908 4105
ed2dc618
SM
4106 /* Some caution must be observed with overloaded functions
4107 and methods, since the index will not contain any overload
4108 information (but NAME might contain it). */
da51c347 4109
ed2dc618
SM
4110 if (sym != NULL
4111 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4112 return stab;
4113 if (with_opaque != NULL
4114 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4115 stab_best = stab;
da51c347 4116
ed2dc618 4117 /* Keep looking through other CUs. */
9291a0cd 4118 }
9291a0cd 4119
da51c347 4120 return stab_best;
9291a0cd
TT
4121}
4122
4123static void
4124dw2_print_stats (struct objfile *objfile)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
b76e467d 4128 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4129 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4130 int count = 0;
9291a0cd 4131
ed2dc618 4132 for (int i = 0; i < total; ++i)
9291a0cd 4133 {
ff4c9fec 4134 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4135
43f3e411 4136 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4137 ++count;
4138 }
e4a48d9d 4139 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4140 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4141}
4142
779bd270
DE
4143/* This dumps minimal information about the index.
4144 It is called via "mt print objfiles".
4145 One use is to verify .gdb_index has been loaded by the
4146 gdb.dwarf2/gdb-index.exp testcase. */
4147
9291a0cd
TT
4148static void
4149dw2_dump (struct objfile *objfile)
4150{
ed2dc618
SM
4151 struct dwarf2_per_objfile *dwarf2_per_objfile
4152 = get_dwarf2_per_objfile (objfile);
4153
779bd270
DE
4154 gdb_assert (dwarf2_per_objfile->using_index);
4155 printf_filtered (".gdb_index:");
4156 if (dwarf2_per_objfile->index_table != NULL)
4157 {
4158 printf_filtered (" version %d\n",
4159 dwarf2_per_objfile->index_table->version);
4160 }
4161 else
4162 printf_filtered (" faked for \"readnow\"\n");
4163 printf_filtered ("\n");
9291a0cd
TT
4164}
4165
9291a0cd
TT
4166static void
4167dw2_expand_symtabs_for_function (struct objfile *objfile,
4168 const char *func_name)
4169{
ed2dc618
SM
4170 struct dwarf2_per_objfile *dwarf2_per_objfile
4171 = get_dwarf2_per_objfile (objfile);
da51c347 4172
ed2dc618
SM
4173 struct dw2_symtab_iterator iter;
4174 struct dwarf2_per_cu_data *per_cu;
da51c347 4175
2b79f376 4176 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4177
ed2dc618 4178 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4179 dw2_instantiate_symtab (per_cu, false);
da51c347 4180
9291a0cd
TT
4181}
4182
4183static void
4184dw2_expand_all_symtabs (struct objfile *objfile)
4185{
ed2dc618
SM
4186 struct dwarf2_per_objfile *dwarf2_per_objfile
4187 = get_dwarf2_per_objfile (objfile);
b76e467d 4188 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4189 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4190
ed2dc618 4191 for (int i = 0; i < total_units; ++i)
9291a0cd 4192 {
ff4c9fec 4193 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4194
58f0c718
TT
4195 /* We don't want to directly expand a partial CU, because if we
4196 read it with the wrong language, then assertion failures can
4197 be triggered later on. See PR symtab/23010. So, tell
4198 dw2_instantiate_symtab to skip partial CUs -- any important
4199 partial CU will be read via DW_TAG_imported_unit anyway. */
4200 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4201 }
4202}
4203
4204static void
652a8996
JK
4205dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4206 const char *fullname)
9291a0cd 4207{
ed2dc618
SM
4208 struct dwarf2_per_objfile *dwarf2_per_objfile
4209 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4210
4211 /* We don't need to consider type units here.
4212 This is only called for examining code, e.g. expand_line_sal.
4213 There can be an order of magnitude (or more) more type units
4214 than comp units, and we avoid them if we can. */
4215
b76e467d 4216 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4217 {
3d7bb9d9 4218 /* We only need to look at symtabs not already expanded. */
43f3e411 4219 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4220 continue;
4221
b76e467d 4222 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4223 if (file_data == NULL)
9291a0cd
TT
4224 continue;
4225
b76e467d 4226 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4227 {
652a8996
JK
4228 const char *this_fullname = file_data->file_names[j];
4229
4230 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4231 {
58f0c718 4232 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4233 break;
4234 }
4235 }
4236 }
4237}
4238
9291a0cd 4239static void
199b4314
TT
4240dw2_map_matching_symbols
4241 (struct objfile *objfile,
b054970d 4242 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4243 int global,
4244 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4245 symbol_compare_ftype *ordered_compare)
9291a0cd 4246{
40658b94 4247 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4248 current language is Ada for a non-Ada objfile using GNU index. As Ada
4249 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4250}
4251
e1ef7d7a
PA
4252/* Starting from a search name, return the string that finds the upper
4253 bound of all strings that start with SEARCH_NAME in a sorted name
4254 list. Returns the empty string to indicate that the upper bound is
4255 the end of the list. */
4256
4257static std::string
4258make_sort_after_prefix_name (const char *search_name)
4259{
4260 /* When looking to complete "func", we find the upper bound of all
4261 symbols that start with "func" by looking for where we'd insert
4262 the closest string that would follow "func" in lexicographical
4263 order. Usually, that's "func"-with-last-character-incremented,
4264 i.e. "fund". Mind non-ASCII characters, though. Usually those
4265 will be UTF-8 multi-byte sequences, but we can't be certain.
4266 Especially mind the 0xff character, which is a valid character in
4267 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4268 rule out compilers allowing it in identifiers. Note that
4269 conveniently, strcmp/strcasecmp are specified to compare
4270 characters interpreted as unsigned char. So what we do is treat
4271 the whole string as a base 256 number composed of a sequence of
4272 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4273 to 0, and carries 1 to the following more-significant position.
4274 If the very first character in SEARCH_NAME ends up incremented
4275 and carries/overflows, then the upper bound is the end of the
4276 list. The string after the empty string is also the empty
4277 string.
4278
4279 Some examples of this operation:
4280
4281 SEARCH_NAME => "+1" RESULT
4282
4283 "abc" => "abd"
4284 "ab\xff" => "ac"
4285 "\xff" "a" "\xff" => "\xff" "b"
4286 "\xff" => ""
4287 "\xff\xff" => ""
4288 "" => ""
4289
4290 Then, with these symbols for example:
4291
4292 func
4293 func1
4294 fund
4295
4296 completing "func" looks for symbols between "func" and
4297 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4298 which finds "func" and "func1", but not "fund".
4299
4300 And with:
4301
4302 funcÿ (Latin1 'ÿ' [0xff])
4303 funcÿ1
4304 fund
4305
4306 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4307 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4308
4309 And with:
4310
4311 ÿÿ (Latin1 'ÿ' [0xff])
4312 ÿÿ1
4313
4314 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4315 the end of the list.
4316 */
4317 std::string after = search_name;
4318 while (!after.empty () && (unsigned char) after.back () == 0xff)
4319 after.pop_back ();
4320 if (!after.empty ())
4321 after.back () = (unsigned char) after.back () + 1;
4322 return after;
4323}
4324
5c58de74 4325/* See declaration. */
61d96d7e 4326
5c58de74
PA
4327std::pair<std::vector<name_component>::const_iterator,
4328 std::vector<name_component>::const_iterator>
44ed8f3e 4329mapped_index_base::find_name_components_bounds
3b00ef10 4330 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4331{
5c58de74
PA
4332 auto *name_cmp
4333 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4334
3b00ef10
TT
4335 const char *lang_name
4336 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4337
3f563c84
PA
4338 /* Comparison function object for lower_bound that matches against a
4339 given symbol name. */
4340 auto lookup_compare_lower = [&] (const name_component &elem,
4341 const char *name)
4342 {
5c58de74 4343 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4344 const char *elem_name = elem_qualified + elem.name_offset;
4345 return name_cmp (elem_name, name) < 0;
4346 };
4347
4348 /* Comparison function object for upper_bound that matches against a
4349 given symbol name. */
4350 auto lookup_compare_upper = [&] (const char *name,
4351 const name_component &elem)
4352 {
5c58de74 4353 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4354 const char *elem_name = elem_qualified + elem.name_offset;
4355 return name_cmp (name, elem_name) < 0;
4356 };
4357
5c58de74
PA
4358 auto begin = this->name_components.begin ();
4359 auto end = this->name_components.end ();
3f563c84
PA
4360
4361 /* Find the lower bound. */
4362 auto lower = [&] ()
4363 {
3b00ef10 4364 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4365 return begin;
4366 else
3b00ef10 4367 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4368 } ();
4369
4370 /* Find the upper bound. */
4371 auto upper = [&] ()
4372 {
5c58de74 4373 if (lookup_name_without_params.completion_mode ())
3f563c84 4374 {
e1ef7d7a
PA
4375 /* In completion mode, we want UPPER to point past all
4376 symbols names that have the same prefix. I.e., with
4377 these symbols, and completing "func":
4378
4379 function << lower bound
4380 function1
4381 other_function << upper bound
4382
4383 We find the upper bound by looking for the insertion
4384 point of "func"-with-last-character-incremented,
4385 i.e. "fund". */
3b00ef10 4386 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4387 if (after.empty ())
3f563c84 4388 return end;
e6b2f5ef
PA
4389 return std::lower_bound (lower, end, after.c_str (),
4390 lookup_compare_lower);
3f563c84
PA
4391 }
4392 else
3b00ef10 4393 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4394 } ();
4395
5c58de74
PA
4396 return {lower, upper};
4397}
4398
4399/* See declaration. */
4400
4401void
44ed8f3e 4402mapped_index_base::build_name_components ()
5c58de74
PA
4403{
4404 if (!this->name_components.empty ())
4405 return;
4406
4407 this->name_components_casing = case_sensitivity;
4408 auto *name_cmp
4409 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4410
4411 /* The code below only knows how to break apart components of C++
4412 symbol names (and other languages that use '::' as
3b00ef10 4413 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4414 auto count = this->symbol_name_count ();
4415 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4416 {
44ed8f3e 4417 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4418 continue;
4419
4420 const char *name = this->symbol_name_at (idx);
4421
4422 /* Add each name component to the name component table. */
4423 unsigned int previous_len = 0;
3b00ef10
TT
4424
4425 if (strstr (name, "::") != nullptr)
4426 {
4427 for (unsigned int current_len = cp_find_first_component (name);
4428 name[current_len] != '\0';
4429 current_len += cp_find_first_component (name + current_len))
4430 {
4431 gdb_assert (name[current_len] == ':');
4432 this->name_components.push_back ({previous_len, idx});
4433 /* Skip the '::'. */
4434 current_len += 2;
4435 previous_len = current_len;
4436 }
4437 }
4438 else
5c58de74 4439 {
3b00ef10
TT
4440 /* Handle the Ada encoded (aka mangled) form here. */
4441 for (const char *iter = strstr (name, "__");
4442 iter != nullptr;
4443 iter = strstr (iter, "__"))
4444 {
4445 this->name_components.push_back ({previous_len, idx});
4446 iter += 2;
4447 previous_len = iter - name;
4448 }
5c58de74 4449 }
3b00ef10 4450
5c58de74
PA
4451 this->name_components.push_back ({previous_len, idx});
4452 }
4453
4454 /* Sort name_components elements by name. */
4455 auto name_comp_compare = [&] (const name_component &left,
4456 const name_component &right)
4457 {
4458 const char *left_qualified = this->symbol_name_at (left.idx);
4459 const char *right_qualified = this->symbol_name_at (right.idx);
4460
4461 const char *left_name = left_qualified + left.name_offset;
4462 const char *right_name = right_qualified + right.name_offset;
4463
4464 return name_cmp (left_name, right_name) < 0;
4465 };
4466
4467 std::sort (this->name_components.begin (),
4468 this->name_components.end (),
4469 name_comp_compare);
4470}
4471
4472/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4473 mapped_index_base instead of the containing objfile. This is split
4474 to a separate function in order to be able to unit test the
4475 name_components matching using a mock mapped_index_base. For each
5c58de74 4476 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4477 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4478
4479static void
4480dw2_expand_symtabs_matching_symbol
44ed8f3e 4481 (mapped_index_base &index,
5c58de74
PA
4482 const lookup_name_info &lookup_name_in,
4483 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4484 enum search_domain kind,
3b00ef10 4485 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4486{
4487 lookup_name_info lookup_name_without_params
4488 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4489
4490 /* Build the symbol name component sorted vector, if we haven't
4491 yet. */
4492 index.build_name_components ();
4493
3f563c84
PA
4494 /* The same symbol may appear more than once in the range though.
4495 E.g., if we're looking for symbols that complete "w", and we have
4496 a symbol named "w1::w2", we'll find the two name components for
4497 that same symbol in the range. To be sure we only call the
4498 callback once per symbol, we first collect the symbol name
4499 indexes that matched in a temporary vector and ignore
4500 duplicates. */
4501 std::vector<offset_type> matches;
3f563c84 4502
3b00ef10
TT
4503 struct name_and_matcher
4504 {
4505 symbol_name_matcher_ftype *matcher;
4506 const std::string &name;
4507
4508 bool operator== (const name_and_matcher &other) const
3f563c84 4509 {
3b00ef10
TT
4510 return matcher == other.matcher && name == other.name;
4511 }
4512 };
4513
4514 /* A vector holding all the different symbol name matchers, for all
4515 languages. */
4516 std::vector<name_and_matcher> matchers;
4517
4518 for (int i = 0; i < nr_languages; i++)
4519 {
4520 enum language lang_e = (enum language) i;
4521
4522 const language_defn *lang = language_def (lang_e);
4523 symbol_name_matcher_ftype *name_matcher
4524 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4525
3b00ef10
TT
4526 name_and_matcher key {
4527 name_matcher,
4528 lookup_name_without_params.language_lookup_name (lang_e)
4529 };
4530
4531 /* Don't insert the same comparison routine more than once.
4532 Note that we do this linear walk. This is not a problem in
4533 practice because the number of supported languages is
4534 low. */
4535 if (std::find (matchers.begin (), matchers.end (), key)
4536 != matchers.end ())
9291a0cd 4537 continue;
3b00ef10
TT
4538 matchers.push_back (std::move (key));
4539
4540 auto bounds
4541 = index.find_name_components_bounds (lookup_name_without_params,
4542 lang_e);
4543
4544 /* Now for each symbol name in range, check to see if we have a name
4545 match, and if so, call the MATCH_CALLBACK callback. */
4546
4547 for (; bounds.first != bounds.second; ++bounds.first)
4548 {
4549 const char *qualified = index.symbol_name_at (bounds.first->idx);
4550
4551 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4552 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4553 continue;
9291a0cd 4554
3b00ef10
TT
4555 matches.push_back (bounds.first->idx);
4556 }
3f563c84
PA
4557 }
4558
4559 std::sort (matches.begin (), matches.end ());
4560
4561 /* Finally call the callback, once per match. */
4562 ULONGEST prev = -1;
4563 for (offset_type idx : matches)
4564 {
4565 if (prev != idx)
4566 {
3b00ef10
TT
4567 if (!match_callback (idx))
4568 break;
3f563c84
PA
4569 prev = idx;
4570 }
4571 }
4572
4573 /* Above we use a type wider than idx's for 'prev', since 0 and
4574 (offset_type)-1 are both possible values. */
4575 static_assert (sizeof (prev) > sizeof (offset_type), "");
4576}
4577
c62446b1
PA
4578#if GDB_SELF_TEST
4579
4580namespace selftests { namespace dw2_expand_symtabs_matching {
4581
a3c5fafd
PA
4582/* A mock .gdb_index/.debug_names-like name index table, enough to
4583 exercise dw2_expand_symtabs_matching_symbol, which works with the
4584 mapped_index_base interface. Builds an index from the symbol list
4585 passed as parameter to the constructor. */
4586class mock_mapped_index : public mapped_index_base
c62446b1
PA
4587{
4588public:
a3c5fafd
PA
4589 mock_mapped_index (gdb::array_view<const char *> symbols)
4590 : m_symbol_table (symbols)
c62446b1
PA
4591 {}
4592
a3c5fafd 4593 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4594
a3c5fafd 4595 /* Return the number of names in the symbol table. */
632e107b 4596 size_t symbol_name_count () const override
c62446b1 4597 {
a3c5fafd 4598 return m_symbol_table.size ();
c62446b1
PA
4599 }
4600
a3c5fafd 4601 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4602 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4603 {
4604 return m_symbol_table[idx];
4605 }
c62446b1 4606
a3c5fafd
PA
4607private:
4608 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4609};
4610
4611/* Convenience function that converts a NULL pointer to a "<null>"
4612 string, to pass to print routines. */
4613
4614static const char *
4615string_or_null (const char *str)
4616{
4617 return str != NULL ? str : "<null>";
4618}
4619
4620/* Check if a lookup_name_info built from
4621 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4622 index. EXPECTED_LIST is the list of expected matches, in expected
4623 matching order. If no match expected, then an empty list is
4624 specified. Returns true on success. On failure prints a warning
4625 indicating the file:line that failed, and returns false. */
4626
4627static bool
4628check_match (const char *file, int line,
4629 mock_mapped_index &mock_index,
4630 const char *name, symbol_name_match_type match_type,
4631 bool completion_mode,
4632 std::initializer_list<const char *> expected_list)
4633{
4634 lookup_name_info lookup_name (name, match_type, completion_mode);
4635
4636 bool matched = true;
4637
4638 auto mismatch = [&] (const char *expected_str,
4639 const char *got)
4640 {
4641 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4642 "expected=\"%s\", got=\"%s\"\n"),
4643 file, line,
4644 (match_type == symbol_name_match_type::FULL
4645 ? "FULL" : "WILD"),
4646 name, string_or_null (expected_str), string_or_null (got));
4647 matched = false;
4648 };
4649
4650 auto expected_it = expected_list.begin ();
4651 auto expected_end = expected_list.end ();
4652
a3c5fafd 4653 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4654 NULL, ALL_DOMAIN,
4655 [&] (offset_type idx)
4656 {
a3c5fafd 4657 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4658 const char *expected_str
4659 = expected_it == expected_end ? NULL : *expected_it++;
4660
4661 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4662 mismatch (expected_str, matched_name);
3b00ef10 4663 return true;
c62446b1
PA
4664 });
4665
4666 const char *expected_str
4667 = expected_it == expected_end ? NULL : *expected_it++;
4668 if (expected_str != NULL)
4669 mismatch (expected_str, NULL);
4670
4671 return matched;
4672}
4673
4674/* The symbols added to the mock mapped_index for testing (in
4675 canonical form). */
4676static const char *test_symbols[] = {
4677 "function",
4678 "std::bar",
4679 "std::zfunction",
4680 "std::zfunction2",
4681 "w1::w2",
4682 "ns::foo<char*>",
4683 "ns::foo<int>",
4684 "ns::foo<long>",
a20714ff
PA
4685 "ns2::tmpl<int>::foo2",
4686 "(anonymous namespace)::A::B::C",
c62446b1 4687
e1ef7d7a
PA
4688 /* These are used to check that the increment-last-char in the
4689 matching algorithm for completion doesn't match "t1_fund" when
4690 completing "t1_func". */
4691 "t1_func",
4692 "t1_func1",
4693 "t1_fund",
4694 "t1_fund1",
4695
4696 /* A UTF-8 name with multi-byte sequences to make sure that
4697 cp-name-parser understands this as a single identifier ("função"
4698 is "function" in PT). */
4699 u8"u8função",
4700
4701 /* \377 (0xff) is Latin1 'ÿ'. */
4702 "yfunc\377",
4703
4704 /* \377 (0xff) is Latin1 'ÿ'. */
4705 "\377",
4706 "\377\377123",
4707
c62446b1
PA
4708 /* A name with all sorts of complications. Starts with "z" to make
4709 it easier for the completion tests below. */
4710#define Z_SYM_NAME \
4711 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4712 "::tuple<(anonymous namespace)::ui*, " \
4713 "std::default_delete<(anonymous namespace)::ui>, void>"
4714
4715 Z_SYM_NAME
4716};
4717
a3c5fafd
PA
4718/* Returns true if the mapped_index_base::find_name_component_bounds
4719 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4720 in completion mode. */
5c58de74
PA
4721
4722static bool
a3c5fafd 4723check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4724 const char *search_name,
4725 gdb::array_view<const char *> expected_syms)
4726{
4727 lookup_name_info lookup_name (search_name,
4728 symbol_name_match_type::FULL, true);
4729
3b00ef10
TT
4730 auto bounds = index.find_name_components_bounds (lookup_name,
4731 language_cplus);
5c58de74
PA
4732
4733 size_t distance = std::distance (bounds.first, bounds.second);
4734 if (distance != expected_syms.size ())
4735 return false;
4736
4737 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4738 {
4739 auto nc_elem = bounds.first + exp_elem;
4740 const char *qualified = index.symbol_name_at (nc_elem->idx);
4741 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4742 return false;
4743 }
4744
4745 return true;
4746}
4747
4748/* Test the lower-level mapped_index::find_name_component_bounds
4749 method. */
4750
c62446b1 4751static void
5c58de74
PA
4752test_mapped_index_find_name_component_bounds ()
4753{
4754 mock_mapped_index mock_index (test_symbols);
4755
a3c5fafd 4756 mock_index.build_name_components ();
5c58de74
PA
4757
4758 /* Test the lower-level mapped_index::find_name_component_bounds
4759 method in completion mode. */
4760 {
4761 static const char *expected_syms[] = {
4762 "t1_func",
4763 "t1_func1",
5c58de74
PA
4764 };
4765
a3c5fafd 4766 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4767 "t1_func", expected_syms));
4768 }
4769
4770 /* Check that the increment-last-char in the name matching algorithm
4771 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4772 {
4773 static const char *expected_syms1[] = {
4774 "\377",
4775 "\377\377123",
4776 };
a3c5fafd 4777 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4778 "\377", expected_syms1));
4779
4780 static const char *expected_syms2[] = {
4781 "\377\377123",
4782 };
a3c5fafd 4783 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4784 "\377\377", expected_syms2));
4785 }
4786}
4787
4788/* Test dw2_expand_symtabs_matching_symbol. */
4789
4790static void
4791test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4792{
4793 mock_mapped_index mock_index (test_symbols);
4794
4795 /* We let all tests run until the end even if some fails, for debug
4796 convenience. */
4797 bool any_mismatch = false;
4798
4799 /* Create the expected symbols list (an initializer_list). Needed
4800 because lists have commas, and we need to pass them to CHECK,
4801 which is a macro. */
4802#define EXPECT(...) { __VA_ARGS__ }
4803
4804 /* Wrapper for check_match that passes down the current
4805 __FILE__/__LINE__. */
4806#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4807 any_mismatch |= !check_match (__FILE__, __LINE__, \
4808 mock_index, \
4809 NAME, MATCH_TYPE, COMPLETION_MODE, \
4810 EXPECTED_LIST)
4811
4812 /* Identity checks. */
4813 for (const char *sym : test_symbols)
4814 {
4815 /* Should be able to match all existing symbols. */
4816 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4817 EXPECT (sym));
4818
4819 /* Should be able to match all existing symbols with
4820 parameters. */
4821 std::string with_params = std::string (sym) + "(int)";
4822 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4823 EXPECT (sym));
4824
4825 /* Should be able to match all existing symbols with
4826 parameters and qualifiers. */
4827 with_params = std::string (sym) + " ( int ) const";
4828 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4829 EXPECT (sym));
4830
4831 /* This should really find sym, but cp-name-parser.y doesn't
4832 know about lvalue/rvalue qualifiers yet. */
4833 with_params = std::string (sym) + " ( int ) &&";
4834 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4835 {});
4836 }
4837
e1ef7d7a
PA
4838 /* Check that the name matching algorithm for completion doesn't get
4839 confused with Latin1 'ÿ' / 0xff. */
4840 {
4841 static const char str[] = "\377";
4842 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4843 EXPECT ("\377", "\377\377123"));
4844 }
4845
4846 /* Check that the increment-last-char in the matching algorithm for
4847 completion doesn't match "t1_fund" when completing "t1_func". */
4848 {
4849 static const char str[] = "t1_func";
4850 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4851 EXPECT ("t1_func", "t1_func1"));
4852 }
4853
c62446b1
PA
4854 /* Check that completion mode works at each prefix of the expected
4855 symbol name. */
4856 {
4857 static const char str[] = "function(int)";
4858 size_t len = strlen (str);
4859 std::string lookup;
4860
4861 for (size_t i = 1; i < len; i++)
4862 {
4863 lookup.assign (str, i);
4864 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4865 EXPECT ("function"));
4866 }
4867 }
4868
4869 /* While "w" is a prefix of both components, the match function
4870 should still only be called once. */
4871 {
4872 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4873 EXPECT ("w1::w2"));
a20714ff
PA
4874 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4875 EXPECT ("w1::w2"));
c62446b1
PA
4876 }
4877
4878 /* Same, with a "complicated" symbol. */
4879 {
4880 static const char str[] = Z_SYM_NAME;
4881 size_t len = strlen (str);
4882 std::string lookup;
4883
4884 for (size_t i = 1; i < len; i++)
4885 {
4886 lookup.assign (str, i);
4887 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4888 EXPECT (Z_SYM_NAME));
4889 }
4890 }
4891
4892 /* In FULL mode, an incomplete symbol doesn't match. */
4893 {
4894 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4895 {});
4896 }
4897
4898 /* A complete symbol with parameters matches any overload, since the
4899 index has no overload info. */
4900 {
4901 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4902 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4903 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4904 EXPECT ("std::zfunction", "std::zfunction2"));
4905 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4906 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4907 }
4908
4909 /* Check that whitespace is ignored appropriately. A symbol with a
4910 template argument list. */
4911 {
4912 static const char expected[] = "ns::foo<int>";
4913 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4914 EXPECT (expected));
a20714ff
PA
4915 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4916 EXPECT (expected));
c62446b1
PA
4917 }
4918
4919 /* Check that whitespace is ignored appropriately. A symbol with a
4920 template argument list that includes a pointer. */
4921 {
4922 static const char expected[] = "ns::foo<char*>";
4923 /* Try both completion and non-completion modes. */
4924 static const bool completion_mode[2] = {false, true};
4925 for (size_t i = 0; i < 2; i++)
4926 {
4927 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4928 completion_mode[i], EXPECT (expected));
a20714ff
PA
4929 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4930 completion_mode[i], EXPECT (expected));
c62446b1
PA
4931
4932 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4933 completion_mode[i], EXPECT (expected));
a20714ff
PA
4934 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4935 completion_mode[i], EXPECT (expected));
c62446b1
PA
4936 }
4937 }
4938
4939 {
4940 /* Check method qualifiers are ignored. */
4941 static const char expected[] = "ns::foo<char*>";
4942 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4943 symbol_name_match_type::FULL, true, EXPECT (expected));
4944 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4945 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4946 CHECK_MATCH ("foo < char * > ( int ) const",
4947 symbol_name_match_type::WILD, true, EXPECT (expected));
4948 CHECK_MATCH ("foo < char * > ( int ) &&",
4949 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4950 }
4951
4952 /* Test lookup names that don't match anything. */
4953 {
a20714ff
PA
4954 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4955 {});
4956
c62446b1
PA
4957 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4958 {});
4959 }
4960
a20714ff
PA
4961 /* Some wild matching tests, exercising "(anonymous namespace)",
4962 which should not be confused with a parameter list. */
4963 {
4964 static const char *syms[] = {
4965 "A::B::C",
4966 "B::C",
4967 "C",
4968 "A :: B :: C ( int )",
4969 "B :: C ( int )",
4970 "C ( int )",
4971 };
4972
4973 for (const char *s : syms)
4974 {
4975 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4976 EXPECT ("(anonymous namespace)::A::B::C"));
4977 }
4978 }
4979
4980 {
4981 static const char expected[] = "ns2::tmpl<int>::foo2";
4982 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4983 EXPECT (expected));
4984 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4985 EXPECT (expected));
4986 }
4987
c62446b1
PA
4988 SELF_CHECK (!any_mismatch);
4989
4990#undef EXPECT
4991#undef CHECK_MATCH
4992}
4993
5c58de74
PA
4994static void
4995run_test ()
4996{
4997 test_mapped_index_find_name_component_bounds ();
4998 test_dw2_expand_symtabs_matching_symbol ();
4999}
5000
c62446b1
PA
5001}} // namespace selftests::dw2_expand_symtabs_matching
5002
5003#endif /* GDB_SELF_TEST */
5004
4b514bc8
JK
5005/* If FILE_MATCHER is NULL or if PER_CU has
5006 dwarf2_per_cu_quick_data::MARK set (see
5007 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5008 EXPANSION_NOTIFY on it. */
5009
5010static void
5011dw2_expand_symtabs_matching_one
5012 (struct dwarf2_per_cu_data *per_cu,
5013 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5014 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5015{
5016 if (file_matcher == NULL || per_cu->v.quick->mark)
5017 {
5018 bool symtab_was_null
5019 = (per_cu->v.quick->compunit_symtab == NULL);
5020
58f0c718 5021 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5022
5023 if (expansion_notify != NULL
5024 && symtab_was_null
5025 && per_cu->v.quick->compunit_symtab != NULL)
5026 expansion_notify (per_cu->v.quick->compunit_symtab);
5027 }
5028}
5029
3f563c84
PA
5030/* Helper for dw2_expand_matching symtabs. Called on each symbol
5031 matched, to expand corresponding CUs that were marked. IDX is the
5032 index of the symbol name that matched. */
5033
5034static void
5035dw2_expand_marked_cus
ed2dc618 5036 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5037 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5038 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5039 search_domain kind)
5040{
3f563c84
PA
5041 offset_type *vec, vec_len, vec_idx;
5042 bool global_seen = false;
ed2dc618 5043 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5044
61920122 5045 vec = (offset_type *) (index.constant_pool
f00a2de2 5046 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5047 vec_len = MAYBE_SWAP (vec[0]);
5048 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5049 {
61920122
PA
5050 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5051 /* This value is only valid for index versions >= 7. */
5052 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5053 gdb_index_symbol_kind symbol_kind =
5054 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5055 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5056 /* Only check the symbol attributes if they're present.
5057 Indices prior to version 7 don't record them,
5058 and indices >= 7 may elide them for certain symbols
5059 (gold does this). */
5060 int attrs_valid =
5061 (index.version >= 7
5062 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5063
5064 /* Work around gold/15646. */
5065 if (attrs_valid)
9291a0cd 5066 {
61920122
PA
5067 if (!is_static && global_seen)
5068 continue;
5069 if (!is_static)
5070 global_seen = true;
5071 }
3190f0c6 5072
61920122
PA
5073 /* Only check the symbol's kind if it has one. */
5074 if (attrs_valid)
5075 {
5076 switch (kind)
8943b874 5077 {
61920122
PA
5078 case VARIABLES_DOMAIN:
5079 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5080 continue;
5081 break;
5082 case FUNCTIONS_DOMAIN:
5083 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5084 continue;
61920122
PA
5085 break;
5086 case TYPES_DOMAIN:
5087 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5088 continue;
5089 break;
59c35742
AB
5090 case MODULES_DOMAIN:
5091 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5092 continue;
5093 break;
61920122
PA
5094 default:
5095 break;
8943b874 5096 }
61920122 5097 }
8943b874 5098
61920122 5099 /* Don't crash on bad data. */
b76e467d 5100 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5101 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5102 {
b98664d3 5103 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5104 " [in module %s]"),
5105 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5106 continue;
5107 }
5108
ff4c9fec 5109 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5110 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5111 expansion_notify);
61920122
PA
5112 }
5113}
5114
4b514bc8
JK
5115/* If FILE_MATCHER is non-NULL, set all the
5116 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5117 that match FILE_MATCHER. */
5118
61920122 5119static void
4b514bc8 5120dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5121 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5122 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5123{
4b514bc8 5124 if (file_matcher == NULL)
61920122
PA
5125 return;
5126
4b514bc8
JK
5127 objfile *const objfile = dwarf2_per_objfile->objfile;
5128
5129 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5130 htab_eq_pointer,
5131 NULL, xcalloc, xfree));
5132 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5133 htab_eq_pointer,
5134 NULL, xcalloc, xfree));
61920122 5135
4b514bc8
JK
5136 /* The rule is CUs specify all the files, including those used by
5137 any TU, so there's no need to scan TUs here. */
61920122 5138
b76e467d 5139 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5140 {
927aa2e7
JK
5141 QUIT;
5142
5143 per_cu->v.quick->mark = 0;
5144
5145 /* We only need to look at symtabs not already expanded. */
5146 if (per_cu->v.quick->compunit_symtab)
5147 continue;
5148
b76e467d 5149 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5150 if (file_data == NULL)
5151 continue;
5152
5153 if (htab_find (visited_not_found.get (), file_data) != NULL)
5154 continue;
5155 else if (htab_find (visited_found.get (), file_data) != NULL)
5156 {
5157 per_cu->v.quick->mark = 1;
5158 continue;
5159 }
5160
b76e467d 5161 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5162 {
5163 const char *this_real_name;
5164
5165 if (file_matcher (file_data->file_names[j], false))
5166 {
5167 per_cu->v.quick->mark = 1;
5168 break;
5169 }
5170
5171 /* Before we invoke realpath, which can get expensive when many
5172 files are involved, do a quick comparison of the basenames. */
5173 if (!basenames_may_differ
5174 && !file_matcher (lbasename (file_data->file_names[j]),
5175 true))
5176 continue;
5177
5178 this_real_name = dw2_get_real_path (objfile, file_data, j);
5179 if (file_matcher (this_real_name, false))
5180 {
5181 per_cu->v.quick->mark = 1;
5182 break;
5183 }
5184 }
5185
b76e467d
SM
5186 void **slot = htab_find_slot (per_cu->v.quick->mark
5187 ? visited_found.get ()
5188 : visited_not_found.get (),
5189 file_data, INSERT);
927aa2e7
JK
5190 *slot = file_data;
5191 }
5192}
5193
5194static void
5195dw2_expand_symtabs_matching
5196 (struct objfile *objfile,
5197 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5198 const lookup_name_info &lookup_name,
5199 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5200 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5201 enum search_domain kind)
5202{
ed2dc618
SM
5203 struct dwarf2_per_objfile *dwarf2_per_objfile
5204 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5205
5206 /* index_table is NULL if OBJF_READNOW. */
5207 if (!dwarf2_per_objfile->index_table)
5208 return;
5209
ed2dc618 5210 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5211
5212 mapped_index &index = *dwarf2_per_objfile->index_table;
5213
5214 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5215 symbol_matcher,
5216 kind, [&] (offset_type idx)
5217 {
ed2dc618 5218 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5219 expansion_notify, kind);
3b00ef10 5220 return true;
927aa2e7
JK
5221 });
5222}
5223
5224/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5225 symtab. */
5226
5227static struct compunit_symtab *
5228recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5229 CORE_ADDR pc)
5230{
5231 int i;
5232
5233 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5234 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5235 return cust;
5236
5237 if (cust->includes == NULL)
5238 return NULL;
5239
5240 for (i = 0; cust->includes[i]; ++i)
5241 {
5242 struct compunit_symtab *s = cust->includes[i];
5243
5244 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5245 if (s != NULL)
5246 return s;
5247 }
5248
5249 return NULL;
5250}
5251
5252static struct compunit_symtab *
5253dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5254 struct bound_minimal_symbol msymbol,
5255 CORE_ADDR pc,
5256 struct obj_section *section,
5257 int warn_if_readin)
5258{
5259 struct dwarf2_per_cu_data *data;
5260 struct compunit_symtab *result;
5261
d320c2b5 5262 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5263 return NULL;
5264
b3b3bada 5265 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5266 data = (struct dwarf2_per_cu_data *) addrmap_find
5267 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5268 if (!data)
5269 return NULL;
5270
5271 if (warn_if_readin && data->v.quick->compunit_symtab)
5272 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5273 paddress (get_objfile_arch (objfile), pc));
5274
5275 result
58f0c718
TT
5276 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5277 false),
927aa2e7
JK
5278 pc);
5279 gdb_assert (result != NULL);
5280 return result;
5281}
5282
5283static void
5284dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5285 void *data, int need_fullname)
5286{
ed2dc618
SM
5287 struct dwarf2_per_objfile *dwarf2_per_objfile
5288 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5289
5290 if (!dwarf2_per_objfile->filenames_cache)
5291 {
5292 dwarf2_per_objfile->filenames_cache.emplace ();
5293
5294 htab_up visited (htab_create_alloc (10,
5295 htab_hash_pointer, htab_eq_pointer,
5296 NULL, xcalloc, xfree));
5297
5298 /* The rule is CUs specify all the files, including those used
5299 by any TU, so there's no need to scan TUs here. We can
5300 ignore file names coming from already-expanded CUs. */
5301
b76e467d 5302 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5303 {
927aa2e7
JK
5304 if (per_cu->v.quick->compunit_symtab)
5305 {
5306 void **slot = htab_find_slot (visited.get (),
5307 per_cu->v.quick->file_names,
5308 INSERT);
5309
5310 *slot = per_cu->v.quick->file_names;
5311 }
5312 }
5313
b76e467d 5314 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5315 {
927aa2e7
JK
5316 /* We only need to look at symtabs not already expanded. */
5317 if (per_cu->v.quick->compunit_symtab)
5318 continue;
5319
b76e467d 5320 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5321 if (file_data == NULL)
5322 continue;
5323
b76e467d 5324 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5325 if (*slot)
5326 {
5327 /* Already visited. */
5328 continue;
5329 }
5330 *slot = file_data;
5331
5332 for (int j = 0; j < file_data->num_file_names; ++j)
5333 {
5334 const char *filename = file_data->file_names[j];
5335 dwarf2_per_objfile->filenames_cache->seen (filename);
5336 }
5337 }
5338 }
5339
5340 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5341 {
5342 gdb::unique_xmalloc_ptr<char> this_real_name;
5343
5344 if (need_fullname)
5345 this_real_name = gdb_realpath (filename);
5346 (*fun) (filename, this_real_name.get (), data);
5347 });
5348}
5349
5350static int
5351dw2_has_symbols (struct objfile *objfile)
5352{
5353 return 1;
5354}
5355
5356const struct quick_symbol_functions dwarf2_gdb_index_functions =
5357{
5358 dw2_has_symbols,
5359 dw2_find_last_source_symtab,
5360 dw2_forget_cached_source_info,
5361 dw2_map_symtabs_matching_filename,
5362 dw2_lookup_symbol,
5363 dw2_print_stats,
5364 dw2_dump,
927aa2e7
JK
5365 dw2_expand_symtabs_for_function,
5366 dw2_expand_all_symtabs,
5367 dw2_expand_symtabs_with_fullname,
5368 dw2_map_matching_symbols,
5369 dw2_expand_symtabs_matching,
5370 dw2_find_pc_sect_compunit_symtab,
5371 NULL,
5372 dw2_map_symbol_filenames
5373};
5374
5375/* DWARF-5 debug_names reader. */
5376
5377/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5378static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5379
5380/* A helper function that reads the .debug_names section in SECTION
5381 and fills in MAP. FILENAME is the name of the file containing the
5382 section; it is used for error reporting.
5383
5384 Returns true if all went well, false otherwise. */
5385
5386static bool
5387read_debug_names_from_section (struct objfile *objfile,
5388 const char *filename,
5389 struct dwarf2_section_info *section,
5390 mapped_debug_names &map)
5391{
5392 if (dwarf2_section_empty_p (section))
5393 return false;
5394
5395 /* Older elfutils strip versions could keep the section in the main
5396 executable while splitting it for the separate debug info file. */
5397 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5398 return false;
5399
5400 dwarf2_read_section (objfile, section);
5401
5402 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5403
5404 const gdb_byte *addr = section->buffer;
5405
5406 bfd *const abfd = get_section_bfd_owner (section);
5407
5408 unsigned int bytes_read;
5409 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5410 addr += bytes_read;
5411
5412 map.dwarf5_is_dwarf64 = bytes_read != 4;
5413 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5414 if (bytes_read + length != section->size)
5415 {
5416 /* There may be multiple per-CU indices. */
5417 warning (_("Section .debug_names in %s length %s does not match "
5418 "section length %s, ignoring .debug_names."),
5419 filename, plongest (bytes_read + length),
5420 pulongest (section->size));
5421 return false;
5422 }
5423
5424 /* The version number. */
5425 uint16_t version = read_2_bytes (abfd, addr);
5426 addr += 2;
5427 if (version != 5)
5428 {
5429 warning (_("Section .debug_names in %s has unsupported version %d, "
5430 "ignoring .debug_names."),
5431 filename, version);
5432 return false;
5433 }
5434
5435 /* Padding. */
5436 uint16_t padding = read_2_bytes (abfd, addr);
5437 addr += 2;
5438 if (padding != 0)
5439 {
5440 warning (_("Section .debug_names in %s has unsupported padding %d, "
5441 "ignoring .debug_names."),
5442 filename, padding);
5443 return false;
5444 }
5445
5446 /* comp_unit_count - The number of CUs in the CU list. */
5447 map.cu_count = read_4_bytes (abfd, addr);
5448 addr += 4;
5449
5450 /* local_type_unit_count - The number of TUs in the local TU
5451 list. */
5452 map.tu_count = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* foreign_type_unit_count - The number of TUs in the foreign TU
5456 list. */
5457 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5458 addr += 4;
5459 if (foreign_tu_count != 0)
5460 {
5461 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5462 "ignoring .debug_names."),
5463 filename, static_cast<unsigned long> (foreign_tu_count));
5464 return false;
5465 }
5466
5467 /* bucket_count - The number of hash buckets in the hash lookup
5468 table. */
5469 map.bucket_count = read_4_bytes (abfd, addr);
5470 addr += 4;
5471
5472 /* name_count - The number of unique names in the index. */
5473 map.name_count = read_4_bytes (abfd, addr);
5474 addr += 4;
5475
5476 /* abbrev_table_size - The size in bytes of the abbreviations
5477 table. */
5478 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5479 addr += 4;
5480
5481 /* augmentation_string_size - The size in bytes of the augmentation
5482 string. This value is rounded up to a multiple of 4. */
5483 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5484 addr += 4;
5485 map.augmentation_is_gdb = ((augmentation_string_size
5486 == sizeof (dwarf5_augmentation))
5487 && memcmp (addr, dwarf5_augmentation,
5488 sizeof (dwarf5_augmentation)) == 0);
5489 augmentation_string_size += (-augmentation_string_size) & 3;
5490 addr += augmentation_string_size;
5491
5492 /* List of CUs */
5493 map.cu_table_reordered = addr;
5494 addr += map.cu_count * map.offset_size;
5495
5496 /* List of Local TUs */
5497 map.tu_table_reordered = addr;
5498 addr += map.tu_count * map.offset_size;
5499
5500 /* Hash Lookup Table */
5501 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5502 addr += map.bucket_count * 4;
5503 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5504 addr += map.name_count * 4;
5505
5506 /* Name Table */
5507 map.name_table_string_offs_reordered = addr;
5508 addr += map.name_count * map.offset_size;
5509 map.name_table_entry_offs_reordered = addr;
5510 addr += map.name_count * map.offset_size;
5511
5512 const gdb_byte *abbrev_table_start = addr;
5513 for (;;)
5514 {
927aa2e7
JK
5515 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5517 if (index_num == 0)
5518 break;
5519
5520 const auto insertpair
5521 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5522 if (!insertpair.second)
5523 {
5524 warning (_("Section .debug_names in %s has duplicate index %s, "
5525 "ignoring .debug_names."),
5526 filename, pulongest (index_num));
5527 return false;
5528 }
5529 mapped_debug_names::index_val &indexval = insertpair.first->second;
5530 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5531 addr += bytes_read;
5532
5533 for (;;)
5534 {
5535 mapped_debug_names::index_val::attr attr;
5536 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5537 addr += bytes_read;
5538 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5539 addr += bytes_read;
5540 if (attr.form == DW_FORM_implicit_const)
5541 {
5542 attr.implicit_const = read_signed_leb128 (abfd, addr,
5543 &bytes_read);
5544 addr += bytes_read;
5545 }
5546 if (attr.dw_idx == 0 && attr.form == 0)
5547 break;
5548 indexval.attr_vec.push_back (std::move (attr));
5549 }
5550 }
5551 if (addr != abbrev_table_start + abbrev_table_size)
5552 {
5553 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5554 "of size %s vs. written as %u, ignoring .debug_names."),
5555 filename, plongest (addr - abbrev_table_start),
5556 abbrev_table_size);
927aa2e7
JK
5557 return false;
5558 }
5559 map.entry_pool = addr;
5560
5561 return true;
5562}
5563
5564/* A helper for create_cus_from_debug_names that handles the MAP's CU
5565 list. */
5566
5567static void
ed2dc618 5568create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5569 const mapped_debug_names &map,
5570 dwarf2_section_info &section,
b76e467d 5571 bool is_dwz)
927aa2e7
JK
5572{
5573 sect_offset sect_off_prev;
5574 for (uint32_t i = 0; i <= map.cu_count; ++i)
5575 {
5576 sect_offset sect_off_next;
5577 if (i < map.cu_count)
5578 {
5579 sect_off_next
5580 = (sect_offset) (extract_unsigned_integer
5581 (map.cu_table_reordered + i * map.offset_size,
5582 map.offset_size,
5583 map.dwarf5_byte_order));
5584 }
5585 else
5586 sect_off_next = (sect_offset) section.size;
5587 if (i >= 1)
5588 {
5589 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5590 dwarf2_per_cu_data *per_cu
ed2dc618 5591 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5592 sect_off_prev, length);
b76e467d 5593 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5594 }
5595 sect_off_prev = sect_off_next;
5596 }
5597}
5598
5599/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5600 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5601
5602static void
ed2dc618 5603create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5604 const mapped_debug_names &map,
5605 const mapped_debug_names &dwz_map)
5606{
b76e467d
SM
5607 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5608 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5609
ed2dc618
SM
5610 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5611 dwarf2_per_objfile->info,
b76e467d 5612 false /* is_dwz */);
927aa2e7
JK
5613
5614 if (dwz_map.cu_count == 0)
5615 return;
5616
ed2dc618
SM
5617 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5618 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5619 true /* is_dwz */);
927aa2e7
JK
5620}
5621
5622/* Read .debug_names. If everything went ok, initialize the "quick"
5623 elements of all the CUs and return true. Otherwise, return false. */
5624
5625static bool
ed2dc618 5626dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5627{
22ca247e
TT
5628 std::unique_ptr<mapped_debug_names> map
5629 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5630 mapped_debug_names dwz_map (dwarf2_per_objfile);
5631 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5632
5633 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5634 &dwarf2_per_objfile->debug_names,
22ca247e 5635 *map))
927aa2e7
JK
5636 return false;
5637
5638 /* Don't use the index if it's empty. */
22ca247e 5639 if (map->name_count == 0)
927aa2e7
JK
5640 return false;
5641
5642 /* If there is a .dwz file, read it so we can get its CU list as
5643 well. */
ed2dc618 5644 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5645 if (dwz != NULL)
5646 {
5647 if (!read_debug_names_from_section (objfile,
00f93c44 5648 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5649 &dwz->debug_names, dwz_map))
5650 {
5651 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5652 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5653 return false;
5654 }
5655 }
5656
22ca247e 5657 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5658
22ca247e 5659 if (map->tu_count != 0)
927aa2e7
JK
5660 {
5661 /* We can only handle a single .debug_types when we have an
5662 index. */
fd5866f6 5663 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5664 return false;
5665
fd5866f6 5666 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5667
5668 create_signatured_type_table_from_debug_names
22ca247e 5669 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5670 }
5671
ed2dc618
SM
5672 create_addrmap_from_aranges (dwarf2_per_objfile,
5673 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5674
22ca247e 5675 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5676 dwarf2_per_objfile->using_index = 1;
5677 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5678 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5679
5680 return true;
5681}
5682
927aa2e7
JK
5683/* Type used to manage iterating over all CUs looking for a symbol for
5684 .debug_names. */
5685
5686class dw2_debug_names_iterator
5687{
5688public:
927aa2e7 5689 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5690 gdb::optional<block_enum> block_index,
5691 domain_enum domain,
927aa2e7 5692 const char *name)
2b79f376 5693 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5694 m_addr (find_vec_in_debug_names (map, name))
5695 {}
5696
5697 dw2_debug_names_iterator (const mapped_debug_names &map,
5698 search_domain search, uint32_t namei)
5699 : m_map (map),
5700 m_search (search),
5701 m_addr (find_vec_in_debug_names (map, namei))
5702 {}
5703
3b00ef10
TT
5704 dw2_debug_names_iterator (const mapped_debug_names &map,
5705 block_enum block_index, domain_enum domain,
5706 uint32_t namei)
5707 : m_map (map), m_block_index (block_index), m_domain (domain),
5708 m_addr (find_vec_in_debug_names (map, namei))
5709 {}
5710
927aa2e7
JK
5711 /* Return the next matching CU or NULL if there are no more. */
5712 dwarf2_per_cu_data *next ();
5713
5714private:
5715 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5716 const char *name);
5717 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5718 uint32_t namei);
5719
5720 /* The internalized form of .debug_names. */
5721 const mapped_debug_names &m_map;
5722
2b79f376
SM
5723 /* If set, only look for symbols that match that block. Valid values are
5724 GLOBAL_BLOCK and STATIC_BLOCK. */
5725 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5726
5727 /* The kind of symbol we're looking for. */
5728 const domain_enum m_domain = UNDEF_DOMAIN;
5729 const search_domain m_search = ALL_DOMAIN;
5730
5731 /* The list of CUs from the index entry of the symbol, or NULL if
5732 not found. */
5733 const gdb_byte *m_addr;
5734};
5735
5736const char *
5737mapped_debug_names::namei_to_name (uint32_t namei) const
5738{
5739 const ULONGEST namei_string_offs
5740 = extract_unsigned_integer ((name_table_string_offs_reordered
5741 + namei * offset_size),
5742 offset_size,
5743 dwarf5_byte_order);
5744 return read_indirect_string_at_offset
ed2dc618 5745 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5746}
5747
5748/* Find a slot in .debug_names for the object named NAME. If NAME is
5749 found, return pointer to its pool data. If NAME cannot be found,
5750 return NULL. */
5751
5752const gdb_byte *
5753dw2_debug_names_iterator::find_vec_in_debug_names
5754 (const mapped_debug_names &map, const char *name)
5755{
5756 int (*cmp) (const char *, const char *);
5757
54ee4252 5758 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5759 if (current_language->la_language == language_cplus
5760 || current_language->la_language == language_fortran
5761 || current_language->la_language == language_d)
5762 {
5763 /* NAME is already canonical. Drop any qualifiers as
5764 .debug_names does not contain any. */
5765
5766 if (strchr (name, '(') != NULL)
5767 {
54ee4252 5768 without_params = cp_remove_params (name);
927aa2e7 5769 if (without_params != NULL)
54ee4252 5770 name = without_params.get ();
927aa2e7
JK
5771 }
5772 }
5773
5774 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5775
5776 const uint32_t full_hash = dwarf5_djb_hash (name);
5777 uint32_t namei
5778 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5779 (map.bucket_table_reordered
5780 + (full_hash % map.bucket_count)), 4,
5781 map.dwarf5_byte_order);
5782 if (namei == 0)
5783 return NULL;
5784 --namei;
5785 if (namei >= map.name_count)
5786 {
b98664d3 5787 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5788 "[in module %s]"),
5789 namei, map.name_count,
ed2dc618 5790 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5791 return NULL;
5792 }
5793
5794 for (;;)
5795 {
5796 const uint32_t namei_full_hash
5797 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5798 (map.hash_table_reordered + namei), 4,
5799 map.dwarf5_byte_order);
5800 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5801 return NULL;
5802
5803 if (full_hash == namei_full_hash)
5804 {
5805 const char *const namei_string = map.namei_to_name (namei);
5806
5807#if 0 /* An expensive sanity check. */
5808 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5809 {
b98664d3 5810 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5811 "[in module %s]"),
5812 namei, objfile_name (dwarf2_per_objfile->objfile));
5813 return NULL;
5814 }
5815#endif
5816
5817 if (cmp (namei_string, name) == 0)
5818 {
5819 const ULONGEST namei_entry_offs
5820 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5821 + namei * map.offset_size),
5822 map.offset_size, map.dwarf5_byte_order);
5823 return map.entry_pool + namei_entry_offs;
5824 }
5825 }
5826
5827 ++namei;
5828 if (namei >= map.name_count)
5829 return NULL;
5830 }
5831}
5832
5833const gdb_byte *
5834dw2_debug_names_iterator::find_vec_in_debug_names
5835 (const mapped_debug_names &map, uint32_t namei)
5836{
5837 if (namei >= map.name_count)
5838 {
b98664d3 5839 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5840 "[in module %s]"),
5841 namei, map.name_count,
ed2dc618 5842 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5843 return NULL;
5844 }
5845
5846 const ULONGEST namei_entry_offs
5847 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5848 + namei * map.offset_size),
5849 map.offset_size, map.dwarf5_byte_order);
5850 return map.entry_pool + namei_entry_offs;
5851}
5852
5853/* See dw2_debug_names_iterator. */
5854
5855dwarf2_per_cu_data *
5856dw2_debug_names_iterator::next ()
5857{
5858 if (m_addr == NULL)
5859 return NULL;
5860
ed2dc618
SM
5861 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5862 struct objfile *objfile = dwarf2_per_objfile->objfile;
5863 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5864
5865 again:
5866
5867 unsigned int bytes_read;
5868 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5869 m_addr += bytes_read;
5870 if (abbrev == 0)
5871 return NULL;
5872
5873 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5874 if (indexval_it == m_map.abbrev_map.cend ())
5875 {
b98664d3 5876 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5877 "[in module %s]"),
ed2dc618 5878 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5879 return NULL;
5880 }
5881 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5882 enum class symbol_linkage {
5883 unknown,
5884 static_,
5885 extern_,
23c13d42 5886 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5887 dwarf2_per_cu_data *per_cu = NULL;
5888 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5889 {
5890 ULONGEST ull;
5891 switch (attr.form)
5892 {
5893 case DW_FORM_implicit_const:
5894 ull = attr.implicit_const;
5895 break;
5896 case DW_FORM_flag_present:
5897 ull = 1;
5898 break;
5899 case DW_FORM_udata:
5900 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5901 m_addr += bytes_read;
5902 break;
5903 default:
b98664d3 5904 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5905 dwarf_form_name (attr.form),
ed2dc618 5906 objfile_name (objfile));
927aa2e7
JK
5907 return NULL;
5908 }
5909 switch (attr.dw_idx)
5910 {
5911 case DW_IDX_compile_unit:
5912 /* Don't crash on bad data. */
b76e467d 5913 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5914 {
b98664d3 5915 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5916 " [in module %s]"),
5917 pulongest (ull),
5918 objfile_name (dwarf2_per_objfile->objfile));
5919 continue;
5920 }
ff4c9fec 5921 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5922 break;
8af5c486
JK
5923 case DW_IDX_type_unit:
5924 /* Don't crash on bad data. */
b2bdb8cf 5925 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5926 {
b98664d3 5927 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5928 " [in module %s]"),
5929 pulongest (ull),
5930 objfile_name (dwarf2_per_objfile->objfile));
5931 continue;
5932 }
ff4c9fec 5933 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5934 break;
927aa2e7
JK
5935 case DW_IDX_GNU_internal:
5936 if (!m_map.augmentation_is_gdb)
5937 break;
23c13d42 5938 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5939 break;
5940 case DW_IDX_GNU_external:
5941 if (!m_map.augmentation_is_gdb)
5942 break;
23c13d42 5943 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5944 break;
5945 }
5946 }
5947
5948 /* Skip if already read in. */
5949 if (per_cu->v.quick->compunit_symtab)
5950 goto again;
5951
5952 /* Check static vs global. */
23c13d42 5953 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5954 {
2b79f376 5955 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5956 const bool symbol_is_static =
5957 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5958 if (want_static != symbol_is_static)
2b79f376 5959 goto again;
927aa2e7
JK
5960 }
5961
5962 /* Match dw2_symtab_iter_next, symbol_kind
5963 and debug_names::psymbol_tag. */
5964 switch (m_domain)
5965 {
5966 case VAR_DOMAIN:
5967 switch (indexval.dwarf_tag)
5968 {
5969 case DW_TAG_variable:
5970 case DW_TAG_subprogram:
5971 /* Some types are also in VAR_DOMAIN. */
5972 case DW_TAG_typedef:
5973 case DW_TAG_structure_type:
5974 break;
5975 default:
5976 goto again;
5977 }
5978 break;
5979 case STRUCT_DOMAIN:
5980 switch (indexval.dwarf_tag)
5981 {
5982 case DW_TAG_typedef:
5983 case DW_TAG_structure_type:
5984 break;
5985 default:
5986 goto again;
5987 }
5988 break;
5989 case LABEL_DOMAIN:
5990 switch (indexval.dwarf_tag)
5991 {
5992 case 0:
5993 case DW_TAG_variable:
5994 break;
5995 default:
5996 goto again;
5997 }
5998 break;
59c35742
AB
5999 case MODULE_DOMAIN:
6000 switch (indexval.dwarf_tag)
6001 {
6002 case DW_TAG_module:
6003 break;
6004 default:
6005 goto again;
6006 }
6007 break;
927aa2e7
JK
6008 default:
6009 break;
6010 }
6011
6012 /* Match dw2_expand_symtabs_matching, symbol_kind and
6013 debug_names::psymbol_tag. */
6014 switch (m_search)
4b514bc8 6015 {
927aa2e7
JK
6016 case VARIABLES_DOMAIN:
6017 switch (indexval.dwarf_tag)
4b514bc8 6018 {
927aa2e7
JK
6019 case DW_TAG_variable:
6020 break;
6021 default:
6022 goto again;
4b514bc8 6023 }
927aa2e7
JK
6024 break;
6025 case FUNCTIONS_DOMAIN:
6026 switch (indexval.dwarf_tag)
4b514bc8 6027 {
927aa2e7
JK
6028 case DW_TAG_subprogram:
6029 break;
6030 default:
6031 goto again;
4b514bc8 6032 }
927aa2e7
JK
6033 break;
6034 case TYPES_DOMAIN:
6035 switch (indexval.dwarf_tag)
6036 {
6037 case DW_TAG_typedef:
6038 case DW_TAG_structure_type:
6039 break;
6040 default:
6041 goto again;
6042 }
6043 break;
59c35742
AB
6044 case MODULES_DOMAIN:
6045 switch (indexval.dwarf_tag)
6046 {
6047 case DW_TAG_module:
6048 break;
6049 default:
6050 goto again;
6051 }
927aa2e7
JK
6052 default:
6053 break;
4b514bc8 6054 }
927aa2e7
JK
6055
6056 return per_cu;
4b514bc8 6057}
61920122 6058
927aa2e7 6059static struct compunit_symtab *
c7f839cb 6060dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6061 const char *name, domain_enum domain)
4b514bc8 6062{
ed2dc618
SM
6063 struct dwarf2_per_objfile *dwarf2_per_objfile
6064 = get_dwarf2_per_objfile (objfile);
61920122 6065
927aa2e7
JK
6066 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6067 if (!mapp)
61920122 6068 {
927aa2e7
JK
6069 /* index is NULL if OBJF_READNOW. */
6070 return NULL;
6071 }
6072 const auto &map = *mapp;
9291a0cd 6073
2b79f376 6074 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6075
927aa2e7
JK
6076 struct compunit_symtab *stab_best = NULL;
6077 struct dwarf2_per_cu_data *per_cu;
6078 while ((per_cu = iter.next ()) != NULL)
6079 {
6080 struct symbol *sym, *with_opaque = NULL;
58f0c718 6081 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6082 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6083 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6084
927aa2e7
JK
6085 sym = block_find_symbol (block, name, domain,
6086 block_find_non_opaque_type_preferred,
6087 &with_opaque);
9703b513 6088
927aa2e7
JK
6089 /* Some caution must be observed with overloaded functions and
6090 methods, since the index will not contain any overload
6091 information (but NAME might contain it). */
a3ec0bb1 6092
927aa2e7 6093 if (sym != NULL
987012b8 6094 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6095 return stab;
6096 if (with_opaque != NULL
987012b8 6097 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6098 stab_best = stab;
9703b513 6099
927aa2e7 6100 /* Keep looking through other CUs. */
9703b513
TT
6101 }
6102
927aa2e7 6103 return stab_best;
9703b513
TT
6104}
6105
927aa2e7
JK
6106/* This dumps minimal information about .debug_names. It is called
6107 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6108 uses this to verify that .debug_names has been loaded. */
9291a0cd 6109
927aa2e7
JK
6110static void
6111dw2_debug_names_dump (struct objfile *objfile)
6112{
ed2dc618
SM
6113 struct dwarf2_per_objfile *dwarf2_per_objfile
6114 = get_dwarf2_per_objfile (objfile);
6115
927aa2e7
JK
6116 gdb_assert (dwarf2_per_objfile->using_index);
6117 printf_filtered (".debug_names:");
6118 if (dwarf2_per_objfile->debug_names_table)
6119 printf_filtered (" exists\n");
6120 else
6121 printf_filtered (" faked for \"readnow\"\n");
6122 printf_filtered ("\n");
9291a0cd
TT
6123}
6124
9291a0cd 6125static void
927aa2e7
JK
6126dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6127 const char *func_name)
9291a0cd 6128{
ed2dc618
SM
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6131
927aa2e7
JK
6132 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6133 if (dwarf2_per_objfile->debug_names_table)
24c79950 6134 {
927aa2e7 6135 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6136
2b79f376 6137 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6138
927aa2e7
JK
6139 struct dwarf2_per_cu_data *per_cu;
6140 while ((per_cu = iter.next ()) != NULL)
58f0c718 6141 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6142 }
6143}
24c79950 6144
3b00ef10
TT
6145static void
6146dw2_debug_names_map_matching_symbols
6147 (struct objfile *objfile,
6148 const lookup_name_info &name, domain_enum domain,
6149 int global,
6150 gdb::function_view<symbol_found_callback_ftype> callback,
6151 symbol_compare_ftype *ordered_compare)
6152{
6153 struct dwarf2_per_objfile *dwarf2_per_objfile
6154 = get_dwarf2_per_objfile (objfile);
6155
6156 /* debug_names_table is NULL if OBJF_READNOW. */
6157 if (!dwarf2_per_objfile->debug_names_table)
6158 return;
6159
6160 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6161 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6162
6163 const char *match_name = name.ada ().lookup_name ().c_str ();
6164 auto matcher = [&] (const char *symname)
6165 {
6166 if (ordered_compare == nullptr)
6167 return true;
6168 return ordered_compare (symname, match_name) == 0;
6169 };
6170
6171 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6172 [&] (offset_type namei)
6173 {
6174 /* The name was matched, now expand corresponding CUs that were
6175 marked. */
6176 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6177
6178 struct dwarf2_per_cu_data *per_cu;
6179 while ((per_cu = iter.next ()) != NULL)
6180 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6181 return true;
6182 });
6183
6184 /* It's a shame we couldn't do this inside the
6185 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6186 that have already been expanded. Instead, this loop matches what
6187 the psymtab code does. */
6188 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6189 {
6190 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6191 if (cust != nullptr)
6192 {
6193 const struct block *block
6194 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6195 if (!iterate_over_symbols_terminated (block, name,
6196 domain, callback))
6197 break;
6198 }
6199 }
6200}
6201
927aa2e7
JK
6202static void
6203dw2_debug_names_expand_symtabs_matching
6204 (struct objfile *objfile,
6205 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6206 const lookup_name_info &lookup_name,
6207 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6208 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6209 enum search_domain kind)
6210{
ed2dc618
SM
6211 struct dwarf2_per_objfile *dwarf2_per_objfile
6212 = get_dwarf2_per_objfile (objfile);
9291a0cd 6213
927aa2e7
JK
6214 /* debug_names_table is NULL if OBJF_READNOW. */
6215 if (!dwarf2_per_objfile->debug_names_table)
6216 return;
9291a0cd 6217
ed2dc618 6218 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6219
44ed8f3e 6220 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6221
44ed8f3e
PA
6222 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6223 symbol_matcher,
6224 kind, [&] (offset_type namei)
927aa2e7 6225 {
927aa2e7
JK
6226 /* The name was matched, now expand corresponding CUs that were
6227 marked. */
6228 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6229
927aa2e7
JK
6230 struct dwarf2_per_cu_data *per_cu;
6231 while ((per_cu = iter.next ()) != NULL)
6232 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6233 expansion_notify);
3b00ef10 6234 return true;
44ed8f3e 6235 });
9291a0cd
TT
6236}
6237
927aa2e7 6238const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6239{
6240 dw2_has_symbols,
6241 dw2_find_last_source_symtab,
6242 dw2_forget_cached_source_info,
f8eba3c6 6243 dw2_map_symtabs_matching_filename,
927aa2e7 6244 dw2_debug_names_lookup_symbol,
9291a0cd 6245 dw2_print_stats,
927aa2e7 6246 dw2_debug_names_dump,
927aa2e7 6247 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6248 dw2_expand_all_symtabs,
652a8996 6249 dw2_expand_symtabs_with_fullname,
3b00ef10 6250 dw2_debug_names_map_matching_symbols,
927aa2e7 6251 dw2_debug_names_expand_symtabs_matching,
43f3e411 6252 dw2_find_pc_sect_compunit_symtab,
71a3c369 6253 NULL,
9291a0cd
TT
6254 dw2_map_symbol_filenames
6255};
6256
4485a1c1
SM
6257/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6258 to either a dwarf2_per_objfile or dwz_file object. */
6259
6260template <typename T>
6261static gdb::array_view<const gdb_byte>
6262get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6263{
6264 dwarf2_section_info *section = &section_owner->gdb_index;
6265
6266 if (dwarf2_section_empty_p (section))
6267 return {};
6268
6269 /* Older elfutils strip versions could keep the section in the main
6270 executable while splitting it for the separate debug info file. */
6271 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6272 return {};
6273
6274 dwarf2_read_section (obj, section);
6275
8bebfcda
PA
6276 /* dwarf2_section_info::size is a bfd_size_type, while
6277 gdb::array_view works with size_t. On 32-bit hosts, with
6278 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6279 is 32-bit. So we need an explicit narrowing conversion here.
6280 This is fine, because it's impossible to allocate or mmap an
6281 array/buffer larger than what size_t can represent. */
6282 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6283}
6284
87d6a7aa
SM
6285/* Lookup the index cache for the contents of the index associated to
6286 DWARF2_OBJ. */
6287
6288static gdb::array_view<const gdb_byte>
6289get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6290{
6291 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6292 if (build_id == nullptr)
6293 return {};
6294
6295 return global_index_cache.lookup_gdb_index (build_id,
6296 &dwarf2_obj->index_cache_res);
6297}
6298
6299/* Same as the above, but for DWZ. */
6300
6301static gdb::array_view<const gdb_byte>
6302get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6303{
6304 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6305 if (build_id == nullptr)
6306 return {};
6307
6308 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6309}
6310
3c0aa29a 6311/* See symfile.h. */
9291a0cd 6312
3c0aa29a
PA
6313bool
6314dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6315{
ed2dc618
SM
6316 struct dwarf2_per_objfile *dwarf2_per_objfile
6317 = get_dwarf2_per_objfile (objfile);
6318
9291a0cd
TT
6319 /* If we're about to read full symbols, don't bother with the
6320 indices. In this case we also don't care if some other debug
6321 format is making psymtabs, because they are all about to be
6322 expanded anyway. */
6323 if ((objfile->flags & OBJF_READNOW))
6324 {
9291a0cd 6325 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6326 create_all_comp_units (dwarf2_per_objfile);
6327 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6328 dwarf2_per_objfile->quick_file_names_table
6329 = create_quick_file_names_table
6330 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6331
b76e467d 6332 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6333 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6334 {
ff4c9fec 6335 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6336
e254ef6a
DE
6337 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6338 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6339 }
6340
6341 /* Return 1 so that gdb sees the "quick" functions. However,
6342 these functions will be no-ops because we will have expanded
6343 all symtabs. */
3c0aa29a
PA
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
9291a0cd
TT
6346 }
6347
ed2dc618 6348 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6349 {
6350 *index_kind = dw_index_kind::DEBUG_NAMES;
6351 return true;
6352 }
927aa2e7 6353
4485a1c1
SM
6354 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6355 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6356 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6357 {
6358 *index_kind = dw_index_kind::GDB_INDEX;
6359 return true;
6360 }
9291a0cd 6361
87d6a7aa
SM
6362 /* ... otherwise, try to find the index in the index cache. */
6363 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6364 get_gdb_index_contents_from_cache,
6365 get_gdb_index_contents_from_cache_dwz))
6366 {
6367 global_index_cache.hit ();
6368 *index_kind = dw_index_kind::GDB_INDEX;
6369 return true;
6370 }
6371
6372 global_index_cache.miss ();
3c0aa29a 6373 return false;
9291a0cd
TT
6374}
6375
6376\f
6377
dce234bc
PP
6378/* Build a partial symbol table. */
6379
6380void
f29dff0a 6381dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6382{
ed2dc618
SM
6383 struct dwarf2_per_objfile *dwarf2_per_objfile
6384 = get_dwarf2_per_objfile (objfile);
c9bf0622 6385
6eee24ce 6386 init_psymbol_list (objfile, 1024);
c906108c 6387
a70b8144 6388 try
c9bf0622
TT
6389 {
6390 /* This isn't really ideal: all the data we allocate on the
6391 objfile's obstack is still uselessly kept around. However,
6392 freeing it seems unsafe. */
906768f9 6393 psymtab_discarder psymtabs (objfile);
ed2dc618 6394 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6395 psymtabs.keep ();
87d6a7aa
SM
6396
6397 /* (maybe) store an index in the cache. */
6398 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6399 }
230d2906 6400 catch (const gdb_exception_error &except)
492d29ea
PA
6401 {
6402 exception_print (gdb_stderr, except);
6403 }
c906108c 6404}
c906108c 6405
1ce1cefd
DE
6406/* Return the total length of the CU described by HEADER. */
6407
6408static unsigned int
6409get_cu_length (const struct comp_unit_head *header)
6410{
6411 return header->initial_length_size + header->length;
6412}
6413
9c541725 6414/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6415
9c541725
PA
6416static inline bool
6417offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6418{
9c541725
PA
6419 sect_offset bottom = cu_header->sect_off;
6420 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6421
9c541725 6422 return sect_off >= bottom && sect_off < top;
45452591
DE
6423}
6424
3b80fe9b
DE
6425/* Find the base address of the compilation unit for range lists and
6426 location lists. It will normally be specified by DW_AT_low_pc.
6427 In DWARF-3 draft 4, the base address could be overridden by
6428 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6429 compilation units with discontinuous ranges. */
6430
6431static void
6432dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6433{
6434 struct attribute *attr;
6435
6436 cu->base_known = 0;
6437 cu->base_address = 0;
6438
6439 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6440 if (attr != nullptr)
3b80fe9b 6441 {
31aa7e4e 6442 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6443 cu->base_known = 1;
6444 }
6445 else
6446 {
6447 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6448 if (attr != nullptr)
3b80fe9b 6449 {
31aa7e4e 6450 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6451 cu->base_known = 1;
6452 }
6453 }
6454}
6455
93311388 6456/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6457 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6458 NOTE: This leaves members offset, first_die_offset to be filled in
6459 by the caller. */
107d2387 6460
d521ce57 6461static const gdb_byte *
107d2387 6462read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6463 const gdb_byte *info_ptr,
6464 struct dwarf2_section_info *section,
6465 rcuh_kind section_kind)
107d2387
AC
6466{
6467 int signed_addr;
891d2f0b 6468 unsigned int bytes_read;
43988095
JK
6469 const char *filename = get_section_file_name (section);
6470 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6471
6472 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6473 cu_header->initial_length_size = bytes_read;
6474 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6475 info_ptr += bytes_read;
107d2387 6476 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6477 if (cu_header->version < 2 || cu_header->version > 5)
6478 error (_("Dwarf Error: wrong version in compilation unit header "
6479 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6480 cu_header->version, filename);
107d2387 6481 info_ptr += 2;
43988095
JK
6482 if (cu_header->version < 5)
6483 switch (section_kind)
6484 {
6485 case rcuh_kind::COMPILE:
6486 cu_header->unit_type = DW_UT_compile;
6487 break;
6488 case rcuh_kind::TYPE:
6489 cu_header->unit_type = DW_UT_type;
6490 break;
6491 default:
6492 internal_error (__FILE__, __LINE__,
6493 _("read_comp_unit_head: invalid section_kind"));
6494 }
6495 else
6496 {
6497 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6498 (read_1_byte (abfd, info_ptr));
6499 info_ptr += 1;
6500 switch (cu_header->unit_type)
6501 {
6502 case DW_UT_compile:
a084a2a6
AT
6503 case DW_UT_partial:
6504 case DW_UT_skeleton:
6505 case DW_UT_split_compile:
43988095
JK
6506 if (section_kind != rcuh_kind::COMPILE)
6507 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6508 "(is %s, should be %s) [in module %s]"),
6509 dwarf_unit_type_name (cu_header->unit_type),
6510 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6511 break;
6512 case DW_UT_type:
a084a2a6 6513 case DW_UT_split_type:
43988095
JK
6514 section_kind = rcuh_kind::TYPE;
6515 break;
6516 default:
6517 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6518 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6519 "[in module %s]"), cu_header->unit_type,
6520 dwarf_unit_type_name (DW_UT_compile),
6521 dwarf_unit_type_name (DW_UT_skeleton),
6522 dwarf_unit_type_name (DW_UT_split_compile),
6523 dwarf_unit_type_name (DW_UT_type),
6524 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6525 }
6526
6527 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6528 info_ptr += 1;
6529 }
9c541725
PA
6530 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6531 cu_header,
6532 &bytes_read);
613e1657 6533 info_ptr += bytes_read;
43988095
JK
6534 if (cu_header->version < 5)
6535 {
6536 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6537 info_ptr += 1;
6538 }
107d2387
AC
6539 signed_addr = bfd_get_sign_extend_vma (abfd);
6540 if (signed_addr < 0)
8e65ff28 6541 internal_error (__FILE__, __LINE__,
e2e0b3e5 6542 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6543 cu_header->signed_addr_p = signed_addr;
c764a876 6544
a084a2a6
AT
6545 bool header_has_signature = section_kind == rcuh_kind::TYPE
6546 || cu_header->unit_type == DW_UT_skeleton
6547 || cu_header->unit_type == DW_UT_split_compile;
43988095 6548
a084a2a6
AT
6549 if (header_has_signature)
6550 {
43988095
JK
6551 cu_header->signature = read_8_bytes (abfd, info_ptr);
6552 info_ptr += 8;
a084a2a6 6553 }
43988095 6554
a084a2a6
AT
6555 if (section_kind == rcuh_kind::TYPE)
6556 {
6557 LONGEST type_offset;
43988095
JK
6558 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6559 info_ptr += bytes_read;
9c541725
PA
6560 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6561 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6562 error (_("Dwarf Error: Too big type_offset in compilation unit "
6563 "header (is %s) [in module %s]"), plongest (type_offset),
6564 filename);
6565 }
6566
107d2387
AC
6567 return info_ptr;
6568}
6569
36586728
TT
6570/* Helper function that returns the proper abbrev section for
6571 THIS_CU. */
6572
6573static struct dwarf2_section_info *
6574get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6575{
6576 struct dwarf2_section_info *abbrev;
ed2dc618 6577 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6578
6579 if (this_cu->is_dwz)
ed2dc618 6580 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6581 else
6582 abbrev = &dwarf2_per_objfile->abbrev;
6583
6584 return abbrev;
6585}
6586
9ff913ba
DE
6587/* Subroutine of read_and_check_comp_unit_head and
6588 read_and_check_type_unit_head to simplify them.
6589 Perform various error checking on the header. */
6590
6591static void
ed2dc618
SM
6592error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6593 struct comp_unit_head *header,
4bdcc0c1
DE
6594 struct dwarf2_section_info *section,
6595 struct dwarf2_section_info *abbrev_section)
9ff913ba 6596{
a32a8923 6597 const char *filename = get_section_file_name (section);
9ff913ba 6598
9c541725 6599 if (to_underlying (header->abbrev_sect_off)
36586728 6600 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6601 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6602 "(offset %s + 6) [in module %s]"),
6603 sect_offset_str (header->abbrev_sect_off),
6604 sect_offset_str (header->sect_off),
9ff913ba
DE
6605 filename);
6606
9c541725 6607 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6608 avoid potential 32-bit overflow. */
9c541725 6609 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6610 > section->size)
9c541725 6611 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6612 "(offset %s + 0) [in module %s]"),
6613 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6614 filename);
6615}
6616
6617/* Read in a CU/TU header and perform some basic error checking.
6618 The contents of the header are stored in HEADER.
6619 The result is a pointer to the start of the first DIE. */
adabb602 6620
d521ce57 6621static const gdb_byte *
ed2dc618
SM
6622read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6623 struct comp_unit_head *header,
9ff913ba 6624 struct dwarf2_section_info *section,
4bdcc0c1 6625 struct dwarf2_section_info *abbrev_section,
d521ce57 6626 const gdb_byte *info_ptr,
43988095 6627 rcuh_kind section_kind)
72bf9492 6628{
d521ce57 6629 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6630
9c541725 6631 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6632
43988095 6633 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6634
9c541725 6635 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6636
ed2dc618
SM
6637 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6638 abbrev_section);
9ff913ba
DE
6639
6640 return info_ptr;
348e048f
DE
6641}
6642
f4dc4d17
DE
6643/* Fetch the abbreviation table offset from a comp or type unit header. */
6644
6645static sect_offset
ed2dc618
SM
6646read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6647 struct dwarf2_section_info *section,
9c541725 6648 sect_offset sect_off)
f4dc4d17 6649{
a32a8923 6650 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6651 const gdb_byte *info_ptr;
ac298888 6652 unsigned int initial_length_size, offset_size;
43988095 6653 uint16_t version;
f4dc4d17
DE
6654
6655 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6656 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6657 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6658 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6659 info_ptr += initial_length_size;
6660
6661 version = read_2_bytes (abfd, info_ptr);
6662 info_ptr += 2;
6663 if (version >= 5)
6664 {
6665 /* Skip unit type and address size. */
6666 info_ptr += 2;
6667 }
6668
9c541725 6669 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6670}
6671
aaa75496
JB
6672/* Allocate a new partial symtab for file named NAME and mark this new
6673 partial symtab as being an include of PST. */
6674
6675static void
891813be 6676dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6677 struct objfile *objfile)
6678{
891813be 6679 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 6680
fbd9ab74
JK
6681 if (!IS_ABSOLUTE_PATH (subpst->filename))
6682 {
6683 /* It shares objfile->objfile_obstack. */
6684 subpst->dirname = pst->dirname;
6685 }
6686
a9342b62 6687 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6688 subpst->dependencies[0] = pst;
6689 subpst->number_of_dependencies = 1;
6690
aaa75496
JB
6691 /* No private part is necessary for include psymtabs. This property
6692 can be used to differentiate between such include psymtabs and
10b3939b 6693 the regular ones. */
891813be 6694 subpst->per_cu_data = nullptr;
aaa75496
JB
6695}
6696
6697/* Read the Line Number Program data and extract the list of files
6698 included by the source file represented by PST. Build an include
d85a05f0 6699 partial symtab for each of these included files. */
aaa75496
JB
6700
6701static void
6702dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6703 struct die_info *die,
891813be 6704 dwarf2_psymtab *pst)
aaa75496 6705{
fff8551c 6706 line_header_up lh;
d85a05f0 6707 struct attribute *attr;
aaa75496 6708
d85a05f0 6709 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6710 if (attr != nullptr)
9c541725 6711 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6712 if (lh == NULL)
6713 return; /* No linetable, so no includes. */
6714
79748972
TT
6715 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6716 that we pass in the raw text_low here; that is ok because we're
6717 only decoding the line table to make include partial symtabs, and
6718 so the addresses aren't really used. */
4ae976d1 6719 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6720 pst->raw_text_low (), 1);
aaa75496
JB
6721}
6722
348e048f 6723static hashval_t
52dc124a 6724hash_signatured_type (const void *item)
348e048f 6725{
9a3c8263
SM
6726 const struct signatured_type *sig_type
6727 = (const struct signatured_type *) item;
9a619af0 6728
348e048f 6729 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6730 return sig_type->signature;
348e048f
DE
6731}
6732
6733static int
52dc124a 6734eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6735{
9a3c8263
SM
6736 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6737 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6738
348e048f
DE
6739 return lhs->signature == rhs->signature;
6740}
6741
1fd400ff
TT
6742/* Allocate a hash table for signatured types. */
6743
6744static htab_t
673bfd45 6745allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6746{
6747 return htab_create_alloc_ex (41,
52dc124a
DE
6748 hash_signatured_type,
6749 eq_signatured_type,
1fd400ff
TT
6750 NULL,
6751 &objfile->objfile_obstack,
6752 hashtab_obstack_allocate,
6753 dummy_obstack_deallocate);
6754}
6755
d467dd73 6756/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6757
6758static int
d467dd73 6759add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6760{
9a3c8263 6761 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6762 std::vector<signatured_type *> *all_type_units
6763 = (std::vector<signatured_type *> *) datum;
1fd400ff 6764
b2bdb8cf 6765 all_type_units->push_back (sigt);
1fd400ff
TT
6766
6767 return 1;
6768}
6769
78d4d2c5 6770/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6771 and fill them into TYPES_HTAB. It will process only type units,
6772 therefore DW_UT_type. */
c88ee1f0 6773
78d4d2c5 6774static void
ed2dc618
SM
6775create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6776 struct dwo_file *dwo_file,
43988095
JK
6777 dwarf2_section_info *section, htab_t &types_htab,
6778 rcuh_kind section_kind)
348e048f 6779{
3019eac3 6780 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6781 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6782 bfd *abfd;
6783 const gdb_byte *info_ptr, *end_ptr;
348e048f 6784
4bdcc0c1
DE
6785 abbrev_section = (dwo_file != NULL
6786 ? &dwo_file->sections.abbrev
6787 : &dwarf2_per_objfile->abbrev);
6788
b4f54984 6789 if (dwarf_read_debug)
43988095
JK
6790 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6791 get_section_name (section),
a32a8923 6792 get_section_file_name (abbrev_section));
09406207 6793
78d4d2c5
JK
6794 dwarf2_read_section (objfile, section);
6795 info_ptr = section->buffer;
348e048f 6796
78d4d2c5
JK
6797 if (info_ptr == NULL)
6798 return;
348e048f 6799
78d4d2c5
JK
6800 /* We can't set abfd until now because the section may be empty or
6801 not present, in which case the bfd is unknown. */
6802 abfd = get_section_bfd_owner (section);
348e048f 6803
c0ab21c2
TT
6804 /* We don't use cutu_reader here because we don't need to read
6805 any dies: the signature is in the header. */
3019eac3 6806
78d4d2c5
JK
6807 end_ptr = info_ptr + section->size;
6808 while (info_ptr < end_ptr)
6809 {
78d4d2c5
JK
6810 struct signatured_type *sig_type;
6811 struct dwo_unit *dwo_tu;
6812 void **slot;
6813 const gdb_byte *ptr = info_ptr;
6814 struct comp_unit_head header;
6815 unsigned int length;
8b70b953 6816
9c541725 6817 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6818
a49dd8dd
JK
6819 /* Initialize it due to a false compiler warning. */
6820 header.signature = -1;
9c541725 6821 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6822
78d4d2c5
JK
6823 /* We need to read the type's signature in order to build the hash
6824 table, but we don't need anything else just yet. */
348e048f 6825
ed2dc618 6826 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6827 abbrev_section, ptr, section_kind);
348e048f 6828
78d4d2c5 6829 length = get_cu_length (&header);
6caca83c 6830
78d4d2c5
JK
6831 /* Skip dummy type units. */
6832 if (ptr >= info_ptr + length
43988095
JK
6833 || peek_abbrev_code (abfd, ptr) == 0
6834 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6835 {
6836 info_ptr += length;
6837 continue;
6838 }
dee91e82 6839
78d4d2c5
JK
6840 if (types_htab == NULL)
6841 {
6842 if (dwo_file)
6843 types_htab = allocate_dwo_unit_table (objfile);
6844 else
6845 types_htab = allocate_signatured_type_table (objfile);
6846 }
8b70b953 6847
78d4d2c5
JK
6848 if (dwo_file)
6849 {
6850 sig_type = NULL;
6851 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6852 struct dwo_unit);
6853 dwo_tu->dwo_file = dwo_file;
43988095 6854 dwo_tu->signature = header.signature;
9c541725 6855 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6856 dwo_tu->section = section;
9c541725 6857 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6858 dwo_tu->length = length;
6859 }
6860 else
6861 {
6862 /* N.B.: type_offset is not usable if this type uses a DWO file.
6863 The real type_offset is in the DWO file. */
6864 dwo_tu = NULL;
6865 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6866 struct signatured_type);
43988095 6867 sig_type->signature = header.signature;
9c541725 6868 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6869 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6870 sig_type->per_cu.is_debug_types = 1;
6871 sig_type->per_cu.section = section;
9c541725 6872 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6873 sig_type->per_cu.length = length;
6874 }
6875
6876 slot = htab_find_slot (types_htab,
6877 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6878 INSERT);
6879 gdb_assert (slot != NULL);
6880 if (*slot != NULL)
6881 {
9c541725 6882 sect_offset dup_sect_off;
0349ea22 6883
3019eac3
DE
6884 if (dwo_file)
6885 {
78d4d2c5
JK
6886 const struct dwo_unit *dup_tu
6887 = (const struct dwo_unit *) *slot;
6888
9c541725 6889 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6890 }
6891 else
6892 {
78d4d2c5
JK
6893 const struct signatured_type *dup_tu
6894 = (const struct signatured_type *) *slot;
6895
9c541725 6896 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6897 }
8b70b953 6898
b98664d3 6899 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6900 " the entry at offset %s, signature %s"),
6901 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6902 hex_string (header.signature));
78d4d2c5
JK
6903 }
6904 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6905
78d4d2c5 6906 if (dwarf_read_debug > 1)
9d8780f0
SM
6907 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6908 sect_offset_str (sect_off),
43988095 6909 hex_string (header.signature));
3019eac3 6910
78d4d2c5
JK
6911 info_ptr += length;
6912 }
6913}
3019eac3 6914
78d4d2c5
JK
6915/* Create the hash table of all entries in the .debug_types
6916 (or .debug_types.dwo) section(s).
6917 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6918 otherwise it is NULL.
b3c8eb43 6919
78d4d2c5 6920 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6921
78d4d2c5 6922 Note: This function processes DWO files only, not DWP files. */
348e048f 6923
78d4d2c5 6924static void
ed2dc618
SM
6925create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6926 struct dwo_file *dwo_file,
fd5866f6 6927 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6928 htab_t &types_htab)
6929{
fd5866f6
SM
6930 for (dwarf2_section_info &section : type_sections)
6931 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6932 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6933}
6934
6935/* Create the hash table of all entries in the .debug_types section,
6936 and initialize all_type_units.
6937 The result is zero if there is an error (e.g. missing .debug_types section),
6938 otherwise non-zero. */
6939
6940static int
ed2dc618 6941create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6942{
78d4d2c5 6943 htab_t types_htab = NULL;
3019eac3 6944
ed2dc618
SM
6945 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6946 &dwarf2_per_objfile->info, types_htab,
43988095 6947 rcuh_kind::COMPILE);
ed2dc618
SM
6948 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6949 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6950 if (types_htab == NULL)
6951 {
6952 dwarf2_per_objfile->signatured_types = NULL;
6953 return 0;
6954 }
6955
348e048f
DE
6956 dwarf2_per_objfile->signatured_types = types_htab;
6957
b2bdb8cf
SM
6958 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6959 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6960
6961 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6962 &dwarf2_per_objfile->all_type_units);
1fd400ff 6963
348e048f
DE
6964 return 1;
6965}
6966
6aa5f3a6
DE
6967/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6968 If SLOT is non-NULL, it is the entry to use in the hash table.
6969 Otherwise we find one. */
6970
6971static struct signatured_type *
ed2dc618
SM
6972add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6973 void **slot)
6aa5f3a6
DE
6974{
6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6976
b2bdb8cf
SM
6977 if (dwarf2_per_objfile->all_type_units.size ()
6978 == dwarf2_per_objfile->all_type_units.capacity ())
6979 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6980
b2bdb8cf
SM
6981 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6982 struct signatured_type);
6983
6984 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6985 sig_type->signature = sig;
6986 sig_type->per_cu.is_debug_types = 1;
6987 if (dwarf2_per_objfile->using_index)
6988 {
6989 sig_type->per_cu.v.quick =
6990 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6991 struct dwarf2_per_cu_quick_data);
6992 }
6993
6994 if (slot == NULL)
6995 {
6996 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6997 sig_type, INSERT);
6998 }
6999 gdb_assert (*slot == NULL);
7000 *slot = sig_type;
7001 /* The rest of sig_type must be filled in by the caller. */
7002 return sig_type;
7003}
7004
a2ce51a0
DE
7005/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7006 Fill in SIG_ENTRY with DWO_ENTRY. */
7007
7008static void
ed2dc618 7009fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
7010 struct signatured_type *sig_entry,
7011 struct dwo_unit *dwo_entry)
7012{
7ee85ab1 7013 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7014 gdb_assert (! sig_entry->per_cu.queued);
7015 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7016 if (dwarf2_per_objfile->using_index)
7017 {
7018 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7019 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7020 }
7021 else
7022 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7023 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7024 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7025 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7026 gdb_assert (sig_entry->dwo_unit == NULL);
7027
7028 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7029 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7030 sig_entry->per_cu.length = dwo_entry->length;
7031 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7032 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7033 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7034 sig_entry->dwo_unit = dwo_entry;
7035}
7036
7037/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7038 If we haven't read the TU yet, create the signatured_type data structure
7039 for a TU to be read in directly from a DWO file, bypassing the stub.
7040 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7041 using .gdb_index, then when reading a CU we want to stay in the DWO file
7042 containing that CU. Otherwise we could end up reading several other DWO
7043 files (due to comdat folding) to process the transitive closure of all the
7044 mentioned TUs, and that can be slow. The current DWO file will have every
7045 type signature that it needs.
a2ce51a0
DE
7046 We only do this for .gdb_index because in the psymtab case we already have
7047 to read all the DWOs to build the type unit groups. */
7048
7049static struct signatured_type *
7050lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7051{
518817b3
SM
7052 struct dwarf2_per_objfile *dwarf2_per_objfile
7053 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7054 struct objfile *objfile = dwarf2_per_objfile->objfile;
7055 struct dwo_file *dwo_file;
7056 struct dwo_unit find_dwo_entry, *dwo_entry;
7057 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7058 void **slot;
a2ce51a0
DE
7059
7060 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7061
6aa5f3a6
DE
7062 /* If TU skeletons have been removed then we may not have read in any
7063 TUs yet. */
7064 if (dwarf2_per_objfile->signatured_types == NULL)
7065 {
7066 dwarf2_per_objfile->signatured_types
7067 = allocate_signatured_type_table (objfile);
7068 }
a2ce51a0
DE
7069
7070 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7071 Use the global signatured_types array to do our own comdat-folding
7072 of types. If this is the first time we're reading this TU, and
7073 the TU has an entry in .gdb_index, replace the recorded data from
7074 .gdb_index with this TU. */
a2ce51a0 7075
a2ce51a0 7076 find_sig_entry.signature = sig;
6aa5f3a6
DE
7077 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7078 &find_sig_entry, INSERT);
9a3c8263 7079 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7080
7081 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7082 read. Don't reassign the global entry to point to this DWO if that's
7083 the case. Also note that if the TU is already being read, it may not
7084 have come from a DWO, the program may be a mix of Fission-compiled
7085 code and non-Fission-compiled code. */
7086
7087 /* Have we already tried to read this TU?
7088 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7089 needn't exist in the global table yet). */
7090 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7091 return sig_entry;
7092
6aa5f3a6
DE
7093 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7094 dwo_unit of the TU itself. */
7095 dwo_file = cu->dwo_unit->dwo_file;
7096
a2ce51a0
DE
7097 /* Ok, this is the first time we're reading this TU. */
7098 if (dwo_file->tus == NULL)
7099 return NULL;
7100 find_dwo_entry.signature = sig;
9a3c8263 7101 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7102 if (dwo_entry == NULL)
7103 return NULL;
7104
6aa5f3a6
DE
7105 /* If the global table doesn't have an entry for this TU, add one. */
7106 if (sig_entry == NULL)
ed2dc618 7107 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7108
ed2dc618 7109 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7110 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7111 return sig_entry;
7112}
7113
a2ce51a0
DE
7114/* Subroutine of lookup_signatured_type.
7115 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7116 then try the DWP file. If the TU stub (skeleton) has been removed then
7117 it won't be in .gdb_index. */
a2ce51a0
DE
7118
7119static struct signatured_type *
7120lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7121{
518817b3
SM
7122 struct dwarf2_per_objfile *dwarf2_per_objfile
7123 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7124 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7125 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7126 struct dwo_unit *dwo_entry;
7127 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7128 void **slot;
a2ce51a0
DE
7129
7130 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7131 gdb_assert (dwp_file != NULL);
7132
6aa5f3a6
DE
7133 /* If TU skeletons have been removed then we may not have read in any
7134 TUs yet. */
7135 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7136 {
6aa5f3a6
DE
7137 dwarf2_per_objfile->signatured_types
7138 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7139 }
7140
6aa5f3a6
DE
7141 find_sig_entry.signature = sig;
7142 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7143 &find_sig_entry, INSERT);
9a3c8263 7144 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7145
7146 /* Have we already tried to read this TU?
7147 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7148 needn't exist in the global table yet). */
7149 if (sig_entry != NULL)
7150 return sig_entry;
7151
a2ce51a0
DE
7152 if (dwp_file->tus == NULL)
7153 return NULL;
ed2dc618 7154 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7155 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7156 if (dwo_entry == NULL)
7157 return NULL;
7158
ed2dc618
SM
7159 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7160 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7161
a2ce51a0
DE
7162 return sig_entry;
7163}
7164
380bca97 7165/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7166 Returns NULL if signature SIG is not present in the table.
7167 It is up to the caller to complain about this. */
348e048f
DE
7168
7169static struct signatured_type *
a2ce51a0 7170lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7171{
518817b3
SM
7172 struct dwarf2_per_objfile *dwarf2_per_objfile
7173 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7174
a2ce51a0
DE
7175 if (cu->dwo_unit
7176 && dwarf2_per_objfile->using_index)
7177 {
7178 /* We're in a DWO/DWP file, and we're using .gdb_index.
7179 These cases require special processing. */
ed2dc618 7180 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7181 return lookup_dwo_signatured_type (cu, sig);
7182 else
7183 return lookup_dwp_signatured_type (cu, sig);
7184 }
7185 else
7186 {
7187 struct signatured_type find_entry, *entry;
348e048f 7188
a2ce51a0
DE
7189 if (dwarf2_per_objfile->signatured_types == NULL)
7190 return NULL;
7191 find_entry.signature = sig;
9a3c8263
SM
7192 entry = ((struct signatured_type *)
7193 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7194 return entry;
7195 }
348e048f 7196}
18a8505e
AT
7197
7198/* Return the address base of the compile unit, which, if exists, is stored
7199 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7200static gdb::optional<ULONGEST>
7201lookup_addr_base (struct die_info *comp_unit_die)
7202{
7203 struct attribute *attr;
7204 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7205 if (attr == nullptr)
7206 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7207 if (attr == nullptr)
7208 return gdb::optional<ULONGEST> ();
7209 return DW_UNSND (attr);
7210}
7211
7212/* Return range lists base of the compile unit, which, if exists, is stored
7213 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7214static ULONGEST
7215lookup_ranges_base (struct die_info *comp_unit_die)
7216{
7217 struct attribute *attr;
7218 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7219 if (attr == nullptr)
7220 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7221 if (attr == nullptr)
7222 return 0;
7223 return DW_UNSND (attr);
7224}
7225
42e7ad6c 7226/* Low level DIE reading support. */
348e048f 7227
d85a05f0
DJ
7228/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7229
7230static void
7231init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7232 struct dwarf2_cu *cu,
3019eac3 7233 struct dwarf2_section_info *section,
685af9cd
TT
7234 struct dwo_file *dwo_file,
7235 struct abbrev_table *abbrev_table)
d85a05f0 7236{
fceca515 7237 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7238 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7239 reader->cu = cu;
3019eac3 7240 reader->dwo_file = dwo_file;
dee91e82
DE
7241 reader->die_section = section;
7242 reader->buffer = section->buffer;
f664829e 7243 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7244 reader->comp_dir = NULL;
685af9cd 7245 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7246}
7247
c0ab21c2 7248/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7249 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 7250 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
7251 already.
7252
7253 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7254 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7255 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7256 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7257 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7258 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7259 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7260 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7261 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7262 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7263 kept around for at least as long as *RESULT_READER.
7264
b0c7bfa9
DE
7265 The result is non-zero if a valid (non-dummy) DIE was found. */
7266
7267static int
7268read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7269 struct dwo_unit *dwo_unit,
b0c7bfa9 7270 struct die_info *stub_comp_unit_die,
a2ce51a0 7271 const char *stub_comp_dir,
b0c7bfa9 7272 struct die_reader_specs *result_reader,
d521ce57 7273 const gdb_byte **result_info_ptr,
b0c7bfa9 7274 struct die_info **result_comp_unit_die,
685af9cd
TT
7275 int *result_has_children,
7276 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7277{
ed2dc618 7278 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7279 struct objfile *objfile = dwarf2_per_objfile->objfile;
7280 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7281 bfd *abfd;
d521ce57 7282 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7283 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7284 int i,num_extra_attrs;
7285 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
7286 struct die_info *comp_unit_die;
7287
b0aeadb3
DE
7288 /* At most one of these may be provided. */
7289 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7290
b0c7bfa9
DE
7291 /* These attributes aren't processed until later:
7292 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7293 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7294 referenced later. However, these attributes are found in the stub
7295 which we won't have later. In order to not impose this complication
7296 on the rest of the code, we read them here and copy them to the
7297 DWO CU/TU die. */
b0c7bfa9
DE
7298
7299 stmt_list = NULL;
7300 low_pc = NULL;
7301 high_pc = NULL;
7302 ranges = NULL;
7303 comp_dir = NULL;
7304
7305 if (stub_comp_unit_die != NULL)
7306 {
7307 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7308 DWO file. */
7309 if (! this_cu->is_debug_types)
7310 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7311 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7312 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7313 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7314 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7315
18a8505e 7316 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 7317
18a8505e
AT
7318 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7319 here (if needed). We need the value before we can process
7320 DW_AT_ranges. */
7321 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 7322 }
a2ce51a0
DE
7323 else if (stub_comp_dir != NULL)
7324 {
7325 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7326 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7327 comp_dir->name = DW_AT_comp_dir;
7328 comp_dir->form = DW_FORM_string;
7329 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7330 DW_STRING (comp_dir) = stub_comp_dir;
7331 }
b0c7bfa9
DE
7332
7333 /* Set up for reading the DWO CU/TU. */
7334 cu->dwo_unit = dwo_unit;
685af9cd 7335 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7336 dwarf2_read_section (objfile, section);
a32a8923 7337 abfd = get_section_bfd_owner (section);
9c541725
PA
7338 begin_info_ptr = info_ptr = (section->buffer
7339 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7340 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7341
7342 if (this_cu->is_debug_types)
7343 {
b0c7bfa9
DE
7344 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7345
ed2dc618
SM
7346 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7347 &cu->header, section,
b0c7bfa9 7348 dwo_abbrev_section,
43988095 7349 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7350 /* This is not an assert because it can be caused by bad debug info. */
43988095 7351 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7352 {
7353 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7354 " TU at offset %s [in module %s]"),
a2ce51a0 7355 hex_string (sig_type->signature),
43988095 7356 hex_string (cu->header.signature),
9d8780f0 7357 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7358 bfd_get_filename (abfd));
7359 }
9c541725 7360 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7361 /* For DWOs coming from DWP files, we don't know the CU length
7362 nor the type's offset in the TU until now. */
7363 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7364 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7365
7366 /* Establish the type offset that can be used to lookup the type.
7367 For DWO files, we don't know it until now. */
9c541725
PA
7368 sig_type->type_offset_in_section
7369 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7370 }
7371 else
7372 {
ed2dc618
SM
7373 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7374 &cu->header, section,
b0c7bfa9 7375 dwo_abbrev_section,
43988095 7376 info_ptr, rcuh_kind::COMPILE);
9c541725 7377 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7378 /* For DWOs coming from DWP files, we don't know the CU length
7379 until now. */
7380 dwo_unit->length = get_cu_length (&cu->header);
7381 }
7382
685af9cd
TT
7383 *result_dwo_abbrev_table
7384 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7385 cu->header.abbrev_sect_off);
7386 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7387 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7388
7389 /* Read in the die, but leave space to copy over the attributes
7390 from the stub. This has the benefit of simplifying the rest of
7391 the code - all the work to maintain the illusion of a single
7392 DW_TAG_{compile,type}_unit DIE is done here. */
7393 num_extra_attrs = ((stmt_list != NULL)
7394 + (low_pc != NULL)
7395 + (high_pc != NULL)
7396 + (ranges != NULL)
7397 + (comp_dir != NULL));
7398 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7399 result_has_children, num_extra_attrs);
7400
7401 /* Copy over the attributes from the stub to the DIE we just read in. */
7402 comp_unit_die = *result_comp_unit_die;
7403 i = comp_unit_die->num_attrs;
7404 if (stmt_list != NULL)
7405 comp_unit_die->attrs[i++] = *stmt_list;
7406 if (low_pc != NULL)
7407 comp_unit_die->attrs[i++] = *low_pc;
7408 if (high_pc != NULL)
7409 comp_unit_die->attrs[i++] = *high_pc;
7410 if (ranges != NULL)
7411 comp_unit_die->attrs[i++] = *ranges;
7412 if (comp_dir != NULL)
7413 comp_unit_die->attrs[i++] = *comp_dir;
7414 comp_unit_die->num_attrs += num_extra_attrs;
7415
b4f54984 7416 if (dwarf_die_debug)
bf6af496
DE
7417 {
7418 fprintf_unfiltered (gdb_stdlog,
7419 "Read die from %s@0x%x of %s:\n",
a32a8923 7420 get_section_name (section),
bf6af496
DE
7421 (unsigned) (begin_info_ptr - section->buffer),
7422 bfd_get_filename (abfd));
b4f54984 7423 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7424 }
7425
a2ce51a0
DE
7426 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7427 TUs by skipping the stub and going directly to the entry in the DWO file.
7428 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7429 to get it via circuitous means. Blech. */
7430 if (comp_dir != NULL)
7431 result_reader->comp_dir = DW_STRING (comp_dir);
7432
b0c7bfa9
DE
7433 /* Skip dummy compilation units. */
7434 if (info_ptr >= begin_info_ptr + dwo_unit->length
7435 || peek_abbrev_code (abfd, info_ptr) == 0)
7436 return 0;
7437
7438 *result_info_ptr = info_ptr;
7439 return 1;
7440}
7441
a084a2a6
AT
7442/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7443 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7444 signature is part of the header. */
7445static gdb::optional<ULONGEST>
7446lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7447{
7448 if (cu->header.version >= 5)
7449 return cu->header.signature;
7450 struct attribute *attr;
7451 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7452 if (attr == nullptr)
7453 return gdb::optional<ULONGEST> ();
7454 return DW_UNSND (attr);
7455}
7456
c0ab21c2 7457/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7458 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7459 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7460
7461static struct dwo_unit *
7462lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
7463 struct die_info *comp_unit_die,
7464 const char *dwo_name)
b0c7bfa9
DE
7465{
7466 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7467 struct dwo_unit *dwo_unit;
c0ab21c2 7468 const char *comp_dir;
b0c7bfa9 7469
a2ce51a0
DE
7470 gdb_assert (cu != NULL);
7471
b0c7bfa9 7472 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7473 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7474 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7475
7476 if (this_cu->is_debug_types)
7477 {
7478 struct signatured_type *sig_type;
7479
7480 /* Since this_cu is the first member of struct signatured_type,
7481 we can go from a pointer to one to a pointer to the other. */
7482 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7483 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7484 }
7485 else
7486 {
a084a2a6
AT
7487 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7488 if (!signature.has_value ())
b0c7bfa9
DE
7489 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7490 " [in module %s]"),
e3b94546 7491 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7492 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7493 *signature);
b0c7bfa9
DE
7494 }
7495
b0c7bfa9
DE
7496 return dwo_unit;
7497}
7498
c0ab21c2 7499/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7500 See it for a description of the parameters.
fcd3b13d 7501 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7502
c0ab21c2
TT
7503void
7504cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7505 int use_existing_cu, int keep)
a2ce51a0 7506{
a2ce51a0 7507 struct signatured_type *sig_type;
a2ce51a0 7508 struct die_reader_specs reader;
a2ce51a0
DE
7509
7510 /* Verify we can do the following downcast, and that we have the
7511 data we need. */
7512 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7513 sig_type = (struct signatured_type *) this_cu;
7514 gdb_assert (sig_type->dwo_unit != NULL);
7515
6aa5f3a6
DE
7516 if (use_existing_cu && this_cu->cu != NULL)
7517 {
7518 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7519 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7520 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7521 }
7522 else
7523 {
7524 /* If !use_existing_cu, this_cu->cu must be NULL. */
7525 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7526 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7527 }
7528
7529 /* A future optimization, if needed, would be to use an existing
7530 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7531 could share abbrev tables. */
a2ce51a0
DE
7532
7533 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7534 NULL /* stub_comp_unit_die */,
7535 sig_type->dwo_unit->dwo_file->comp_dir,
7536 &reader, &info_ptr,
685af9cd 7537 &comp_unit_die, &has_children,
c0ab21c2 7538 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7539 {
7540 /* Dummy die. */
c0ab21c2 7541 dummy_p = true;
a2ce51a0 7542 }
a2ce51a0
DE
7543}
7544
fd820528 7545/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7546 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7547
f4dc4d17
DE
7548 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7549 Otherwise the table specified in the comp unit header is read in and used.
7550 This is an optimization for when we already have the abbrev table.
7551
dee91e82
DE
7552 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7553 Otherwise, a new CU is allocated with xmalloc.
7554
7555 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7556 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7557 end. */
aaa75496 7558
c0ab21c2
TT
7559cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7560 struct abbrev_table *abbrev_table,
7561 int use_existing_cu, int keep,
7562 bool skip_partial)
7563 : die_reader_specs {},
7564 m_this_cu (this_cu),
7565 m_keep (keep)
c906108c 7566{
ed2dc618 7567 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7568 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7569 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7570 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7571 struct dwarf2_cu *cu;
c0ab21c2 7572 const gdb_byte *begin_info_ptr;
dee91e82 7573 struct signatured_type *sig_type = NULL;
4bdcc0c1 7574 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7575 /* Non-zero if CU currently points to a DWO file and we need to
7576 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7577 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7578 int rereading_dwo_cu = 0;
c906108c 7579
b4f54984 7580 if (dwarf_die_debug)
9d8780f0 7581 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7582 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7583 sect_offset_str (this_cu->sect_off));
09406207 7584
dee91e82
DE
7585 if (use_existing_cu)
7586 gdb_assert (keep);
23745b47 7587
a2ce51a0
DE
7588 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7589 file (instead of going through the stub), short-circuit all of this. */
7590 if (this_cu->reading_dwo_directly)
7591 {
7592 /* Narrow down the scope of possibilities to have to understand. */
7593 gdb_assert (this_cu->is_debug_types);
7594 gdb_assert (abbrev_table == NULL);
c0ab21c2 7595 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7596 return;
7597 }
7598
dee91e82
DE
7599 /* This is cheap if the section is already read in. */
7600 dwarf2_read_section (objfile, section);
7601
9c541725 7602 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7603
7604 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7605
7606 if (use_existing_cu && this_cu->cu != NULL)
7607 {
7608 cu = this_cu->cu;
42e7ad6c
DE
7609 /* If this CU is from a DWO file we need to start over, we need to
7610 refetch the attributes from the skeleton CU.
7611 This could be optimized by retrieving those attributes from when we
7612 were here the first time: the previous comp_unit_die was stored in
7613 comp_unit_obstack. But there's no data yet that we need this
7614 optimization. */
7615 if (cu->dwo_unit != NULL)
7616 rereading_dwo_cu = 1;
dee91e82
DE
7617 }
7618 else
7619 {
7620 /* If !use_existing_cu, this_cu->cu must be NULL. */
7621 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7622 m_new_cu.reset (new dwarf2_cu (this_cu));
7623 cu = m_new_cu.get ();
42e7ad6c 7624 }
dee91e82 7625
b0c7bfa9 7626 /* Get the header. */
9c541725 7627 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7628 {
7629 /* We already have the header, there's no need to read it in again. */
9c541725 7630 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7631 }
7632 else
7633 {
3019eac3 7634 if (this_cu->is_debug_types)
dee91e82 7635 {
ed2dc618
SM
7636 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7637 &cu->header, section,
4bdcc0c1 7638 abbrev_section, info_ptr,
43988095 7639 rcuh_kind::TYPE);
dee91e82 7640
42e7ad6c
DE
7641 /* Since per_cu is the first member of struct signatured_type,
7642 we can go from a pointer to one to a pointer to the other. */
7643 sig_type = (struct signatured_type *) this_cu;
43988095 7644 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7645 gdb_assert (sig_type->type_offset_in_tu
7646 == cu->header.type_cu_offset_in_tu);
7647 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7648
42e7ad6c
DE
7649 /* LENGTH has not been set yet for type units if we're
7650 using .gdb_index. */
1ce1cefd 7651 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7652
7653 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7654 sig_type->type_offset_in_section =
7655 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7656
7657 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7658 }
7659 else
7660 {
ed2dc618
SM
7661 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7662 &cu->header, section,
4bdcc0c1 7663 abbrev_section,
43988095
JK
7664 info_ptr,
7665 rcuh_kind::COMPILE);
dee91e82 7666
9c541725 7667 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7668 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7669 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7670 }
7671 }
10b3939b 7672
6caca83c 7673 /* Skip dummy compilation units. */
dee91e82 7674 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7675 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7676 {
7677 dummy_p = true;
7678 return;
7679 }
6caca83c 7680
433df2d4
DE
7681 /* If we don't have them yet, read the abbrevs for this compilation unit.
7682 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7683 done. */
f4dc4d17 7684 if (abbrev_table != NULL)
685af9cd
TT
7685 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7686 else
f4dc4d17 7687 {
c0ab21c2 7688 m_abbrev_table_holder
685af9cd
TT
7689 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7690 cu->header.abbrev_sect_off);
c0ab21c2 7691 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7692 }
af703f96 7693
dee91e82 7694 /* Read the top level CU/TU die. */
c0ab21c2
TT
7695 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7696 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
93311388 7697
58f0c718 7698 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7699 {
7700 dummy_p = true;
7701 return;
7702 }
58f0c718 7703
b0c7bfa9 7704 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7705 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7706 table from the DWO file and pass the ownership over to us. It will be
7707 referenced from READER, so we must make sure to free it after we're done
7708 with READER.
7709
b0c7bfa9
DE
7710 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7711 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7712 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7713 if (dwo_name != nullptr)
3019eac3 7714 {
3019eac3 7715 struct dwo_unit *dwo_unit;
b0c7bfa9 7716 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7717
7718 if (has_children)
6a506a2d 7719 {
b98664d3 7720 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7721 " has children (offset %s) [in module %s]"),
7722 sect_offset_str (this_cu->sect_off),
7723 bfd_get_filename (abfd));
6a506a2d 7724 }
c0ab21c2 7725 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7726 if (dwo_unit != NULL)
3019eac3 7727 {
6a506a2d 7728 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7729 comp_unit_die, NULL,
c0ab21c2 7730 this, &info_ptr,
685af9cd 7731 &dwo_comp_unit_die, &has_children,
c0ab21c2 7732 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7733 {
7734 /* Dummy die. */
c0ab21c2 7735 dummy_p = true;
6a506a2d
DE
7736 return;
7737 }
7738 comp_unit_die = dwo_comp_unit_die;
7739 }
7740 else
7741 {
7742 /* Yikes, we couldn't find the rest of the DIE, we only have
7743 the stub. A complaint has already been logged. There's
7744 not much more we can do except pass on the stub DIE to
7745 die_reader_func. We don't want to throw an error on bad
7746 debug info. */
3019eac3
DE
7747 }
7748 }
c0ab21c2 7749}
3019eac3 7750
c0ab21c2
TT
7751cutu_reader::~cutu_reader ()
7752{
b0c7bfa9 7753 /* Done, clean up. */
c0ab21c2 7754 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7755 {
c0ab21c2
TT
7756 struct dwarf2_per_objfile *dwarf2_per_objfile
7757 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7758 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7759 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7760 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7761 /* The chain owns it now. */
c0ab21c2 7762 m_new_cu.release ();
348e048f 7763 }
dee91e82
DE
7764}
7765
18a8505e
AT
7766/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7767 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7768 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7769
7770 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7771 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7772
7773 We fill in THIS_CU->length.
7774
dee91e82 7775 THIS_CU->cu is always freed when done.
3019eac3 7776 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7777 to care whether it refers to the "main" CU or the DWO CU.
7778
7779 When parent_cu is passed, it is used to provide a default value for
7780 str_offsets_base and addr_base from the parent. */
dee91e82 7781
c0ab21c2
TT
7782cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7783 struct dwarf2_cu *parent_cu,
7784 struct dwo_file *dwo_file)
7785 : die_reader_specs {},
7786 m_this_cu (this_cu)
dee91e82 7787{
ed2dc618 7788 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7789 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7790 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7791 bfd *abfd = get_section_bfd_owner (section);
33e80786 7792 struct dwarf2_section_info *abbrev_section;
d521ce57 7793 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82
DE
7794 int has_children;
7795
b4f54984 7796 if (dwarf_die_debug)
9d8780f0 7797 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7798 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7799 sect_offset_str (this_cu->sect_off));
09406207 7800
dee91e82
DE
7801 gdb_assert (this_cu->cu == NULL);
7802
33e80786
DE
7803 abbrev_section = (dwo_file != NULL
7804 ? &dwo_file->sections.abbrev
7805 : get_abbrev_section_for_cu (this_cu));
7806
dee91e82
DE
7807 /* This is cheap if the section is already read in. */
7808 dwarf2_read_section (objfile, section);
7809
c0ab21c2 7810 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7811
9c541725 7812 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7813 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7814 &m_new_cu->header, section,
4bdcc0c1 7815 abbrev_section, info_ptr,
43988095
JK
7816 (this_cu->is_debug_types
7817 ? rcuh_kind::TYPE
7818 : rcuh_kind::COMPILE));
dee91e82 7819
18a8505e
AT
7820 if (parent_cu != nullptr)
7821 {
c0ab21c2
TT
7822 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7823 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7824 }
c0ab21c2 7825 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7826
7827 /* Skip dummy compilation units. */
7828 if (info_ptr >= begin_info_ptr + this_cu->length
7829 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7830 {
7831 dummy_p = true;
7832 return;
7833 }
72bf9492 7834
c0ab21c2 7835 m_abbrev_table_holder
685af9cd 7836 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
c0ab21c2 7837 m_new_cu->header.abbrev_sect_off);
dee91e82 7838
c0ab21c2
TT
7839 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7840 m_abbrev_table_holder.get ());
7841 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
dee91e82
DE
7842}
7843
0018ea6f
DE
7844\f
7845/* Type Unit Groups.
dee91e82 7846
0018ea6f
DE
7847 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7848 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7849 so that all types coming from the same compilation (.o file) are grouped
7850 together. A future step could be to put the types in the same symtab as
7851 the CU the types ultimately came from. */
ff013f42 7852
f4dc4d17
DE
7853static hashval_t
7854hash_type_unit_group (const void *item)
7855{
9a3c8263
SM
7856 const struct type_unit_group *tu_group
7857 = (const struct type_unit_group *) item;
f4dc4d17 7858
094b34ac 7859 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7860}
348e048f
DE
7861
7862static int
f4dc4d17 7863eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7864{
9a3c8263
SM
7865 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7866 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7867
094b34ac 7868 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7869}
348e048f 7870
f4dc4d17
DE
7871/* Allocate a hash table for type unit groups. */
7872
7873static htab_t
ed2dc618 7874allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7875{
7876 return htab_create_alloc_ex (3,
7877 hash_type_unit_group,
7878 eq_type_unit_group,
7879 NULL,
ed2dc618 7880 &objfile->objfile_obstack,
f4dc4d17
DE
7881 hashtab_obstack_allocate,
7882 dummy_obstack_deallocate);
7883}
dee91e82 7884
f4dc4d17
DE
7885/* Type units that don't have DW_AT_stmt_list are grouped into their own
7886 partial symtabs. We combine several TUs per psymtab to not let the size
7887 of any one psymtab grow too big. */
7888#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7889#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7890
094b34ac 7891/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7892 Create the type_unit_group object used to hold one or more TUs. */
7893
7894static struct type_unit_group *
094b34ac 7895create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7896{
518817b3
SM
7897 struct dwarf2_per_objfile *dwarf2_per_objfile
7898 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7899 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7900 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7901 struct type_unit_group *tu_group;
f4dc4d17
DE
7902
7903 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7904 struct type_unit_group);
094b34ac 7905 per_cu = &tu_group->per_cu;
518817b3 7906 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7907
094b34ac
DE
7908 if (dwarf2_per_objfile->using_index)
7909 {
7910 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7911 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7912 }
7913 else
7914 {
9c541725 7915 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7916 dwarf2_psymtab *pst;
528e1572 7917 std::string name;
094b34ac
DE
7918
7919 /* Give the symtab a useful name for debug purposes. */
7920 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7921 name = string_printf ("<type_units_%d>",
7922 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7923 else
528e1572 7924 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7925
528e1572 7926 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7927 pst->anonymous = true;
094b34ac 7928 }
f4dc4d17 7929
094b34ac 7930 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7931 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7932
7933 return tu_group;
7934}
7935
094b34ac
DE
7936/* Look up the type_unit_group for type unit CU, and create it if necessary.
7937 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7938
7939static struct type_unit_group *
ff39bb5e 7940get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7941{
518817b3
SM
7942 struct dwarf2_per_objfile *dwarf2_per_objfile
7943 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7944 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7945 struct type_unit_group *tu_group;
7946 void **slot;
7947 unsigned int line_offset;
7948 struct type_unit_group type_unit_group_for_lookup;
7949
7950 if (dwarf2_per_objfile->type_unit_groups == NULL)
7951 {
7952 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7953 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7954 }
7955
7956 /* Do we need to create a new group, or can we use an existing one? */
7957
7958 if (stmt_list)
7959 {
7960 line_offset = DW_UNSND (stmt_list);
7961 ++tu_stats->nr_symtab_sharers;
7962 }
7963 else
7964 {
7965 /* Ugh, no stmt_list. Rare, but we have to handle it.
7966 We can do various things here like create one group per TU or
7967 spread them over multiple groups to split up the expansion work.
7968 To avoid worst case scenarios (too many groups or too large groups)
7969 we, umm, group them in bunches. */
7970 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7971 | (tu_stats->nr_stmt_less_type_units
7972 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7973 ++tu_stats->nr_stmt_less_type_units;
7974 }
7975
094b34ac 7976 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7977 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7978 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7979 &type_unit_group_for_lookup, INSERT);
7980 if (*slot != NULL)
7981 {
9a3c8263 7982 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7983 gdb_assert (tu_group != NULL);
7984 }
7985 else
7986 {
9c541725 7987 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7988 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7989 *slot = tu_group;
7990 ++tu_stats->nr_symtabs;
7991 }
7992
7993 return tu_group;
7994}
0018ea6f
DE
7995\f
7996/* Partial symbol tables. */
7997
7998/* Create a psymtab named NAME and assign it to PER_CU.
7999
8000 The caller must fill in the following details:
8001 dirname, textlow, texthigh. */
8002
891813be 8003static dwarf2_psymtab *
0018ea6f
DE
8004create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8005{
e3b94546 8006 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 8007 dwarf2_psymtab *pst;
0018ea6f 8008
891813be 8009 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 8010
6d94535f 8011 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
8012
8013 /* This is the glue that links PST into GDB's symbol API. */
891813be 8014 pst->per_cu_data = per_cu;
0018ea6f
DE
8015 per_cu->v.psymtab = pst;
8016
8017 return pst;
8018}
8019
c0ab21c2 8020/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
8021
8022static void
8023process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8024 const gdb_byte *info_ptr,
0018ea6f
DE
8025 struct die_info *comp_unit_die,
8026 int has_children,
c0ab21c2
TT
8027 int want_partial_unit,
8028 enum language pretend_language)
0018ea6f
DE
8029{
8030 struct dwarf2_cu *cu = reader->cu;
518817b3 8031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8032 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8033 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8034 CORE_ADDR baseaddr;
8035 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 8036 dwarf2_psymtab *pst;
3a2b436a 8037 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8038 const char *filename;
0018ea6f 8039
c0ab21c2 8040 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
8041 return;
8042
8043 gdb_assert (! per_cu->is_debug_types);
8044
c0ab21c2 8045 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 8046
0018ea6f 8047 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8048 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8049 if (filename == NULL)
0018ea6f 8050 filename = "";
0018ea6f
DE
8051
8052 pst = create_partial_symtab (per_cu, filename);
8053
8054 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8055 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 8056
b3b3bada 8057 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
8058
8059 dwarf2_find_base_address (comp_unit_die, cu);
8060
8061 /* Possibly set the default values of LOWPC and HIGHPC from
8062 `DW_AT_ranges'. */
3a2b436a
JK
8063 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8064 &best_highpc, cu, pst);
8065 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8066 {
8067 CORE_ADDR low
8068 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8069 - baseaddr);
8070 CORE_ADDR high
8071 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8072 - baseaddr - 1);
8073 /* Store the contiguous range if it is not empty; it can be
8074 empty for CUs with no code. */
d320c2b5
TT
8075 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8076 low, high, pst);
79748972 8077 }
0018ea6f
DE
8078
8079 /* Check if comp unit has_children.
8080 If so, read the rest of the partial symbols from this comp unit.
8081 If not, there's no more debug_info for this comp unit. */
8082 if (has_children)
8083 {
8084 struct partial_die_info *first_die;
8085 CORE_ADDR lowpc, highpc;
8086
8087 lowpc = ((CORE_ADDR) -1);
8088 highpc = ((CORE_ADDR) 0);
8089
8090 first_die = load_partial_dies (reader, info_ptr, 1);
8091
8092 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8093 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8094
8095 /* If we didn't find a lowpc, set it to highpc to avoid
8096 complaints from `maint check'. */
8097 if (lowpc == ((CORE_ADDR) -1))
8098 lowpc = highpc;
8099
8100 /* If the compilation unit didn't have an explicit address range,
8101 then use the information extracted from its child dies. */
e385593e 8102 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8103 {
8104 best_lowpc = lowpc;
8105 best_highpc = highpc;
8106 }
8107 }
4ae976d1 8108 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8109 best_lowpc + baseaddr)
8110 - baseaddr);
4ae976d1 8111 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8112 best_highpc + baseaddr)
8113 - baseaddr);
0018ea6f 8114
8763cede 8115 end_psymtab_common (objfile, pst);
0018ea6f 8116
ae640021 8117 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8118 {
8119 int i;
ae640021 8120 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8121
8122 /* Fill in 'dependencies' here; we fill in 'users' in a
8123 post-pass. */
8124 pst->number_of_dependencies = len;
a9342b62
TT
8125 pst->dependencies
8126 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8127 for (i = 0; i < len; ++i)
8128 {
8129 pst->dependencies[i]
8130 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8131 }
0018ea6f 8132
ae640021 8133 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8134 }
8135
8136 /* Get the list of files included in the current compilation unit,
8137 and build a psymtab for each of them. */
8138 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8139
b4f54984 8140 if (dwarf_read_debug)
b926417a
TT
8141 fprintf_unfiltered (gdb_stdlog,
8142 "Psymtab for %s unit @%s: %s - %s"
8143 ", %d global, %d static syms\n",
8144 per_cu->is_debug_types ? "type" : "comp",
8145 sect_offset_str (per_cu->sect_off),
8146 paddress (gdbarch, pst->text_low (objfile)),
8147 paddress (gdbarch, pst->text_high (objfile)),
8148 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8149}
8150
8151/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8152 Process compilation unit THIS_CU for a psymtab. */
8153
8154static void
8155process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8156 int want_partial_unit,
8157 enum language pretend_language)
0018ea6f
DE
8158{
8159 /* If this compilation unit was already read in, free the
8160 cached copy in order to read it in again. This is
8161 necessary because we skipped some symbols when we first
8162 read in the compilation unit (see load_partial_dies).
8163 This problem could be avoided, but the benefit is unclear. */
8164 if (this_cu->cu != NULL)
8165 free_one_cached_comp_unit (this_cu);
8166
c0ab21c2
TT
8167 cutu_reader reader (this_cu, NULL, 0, 0, false);
8168
8169 if (reader.dummy_p)
f1902523 8170 {
c0ab21c2 8171 /* Nothing. */
f1902523 8172 }
c0ab21c2
TT
8173 else if (this_cu->is_debug_types)
8174 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8175 reader.has_children);
8176 else
8177 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8178 reader.comp_unit_die,
8179 reader.has_children,
8180 want_partial_unit,
8181 pretend_language);
0018ea6f
DE
8182
8183 /* Age out any secondary CUs. */
ed2dc618 8184 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8185}
f4dc4d17
DE
8186
8187/* Reader function for build_type_psymtabs. */
8188
8189static void
8190build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8191 const gdb_byte *info_ptr,
f4dc4d17 8192 struct die_info *type_unit_die,
c0ab21c2 8193 int has_children)
f4dc4d17 8194{
ed2dc618 8195 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8196 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8197 struct objfile *objfile = dwarf2_per_objfile->objfile;
8198 struct dwarf2_cu *cu = reader->cu;
8199 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8200 struct signatured_type *sig_type;
f4dc4d17
DE
8201 struct type_unit_group *tu_group;
8202 struct attribute *attr;
8203 struct partial_die_info *first_die;
8204 CORE_ADDR lowpc, highpc;
891813be 8205 dwarf2_psymtab *pst;
f4dc4d17 8206
0186c6a7
DE
8207 gdb_assert (per_cu->is_debug_types);
8208 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8209
8210 if (! has_children)
8211 return;
8212
8213 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8214 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8215
df07e2c7 8216 if (tu_group->tus == nullptr)
a8b3b8e9 8217 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8218 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8219
8220 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 8221 pst = create_partial_symtab (per_cu, "");
6d94535f 8222 pst->anonymous = true;
f4dc4d17
DE
8223
8224 first_die = load_partial_dies (reader, info_ptr, 1);
8225
8226 lowpc = (CORE_ADDR) -1;
8227 highpc = (CORE_ADDR) 0;
8228 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8229
8763cede 8230 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8231}
8232
73051182
DE
8233/* Struct used to sort TUs by their abbreviation table offset. */
8234
8235struct tu_abbrev_offset
8236{
b2bdb8cf
SM
8237 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8238 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8239 {}
8240
8241 signatured_type *sig_type;
73051182
DE
8242 sect_offset abbrev_offset;
8243};
8244
484cf504 8245/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8246
484cf504
TT
8247static bool
8248sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8249 const struct tu_abbrev_offset &b)
73051182 8250{
484cf504 8251 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8252}
8253
8254/* Efficiently read all the type units.
8255 This does the bulk of the work for build_type_psymtabs.
8256
8257 The efficiency is because we sort TUs by the abbrev table they use and
8258 only read each abbrev table once. In one program there are 200K TUs
8259 sharing 8K abbrev tables.
8260
8261 The main purpose of this function is to support building the
8262 dwarf2_per_objfile->type_unit_groups table.
8263 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8264 can collapse the search space by grouping them by stmt_list.
8265 The savings can be significant, in the same program from above the 200K TUs
8266 share 8K stmt_list tables.
8267
8268 FUNC is expected to call get_type_unit_group, which will create the
8269 struct type_unit_group if necessary and add it to
8270 dwarf2_per_objfile->type_unit_groups. */
8271
8272static void
ed2dc618 8273build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8274{
73051182 8275 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8276 abbrev_table_up abbrev_table;
73051182 8277 sect_offset abbrev_offset;
73051182
DE
8278
8279 /* It's up to the caller to not call us multiple times. */
8280 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8281
b2bdb8cf 8282 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8283 return;
8284
8285 /* TUs typically share abbrev tables, and there can be way more TUs than
8286 abbrev tables. Sort by abbrev table to reduce the number of times we
8287 read each abbrev table in.
8288 Alternatives are to punt or to maintain a cache of abbrev tables.
8289 This is simpler and efficient enough for now.
8290
8291 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8292 symtab to use). Typically TUs with the same abbrev offset have the same
8293 stmt_list value too so in practice this should work well.
8294
8295 The basic algorithm here is:
8296
8297 sort TUs by abbrev table
8298 for each TU with same abbrev table:
8299 read abbrev table if first user
8300 read TU top level DIE
8301 [IWBN if DWO skeletons had DW_AT_stmt_list]
8302 call FUNC */
8303
b4f54984 8304 if (dwarf_read_debug)
73051182
DE
8305 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8306
8307 /* Sort in a separate table to maintain the order of all_type_units
8308 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8309 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8310 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8311
8312 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8313 sorted_by_abbrev.emplace_back
8314 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8315 sig_type->per_cu.section,
8316 sig_type->per_cu.sect_off));
73051182 8317
484cf504
TT
8318 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8319 sort_tu_by_abbrev_offset);
73051182 8320
9c541725 8321 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8322
b2bdb8cf 8323 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8324 {
73051182
DE
8325 /* Switch to the next abbrev table if necessary. */
8326 if (abbrev_table == NULL
b2bdb8cf 8327 || tu.abbrev_offset != abbrev_offset)
73051182 8328 {
b2bdb8cf 8329 abbrev_offset = tu.abbrev_offset;
73051182 8330 abbrev_table =
ed2dc618
SM
8331 abbrev_table_read_table (dwarf2_per_objfile,
8332 &dwarf2_per_objfile->abbrev,
73051182
DE
8333 abbrev_offset);
8334 ++tu_stats->nr_uniq_abbrev_tables;
8335 }
8336
c0ab21c2
TT
8337 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8338 0, 0, false);
8339 if (!reader.dummy_p)
8340 build_type_psymtabs_reader (&reader, reader.info_ptr,
8341 reader.comp_unit_die,
8342 reader.has_children);
73051182 8343 }
6aa5f3a6 8344}
73051182 8345
6aa5f3a6
DE
8346/* Print collected type unit statistics. */
8347
8348static void
ed2dc618 8349print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8350{
8351 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8352
8353 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8354 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8355 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8356 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8357 tu_stats->nr_uniq_abbrev_tables);
8358 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8359 tu_stats->nr_symtabs);
8360 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8361 tu_stats->nr_symtab_sharers);
8362 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8363 tu_stats->nr_stmt_less_type_units);
8364 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8365 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8366}
8367
f4dc4d17
DE
8368/* Traversal function for build_type_psymtabs. */
8369
8370static int
8371build_type_psymtab_dependencies (void **slot, void *info)
8372{
ed2dc618
SM
8373 struct dwarf2_per_objfile *dwarf2_per_objfile
8374 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8375 struct objfile *objfile = dwarf2_per_objfile->objfile;
8376 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8377 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8378 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8379 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8380 int i;
8381
8382 gdb_assert (len > 0);
0186c6a7 8383 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8384
8385 pst->number_of_dependencies = len;
a9342b62 8386 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8387 for (i = 0; i < len; ++i)
f4dc4d17 8388 {
df07e2c7 8389 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8390 gdb_assert (iter->per_cu.is_debug_types);
8391 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8392 iter->type_unit_group = tu_group;
f4dc4d17
DE
8393 }
8394
df07e2c7
AB
8395 delete tu_group->tus;
8396 tu_group->tus = nullptr;
348e048f
DE
8397
8398 return 1;
8399}
8400
8401/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8402 Build partial symbol tables for the .debug_types comp-units. */
8403
8404static void
ed2dc618 8405build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8406{
ed2dc618 8407 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8408 return;
8409
ed2dc618 8410 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8411}
f4dc4d17 8412
6aa5f3a6
DE
8413/* Traversal function for process_skeletonless_type_unit.
8414 Read a TU in a DWO file and build partial symbols for it. */
8415
8416static int
8417process_skeletonless_type_unit (void **slot, void *info)
8418{
8419 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8420 struct dwarf2_per_objfile *dwarf2_per_objfile
8421 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8422 struct signatured_type find_entry, *entry;
8423
8424 /* If this TU doesn't exist in the global table, add it and read it in. */
8425
8426 if (dwarf2_per_objfile->signatured_types == NULL)
8427 {
8428 dwarf2_per_objfile->signatured_types
ed2dc618 8429 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8430 }
8431
8432 find_entry.signature = dwo_unit->signature;
8433 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8434 INSERT);
8435 /* If we've already seen this type there's nothing to do. What's happening
8436 is we're doing our own version of comdat-folding here. */
8437 if (*slot != NULL)
8438 return 1;
8439
8440 /* This does the job that create_all_type_units would have done for
8441 this TU. */
ed2dc618
SM
8442 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8443 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8444 *slot = entry;
8445
8446 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
8447 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8448 if (!reader.dummy_p)
8449 build_type_psymtabs_reader (&reader, reader.info_ptr,
8450 reader.comp_unit_die, reader.has_children);
6aa5f3a6
DE
8451
8452 return 1;
8453}
8454
8455/* Traversal function for process_skeletonless_type_units. */
8456
8457static int
8458process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8459{
8460 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8461
8462 if (dwo_file->tus != NULL)
8463 {
8464 htab_traverse_noresize (dwo_file->tus,
8465 process_skeletonless_type_unit, info);
8466 }
8467
8468 return 1;
8469}
8470
8471/* Scan all TUs of DWO files, verifying we've processed them.
8472 This is needed in case a TU was emitted without its skeleton.
8473 Note: This can't be done until we know what all the DWO files are. */
8474
8475static void
ed2dc618 8476process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8477{
8478 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8479 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8480 && dwarf2_per_objfile->dwo_files != NULL)
8481 {
51ac9db5 8482 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8483 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8484 dwarf2_per_objfile);
6aa5f3a6 8485 }
348e048f
DE
8486}
8487
ed2dc618 8488/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8489
8490static void
ed2dc618 8491set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8492{
b76e467d 8493 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8494 {
891813be 8495 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8496
36586728
TT
8497 if (pst == NULL)
8498 continue;
8499
b76e467d 8500 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8501 {
8502 /* Set the 'user' field only if it is not already set. */
8503 if (pst->dependencies[j]->user == NULL)
8504 pst->dependencies[j]->user = pst;
8505 }
8506 }
8507}
8508
93311388
DE
8509/* Build the partial symbol table by doing a quick pass through the
8510 .debug_info and .debug_abbrev sections. */
72bf9492 8511
93311388 8512static void
ed2dc618 8513dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8514{
ed2dc618 8515 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8516
b4f54984 8517 if (dwarf_read_debug)
45cfd468
DE
8518 {
8519 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8520 objfile_name (objfile));
45cfd468
DE
8521 }
8522
98bfdba5
PA
8523 dwarf2_per_objfile->reading_partial_symbols = 1;
8524
be391dca 8525 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8526
93311388
DE
8527 /* Any cached compilation units will be linked by the per-objfile
8528 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8529 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8530
ed2dc618 8531 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8532
ed2dc618 8533 create_all_comp_units (dwarf2_per_objfile);
c906108c 8534
60606b2c
TT
8535 /* Create a temporary address map on a temporary obstack. We later
8536 copy this to the final obstack. */
8268c778 8537 auto_obstack temp_obstack;
791afaa2
TT
8538
8539 scoped_restore save_psymtabs_addrmap
d320c2b5 8540 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8541 addrmap_create_mutable (&temp_obstack));
72bf9492 8542
b76e467d
SM
8543 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8544 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8545
6aa5f3a6 8546 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8547 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8548
8549 /* Now that all TUs have been processed we can fill in the dependencies. */
8550 if (dwarf2_per_objfile->type_unit_groups != NULL)
8551 {
8552 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8553 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8554 }
8555
b4f54984 8556 if (dwarf_read_debug)
ed2dc618 8557 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8558
ed2dc618 8559 set_partial_user (dwarf2_per_objfile);
95554aad 8560
d320c2b5
TT
8561 objfile->partial_symtabs->psymtabs_addrmap
8562 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8563 objfile->partial_symtabs->obstack ());
791afaa2
TT
8564 /* At this point we want to keep the address map. */
8565 save_psymtabs_addrmap.release ();
ff013f42 8566
b4f54984 8567 if (dwarf_read_debug)
45cfd468 8568 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8569 objfile_name (objfile));
ae038cb0
DJ
8570}
8571
dee91e82
DE
8572/* Load the partial DIEs for a secondary CU into memory.
8573 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8574
dee91e82
DE
8575static void
8576load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8577{
c0ab21c2
TT
8578 cutu_reader reader (this_cu, NULL, 1, 1, false);
8579
8580 if (!reader.dummy_p)
8581 {
8582 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8583 language_minimal);
8584
8585 /* Check if comp unit has_children.
8586 If so, read the rest of the partial symbols from this comp unit.
8587 If not, there's no more debug_info for this comp unit. */
8588 if (reader.has_children)
8589 load_partial_dies (&reader, reader.info_ptr, 0);
8590 }
ae038cb0
DJ
8591}
8592
ae038cb0 8593static void
ed2dc618 8594read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8595 struct dwarf2_section_info *section,
f1902523 8596 struct dwarf2_section_info *abbrev_section,
b76e467d 8597 unsigned int is_dwz)
ae038cb0 8598{
d521ce57 8599 const gdb_byte *info_ptr;
ed2dc618 8600 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8601
b4f54984 8602 if (dwarf_read_debug)
bf6af496 8603 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8604 get_section_name (section),
8605 get_section_file_name (section));
bf6af496 8606
36586728 8607 dwarf2_read_section (objfile, section);
ae038cb0 8608
36586728 8609 info_ptr = section->buffer;
6e70227d 8610
36586728 8611 while (info_ptr < section->buffer + section->size)
ae038cb0 8612 {
ae038cb0 8613 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8614
9c541725 8615 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8616
f1902523 8617 comp_unit_head cu_header;
ed2dc618
SM
8618 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8619 abbrev_section, info_ptr,
8620 rcuh_kind::COMPILE);
ae038cb0
DJ
8621
8622 /* Save the compilation unit for later lookup. */
f1902523
JK
8623 if (cu_header.unit_type != DW_UT_type)
8624 {
8625 this_cu = XOBNEW (&objfile->objfile_obstack,
8626 struct dwarf2_per_cu_data);
8627 memset (this_cu, 0, sizeof (*this_cu));
8628 }
8629 else
8630 {
8631 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8632 struct signatured_type);
8633 memset (sig_type, 0, sizeof (*sig_type));
8634 sig_type->signature = cu_header.signature;
8635 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8636 this_cu = &sig_type->per_cu;
8637 }
8638 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8639 this_cu->sect_off = sect_off;
f1902523 8640 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8641 this_cu->is_dwz = is_dwz;
e3b94546 8642 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8643 this_cu->section = section;
ae038cb0 8644
b76e467d 8645 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8646
8647 info_ptr = info_ptr + this_cu->length;
8648 }
36586728
TT
8649}
8650
8651/* Create a list of all compilation units in OBJFILE.
8652 This is only done for -readnow and building partial symtabs. */
8653
8654static void
ed2dc618 8655create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8656{
b76e467d 8657 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8658 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8659 &dwarf2_per_objfile->abbrev, 0);
36586728 8660
b76e467d 8661 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8662 if (dwz != NULL)
ed2dc618 8663 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8664 1);
c906108c
SS
8665}
8666
5734ee8b 8667/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8668 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8669 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8670 DW_AT_ranges). See the comments of add_partial_subprogram on how
8671 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8672
72bf9492
DJ
8673static void
8674scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8675 CORE_ADDR *highpc, int set_addrmap,
8676 struct dwarf2_cu *cu)
c906108c 8677{
72bf9492 8678 struct partial_die_info *pdi;
c906108c 8679
91c24f0a
DC
8680 /* Now, march along the PDI's, descending into ones which have
8681 interesting children but skipping the children of the other ones,
8682 until we reach the end of the compilation unit. */
c906108c 8683
72bf9492 8684 pdi = first_die;
91c24f0a 8685
72bf9492
DJ
8686 while (pdi != NULL)
8687 {
52356b79 8688 pdi->fixup (cu);
c906108c 8689
f55ee35c 8690 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8691 children, so we need to look at them. Ditto for anonymous
8692 enums. */
933c6fe4 8693
72bf9492 8694 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8695 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8696 || pdi->tag == DW_TAG_imported_unit
8697 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8698 {
72bf9492 8699 switch (pdi->tag)
c906108c
SS
8700 {
8701 case DW_TAG_subprogram:
b1dc1806 8702 case DW_TAG_inlined_subroutine:
cdc07690 8703 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8704 break;
72929c62 8705 case DW_TAG_constant:
c906108c
SS
8706 case DW_TAG_variable:
8707 case DW_TAG_typedef:
91c24f0a 8708 case DW_TAG_union_type:
72bf9492 8709 if (!pdi->is_declaration)
63d06c5c 8710 {
72bf9492 8711 add_partial_symbol (pdi, cu);
63d06c5c
DC
8712 }
8713 break;
c906108c 8714 case DW_TAG_class_type:
680b30c7 8715 case DW_TAG_interface_type:
c906108c 8716 case DW_TAG_structure_type:
72bf9492 8717 if (!pdi->is_declaration)
c906108c 8718 {
72bf9492 8719 add_partial_symbol (pdi, cu);
c906108c 8720 }
b7fee5a3
KS
8721 if ((cu->language == language_rust
8722 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8723 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8724 set_addrmap, cu);
c906108c 8725 break;
91c24f0a 8726 case DW_TAG_enumeration_type:
72bf9492
DJ
8727 if (!pdi->is_declaration)
8728 add_partial_enumeration (pdi, cu);
c906108c
SS
8729 break;
8730 case DW_TAG_base_type:
a02abb62 8731 case DW_TAG_subrange_type:
c906108c 8732 /* File scope base type definitions are added to the partial
c5aa993b 8733 symbol table. */
72bf9492 8734 add_partial_symbol (pdi, cu);
c906108c 8735 break;
d9fa45fe 8736 case DW_TAG_namespace:
cdc07690 8737 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8738 break;
5d7cb8df 8739 case DW_TAG_module:
59c35742
AB
8740 if (!pdi->is_declaration)
8741 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8742 break;
95554aad
TT
8743 case DW_TAG_imported_unit:
8744 {
8745 struct dwarf2_per_cu_data *per_cu;
8746
f4dc4d17
DE
8747 /* For now we don't handle imported units in type units. */
8748 if (cu->per_cu->is_debug_types)
8749 {
8750 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8751 " supported in type units [in module %s]"),
518817b3 8752 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8753 }
8754
e3b94546
SM
8755 per_cu = dwarf2_find_containing_comp_unit
8756 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8757 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8758
8759 /* Go read the partial unit, if needed. */
8760 if (per_cu->v.psymtab == NULL)
b93601f3 8761 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8762
ae640021 8763 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8764 }
8765 break;
74921315
KS
8766 case DW_TAG_imported_declaration:
8767 add_partial_symbol (pdi, cu);
8768 break;
c906108c
SS
8769 default:
8770 break;
8771 }
8772 }
8773
72bf9492
DJ
8774 /* If the die has a sibling, skip to the sibling. */
8775
8776 pdi = pdi->die_sibling;
8777 }
8778}
8779
8780/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8781
72bf9492 8782 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8783 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8784 Enumerators are an exception; they use the scope of their parent
8785 enumeration type, i.e. the name of the enumeration type is not
8786 prepended to the enumerator.
91c24f0a 8787
72bf9492
DJ
8788 There are two complexities. One is DW_AT_specification; in this
8789 case "parent" means the parent of the target of the specification,
8790 instead of the direct parent of the DIE. The other is compilers
8791 which do not emit DW_TAG_namespace; in this case we try to guess
8792 the fully qualified name of structure types from their members'
8793 linkage names. This must be done using the DIE's children rather
8794 than the children of any DW_AT_specification target. We only need
8795 to do this for structures at the top level, i.e. if the target of
8796 any DW_AT_specification (if any; otherwise the DIE itself) does not
8797 have a parent. */
8798
8799/* Compute the scope prefix associated with PDI's parent, in
8800 compilation unit CU. The result will be allocated on CU's
8801 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8802 field. NULL is returned if no prefix is necessary. */
15d034d0 8803static const char *
72bf9492
DJ
8804partial_die_parent_scope (struct partial_die_info *pdi,
8805 struct dwarf2_cu *cu)
8806{
15d034d0 8807 const char *grandparent_scope;
72bf9492 8808 struct partial_die_info *parent, *real_pdi;
91c24f0a 8809
72bf9492
DJ
8810 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8811 then this means the parent of the specification DIE. */
8812
8813 real_pdi = pdi;
72bf9492 8814 while (real_pdi->has_specification)
fb816e8b 8815 {
122cf0f2
AB
8816 auto res = find_partial_die (real_pdi->spec_offset,
8817 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8818 real_pdi = res.pdi;
8819 cu = res.cu;
8820 }
72bf9492
DJ
8821
8822 parent = real_pdi->die_parent;
8823 if (parent == NULL)
8824 return NULL;
8825
8826 if (parent->scope_set)
8827 return parent->scope;
8828
52356b79 8829 parent->fixup (cu);
72bf9492 8830
10b3939b 8831 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8832
acebe513
UW
8833 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8834 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8835 Work around this problem here. */
8836 if (cu->language == language_cplus
6e70227d 8837 && parent->tag == DW_TAG_namespace
acebe513
UW
8838 && strcmp (parent->name, "::") == 0
8839 && grandparent_scope == NULL)
8840 {
8841 parent->scope = NULL;
8842 parent->scope_set = 1;
8843 return NULL;
8844 }
8845
0a4b0913 8846 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8847 if (pdi->tag == DW_TAG_enumerator)
8848 /* Enumerators should not get the name of the enumeration as a prefix. */
8849 parent->scope = grandparent_scope;
8850 else if (parent->tag == DW_TAG_namespace
f55ee35c 8851 || parent->tag == DW_TAG_module
72bf9492
DJ
8852 || parent->tag == DW_TAG_structure_type
8853 || parent->tag == DW_TAG_class_type
680b30c7 8854 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8855 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8856 || parent->tag == DW_TAG_enumeration_type
8857 || (cu->language == language_fortran
8858 && parent->tag == DW_TAG_subprogram
8859 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8860 {
8861 if (grandparent_scope == NULL)
8862 parent->scope = parent->name;
8863 else
3e43a32a
MS
8864 parent->scope = typename_concat (&cu->comp_unit_obstack,
8865 grandparent_scope,
f55ee35c 8866 parent->name, 0, cu);
72bf9492 8867 }
72bf9492
DJ
8868 else
8869 {
8870 /* FIXME drow/2004-04-01: What should we be doing with
8871 function-local names? For partial symbols, we should probably be
8872 ignoring them. */
fa9c3fa0
TT
8873 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8874 dwarf_tag_name (parent->tag),
8875 sect_offset_str (pdi->sect_off));
72bf9492 8876 parent->scope = grandparent_scope;
c906108c
SS
8877 }
8878
72bf9492
DJ
8879 parent->scope_set = 1;
8880 return parent->scope;
8881}
8882
8883/* Return the fully scoped name associated with PDI, from compilation unit
8884 CU. The result will be allocated with malloc. */
4568ecf9 8885
43816ebc 8886static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8887partial_die_full_name (struct partial_die_info *pdi,
8888 struct dwarf2_cu *cu)
8889{
15d034d0 8890 const char *parent_scope;
72bf9492 8891
98bfdba5
PA
8892 /* If this is a template instantiation, we can not work out the
8893 template arguments from partial DIEs. So, unfortunately, we have
8894 to go through the full DIEs. At least any work we do building
8895 types here will be reused if full symbols are loaded later. */
8896 if (pdi->has_template_arguments)
8897 {
52356b79 8898 pdi->fixup (cu);
98bfdba5
PA
8899
8900 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8901 {
8902 struct die_info *die;
8903 struct attribute attr;
8904 struct dwarf2_cu *ref_cu = cu;
8905
b64f50a1 8906 /* DW_FORM_ref_addr is using section offset. */
b4069958 8907 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8908 attr.form = DW_FORM_ref_addr;
9c541725 8909 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8910 die = follow_die_ref (NULL, &attr, &ref_cu);
8911
43816ebc 8912 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8913 }
8914 }
8915
72bf9492
DJ
8916 parent_scope = partial_die_parent_scope (pdi, cu);
8917 if (parent_scope == NULL)
8918 return NULL;
8919 else
43816ebc
TT
8920 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8921 pdi->name, 0, cu));
c906108c
SS
8922}
8923
8924static void
72bf9492 8925add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8926{
518817b3
SM
8927 struct dwarf2_per_objfile *dwarf2_per_objfile
8928 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8929 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8930 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8931 CORE_ADDR addr = 0;
15d034d0 8932 const char *actual_name = NULL;
e142c38c
DJ
8933 CORE_ADDR baseaddr;
8934
b3b3bada 8935 baseaddr = objfile->text_section_offset ();
c906108c 8936
43816ebc
TT
8937 gdb::unique_xmalloc_ptr<char> built_actual_name
8938 = partial_die_full_name (pdi, cu);
15d034d0 8939 if (built_actual_name != NULL)
43816ebc 8940 actual_name = built_actual_name.get ();
63d06c5c 8941
72bf9492
DJ
8942 if (actual_name == NULL)
8943 actual_name = pdi->name;
8944
c906108c
SS
8945 switch (pdi->tag)
8946 {
b1dc1806 8947 case DW_TAG_inlined_subroutine:
c906108c 8948 case DW_TAG_subprogram:
79748972
TT
8949 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8950 - baseaddr);
0a4b0913
AB
8951 if (pdi->is_external
8952 || cu->language == language_ada
8953 || (cu->language == language_fortran
8954 && pdi->die_parent != NULL
8955 && pdi->die_parent->tag == DW_TAG_subprogram))
8956 {
8957 /* Normally, only "external" DIEs are part of the global scope.
8958 But in Ada and Fortran, we want to be able to access nested
8959 procedures globally. So all Ada and Fortran subprograms are
8960 stored in the global scope. */
31edb802 8961 add_psymbol_to_list (actual_name,
15d034d0 8962 built_actual_name != NULL,
f47fb265 8963 VAR_DOMAIN, LOC_BLOCK,
79748972 8964 SECT_OFF_TEXT (objfile),
75aedd27 8965 psymbol_placement::GLOBAL,
79748972
TT
8966 addr,
8967 cu->language, objfile);
c906108c
SS
8968 }
8969 else
8970 {
31edb802 8971 add_psymbol_to_list (actual_name,
15d034d0 8972 built_actual_name != NULL,
f47fb265 8973 VAR_DOMAIN, LOC_BLOCK,
79748972 8974 SECT_OFF_TEXT (objfile),
75aedd27 8975 psymbol_placement::STATIC,
1762568f 8976 addr, cu->language, objfile);
c906108c 8977 }
0c1b455e
TT
8978
8979 if (pdi->main_subprogram && actual_name != NULL)
8980 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8981 break;
72929c62 8982 case DW_TAG_constant:
31edb802 8983 add_psymbol_to_list (actual_name,
75aedd27
TT
8984 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8985 -1, (pdi->is_external
8986 ? psymbol_placement::GLOBAL
8987 : psymbol_placement::STATIC),
8988 0, cu->language, objfile);
72929c62 8989 break;
c906108c 8990 case DW_TAG_variable:
95554aad
TT
8991 if (pdi->d.locdesc)
8992 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8993
95554aad 8994 if (pdi->d.locdesc
caac4577
JG
8995 && addr == 0
8996 && !dwarf2_per_objfile->has_section_at_zero)
8997 {
8998 /* A global or static variable may also have been stripped
8999 out by the linker if unused, in which case its address
9000 will be nullified; do not add such variables into partial
9001 symbol table then. */
9002 }
9003 else if (pdi->is_external)
c906108c
SS
9004 {
9005 /* Global Variable.
9006 Don't enter into the minimal symbol tables as there is
9007 a minimal symbol table entry from the ELF symbols already.
9008 Enter into partial symbol table if it has a location
9009 descriptor or a type.
9010 If the location descriptor is missing, new_symbol will create
9011 a LOC_UNRESOLVED symbol, the address of the variable will then
9012 be determined from the minimal symbol table whenever the variable
9013 is referenced.
9014 The address for the partial symbol table entry is not
9015 used by GDB, but it comes in handy for debugging partial symbol
9016 table building. */
9017
95554aad 9018 if (pdi->d.locdesc || pdi->has_type)
31edb802 9019 add_psymbol_to_list (actual_name,
15d034d0 9020 built_actual_name != NULL,
f47fb265 9021 VAR_DOMAIN, LOC_STATIC,
79748972 9022 SECT_OFF_TEXT (objfile),
75aedd27 9023 psymbol_placement::GLOBAL,
79748972 9024 addr, cu->language, objfile);
c906108c
SS
9025 }
9026 else
9027 {
ff908ebf
AW
9028 int has_loc = pdi->d.locdesc != NULL;
9029
9030 /* Static Variable. Skip symbols whose value we cannot know (those
9031 without location descriptors or constant values). */
9032 if (!has_loc && !pdi->has_const_value)
43816ebc 9033 return;
ff908ebf 9034
31edb802 9035 add_psymbol_to_list (actual_name,
15d034d0 9036 built_actual_name != NULL,
f47fb265 9037 VAR_DOMAIN, LOC_STATIC,
79748972 9038 SECT_OFF_TEXT (objfile),
75aedd27 9039 psymbol_placement::STATIC,
79748972 9040 has_loc ? addr : 0,
f47fb265 9041 cu->language, objfile);
c906108c
SS
9042 }
9043 break;
9044 case DW_TAG_typedef:
9045 case DW_TAG_base_type:
a02abb62 9046 case DW_TAG_subrange_type:
31edb802 9047 add_psymbol_to_list (actual_name,
15d034d0 9048 built_actual_name != NULL,
79748972 9049 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9050 psymbol_placement::STATIC,
1762568f 9051 0, cu->language, objfile);
c906108c 9052 break;
74921315 9053 case DW_TAG_imported_declaration:
72bf9492 9054 case DW_TAG_namespace:
31edb802 9055 add_psymbol_to_list (actual_name,
15d034d0 9056 built_actual_name != NULL,
79748972 9057 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9058 psymbol_placement::GLOBAL,
1762568f 9059 0, cu->language, objfile);
72bf9492 9060 break;
530e8392 9061 case DW_TAG_module:
a5fd13a9
BH
9062 /* With Fortran 77 there might be a "BLOCK DATA" module
9063 available without any name. If so, we skip the module as it
9064 doesn't bring any value. */
9065 if (actual_name != nullptr)
31edb802 9066 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9067 built_actual_name != NULL,
9068 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9069 psymbol_placement::GLOBAL,
9070 0, cu->language, objfile);
530e8392 9071 break;
c906108c 9072 case DW_TAG_class_type:
680b30c7 9073 case DW_TAG_interface_type:
c906108c
SS
9074 case DW_TAG_structure_type:
9075 case DW_TAG_union_type:
9076 case DW_TAG_enumeration_type:
fa4028e9
JB
9077 /* Skip external references. The DWARF standard says in the section
9078 about "Structure, Union, and Class Type Entries": "An incomplete
9079 structure, union or class type is represented by a structure,
9080 union or class entry that does not have a byte size attribute
9081 and that has a DW_AT_declaration attribute." */
9082 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9083 return;
fa4028e9 9084
63d06c5c
DC
9085 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9086 static vs. global. */
31edb802 9087 add_psymbol_to_list (actual_name,
15d034d0 9088 built_actual_name != NULL,
79748972 9089 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9090 cu->language == language_cplus
75aedd27
TT
9091 ? psymbol_placement::GLOBAL
9092 : psymbol_placement::STATIC,
1762568f 9093 0, cu->language, objfile);
c906108c 9094
c906108c
SS
9095 break;
9096 case DW_TAG_enumerator:
31edb802 9097 add_psymbol_to_list (actual_name,
15d034d0 9098 built_actual_name != NULL,
79748972 9099 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9100 cu->language == language_cplus
75aedd27
TT
9101 ? psymbol_placement::GLOBAL
9102 : psymbol_placement::STATIC,
1762568f 9103 0, cu->language, objfile);
c906108c
SS
9104 break;
9105 default:
9106 break;
9107 }
9108}
9109
5c4e30ca
DC
9110/* Read a partial die corresponding to a namespace; also, add a symbol
9111 corresponding to that namespace to the symbol table. NAMESPACE is
9112 the name of the enclosing namespace. */
91c24f0a 9113
72bf9492
DJ
9114static void
9115add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9116 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9117 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9118{
72bf9492 9119 /* Add a symbol for the namespace. */
e7c27a73 9120
72bf9492 9121 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9122
9123 /* Now scan partial symbols in that namespace. */
9124
91c24f0a 9125 if (pdi->has_children)
cdc07690 9126 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9127}
9128
5d7cb8df
JK
9129/* Read a partial die corresponding to a Fortran module. */
9130
9131static void
9132add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9133 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9134{
530e8392
KB
9135 /* Add a symbol for the namespace. */
9136
9137 add_partial_symbol (pdi, cu);
9138
f55ee35c 9139 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9140
9141 if (pdi->has_children)
cdc07690 9142 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9143}
9144
b1dc1806
XR
9145/* Read a partial die corresponding to a subprogram or an inlined
9146 subprogram and create a partial symbol for that subprogram.
9147 When the CU language allows it, this routine also defines a partial
9148 symbol for each nested subprogram that this subprogram contains.
9149 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9150 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9151
cdc07690
YQ
9152 PDI may also be a lexical block, in which case we simply search
9153 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9154 Again, this is only performed when the CU language allows this
9155 type of definitions. */
9156
9157static void
9158add_partial_subprogram (struct partial_die_info *pdi,
9159 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9160 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9161{
b1dc1806 9162 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9163 {
9164 if (pdi->has_pc_info)
9165 {
9166 if (pdi->lowpc < *lowpc)
9167 *lowpc = pdi->lowpc;
9168 if (pdi->highpc > *highpc)
9169 *highpc = pdi->highpc;
cdc07690 9170 if (set_addrmap)
5734ee8b 9171 {
518817b3 9172 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9173 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9174 CORE_ADDR baseaddr;
b926417a
TT
9175 CORE_ADDR this_highpc;
9176 CORE_ADDR this_lowpc;
5734ee8b 9177
b3b3bada 9178 baseaddr = objfile->text_section_offset ();
b926417a
TT
9179 this_lowpc
9180 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9181 pdi->lowpc + baseaddr)
9182 - baseaddr);
9183 this_highpc
9184 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9185 pdi->highpc + baseaddr)
9186 - baseaddr);
d320c2b5 9187 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9188 this_lowpc, this_highpc - 1,
9291a0cd 9189 cu->per_cu->v.psymtab);
5734ee8b 9190 }
481860b3
GB
9191 }
9192
9193 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9194 {
bc30ff58 9195 if (!pdi->is_declaration)
e8d05480
JB
9196 /* Ignore subprogram DIEs that do not have a name, they are
9197 illegal. Do not emit a complaint at this point, we will
9198 do so when we convert this psymtab into a symtab. */
9199 if (pdi->name)
9200 add_partial_symbol (pdi, cu);
bc30ff58
JB
9201 }
9202 }
6e70227d 9203
bc30ff58
JB
9204 if (! pdi->has_children)
9205 return;
9206
0a4b0913 9207 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9208 {
9209 pdi = pdi->die_child;
9210 while (pdi != NULL)
9211 {
52356b79 9212 pdi->fixup (cu);
bc30ff58 9213 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9214 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9215 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9216 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9217 pdi = pdi->die_sibling;
9218 }
9219 }
9220}
9221
91c24f0a
DC
9222/* Read a partial die corresponding to an enumeration type. */
9223
72bf9492
DJ
9224static void
9225add_partial_enumeration (struct partial_die_info *enum_pdi,
9226 struct dwarf2_cu *cu)
91c24f0a 9227{
72bf9492 9228 struct partial_die_info *pdi;
91c24f0a
DC
9229
9230 if (enum_pdi->name != NULL)
72bf9492
DJ
9231 add_partial_symbol (enum_pdi, cu);
9232
9233 pdi = enum_pdi->die_child;
9234 while (pdi)
91c24f0a 9235 {
72bf9492 9236 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9237 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9238 else
72bf9492
DJ
9239 add_partial_symbol (pdi, cu);
9240 pdi = pdi->die_sibling;
91c24f0a 9241 }
91c24f0a
DC
9242}
9243
6caca83c
CC
9244/* Return the initial uleb128 in the die at INFO_PTR. */
9245
9246static unsigned int
d521ce57 9247peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9248{
9249 unsigned int bytes_read;
9250
9251 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9252}
9253
685af9cd
TT
9254/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9255 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9256
4bb7a0a7
DJ
9257 Return the corresponding abbrev, or NULL if the number is zero (indicating
9258 an empty DIE). In either case *BYTES_READ will be set to the length of
9259 the initial number. */
9260
9261static struct abbrev_info *
685af9cd
TT
9262peek_die_abbrev (const die_reader_specs &reader,
9263 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9264{
685af9cd 9265 dwarf2_cu *cu = reader.cu;
518817b3 9266 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9267 unsigned int abbrev_number
9268 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9269
9270 if (abbrev_number == 0)
9271 return NULL;
9272
685af9cd 9273 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9274 if (!abbrev)
9275 {
422b9917 9276 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9277 " at offset %s [in module %s]"),
422b9917 9278 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9279 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9280 }
9281
9282 return abbrev;
9283}
9284
93311388
DE
9285/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9286 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9287 DIE. Any children of the skipped DIEs will also be skipped. */
9288
d521ce57
TT
9289static const gdb_byte *
9290skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9291{
4bb7a0a7
DJ
9292 while (1)
9293 {
685af9cd
TT
9294 unsigned int bytes_read;
9295 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9296
4bb7a0a7
DJ
9297 if (abbrev == NULL)
9298 return info_ptr + bytes_read;
9299 else
dee91e82 9300 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9301 }
9302}
9303
93311388
DE
9304/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9305 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9306 abbrev corresponding to that skipped uleb128 should be passed in
9307 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9308 children. */
9309
d521ce57
TT
9310static const gdb_byte *
9311skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9312 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9313{
9314 unsigned int bytes_read;
9315 struct attribute attr;
dee91e82
DE
9316 bfd *abfd = reader->abfd;
9317 struct dwarf2_cu *cu = reader->cu;
d521ce57 9318 const gdb_byte *buffer = reader->buffer;
f664829e 9319 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9320 unsigned int form, i;
9321
9322 for (i = 0; i < abbrev->num_attrs; i++)
9323 {
9324 /* The only abbrev we care about is DW_AT_sibling. */
9325 if (abbrev->attrs[i].name == DW_AT_sibling)
9326 {
18a8505e
AT
9327 bool ignored;
9328 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9329 &ignored);
4bb7a0a7 9330 if (attr.form == DW_FORM_ref_addr)
b98664d3 9331 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9332 else
b9502d3f 9333 {
9c541725
PA
9334 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9335 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9336
9337 if (sibling_ptr < info_ptr)
b98664d3 9338 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9339 else if (sibling_ptr > reader->buffer_end)
9340 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9341 else
9342 return sibling_ptr;
9343 }
4bb7a0a7
DJ
9344 }
9345
9346 /* If it isn't DW_AT_sibling, skip this attribute. */
9347 form = abbrev->attrs[i].form;
9348 skip_attribute:
9349 switch (form)
9350 {
4bb7a0a7 9351 case DW_FORM_ref_addr:
ae411497
TT
9352 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9353 and later it is offset sized. */
9354 if (cu->header.version == 2)
9355 info_ptr += cu->header.addr_size;
9356 else
9357 info_ptr += cu->header.offset_size;
9358 break;
36586728
TT
9359 case DW_FORM_GNU_ref_alt:
9360 info_ptr += cu->header.offset_size;
9361 break;
ae411497 9362 case DW_FORM_addr:
4bb7a0a7
DJ
9363 info_ptr += cu->header.addr_size;
9364 break;
9365 case DW_FORM_data1:
9366 case DW_FORM_ref1:
9367 case DW_FORM_flag:
8fe0f950 9368 case DW_FORM_strx1:
4bb7a0a7
DJ
9369 info_ptr += 1;
9370 break;
2dc7f7b3 9371 case DW_FORM_flag_present:
43988095 9372 case DW_FORM_implicit_const:
2dc7f7b3 9373 break;
4bb7a0a7
DJ
9374 case DW_FORM_data2:
9375 case DW_FORM_ref2:
8fe0f950 9376 case DW_FORM_strx2:
4bb7a0a7
DJ
9377 info_ptr += 2;
9378 break;
8fe0f950
AT
9379 case DW_FORM_strx3:
9380 info_ptr += 3;
9381 break;
4bb7a0a7
DJ
9382 case DW_FORM_data4:
9383 case DW_FORM_ref4:
8fe0f950 9384 case DW_FORM_strx4:
4bb7a0a7
DJ
9385 info_ptr += 4;
9386 break;
9387 case DW_FORM_data8:
9388 case DW_FORM_ref8:
55f1336d 9389 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9390 info_ptr += 8;
9391 break;
0224619f
JK
9392 case DW_FORM_data16:
9393 info_ptr += 16;
9394 break;
4bb7a0a7 9395 case DW_FORM_string:
9b1c24c8 9396 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9397 info_ptr += bytes_read;
9398 break;
2dc7f7b3 9399 case DW_FORM_sec_offset:
4bb7a0a7 9400 case DW_FORM_strp:
36586728 9401 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9402 info_ptr += cu->header.offset_size;
9403 break;
2dc7f7b3 9404 case DW_FORM_exprloc:
4bb7a0a7
DJ
9405 case DW_FORM_block:
9406 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9407 info_ptr += bytes_read;
9408 break;
9409 case DW_FORM_block1:
9410 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9411 break;
9412 case DW_FORM_block2:
9413 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9414 break;
9415 case DW_FORM_block4:
9416 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9417 break;
336d760d 9418 case DW_FORM_addrx:
cf532bd1 9419 case DW_FORM_strx:
4bb7a0a7
DJ
9420 case DW_FORM_sdata:
9421 case DW_FORM_udata:
9422 case DW_FORM_ref_udata:
3019eac3
DE
9423 case DW_FORM_GNU_addr_index:
9424 case DW_FORM_GNU_str_index:
18a8505e 9425 case DW_FORM_rnglistx:
d521ce57 9426 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9427 break;
9428 case DW_FORM_indirect:
9429 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9430 info_ptr += bytes_read;
9431 /* We need to continue parsing from here, so just go back to
9432 the top. */
9433 goto skip_attribute;
9434
9435 default:
3e43a32a
MS
9436 error (_("Dwarf Error: Cannot handle %s "
9437 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9438 dwarf_form_name (form),
9439 bfd_get_filename (abfd));
9440 }
9441 }
9442
9443 if (abbrev->has_children)
dee91e82 9444 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9445 else
9446 return info_ptr;
9447}
9448
93311388 9449/* Locate ORIG_PDI's sibling.
dee91e82 9450 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9451
d521ce57 9452static const gdb_byte *
dee91e82
DE
9453locate_pdi_sibling (const struct die_reader_specs *reader,
9454 struct partial_die_info *orig_pdi,
d521ce57 9455 const gdb_byte *info_ptr)
91c24f0a
DC
9456{
9457 /* Do we know the sibling already? */
72bf9492 9458
91c24f0a
DC
9459 if (orig_pdi->sibling)
9460 return orig_pdi->sibling;
9461
9462 /* Are there any children to deal with? */
9463
9464 if (!orig_pdi->has_children)
9465 return info_ptr;
9466
4bb7a0a7 9467 /* Skip the children the long way. */
91c24f0a 9468
dee91e82 9469 return skip_children (reader, info_ptr);
91c24f0a
DC
9470}
9471
257e7a09 9472/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9473 not NULL. */
c906108c 9474
891813be
TT
9475void
9476dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9477{
ed2dc618
SM
9478 struct dwarf2_per_objfile *dwarf2_per_objfile
9479 = get_dwarf2_per_objfile (objfile);
9480
077cbab2
TT
9481 gdb_assert (!readin);
9482 /* If this psymtab is constructed from a debug-only objfile, the
9483 has_section_at_zero flag will not necessarily be correct. We
9484 can get the correct value for this flag by looking at the data
9485 associated with the (presumably stripped) associated objfile. */
9486 if (objfile->separate_debug_objfile_backlink)
c906108c 9487 {
077cbab2
TT
9488 struct dwarf2_per_objfile *dpo_backlink
9489 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9490
077cbab2
TT
9491 dwarf2_per_objfile->has_section_at_zero
9492 = dpo_backlink->has_section_at_zero;
9493 }
98bfdba5 9494
077cbab2 9495 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 9496
8566b89b 9497 expand_psymtab (objfile);
95554aad 9498
ed2dc618 9499 process_cu_includes (dwarf2_per_objfile);
c906108c 9500}
9cdd5dbd
DE
9501\f
9502/* Reading in full CUs. */
c906108c 9503
10b3939b
DJ
9504/* Add PER_CU to the queue. */
9505
9506static void
95554aad
TT
9507queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9508 enum language pretend_language)
10b3939b
DJ
9509{
9510 struct dwarf2_queue_item *item;
9511
9512 per_cu->queued = 1;
8d749320 9513 item = XNEW (struct dwarf2_queue_item);
10b3939b 9514 item->per_cu = per_cu;
95554aad 9515 item->pretend_language = pretend_language;
10b3939b
DJ
9516 item->next = NULL;
9517
9518 if (dwarf2_queue == NULL)
9519 dwarf2_queue = item;
9520 else
9521 dwarf2_queue_tail->next = item;
9522
9523 dwarf2_queue_tail = item;
9524}
9525
89e63ee4
DE
9526/* If PER_CU is not yet queued, add it to the queue.
9527 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9528 dependency.
0907af0c 9529 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9530 meaning either PER_CU is already queued or it is already loaded.
9531
9532 N.B. There is an invariant here that if a CU is queued then it is loaded.
9533 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9534
9535static int
89e63ee4 9536maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9537 struct dwarf2_per_cu_data *per_cu,
9538 enum language pretend_language)
9539{
9540 /* We may arrive here during partial symbol reading, if we need full
9541 DIEs to process an unusual case (e.g. template arguments). Do
9542 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9543 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9544 {
9545 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9546 return 1;
9547 return 0;
9548 }
9549
9550 /* Mark the dependence relation so that we don't flush PER_CU
9551 too early. */
89e63ee4
DE
9552 if (dependent_cu != NULL)
9553 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9554
9555 /* If it's already on the queue, we have nothing to do. */
9556 if (per_cu->queued)
9557 return 0;
9558
9559 /* If the compilation unit is already loaded, just mark it as
9560 used. */
9561 if (per_cu->cu != NULL)
9562 {
9563 per_cu->cu->last_used = 0;
9564 return 0;
9565 }
9566
9567 /* Add it to the queue. */
9568 queue_comp_unit (per_cu, pretend_language);
9569
9570 return 1;
9571}
9572
10b3939b
DJ
9573/* Process the queue. */
9574
9575static void
ed2dc618 9576process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9577{
9578 struct dwarf2_queue_item *item, *next_item;
9579
b4f54984 9580 if (dwarf_read_debug)
45cfd468
DE
9581 {
9582 fprintf_unfiltered (gdb_stdlog,
9583 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9584 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9585 }
9586
03dd20cc
DJ
9587 /* The queue starts out with one item, but following a DIE reference
9588 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9589 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9590 {
cc12ce38
DE
9591 if ((dwarf2_per_objfile->using_index
9592 ? !item->per_cu->v.quick->compunit_symtab
9593 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9594 /* Skip dummy CUs. */
9595 && item->per_cu->cu != NULL)
f4dc4d17
DE
9596 {
9597 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9598 unsigned int debug_print_threshold;
247f5c4f 9599 char buf[100];
f4dc4d17 9600
247f5c4f 9601 if (per_cu->is_debug_types)
f4dc4d17 9602 {
247f5c4f
DE
9603 struct signatured_type *sig_type =
9604 (struct signatured_type *) per_cu;
9605
9d8780f0 9606 sprintf (buf, "TU %s at offset %s",
73be47f5 9607 hex_string (sig_type->signature),
9d8780f0 9608 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9609 /* There can be 100s of TUs.
9610 Only print them in verbose mode. */
9611 debug_print_threshold = 2;
f4dc4d17 9612 }
247f5c4f 9613 else
73be47f5 9614 {
9d8780f0
SM
9615 sprintf (buf, "CU at offset %s",
9616 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9617 debug_print_threshold = 1;
9618 }
247f5c4f 9619
b4f54984 9620 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9621 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9622
9623 if (per_cu->is_debug_types)
9624 process_full_type_unit (per_cu, item->pretend_language);
9625 else
9626 process_full_comp_unit (per_cu, item->pretend_language);
9627
b4f54984 9628 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9629 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9630 }
10b3939b
DJ
9631
9632 item->per_cu->queued = 0;
9633 next_item = item->next;
9634 xfree (item);
9635 }
9636
9637 dwarf2_queue_tail = NULL;
45cfd468 9638
b4f54984 9639 if (dwarf_read_debug)
45cfd468
DE
9640 {
9641 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9642 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9643 }
10b3939b
DJ
9644}
9645
10b3939b
DJ
9646/* Read in full symbols for PST, and anything it depends on. */
9647
8566b89b
TT
9648void
9649dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9650{
10b3939b 9651 struct dwarf2_per_cu_data *per_cu;
aaa75496 9652
8566b89b 9653 if (readin)
95554aad
TT
9654 return;
9655
0494dbec 9656 read_dependencies (objfile);
aaa75496 9657
8566b89b 9658 per_cu = per_cu_data;
10b3939b
DJ
9659
9660 if (per_cu == NULL)
aaa75496
JB
9661 {
9662 /* It's an include file, no symbols to read for it.
9663 Everything is in the parent symtab. */
8566b89b 9664 readin = true;
aaa75496
JB
9665 return;
9666 }
c906108c 9667
58f0c718 9668 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9669}
9670
dee91e82
DE
9671/* Trivial hash function for die_info: the hash value of a DIE
9672 is its offset in .debug_info for this objfile. */
10b3939b 9673
dee91e82
DE
9674static hashval_t
9675die_hash (const void *item)
10b3939b 9676{
9a3c8263 9677 const struct die_info *die = (const struct die_info *) item;
6502dd73 9678
9c541725 9679 return to_underlying (die->sect_off);
dee91e82 9680}
63d06c5c 9681
dee91e82
DE
9682/* Trivial comparison function for die_info structures: two DIEs
9683 are equal if they have the same offset. */
98bfdba5 9684
dee91e82
DE
9685static int
9686die_eq (const void *item_lhs, const void *item_rhs)
9687{
9a3c8263
SM
9688 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9689 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9690
9c541725 9691 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9692}
c906108c 9693
c0ab21c2 9694/* Load the DIEs associated with PER_CU into memory. */
c906108c 9695
dee91e82 9696static void
c0ab21c2
TT
9697load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9698 bool skip_partial,
9699 enum language pretend_language)
dee91e82 9700{
c0ab21c2
TT
9701 gdb_assert (! this_cu->is_debug_types);
9702
9703 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9704 if (reader.dummy_p)
9705 return;
9706
9707 struct dwarf2_cu *cu = reader.cu;
9708 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9709
dee91e82
DE
9710 gdb_assert (cu->die_hash == NULL);
9711 cu->die_hash =
9712 htab_create_alloc_ex (cu->header.length / 12,
9713 die_hash,
9714 die_eq,
9715 NULL,
9716 &cu->comp_unit_obstack,
9717 hashtab_obstack_allocate,
9718 dummy_obstack_deallocate);
e142c38c 9719
c0ab21c2
TT
9720 if (reader.has_children)
9721 reader.comp_unit_die->child
9722 = read_die_and_siblings (&reader, reader.info_ptr,
9723 &info_ptr, reader.comp_unit_die);
9724 cu->dies = reader.comp_unit_die;
dee91e82 9725 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9726
9727 /* We try not to read any attributes in this function, because not
9cdd5dbd 9728 all CUs needed for references have been loaded yet, and symbol
10b3939b 9729 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9730 or we won't be able to build types correctly.
9731 Similarly, if we do not read the producer, we can not apply
9732 producer-specific interpretation. */
c0ab21c2 9733 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9734}
9735
3da10d80
KS
9736/* Add a DIE to the delayed physname list. */
9737
9738static void
9739add_to_method_list (struct type *type, int fnfield_index, int index,
9740 const char *name, struct die_info *die,
9741 struct dwarf2_cu *cu)
9742{
9743 struct delayed_method_info mi;
9744 mi.type = type;
9745 mi.fnfield_index = fnfield_index;
9746 mi.index = index;
9747 mi.name = name;
9748 mi.die = die;
c89b44cd 9749 cu->method_list.push_back (mi);
3da10d80
KS
9750}
9751
3693fdb3
PA
9752/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9753 "const" / "volatile". If so, decrements LEN by the length of the
9754 modifier and return true. Otherwise return false. */
9755
9756template<size_t N>
9757static bool
9758check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9759{
9760 size_t mod_len = sizeof (mod) - 1;
9761 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9762 {
9763 len -= mod_len;
9764 return true;
9765 }
9766 return false;
9767}
9768
3da10d80
KS
9769/* Compute the physnames of any methods on the CU's method list.
9770
9771 The computation of method physnames is delayed in order to avoid the
9772 (bad) condition that one of the method's formal parameters is of an as yet
9773 incomplete type. */
9774
9775static void
9776compute_delayed_physnames (struct dwarf2_cu *cu)
9777{
3693fdb3 9778 /* Only C++ delays computing physnames. */
c89b44cd 9779 if (cu->method_list.empty ())
3693fdb3
PA
9780 return;
9781 gdb_assert (cu->language == language_cplus);
9782
52941706 9783 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9784 {
1d06ead6 9785 const char *physname;
3da10d80 9786 struct fn_fieldlist *fn_flp
c89b44cd
TT
9787 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9788 physname = dwarf2_physname (mi.name, mi.die, cu);
9789 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9790 = physname ? physname : "";
3693fdb3
PA
9791
9792 /* Since there's no tag to indicate whether a method is a
9793 const/volatile overload, extract that information out of the
9794 demangled name. */
9795 if (physname != NULL)
9796 {
9797 size_t len = strlen (physname);
9798
9799 while (1)
9800 {
9801 if (physname[len] == ')') /* shortcut */
9802 break;
9803 else if (check_modifier (physname, len, " const"))
c89b44cd 9804 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9805 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9806 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9807 else
9808 break;
9809 }
9810 }
3da10d80 9811 }
c89b44cd
TT
9812
9813 /* The list is no longer needed. */
9814 cu->method_list.clear ();
3da10d80
KS
9815}
9816
a766d390
DE
9817/* Go objects should be embedded in a DW_TAG_module DIE,
9818 and it's not clear if/how imported objects will appear.
9819 To keep Go support simple until that's worked out,
9820 go back through what we've read and create something usable.
9821 We could do this while processing each DIE, and feels kinda cleaner,
9822 but that way is more invasive.
9823 This is to, for example, allow the user to type "p var" or "b main"
9824 without having to specify the package name, and allow lookups
9825 of module.object to work in contexts that use the expression
9826 parser. */
9827
9828static void
9829fixup_go_packaging (struct dwarf2_cu *cu)
9830{
421d1616 9831 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9832 struct pending *list;
9833 int i;
9834
c24bdb02 9835 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9836 list != NULL;
9837 list = list->next)
a766d390
DE
9838 {
9839 for (i = 0; i < list->nsyms; ++i)
9840 {
9841 struct symbol *sym = list->symbol[i];
9842
c1b5c1eb 9843 if (sym->language () == language_go
a766d390
DE
9844 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9845 {
421d1616
TT
9846 gdb::unique_xmalloc_ptr<char> this_package_name
9847 (go_symbol_package_name (sym));
a766d390
DE
9848
9849 if (this_package_name == NULL)
9850 continue;
9851 if (package_name == NULL)
421d1616 9852 package_name = std::move (this_package_name);
a766d390
DE
9853 else
9854 {
518817b3
SM
9855 struct objfile *objfile
9856 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9857 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9858 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9859 (symbol_symtab (sym) != NULL
9860 ? symtab_to_filename_for_display
9861 (symbol_symtab (sym))
e3b94546 9862 : objfile_name (objfile)),
421d1616 9863 this_package_name.get (), package_name.get ());
a766d390
DE
9864 }
9865 }
9866 }
9867 }
9868
9869 if (package_name != NULL)
9870 {
518817b3 9871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9872 const char *saved_package_name
421d1616 9873 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9874 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9875 saved_package_name);
a766d390
DE
9876 struct symbol *sym;
9877
e623cf5d 9878 sym = allocate_symbol (objfile);
d3ecddab 9879 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9880 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9881 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9882 e.g., "main" finds the "main" module and not C's main(). */
9883 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9884 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9885 SYMBOL_TYPE (sym) = type;
9886
c24bdb02 9887 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9888 }
9889}
9890
c9317f21
TT
9891/* Allocate a fully-qualified name consisting of the two parts on the
9892 obstack. */
9893
9894static const char *
9895rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9896{
9897 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9898}
9899
9900/* A helper that allocates a struct discriminant_info to attach to a
9901 union type. */
9902
9903static struct discriminant_info *
9904alloc_discriminant_info (struct type *type, int discriminant_index,
9905 int default_index)
9906{
9907 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9908 gdb_assert (discriminant_index == -1
9909 || (discriminant_index >= 0
9910 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9911 gdb_assert (default_index == -1
c7b15a66 9912 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9913
9914 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9915
9916 struct discriminant_info *disc
9917 = ((struct discriminant_info *)
9918 TYPE_ZALLOC (type,
9919 offsetof (struct discriminant_info, discriminants)
9920 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9921 disc->default_index = default_index;
9922 disc->discriminant_index = discriminant_index;
9923
9924 struct dynamic_prop prop;
9925 prop.kind = PROP_UNDEFINED;
9926 prop.data.baton = disc;
9927
9928 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9929
9930 return disc;
9931}
9932
9933/* Some versions of rustc emitted enums in an unusual way.
9934
9935 Ordinary enums were emitted as unions. The first element of each
9936 structure in the union was named "RUST$ENUM$DISR". This element
9937 held the discriminant.
9938
9939 These versions of Rust also implemented the "non-zero"
9940 optimization. When the enum had two values, and one is empty and
9941 the other holds a pointer that cannot be zero, the pointer is used
9942 as the discriminant, with a zero value meaning the empty variant.
9943 Here, the union's first member is of the form
9944 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9945 where the fieldnos are the indices of the fields that should be
9946 traversed in order to find the field (which may be several fields deep)
9947 and the variantname is the name of the variant of the case when the
9948 field is zero.
9949
9950 This function recognizes whether TYPE is of one of these forms,
9951 and, if so, smashes it to be a variant type. */
9952
9953static void
9954quirk_rust_enum (struct type *type, struct objfile *objfile)
9955{
9956 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9957
9958 /* We don't need to deal with empty enums. */
9959 if (TYPE_NFIELDS (type) == 0)
9960 return;
9961
9962#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9963 if (TYPE_NFIELDS (type) == 1
9964 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9965 {
9966 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9967
9968 /* Decode the field name to find the offset of the
9969 discriminant. */
9970 ULONGEST bit_offset = 0;
9971 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9972 while (name[0] >= '0' && name[0] <= '9')
9973 {
9974 char *tail;
9975 unsigned long index = strtoul (name, &tail, 10);
9976 name = tail;
9977 if (*name != '$'
9978 || index >= TYPE_NFIELDS (field_type)
9979 || (TYPE_FIELD_LOC_KIND (field_type, index)
9980 != FIELD_LOC_KIND_BITPOS))
9981 {
b98664d3 9982 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9983 "[in module %s]"),
9984 TYPE_FIELD_NAME (type, 0),
9985 objfile_name (objfile));
9986 return;
9987 }
9988 ++name;
9989
9990 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9991 field_type = TYPE_FIELD_TYPE (field_type, index);
9992 }
9993
9994 /* Make a union to hold the variants. */
9995 struct type *union_type = alloc_type (objfile);
9996 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9997 TYPE_NFIELDS (union_type) = 3;
9998 TYPE_FIELDS (union_type)
9999 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10000 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10001 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10002
10003 /* Put the discriminant must at index 0. */
10004 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10005 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10006 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10007 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10008
10009 /* The order of fields doesn't really matter, so put the real
10010 field at index 1 and the data-less field at index 2. */
10011 struct discriminant_info *disc
10012 = alloc_discriminant_info (union_type, 0, 1);
10013 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10014 TYPE_FIELD_NAME (union_type, 1)
10015 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10016 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10017 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10018 TYPE_FIELD_NAME (union_type, 1));
10019
10020 const char *dataless_name
10021 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10022 name);
10023 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10024 dataless_name);
10025 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10026 /* NAME points into the original discriminant name, which
10027 already has the correct lifetime. */
10028 TYPE_FIELD_NAME (union_type, 2) = name;
10029 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10030 disc->discriminants[2] = 0;
10031
10032 /* Smash this type to be a structure type. We have to do this
10033 because the type has already been recorded. */
10034 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10035 TYPE_NFIELDS (type) = 1;
10036 TYPE_FIELDS (type)
10037 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10038
10039 /* Install the variant part. */
10040 TYPE_FIELD_TYPE (type, 0) = union_type;
10041 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10042 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10043 }
77c2dba3
TT
10044 /* A union with a single anonymous field is probably an old-style
10045 univariant enum. */
10046 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10047 {
c9317f21
TT
10048 /* Smash this type to be a structure type. We have to do this
10049 because the type has already been recorded. */
10050 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10051
10052 /* Make a union to hold the variants. */
10053 struct type *union_type = alloc_type (objfile);
10054 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10055 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10056 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10057 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10058 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10059
10060 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10061 const char *variant_name
10062 = rust_last_path_segment (TYPE_NAME (field_type));
10063 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10064 TYPE_NAME (field_type)
10065 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10066 TYPE_NAME (type), variant_name);
c9317f21
TT
10067
10068 /* Install the union in the outer struct type. */
10069 TYPE_NFIELDS (type) = 1;
10070 TYPE_FIELDS (type)
10071 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10072 TYPE_FIELD_TYPE (type, 0) = union_type;
10073 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10074 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10075
10076 alloc_discriminant_info (union_type, -1, 0);
10077 }
10078 else
10079 {
10080 struct type *disr_type = nullptr;
10081 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10082 {
10083 disr_type = TYPE_FIELD_TYPE (type, i);
10084
a037790e
TT
10085 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10086 {
10087 /* All fields of a true enum will be structs. */
10088 return;
10089 }
10090 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10091 {
10092 /* Could be data-less variant, so keep going. */
a037790e 10093 disr_type = nullptr;
c9317f21
TT
10094 }
10095 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10096 "RUST$ENUM$DISR") != 0)
10097 {
10098 /* Not a Rust enum. */
10099 return;
10100 }
10101 else
10102 {
10103 /* Found one. */
10104 break;
10105 }
10106 }
10107
10108 /* If we got here without a discriminant, then it's probably
10109 just a union. */
10110 if (disr_type == nullptr)
10111 return;
10112
10113 /* Smash this type to be a structure type. We have to do this
10114 because the type has already been recorded. */
10115 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10116
10117 /* Make a union to hold the variants. */
10118 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10119 struct type *union_type = alloc_type (objfile);
10120 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10121 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10122 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10123 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10124 TYPE_FIELDS (union_type)
10125 = (struct field *) TYPE_ZALLOC (union_type,
10126 (TYPE_NFIELDS (union_type)
10127 * sizeof (struct field)));
10128
10129 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10130 TYPE_NFIELDS (type) * sizeof (struct field));
10131
10132 /* Install the discriminant at index 0 in the union. */
10133 TYPE_FIELD (union_type, 0) = *disr_field;
10134 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10135 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10136
10137 /* Install the union in the outer struct type. */
10138 TYPE_FIELD_TYPE (type, 0) = union_type;
10139 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10140 TYPE_NFIELDS (type) = 1;
10141
10142 /* Set the size and offset of the union type. */
10143 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10144
10145 /* We need a way to find the correct discriminant given a
10146 variant name. For convenience we build a map here. */
10147 struct type *enum_type = FIELD_TYPE (*disr_field);
10148 std::unordered_map<std::string, ULONGEST> discriminant_map;
10149 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10150 {
10151 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10152 {
10153 const char *name
10154 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10155 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10156 }
10157 }
10158
10159 int n_fields = TYPE_NFIELDS (union_type);
10160 struct discriminant_info *disc
10161 = alloc_discriminant_info (union_type, 0, -1);
10162 /* Skip the discriminant here. */
10163 for (int i = 1; i < n_fields; ++i)
10164 {
10165 /* Find the final word in the name of this variant's type.
10166 That name can be used to look up the correct
10167 discriminant. */
10168 const char *variant_name
10169 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10170 i)));
10171
10172 auto iter = discriminant_map.find (variant_name);
10173 if (iter != discriminant_map.end ())
10174 disc->discriminants[i] = iter->second;
10175
bedda9ac 10176 /* Remove the discriminant field, if it exists. */
c9317f21 10177 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10178 if (TYPE_NFIELDS (sub_type) > 0)
10179 {
10180 --TYPE_NFIELDS (sub_type);
10181 ++TYPE_FIELDS (sub_type);
10182 }
c9317f21
TT
10183 TYPE_FIELD_NAME (union_type, i) = variant_name;
10184 TYPE_NAME (sub_type)
10185 = rust_fully_qualify (&objfile->objfile_obstack,
10186 TYPE_NAME (type), variant_name);
10187 }
10188 }
10189}
10190
10191/* Rewrite some Rust unions to be structures with variants parts. */
10192
10193static void
10194rust_union_quirks (struct dwarf2_cu *cu)
10195{
10196 gdb_assert (cu->language == language_rust);
52941706
SM
10197 for (type *type_ : cu->rust_unions)
10198 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10199 /* We don't need this any more. */
10200 cu->rust_unions.clear ();
c9317f21
TT
10201}
10202
95554aad
TT
10203/* Return the symtab for PER_CU. This works properly regardless of
10204 whether we're using the index or psymtabs. */
10205
43f3e411
DE
10206static struct compunit_symtab *
10207get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10208{
ed2dc618 10209 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10210 ? per_cu->v.quick->compunit_symtab
10211 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10212}
10213
10214/* A helper function for computing the list of all symbol tables
10215 included by PER_CU. */
10216
10217static void
4c39bc03 10218recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10219 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10220 struct dwarf2_per_cu_data *per_cu,
43f3e411 10221 struct compunit_symtab *immediate_parent)
95554aad
TT
10222{
10223 void **slot;
43f3e411 10224 struct compunit_symtab *cust;
95554aad
TT
10225
10226 slot = htab_find_slot (all_children, per_cu, INSERT);
10227 if (*slot != NULL)
10228 {
10229 /* This inclusion and its children have been processed. */
10230 return;
10231 }
10232
10233 *slot = per_cu;
10234 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10235 cust = get_compunit_symtab (per_cu);
10236 if (cust != NULL)
ec94af83
DE
10237 {
10238 /* If this is a type unit only add its symbol table if we haven't
10239 seen it yet (type unit per_cu's can share symtabs). */
10240 if (per_cu->is_debug_types)
10241 {
43f3e411 10242 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10243 if (*slot == NULL)
10244 {
43f3e411 10245 *slot = cust;
4c39bc03 10246 result->push_back (cust);
43f3e411
DE
10247 if (cust->user == NULL)
10248 cust->user = immediate_parent;
ec94af83
DE
10249 }
10250 }
10251 else
f9125b6c 10252 {
4c39bc03 10253 result->push_back (cust);
43f3e411
DE
10254 if (cust->user == NULL)
10255 cust->user = immediate_parent;
f9125b6c 10256 }
ec94af83 10257 }
95554aad 10258
ae640021
AB
10259 if (!per_cu->imported_symtabs_empty ())
10260 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10261 {
10262 recursively_compute_inclusions (result, all_children,
10263 all_type_symtabs, ptr, cust);
10264 }
95554aad
TT
10265}
10266
43f3e411 10267/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10268 PER_CU. */
10269
10270static void
43f3e411 10271compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10272{
f4dc4d17
DE
10273 gdb_assert (! per_cu->is_debug_types);
10274
ae640021 10275 if (!per_cu->imported_symtabs_empty ())
95554aad 10276 {
ae640021 10277 int len;
4c39bc03 10278 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10279 htab_t all_children, all_type_symtabs;
43f3e411 10280 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10281
10282 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10283 if (cust == NULL)
95554aad
TT
10284 return;
10285
10286 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10287 NULL, xcalloc, xfree);
ec94af83
DE
10288 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10289 NULL, xcalloc, xfree);
95554aad 10290
ae640021 10291 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10292 {
10293 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10294 all_type_symtabs, ptr, cust);
ec94af83 10295 }
95554aad 10296
ec94af83 10297 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10298 len = result_symtabs.size ();
43f3e411 10299 cust->includes
ed2dc618 10300 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10301 struct compunit_symtab *, len + 1);
4c39bc03
TT
10302 memcpy (cust->includes, result_symtabs.data (),
10303 len * sizeof (compunit_symtab *));
43f3e411 10304 cust->includes[len] = NULL;
95554aad 10305
95554aad 10306 htab_delete (all_children);
ec94af83 10307 htab_delete (all_type_symtabs);
95554aad
TT
10308 }
10309}
10310
10311/* Compute the 'includes' field for the symtabs of all the CUs we just
10312 read. */
10313
10314static void
ed2dc618 10315process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10316{
71b73764 10317 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10318 {
10319 if (! iter->is_debug_types)
43f3e411 10320 compute_compunit_symtab_includes (iter);
f4dc4d17 10321 }
95554aad 10322
c5d0225d 10323 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10324}
10325
9cdd5dbd 10326/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10327 already been loaded into memory. */
10328
10329static void
95554aad
TT
10330process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10331 enum language pretend_language)
10b3939b 10332{
10b3939b 10333 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10334 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10335 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10336 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10337 CORE_ADDR lowpc, highpc;
43f3e411 10338 struct compunit_symtab *cust;
10b3939b 10339 CORE_ADDR baseaddr;
4359dff1 10340 struct block *static_block;
3e29f34a 10341 CORE_ADDR addr;
10b3939b 10342
b3b3bada 10343 baseaddr = objfile->text_section_offset ();
10b3939b 10344
c89b44cd
TT
10345 /* Clear the list here in case something was left over. */
10346 cu->method_list.clear ();
10b3939b 10347
95554aad
TT
10348 cu->language = pretend_language;
10349 cu->language_defn = language_def (cu->language);
10350
c906108c 10351 /* Do line number decoding in read_file_scope () */
10b3939b 10352 process_die (cu->dies, cu);
c906108c 10353
a766d390
DE
10354 /* For now fudge the Go package. */
10355 if (cu->language == language_go)
10356 fixup_go_packaging (cu);
10357
5f48f8f3 10358 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10359 should be complete, and it should now be safe to compute all of the
10360 physnames. */
10361 compute_delayed_physnames (cu);
3da10d80 10362
c9317f21
TT
10363 if (cu->language == language_rust)
10364 rust_union_quirks (cu);
10365
fae299cd
DC
10366 /* Some compilers don't define a DW_AT_high_pc attribute for the
10367 compilation unit. If the DW_AT_high_pc is missing, synthesize
10368 it, by scanning the DIE's below the compilation unit. */
10b3939b 10369 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10370
3e29f34a 10371 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10372 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10373
10374 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10375 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10376 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10377 addrmap to help ensure it has an accurate map of pc values belonging to
10378 this comp unit. */
10379 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10380
c24bdb02 10381 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10382 SECT_OFF_TEXT (objfile),
10383 0);
c906108c 10384
43f3e411 10385 if (cust != NULL)
c906108c 10386 {
df15bd07 10387 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10388
8be455d7
JK
10389 /* Set symtab language to language from DW_AT_language. If the
10390 compilation is from a C file generated by language preprocessors, do
10391 not set the language if it was already deduced by start_subfile. */
43f3e411 10392 if (!(cu->language == language_c
40e3ad0e 10393 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10394 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10395
10396 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10397 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10398 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10399 there were bugs in prologue debug info, fixed later in GCC-4.5
10400 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10401
10402 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10403 needed, it would be wrong due to missing DW_AT_producer there.
10404
10405 Still one can confuse GDB by using non-standard GCC compilation
10406 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10407 */
ab260dad 10408 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10409 cust->locations_valid = 1;
e0d00bc7
JK
10410
10411 if (gcc_4_minor >= 5)
43f3e411 10412 cust->epilogue_unwind_valid = 1;
96408a79 10413
43f3e411 10414 cust->call_site_htab = cu->call_site_htab;
c906108c 10415 }
9291a0cd
TT
10416
10417 if (dwarf2_per_objfile->using_index)
43f3e411 10418 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10419 else
10420 {
891813be 10421 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10422 pst->compunit_symtab = cust;
6d94535f 10423 pst->readin = true;
9291a0cd 10424 }
c906108c 10425
95554aad 10426 /* Push it for inclusion processing later. */
c5d0225d 10427 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10428
10429 /* Not needed any more. */
c24bdb02 10430 cu->reset_builder ();
f4dc4d17 10431}
45cfd468 10432
f4dc4d17
DE
10433/* Generate full symbol information for type unit PER_CU, whose DIEs have
10434 already been loaded into memory. */
10435
10436static void
10437process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10438 enum language pretend_language)
10439{
10440 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10441 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10442 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10443 struct compunit_symtab *cust;
0186c6a7
DE
10444 struct signatured_type *sig_type;
10445
10446 gdb_assert (per_cu->is_debug_types);
10447 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10448
c89b44cd
TT
10449 /* Clear the list here in case something was left over. */
10450 cu->method_list.clear ();
f4dc4d17 10451
f4dc4d17
DE
10452 cu->language = pretend_language;
10453 cu->language_defn = language_def (cu->language);
10454
10455 /* The symbol tables are set up in read_type_unit_scope. */
10456 process_die (cu->dies, cu);
10457
10458 /* For now fudge the Go package. */
10459 if (cu->language == language_go)
10460 fixup_go_packaging (cu);
10461
5f48f8f3 10462 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10463 should be complete, and it should now be safe to compute all of the
10464 physnames. */
10465 compute_delayed_physnames (cu);
f4dc4d17 10466
c9317f21
TT
10467 if (cu->language == language_rust)
10468 rust_union_quirks (cu);
10469
f4dc4d17
DE
10470 /* TUs share symbol tables.
10471 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10472 of it with end_expandable_symtab. Otherwise, complete the addition of
10473 this TU's symbols to the existing symtab. */
43f3e411 10474 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10475 {
c24bdb02
KS
10476 buildsym_compunit *builder = cu->get_builder ();
10477 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10478 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10479
43f3e411 10480 if (cust != NULL)
f4dc4d17
DE
10481 {
10482 /* Set symtab language to language from DW_AT_language. If the
10483 compilation is from a C file generated by language preprocessors,
10484 do not set the language if it was already deduced by
10485 start_subfile. */
43f3e411
DE
10486 if (!(cu->language == language_c
10487 && COMPUNIT_FILETABS (cust)->language != language_c))
10488 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10489 }
10490 }
10491 else
10492 {
c24bdb02 10493 cu->get_builder ()->augment_type_symtab ();
43f3e411 10494 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10495 }
10496
10497 if (dwarf2_per_objfile->using_index)
43f3e411 10498 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10499 else
10500 {
891813be 10501 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 10502 pst->compunit_symtab = cust;
6d94535f 10503 pst->readin = true;
45cfd468 10504 }
804d2729
TT
10505
10506 /* Not needed any more. */
c24bdb02 10507 cu->reset_builder ();
c906108c
SS
10508}
10509
95554aad
TT
10510/* Process an imported unit DIE. */
10511
10512static void
10513process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10514{
10515 struct attribute *attr;
10516
f4dc4d17
DE
10517 /* For now we don't handle imported units in type units. */
10518 if (cu->per_cu->is_debug_types)
10519 {
10520 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10521 " supported in type units [in module %s]"),
518817b3 10522 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10523 }
10524
95554aad
TT
10525 attr = dwarf2_attr (die, DW_AT_import, cu);
10526 if (attr != NULL)
10527 {
9c541725
PA
10528 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10529 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10530 dwarf2_per_cu_data *per_cu
e3b94546 10531 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10532 cu->per_cu->dwarf2_per_objfile);
95554aad 10533
69d751e3 10534 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10535 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10536 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10537
ae640021 10538 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10539 }
10540}
10541
4c8aa72d
PA
10542/* RAII object that represents a process_die scope: i.e.,
10543 starts/finishes processing a DIE. */
10544class process_die_scope
adde2bff 10545{
4c8aa72d
PA
10546public:
10547 process_die_scope (die_info *die, dwarf2_cu *cu)
10548 : m_die (die), m_cu (cu)
10549 {
10550 /* We should only be processing DIEs not already in process. */
10551 gdb_assert (!m_die->in_process);
10552 m_die->in_process = true;
10553 }
8c3cb9fa 10554
4c8aa72d
PA
10555 ~process_die_scope ()
10556 {
10557 m_die->in_process = false;
10558
10559 /* If we're done processing the DIE for the CU that owns the line
10560 header, we don't need the line header anymore. */
10561 if (m_cu->line_header_die_owner == m_die)
10562 {
10563 delete m_cu->line_header;
10564 m_cu->line_header = NULL;
10565 m_cu->line_header_die_owner = NULL;
10566 }
10567 }
10568
10569private:
10570 die_info *m_die;
10571 dwarf2_cu *m_cu;
10572};
adde2bff 10573
c906108c
SS
10574/* Process a die and its children. */
10575
10576static void
e7c27a73 10577process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10578{
4c8aa72d 10579 process_die_scope scope (die, cu);
adde2bff 10580
c906108c
SS
10581 switch (die->tag)
10582 {
10583 case DW_TAG_padding:
10584 break;
10585 case DW_TAG_compile_unit:
95554aad 10586 case DW_TAG_partial_unit:
e7c27a73 10587 read_file_scope (die, cu);
c906108c 10588 break;
348e048f
DE
10589 case DW_TAG_type_unit:
10590 read_type_unit_scope (die, cu);
10591 break;
c906108c 10592 case DW_TAG_subprogram:
0a4b0913
AB
10593 /* Nested subprograms in Fortran get a prefix. */
10594 if (cu->language == language_fortran
10595 && die->parent != NULL
10596 && die->parent->tag == DW_TAG_subprogram)
10597 cu->processing_has_namespace_info = true;
10598 /* Fall through. */
c906108c 10599 case DW_TAG_inlined_subroutine:
edb3359d 10600 read_func_scope (die, cu);
c906108c
SS
10601 break;
10602 case DW_TAG_lexical_block:
14898363
L
10603 case DW_TAG_try_block:
10604 case DW_TAG_catch_block:
e7c27a73 10605 read_lexical_block_scope (die, cu);
c906108c 10606 break;
216f72a1 10607 case DW_TAG_call_site:
96408a79
SA
10608 case DW_TAG_GNU_call_site:
10609 read_call_site_scope (die, cu);
10610 break;
c906108c 10611 case DW_TAG_class_type:
680b30c7 10612 case DW_TAG_interface_type:
c906108c
SS
10613 case DW_TAG_structure_type:
10614 case DW_TAG_union_type:
134d01f1 10615 process_structure_scope (die, cu);
c906108c
SS
10616 break;
10617 case DW_TAG_enumeration_type:
134d01f1 10618 process_enumeration_scope (die, cu);
c906108c 10619 break;
134d01f1 10620
f792889a
DJ
10621 /* These dies have a type, but processing them does not create
10622 a symbol or recurse to process the children. Therefore we can
10623 read them on-demand through read_type_die. */
c906108c 10624 case DW_TAG_subroutine_type:
72019c9c 10625 case DW_TAG_set_type:
c906108c 10626 case DW_TAG_array_type:
c906108c 10627 case DW_TAG_pointer_type:
c906108c 10628 case DW_TAG_ptr_to_member_type:
c906108c 10629 case DW_TAG_reference_type:
4297a3f0 10630 case DW_TAG_rvalue_reference_type:
c906108c 10631 case DW_TAG_string_type:
c906108c 10632 break;
134d01f1 10633
c906108c 10634 case DW_TAG_base_type:
a02abb62 10635 case DW_TAG_subrange_type:
cb249c71 10636 case DW_TAG_typedef:
134d01f1
DJ
10637 /* Add a typedef symbol for the type definition, if it has a
10638 DW_AT_name. */
f792889a 10639 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10640 break;
c906108c 10641 case DW_TAG_common_block:
e7c27a73 10642 read_common_block (die, cu);
c906108c
SS
10643 break;
10644 case DW_TAG_common_inclusion:
10645 break;
d9fa45fe 10646 case DW_TAG_namespace:
9068261f 10647 cu->processing_has_namespace_info = true;
e7c27a73 10648 read_namespace (die, cu);
d9fa45fe 10649 break;
5d7cb8df 10650 case DW_TAG_module:
9068261f 10651 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10652 read_module (die, cu);
10653 break;
d9fa45fe 10654 case DW_TAG_imported_declaration:
9068261f 10655 cu->processing_has_namespace_info = true;
74921315
KS
10656 if (read_namespace_alias (die, cu))
10657 break;
86a73007
TT
10658 /* The declaration is not a global namespace alias. */
10659 /* Fall through. */
d9fa45fe 10660 case DW_TAG_imported_module:
9068261f 10661 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10662 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10663 || cu->language != language_fortran))
b98664d3 10664 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10665 dwarf_tag_name (die->tag));
10666 read_import_statement (die, cu);
d9fa45fe 10667 break;
95554aad
TT
10668
10669 case DW_TAG_imported_unit:
10670 process_imported_unit_die (die, cu);
10671 break;
10672
71a3c369
TT
10673 case DW_TAG_variable:
10674 read_variable (die, cu);
10675 break;
10676
c906108c 10677 default:
e7c27a73 10678 new_symbol (die, NULL, cu);
c906108c
SS
10679 break;
10680 }
10681}
ca69b9e6
DE
10682\f
10683/* DWARF name computation. */
c906108c 10684
94af9270
KS
10685/* A helper function for dwarf2_compute_name which determines whether DIE
10686 needs to have the name of the scope prepended to the name listed in the
10687 die. */
10688
10689static int
10690die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10691{
1c809c68
TT
10692 struct attribute *attr;
10693
94af9270
KS
10694 switch (die->tag)
10695 {
10696 case DW_TAG_namespace:
10697 case DW_TAG_typedef:
10698 case DW_TAG_class_type:
10699 case DW_TAG_interface_type:
10700 case DW_TAG_structure_type:
10701 case DW_TAG_union_type:
10702 case DW_TAG_enumeration_type:
10703 case DW_TAG_enumerator:
10704 case DW_TAG_subprogram:
08a76f8a 10705 case DW_TAG_inlined_subroutine:
94af9270 10706 case DW_TAG_member:
74921315 10707 case DW_TAG_imported_declaration:
94af9270
KS
10708 return 1;
10709
10710 case DW_TAG_variable:
c2b0a229 10711 case DW_TAG_constant:
94af9270
KS
10712 /* We only need to prefix "globally" visible variables. These include
10713 any variable marked with DW_AT_external or any variable that
10714 lives in a namespace. [Variables in anonymous namespaces
10715 require prefixing, but they are not DW_AT_external.] */
10716
10717 if (dwarf2_attr (die, DW_AT_specification, cu))
10718 {
10719 struct dwarf2_cu *spec_cu = cu;
9a619af0 10720
94af9270
KS
10721 return die_needs_namespace (die_specification (die, &spec_cu),
10722 spec_cu);
10723 }
10724
1c809c68 10725 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10726 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10727 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10728 return 0;
10729 /* A variable in a lexical block of some kind does not need a
10730 namespace, even though in C++ such variables may be external
10731 and have a mangled name. */
10732 if (die->parent->tag == DW_TAG_lexical_block
10733 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10734 || die->parent->tag == DW_TAG_catch_block
10735 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10736 return 0;
10737 return 1;
94af9270
KS
10738
10739 default:
10740 return 0;
10741 }
10742}
10743
73b9be8b
KS
10744/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10745 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10746 defined for the given DIE. */
10747
10748static struct attribute *
10749dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10750{
10751 struct attribute *attr;
10752
10753 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10754 if (attr == NULL)
10755 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10756
10757 return attr;
10758}
10759
10760/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10761 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10762 defined for the given DIE. */
10763
10764static const char *
10765dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10766{
10767 const char *linkage_name;
10768
10769 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10770 if (linkage_name == NULL)
10771 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10772
10773 return linkage_name;
10774}
10775
94af9270 10776/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10777 compute the physname for the object, which include a method's:
9c37b5ae 10778 - formal parameters (C++),
a766d390 10779 - receiver type (Go),
a766d390
DE
10780
10781 The term "physname" is a bit confusing.
10782 For C++, for example, it is the demangled name.
10783 For Go, for example, it's the mangled name.
94af9270 10784
af6b7be1
JB
10785 For Ada, return the DIE's linkage name rather than the fully qualified
10786 name. PHYSNAME is ignored..
10787
94af9270
KS
10788 The result is allocated on the objfile_obstack and canonicalized. */
10789
10790static const char *
15d034d0
TT
10791dwarf2_compute_name (const char *name,
10792 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10793 int physname)
10794{
518817b3 10795 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10796
94af9270
KS
10797 if (name == NULL)
10798 name = dwarf2_name (die, cu);
10799
2ee7123e
DE
10800 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10801 but otherwise compute it by typename_concat inside GDB.
10802 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10803 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10804 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10805 will set the demangled name to the result of dwarf2_full_name, and it is
10806 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10807 if (cu->language == language_ada
10808 || (cu->language == language_fortran && physname))
10809 {
10810 /* For Ada unit, we prefer the linkage name over the name, as
10811 the former contains the exported name, which the user expects
10812 to be able to reference. Ideally, we want the user to be able
10813 to reference this entity using either natural or linkage name,
10814 but we haven't started looking at this enhancement yet. */
73b9be8b 10815 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10816
2ee7123e
DE
10817 if (linkage_name != NULL)
10818 return linkage_name;
f55ee35c
JK
10819 }
10820
94af9270
KS
10821 /* These are the only languages we know how to qualify names in. */
10822 if (name != NULL
9c37b5ae 10823 && (cu->language == language_cplus
c44af4eb
TT
10824 || cu->language == language_fortran || cu->language == language_d
10825 || cu->language == language_rust))
94af9270
KS
10826 {
10827 if (die_needs_namespace (die, cu))
10828 {
0d5cff50 10829 const char *prefix;
34a68019 10830 const char *canonical_name = NULL;
94af9270 10831
d7e74731
PA
10832 string_file buf;
10833
94af9270 10834 prefix = determine_prefix (die, cu);
94af9270
KS
10835 if (*prefix != '\0')
10836 {
43816ebc
TT
10837 gdb::unique_xmalloc_ptr<char> prefixed_name
10838 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10839
43816ebc 10840 buf.puts (prefixed_name.get ());
94af9270
KS
10841 }
10842 else
d7e74731 10843 buf.puts (name);
94af9270 10844
98bfdba5
PA
10845 /* Template parameters may be specified in the DIE's DW_AT_name, or
10846 as children with DW_TAG_template_type_param or
10847 DW_TAG_value_type_param. If the latter, add them to the name
10848 here. If the name already has template parameters, then
10849 skip this step; some versions of GCC emit both, and
10850 it is more efficient to use the pre-computed name.
10851
10852 Something to keep in mind about this process: it is very
10853 unlikely, or in some cases downright impossible, to produce
10854 something that will match the mangled name of a function.
10855 If the definition of the function has the same debug info,
10856 we should be able to match up with it anyway. But fallbacks
10857 using the minimal symbol, for instance to find a method
10858 implemented in a stripped copy of libstdc++, will not work.
10859 If we do not have debug info for the definition, we will have to
10860 match them up some other way.
10861
10862 When we do name matching there is a related problem with function
10863 templates; two instantiated function templates are allowed to
10864 differ only by their return types, which we do not add here. */
10865
10866 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10867 {
10868 struct attribute *attr;
10869 struct die_info *child;
10870 int first = 1;
10871
10872 die->building_fullname = 1;
10873
10874 for (child = die->child; child != NULL; child = child->sibling)
10875 {
10876 struct type *type;
12df843f 10877 LONGEST value;
d521ce57 10878 const gdb_byte *bytes;
98bfdba5
PA
10879 struct dwarf2_locexpr_baton *baton;
10880 struct value *v;
10881
10882 if (child->tag != DW_TAG_template_type_param
10883 && child->tag != DW_TAG_template_value_param)
10884 continue;
10885
10886 if (first)
10887 {
d7e74731 10888 buf.puts ("<");
98bfdba5
PA
10889 first = 0;
10890 }
10891 else
d7e74731 10892 buf.puts (", ");
98bfdba5
PA
10893
10894 attr = dwarf2_attr (child, DW_AT_type, cu);
10895 if (attr == NULL)
10896 {
b98664d3 10897 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10898 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10899 continue;
10900 }
10901 type = die_type (child, cu);
10902
10903 if (child->tag == DW_TAG_template_type_param)
10904 {
c1ec8cea
TT
10905 c_print_type (type, "", &buf, -1, 0, cu->language,
10906 &type_print_raw_options);
98bfdba5
PA
10907 continue;
10908 }
10909
10910 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10911 if (attr == NULL)
10912 {
b98664d3 10913 complaint (_("template parameter missing "
3e43a32a 10914 "DW_AT_const_value"));
d7e74731 10915 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10916 continue;
10917 }
10918
10919 dwarf2_const_value_attr (attr, type, name,
10920 &cu->comp_unit_obstack, cu,
10921 &value, &bytes, &baton);
10922
10923 if (TYPE_NOSIGN (type))
10924 /* GDB prints characters as NUMBER 'CHAR'. If that's
10925 changed, this can use value_print instead. */
d7e74731 10926 c_printchar (value, type, &buf);
98bfdba5
PA
10927 else
10928 {
10929 struct value_print_options opts;
10930
10931 if (baton != NULL)
10932 v = dwarf2_evaluate_loc_desc (type, NULL,
10933 baton->data,
10934 baton->size,
10935 baton->per_cu);
10936 else if (bytes != NULL)
10937 {
10938 v = allocate_value (type);
10939 memcpy (value_contents_writeable (v), bytes,
10940 TYPE_LENGTH (type));
10941 }
10942 else
10943 v = value_from_longest (type, value);
10944
3e43a32a
MS
10945 /* Specify decimal so that we do not depend on
10946 the radix. */
98bfdba5
PA
10947 get_formatted_print_options (&opts, 'd');
10948 opts.raw = 1;
d7e74731 10949 value_print (v, &buf, &opts);
98bfdba5 10950 release_value (v);
98bfdba5
PA
10951 }
10952 }
10953
10954 die->building_fullname = 0;
10955
10956 if (!first)
10957 {
10958 /* Close the argument list, with a space if necessary
10959 (nested templates). */
d7e74731
PA
10960 if (!buf.empty () && buf.string ().back () == '>')
10961 buf.puts (" >");
98bfdba5 10962 else
d7e74731 10963 buf.puts (">");
98bfdba5
PA
10964 }
10965 }
10966
9c37b5ae 10967 /* For C++ methods, append formal parameter type
94af9270 10968 information, if PHYSNAME. */
6e70227d 10969
94af9270 10970 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10971 && cu->language == language_cplus)
94af9270
KS
10972 {
10973 struct type *type = read_type_die (die, cu);
10974
d7e74731 10975 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10976 &type_print_raw_options);
94af9270 10977
9c37b5ae 10978 if (cu->language == language_cplus)
94af9270 10979 {
60430eff
DJ
10980 /* Assume that an artificial first parameter is
10981 "this", but do not crash if it is not. RealView
10982 marks unnamed (and thus unused) parameters as
10983 artificial; there is no way to differentiate
10984 the two cases. */
94af9270
KS
10985 if (TYPE_NFIELDS (type) > 0
10986 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10987 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10988 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10989 0))))
d7e74731 10990 buf.puts (" const");
94af9270
KS
10991 }
10992 }
10993
d7e74731 10994 const std::string &intermediate_name = buf.string ();
94af9270
KS
10995
10996 if (cu->language == language_cplus)
34a68019 10997 canonical_name
322a8516 10998 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10999 &objfile->per_bfd->storage_obstack);
11000
11001 /* If we only computed INTERMEDIATE_NAME, or if
11002 INTERMEDIATE_NAME is already canonical, then we need to
11003 copy it to the appropriate obstack. */
322a8516 11004 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11005 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11006 intermediate_name);
34a68019
TT
11007 else
11008 name = canonical_name;
94af9270
KS
11009 }
11010 }
11011
11012 return name;
11013}
11014
0114d602
DJ
11015/* Return the fully qualified name of DIE, based on its DW_AT_name.
11016 If scope qualifiers are appropriate they will be added. The result
34a68019 11017 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11018 not have a name. NAME may either be from a previous call to
11019 dwarf2_name or NULL.
11020
9c37b5ae 11021 The output string will be canonicalized (if C++). */
0114d602
DJ
11022
11023static const char *
15d034d0 11024dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11025{
94af9270
KS
11026 return dwarf2_compute_name (name, die, cu, 0);
11027}
0114d602 11028
94af9270
KS
11029/* Construct a physname for the given DIE in CU. NAME may either be
11030 from a previous call to dwarf2_name or NULL. The result will be
11031 allocated on the objfile_objstack or NULL if the DIE does not have a
11032 name.
0114d602 11033
9c37b5ae 11034 The output string will be canonicalized (if C++). */
0114d602 11035
94af9270 11036static const char *
15d034d0 11037dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11038{
518817b3 11039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11040 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11041 int need_copy = 1;
11042
11043 /* In this case dwarf2_compute_name is just a shortcut not building anything
11044 on its own. */
11045 if (!die_needs_namespace (die, cu))
11046 return dwarf2_compute_name (name, die, cu, 1);
11047
73b9be8b 11048 mangled = dw2_linkage_name (die, cu);
900e11f9 11049
e98c9e7c
TT
11050 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11051 See https://github.com/rust-lang/rust/issues/32925. */
11052 if (cu->language == language_rust && mangled != NULL
11053 && strchr (mangled, '{') != NULL)
11054 mangled = NULL;
11055
900e11f9
JK
11056 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11057 has computed. */
791afaa2 11058 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11059 if (mangled != NULL)
900e11f9 11060 {
900e11f9 11061
59cc4834
JB
11062 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11063 {
11064 /* Do nothing (do not demangle the symbol name). */
11065 }
11066 else if (cu->language == language_go)
a766d390 11067 {
5e2db402
TT
11068 /* This is a lie, but we already lie to the caller new_symbol.
11069 new_symbol assumes we return the mangled name.
a766d390 11070 This just undoes that lie until things are cleaned up. */
a766d390
DE
11071 }
11072 else
11073 {
0eb876f5
JB
11074 /* Use DMGL_RET_DROP for C++ template functions to suppress
11075 their return type. It is easier for GDB users to search
11076 for such functions as `name(params)' than `long name(params)'.
11077 In such case the minimal symbol names do not match the full
11078 symbol names but for template functions there is never a need
11079 to look up their definition from their declaration so
11080 the only disadvantage remains the minimal symbol variant
11081 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11082 demangled.reset (gdb_demangle (mangled,
11083 (DMGL_PARAMS | DMGL_ANSI
11084 | DMGL_RET_DROP)));
a766d390 11085 }
900e11f9 11086 if (demangled)
791afaa2 11087 canon = demangled.get ();
900e11f9
JK
11088 else
11089 {
11090 canon = mangled;
11091 need_copy = 0;
11092 }
11093 }
11094
11095 if (canon == NULL || check_physname)
11096 {
11097 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11098
11099 if (canon != NULL && strcmp (physname, canon) != 0)
11100 {
11101 /* It may not mean a bug in GDB. The compiler could also
11102 compute DW_AT_linkage_name incorrectly. But in such case
11103 GDB would need to be bug-to-bug compatible. */
11104
b98664d3 11105 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11106 "(from linkage <%s>) - DIE at %s [in module %s]"),
11107 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11108 objfile_name (objfile));
900e11f9
JK
11109
11110 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11111 is available here - over computed PHYSNAME. It is safer
11112 against both buggy GDB and buggy compilers. */
11113
11114 retval = canon;
11115 }
11116 else
11117 {
11118 retval = physname;
11119 need_copy = 0;
11120 }
11121 }
11122 else
11123 retval = canon;
11124
11125 if (need_copy)
021887d8 11126 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11127
900e11f9 11128 return retval;
0114d602
DJ
11129}
11130
74921315
KS
11131/* Inspect DIE in CU for a namespace alias. If one exists, record
11132 a new symbol for it.
11133
11134 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11135
11136static int
11137read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11138{
11139 struct attribute *attr;
11140
11141 /* If the die does not have a name, this is not a namespace
11142 alias. */
11143 attr = dwarf2_attr (die, DW_AT_name, cu);
11144 if (attr != NULL)
11145 {
11146 int num;
11147 struct die_info *d = die;
11148 struct dwarf2_cu *imported_cu = cu;
11149
11150 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11151 keep inspecting DIEs until we hit the underlying import. */
11152#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11153 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11154 {
11155 attr = dwarf2_attr (d, DW_AT_import, cu);
11156 if (attr == NULL)
11157 break;
11158
11159 d = follow_die_ref (d, attr, &imported_cu);
11160 if (d->tag != DW_TAG_imported_declaration)
11161 break;
11162 }
11163
11164 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11165 {
b98664d3 11166 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11167 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11168 return 0;
11169 }
11170
11171 if (attr != NULL)
11172 {
11173 struct type *type;
9c541725 11174 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11175
9c541725 11176 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11177 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11178 {
11179 /* This declaration is a global namespace alias. Add
11180 a symbol for it whose type is the aliased namespace. */
11181 new_symbol (die, type, cu);
11182 return 1;
11183 }
11184 }
11185 }
11186
11187 return 0;
11188}
11189
22cee43f 11190/* Return the using directives repository (global or local?) to use in the
804d2729 11191 current context for CU.
22cee43f
PMR
11192
11193 For Ada, imported declarations can materialize renamings, which *may* be
11194 global. However it is impossible (for now?) in DWARF to distinguish
11195 "external" imported declarations and "static" ones. As all imported
11196 declarations seem to be static in all other languages, make them all CU-wide
11197 global only in Ada. */
11198
11199static struct using_direct **
804d2729 11200using_directives (struct dwarf2_cu *cu)
22cee43f 11201{
c24bdb02
KS
11202 if (cu->language == language_ada
11203 && cu->get_builder ()->outermost_context_p ())
11204 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11205 else
c24bdb02 11206 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11207}
11208
27aa8d6a
SW
11209/* Read the import statement specified by the given die and record it. */
11210
11211static void
11212read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11213{
518817b3 11214 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11215 struct attribute *import_attr;
32019081 11216 struct die_info *imported_die, *child_die;
de4affc9 11217 struct dwarf2_cu *imported_cu;
27aa8d6a 11218 const char *imported_name;
794684b6 11219 const char *imported_name_prefix;
13387711
SW
11220 const char *canonical_name;
11221 const char *import_alias;
11222 const char *imported_declaration = NULL;
794684b6 11223 const char *import_prefix;
eb1e02fd 11224 std::vector<const char *> excludes;
13387711 11225
27aa8d6a
SW
11226 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11227 if (import_attr == NULL)
11228 {
b98664d3 11229 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11230 dwarf_tag_name (die->tag));
11231 return;
11232 }
11233
de4affc9
CC
11234 imported_cu = cu;
11235 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11236 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11237 if (imported_name == NULL)
11238 {
11239 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11240
11241 The import in the following code:
11242 namespace A
11243 {
11244 typedef int B;
11245 }
11246
11247 int main ()
11248 {
11249 using A::B;
11250 B b;
11251 return b;
11252 }
11253
11254 ...
11255 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11256 <52> DW_AT_decl_file : 1
11257 <53> DW_AT_decl_line : 6
11258 <54> DW_AT_import : <0x75>
11259 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11260 <59> DW_AT_name : B
11261 <5b> DW_AT_decl_file : 1
11262 <5c> DW_AT_decl_line : 2
11263 <5d> DW_AT_type : <0x6e>
11264 ...
11265 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11266 <76> DW_AT_byte_size : 4
11267 <77> DW_AT_encoding : 5 (signed)
11268
11269 imports the wrong die ( 0x75 instead of 0x58 ).
11270 This case will be ignored until the gcc bug is fixed. */
11271 return;
11272 }
11273
82856980
SW
11274 /* Figure out the local name after import. */
11275 import_alias = dwarf2_name (die, cu);
27aa8d6a 11276
794684b6
SW
11277 /* Figure out where the statement is being imported to. */
11278 import_prefix = determine_prefix (die, cu);
11279
11280 /* Figure out what the scope of the imported die is and prepend it
11281 to the name of the imported die. */
de4affc9 11282 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11283
f55ee35c
JK
11284 if (imported_die->tag != DW_TAG_namespace
11285 && imported_die->tag != DW_TAG_module)
794684b6 11286 {
13387711
SW
11287 imported_declaration = imported_name;
11288 canonical_name = imported_name_prefix;
794684b6 11289 }
13387711 11290 else if (strlen (imported_name_prefix) > 0)
12aaed36 11291 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11292 imported_name_prefix,
11293 (cu->language == language_d ? "." : "::"),
11294 imported_name, (char *) NULL);
13387711
SW
11295 else
11296 canonical_name = imported_name;
794684b6 11297
32019081
JK
11298 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11299 for (child_die = die->child; child_die && child_die->tag;
11300 child_die = sibling_die (child_die))
11301 {
11302 /* DWARF-4: A Fortran use statement with a “rename list” may be
11303 represented by an imported module entry with an import attribute
11304 referring to the module and owned entries corresponding to those
11305 entities that are renamed as part of being imported. */
11306
11307 if (child_die->tag != DW_TAG_imported_declaration)
11308 {
b98664d3 11309 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11310 "- DIE at %s [in module %s]"),
11311 sect_offset_str (child_die->sect_off),
11312 objfile_name (objfile));
32019081
JK
11313 continue;
11314 }
11315
11316 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11317 if (import_attr == NULL)
11318 {
b98664d3 11319 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11320 dwarf_tag_name (child_die->tag));
11321 continue;
11322 }
11323
11324 imported_cu = cu;
11325 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11326 &imported_cu);
11327 imported_name = dwarf2_name (imported_die, imported_cu);
11328 if (imported_name == NULL)
11329 {
b98664d3 11330 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11331 "imported name - DIE at %s [in module %s]"),
11332 sect_offset_str (child_die->sect_off),
11333 objfile_name (objfile));
32019081
JK
11334 continue;
11335 }
11336
eb1e02fd 11337 excludes.push_back (imported_name);
32019081
JK
11338
11339 process_die (child_die, cu);
11340 }
11341
804d2729 11342 add_using_directive (using_directives (cu),
22cee43f
PMR
11343 import_prefix,
11344 canonical_name,
11345 import_alias,
11346 imported_declaration,
11347 excludes,
11348 0,
11349 &objfile->objfile_obstack);
27aa8d6a
SW
11350}
11351
5230b05a
WT
11352/* ICC<14 does not output the required DW_AT_declaration on incomplete
11353 types, but gives them a size of zero. Starting with version 14,
11354 ICC is compatible with GCC. */
11355
9068261f 11356static bool
5230b05a
WT
11357producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11358{
11359 if (!cu->checked_producer)
11360 check_producer (cu);
11361
11362 return cu->producer_is_icc_lt_14;
11363}
11364
eb77c9df
AB
11365/* ICC generates a DW_AT_type for C void functions. This was observed on
11366 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11367 which says that void functions should not have a DW_AT_type. */
11368
11369static bool
11370producer_is_icc (struct dwarf2_cu *cu)
11371{
11372 if (!cu->checked_producer)
11373 check_producer (cu);
11374
11375 return cu->producer_is_icc;
11376}
11377
1b80a9fa
JK
11378/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11379 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11380 this, it was first present in GCC release 4.3.0. */
11381
9068261f 11382static bool
1b80a9fa
JK
11383producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11384{
11385 if (!cu->checked_producer)
11386 check_producer (cu);
11387
11388 return cu->producer_is_gcc_lt_4_3;
11389}
11390
d721ba37
PA
11391static file_and_directory
11392find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11393{
d721ba37
PA
11394 file_and_directory res;
11395
9291a0cd
TT
11396 /* Find the filename. Do not use dwarf2_name here, since the filename
11397 is not a source language identifier. */
d721ba37
PA
11398 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11399 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11400
d721ba37
PA
11401 if (res.comp_dir == NULL
11402 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11403 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11404 {
d721ba37
PA
11405 res.comp_dir_storage = ldirname (res.name);
11406 if (!res.comp_dir_storage.empty ())
11407 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11408 }
d721ba37 11409 if (res.comp_dir != NULL)
9291a0cd
TT
11410 {
11411 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11412 directory, get rid of it. */
d721ba37 11413 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11414
d721ba37
PA
11415 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11416 res.comp_dir = cp + 1;
9291a0cd
TT
11417 }
11418
d721ba37
PA
11419 if (res.name == NULL)
11420 res.name = "<unknown>";
11421
11422 return res;
9291a0cd
TT
11423}
11424
f4dc4d17
DE
11425/* Handle DW_AT_stmt_list for a compilation unit.
11426 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11427 COMP_DIR is the compilation directory. LOWPC is passed to
11428 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11429
11430static void
11431handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11432 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11433{
518817b3
SM
11434 struct dwarf2_per_objfile *dwarf2_per_objfile
11435 = cu->per_cu->dwarf2_per_objfile;
527f3840 11436 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11437 struct attribute *attr;
527f3840
JK
11438 struct line_header line_header_local;
11439 hashval_t line_header_local_hash;
527f3840
JK
11440 void **slot;
11441 int decode_mapping;
2ab95328 11442
f4dc4d17
DE
11443 gdb_assert (! cu->per_cu->is_debug_types);
11444
2ab95328 11445 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11446 if (attr == NULL)
11447 return;
11448
9c541725 11449 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11450
11451 /* The line header hash table is only created if needed (it exists to
11452 prevent redundant reading of the line table for partial_units).
11453 If we're given a partial_unit, we'll need it. If we're given a
11454 compile_unit, then use the line header hash table if it's already
11455 created, but don't create one just yet. */
11456
11457 if (dwarf2_per_objfile->line_header_hash == NULL
11458 && die->tag == DW_TAG_partial_unit)
2ab95328 11459 {
527f3840
JK
11460 dwarf2_per_objfile->line_header_hash
11461 = htab_create_alloc_ex (127, line_header_hash_voidp,
11462 line_header_eq_voidp,
11463 free_line_header_voidp,
11464 &objfile->objfile_obstack,
11465 hashtab_obstack_allocate,
11466 dummy_obstack_deallocate);
11467 }
2ab95328 11468
9c541725 11469 line_header_local.sect_off = line_offset;
527f3840
JK
11470 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11471 line_header_local_hash = line_header_hash (&line_header_local);
11472 if (dwarf2_per_objfile->line_header_hash != NULL)
11473 {
11474 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11475 &line_header_local,
11476 line_header_local_hash, NO_INSERT);
11477
11478 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11479 is not present in *SLOT (since if there is something in *SLOT then
11480 it will be for a partial_unit). */
11481 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11482 {
527f3840 11483 gdb_assert (*slot != NULL);
9a3c8263 11484 cu->line_header = (struct line_header *) *slot;
527f3840 11485 return;
dee91e82 11486 }
2ab95328 11487 }
527f3840
JK
11488
11489 /* dwarf_decode_line_header does not yet provide sufficient information.
11490 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11491 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11492 if (lh == NULL)
527f3840 11493 return;
4c8aa72d
PA
11494
11495 cu->line_header = lh.release ();
11496 cu->line_header_die_owner = die;
527f3840
JK
11497
11498 if (dwarf2_per_objfile->line_header_hash == NULL)
11499 slot = NULL;
11500 else
11501 {
11502 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11503 &line_header_local,
11504 line_header_local_hash, INSERT);
11505 gdb_assert (slot != NULL);
11506 }
11507 if (slot != NULL && *slot == NULL)
11508 {
11509 /* This newly decoded line number information unit will be owned
11510 by line_header_hash hash table. */
11511 *slot = cu->line_header;
4c8aa72d 11512 cu->line_header_die_owner = NULL;
527f3840
JK
11513 }
11514 else
11515 {
11516 /* We cannot free any current entry in (*slot) as that struct line_header
11517 may be already used by multiple CUs. Create only temporary decoded
11518 line_header for this CU - it may happen at most once for each line
11519 number information unit. And if we're not using line_header_hash
11520 then this is what we want as well. */
11521 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11522 }
11523 decode_mapping = (die->tag != DW_TAG_partial_unit);
11524 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11525 decode_mapping);
fff8551c 11526
2ab95328
TT
11527}
11528
95554aad 11529/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11530
c906108c 11531static void
e7c27a73 11532read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11533{
518817b3
SM
11534 struct dwarf2_per_objfile *dwarf2_per_objfile
11535 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11536 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11538 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11539 CORE_ADDR highpc = ((CORE_ADDR) 0);
11540 struct attribute *attr;
c906108c 11541 struct die_info *child_die;
e142c38c 11542 CORE_ADDR baseaddr;
6e70227d 11543
380618d6 11544 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11545 baseaddr = objfile->text_section_offset ();
c906108c 11546
fae299cd 11547 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11548
11549 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11550 from finish_block. */
2acceee2 11551 if (lowpc == ((CORE_ADDR) -1))
c906108c 11552 lowpc = highpc;
3e29f34a 11553 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11554
d721ba37 11555 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11556
f4b8a18d
KW
11557 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11558 standardised yet. As a workaround for the language detection we fall
11559 back to the DW_AT_producer string. */
11560 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11561 cu->language = language_opencl;
11562
3019eac3
DE
11563 /* Similar hack for Go. */
11564 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11565 set_cu_language (DW_LANG_Go, cu);
11566
c24bdb02 11567 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11568
11569 /* Decode line number information if present. We do this before
11570 processing child DIEs, so that the line header table is available
11571 for DW_AT_decl_file. */
d721ba37 11572 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11573
11574 /* Process all dies in compilation unit. */
11575 if (die->child != NULL)
11576 {
11577 child_die = die->child;
11578 while (child_die && child_die->tag)
11579 {
11580 process_die (child_die, cu);
11581 child_die = sibling_die (child_die);
11582 }
11583 }
11584
11585 /* Decode macro information, if present. Dwarf 2 macro information
11586 refers to information in the line number info statement program
11587 header, so we can only read it if we've read the header
11588 successfully. */
0af92d60
JK
11589 attr = dwarf2_attr (die, DW_AT_macros, cu);
11590 if (attr == NULL)
11591 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11592 if (attr && cu->line_header)
11593 {
11594 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11595 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11596
43f3e411 11597 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11598 }
11599 else
11600 {
11601 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11602 if (attr && cu->line_header)
11603 {
11604 unsigned int macro_offset = DW_UNSND (attr);
11605
43f3e411 11606 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11607 }
11608 }
3019eac3
DE
11609}
11610
c24bdb02
KS
11611void
11612dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11613{
f4dc4d17
DE
11614 struct type_unit_group *tu_group;
11615 int first_time;
3019eac3 11616 struct attribute *attr;
9c541725 11617 unsigned int i;
0186c6a7 11618 struct signatured_type *sig_type;
3019eac3 11619
f4dc4d17 11620 gdb_assert (per_cu->is_debug_types);
0186c6a7 11621 sig_type = (struct signatured_type *) per_cu;
3019eac3 11622
c24bdb02 11623 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11624
f4dc4d17 11625 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11626 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11627 if (sig_type->type_unit_group == NULL)
c24bdb02 11628 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11629 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11630
11631 /* If we've already processed this stmt_list there's no real need to
11632 do it again, we could fake it and just recreate the part we need
11633 (file name,index -> symtab mapping). If data shows this optimization
11634 is useful we can do it then. */
43f3e411 11635 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11636
11637 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11638 debug info. */
fff8551c 11639 line_header_up lh;
f4dc4d17 11640 if (attr != NULL)
3019eac3 11641 {
9c541725 11642 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11643 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11644 }
11645 if (lh == NULL)
11646 {
11647 if (first_time)
c24bdb02 11648 start_symtab ("", NULL, 0);
f4dc4d17
DE
11649 else
11650 {
11651 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11652 gdb_assert (m_builder == nullptr);
804d2729 11653 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11654 m_builder.reset (new struct buildsym_compunit
11655 (COMPUNIT_OBJFILE (cust), "",
11656 COMPUNIT_DIRNAME (cust),
11657 compunit_language (cust),
11658 0, cust));
f4dc4d17 11659 }
f4dc4d17 11660 return;
3019eac3
DE
11661 }
11662
c24bdb02
KS
11663 line_header = lh.release ();
11664 line_header_die_owner = die;
3019eac3 11665
f4dc4d17
DE
11666 if (first_time)
11667 {
c24bdb02 11668 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11669
1fd60fc0
DE
11670 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11671 still initializing it, and our caller (a few levels up)
11672 process_full_type_unit still needs to know if this is the first
11673 time. */
11674
7ba99d21 11675 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11676 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11677 line_header->file_names_size ());
3019eac3 11678
7ba99d21
AT
11679 auto &file_names = line_header->file_names ();
11680 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11681 {
7ba99d21 11682 file_entry &fe = file_names[i];
c24bdb02
KS
11683 dwarf2_start_subfile (this, fe.name,
11684 fe.include_dir (line_header));
11685 buildsym_compunit *b = get_builder ();
11686 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11687 {
4c8aa72d
PA
11688 /* NOTE: start_subfile will recognize when it's been
11689 passed a file it has already seen. So we can't
11690 assume there's a simple mapping from
11691 cu->line_header->file_names to subfiles, plus
11692 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11693 b->get_current_subfile ()->symtab
11694 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11695 }
11696
c24bdb02 11697 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11698 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11699 }
11700 }
11701 else
3019eac3 11702 {
c24bdb02 11703 gdb_assert (m_builder == nullptr);
804d2729 11704 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11705 m_builder.reset (new struct buildsym_compunit
11706 (COMPUNIT_OBJFILE (cust), "",
11707 COMPUNIT_DIRNAME (cust),
11708 compunit_language (cust),
11709 0, cust));
f4dc4d17 11710
7ba99d21
AT
11711 auto &file_names = line_header->file_names ();
11712 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11713 {
7ba99d21 11714 file_entry &fe = file_names[i];
4c8aa72d 11715 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11716 }
3019eac3
DE
11717 }
11718
f4dc4d17
DE
11719 /* The main symtab is allocated last. Type units don't have DW_AT_name
11720 so they don't have a "real" (so to speak) symtab anyway.
11721 There is later code that will assign the main symtab to all symbols
11722 that don't have one. We need to handle the case of a symbol with a
11723 missing symtab (DW_AT_decl_file) anyway. */
11724}
3019eac3 11725
f4dc4d17
DE
11726/* Process DW_TAG_type_unit.
11727 For TUs we want to skip the first top level sibling if it's not the
11728 actual type being defined by this TU. In this case the first top
11729 level sibling is there to provide context only. */
3019eac3 11730
f4dc4d17
DE
11731static void
11732read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11733{
11734 struct die_info *child_die;
3019eac3 11735
f4dc4d17
DE
11736 prepare_one_comp_unit (cu, die, language_minimal);
11737
11738 /* Initialize (or reinitialize) the machinery for building symtabs.
11739 We do this before processing child DIEs, so that the line header table
11740 is available for DW_AT_decl_file. */
c24bdb02 11741 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11742
11743 if (die->child != NULL)
11744 {
11745 child_die = die->child;
11746 while (child_die && child_die->tag)
11747 {
11748 process_die (child_die, cu);
11749 child_die = sibling_die (child_die);
11750 }
11751 }
3019eac3
DE
11752}
11753\f
80626a55
DE
11754/* DWO/DWP files.
11755
11756 http://gcc.gnu.org/wiki/DebugFission
11757 http://gcc.gnu.org/wiki/DebugFissionDWP
11758
11759 To simplify handling of both DWO files ("object" files with the DWARF info)
11760 and DWP files (a file with the DWOs packaged up into one file), we treat
11761 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11762
11763static hashval_t
11764hash_dwo_file (const void *item)
11765{
9a3c8263 11766 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11767 hashval_t hash;
3019eac3 11768
a2ce51a0
DE
11769 hash = htab_hash_string (dwo_file->dwo_name);
11770 if (dwo_file->comp_dir != NULL)
11771 hash += htab_hash_string (dwo_file->comp_dir);
11772 return hash;
3019eac3
DE
11773}
11774
11775static int
11776eq_dwo_file (const void *item_lhs, const void *item_rhs)
11777{
9a3c8263
SM
11778 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11779 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11780
a2ce51a0
DE
11781 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11782 return 0;
11783 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11784 return lhs->comp_dir == rhs->comp_dir;
11785 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11786}
11787
11788/* Allocate a hash table for DWO files. */
11789
51ac9db5 11790static htab_up
ed2dc618 11791allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11792{
51ac9db5
SM
11793 auto delete_dwo_file = [] (void *item)
11794 {
11795 struct dwo_file *dwo_file = (struct dwo_file *) item;
11796
11797 delete dwo_file;
11798 };
11799
11800 return htab_up (htab_create_alloc_ex (41,
11801 hash_dwo_file,
11802 eq_dwo_file,
11803 delete_dwo_file,
11804 &objfile->objfile_obstack,
11805 hashtab_obstack_allocate,
11806 dummy_obstack_deallocate));
3019eac3
DE
11807}
11808
80626a55
DE
11809/* Lookup DWO file DWO_NAME. */
11810
11811static void **
ed2dc618
SM
11812lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11813 const char *dwo_name,
11814 const char *comp_dir)
80626a55
DE
11815{
11816 struct dwo_file find_entry;
11817 void **slot;
11818
11819 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11820 dwarf2_per_objfile->dwo_files
11821 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11822
0ac5b59e
DE
11823 find_entry.dwo_name = dwo_name;
11824 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11825 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11826 INSERT);
80626a55
DE
11827
11828 return slot;
11829}
11830
3019eac3
DE
11831static hashval_t
11832hash_dwo_unit (const void *item)
11833{
9a3c8263 11834 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11835
11836 /* This drops the top 32 bits of the id, but is ok for a hash. */
11837 return dwo_unit->signature;
11838}
11839
11840static int
11841eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11842{
9a3c8263
SM
11843 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11844 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11845
11846 /* The signature is assumed to be unique within the DWO file.
11847 So while object file CU dwo_id's always have the value zero,
11848 that's OK, assuming each object file DWO file has only one CU,
11849 and that's the rule for now. */
11850 return lhs->signature == rhs->signature;
11851}
11852
11853/* Allocate a hash table for DWO CUs,TUs.
11854 There is one of these tables for each of CUs,TUs for each DWO file. */
11855
11856static htab_t
11857allocate_dwo_unit_table (struct objfile *objfile)
11858{
11859 /* Start out with a pretty small number.
11860 Generally DWO files contain only one CU and maybe some TUs. */
11861 return htab_create_alloc_ex (3,
11862 hash_dwo_unit,
11863 eq_dwo_unit,
11864 NULL,
11865 &objfile->objfile_obstack,
11866 hashtab_obstack_allocate,
11867 dummy_obstack_deallocate);
11868}
11869
19c3d4c9 11870/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11871
11872static void
19c3d4c9
DE
11873create_dwo_cu_reader (const struct die_reader_specs *reader,
11874 const gdb_byte *info_ptr,
11875 struct die_info *comp_unit_die,
11876 int has_children,
c0ab21c2
TT
11877 struct dwo_file *dwo_file,
11878 struct dwo_unit *dwo_unit)
3019eac3
DE
11879{
11880 struct dwarf2_cu *cu = reader->cu;
9c541725 11881 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11882 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11883
a084a2a6
AT
11884 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11885 if (!signature.has_value ())
3019eac3 11886 {
b98664d3 11887 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11888 " its dwo_id [in module %s]"),
9d8780f0 11889 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11890 return;
11891 }
11892
3019eac3 11893 dwo_unit->dwo_file = dwo_file;
a084a2a6 11894 dwo_unit->signature = *signature;
8a0459fd 11895 dwo_unit->section = section;
9c541725 11896 dwo_unit->sect_off = sect_off;
3019eac3
DE
11897 dwo_unit->length = cu->per_cu->length;
11898
b4f54984 11899 if (dwarf_read_debug)
9d8780f0
SM
11900 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11901 sect_offset_str (sect_off),
9c541725 11902 hex_string (dwo_unit->signature));
3019eac3
DE
11903}
11904
33c5cd75 11905/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11906 Note: This function processes DWO files only, not DWP files. */
3019eac3 11907
33c5cd75 11908static void
ed2dc618 11909create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
11910 dwarf2_cu *cu, struct dwo_file &dwo_file,
11911 dwarf2_section_info &section, htab_t &cus_htab)
3019eac3
DE
11912{
11913 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11914 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11915
33c5cd75
DB
11916 dwarf2_read_section (objfile, &section);
11917 info_ptr = section.buffer;
3019eac3
DE
11918
11919 if (info_ptr == NULL)
33c5cd75 11920 return;
3019eac3 11921
b4f54984 11922 if (dwarf_read_debug)
19c3d4c9
DE
11923 {
11924 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11925 get_section_name (&section),
11926 get_section_file_name (&section));
19c3d4c9 11927 }
3019eac3 11928
33c5cd75 11929 end_ptr = info_ptr + section.size;
3019eac3
DE
11930 while (info_ptr < end_ptr)
11931 {
11932 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11933 struct dwo_unit read_unit {};
33c5cd75
DB
11934 struct dwo_unit *dwo_unit;
11935 void **slot;
11936 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11937
11938 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11939 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11940 per_cu.is_debug_types = 0;
33c5cd75
DB
11941 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11942 per_cu.section = &section;
11943
c0ab21c2
TT
11944 cutu_reader reader (&per_cu, cu, &dwo_file);
11945 if (!reader.dummy_p)
11946 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11947 reader.has_children, &dwo_file, &read_unit);
33c5cd75
DB
11948 info_ptr += per_cu.length;
11949
11950 // If the unit could not be parsed, skip it.
c0ab21c2 11951 if (read_unit.dwo_file == NULL)
33c5cd75 11952 continue;
3019eac3 11953
33c5cd75
DB
11954 if (cus_htab == NULL)
11955 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11956
33c5cd75 11957 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11958 *dwo_unit = read_unit;
33c5cd75
DB
11959 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11960 gdb_assert (slot != NULL);
11961 if (*slot != NULL)
19c3d4c9 11962 {
33c5cd75
DB
11963 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11964 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11965
b98664d3 11966 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11967 " the entry at offset %s, signature %s"),
11968 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11969 hex_string (dwo_unit->signature));
19c3d4c9 11970 }
33c5cd75 11971 *slot = (void *)dwo_unit;
3019eac3 11972 }
3019eac3
DE
11973}
11974
80626a55
DE
11975/* DWP file .debug_{cu,tu}_index section format:
11976 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11977
d2415c6c
DE
11978 DWP Version 1:
11979
80626a55
DE
11980 Both index sections have the same format, and serve to map a 64-bit
11981 signature to a set of section numbers. Each section begins with a header,
11982 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11983 indexes, and a pool of 32-bit section numbers. The index sections will be
11984 aligned at 8-byte boundaries in the file.
11985
d2415c6c
DE
11986 The index section header consists of:
11987
11988 V, 32 bit version number
11989 -, 32 bits unused
11990 N, 32 bit number of compilation units or type units in the index
11991 M, 32 bit number of slots in the hash table
80626a55 11992
d2415c6c 11993 Numbers are recorded using the byte order of the application binary.
80626a55 11994
d2415c6c
DE
11995 The hash table begins at offset 16 in the section, and consists of an array
11996 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11997 order of the application binary). Unused slots in the hash table are 0.
11998 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11999
d2415c6c
DE
12000 The parallel table begins immediately after the hash table
12001 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12002 array of 32-bit indexes (using the byte order of the application binary),
12003 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12004 table contains a 32-bit index into the pool of section numbers. For unused
12005 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12006
73869dc2
DE
12007 The pool of section numbers begins immediately following the hash table
12008 (at offset 16 + 12 * M from the beginning of the section). The pool of
12009 section numbers consists of an array of 32-bit words (using the byte order
12010 of the application binary). Each item in the array is indexed starting
12011 from 0. The hash table entry provides the index of the first section
12012 number in the set. Additional section numbers in the set follow, and the
12013 set is terminated by a 0 entry (section number 0 is not used in ELF).
12014
12015 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12016 section must be the first entry in the set, and the .debug_abbrev.dwo must
12017 be the second entry. Other members of the set may follow in any order.
12018
12019 ---
12020
12021 DWP Version 2:
12022
12023 DWP Version 2 combines all the .debug_info, etc. sections into one,
12024 and the entries in the index tables are now offsets into these sections.
12025 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12026 section.
12027
12028 Index Section Contents:
12029 Header
12030 Hash Table of Signatures dwp_hash_table.hash_table
12031 Parallel Table of Indices dwp_hash_table.unit_table
12032 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12033 Table of Section Sizes dwp_hash_table.v2.sizes
12034
12035 The index section header consists of:
12036
12037 V, 32 bit version number
12038 L, 32 bit number of columns in the table of section offsets
12039 N, 32 bit number of compilation units or type units in the index
12040 M, 32 bit number of slots in the hash table
12041
12042 Numbers are recorded using the byte order of the application binary.
12043
12044 The hash table has the same format as version 1.
12045 The parallel table of indices has the same format as version 1,
12046 except that the entries are origin-1 indices into the table of sections
12047 offsets and the table of section sizes.
12048
12049 The table of offsets begins immediately following the parallel table
12050 (at offset 16 + 12 * M from the beginning of the section). The table is
12051 a two-dimensional array of 32-bit words (using the byte order of the
12052 application binary), with L columns and N+1 rows, in row-major order.
12053 Each row in the array is indexed starting from 0. The first row provides
12054 a key to the remaining rows: each column in this row provides an identifier
12055 for a debug section, and the offsets in the same column of subsequent rows
12056 refer to that section. The section identifiers are:
12057
12058 DW_SECT_INFO 1 .debug_info.dwo
12059 DW_SECT_TYPES 2 .debug_types.dwo
12060 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12061 DW_SECT_LINE 4 .debug_line.dwo
12062 DW_SECT_LOC 5 .debug_loc.dwo
12063 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12064 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12065 DW_SECT_MACRO 8 .debug_macro.dwo
12066
12067 The offsets provided by the CU and TU index sections are the base offsets
12068 for the contributions made by each CU or TU to the corresponding section
12069 in the package file. Each CU and TU header contains an abbrev_offset
12070 field, used to find the abbreviations table for that CU or TU within the
12071 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12072 be interpreted as relative to the base offset given in the index section.
12073 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12074 should be interpreted as relative to the base offset for .debug_line.dwo,
12075 and offsets into other debug sections obtained from DWARF attributes should
12076 also be interpreted as relative to the corresponding base offset.
12077
12078 The table of sizes begins immediately following the table of offsets.
12079 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12080 with L columns and N rows, in row-major order. Each row in the array is
12081 indexed starting from 1 (row 0 is shared by the two tables).
12082
12083 ---
12084
12085 Hash table lookup is handled the same in version 1 and 2:
12086
12087 We assume that N and M will not exceed 2^32 - 1.
12088 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12089
d2415c6c
DE
12090 Given a 64-bit compilation unit signature or a type signature S, an entry
12091 in the hash table is located as follows:
80626a55 12092
d2415c6c
DE
12093 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12094 the low-order k bits all set to 1.
80626a55 12095
d2415c6c 12096 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12097
d2415c6c
DE
12098 3) If the hash table entry at index H matches the signature, use that
12099 entry. If the hash table entry at index H is unused (all zeroes),
12100 terminate the search: the signature is not present in the table.
80626a55 12101
d2415c6c 12102 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12103
d2415c6c 12104 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12105 to stop at an unused slot or find the match. */
80626a55
DE
12106
12107/* Create a hash table to map DWO IDs to their CU/TU entry in
12108 .debug_{info,types}.dwo in DWP_FILE.
12109 Returns NULL if there isn't one.
12110 Note: This function processes DWP files only, not DWO files. */
12111
12112static struct dwp_hash_table *
ed2dc618
SM
12113create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12114 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12115{
12116 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12117 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12118 const gdb_byte *index_ptr, *index_end;
80626a55 12119 struct dwarf2_section_info *index;
73869dc2 12120 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12121 struct dwp_hash_table *htab;
12122
12123 if (is_debug_types)
12124 index = &dwp_file->sections.tu_index;
12125 else
12126 index = &dwp_file->sections.cu_index;
12127
12128 if (dwarf2_section_empty_p (index))
12129 return NULL;
12130 dwarf2_read_section (objfile, index);
12131
12132 index_ptr = index->buffer;
12133 index_end = index_ptr + index->size;
12134
12135 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12136 index_ptr += 4;
12137 if (version == 2)
12138 nr_columns = read_4_bytes (dbfd, index_ptr);
12139 else
12140 nr_columns = 0;
12141 index_ptr += 4;
80626a55
DE
12142 nr_units = read_4_bytes (dbfd, index_ptr);
12143 index_ptr += 4;
12144 nr_slots = read_4_bytes (dbfd, index_ptr);
12145 index_ptr += 4;
12146
73869dc2 12147 if (version != 1 && version != 2)
80626a55 12148 {
21aa081e 12149 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12150 " [in module %s]"),
21aa081e 12151 pulongest (version), dwp_file->name);
80626a55
DE
12152 }
12153 if (nr_slots != (nr_slots & -nr_slots))
12154 {
21aa081e 12155 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12156 " is not power of 2 [in module %s]"),
21aa081e 12157 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12158 }
12159
12160 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12161 htab->version = version;
12162 htab->nr_columns = nr_columns;
80626a55
DE
12163 htab->nr_units = nr_units;
12164 htab->nr_slots = nr_slots;
12165 htab->hash_table = index_ptr;
12166 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12167
12168 /* Exit early if the table is empty. */
12169 if (nr_slots == 0 || nr_units == 0
12170 || (version == 2 && nr_columns == 0))
12171 {
12172 /* All must be zero. */
12173 if (nr_slots != 0 || nr_units != 0
12174 || (version == 2 && nr_columns != 0))
12175 {
b98664d3 12176 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12177 " all zero [in modules %s]"),
12178 dwp_file->name);
12179 }
12180 return htab;
12181 }
12182
12183 if (version == 1)
12184 {
12185 htab->section_pool.v1.indices =
12186 htab->unit_table + sizeof (uint32_t) * nr_slots;
12187 /* It's harder to decide whether the section is too small in v1.
12188 V1 is deprecated anyway so we punt. */
12189 }
12190 else
12191 {
12192 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12193 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12194 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12195 /* Reverse map for error checking. */
12196 int ids_seen[DW_SECT_MAX + 1];
12197 int i;
12198
12199 if (nr_columns < 2)
12200 {
12201 error (_("Dwarf Error: bad DWP hash table, too few columns"
12202 " in section table [in module %s]"),
12203 dwp_file->name);
12204 }
12205 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12206 {
12207 error (_("Dwarf Error: bad DWP hash table, too many columns"
12208 " in section table [in module %s]"),
12209 dwp_file->name);
12210 }
04fd5eed
GB
12211 memset (ids, 255, sizeof_ids);
12212 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12213 for (i = 0; i < nr_columns; ++i)
12214 {
12215 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12216
12217 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12218 {
12219 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12220 " in section table [in module %s]"),
12221 id, dwp_file->name);
12222 }
12223 if (ids_seen[id] != -1)
12224 {
12225 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12226 " id %d in section table [in module %s]"),
12227 id, dwp_file->name);
12228 }
12229 ids_seen[id] = i;
12230 ids[i] = id;
12231 }
12232 /* Must have exactly one info or types section. */
12233 if (((ids_seen[DW_SECT_INFO] != -1)
12234 + (ids_seen[DW_SECT_TYPES] != -1))
12235 != 1)
12236 {
12237 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12238 " DWO info/types section [in module %s]"),
12239 dwp_file->name);
12240 }
12241 /* Must have an abbrev section. */
12242 if (ids_seen[DW_SECT_ABBREV] == -1)
12243 {
12244 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12245 " section [in module %s]"),
12246 dwp_file->name);
12247 }
12248 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12249 htab->section_pool.v2.sizes =
12250 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12251 * nr_units * nr_columns);
12252 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12253 * nr_units * nr_columns))
12254 > index_end)
12255 {
12256 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12257 " [in module %s]"),
12258 dwp_file->name);
12259 }
12260 }
80626a55
DE
12261
12262 return htab;
12263}
12264
12265/* Update SECTIONS with the data from SECTP.
12266
12267 This function is like the other "locate" section routines that are
12268 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12269 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12270
12271 The result is non-zero for success, or zero if an error was found. */
12272
12273static int
73869dc2
DE
12274locate_v1_virtual_dwo_sections (asection *sectp,
12275 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12276{
12277 const struct dwop_section_names *names = &dwop_section_names;
12278
12279 if (section_is_p (sectp->name, &names->abbrev_dwo))
12280 {
12281 /* There can be only one. */
049412e3 12282 if (sections->abbrev.s.section != NULL)
80626a55 12283 return 0;
049412e3 12284 sections->abbrev.s.section = sectp;
fd361982 12285 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12286 }
12287 else if (section_is_p (sectp->name, &names->info_dwo)
12288 || section_is_p (sectp->name, &names->types_dwo))
12289 {
12290 /* There can be only one. */
049412e3 12291 if (sections->info_or_types.s.section != NULL)
80626a55 12292 return 0;
049412e3 12293 sections->info_or_types.s.section = sectp;
fd361982 12294 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12295 }
12296 else if (section_is_p (sectp->name, &names->line_dwo))
12297 {
12298 /* There can be only one. */
049412e3 12299 if (sections->line.s.section != NULL)
80626a55 12300 return 0;
049412e3 12301 sections->line.s.section = sectp;
fd361982 12302 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12303 }
12304 else if (section_is_p (sectp->name, &names->loc_dwo))
12305 {
12306 /* There can be only one. */
049412e3 12307 if (sections->loc.s.section != NULL)
80626a55 12308 return 0;
049412e3 12309 sections->loc.s.section = sectp;
fd361982 12310 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12311 }
12312 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12313 {
12314 /* There can be only one. */
049412e3 12315 if (sections->macinfo.s.section != NULL)
80626a55 12316 return 0;
049412e3 12317 sections->macinfo.s.section = sectp;
fd361982 12318 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12319 }
12320 else if (section_is_p (sectp->name, &names->macro_dwo))
12321 {
12322 /* There can be only one. */
049412e3 12323 if (sections->macro.s.section != NULL)
80626a55 12324 return 0;
049412e3 12325 sections->macro.s.section = sectp;
fd361982 12326 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12327 }
12328 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12329 {
12330 /* There can be only one. */
049412e3 12331 if (sections->str_offsets.s.section != NULL)
80626a55 12332 return 0;
049412e3 12333 sections->str_offsets.s.section = sectp;
fd361982 12334 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12335 }
12336 else
12337 {
12338 /* No other kind of section is valid. */
12339 return 0;
12340 }
12341
12342 return 1;
12343}
12344
73869dc2
DE
12345/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12346 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12347 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12348 This is for DWP version 1 files. */
80626a55
DE
12349
12350static struct dwo_unit *
ed2dc618
SM
12351create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12352 struct dwp_file *dwp_file,
73869dc2
DE
12353 uint32_t unit_index,
12354 const char *comp_dir,
12355 ULONGEST signature, int is_debug_types)
80626a55
DE
12356{
12357 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12358 const struct dwp_hash_table *dwp_htab =
12359 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12360 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12361 const char *kind = is_debug_types ? "TU" : "CU";
12362 struct dwo_file *dwo_file;
12363 struct dwo_unit *dwo_unit;
73869dc2 12364 struct virtual_v1_dwo_sections sections;
80626a55 12365 void **dwo_file_slot;
80626a55
DE
12366 int i;
12367
73869dc2
DE
12368 gdb_assert (dwp_file->version == 1);
12369
b4f54984 12370 if (dwarf_read_debug)
80626a55 12371 {
73869dc2 12372 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12373 kind,
73869dc2 12374 pulongest (unit_index), hex_string (signature),
80626a55
DE
12375 dwp_file->name);
12376 }
12377
19ac8c2e 12378 /* Fetch the sections of this DWO unit.
80626a55
DE
12379 Put a limit on the number of sections we look for so that bad data
12380 doesn't cause us to loop forever. */
12381
73869dc2 12382#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12383 (1 /* .debug_info or .debug_types */ \
12384 + 1 /* .debug_abbrev */ \
12385 + 1 /* .debug_line */ \
12386 + 1 /* .debug_loc */ \
12387 + 1 /* .debug_str_offsets */ \
19ac8c2e 12388 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12389 + 1 /* trailing zero */)
12390
12391 memset (&sections, 0, sizeof (sections));
80626a55 12392
73869dc2 12393 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12394 {
12395 asection *sectp;
12396 uint32_t section_nr =
12397 read_4_bytes (dbfd,
73869dc2
DE
12398 dwp_htab->section_pool.v1.indices
12399 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12400
12401 if (section_nr == 0)
12402 break;
12403 if (section_nr >= dwp_file->num_sections)
12404 {
12405 error (_("Dwarf Error: bad DWP hash table, section number too large"
12406 " [in module %s]"),
12407 dwp_file->name);
12408 }
12409
12410 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12411 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12412 {
12413 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12414 " [in module %s]"),
12415 dwp_file->name);
12416 }
12417 }
12418
12419 if (i < 2
a32a8923
DE
12420 || dwarf2_section_empty_p (&sections.info_or_types)
12421 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12422 {
12423 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12424 " [in module %s]"),
12425 dwp_file->name);
12426 }
73869dc2 12427 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12428 {
12429 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12430 " [in module %s]"),
12431 dwp_file->name);
12432 }
12433
12434 /* It's easier for the rest of the code if we fake a struct dwo_file and
12435 have dwo_unit "live" in that. At least for now.
12436
12437 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12438 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12439 file, we can combine them back into a virtual DWO file to save space
12440 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12441 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12442
791afaa2
TT
12443 std::string virtual_dwo_name =
12444 string_printf ("virtual-dwo/%d-%d-%d-%d",
12445 get_section_id (&sections.abbrev),
12446 get_section_id (&sections.line),
12447 get_section_id (&sections.loc),
12448 get_section_id (&sections.str_offsets));
80626a55 12449 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12450 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12451 virtual_dwo_name.c_str (),
12452 comp_dir);
80626a55
DE
12453 /* Create one if necessary. */
12454 if (*dwo_file_slot == NULL)
12455 {
b4f54984 12456 if (dwarf_read_debug)
80626a55
DE
12457 {
12458 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12459 virtual_dwo_name.c_str ());
80626a55 12460 }
51ac9db5 12461 dwo_file = new struct dwo_file;
efba19b0
TT
12462 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12463 virtual_dwo_name);
0ac5b59e 12464 dwo_file->comp_dir = comp_dir;
80626a55
DE
12465 dwo_file->sections.abbrev = sections.abbrev;
12466 dwo_file->sections.line = sections.line;
12467 dwo_file->sections.loc = sections.loc;
12468 dwo_file->sections.macinfo = sections.macinfo;
12469 dwo_file->sections.macro = sections.macro;
12470 dwo_file->sections.str_offsets = sections.str_offsets;
12471 /* The "str" section is global to the entire DWP file. */
12472 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12473 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12474 there's no need to record it in dwo_file.
12475 Also, we can't simply record type sections in dwo_file because
12476 we record a pointer into the vector in dwo_unit. As we collect more
12477 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12478 for it, invalidating all copies of pointers into the previous
12479 contents. */
80626a55
DE
12480 *dwo_file_slot = dwo_file;
12481 }
12482 else
12483 {
b4f54984 12484 if (dwarf_read_debug)
80626a55
DE
12485 {
12486 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12487 virtual_dwo_name.c_str ());
80626a55 12488 }
9a3c8263 12489 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12490 }
80626a55
DE
12491
12492 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12493 dwo_unit->dwo_file = dwo_file;
12494 dwo_unit->signature = signature;
8d749320
SM
12495 dwo_unit->section =
12496 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12497 *dwo_unit->section = sections.info_or_types;
57d63ce2 12498 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12499
12500 return dwo_unit;
12501}
12502
73869dc2
DE
12503/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12504 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12505 piece within that section used by a TU/CU, return a virtual section
12506 of just that piece. */
12507
12508static struct dwarf2_section_info
ed2dc618
SM
12509create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12510 struct dwarf2_section_info *section,
73869dc2
DE
12511 bfd_size_type offset, bfd_size_type size)
12512{
12513 struct dwarf2_section_info result;
12514 asection *sectp;
12515
12516 gdb_assert (section != NULL);
12517 gdb_assert (!section->is_virtual);
12518
12519 memset (&result, 0, sizeof (result));
12520 result.s.containing_section = section;
dc4ccb6f 12521 result.is_virtual = true;
73869dc2
DE
12522
12523 if (size == 0)
12524 return result;
12525
12526 sectp = get_section_bfd_section (section);
12527
12528 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12529 bounds of the real section. This is a pretty-rare event, so just
12530 flag an error (easier) instead of a warning and trying to cope. */
12531 if (sectp == NULL
fd361982 12532 || offset + size > bfd_section_size (sectp))
73869dc2 12533 {
73869dc2
DE
12534 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12535 " in section %s [in module %s]"),
fd361982 12536 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12537 objfile_name (dwarf2_per_objfile->objfile));
12538 }
12539
12540 result.virtual_offset = offset;
12541 result.size = size;
12542 return result;
12543}
12544
12545/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12546 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12547 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12548 This is for DWP version 2 files. */
12549
12550static struct dwo_unit *
ed2dc618
SM
12551create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12552 struct dwp_file *dwp_file,
73869dc2
DE
12553 uint32_t unit_index,
12554 const char *comp_dir,
12555 ULONGEST signature, int is_debug_types)
12556{
12557 struct objfile *objfile = dwarf2_per_objfile->objfile;
12558 const struct dwp_hash_table *dwp_htab =
12559 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12560 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12561 const char *kind = is_debug_types ? "TU" : "CU";
12562 struct dwo_file *dwo_file;
12563 struct dwo_unit *dwo_unit;
12564 struct virtual_v2_dwo_sections sections;
12565 void **dwo_file_slot;
73869dc2
DE
12566 int i;
12567
12568 gdb_assert (dwp_file->version == 2);
12569
b4f54984 12570 if (dwarf_read_debug)
73869dc2
DE
12571 {
12572 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12573 kind,
12574 pulongest (unit_index), hex_string (signature),
12575 dwp_file->name);
12576 }
12577
12578 /* Fetch the section offsets of this DWO unit. */
12579
12580 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12581
12582 for (i = 0; i < dwp_htab->nr_columns; ++i)
12583 {
12584 uint32_t offset = read_4_bytes (dbfd,
12585 dwp_htab->section_pool.v2.offsets
12586 + (((unit_index - 1) * dwp_htab->nr_columns
12587 + i)
12588 * sizeof (uint32_t)));
12589 uint32_t size = read_4_bytes (dbfd,
12590 dwp_htab->section_pool.v2.sizes
12591 + (((unit_index - 1) * dwp_htab->nr_columns
12592 + i)
12593 * sizeof (uint32_t)));
12594
12595 switch (dwp_htab->section_pool.v2.section_ids[i])
12596 {
12597 case DW_SECT_INFO:
12598 case DW_SECT_TYPES:
12599 sections.info_or_types_offset = offset;
12600 sections.info_or_types_size = size;
12601 break;
12602 case DW_SECT_ABBREV:
12603 sections.abbrev_offset = offset;
12604 sections.abbrev_size = size;
12605 break;
12606 case DW_SECT_LINE:
12607 sections.line_offset = offset;
12608 sections.line_size = size;
12609 break;
12610 case DW_SECT_LOC:
12611 sections.loc_offset = offset;
12612 sections.loc_size = size;
12613 break;
12614 case DW_SECT_STR_OFFSETS:
12615 sections.str_offsets_offset = offset;
12616 sections.str_offsets_size = size;
12617 break;
12618 case DW_SECT_MACINFO:
12619 sections.macinfo_offset = offset;
12620 sections.macinfo_size = size;
12621 break;
12622 case DW_SECT_MACRO:
12623 sections.macro_offset = offset;
12624 sections.macro_size = size;
12625 break;
12626 }
12627 }
12628
12629 /* It's easier for the rest of the code if we fake a struct dwo_file and
12630 have dwo_unit "live" in that. At least for now.
12631
12632 The DWP file can be made up of a random collection of CUs and TUs.
12633 However, for each CU + set of TUs that came from the same original DWO
12634 file, we can combine them back into a virtual DWO file to save space
12635 (fewer struct dwo_file objects to allocate). Remember that for really
12636 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12637
791afaa2
TT
12638 std::string virtual_dwo_name =
12639 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12640 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12641 (long) (sections.line_size ? sections.line_offset : 0),
12642 (long) (sections.loc_size ? sections.loc_offset : 0),
12643 (long) (sections.str_offsets_size
12644 ? sections.str_offsets_offset : 0));
73869dc2 12645 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12646 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12647 virtual_dwo_name.c_str (),
12648 comp_dir);
73869dc2
DE
12649 /* Create one if necessary. */
12650 if (*dwo_file_slot == NULL)
12651 {
b4f54984 12652 if (dwarf_read_debug)
73869dc2
DE
12653 {
12654 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12655 virtual_dwo_name.c_str ());
73869dc2 12656 }
51ac9db5 12657 dwo_file = new struct dwo_file;
efba19b0
TT
12658 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12659 virtual_dwo_name);
73869dc2
DE
12660 dwo_file->comp_dir = comp_dir;
12661 dwo_file->sections.abbrev =
ed2dc618 12662 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12663 sections.abbrev_offset, sections.abbrev_size);
12664 dwo_file->sections.line =
ed2dc618 12665 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12666 sections.line_offset, sections.line_size);
12667 dwo_file->sections.loc =
ed2dc618 12668 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12669 sections.loc_offset, sections.loc_size);
12670 dwo_file->sections.macinfo =
ed2dc618 12671 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12672 sections.macinfo_offset, sections.macinfo_size);
12673 dwo_file->sections.macro =
ed2dc618 12674 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12675 sections.macro_offset, sections.macro_size);
12676 dwo_file->sections.str_offsets =
ed2dc618
SM
12677 create_dwp_v2_section (dwarf2_per_objfile,
12678 &dwp_file->sections.str_offsets,
73869dc2
DE
12679 sections.str_offsets_offset,
12680 sections.str_offsets_size);
12681 /* The "str" section is global to the entire DWP file. */
12682 dwo_file->sections.str = dwp_file->sections.str;
12683 /* The info or types section is assigned below to dwo_unit,
12684 there's no need to record it in dwo_file.
12685 Also, we can't simply record type sections in dwo_file because
12686 we record a pointer into the vector in dwo_unit. As we collect more
12687 types we'll grow the vector and eventually have to reallocate space
12688 for it, invalidating all copies of pointers into the previous
12689 contents. */
12690 *dwo_file_slot = dwo_file;
12691 }
12692 else
12693 {
b4f54984 12694 if (dwarf_read_debug)
73869dc2
DE
12695 {
12696 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12697 virtual_dwo_name.c_str ());
73869dc2 12698 }
9a3c8263 12699 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12700 }
73869dc2
DE
12701
12702 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12703 dwo_unit->dwo_file = dwo_file;
12704 dwo_unit->signature = signature;
8d749320
SM
12705 dwo_unit->section =
12706 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12707 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12708 is_debug_types
73869dc2
DE
12709 ? &dwp_file->sections.types
12710 : &dwp_file->sections.info,
12711 sections.info_or_types_offset,
12712 sections.info_or_types_size);
12713 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12714
12715 return dwo_unit;
12716}
12717
57d63ce2
DE
12718/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12719 Returns NULL if the signature isn't found. */
80626a55
DE
12720
12721static struct dwo_unit *
ed2dc618
SM
12722lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12723 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12724 ULONGEST signature, int is_debug_types)
80626a55 12725{
57d63ce2
DE
12726 const struct dwp_hash_table *dwp_htab =
12727 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12728 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12729 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12730 uint32_t hash = signature & mask;
12731 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12732 unsigned int i;
12733 void **slot;
870f88f7 12734 struct dwo_unit find_dwo_cu;
80626a55
DE
12735
12736 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12737 find_dwo_cu.signature = signature;
19ac8c2e
DE
12738 slot = htab_find_slot (is_debug_types
12739 ? dwp_file->loaded_tus
12740 : dwp_file->loaded_cus,
12741 &find_dwo_cu, INSERT);
80626a55
DE
12742
12743 if (*slot != NULL)
9a3c8263 12744 return (struct dwo_unit *) *slot;
80626a55
DE
12745
12746 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12747 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12748 {
12749 ULONGEST signature_in_table;
12750
12751 signature_in_table =
57d63ce2 12752 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12753 if (signature_in_table == signature)
12754 {
57d63ce2
DE
12755 uint32_t unit_index =
12756 read_4_bytes (dbfd,
12757 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12758
73869dc2
DE
12759 if (dwp_file->version == 1)
12760 {
ed2dc618
SM
12761 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12762 dwp_file, unit_index,
73869dc2
DE
12763 comp_dir, signature,
12764 is_debug_types);
12765 }
12766 else
12767 {
ed2dc618
SM
12768 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12769 dwp_file, unit_index,
73869dc2
DE
12770 comp_dir, signature,
12771 is_debug_types);
12772 }
9a3c8263 12773 return (struct dwo_unit *) *slot;
80626a55
DE
12774 }
12775 if (signature_in_table == 0)
12776 return NULL;
12777 hash = (hash + hash2) & mask;
12778 }
12779
12780 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12781 " [in module %s]"),
12782 dwp_file->name);
12783}
12784
ab5088bf 12785/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12786 Open the file specified by FILE_NAME and hand it off to BFD for
12787 preliminary analysis. Return a newly initialized bfd *, which
12788 includes a canonicalized copy of FILE_NAME.
80626a55 12789 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12790 SEARCH_CWD is true if the current directory is to be searched.
12791 It will be searched before debug-file-directory.
13aaf454
DE
12792 If successful, the file is added to the bfd include table of the
12793 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12794 If unable to find/open the file, return NULL.
3019eac3
DE
12795 NOTE: This function is derived from symfile_bfd_open. */
12796
192b62ce 12797static gdb_bfd_ref_ptr
ed2dc618
SM
12798try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12799 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12800{
24b9144d 12801 int desc;
9c02c129
DE
12802 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12803 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12804 to debug_file_directory. */
e0cc99a6 12805 const char *search_path;
9c02c129
DE
12806 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12807
e0cc99a6 12808 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12809 if (search_cwd)
12810 {
12811 if (*debug_file_directory != '\0')
e0cc99a6
TT
12812 {
12813 search_path_holder.reset (concat (".", dirname_separator_string,
12814 debug_file_directory,
12815 (char *) NULL));
12816 search_path = search_path_holder.get ();
12817 }
6ac97d4c 12818 else
e0cc99a6 12819 search_path = ".";
6ac97d4c 12820 }
9c02c129 12821 else
e0cc99a6 12822 search_path = debug_file_directory;
3019eac3 12823
24b9144d 12824 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12825 if (is_dwp)
12826 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12827
12828 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12829 desc = openp (search_path, flags, file_name,
3019eac3
DE
12830 O_RDONLY | O_BINARY, &absolute_name);
12831 if (desc < 0)
12832 return NULL;
12833
e0cc99a6
TT
12834 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12835 gnutarget, desc));
9c02c129
DE
12836 if (sym_bfd == NULL)
12837 return NULL;
192b62ce 12838 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12839
192b62ce
TT
12840 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12841 return NULL;
3019eac3 12842
13aaf454
DE
12843 /* Success. Record the bfd as having been included by the objfile's bfd.
12844 This is important because things like demangled_names_hash lives in the
12845 objfile's per_bfd space and may have references to things like symbol
12846 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12847 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12848
3019eac3
DE
12849 return sym_bfd;
12850}
12851
ab5088bf 12852/* Try to open DWO file FILE_NAME.
3019eac3
DE
12853 COMP_DIR is the DW_AT_comp_dir attribute.
12854 The result is the bfd handle of the file.
12855 If there is a problem finding or opening the file, return NULL.
12856 Upon success, the canonicalized path of the file is stored in the bfd,
12857 same as symfile_bfd_open. */
12858
192b62ce 12859static gdb_bfd_ref_ptr
ed2dc618
SM
12860open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12861 const char *file_name, const char *comp_dir)
3019eac3 12862{
80626a55 12863 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12864 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12865 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12866
12867 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12868
12869 if (comp_dir != NULL)
12870 {
43816ebc
TT
12871 gdb::unique_xmalloc_ptr<char> path_to_try
12872 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12873
12874 /* NOTE: If comp_dir is a relative path, this will also try the
12875 search path, which seems useful. */
ed2dc618 12876 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12877 path_to_try.get (),
ed2dc618 12878 0 /*is_dwp*/,
192b62ce 12879 1 /*search_cwd*/));
3019eac3
DE
12880 if (abfd != NULL)
12881 return abfd;
12882 }
12883
12884 /* That didn't work, try debug-file-directory, which, despite its name,
12885 is a list of paths. */
12886
12887 if (*debug_file_directory == '\0')
12888 return NULL;
12889
ed2dc618
SM
12890 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12891 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12892}
12893
80626a55
DE
12894/* This function is mapped across the sections and remembers the offset and
12895 size of each of the DWO debugging sections we are interested in. */
12896
12897static void
12898dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12899{
9a3c8263 12900 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12901 const struct dwop_section_names *names = &dwop_section_names;
12902
12903 if (section_is_p (sectp->name, &names->abbrev_dwo))
12904 {
049412e3 12905 dwo_sections->abbrev.s.section = sectp;
fd361982 12906 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12907 }
12908 else if (section_is_p (sectp->name, &names->info_dwo))
12909 {
049412e3 12910 dwo_sections->info.s.section = sectp;
fd361982 12911 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12912 }
12913 else if (section_is_p (sectp->name, &names->line_dwo))
12914 {
049412e3 12915 dwo_sections->line.s.section = sectp;
fd361982 12916 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12917 }
12918 else if (section_is_p (sectp->name, &names->loc_dwo))
12919 {
049412e3 12920 dwo_sections->loc.s.section = sectp;
fd361982 12921 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12922 }
12923 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12924 {
049412e3 12925 dwo_sections->macinfo.s.section = sectp;
fd361982 12926 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12927 }
12928 else if (section_is_p (sectp->name, &names->macro_dwo))
12929 {
049412e3 12930 dwo_sections->macro.s.section = sectp;
fd361982 12931 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12932 }
12933 else if (section_is_p (sectp->name, &names->str_dwo))
12934 {
049412e3 12935 dwo_sections->str.s.section = sectp;
fd361982 12936 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12937 }
12938 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12939 {
049412e3 12940 dwo_sections->str_offsets.s.section = sectp;
fd361982 12941 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12942 }
12943 else if (section_is_p (sectp->name, &names->types_dwo))
12944 {
12945 struct dwarf2_section_info type_section;
12946
12947 memset (&type_section, 0, sizeof (type_section));
049412e3 12948 type_section.s.section = sectp;
fd361982 12949 type_section.size = bfd_section_size (sectp);
fd5866f6 12950 dwo_sections->types.push_back (type_section);
80626a55
DE
12951 }
12952}
12953
ab5088bf 12954/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12955 by PER_CU. This is for the non-DWP case.
80626a55 12956 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12957
12958static struct dwo_file *
0ac5b59e
DE
12959open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12960 const char *dwo_name, const char *comp_dir)
3019eac3 12961{
ed2dc618 12962 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12963
fb1eb2f9 12964 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12965 if (dbfd == NULL)
12966 {
b4f54984 12967 if (dwarf_read_debug)
80626a55
DE
12968 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12969 return NULL;
12970 }
263db9a1 12971
51ac9db5 12972 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12973 dwo_file->dwo_name = dwo_name;
12974 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12975 dwo_file->dbfd = std::move (dbfd);
3019eac3 12976
fb1eb2f9 12977 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12978 &dwo_file->sections);
3019eac3 12979
18a8505e
AT
12980 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12981 dwo_file->sections.info, dwo_file->cus);
3019eac3 12982
263db9a1 12983 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12984 dwo_file->sections.types, dwo_file->tus);
3019eac3 12985
b4f54984 12986 if (dwarf_read_debug)
80626a55
DE
12987 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12988
263db9a1 12989 return dwo_file.release ();
3019eac3
DE
12990}
12991
80626a55 12992/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12993 size of each of the DWP debugging sections common to version 1 and 2 that
12994 we are interested in. */
3019eac3 12995
80626a55 12996static void
73869dc2
DE
12997dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12998 void *dwp_file_ptr)
3019eac3 12999{
9a3c8263 13000 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13001 const struct dwop_section_names *names = &dwop_section_names;
13002 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13003
80626a55 13004 /* Record the ELF section number for later lookup: this is what the
73869dc2 13005 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13006 gdb_assert (elf_section_nr < dwp_file->num_sections);
13007 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13008
80626a55
DE
13009 /* Look for specific sections that we need. */
13010 if (section_is_p (sectp->name, &names->str_dwo))
13011 {
049412e3 13012 dwp_file->sections.str.s.section = sectp;
fd361982 13013 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13014 }
13015 else if (section_is_p (sectp->name, &names->cu_index))
13016 {
049412e3 13017 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13018 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13019 }
13020 else if (section_is_p (sectp->name, &names->tu_index))
13021 {
049412e3 13022 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13023 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13024 }
13025}
3019eac3 13026
73869dc2
DE
13027/* This function is mapped across the sections and remembers the offset and
13028 size of each of the DWP version 2 debugging sections that we are interested
13029 in. This is split into a separate function because we don't know if we
13030 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13031
13032static void
13033dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13034{
9a3c8263 13035 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13036 const struct dwop_section_names *names = &dwop_section_names;
13037 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13038
13039 /* Record the ELF section number for later lookup: this is what the
13040 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13041 gdb_assert (elf_section_nr < dwp_file->num_sections);
13042 dwp_file->elf_sections[elf_section_nr] = sectp;
13043
13044 /* Look for specific sections that we need. */
13045 if (section_is_p (sectp->name, &names->abbrev_dwo))
13046 {
049412e3 13047 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13048 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13049 }
13050 else if (section_is_p (sectp->name, &names->info_dwo))
13051 {
049412e3 13052 dwp_file->sections.info.s.section = sectp;
fd361982 13053 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13054 }
13055 else if (section_is_p (sectp->name, &names->line_dwo))
13056 {
049412e3 13057 dwp_file->sections.line.s.section = sectp;
fd361982 13058 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13059 }
13060 else if (section_is_p (sectp->name, &names->loc_dwo))
13061 {
049412e3 13062 dwp_file->sections.loc.s.section = sectp;
fd361982 13063 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13064 }
13065 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13066 {
049412e3 13067 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13068 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13069 }
13070 else if (section_is_p (sectp->name, &names->macro_dwo))
13071 {
049412e3 13072 dwp_file->sections.macro.s.section = sectp;
fd361982 13073 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13074 }
13075 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13076 {
049412e3 13077 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13078 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13079 }
13080 else if (section_is_p (sectp->name, &names->types_dwo))
13081 {
049412e3 13082 dwp_file->sections.types.s.section = sectp;
fd361982 13083 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13084 }
13085}
13086
80626a55 13087/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13088
80626a55
DE
13089static hashval_t
13090hash_dwp_loaded_cutus (const void *item)
13091{
9a3c8263 13092 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13093
80626a55
DE
13094 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13095 return dwo_unit->signature;
3019eac3
DE
13096}
13097
80626a55 13098/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13099
80626a55
DE
13100static int
13101eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13102{
9a3c8263
SM
13103 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13104 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13105
80626a55
DE
13106 return dua->signature == dub->signature;
13107}
3019eac3 13108
80626a55 13109/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13110
80626a55
DE
13111static htab_t
13112allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13113{
13114 return htab_create_alloc_ex (3,
13115 hash_dwp_loaded_cutus,
13116 eq_dwp_loaded_cutus,
13117 NULL,
13118 &objfile->objfile_obstack,
13119 hashtab_obstack_allocate,
13120 dummy_obstack_deallocate);
13121}
3019eac3 13122
ab5088bf
DE
13123/* Try to open DWP file FILE_NAME.
13124 The result is the bfd handle of the file.
13125 If there is a problem finding or opening the file, return NULL.
13126 Upon success, the canonicalized path of the file is stored in the bfd,
13127 same as symfile_bfd_open. */
13128
192b62ce 13129static gdb_bfd_ref_ptr
ed2dc618
SM
13130open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13131 const char *file_name)
ab5088bf 13132{
ed2dc618
SM
13133 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13134 1 /*is_dwp*/,
192b62ce 13135 1 /*search_cwd*/));
6ac97d4c
DE
13136 if (abfd != NULL)
13137 return abfd;
13138
13139 /* Work around upstream bug 15652.
13140 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13141 [Whether that's a "bug" is debatable, but it is getting in our way.]
13142 We have no real idea where the dwp file is, because gdb's realpath-ing
13143 of the executable's path may have discarded the needed info.
13144 [IWBN if the dwp file name was recorded in the executable, akin to
13145 .gnu_debuglink, but that doesn't exist yet.]
13146 Strip the directory from FILE_NAME and search again. */
13147 if (*debug_file_directory != '\0')
13148 {
13149 /* Don't implicitly search the current directory here.
13150 If the user wants to search "." to handle this case,
13151 it must be added to debug-file-directory. */
ed2dc618
SM
13152 return try_open_dwop_file (dwarf2_per_objfile,
13153 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13154 0 /*search_cwd*/);
13155 }
13156
13157 return NULL;
ab5088bf
DE
13158}
13159
80626a55
DE
13160/* Initialize the use of the DWP file for the current objfile.
13161 By convention the name of the DWP file is ${objfile}.dwp.
13162 The result is NULL if it can't be found. */
a766d390 13163
400174b1 13164static std::unique_ptr<struct dwp_file>
ed2dc618 13165open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13166{
13167 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13168
82bf32bc
JK
13169 /* Try to find first .dwp for the binary file before any symbolic links
13170 resolving. */
6c447423
DE
13171
13172 /* If the objfile is a debug file, find the name of the real binary
13173 file and get the name of dwp file from there. */
d721ba37 13174 std::string dwp_name;
6c447423
DE
13175 if (objfile->separate_debug_objfile_backlink != NULL)
13176 {
13177 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13178 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13179
d721ba37 13180 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13181 }
13182 else
d721ba37
PA
13183 dwp_name = objfile->original_name;
13184
13185 dwp_name += ".dwp";
80626a55 13186
ed2dc618 13187 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13188 if (dbfd == NULL
13189 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13190 {
13191 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13192 dwp_name = objfile_name (objfile);
13193 dwp_name += ".dwp";
ed2dc618 13194 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13195 }
13196
80626a55
DE
13197 if (dbfd == NULL)
13198 {
b4f54984 13199 if (dwarf_read_debug)
d721ba37 13200 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13201 return std::unique_ptr<dwp_file> ();
3019eac3 13202 }
400174b1
TT
13203
13204 const char *name = bfd_get_filename (dbfd.get ());
13205 std::unique_ptr<struct dwp_file> dwp_file
13206 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13207
0a0f4c01 13208 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13209 dwp_file->elf_sections =
13210 OBSTACK_CALLOC (&objfile->objfile_obstack,
13211 dwp_file->num_sections, asection *);
13212
400174b1
TT
13213 bfd_map_over_sections (dwp_file->dbfd.get (),
13214 dwarf2_locate_common_dwp_sections,
13215 dwp_file.get ());
80626a55 13216
400174b1
TT
13217 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13218 0);
80626a55 13219
400174b1
TT
13220 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13221 1);
80626a55 13222
73869dc2 13223 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13224 if (dwp_file->cus && dwp_file->tus
13225 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13226 {
13227 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13228 pretty bizarre. We use pulongest here because that's the established
4d65956b 13229 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13230 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13231 " TU version %s [in DWP file %s]"),
13232 pulongest (dwp_file->cus->version),
d721ba37 13233 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13234 }
08302ed2
DE
13235
13236 if (dwp_file->cus)
13237 dwp_file->version = dwp_file->cus->version;
13238 else if (dwp_file->tus)
13239 dwp_file->version = dwp_file->tus->version;
13240 else
13241 dwp_file->version = 2;
73869dc2
DE
13242
13243 if (dwp_file->version == 2)
400174b1
TT
13244 bfd_map_over_sections (dwp_file->dbfd.get (),
13245 dwarf2_locate_v2_dwp_sections,
13246 dwp_file.get ());
73869dc2 13247
19ac8c2e
DE
13248 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13249 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13250
b4f54984 13251 if (dwarf_read_debug)
80626a55
DE
13252 {
13253 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13254 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13255 " %s CUs, %s TUs\n",
13256 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13257 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13258 }
13259
13260 return dwp_file;
3019eac3 13261}
c906108c 13262
ab5088bf
DE
13263/* Wrapper around open_and_init_dwp_file, only open it once. */
13264
13265static struct dwp_file *
ed2dc618 13266get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13267{
13268 if (! dwarf2_per_objfile->dwp_checked)
13269 {
ed2dc618
SM
13270 dwarf2_per_objfile->dwp_file
13271 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13272 dwarf2_per_objfile->dwp_checked = 1;
13273 }
400174b1 13274 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13275}
13276
80626a55
DE
13277/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13278 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13279 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13280 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13281 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13282
13283 This is called, for example, when wanting to read a variable with a
13284 complex location. Therefore we don't want to do file i/o for every call.
13285 Therefore we don't want to look for a DWO file on every call.
13286 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13287 then we check if we've already seen DWO_NAME, and only THEN do we check
13288 for a DWO file.
13289
1c658ad5 13290 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13291 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13292
3019eac3 13293static struct dwo_unit *
80626a55
DE
13294lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13295 const char *dwo_name, const char *comp_dir,
13296 ULONGEST signature, int is_debug_types)
3019eac3 13297{
ed2dc618 13298 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13299 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13300 const char *kind = is_debug_types ? "TU" : "CU";
13301 void **dwo_file_slot;
3019eac3 13302 struct dwo_file *dwo_file;
80626a55 13303 struct dwp_file *dwp_file;
cb1df416 13304
6a506a2d
DE
13305 /* First see if there's a DWP file.
13306 If we have a DWP file but didn't find the DWO inside it, don't
13307 look for the original DWO file. It makes gdb behave differently
13308 depending on whether one is debugging in the build tree. */
cf2c3c16 13309
ed2dc618 13310 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13311 if (dwp_file != NULL)
cf2c3c16 13312 {
80626a55
DE
13313 const struct dwp_hash_table *dwp_htab =
13314 is_debug_types ? dwp_file->tus : dwp_file->cus;
13315
13316 if (dwp_htab != NULL)
13317 {
13318 struct dwo_unit *dwo_cutu =
ed2dc618 13319 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13320 signature, is_debug_types);
80626a55
DE
13321
13322 if (dwo_cutu != NULL)
13323 {
b4f54984 13324 if (dwarf_read_debug)
80626a55
DE
13325 {
13326 fprintf_unfiltered (gdb_stdlog,
13327 "Virtual DWO %s %s found: @%s\n",
13328 kind, hex_string (signature),
13329 host_address_to_string (dwo_cutu));
13330 }
13331 return dwo_cutu;
13332 }
13333 }
13334 }
6a506a2d 13335 else
80626a55 13336 {
6a506a2d 13337 /* No DWP file, look for the DWO file. */
80626a55 13338
ed2dc618
SM
13339 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13340 dwo_name, comp_dir);
6a506a2d 13341 if (*dwo_file_slot == NULL)
80626a55 13342 {
6a506a2d
DE
13343 /* Read in the file and build a table of the CUs/TUs it contains. */
13344 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13345 }
6a506a2d 13346 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13347 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13348
6a506a2d 13349 if (dwo_file != NULL)
19c3d4c9 13350 {
6a506a2d
DE
13351 struct dwo_unit *dwo_cutu = NULL;
13352
13353 if (is_debug_types && dwo_file->tus)
13354 {
13355 struct dwo_unit find_dwo_cutu;
13356
13357 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13358 find_dwo_cutu.signature = signature;
9a3c8263
SM
13359 dwo_cutu
13360 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13361 }
33c5cd75 13362 else if (!is_debug_types && dwo_file->cus)
80626a55 13363 {
33c5cd75
DB
13364 struct dwo_unit find_dwo_cutu;
13365
13366 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13367 find_dwo_cutu.signature = signature;
13368 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13369 &find_dwo_cutu);
6a506a2d
DE
13370 }
13371
13372 if (dwo_cutu != NULL)
13373 {
b4f54984 13374 if (dwarf_read_debug)
6a506a2d
DE
13375 {
13376 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13377 kind, dwo_name, hex_string (signature),
13378 host_address_to_string (dwo_cutu));
13379 }
13380 return dwo_cutu;
80626a55
DE
13381 }
13382 }
2e276125 13383 }
9cdd5dbd 13384
80626a55
DE
13385 /* We didn't find it. This could mean a dwo_id mismatch, or
13386 someone deleted the DWO/DWP file, or the search path isn't set up
13387 correctly to find the file. */
13388
b4f54984 13389 if (dwarf_read_debug)
80626a55
DE
13390 {
13391 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13392 kind, dwo_name, hex_string (signature));
13393 }
3019eac3 13394
6656a72d
DE
13395 /* This is a warning and not a complaint because it can be caused by
13396 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13397 {
13398 /* Print the name of the DWP file if we looked there, helps the user
13399 better diagnose the problem. */
791afaa2 13400 std::string dwp_text;
43942612
DE
13401
13402 if (dwp_file != NULL)
791afaa2
TT
13403 dwp_text = string_printf (" [in DWP file %s]",
13404 lbasename (dwp_file->name));
43942612 13405
9d8780f0 13406 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13407 " [in module %s]"),
13408 kind, dwo_name, hex_string (signature),
791afaa2 13409 dwp_text.c_str (),
43942612 13410 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13411 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13412 }
3019eac3 13413 return NULL;
5fb290d7
DJ
13414}
13415
80626a55
DE
13416/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13417 See lookup_dwo_cutu_unit for details. */
13418
13419static struct dwo_unit *
13420lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13421 const char *dwo_name, const char *comp_dir,
13422 ULONGEST signature)
13423{
13424 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13425}
13426
13427/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13428 See lookup_dwo_cutu_unit for details. */
13429
13430static struct dwo_unit *
13431lookup_dwo_type_unit (struct signatured_type *this_tu,
13432 const char *dwo_name, const char *comp_dir)
13433{
13434 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13435}
13436
89e63ee4
DE
13437/* Traversal function for queue_and_load_all_dwo_tus. */
13438
13439static int
13440queue_and_load_dwo_tu (void **slot, void *info)
13441{
13442 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13443 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13444 ULONGEST signature = dwo_unit->signature;
13445 struct signatured_type *sig_type =
13446 lookup_dwo_signatured_type (per_cu->cu, signature);
13447
13448 if (sig_type != NULL)
13449 {
13450 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13451
13452 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13453 a real dependency of PER_CU on SIG_TYPE. That is detected later
13454 while processing PER_CU. */
13455 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13456 load_full_type_unit (sig_cu);
ae640021 13457 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13458 }
13459
13460 return 1;
13461}
13462
13463/* Queue all TUs contained in the DWO of PER_CU to be read in.
13464 The DWO may have the only definition of the type, though it may not be
13465 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13466 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13467
13468static void
13469queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13470{
13471 struct dwo_unit *dwo_unit;
13472 struct dwo_file *dwo_file;
13473
13474 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13475 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13476 gdb_assert (per_cu->cu != NULL);
13477
13478 dwo_unit = per_cu->cu->dwo_unit;
13479 gdb_assert (dwo_unit != NULL);
13480
13481 dwo_file = dwo_unit->dwo_file;
13482 if (dwo_file->tus != NULL)
13483 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13484}
13485
3019eac3 13486/* Read in various DIEs. */
348e048f 13487
d389af10 13488/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13489 Inherit only the children of the DW_AT_abstract_origin DIE not being
13490 already referenced by DW_AT_abstract_origin from the children of the
13491 current DIE. */
d389af10
JK
13492
13493static void
13494inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13495{
13496 struct die_info *child_die;
791afaa2 13497 sect_offset *offsetp;
d389af10
JK
13498 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13499 struct die_info *origin_die;
13500 /* Iterator of the ORIGIN_DIE children. */
13501 struct die_info *origin_child_die;
d389af10 13502 struct attribute *attr;
cd02d79d
PA
13503 struct dwarf2_cu *origin_cu;
13504 struct pending **origin_previous_list_in_scope;
d389af10
JK
13505
13506 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13507 if (!attr)
13508 return;
13509
cd02d79d
PA
13510 /* Note that following die references may follow to a die in a
13511 different cu. */
13512
13513 origin_cu = cu;
13514 origin_die = follow_die_ref (die, attr, &origin_cu);
13515
13516 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13517 symbols in. */
13518 origin_previous_list_in_scope = origin_cu->list_in_scope;
13519 origin_cu->list_in_scope = cu->list_in_scope;
13520
edb3359d
DJ
13521 if (die->tag != origin_die->tag
13522 && !(die->tag == DW_TAG_inlined_subroutine
13523 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13524 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13525 sect_offset_str (die->sect_off),
13526 sect_offset_str (origin_die->sect_off));
d389af10 13527
791afaa2 13528 std::vector<sect_offset> offsets;
d389af10 13529
3ea89b92
PMR
13530 for (child_die = die->child;
13531 child_die && child_die->tag;
13532 child_die = sibling_die (child_die))
13533 {
13534 struct die_info *child_origin_die;
13535 struct dwarf2_cu *child_origin_cu;
13536
13537 /* We are trying to process concrete instance entries:
216f72a1 13538 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13539 it's not relevant to our analysis here. i.e. detecting DIEs that are
13540 present in the abstract instance but not referenced in the concrete
13541 one. */
216f72a1
JK
13542 if (child_die->tag == DW_TAG_call_site
13543 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13544 continue;
13545
c38f313d
DJ
13546 /* For each CHILD_DIE, find the corresponding child of
13547 ORIGIN_DIE. If there is more than one layer of
13548 DW_AT_abstract_origin, follow them all; there shouldn't be,
13549 but GCC versions at least through 4.4 generate this (GCC PR
13550 40573). */
3ea89b92
PMR
13551 child_origin_die = child_die;
13552 child_origin_cu = cu;
c38f313d
DJ
13553 while (1)
13554 {
cd02d79d
PA
13555 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13556 child_origin_cu);
c38f313d
DJ
13557 if (attr == NULL)
13558 break;
cd02d79d
PA
13559 child_origin_die = follow_die_ref (child_origin_die, attr,
13560 &child_origin_cu);
c38f313d
DJ
13561 }
13562
d389af10
JK
13563 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13564 counterpart may exist. */
c38f313d 13565 if (child_origin_die != child_die)
d389af10 13566 {
edb3359d
DJ
13567 if (child_die->tag != child_origin_die->tag
13568 && !(child_die->tag == DW_TAG_inlined_subroutine
13569 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13570 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13571 "different tags"),
9d8780f0
SM
13572 sect_offset_str (child_die->sect_off),
13573 sect_offset_str (child_origin_die->sect_off));
c38f313d 13574 if (child_origin_die->parent != origin_die)
b98664d3 13575 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13576 "different parents"),
9d8780f0
SM
13577 sect_offset_str (child_die->sect_off),
13578 sect_offset_str (child_origin_die->sect_off));
c38f313d 13579 else
791afaa2 13580 offsets.push_back (child_origin_die->sect_off);
d389af10 13581 }
d389af10 13582 }
791afaa2
TT
13583 std::sort (offsets.begin (), offsets.end ());
13584 sect_offset *offsets_end = offsets.data () + offsets.size ();
13585 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13586 if (offsetp[-1] == *offsetp)
b98664d3 13587 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13588 "to DIE %s as their abstract origin"),
13589 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13590
791afaa2 13591 offsetp = offsets.data ();
d389af10
JK
13592 origin_child_die = origin_die->child;
13593 while (origin_child_die && origin_child_die->tag)
13594 {
13595 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13596 while (offsetp < offsets_end
9c541725 13597 && *offsetp < origin_child_die->sect_off)
d389af10 13598 offsetp++;
b64f50a1 13599 if (offsetp >= offsets_end
9c541725 13600 || *offsetp > origin_child_die->sect_off)
d389af10 13601 {
adde2bff
DE
13602 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13603 Check whether we're already processing ORIGIN_CHILD_DIE.
13604 This can happen with mutually referenced abstract_origins.
13605 PR 16581. */
13606 if (!origin_child_die->in_process)
13607 process_die (origin_child_die, origin_cu);
d389af10
JK
13608 }
13609 origin_child_die = sibling_die (origin_child_die);
13610 }
cd02d79d 13611 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13612
13613 if (cu != origin_cu)
13614 compute_delayed_physnames (origin_cu);
d389af10
JK
13615}
13616
c906108c 13617static void
e7c27a73 13618read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13619{
518817b3 13620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13621 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13622 struct context_stack *newobj;
c906108c
SS
13623 CORE_ADDR lowpc;
13624 CORE_ADDR highpc;
13625 struct die_info *child_die;
edb3359d 13626 struct attribute *attr, *call_line, *call_file;
15d034d0 13627 const char *name;
e142c38c 13628 CORE_ADDR baseaddr;
801e3a5b 13629 struct block *block;
edb3359d 13630 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13631 std::vector<struct symbol *> template_args;
34eaf542 13632 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13633
13634 if (inlined_func)
13635 {
13636 /* If we do not have call site information, we can't show the
13637 caller of this inlined function. That's too confusing, so
13638 only use the scope for local variables. */
13639 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13640 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13641 if (call_line == NULL || call_file == NULL)
13642 {
13643 read_lexical_block_scope (die, cu);
13644 return;
13645 }
13646 }
c906108c 13647
b3b3bada 13648 baseaddr = objfile->text_section_offset ();
e142c38c 13649
94af9270 13650 name = dwarf2_name (die, cu);
c906108c 13651
e8d05480
JB
13652 /* Ignore functions with missing or empty names. These are actually
13653 illegal according to the DWARF standard. */
13654 if (name == NULL)
13655 {
b98664d3 13656 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13657 sect_offset_str (die->sect_off));
e8d05480
JB
13658 return;
13659 }
13660
13661 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13662 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13663 <= PC_BOUNDS_INVALID)
e8d05480 13664 {
ae4d0c03
PM
13665 attr = dwarf2_attr (die, DW_AT_external, cu);
13666 if (!attr || !DW_UNSND (attr))
b98664d3 13667 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13668 "for subprogram DIE at %s"),
13669 sect_offset_str (die->sect_off));
e8d05480
JB
13670 return;
13671 }
c906108c 13672
3e29f34a
MR
13673 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13674 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13675
34eaf542
TT
13676 /* If we have any template arguments, then we must allocate a
13677 different sort of symbol. */
13678 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13679 {
13680 if (child_die->tag == DW_TAG_template_type_param
13681 || child_die->tag == DW_TAG_template_value_param)
13682 {
e623cf5d 13683 templ_func = allocate_template_symbol (objfile);
cf724bc9 13684 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13685 break;
13686 }
13687 }
13688
c24bdb02 13689 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13690 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13691 (struct symbol *) templ_func);
4c2df51b 13692
81873cc8 13693 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13694 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13695 cu->language);
13696
4cecd739
DJ
13697 /* If there is a location expression for DW_AT_frame_base, record
13698 it. */
e142c38c 13699 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13700 if (attr != nullptr)
fe978cb0 13701 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13702
63e43d3a
PMR
13703 /* If there is a location for the static link, record it. */
13704 newobj->static_link = NULL;
13705 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13706 if (attr != nullptr)
63e43d3a 13707 {
224c3ddb
SM
13708 newobj->static_link
13709 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13710 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13711 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13712 }
13713
c24bdb02 13714 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13715
639d11d3 13716 if (die->child != NULL)
c906108c 13717 {
639d11d3 13718 child_die = die->child;
c906108c
SS
13719 while (child_die && child_die->tag)
13720 {
34eaf542
TT
13721 if (child_die->tag == DW_TAG_template_type_param
13722 || child_die->tag == DW_TAG_template_value_param)
13723 {
13724 struct symbol *arg = new_symbol (child_die, NULL, cu);
13725
f1078f66 13726 if (arg != NULL)
2f4732b0 13727 template_args.push_back (arg);
34eaf542
TT
13728 }
13729 else
13730 process_die (child_die, cu);
c906108c
SS
13731 child_die = sibling_die (child_die);
13732 }
13733 }
13734
d389af10
JK
13735 inherit_abstract_dies (die, cu);
13736
4a811a97
UW
13737 /* If we have a DW_AT_specification, we might need to import using
13738 directives from the context of the specification DIE. See the
13739 comment in determine_prefix. */
13740 if (cu->language == language_cplus
13741 && dwarf2_attr (die, DW_AT_specification, cu))
13742 {
13743 struct dwarf2_cu *spec_cu = cu;
13744 struct die_info *spec_die = die_specification (die, &spec_cu);
13745
13746 while (spec_die)
13747 {
13748 child_die = spec_die->child;
13749 while (child_die && child_die->tag)
13750 {
13751 if (child_die->tag == DW_TAG_imported_module)
13752 process_die (child_die, spec_cu);
13753 child_die = sibling_die (child_die);
13754 }
13755
13756 /* In some cases, GCC generates specification DIEs that
13757 themselves contain DW_AT_specification attributes. */
13758 spec_die = die_specification (spec_die, &spec_cu);
13759 }
13760 }
13761
c24bdb02 13762 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13763 /* Make a block for the local symbols within. */
c24bdb02 13764 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13765 cstk.static_link, lowpc, highpc);
801e3a5b 13766
df8a16a1 13767 /* For C++, set the block's scope. */
45280282
IB
13768 if ((cu->language == language_cplus
13769 || cu->language == language_fortran
c44af4eb
TT
13770 || cu->language == language_d
13771 || cu->language == language_rust)
4d4ec4e5 13772 && cu->processing_has_namespace_info)
195a3f6c
TT
13773 block_set_scope (block, determine_prefix (die, cu),
13774 &objfile->objfile_obstack);
df8a16a1 13775
801e3a5b
JB
13776 /* If we have address ranges, record them. */
13777 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13778
a60f3166 13779 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13780
34eaf542 13781 /* Attach template arguments to function. */
2f4732b0 13782 if (!template_args.empty ())
34eaf542
TT
13783 {
13784 gdb_assert (templ_func != NULL);
13785
2f4732b0 13786 templ_func->n_template_arguments = template_args.size ();
34eaf542 13787 templ_func->template_arguments
8d749320
SM
13788 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13789 templ_func->n_template_arguments);
34eaf542 13790 memcpy (templ_func->template_arguments,
2f4732b0 13791 template_args.data (),
34eaf542 13792 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13793
13794 /* Make sure that the symtab is set on the new symbols. Even
13795 though they don't appear in this symtab directly, other parts
13796 of gdb assume that symbols do, and this is reasonably
13797 true. */
8634679f 13798 for (symbol *sym : template_args)
3e1d3d8c 13799 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13800 }
13801
208d8187
JB
13802 /* In C++, we can have functions nested inside functions (e.g., when
13803 a function declares a class that has methods). This means that
13804 when we finish processing a function scope, we may need to go
13805 back to building a containing block's symbol lists. */
c24bdb02
KS
13806 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13807 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13808
921e78cf
JB
13809 /* If we've finished processing a top-level function, subsequent
13810 symbols go in the file symbol list. */
c24bdb02
KS
13811 if (cu->get_builder ()->outermost_context_p ())
13812 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13813}
13814
13815/* Process all the DIES contained within a lexical block scope. Start
13816 a new scope, process the dies, and then close the scope. */
13817
13818static void
e7c27a73 13819read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13820{
518817b3 13821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13822 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13823 CORE_ADDR lowpc, highpc;
13824 struct die_info *child_die;
e142c38c
DJ
13825 CORE_ADDR baseaddr;
13826
b3b3bada 13827 baseaddr = objfile->text_section_offset ();
c906108c
SS
13828
13829 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13830 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13831 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13832 be nasty. Might be easier to properly extend generic blocks to
af34e669 13833 describe ranges. */
e385593e
JK
13834 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13835 {
13836 case PC_BOUNDS_NOT_PRESENT:
13837 /* DW_TAG_lexical_block has no attributes, process its children as if
13838 there was no wrapping by that DW_TAG_lexical_block.
13839 GCC does no longer produces such DWARF since GCC r224161. */
13840 for (child_die = die->child;
13841 child_die != NULL && child_die->tag;
13842 child_die = sibling_die (child_die))
13843 process_die (child_die, cu);
13844 return;
13845 case PC_BOUNDS_INVALID:
13846 return;
13847 }
3e29f34a
MR
13848 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13849 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13850
c24bdb02 13851 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13852 if (die->child != NULL)
c906108c 13853 {
639d11d3 13854 child_die = die->child;
c906108c
SS
13855 while (child_die && child_die->tag)
13856 {
e7c27a73 13857 process_die (child_die, cu);
c906108c
SS
13858 child_die = sibling_die (child_die);
13859 }
13860 }
3ea89b92 13861 inherit_abstract_dies (die, cu);
c24bdb02 13862 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13863
c24bdb02
KS
13864 if (*cu->get_builder ()->get_local_symbols () != NULL
13865 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13866 {
801e3a5b 13867 struct block *block
c24bdb02 13868 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13869 cstk.start_addr, highpc);
801e3a5b
JB
13870
13871 /* Note that recording ranges after traversing children, as we
13872 do here, means that recording a parent's ranges entails
13873 walking across all its children's ranges as they appear in
13874 the address map, which is quadratic behavior.
13875
13876 It would be nicer to record the parent's ranges before
13877 traversing its children, simply overriding whatever you find
13878 there. But since we don't even decide whether to create a
13879 block until after we've traversed its children, that's hard
13880 to do. */
13881 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13882 }
c24bdb02
KS
13883 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13884 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13885}
13886
216f72a1 13887/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13888
13889static void
13890read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13891{
518817b3 13892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13894 CORE_ADDR pc, baseaddr;
13895 struct attribute *attr;
13896 struct call_site *call_site, call_site_local;
13897 void **slot;
13898 int nparams;
13899 struct die_info *child_die;
13900
b3b3bada 13901 baseaddr = objfile->text_section_offset ();
96408a79 13902
216f72a1
JK
13903 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13904 if (attr == NULL)
13905 {
13906 /* This was a pre-DWARF-5 GNU extension alias
13907 for DW_AT_call_return_pc. */
13908 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13909 }
96408a79
SA
13910 if (!attr)
13911 {
b98664d3 13912 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13913 "DIE %s [in module %s]"),
13914 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13915 return;
13916 }
31aa7e4e 13917 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13918 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13919
13920 if (cu->call_site_htab == NULL)
13921 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13922 NULL, &objfile->objfile_obstack,
13923 hashtab_obstack_allocate, NULL);
13924 call_site_local.pc = pc;
13925 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13926 if (*slot != NULL)
13927 {
b98664d3 13928 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13929 "DIE %s [in module %s]"),
13930 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13931 objfile_name (objfile));
96408a79
SA
13932 return;
13933 }
13934
13935 /* Count parameters at the caller. */
13936
13937 nparams = 0;
13938 for (child_die = die->child; child_die && child_die->tag;
13939 child_die = sibling_die (child_die))
13940 {
216f72a1
JK
13941 if (child_die->tag != DW_TAG_call_site_parameter
13942 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13943 {
b98664d3 13944 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13945 "DW_TAG_call_site child DIE %s [in module %s]"),
13946 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13947 objfile_name (objfile));
96408a79
SA
13948 continue;
13949 }
13950
13951 nparams++;
13952 }
13953
224c3ddb
SM
13954 call_site
13955 = ((struct call_site *)
13956 obstack_alloc (&objfile->objfile_obstack,
13957 sizeof (*call_site)
13958 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13959 *slot = call_site;
13960 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13961 call_site->pc = pc;
13962
216f72a1
JK
13963 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13964 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13965 {
13966 struct die_info *func_die;
13967
13968 /* Skip also over DW_TAG_inlined_subroutine. */
13969 for (func_die = die->parent;
13970 func_die && func_die->tag != DW_TAG_subprogram
13971 && func_die->tag != DW_TAG_subroutine_type;
13972 func_die = func_die->parent);
13973
216f72a1
JK
13974 /* DW_AT_call_all_calls is a superset
13975 of DW_AT_call_all_tail_calls. */
96408a79 13976 if (func_die
216f72a1 13977 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13978 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13979 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13980 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13981 {
13982 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13983 not complete. But keep CALL_SITE for look ups via call_site_htab,
13984 both the initial caller containing the real return address PC and
13985 the final callee containing the current PC of a chain of tail
13986 calls do not need to have the tail call list complete. But any
13987 function candidate for a virtual tail call frame searched via
13988 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13989 determined unambiguously. */
13990 }
13991 else
13992 {
13993 struct type *func_type = NULL;
13994
13995 if (func_die)
13996 func_type = get_die_type (func_die, cu);
13997 if (func_type != NULL)
13998 {
13999 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14000
14001 /* Enlist this call site to the function. */
14002 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14003 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14004 }
14005 else
b98664d3 14006 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14007 "DIE %s [in module %s]"),
14008 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14009 }
14010 }
14011
216f72a1
JK
14012 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14013 if (attr == NULL)
14014 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14015 if (attr == NULL)
14016 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14017 if (attr == NULL)
216f72a1
JK
14018 {
14019 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14020 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14021 }
96408a79
SA
14022 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14023 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14024 /* Keep NULL DWARF_BLOCK. */;
14025 else if (attr_form_is_block (attr))
14026 {
14027 struct dwarf2_locexpr_baton *dlbaton;
14028
8d749320 14029 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14030 dlbaton->data = DW_BLOCK (attr)->data;
14031 dlbaton->size = DW_BLOCK (attr)->size;
14032 dlbaton->per_cu = cu->per_cu;
14033
14034 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14035 }
7771576e 14036 else if (attr_form_is_ref (attr))
96408a79 14037 {
96408a79
SA
14038 struct dwarf2_cu *target_cu = cu;
14039 struct die_info *target_die;
14040
ac9ec31b 14041 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14042 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14043 if (die_is_declaration (target_die, target_cu))
14044 {
7d45c7c3 14045 const char *target_physname;
9112db09
JK
14046
14047 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14048 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14049 if (target_physname == NULL)
9112db09 14050 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14051 if (target_physname == NULL)
b98664d3 14052 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14053 "physname, for referencing DIE %s [in module %s]"),
14054 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14055 else
7d455152 14056 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14057 }
14058 else
14059 {
14060 CORE_ADDR lowpc;
14061
14062 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14063 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14064 <= PC_BOUNDS_INVALID)
b98664d3 14065 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14066 "low pc, for referencing DIE %s [in module %s]"),
14067 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14068 else
3e29f34a
MR
14069 {
14070 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14071 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14072 }
96408a79
SA
14073 }
14074 }
14075 else
b98664d3 14076 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14077 "block nor reference, for DIE %s [in module %s]"),
14078 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14079
14080 call_site->per_cu = cu->per_cu;
14081
14082 for (child_die = die->child;
14083 child_die && child_die->tag;
14084 child_die = sibling_die (child_die))
14085 {
96408a79 14086 struct call_site_parameter *parameter;
1788b2d3 14087 struct attribute *loc, *origin;
96408a79 14088
216f72a1
JK
14089 if (child_die->tag != DW_TAG_call_site_parameter
14090 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14091 {
14092 /* Already printed the complaint above. */
14093 continue;
14094 }
14095
14096 gdb_assert (call_site->parameter_count < nparams);
14097 parameter = &call_site->parameter[call_site->parameter_count];
14098
1788b2d3
JK
14099 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14100 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14101 register is contained in DW_AT_call_value. */
96408a79 14102
24c5c679 14103 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14104 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14105 if (origin == NULL)
14106 {
14107 /* This was a pre-DWARF-5 GNU extension alias
14108 for DW_AT_call_parameter. */
14109 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14110 }
7771576e 14111 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14112 {
1788b2d3 14113 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14114
14115 sect_offset sect_off
14116 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14117 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14118 {
14119 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14120 binding can be done only inside one CU. Such referenced DIE
14121 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14122 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14123 "DW_TAG_call_site child DIE %s [in module %s]"),
14124 sect_offset_str (child_die->sect_off),
9c541725 14125 objfile_name (objfile));
d76b7dbc
JK
14126 continue;
14127 }
9c541725
PA
14128 parameter->u.param_cu_off
14129 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14130 }
14131 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14132 {
b98664d3 14133 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14134 "DW_TAG_call_site child DIE %s [in module %s]"),
14135 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14136 continue;
14137 }
24c5c679 14138 else
96408a79 14139 {
24c5c679
JK
14140 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14141 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14142 if (parameter->u.dwarf_reg != -1)
14143 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14144 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14145 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14146 &parameter->u.fb_offset))
14147 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14148 else
14149 {
b98664d3 14150 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14151 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14152 "DW_TAG_call_site child DIE %s "
24c5c679 14153 "[in module %s]"),
9d8780f0 14154 sect_offset_str (child_die->sect_off),
9c541725 14155 objfile_name (objfile));
24c5c679
JK
14156 continue;
14157 }
96408a79
SA
14158 }
14159
216f72a1
JK
14160 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14161 if (attr == NULL)
14162 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14163 if (!attr_form_is_block (attr))
14164 {
b98664d3 14165 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14166 "DW_TAG_call_site child DIE %s [in module %s]"),
14167 sect_offset_str (child_die->sect_off),
9c541725 14168 objfile_name (objfile));
96408a79
SA
14169 continue;
14170 }
14171 parameter->value = DW_BLOCK (attr)->data;
14172 parameter->value_size = DW_BLOCK (attr)->size;
14173
14174 /* Parameters are not pre-cleared by memset above. */
14175 parameter->data_value = NULL;
14176 parameter->data_value_size = 0;
14177 call_site->parameter_count++;
14178
216f72a1
JK
14179 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14180 if (attr == NULL)
14181 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14182 if (attr != nullptr)
96408a79
SA
14183 {
14184 if (!attr_form_is_block (attr))
b98664d3 14185 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14186 "DW_TAG_call_site child DIE %s [in module %s]"),
14187 sect_offset_str (child_die->sect_off),
9c541725 14188 objfile_name (objfile));
96408a79
SA
14189 else
14190 {
14191 parameter->data_value = DW_BLOCK (attr)->data;
14192 parameter->data_value_size = DW_BLOCK (attr)->size;
14193 }
14194 }
14195 }
14196}
14197
71a3c369
TT
14198/* Helper function for read_variable. If DIE represents a virtual
14199 table, then return the type of the concrete object that is
14200 associated with the virtual table. Otherwise, return NULL. */
14201
14202static struct type *
14203rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14204{
14205 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14206 if (attr == NULL)
14207 return NULL;
14208
14209 /* Find the type DIE. */
14210 struct die_info *type_die = NULL;
14211 struct dwarf2_cu *type_cu = cu;
14212
14213 if (attr_form_is_ref (attr))
14214 type_die = follow_die_ref (die, attr, &type_cu);
14215 if (type_die == NULL)
14216 return NULL;
14217
14218 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14219 return NULL;
14220 return die_containing_type (type_die, type_cu);
14221}
14222
14223/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14224
14225static void
14226read_variable (struct die_info *die, struct dwarf2_cu *cu)
14227{
14228 struct rust_vtable_symbol *storage = NULL;
14229
14230 if (cu->language == language_rust)
14231 {
14232 struct type *containing_type = rust_containing_type (die, cu);
14233
14234 if (containing_type != NULL)
14235 {
518817b3 14236 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14237
468c0cbb 14238 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14239 initialize_objfile_symbol (storage);
14240 storage->concrete_type = containing_type;
cf724bc9 14241 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14242 }
14243 }
14244
e4a62c65
TV
14245 struct symbol *res = new_symbol (die, NULL, cu, storage);
14246 struct attribute *abstract_origin
14247 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14248 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14249 if (res == NULL && loc && abstract_origin)
14250 {
14251 /* We have a variable without a name, but with a location and an abstract
14252 origin. This may be a concrete instance of an abstract variable
14253 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14254 later. */
14255 struct dwarf2_cu *origin_cu = cu;
14256 struct die_info *origin_die
14257 = follow_die_ref (die, abstract_origin, &origin_cu);
14258 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14259 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14260 }
71a3c369
TT
14261}
14262
43988095
JK
14263/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14264 reading .debug_rnglists.
14265 Callback's type should be:
14266 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14267 Return true if the attributes are present and valid, otherwise,
14268 return false. */
14269
14270template <typename Callback>
14271static bool
14272dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14273 Callback &&callback)
14274{
ed2dc618 14275 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14276 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14277 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14278 bfd *obfd = objfile->obfd;
43988095
JK
14279 /* Base address selection entry. */
14280 CORE_ADDR base;
14281 int found_base;
43988095 14282 const gdb_byte *buffer;
43988095
JK
14283 CORE_ADDR baseaddr;
14284 bool overflow = false;
14285
14286 found_base = cu->base_known;
14287 base = cu->base_address;
14288
14289 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14290 if (offset >= dwarf2_per_objfile->rnglists.size)
14291 {
b98664d3 14292 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14293 offset);
14294 return false;
14295 }
14296 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14297
b3b3bada 14298 baseaddr = objfile->text_section_offset ();
43988095
JK
14299
14300 while (1)
14301 {
7814882a
JK
14302 /* Initialize it due to a false compiler warning. */
14303 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14304 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14305 + dwarf2_per_objfile->rnglists.size);
14306 unsigned int bytes_read;
14307
14308 if (buffer == buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14314 switch (rlet)
14315 {
14316 case DW_RLE_end_of_list:
14317 break;
14318 case DW_RLE_base_address:
14319 if (buffer + cu->header.addr_size > buf_end)
14320 {
14321 overflow = true;
14322 break;
14323 }
14324 base = read_address (obfd, buffer, cu, &bytes_read);
14325 found_base = 1;
14326 buffer += bytes_read;
14327 break;
14328 case DW_RLE_start_length:
14329 if (buffer + cu->header.addr_size > buf_end)
14330 {
14331 overflow = true;
14332 break;
14333 }
14334 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14335 buffer += bytes_read;
14336 range_end = (range_beginning
14337 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14338 buffer += bytes_read;
14339 if (buffer > buf_end)
14340 {
14341 overflow = true;
14342 break;
14343 }
14344 break;
14345 case DW_RLE_offset_pair:
14346 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14347 buffer += bytes_read;
14348 if (buffer > buf_end)
14349 {
14350 overflow = true;
14351 break;
14352 }
14353 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14354 buffer += bytes_read;
14355 if (buffer > buf_end)
14356 {
14357 overflow = true;
14358 break;
14359 }
14360 break;
14361 case DW_RLE_start_end:
14362 if (buffer + 2 * cu->header.addr_size > buf_end)
14363 {
14364 overflow = true;
14365 break;
14366 }
14367 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14368 buffer += bytes_read;
14369 range_end = read_address (obfd, buffer, cu, &bytes_read);
14370 buffer += bytes_read;
14371 break;
14372 default:
b98664d3 14373 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14374 return false;
14375 }
14376 if (rlet == DW_RLE_end_of_list || overflow)
14377 break;
14378 if (rlet == DW_RLE_base_address)
14379 continue;
14380
14381 if (!found_base)
14382 {
14383 /* We have no valid base address for the ranges
14384 data. */
b98664d3 14385 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14386 return false;
14387 }
14388
14389 if (range_beginning > range_end)
14390 {
14391 /* Inverted range entries are invalid. */
b98664d3 14392 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14393 return false;
14394 }
14395
14396 /* Empty range entries have no effect. */
14397 if (range_beginning == range_end)
14398 continue;
14399
14400 range_beginning += base;
14401 range_end += base;
14402
14403 /* A not-uncommon case of bad debug info.
14404 Don't pollute the addrmap with bad data. */
14405 if (range_beginning + baseaddr == 0
14406 && !dwarf2_per_objfile->has_section_at_zero)
14407 {
b98664d3 14408 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14409 " [in module %s]"), objfile_name (objfile));
14410 continue;
14411 }
14412
14413 callback (range_beginning, range_end);
14414 }
14415
14416 if (overflow)
14417 {
b98664d3 14418 complaint (_("Offset %d is not terminated "
43988095
JK
14419 "for DW_AT_ranges attribute"),
14420 offset);
14421 return false;
14422 }
14423
14424 return true;
14425}
14426
14427/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14428 Callback's type should be:
14429 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14430 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14431
43988095 14432template <typename Callback>
43039443 14433static int
5f46c5a5 14434dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14435 Callback &&callback)
43039443 14436{
ed2dc618 14437 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14438 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14439 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14440 struct comp_unit_head *cu_header = &cu->header;
14441 bfd *obfd = objfile->obfd;
14442 unsigned int addr_size = cu_header->addr_size;
14443 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14444 /* Base address selection entry. */
14445 CORE_ADDR base;
14446 int found_base;
14447 unsigned int dummy;
d521ce57 14448 const gdb_byte *buffer;
ff013f42 14449 CORE_ADDR baseaddr;
43039443 14450
43988095
JK
14451 if (cu_header->version >= 5)
14452 return dwarf2_rnglists_process (offset, cu, callback);
14453
d00adf39
DE
14454 found_base = cu->base_known;
14455 base = cu->base_address;
43039443 14456
be391dca 14457 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14458 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14459 {
b98664d3 14460 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14461 offset);
14462 return 0;
14463 }
dce234bc 14464 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14465
b3b3bada 14466 baseaddr = objfile->text_section_offset ();
ff013f42 14467
43039443
JK
14468 while (1)
14469 {
14470 CORE_ADDR range_beginning, range_end;
14471
14472 range_beginning = read_address (obfd, buffer, cu, &dummy);
14473 buffer += addr_size;
14474 range_end = read_address (obfd, buffer, cu, &dummy);
14475 buffer += addr_size;
14476 offset += 2 * addr_size;
14477
14478 /* An end of list marker is a pair of zero addresses. */
14479 if (range_beginning == 0 && range_end == 0)
14480 /* Found the end of list entry. */
14481 break;
14482
14483 /* Each base address selection entry is a pair of 2 values.
14484 The first is the largest possible address, the second is
14485 the base address. Check for a base address here. */
14486 if ((range_beginning & mask) == mask)
14487 {
28d2bfb9
AB
14488 /* If we found the largest possible address, then we already
14489 have the base address in range_end. */
14490 base = range_end;
43039443
JK
14491 found_base = 1;
14492 continue;
14493 }
14494
14495 if (!found_base)
14496 {
14497 /* We have no valid base address for the ranges
14498 data. */
b98664d3 14499 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14500 return 0;
14501 }
14502
9277c30c
UW
14503 if (range_beginning > range_end)
14504 {
14505 /* Inverted range entries are invalid. */
b98664d3 14506 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14507 return 0;
14508 }
14509
14510 /* Empty range entries have no effect. */
14511 if (range_beginning == range_end)
14512 continue;
14513
43039443
JK
14514 range_beginning += base;
14515 range_end += base;
14516
01093045
DE
14517 /* A not-uncommon case of bad debug info.
14518 Don't pollute the addrmap with bad data. */
14519 if (range_beginning + baseaddr == 0
14520 && !dwarf2_per_objfile->has_section_at_zero)
14521 {
b98664d3 14522 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14523 " [in module %s]"), objfile_name (objfile));
01093045
DE
14524 continue;
14525 }
14526
5f46c5a5
JK
14527 callback (range_beginning, range_end);
14528 }
14529
14530 return 1;
14531}
14532
14533/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14534 Return 1 if the attributes are present and valid, otherwise, return 0.
14535 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14536
14537static int
14538dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14539 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14540 dwarf2_psymtab *ranges_pst)
5f46c5a5 14541{
518817b3 14542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14544 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14545 int low_set = 0;
14546 CORE_ADDR low = 0;
14547 CORE_ADDR high = 0;
14548 int retval;
14549
14550 retval = dwarf2_ranges_process (offset, cu,
14551 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14552 {
9277c30c 14553 if (ranges_pst != NULL)
3e29f34a
MR
14554 {
14555 CORE_ADDR lowpc;
14556 CORE_ADDR highpc;
14557
79748972
TT
14558 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14559 range_beginning + baseaddr)
14560 - baseaddr);
14561 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14562 range_end + baseaddr)
14563 - baseaddr);
d320c2b5
TT
14564 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14565 lowpc, highpc - 1, ranges_pst);
3e29f34a 14566 }
ff013f42 14567
43039443
JK
14568 /* FIXME: This is recording everything as a low-high
14569 segment of consecutive addresses. We should have a
14570 data structure for discontiguous block ranges
14571 instead. */
14572 if (! low_set)
14573 {
14574 low = range_beginning;
14575 high = range_end;
14576 low_set = 1;
14577 }
14578 else
14579 {
14580 if (range_beginning < low)
14581 low = range_beginning;
14582 if (range_end > high)
14583 high = range_end;
14584 }
5f46c5a5
JK
14585 });
14586 if (!retval)
14587 return 0;
43039443
JK
14588
14589 if (! low_set)
14590 /* If the first entry is an end-of-list marker, the range
14591 describes an empty scope, i.e. no instructions. */
14592 return 0;
14593
14594 if (low_return)
14595 *low_return = low;
14596 if (high_return)
14597 *high_return = high;
14598 return 1;
14599}
14600
3a2b436a
JK
14601/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14602 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14603 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14604
3a2b436a 14605static enum pc_bounds_kind
af34e669 14606dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14607 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14608 dwarf2_psymtab *pst)
c906108c 14609{
518817b3
SM
14610 struct dwarf2_per_objfile *dwarf2_per_objfile
14611 = cu->per_cu->dwarf2_per_objfile;
c906108c 14612 struct attribute *attr;
91da1414 14613 struct attribute *attr_high;
af34e669
DJ
14614 CORE_ADDR low = 0;
14615 CORE_ADDR high = 0;
e385593e 14616 enum pc_bounds_kind ret;
c906108c 14617
91da1414
MW
14618 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14619 if (attr_high)
af34e669 14620 {
e142c38c 14621 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14622 if (attr != nullptr)
91da1414 14623 {
31aa7e4e
JB
14624 low = attr_value_as_address (attr);
14625 high = attr_value_as_address (attr_high);
14626 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14627 high += low;
91da1414 14628 }
af34e669
DJ
14629 else
14630 /* Found high w/o low attribute. */
e385593e 14631 return PC_BOUNDS_INVALID;
af34e669
DJ
14632
14633 /* Found consecutive range of addresses. */
3a2b436a 14634 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14635 }
c906108c 14636 else
af34e669 14637 {
e142c38c 14638 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14639 if (attr != NULL)
14640 {
18a8505e 14641 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14642 We take advantage of the fact that DW_AT_ranges does not appear
14643 in DW_TAG_compile_unit of DWO files. */
14644 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14645 unsigned int ranges_offset = (DW_UNSND (attr)
14646 + (need_ranges_base
14647 ? cu->ranges_base
14648 : 0));
2e3cf129 14649
af34e669 14650 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14651 .debug_ranges section. */
2e3cf129 14652 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14653 return PC_BOUNDS_INVALID;
43039443 14654 /* Found discontinuous range of addresses. */
3a2b436a 14655 ret = PC_BOUNDS_RANGES;
af34e669 14656 }
e385593e
JK
14657 else
14658 return PC_BOUNDS_NOT_PRESENT;
af34e669 14659 }
c906108c 14660
48fbe735 14661 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14662 if (high <= low)
e385593e 14663 return PC_BOUNDS_INVALID;
c906108c
SS
14664
14665 /* When using the GNU linker, .gnu.linkonce. sections are used to
14666 eliminate duplicate copies of functions and vtables and such.
14667 The linker will arbitrarily choose one and discard the others.
14668 The AT_*_pc values for such functions refer to local labels in
14669 these sections. If the section from that file was discarded, the
14670 labels are not in the output, so the relocs get a value of 0.
14671 If this is a discarded function, mark the pc bounds as invalid,
14672 so that GDB will ignore it. */
72dca2f5 14673 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14674 return PC_BOUNDS_INVALID;
c906108c
SS
14675
14676 *lowpc = low;
96408a79
SA
14677 if (highpc)
14678 *highpc = high;
af34e669 14679 return ret;
c906108c
SS
14680}
14681
b084d499
JB
14682/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14683 its low and high PC addresses. Do nothing if these addresses could not
14684 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14685 and HIGHPC to the high address if greater than HIGHPC. */
14686
14687static void
14688dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14689 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14690 struct dwarf2_cu *cu)
14691{
14692 CORE_ADDR low, high;
14693 struct die_info *child = die->child;
14694
e385593e 14695 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14696 {
325fac50
PA
14697 *lowpc = std::min (*lowpc, low);
14698 *highpc = std::max (*highpc, high);
b084d499
JB
14699 }
14700
14701 /* If the language does not allow nested subprograms (either inside
14702 subprograms or lexical blocks), we're done. */
14703 if (cu->language != language_ada)
14704 return;
6e70227d 14705
b084d499
JB
14706 /* Check all the children of the given DIE. If it contains nested
14707 subprograms, then check their pc bounds. Likewise, we need to
14708 check lexical blocks as well, as they may also contain subprogram
14709 definitions. */
14710 while (child && child->tag)
14711 {
14712 if (child->tag == DW_TAG_subprogram
14713 || child->tag == DW_TAG_lexical_block)
14714 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14715 child = sibling_die (child);
14716 }
14717}
14718
fae299cd
DC
14719/* Get the low and high pc's represented by the scope DIE, and store
14720 them in *LOWPC and *HIGHPC. If the correct values can't be
14721 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14722
14723static void
14724get_scope_pc_bounds (struct die_info *die,
14725 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14726 struct dwarf2_cu *cu)
14727{
14728 CORE_ADDR best_low = (CORE_ADDR) -1;
14729 CORE_ADDR best_high = (CORE_ADDR) 0;
14730 CORE_ADDR current_low, current_high;
14731
3a2b436a 14732 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14733 >= PC_BOUNDS_RANGES)
fae299cd
DC
14734 {
14735 best_low = current_low;
14736 best_high = current_high;
14737 }
14738 else
14739 {
14740 struct die_info *child = die->child;
14741
14742 while (child && child->tag)
14743 {
14744 switch (child->tag) {
14745 case DW_TAG_subprogram:
b084d499 14746 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14747 break;
14748 case DW_TAG_namespace:
f55ee35c 14749 case DW_TAG_module:
fae299cd
DC
14750 /* FIXME: carlton/2004-01-16: Should we do this for
14751 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14752 that current GCC's always emit the DIEs corresponding
14753 to definitions of methods of classes as children of a
14754 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14755 the DIEs giving the declarations, which could be
14756 anywhere). But I don't see any reason why the
14757 standards says that they have to be there. */
14758 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14759
14760 if (current_low != ((CORE_ADDR) -1))
14761 {
325fac50
PA
14762 best_low = std::min (best_low, current_low);
14763 best_high = std::max (best_high, current_high);
fae299cd
DC
14764 }
14765 break;
14766 default:
0963b4bd 14767 /* Ignore. */
fae299cd
DC
14768 break;
14769 }
14770
14771 child = sibling_die (child);
14772 }
14773 }
14774
14775 *lowpc = best_low;
14776 *highpc = best_high;
14777}
14778
801e3a5b
JB
14779/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14780 in DIE. */
380bca97 14781
801e3a5b
JB
14782static void
14783dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14784 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14785{
518817b3 14786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14787 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14788 struct attribute *attr;
91da1414 14789 struct attribute *attr_high;
801e3a5b 14790
91da1414
MW
14791 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14792 if (attr_high)
801e3a5b 14793 {
801e3a5b 14794 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14795 if (attr != nullptr)
801e3a5b 14796 {
31aa7e4e
JB
14797 CORE_ADDR low = attr_value_as_address (attr);
14798 CORE_ADDR high = attr_value_as_address (attr_high);
14799
14800 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14801 high += low;
9a619af0 14802
3e29f34a
MR
14803 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14804 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14805 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14806 }
14807 }
14808
14809 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14810 if (attr != nullptr)
801e3a5b 14811 {
18a8505e 14812 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14813 We take advantage of the fact that DW_AT_ranges does not appear
14814 in DW_TAG_compile_unit of DWO files. */
14815 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14816
14817 /* The value of the DW_AT_ranges attribute is the offset of the
14818 address range list in the .debug_ranges section. */
ab435259
DE
14819 unsigned long offset = (DW_UNSND (attr)
14820 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14821
2d5f09ec 14822 std::vector<blockrange> blockvec;
5f46c5a5
JK
14823 dwarf2_ranges_process (offset, cu,
14824 [&] (CORE_ADDR start, CORE_ADDR end)
14825 {
58fdfd2c
JK
14826 start += baseaddr;
14827 end += baseaddr;
5f46c5a5
JK
14828 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14829 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14830 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14831 blockvec.emplace_back (start, end);
5f46c5a5 14832 });
2d5f09ec
KB
14833
14834 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14835 }
14836}
14837
685b1105
JK
14838/* Check whether the producer field indicates either of GCC < 4.6, or the
14839 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14840
685b1105
JK
14841static void
14842check_producer (struct dwarf2_cu *cu)
60d5a603 14843{
38360086 14844 int major, minor;
60d5a603
JK
14845
14846 if (cu->producer == NULL)
14847 {
14848 /* For unknown compilers expect their behavior is DWARF version
14849 compliant.
14850
14851 GCC started to support .debug_types sections by -gdwarf-4 since
14852 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14853 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14854 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14855 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14856 }
b1ffba5a 14857 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14858 {
38360086
MW
14859 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14860 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14861 }
5230b05a 14862 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14863 {
14864 cu->producer_is_icc = true;
14865 cu->producer_is_icc_lt_14 = major < 14;
14866 }
c258c396
JD
14867 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14868 cu->producer_is_codewarrior = true;
685b1105
JK
14869 else
14870 {
14871 /* For other non-GCC compilers, expect their behavior is DWARF version
14872 compliant. */
60d5a603
JK
14873 }
14874
9068261f 14875 cu->checked_producer = true;
685b1105 14876}
ba919b58 14877
685b1105
JK
14878/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14879 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14880 during 4.6.0 experimental. */
14881
9068261f 14882static bool
685b1105
JK
14883producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14884{
14885 if (!cu->checked_producer)
14886 check_producer (cu);
14887
14888 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14889}
14890
c258c396
JD
14891
14892/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14893 with incorrect is_stmt attributes. */
14894
14895static bool
14896producer_is_codewarrior (struct dwarf2_cu *cu)
14897{
14898 if (!cu->checked_producer)
14899 check_producer (cu);
14900
14901 return cu->producer_is_codewarrior;
14902}
14903
405feb71 14904/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14905 DW_AT_accessibility. */
14906
14907static enum dwarf_access_attribute
14908dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14909{
14910 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14911 {
14912 /* The default DWARF 2 accessibility for members is public, the default
14913 accessibility for inheritance is private. */
14914
14915 if (die->tag != DW_TAG_inheritance)
14916 return DW_ACCESS_public;
14917 else
14918 return DW_ACCESS_private;
14919 }
14920 else
14921 {
14922 /* DWARF 3+ defines the default accessibility a different way. The same
14923 rules apply now for DW_TAG_inheritance as for the members and it only
14924 depends on the container kind. */
14925
14926 if (die->parent->tag == DW_TAG_class_type)
14927 return DW_ACCESS_private;
14928 else
14929 return DW_ACCESS_public;
14930 }
14931}
14932
74ac6d43
TT
14933/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14934 offset. If the attribute was not found return 0, otherwise return
14935 1. If it was found but could not properly be handled, set *OFFSET
14936 to 0. */
14937
14938static int
14939handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14940 LONGEST *offset)
14941{
14942 struct attribute *attr;
14943
14944 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14945 if (attr != NULL)
14946 {
14947 *offset = 0;
14948
14949 /* Note that we do not check for a section offset first here.
14950 This is because DW_AT_data_member_location is new in DWARF 4,
14951 so if we see it, we can assume that a constant form is really
14952 a constant and not a section offset. */
14953 if (attr_form_is_constant (attr))
14954 *offset = dwarf2_get_attr_constant_value (attr, 0);
14955 else if (attr_form_is_section_offset (attr))
14956 dwarf2_complex_location_expr_complaint ();
14957 else if (attr_form_is_block (attr))
14958 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14959 else
14960 dwarf2_complex_location_expr_complaint ();
14961
14962 return 1;
14963 }
14964
14965 return 0;
14966}
14967
c906108c
SS
14968/* Add an aggregate field to the field list. */
14969
14970static void
107d2387 14971dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14972 struct dwarf2_cu *cu)
6e70227d 14973{
518817b3 14974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14975 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14976 struct nextfield *new_field;
14977 struct attribute *attr;
14978 struct field *fp;
15d034d0 14979 const char *fieldname = "";
c906108c 14980
7d0ccb61
DJ
14981 if (die->tag == DW_TAG_inheritance)
14982 {
be2daae6
TT
14983 fip->baseclasses.emplace_back ();
14984 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14985 }
14986 else
14987 {
be2daae6
TT
14988 fip->fields.emplace_back ();
14989 new_field = &fip->fields.back ();
7d0ccb61 14990 }
be2daae6 14991
c906108c
SS
14992 fip->nfields++;
14993
e142c38c 14994 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14995 if (attr != nullptr)
c906108c 14996 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14997 else
14998 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14999 if (new_field->accessibility != DW_ACCESS_public)
15000 fip->non_public_fields = 1;
60d5a603 15001
e142c38c 15002 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15003 if (attr != nullptr)
c906108c 15004 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15005 else
15006 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15007
15008 fp = &new_field->field;
a9a9bd0f 15009
e142c38c 15010 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15011 {
74ac6d43
TT
15012 LONGEST offset;
15013
a9a9bd0f 15014 /* Data member other than a C++ static data member. */
6e70227d 15015
c906108c 15016 /* Get type of field. */
e7c27a73 15017 fp->type = die_type (die, cu);
c906108c 15018
d6a843b5 15019 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15020
c906108c 15021 /* Get bit size of field (zero if none). */
e142c38c 15022 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15023 if (attr != nullptr)
c906108c
SS
15024 {
15025 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15026 }
15027 else
15028 {
15029 FIELD_BITSIZE (*fp) = 0;
15030 }
15031
15032 /* Get bit offset of field. */
74ac6d43
TT
15033 if (handle_data_member_location (die, cu, &offset))
15034 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15035 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15036 if (attr != nullptr)
c906108c 15037 {
d5a22e77 15038 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15039 {
15040 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15041 additional bit offset from the MSB of the containing
15042 anonymous object to the MSB of the field. We don't
15043 have to do anything special since we don't need to
15044 know the size of the anonymous object. */
f41f5e61 15045 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15046 }
15047 else
15048 {
15049 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15050 MSB of the anonymous object, subtract off the number of
15051 bits from the MSB of the field to the MSB of the
15052 object, and then subtract off the number of bits of
15053 the field itself. The result is the bit offset of
15054 the LSB of the field. */
c906108c
SS
15055 int anonymous_size;
15056 int bit_offset = DW_UNSND (attr);
15057
e142c38c 15058 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15059 if (attr != nullptr)
c906108c
SS
15060 {
15061 /* The size of the anonymous object containing
15062 the bit field is explicit, so use the
15063 indicated size (in bytes). */
15064 anonymous_size = DW_UNSND (attr);
15065 }
15066 else
15067 {
15068 /* The size of the anonymous object containing
15069 the bit field must be inferred from the type
15070 attribute of the data member containing the
15071 bit field. */
15072 anonymous_size = TYPE_LENGTH (fp->type);
15073 }
f41f5e61
PA
15074 SET_FIELD_BITPOS (*fp,
15075 (FIELD_BITPOS (*fp)
15076 + anonymous_size * bits_per_byte
15077 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15078 }
15079 }
da5b30da
AA
15080 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15081 if (attr != NULL)
15082 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15083 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15084
15085 /* Get name of field. */
39cbfefa
DJ
15086 fieldname = dwarf2_name (die, cu);
15087 if (fieldname == NULL)
15088 fieldname = "";
d8151005
DJ
15089
15090 /* The name is already allocated along with this objfile, so we don't
15091 need to duplicate it for the type. */
15092 fp->name = fieldname;
c906108c
SS
15093
15094 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15095 pointer or virtual base class pointer) to private. */
e142c38c 15096 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15097 {
d48cc9dd 15098 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15099 new_field->accessibility = DW_ACCESS_private;
15100 fip->non_public_fields = 1;
15101 }
15102 }
a9a9bd0f 15103 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15104 {
a9a9bd0f
DC
15105 /* C++ static member. */
15106
15107 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15108 is a declaration, but all versions of G++ as of this writing
15109 (so through at least 3.2.1) incorrectly generate
15110 DW_TAG_variable tags. */
6e70227d 15111
ff355380 15112 const char *physname;
c906108c 15113
a9a9bd0f 15114 /* Get name of field. */
39cbfefa
DJ
15115 fieldname = dwarf2_name (die, cu);
15116 if (fieldname == NULL)
c906108c
SS
15117 return;
15118
254e6b9e 15119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15120 if (attr
15121 /* Only create a symbol if this is an external value.
15122 new_symbol checks this and puts the value in the global symbol
15123 table, which we want. If it is not external, new_symbol
15124 will try to put the value in cu->list_in_scope which is wrong. */
15125 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15126 {
15127 /* A static const member, not much different than an enum as far as
15128 we're concerned, except that we can support more types. */
15129 new_symbol (die, NULL, cu);
15130 }
15131
2df3850c 15132 /* Get physical name. */
ff355380 15133 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15134
d8151005
DJ
15135 /* The name is already allocated along with this objfile, so we don't
15136 need to duplicate it for the type. */
15137 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15138 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15139 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15140 }
15141 else if (die->tag == DW_TAG_inheritance)
15142 {
74ac6d43 15143 LONGEST offset;
d4b96c9a 15144
74ac6d43
TT
15145 /* C++ base class field. */
15146 if (handle_data_member_location (die, cu, &offset))
15147 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15148 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15149 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15150 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15151 }
2ddeaf8a
TT
15152 else if (die->tag == DW_TAG_variant_part)
15153 {
15154 /* process_structure_scope will treat this DIE as a union. */
15155 process_structure_scope (die, cu);
15156
15157 /* The variant part is relative to the start of the enclosing
15158 structure. */
15159 SET_FIELD_BITPOS (*fp, 0);
15160 fp->type = get_die_type (die, cu);
15161 fp->artificial = 1;
15162 fp->name = "<<variant>>";
c8c81635
TT
15163
15164 /* Normally a DW_TAG_variant_part won't have a size, but our
15165 representation requires one, so set it to the maximum of the
489dbda6
TT
15166 child sizes, being sure to account for the offset at which
15167 each child is seen. */
c8c81635
TT
15168 if (TYPE_LENGTH (fp->type) == 0)
15169 {
15170 unsigned max = 0;
15171 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15172 {
15173 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15174 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15175 if (len > max)
15176 max = len;
15177 }
c8c81635
TT
15178 TYPE_LENGTH (fp->type) = max;
15179 }
2ddeaf8a
TT
15180 }
15181 else
15182 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15183}
15184
883fd55a
KS
15185/* Can the type given by DIE define another type? */
15186
15187static bool
15188type_can_define_types (const struct die_info *die)
15189{
15190 switch (die->tag)
15191 {
15192 case DW_TAG_typedef:
15193 case DW_TAG_class_type:
15194 case DW_TAG_structure_type:
15195 case DW_TAG_union_type:
15196 case DW_TAG_enumeration_type:
15197 return true;
15198
15199 default:
15200 return false;
15201 }
15202}
15203
15204/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15205
15206static void
883fd55a
KS
15207dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15208 struct dwarf2_cu *cu)
6e70227d 15209{
be2daae6
TT
15210 struct decl_field fp;
15211 memset (&fp, 0, sizeof (fp));
98751a41 15212
883fd55a 15213 gdb_assert (type_can_define_types (die));
98751a41 15214
883fd55a 15215 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15216 fp.name = dwarf2_name (die, cu);
15217 fp.type = read_type_die (die, cu);
98751a41 15218
c191a687
KS
15219 /* Save accessibility. */
15220 enum dwarf_access_attribute accessibility;
15221 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15222 if (attr != NULL)
15223 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15224 else
15225 accessibility = dwarf2_default_access_attribute (die, cu);
15226 switch (accessibility)
15227 {
15228 case DW_ACCESS_public:
15229 /* The assumed value if neither private nor protected. */
15230 break;
15231 case DW_ACCESS_private:
be2daae6 15232 fp.is_private = 1;
c191a687
KS
15233 break;
15234 case DW_ACCESS_protected:
be2daae6 15235 fp.is_protected = 1;
c191a687
KS
15236 break;
15237 default:
b98664d3 15238 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15239 }
15240
883fd55a 15241 if (die->tag == DW_TAG_typedef)
be2daae6 15242 fip->typedef_field_list.push_back (fp);
883fd55a 15243 else
be2daae6 15244 fip->nested_types_list.push_back (fp);
98751a41
JK
15245}
15246
c906108c
SS
15247/* Create the vector of fields, and attach it to the type. */
15248
15249static void
fba45db2 15250dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15251 struct dwarf2_cu *cu)
c906108c
SS
15252{
15253 int nfields = fip->nfields;
15254
15255 /* Record the field count, allocate space for the array of fields,
15256 and create blank accessibility bitfields if necessary. */
15257 TYPE_NFIELDS (type) = nfields;
15258 TYPE_FIELDS (type) = (struct field *)
be2daae6 15259 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15260
b4ba55a1 15261 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15262 {
15263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15264
15265 TYPE_FIELD_PRIVATE_BITS (type) =
15266 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15267 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15268
15269 TYPE_FIELD_PROTECTED_BITS (type) =
15270 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15271 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15272
774b6a14
TT
15273 TYPE_FIELD_IGNORE_BITS (type) =
15274 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15275 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15276 }
15277
15278 /* If the type has baseclasses, allocate and clear a bit vector for
15279 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15280 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15281 {
be2daae6 15282 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15283 unsigned char *pointer;
c906108c
SS
15284
15285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15286 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15287 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15288 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15289 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15290 }
15291
2ddeaf8a
TT
15292 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15293 {
15294 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15295
be2daae6 15296 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15297 {
be2daae6
TT
15298 struct nextfield &field = fip->fields[index];
15299
15300 if (field.variant.is_discriminant)
2ddeaf8a 15301 di->discriminant_index = index;
be2daae6 15302 else if (field.variant.default_branch)
2ddeaf8a
TT
15303 di->default_index = index;
15304 else
be2daae6 15305 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15306 }
15307 }
15308
be2daae6
TT
15309 /* Copy the saved-up fields into the field vector. */
15310 for (int i = 0; i < nfields; ++i)
c906108c 15311 {
be2daae6
TT
15312 struct nextfield &field
15313 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15314 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15315
be2daae6
TT
15316 TYPE_FIELD (type, i) = field.field;
15317 switch (field.accessibility)
c906108c 15318 {
c5aa993b 15319 case DW_ACCESS_private:
b4ba55a1 15320 if (cu->language != language_ada)
be2daae6 15321 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15322 break;
c906108c 15323
c5aa993b 15324 case DW_ACCESS_protected:
b4ba55a1 15325 if (cu->language != language_ada)
be2daae6 15326 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15327 break;
c906108c 15328
c5aa993b
JM
15329 case DW_ACCESS_public:
15330 break;
c906108c 15331
c5aa993b
JM
15332 default:
15333 /* Unknown accessibility. Complain and treat it as public. */
15334 {
b98664d3 15335 complaint (_("unsupported accessibility %d"),
be2daae6 15336 field.accessibility);
c5aa993b
JM
15337 }
15338 break;
c906108c 15339 }
be2daae6 15340 if (i < fip->baseclasses.size ())
c906108c 15341 {
be2daae6 15342 switch (field.virtuality)
c906108c 15343 {
c5aa993b
JM
15344 case DW_VIRTUALITY_virtual:
15345 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15346 if (cu->language == language_ada)
a73c6dcd 15347 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15348 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15349 break;
c906108c
SS
15350 }
15351 }
c906108c
SS
15352 }
15353}
15354
7d27a96d
TT
15355/* Return true if this member function is a constructor, false
15356 otherwise. */
15357
15358static int
15359dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15360{
15361 const char *fieldname;
fe978cb0 15362 const char *type_name;
7d27a96d
TT
15363 int len;
15364
15365 if (die->parent == NULL)
15366 return 0;
15367
15368 if (die->parent->tag != DW_TAG_structure_type
15369 && die->parent->tag != DW_TAG_union_type
15370 && die->parent->tag != DW_TAG_class_type)
15371 return 0;
15372
15373 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15374 type_name = dwarf2_name (die->parent, cu);
15375 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15376 return 0;
15377
15378 len = strlen (fieldname);
fe978cb0
PA
15379 return (strncmp (fieldname, type_name, len) == 0
15380 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15381}
15382
e35000a7
TBA
15383/* Check if the given VALUE is a recognized enum
15384 dwarf_defaulted_attribute constant according to DWARF5 spec,
15385 Table 7.24. */
15386
15387static bool
15388is_valid_DW_AT_defaulted (ULONGEST value)
15389{
15390 switch (value)
15391 {
15392 case DW_DEFAULTED_no:
15393 case DW_DEFAULTED_in_class:
15394 case DW_DEFAULTED_out_of_class:
15395 return true;
15396 }
15397
3142e908 15398 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15399 return false;
15400}
15401
c906108c
SS
15402/* Add a member function to the proper fieldlist. */
15403
15404static void
107d2387 15405dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15406 struct type *type, struct dwarf2_cu *cu)
c906108c 15407{
518817b3 15408 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15409 struct attribute *attr;
c906108c 15410 int i;
be2daae6 15411 struct fnfieldlist *flp = nullptr;
c906108c 15412 struct fn_field *fnp;
15d034d0 15413 const char *fieldname;
f792889a 15414 struct type *this_type;
60d5a603 15415 enum dwarf_access_attribute accessibility;
c906108c 15416
b4ba55a1 15417 if (cu->language == language_ada)
a73c6dcd 15418 error (_("unexpected member function in Ada type"));
b4ba55a1 15419
2df3850c 15420 /* Get name of member function. */
39cbfefa
DJ
15421 fieldname = dwarf2_name (die, cu);
15422 if (fieldname == NULL)
2df3850c 15423 return;
c906108c 15424
c906108c 15425 /* Look up member function name in fieldlist. */
be2daae6 15426 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15427 {
27bfe10e 15428 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15429 {
15430 flp = &fip->fnfieldlists[i];
15431 break;
15432 }
c906108c
SS
15433 }
15434
be2daae6
TT
15435 /* Create a new fnfieldlist if necessary. */
15436 if (flp == nullptr)
c906108c 15437 {
be2daae6
TT
15438 fip->fnfieldlists.emplace_back ();
15439 flp = &fip->fnfieldlists.back ();
c906108c 15440 flp->name = fieldname;
be2daae6 15441 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15442 }
15443
be2daae6
TT
15444 /* Create a new member function field and add it to the vector of
15445 fnfieldlists. */
15446 flp->fnfields.emplace_back ();
15447 fnp = &flp->fnfields.back ();
3da10d80
KS
15448
15449 /* Delay processing of the physname until later. */
9c37b5ae 15450 if (cu->language == language_cplus)
be2daae6
TT
15451 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15452 die, cu);
3da10d80
KS
15453 else
15454 {
1d06ead6 15455 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15456 fnp->physname = physname ? physname : "";
15457 }
15458
c906108c 15459 fnp->type = alloc_type (objfile);
f792889a
DJ
15460 this_type = read_type_die (die, cu);
15461 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15462 {
f792889a 15463 int nparams = TYPE_NFIELDS (this_type);
c906108c 15464
f792889a 15465 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15466 of the method itself (TYPE_CODE_METHOD). */
15467 smash_to_method_type (fnp->type, type,
f792889a
DJ
15468 TYPE_TARGET_TYPE (this_type),
15469 TYPE_FIELDS (this_type),
15470 TYPE_NFIELDS (this_type),
15471 TYPE_VARARGS (this_type));
c906108c
SS
15472
15473 /* Handle static member functions.
c5aa993b 15474 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15475 member functions. G++ helps GDB by marking the first
15476 parameter for non-static member functions (which is the this
15477 pointer) as artificial. We obtain this information from
15478 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15479 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15480 fnp->voffset = VOFFSET_STATIC;
15481 }
15482 else
b98664d3 15483 complaint (_("member function type missing for '%s'"),
3da10d80 15484 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15485
15486 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15487 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15488 fnp->fcontext = die_containing_type (die, cu);
c906108c 15489
3e43a32a
MS
15490 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15491 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15492
15493 /* Get accessibility. */
e142c38c 15494 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15495 if (attr != nullptr)
aead7601 15496 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15497 else
15498 accessibility = dwarf2_default_access_attribute (die, cu);
15499 switch (accessibility)
c906108c 15500 {
60d5a603
JK
15501 case DW_ACCESS_private:
15502 fnp->is_private = 1;
15503 break;
15504 case DW_ACCESS_protected:
15505 fnp->is_protected = 1;
15506 break;
c906108c
SS
15507 }
15508
b02dede2 15509 /* Check for artificial methods. */
e142c38c 15510 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15511 if (attr && DW_UNSND (attr) != 0)
15512 fnp->is_artificial = 1;
15513
e35000a7
TBA
15514 /* Check for defaulted methods. */
15515 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15516 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15517 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15518
15519 /* Check for deleted methods. */
15520 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15521 if (attr != nullptr && DW_UNSND (attr) != 0)
15522 fnp->is_deleted = 1;
15523
7d27a96d
TT
15524 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15525
0d564a31 15526 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15527 function. For older versions of GCC, this is an offset in the
15528 appropriate virtual table, as specified by DW_AT_containing_type.
15529 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15530 to the object address. */
15531
e142c38c 15532 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15533 if (attr != nullptr)
8e19ed76 15534 {
aec5aa8b 15535 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15536 {
aec5aa8b
TT
15537 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15538 {
15539 /* Old-style GCC. */
15540 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15541 }
15542 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15543 || (DW_BLOCK (attr)->size > 1
15544 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15545 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15546 {
aec5aa8b
TT
15547 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15548 if ((fnp->voffset % cu->header.addr_size) != 0)
15549 dwarf2_complex_location_expr_complaint ();
15550 else
15551 fnp->voffset /= cu->header.addr_size;
15552 fnp->voffset += 2;
15553 }
15554 else
15555 dwarf2_complex_location_expr_complaint ();
15556
15557 if (!fnp->fcontext)
7e993ebf
KS
15558 {
15559 /* If there is no `this' field and no DW_AT_containing_type,
15560 we cannot actually find a base class context for the
15561 vtable! */
15562 if (TYPE_NFIELDS (this_type) == 0
15563 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15564 {
b98664d3 15565 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15566 "function \"%s\" (offset %s)"),
15567 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15568 }
15569 else
15570 {
15571 fnp->fcontext
15572 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15573 }
15574 }
aec5aa8b 15575 }
3690dd37 15576 else if (attr_form_is_section_offset (attr))
8e19ed76 15577 {
4d3c2250 15578 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15579 }
15580 else
15581 {
4d3c2250
KB
15582 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15583 fieldname);
8e19ed76 15584 }
0d564a31 15585 }
d48cc9dd
DJ
15586 else
15587 {
15588 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15589 if (attr && DW_UNSND (attr))
15590 {
15591 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15592 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15593 "but the vtable offset is not specified"),
9d8780f0 15594 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15595 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15596 TYPE_CPLUS_DYNAMIC (type) = 1;
15597 }
15598 }
c906108c
SS
15599}
15600
15601/* Create the vector of member function fields, and attach it to the type. */
15602
15603static void
fba45db2 15604dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15605 struct dwarf2_cu *cu)
c906108c 15606{
b4ba55a1 15607 if (cu->language == language_ada)
a73c6dcd 15608 error (_("unexpected member functions in Ada type"));
b4ba55a1 15609
c906108c
SS
15610 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15611 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15612 TYPE_ALLOC (type,
15613 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15614
be2daae6 15615 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15616 {
be2daae6 15617 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15618 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15619
be2daae6
TT
15620 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15621 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15622 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15623 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15624
15625 for (int k = 0; k < nf.fnfields.size (); ++k)
15626 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15627 }
15628
be2daae6 15629 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15630}
15631
1168df01
JB
15632/* Returns non-zero if NAME is the name of a vtable member in CU's
15633 language, zero otherwise. */
15634static int
15635is_vtable_name (const char *name, struct dwarf2_cu *cu)
15636{
15637 static const char vptr[] = "_vptr";
15638
9c37b5ae
TT
15639 /* Look for the C++ form of the vtable. */
15640 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15641 return 1;
15642
15643 return 0;
15644}
15645
c0dd20ea 15646/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15647 functions, with the ABI-specified layout. If TYPE describes
15648 such a structure, smash it into a member function type.
61049d3b
DJ
15649
15650 GCC shouldn't do this; it should just output pointer to member DIEs.
15651 This is GCC PR debug/28767. */
c0dd20ea 15652
0b92b5bb
TT
15653static void
15654quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15655{
09e2d7c7 15656 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15657
15658 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15659 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15660 return;
c0dd20ea
DJ
15661
15662 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15663 if (TYPE_FIELD_NAME (type, 0) == NULL
15664 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15665 || TYPE_FIELD_NAME (type, 1) == NULL
15666 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15667 return;
c0dd20ea
DJ
15668
15669 /* Find the type of the method. */
0b92b5bb 15670 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15671 if (pfn_type == NULL
15672 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15673 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15674 return;
c0dd20ea
DJ
15675
15676 /* Look for the "this" argument. */
15677 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15678 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15679 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15680 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15681 return;
c0dd20ea 15682
09e2d7c7 15683 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15684 new_type = alloc_type (objfile);
09e2d7c7 15685 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15686 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15687 TYPE_VARARGS (pfn_type));
0b92b5bb 15688 smash_to_methodptr_type (type, new_type);
c0dd20ea 15689}
1168df01 15690
2b4424c3
TT
15691/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15692 appropriate error checking and issuing complaints if there is a
15693 problem. */
15694
15695static ULONGEST
15696get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15697{
15698 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15699
15700 if (attr == nullptr)
15701 return 0;
15702
15703 if (!attr_form_is_constant (attr))
15704 {
b98664d3 15705 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15706 " - DIE at %s [in module %s]"),
15707 sect_offset_str (die->sect_off),
15708 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15709 return 0;
15710 }
15711
15712 ULONGEST align;
15713 if (attr->form == DW_FORM_sdata)
15714 {
15715 LONGEST val = DW_SND (attr);
15716 if (val < 0)
15717 {
b98664d3 15718 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15719 " - DIE at %s [in module %s]"),
15720 sect_offset_str (die->sect_off),
15721 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15722 return 0;
15723 }
15724 align = val;
15725 }
15726 else
15727 align = DW_UNSND (attr);
15728
15729 if (align == 0)
15730 {
b98664d3 15731 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15732 " - DIE at %s [in module %s]"),
15733 sect_offset_str (die->sect_off),
15734 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15735 return 0;
15736 }
15737 if ((align & (align - 1)) != 0)
15738 {
b98664d3 15739 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15743 return 0;
15744 }
15745
15746 return align;
15747}
15748
15749/* If the DIE has a DW_AT_alignment attribute, use its value to set
15750 the alignment for TYPE. */
15751
15752static void
15753maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15754 struct type *type)
15755{
15756 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15757 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15758 " - DIE at %s [in module %s]"),
15759 sect_offset_str (die->sect_off),
15760 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15761}
685b1105 15762
e35000a7
TBA
15763/* Check if the given VALUE is a valid enum dwarf_calling_convention
15764 constant for a type, according to DWARF5 spec, Table 5.5. */
15765
15766static bool
15767is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15768{
15769 switch (value)
15770 {
15771 case DW_CC_normal:
15772 case DW_CC_pass_by_reference:
15773 case DW_CC_pass_by_value:
15774 return true;
15775
15776 default:
15777 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15778 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15779 return false;
15780 }
15781}
15782
d0922fcf
TBA
15783/* Check if the given VALUE is a valid enum dwarf_calling_convention
15784 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15785 also according to GNU-specific values (see include/dwarf2.h). */
15786
15787static bool
15788is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15789{
15790 switch (value)
15791 {
15792 case DW_CC_normal:
15793 case DW_CC_program:
15794 case DW_CC_nocall:
15795 return true;
15796
15797 case DW_CC_GNU_renesas_sh:
15798 case DW_CC_GNU_borland_fastcall_i386:
15799 case DW_CC_GDB_IBM_OpenCL:
15800 return true;
15801
15802 default:
15803 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15804 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15805 return false;
15806 }
15807}
15808
c906108c 15809/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15810 (definition) to create a type for the structure or union. Fill in
15811 the type's name and general properties; the members will not be
83655187
DE
15812 processed until process_structure_scope. A symbol table entry for
15813 the type will also not be done until process_structure_scope (assuming
15814 the type has a name).
c906108c 15815
c767944b
DJ
15816 NOTE: we need to call these functions regardless of whether or not the
15817 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15818 structure or union. This gets the type entered into our set of
83655187 15819 user defined types. */
c906108c 15820
f792889a 15821static struct type *
134d01f1 15822read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15823{
518817b3 15824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15825 struct type *type;
15826 struct attribute *attr;
15d034d0 15827 const char *name;
c906108c 15828
348e048f
DE
15829 /* If the definition of this type lives in .debug_types, read that type.
15830 Don't follow DW_AT_specification though, that will take us back up
15831 the chain and we want to go down. */
45e58e77 15832 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15833 if (attr != nullptr)
348e048f 15834 {
ac9ec31b 15835 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15836
ac9ec31b 15837 /* The type's CU may not be the same as CU.
02142a6c 15838 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15839 return set_die_type (die, type, cu);
15840 }
15841
c0dd20ea 15842 type = alloc_type (objfile);
c906108c 15843 INIT_CPLUS_SPECIFIC (type);
93311388 15844
39cbfefa
DJ
15845 name = dwarf2_name (die, cu);
15846 if (name != NULL)
c906108c 15847 {
987504bb 15848 if (cu->language == language_cplus
c44af4eb
TT
15849 || cu->language == language_d
15850 || cu->language == language_rust)
63d06c5c 15851 {
15d034d0 15852 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15853
15854 /* dwarf2_full_name might have already finished building the DIE's
15855 type. If so, there is no need to continue. */
15856 if (get_die_type (die, cu) != NULL)
15857 return get_die_type (die, cu);
15858
e86ca25f 15859 TYPE_NAME (type) = full_name;
63d06c5c
DC
15860 }
15861 else
15862 {
d8151005
DJ
15863 /* The name is already allocated along with this objfile, so
15864 we don't need to duplicate it for the type. */
e86ca25f 15865 TYPE_NAME (type) = name;
63d06c5c 15866 }
c906108c
SS
15867 }
15868
15869 if (die->tag == DW_TAG_structure_type)
15870 {
15871 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15872 }
15873 else if (die->tag == DW_TAG_union_type)
15874 {
15875 TYPE_CODE (type) = TYPE_CODE_UNION;
15876 }
2ddeaf8a
TT
15877 else if (die->tag == DW_TAG_variant_part)
15878 {
15879 TYPE_CODE (type) = TYPE_CODE_UNION;
15880 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15881 }
c906108c
SS
15882 else
15883 {
4753d33b 15884 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15885 }
15886
0cc2414c
TT
15887 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15888 TYPE_DECLARED_CLASS (type) = 1;
15889
e35000a7
TBA
15890 /* Store the calling convention in the type if it's available in
15891 the die. Otherwise the calling convention remains set to
15892 the default value DW_CC_normal. */
15893 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15894 if (attr != nullptr
15895 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15896 {
15897 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15898 TYPE_CPLUS_CALLING_CONVENTION (type)
15899 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15900 }
15901
e142c38c 15902 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15903 if (attr != nullptr)
c906108c 15904 {
155bfbd3
JB
15905 if (attr_form_is_constant (attr))
15906 TYPE_LENGTH (type) = DW_UNSND (attr);
15907 else
15908 {
15909 /* For the moment, dynamic type sizes are not supported
15910 by GDB's struct type. The actual size is determined
15911 on-demand when resolving the type of a given object,
15912 so set the type's length to zero for now. Otherwise,
15913 we record an expression as the length, and that expression
15914 could lead to a very large value, which could eventually
15915 lead to us trying to allocate that much memory when creating
15916 a value of that type. */
15917 TYPE_LENGTH (type) = 0;
15918 }
c906108c
SS
15919 }
15920 else
15921 {
15922 TYPE_LENGTH (type) = 0;
15923 }
15924
2b4424c3
TT
15925 maybe_set_alignment (cu, die, type);
15926
5230b05a 15927 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15928 {
5230b05a
WT
15929 /* ICC<14 does not output the required DW_AT_declaration on
15930 incomplete types, but gives them a size of zero. */
422b1cb0 15931 TYPE_STUB (type) = 1;
685b1105
JK
15932 }
15933 else
15934 TYPE_STUB_SUPPORTED (type) = 1;
15935
dc718098 15936 if (die_is_declaration (die, cu))
876cecd0 15937 TYPE_STUB (type) = 1;
a6c727b2
DJ
15938 else if (attr == NULL && die->child == NULL
15939 && producer_is_realview (cu->producer))
15940 /* RealView does not output the required DW_AT_declaration
15941 on incomplete types. */
15942 TYPE_STUB (type) = 1;
dc718098 15943
c906108c
SS
15944 /* We need to add the type field to the die immediately so we don't
15945 infinitely recurse when dealing with pointers to the structure
0963b4bd 15946 type within the structure itself. */
1c379e20 15947 set_die_type (die, type, cu);
c906108c 15948
7e314c57
JK
15949 /* set_die_type should be already done. */
15950 set_descriptive_type (type, die, cu);
15951
c767944b
DJ
15952 return type;
15953}
15954
2ddeaf8a
TT
15955/* A helper for process_structure_scope that handles a single member
15956 DIE. */
15957
15958static void
15959handle_struct_member_die (struct die_info *child_die, struct type *type,
15960 struct field_info *fi,
15961 std::vector<struct symbol *> *template_args,
15962 struct dwarf2_cu *cu)
15963{
15964 if (child_die->tag == DW_TAG_member
15965 || child_die->tag == DW_TAG_variable
15966 || child_die->tag == DW_TAG_variant_part)
15967 {
15968 /* NOTE: carlton/2002-11-05: A C++ static data member
15969 should be a DW_TAG_member that is a declaration, but
15970 all versions of G++ as of this writing (so through at
15971 least 3.2.1) incorrectly generate DW_TAG_variable
15972 tags for them instead. */
15973 dwarf2_add_field (fi, child_die, cu);
15974 }
15975 else if (child_die->tag == DW_TAG_subprogram)
15976 {
15977 /* Rust doesn't have member functions in the C++ sense.
15978 However, it does emit ordinary functions as children
15979 of a struct DIE. */
15980 if (cu->language == language_rust)
15981 read_func_scope (child_die, cu);
15982 else
15983 {
15984 /* C++ member function. */
15985 dwarf2_add_member_fn (fi, child_die, type, cu);
15986 }
15987 }
15988 else if (child_die->tag == DW_TAG_inheritance)
15989 {
15990 /* C++ base class field. */
15991 dwarf2_add_field (fi, child_die, cu);
15992 }
15993 else if (type_can_define_types (child_die))
15994 dwarf2_add_type_defn (fi, child_die, cu);
15995 else if (child_die->tag == DW_TAG_template_type_param
15996 || child_die->tag == DW_TAG_template_value_param)
15997 {
15998 struct symbol *arg = new_symbol (child_die, NULL, cu);
15999
16000 if (arg != NULL)
16001 template_args->push_back (arg);
16002 }
16003 else if (child_die->tag == DW_TAG_variant)
16004 {
16005 /* In a variant we want to get the discriminant and also add a
16006 field for our sole member child. */
16007 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16008
bde09ab7 16009 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16010 variant_child != NULL;
16011 variant_child = sibling_die (variant_child))
16012 {
16013 if (variant_child->tag == DW_TAG_member)
16014 {
16015 handle_struct_member_die (variant_child, type, fi,
16016 template_args, cu);
16017 /* Only handle the one. */
16018 break;
16019 }
16020 }
16021
16022 /* We don't handle this but we might as well report it if we see
16023 it. */
16024 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16025 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16026 " - DIE at %s [in module %s]"),
16027 sect_offset_str (child_die->sect_off),
16028 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16029
16030 /* The first field was just added, so we can stash the
16031 discriminant there. */
be2daae6 16032 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16033 if (discr == NULL)
be2daae6 16034 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16035 else
be2daae6 16036 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16037 }
16038}
16039
c767944b
DJ
16040/* Finish creating a structure or union type, including filling in
16041 its members and creating a symbol for it. */
16042
16043static void
16044process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16045{
518817b3 16046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16047 struct die_info *child_die;
c767944b
DJ
16048 struct type *type;
16049
16050 type = get_die_type (die, cu);
16051 if (type == NULL)
16052 type = read_structure_type (die, cu);
16053
2ddeaf8a
TT
16054 /* When reading a DW_TAG_variant_part, we need to notice when we
16055 read the discriminant member, so we can record it later in the
16056 discriminant_info. */
16057 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16058 sect_offset discr_offset {};
3e1d3d8c 16059 bool has_template_parameters = false;
2ddeaf8a
TT
16060
16061 if (is_variant_part)
16062 {
16063 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16064 if (discr == NULL)
16065 {
16066 /* Maybe it's a univariant form, an extension we support.
16067 In this case arrange not to check the offset. */
16068 is_variant_part = false;
16069 }
16070 else if (attr_form_is_ref (discr))
16071 {
16072 struct dwarf2_cu *target_cu = cu;
16073 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16074
16075 discr_offset = target_die->sect_off;
16076 }
16077 else
16078 {
b98664d3 16079 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16080 " - DIE at %s [in module %s]"),
16081 sect_offset_str (die->sect_off),
16082 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16083 is_variant_part = false;
16084 }
16085 }
16086
e142c38c 16087 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16088 {
16089 struct field_info fi;
2f4732b0 16090 std::vector<struct symbol *> template_args;
c906108c 16091
639d11d3 16092 child_die = die->child;
c906108c
SS
16093
16094 while (child_die && child_die->tag)
16095 {
2ddeaf8a 16096 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16097
2ddeaf8a 16098 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16099 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16100
c906108c
SS
16101 child_die = sibling_die (child_die);
16102 }
16103
34eaf542 16104 /* Attach template arguments to type. */
2f4732b0 16105 if (!template_args.empty ())
34eaf542 16106 {
3e1d3d8c 16107 has_template_parameters = true;
34eaf542 16108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16109 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16110 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16111 = XOBNEWVEC (&objfile->objfile_obstack,
16112 struct symbol *,
16113 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16114 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16115 template_args.data (),
34eaf542
TT
16116 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16117 * sizeof (struct symbol *)));
34eaf542
TT
16118 }
16119
c906108c
SS
16120 /* Attach fields and member functions to the type. */
16121 if (fi.nfields)
e7c27a73 16122 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16123 if (!fi.fnfieldlists.empty ())
c906108c 16124 {
e7c27a73 16125 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16126
c5aa993b 16127 /* Get the type which refers to the base class (possibly this
c906108c 16128 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16129 class from the DW_AT_containing_type attribute. This use of
16130 DW_AT_containing_type is a GNU extension. */
c906108c 16131
e142c38c 16132 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16133 {
e7c27a73 16134 struct type *t = die_containing_type (die, cu);
c906108c 16135
ae6ae975 16136 set_type_vptr_basetype (type, t);
c906108c
SS
16137 if (type == t)
16138 {
c906108c
SS
16139 int i;
16140
16141 /* Our own class provides vtbl ptr. */
16142 for (i = TYPE_NFIELDS (t) - 1;
16143 i >= TYPE_N_BASECLASSES (t);
16144 --i)
16145 {
0d5cff50 16146 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16147
1168df01 16148 if (is_vtable_name (fieldname, cu))
c906108c 16149 {
ae6ae975 16150 set_type_vptr_fieldno (type, i);
c906108c
SS
16151 break;
16152 }
16153 }
16154
16155 /* Complain if virtual function table field not found. */
16156 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16157 complaint (_("virtual function table pointer "
3e43a32a 16158 "not found when defining class '%s'"),
e86ca25f 16159 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16160 }
16161 else
16162 {
ae6ae975 16163 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16164 }
16165 }
f6235d4c 16166 else if (cu->producer
61012eef 16167 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16168 {
16169 /* The IBM XLC compiler does not provide direct indication
16170 of the containing type, but the vtable pointer is
16171 always named __vfp. */
16172
16173 int i;
16174
16175 for (i = TYPE_NFIELDS (type) - 1;
16176 i >= TYPE_N_BASECLASSES (type);
16177 --i)
16178 {
16179 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16180 {
ae6ae975
DE
16181 set_type_vptr_fieldno (type, i);
16182 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16183 break;
16184 }
16185 }
16186 }
c906108c 16187 }
98751a41
JK
16188
16189 /* Copy fi.typedef_field_list linked list elements content into the
16190 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16191 if (!fi.typedef_field_list.empty ())
98751a41 16192 {
be2daae6 16193 int count = fi.typedef_field_list.size ();
98751a41 16194
a0d7a4ff 16195 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16196 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16197 = ((struct decl_field *)
be2daae6
TT
16198 TYPE_ALLOC (type,
16199 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16200 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16201
be2daae6
TT
16202 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16203 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16204 }
c767944b 16205
883fd55a
KS
16206 /* Copy fi.nested_types_list linked list elements content into the
16207 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16208 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16209 {
be2daae6 16210 int count = fi.nested_types_list.size ();
883fd55a
KS
16211
16212 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16213 TYPE_NESTED_TYPES_ARRAY (type)
16214 = ((struct decl_field *)
be2daae6
TT
16215 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16216 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16217
be2daae6
TT
16218 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16219 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16220 }
c906108c 16221 }
63d06c5c 16222
bb5ed363 16223 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16224 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16225 cu->rust_unions.push_back (type);
0b92b5bb 16226
90aeadfc
DC
16227 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16228 snapshots) has been known to create a die giving a declaration
16229 for a class that has, as a child, a die giving a definition for a
16230 nested class. So we have to process our children even if the
16231 current die is a declaration. Normally, of course, a declaration
16232 won't have any children at all. */
134d01f1 16233
ca040673
DE
16234 child_die = die->child;
16235
90aeadfc
DC
16236 while (child_die != NULL && child_die->tag)
16237 {
16238 if (child_die->tag == DW_TAG_member
16239 || child_die->tag == DW_TAG_variable
34eaf542
TT
16240 || child_die->tag == DW_TAG_inheritance
16241 || child_die->tag == DW_TAG_template_value_param
16242 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16243 {
90aeadfc 16244 /* Do nothing. */
134d01f1 16245 }
90aeadfc
DC
16246 else
16247 process_die (child_die, cu);
134d01f1 16248
90aeadfc 16249 child_die = sibling_die (child_die);
134d01f1
DJ
16250 }
16251
fa4028e9
JB
16252 /* Do not consider external references. According to the DWARF standard,
16253 these DIEs are identified by the fact that they have no byte_size
16254 attribute, and a declaration attribute. */
16255 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16256 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16257 {
16258 struct symbol *sym = new_symbol (die, type, cu);
16259
16260 if (has_template_parameters)
16261 {
a776957c
TT
16262 struct symtab *symtab;
16263 if (sym != nullptr)
16264 symtab = symbol_symtab (sym);
16265 else if (cu->line_header != nullptr)
16266 {
16267 /* Any related symtab will do. */
16268 symtab
7ba99d21 16269 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16270 }
16271 else
16272 {
16273 symtab = nullptr;
16274 complaint (_("could not find suitable "
16275 "symtab for template parameter"
16276 " - DIE at %s [in module %s]"),
16277 sect_offset_str (die->sect_off),
16278 objfile_name (objfile));
16279 }
16280
16281 if (symtab != nullptr)
16282 {
16283 /* Make sure that the symtab is set on the new symbols.
16284 Even though they don't appear in this symtab directly,
16285 other parts of gdb assume that symbols do, and this is
16286 reasonably true. */
16287 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16288 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16289 }
3e1d3d8c
TT
16290 }
16291 }
134d01f1
DJ
16292}
16293
55426c9d
JB
16294/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16295 update TYPE using some information only available in DIE's children. */
16296
16297static void
16298update_enumeration_type_from_children (struct die_info *die,
16299 struct type *type,
16300 struct dwarf2_cu *cu)
16301{
60f7655a 16302 struct die_info *child_die;
55426c9d
JB
16303 int unsigned_enum = 1;
16304 int flag_enum = 1;
16305 ULONGEST mask = 0;
55426c9d 16306
8268c778 16307 auto_obstack obstack;
55426c9d 16308
60f7655a
DE
16309 for (child_die = die->child;
16310 child_die != NULL && child_die->tag;
16311 child_die = sibling_die (child_die))
55426c9d
JB
16312 {
16313 struct attribute *attr;
16314 LONGEST value;
16315 const gdb_byte *bytes;
16316 struct dwarf2_locexpr_baton *baton;
16317 const char *name;
60f7655a 16318
55426c9d
JB
16319 if (child_die->tag != DW_TAG_enumerator)
16320 continue;
16321
16322 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16323 if (attr == NULL)
16324 continue;
16325
16326 name = dwarf2_name (child_die, cu);
16327 if (name == NULL)
16328 name = "<anonymous enumerator>";
16329
16330 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16331 &value, &bytes, &baton);
16332 if (value < 0)
16333 {
16334 unsigned_enum = 0;
16335 flag_enum = 0;
16336 }
16337 else if ((mask & value) != 0)
16338 flag_enum = 0;
16339 else
16340 mask |= value;
16341
16342 /* If we already know that the enum type is neither unsigned, nor
16343 a flag type, no need to look at the rest of the enumerates. */
16344 if (!unsigned_enum && !flag_enum)
16345 break;
55426c9d
JB
16346 }
16347
16348 if (unsigned_enum)
16349 TYPE_UNSIGNED (type) = 1;
16350 if (flag_enum)
16351 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16352}
16353
134d01f1
DJ
16354/* Given a DW_AT_enumeration_type die, set its type. We do not
16355 complete the type's fields yet, or create any symbols. */
c906108c 16356
f792889a 16357static struct type *
134d01f1 16358read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16359{
518817b3 16360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16361 struct type *type;
c906108c 16362 struct attribute *attr;
0114d602 16363 const char *name;
134d01f1 16364
348e048f
DE
16365 /* If the definition of this type lives in .debug_types, read that type.
16366 Don't follow DW_AT_specification though, that will take us back up
16367 the chain and we want to go down. */
45e58e77 16368 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16369 if (attr != nullptr)
348e048f 16370 {
ac9ec31b 16371 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16372
ac9ec31b 16373 /* The type's CU may not be the same as CU.
02142a6c 16374 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16375 return set_die_type (die, type, cu);
16376 }
16377
c906108c
SS
16378 type = alloc_type (objfile);
16379
16380 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16381 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16382 if (name != NULL)
e86ca25f 16383 TYPE_NAME (type) = name;
c906108c 16384
0626fc76
TT
16385 attr = dwarf2_attr (die, DW_AT_type, cu);
16386 if (attr != NULL)
16387 {
16388 struct type *underlying_type = die_type (die, cu);
16389
16390 TYPE_TARGET_TYPE (type) = underlying_type;
16391 }
16392
e142c38c 16393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16394 if (attr != nullptr)
c906108c
SS
16395 {
16396 TYPE_LENGTH (type) = DW_UNSND (attr);
16397 }
16398 else
16399 {
16400 TYPE_LENGTH (type) = 0;
16401 }
16402
2b4424c3
TT
16403 maybe_set_alignment (cu, die, type);
16404
137033e9
JB
16405 /* The enumeration DIE can be incomplete. In Ada, any type can be
16406 declared as private in the package spec, and then defined only
16407 inside the package body. Such types are known as Taft Amendment
16408 Types. When another package uses such a type, an incomplete DIE
16409 may be generated by the compiler. */
02eb380e 16410 if (die_is_declaration (die, cu))
876cecd0 16411 TYPE_STUB (type) = 1;
02eb380e 16412
0626fc76
TT
16413 /* Finish the creation of this type by using the enum's children.
16414 We must call this even when the underlying type has been provided
16415 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16416 update_enumeration_type_from_children (die, type, cu);
16417
0626fc76
TT
16418 /* If this type has an underlying type that is not a stub, then we
16419 may use its attributes. We always use the "unsigned" attribute
16420 in this situation, because ordinarily we guess whether the type
16421 is unsigned -- but the guess can be wrong and the underlying type
16422 can tell us the reality. However, we defer to a local size
16423 attribute if one exists, because this lets the compiler override
16424 the underlying type if needed. */
16425 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16426 {
16427 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16428 if (TYPE_LENGTH (type) == 0)
16429 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16430 if (TYPE_RAW_ALIGN (type) == 0
16431 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16432 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16433 }
16434
3d567982
TT
16435 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16436
f792889a 16437 return set_die_type (die, type, cu);
134d01f1
DJ
16438}
16439
16440/* Given a pointer to a die which begins an enumeration, process all
16441 the dies that define the members of the enumeration, and create the
16442 symbol for the enumeration type.
16443
16444 NOTE: We reverse the order of the element list. */
16445
16446static void
16447process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16448{
f792889a 16449 struct type *this_type;
134d01f1 16450
f792889a
DJ
16451 this_type = get_die_type (die, cu);
16452 if (this_type == NULL)
16453 this_type = read_enumeration_type (die, cu);
9dc481d3 16454
639d11d3 16455 if (die->child != NULL)
c906108c 16456 {
9dc481d3
DE
16457 struct die_info *child_die;
16458 struct symbol *sym;
43816ebc 16459 std::vector<struct field> fields;
15d034d0 16460 const char *name;
9dc481d3 16461
639d11d3 16462 child_die = die->child;
c906108c
SS
16463 while (child_die && child_die->tag)
16464 {
16465 if (child_die->tag != DW_TAG_enumerator)
16466 {
e7c27a73 16467 process_die (child_die, cu);
c906108c
SS
16468 }
16469 else
16470 {
39cbfefa
DJ
16471 name = dwarf2_name (child_die, cu);
16472 if (name)
c906108c 16473 {
f792889a 16474 sym = new_symbol (child_die, this_type, cu);
c906108c 16475
43816ebc
TT
16476 fields.emplace_back ();
16477 struct field &field = fields.back ();
c906108c 16478
43816ebc
TT
16479 FIELD_NAME (field) = sym->linkage_name ();
16480 FIELD_TYPE (field) = NULL;
16481 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16482 FIELD_BITSIZE (field) = 0;
c906108c
SS
16483 }
16484 }
16485
16486 child_die = sibling_die (child_die);
16487 }
16488
43816ebc 16489 if (!fields.empty ())
c906108c 16490 {
43816ebc 16491 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16492 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16493 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16494 memcpy (TYPE_FIELDS (this_type), fields.data (),
16495 sizeof (struct field) * fields.size ());
c906108c 16496 }
c906108c 16497 }
134d01f1 16498
6c83ed52
TT
16499 /* If we are reading an enum from a .debug_types unit, and the enum
16500 is a declaration, and the enum is not the signatured type in the
16501 unit, then we do not want to add a symbol for it. Adding a
16502 symbol would in some cases obscure the true definition of the
16503 enum, giving users an incomplete type when the definition is
16504 actually available. Note that we do not want to do this for all
16505 enums which are just declarations, because C++0x allows forward
16506 enum declarations. */
3019eac3 16507 if (cu->per_cu->is_debug_types
6c83ed52
TT
16508 && die_is_declaration (die, cu))
16509 {
52dc124a 16510 struct signatured_type *sig_type;
6c83ed52 16511
c0f78cd4 16512 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16513 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16514 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16515 return;
16516 }
16517
f792889a 16518 new_symbol (die, this_type, cu);
c906108c
SS
16519}
16520
16521/* Extract all information from a DW_TAG_array_type DIE and put it in
16522 the DIE's type field. For now, this only handles one dimensional
16523 arrays. */
16524
f792889a 16525static struct type *
e7c27a73 16526read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16527{
518817b3 16528 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16529 struct die_info *child_die;
7e314c57 16530 struct type *type;
c906108c 16531 struct type *element_type, *range_type, *index_type;
c906108c 16532 struct attribute *attr;
15d034d0 16533 const char *name;
a405673c 16534 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16535 unsigned int bit_stride = 0;
c906108c 16536
e7c27a73 16537 element_type = die_type (die, cu);
c906108c 16538
7e314c57
JK
16539 /* The die_type call above may have already set the type for this DIE. */
16540 type = get_die_type (die, cu);
16541 if (type)
16542 return type;
16543
dc53a7ad
JB
16544 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16545 if (attr != NULL)
a405673c
JB
16546 {
16547 int stride_ok;
9a49df9d
AB
16548 struct type *prop_type
16549 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16550
16551 byte_stride_prop
16552 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16553 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16554 prop_type);
a405673c
JB
16555 if (!stride_ok)
16556 {
b98664d3 16557 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16558 " - DIE at %s [in module %s]"),
16559 sect_offset_str (die->sect_off),
518817b3 16560 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16561 /* Ignore this attribute. We will likely not be able to print
16562 arrays of this type correctly, but there is little we can do
16563 to help if we cannot read the attribute's value. */
16564 byte_stride_prop = NULL;
16565 }
16566 }
dc53a7ad
JB
16567
16568 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16569 if (attr != NULL)
16570 bit_stride = DW_UNSND (attr);
16571
c906108c
SS
16572 /* Irix 6.2 native cc creates array types without children for
16573 arrays with unspecified length. */
639d11d3 16574 if (die->child == NULL)
c906108c 16575 {
46bf5051 16576 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16577 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16578 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16579 byte_stride_prop, bit_stride);
f792889a 16580 return set_die_type (die, type, cu);
c906108c
SS
16581 }
16582
791afaa2 16583 std::vector<struct type *> range_types;
639d11d3 16584 child_die = die->child;
c906108c
SS
16585 while (child_die && child_die->tag)
16586 {
16587 if (child_die->tag == DW_TAG_subrange_type)
16588 {
f792889a 16589 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16590
f792889a 16591 if (child_type != NULL)
a02abb62 16592 {
0963b4bd
MS
16593 /* The range type was succesfully read. Save it for the
16594 array type creation. */
791afaa2 16595 range_types.push_back (child_type);
a02abb62 16596 }
c906108c
SS
16597 }
16598 child_die = sibling_die (child_die);
16599 }
16600
16601 /* Dwarf2 dimensions are output from left to right, create the
16602 necessary array types in backwards order. */
7ca2d3a3 16603
c906108c 16604 type = element_type;
7ca2d3a3
DL
16605
16606 if (read_array_order (die, cu) == DW_ORD_col_major)
16607 {
16608 int i = 0;
9a619af0 16609
791afaa2 16610 while (i < range_types.size ())
dc53a7ad 16611 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16612 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16613 }
16614 else
16615 {
791afaa2 16616 size_t ndim = range_types.size ();
7ca2d3a3 16617 while (ndim-- > 0)
dc53a7ad 16618 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16619 byte_stride_prop, bit_stride);
7ca2d3a3 16620 }
c906108c 16621
f5f8a009
EZ
16622 /* Understand Dwarf2 support for vector types (like they occur on
16623 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16624 array type. This is not part of the Dwarf2/3 standard yet, but a
16625 custom vendor extension. The main difference between a regular
16626 array and the vector variant is that vectors are passed by value
16627 to functions. */
e142c38c 16628 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16629 if (attr != nullptr)
ea37ba09 16630 make_vector_type (type);
f5f8a009 16631
dbc98a8b
KW
16632 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16633 implementation may choose to implement triple vectors using this
16634 attribute. */
16635 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16636 if (attr != nullptr)
dbc98a8b
KW
16637 {
16638 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16639 TYPE_LENGTH (type) = DW_UNSND (attr);
16640 else
b98664d3 16641 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16642 "than the total size of elements"));
dbc98a8b
KW
16643 }
16644
39cbfefa
DJ
16645 name = dwarf2_name (die, cu);
16646 if (name)
16647 TYPE_NAME (type) = name;
6e70227d 16648
2b4424c3
TT
16649 maybe_set_alignment (cu, die, type);
16650
0963b4bd 16651 /* Install the type in the die. */
7e314c57
JK
16652 set_die_type (die, type, cu);
16653
16654 /* set_die_type should be already done. */
b4ba55a1
JB
16655 set_descriptive_type (type, die, cu);
16656
7e314c57 16657 return type;
c906108c
SS
16658}
16659
7ca2d3a3 16660static enum dwarf_array_dim_ordering
6e70227d 16661read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16662{
16663 struct attribute *attr;
16664
16665 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16666
435d3d88 16667 if (attr != nullptr)
aead7601 16668 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16669
0963b4bd
MS
16670 /* GNU F77 is a special case, as at 08/2004 array type info is the
16671 opposite order to the dwarf2 specification, but data is still
16672 laid out as per normal fortran.
7ca2d3a3 16673
0963b4bd
MS
16674 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16675 version checking. */
7ca2d3a3 16676
905e0470
PM
16677 if (cu->language == language_fortran
16678 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16679 {
16680 return DW_ORD_row_major;
16681 }
16682
6e70227d 16683 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16684 {
16685 case array_column_major:
16686 return DW_ORD_col_major;
16687 case array_row_major:
16688 default:
16689 return DW_ORD_row_major;
16690 };
16691}
16692
72019c9c 16693/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16694 the DIE's type field. */
72019c9c 16695
f792889a 16696static struct type *
72019c9c
GM
16697read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16698{
7e314c57
JK
16699 struct type *domain_type, *set_type;
16700 struct attribute *attr;
f792889a 16701
7e314c57
JK
16702 domain_type = die_type (die, cu);
16703
16704 /* The die_type call above may have already set the type for this DIE. */
16705 set_type = get_die_type (die, cu);
16706 if (set_type)
16707 return set_type;
16708
16709 set_type = create_set_type (NULL, domain_type);
16710
16711 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16712 if (attr != nullptr)
d09039dd 16713 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16714
2b4424c3
TT
16715 maybe_set_alignment (cu, die, set_type);
16716
f792889a 16717 return set_die_type (die, set_type, cu);
72019c9c 16718}
7ca2d3a3 16719
0971de02
TT
16720/* A helper for read_common_block that creates a locexpr baton.
16721 SYM is the symbol which we are marking as computed.
16722 COMMON_DIE is the DIE for the common block.
16723 COMMON_LOC is the location expression attribute for the common
16724 block itself.
16725 MEMBER_LOC is the location expression attribute for the particular
16726 member of the common block that we are processing.
16727 CU is the CU from which the above come. */
16728
16729static void
16730mark_common_block_symbol_computed (struct symbol *sym,
16731 struct die_info *common_die,
16732 struct attribute *common_loc,
16733 struct attribute *member_loc,
16734 struct dwarf2_cu *cu)
16735{
518817b3
SM
16736 struct dwarf2_per_objfile *dwarf2_per_objfile
16737 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16738 struct objfile *objfile = dwarf2_per_objfile->objfile;
16739 struct dwarf2_locexpr_baton *baton;
16740 gdb_byte *ptr;
16741 unsigned int cu_off;
16742 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16743 LONGEST offset = 0;
16744
16745 gdb_assert (common_loc && member_loc);
16746 gdb_assert (attr_form_is_block (common_loc));
16747 gdb_assert (attr_form_is_block (member_loc)
16748 || attr_form_is_constant (member_loc));
16749
8d749320 16750 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16751 baton->per_cu = cu->per_cu;
16752 gdb_assert (baton->per_cu);
16753
16754 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16755
16756 if (attr_form_is_constant (member_loc))
16757 {
16758 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16759 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16760 }
16761 else
16762 baton->size += DW_BLOCK (member_loc)->size;
16763
224c3ddb 16764 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16765 baton->data = ptr;
16766
16767 *ptr++ = DW_OP_call4;
9c541725 16768 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16769 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16770 ptr += 4;
16771
16772 if (attr_form_is_constant (member_loc))
16773 {
16774 *ptr++ = DW_OP_addr;
16775 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16776 ptr += cu->header.addr_size;
16777 }
16778 else
16779 {
16780 /* We have to copy the data here, because DW_OP_call4 will only
16781 use a DW_AT_location attribute. */
16782 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16783 ptr += DW_BLOCK (member_loc)->size;
16784 }
16785
16786 *ptr++ = DW_OP_plus;
16787 gdb_assert (ptr - baton->data == baton->size);
16788
0971de02 16789 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16790 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16791}
16792
4357ac6c
TT
16793/* Create appropriate locally-scoped variables for all the
16794 DW_TAG_common_block entries. Also create a struct common_block
16795 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16796 is used to separate the common blocks name namespace from regular
4357ac6c 16797 variable names. */
c906108c
SS
16798
16799static void
e7c27a73 16800read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16801{
0971de02
TT
16802 struct attribute *attr;
16803
16804 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16805 if (attr != nullptr)
0971de02
TT
16806 {
16807 /* Support the .debug_loc offsets. */
16808 if (attr_form_is_block (attr))
16809 {
16810 /* Ok. */
16811 }
16812 else if (attr_form_is_section_offset (attr))
16813 {
16814 dwarf2_complex_location_expr_complaint ();
16815 attr = NULL;
16816 }
16817 else
16818 {
16819 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16820 "common block member");
16821 attr = NULL;
16822 }
16823 }
16824
639d11d3 16825 if (die->child != NULL)
c906108c 16826 {
518817b3 16827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16828 struct die_info *child_die;
16829 size_t n_entries = 0, size;
16830 struct common_block *common_block;
16831 struct symbol *sym;
74ac6d43 16832
4357ac6c
TT
16833 for (child_die = die->child;
16834 child_die && child_die->tag;
16835 child_die = sibling_die (child_die))
16836 ++n_entries;
16837
16838 size = (sizeof (struct common_block)
16839 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16840 common_block
16841 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16842 size);
4357ac6c
TT
16843 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16844 common_block->n_entries = 0;
16845
16846 for (child_die = die->child;
16847 child_die && child_die->tag;
16848 child_die = sibling_die (child_die))
16849 {
16850 /* Create the symbol in the DW_TAG_common_block block in the current
16851 symbol scope. */
e7c27a73 16852 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16853 if (sym != NULL)
16854 {
16855 struct attribute *member_loc;
16856
16857 common_block->contents[common_block->n_entries++] = sym;
16858
16859 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16860 cu);
16861 if (member_loc)
16862 {
16863 /* GDB has handled this for a long time, but it is
16864 not specified by DWARF. It seems to have been
16865 emitted by gfortran at least as recently as:
16866 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16867 complaint (_("Variable in common block has "
0971de02 16868 "DW_AT_data_member_location "
9d8780f0
SM
16869 "- DIE at %s [in module %s]"),
16870 sect_offset_str (child_die->sect_off),
518817b3 16871 objfile_name (objfile));
0971de02
TT
16872
16873 if (attr_form_is_section_offset (member_loc))
16874 dwarf2_complex_location_expr_complaint ();
16875 else if (attr_form_is_constant (member_loc)
16876 || attr_form_is_block (member_loc))
16877 {
435d3d88 16878 if (attr != nullptr)
0971de02
TT
16879 mark_common_block_symbol_computed (sym, die, attr,
16880 member_loc, cu);
16881 }
16882 else
16883 dwarf2_complex_location_expr_complaint ();
16884 }
16885 }
c906108c 16886 }
4357ac6c
TT
16887
16888 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16889 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16890 }
16891}
16892
0114d602 16893/* Create a type for a C++ namespace. */
d9fa45fe 16894
0114d602
DJ
16895static struct type *
16896read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16897{
518817b3 16898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16899 const char *previous_prefix, *name;
9219021c 16900 int is_anonymous;
0114d602
DJ
16901 struct type *type;
16902
16903 /* For extensions, reuse the type of the original namespace. */
16904 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16905 {
16906 struct die_info *ext_die;
16907 struct dwarf2_cu *ext_cu = cu;
9a619af0 16908
0114d602
DJ
16909 ext_die = dwarf2_extension (die, &ext_cu);
16910 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16911
16912 /* EXT_CU may not be the same as CU.
02142a6c 16913 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16914 return set_die_type (die, type, cu);
16915 }
9219021c 16916
e142c38c 16917 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16918
16919 /* Now build the name of the current namespace. */
16920
0114d602
DJ
16921 previous_prefix = determine_prefix (die, cu);
16922 if (previous_prefix[0] != '\0')
16923 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16924 previous_prefix, name, 0, cu);
0114d602
DJ
16925
16926 /* Create the type. */
19f392bc 16927 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16928
60531b24 16929 return set_die_type (die, type, cu);
0114d602
DJ
16930}
16931
22cee43f 16932/* Read a namespace scope. */
0114d602
DJ
16933
16934static void
16935read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16936{
518817b3 16937 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16938 int is_anonymous;
9219021c 16939
5c4e30ca
DC
16940 /* Add a symbol associated to this if we haven't seen the namespace
16941 before. Also, add a using directive if it's an anonymous
16942 namespace. */
9219021c 16943
f2f0e013 16944 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16945 {
16946 struct type *type;
16947
0114d602 16948 type = read_type_die (die, cu);
e7c27a73 16949 new_symbol (die, type, cu);
5c4e30ca 16950
e8e80198 16951 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16952 if (is_anonymous)
0114d602
DJ
16953 {
16954 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16955
eb1e02fd 16956 std::vector<const char *> excludes;
804d2729 16957 add_using_directive (using_directives (cu),
22cee43f 16958 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16959 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16960 }
5c4e30ca 16961 }
9219021c 16962
639d11d3 16963 if (die->child != NULL)
d9fa45fe 16964 {
639d11d3 16965 struct die_info *child_die = die->child;
6e70227d 16966
d9fa45fe
DC
16967 while (child_die && child_die->tag)
16968 {
e7c27a73 16969 process_die (child_die, cu);
d9fa45fe
DC
16970 child_die = sibling_die (child_die);
16971 }
16972 }
38d518c9
EZ
16973}
16974
f55ee35c
JK
16975/* Read a Fortran module as type. This DIE can be only a declaration used for
16976 imported module. Still we need that type as local Fortran "use ... only"
16977 declaration imports depend on the created type in determine_prefix. */
16978
16979static struct type *
16980read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16981{
518817b3 16982 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16983 const char *module_name;
f55ee35c
JK
16984 struct type *type;
16985
16986 module_name = dwarf2_name (die, cu);
19f392bc 16987 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16988
f55ee35c
JK
16989 return set_die_type (die, type, cu);
16990}
16991
5d7cb8df
JK
16992/* Read a Fortran module. */
16993
16994static void
16995read_module (struct die_info *die, struct dwarf2_cu *cu)
16996{
16997 struct die_info *child_die = die->child;
530e8392
KB
16998 struct type *type;
16999
17000 type = read_type_die (die, cu);
17001 new_symbol (die, type, cu);
5d7cb8df 17002
5d7cb8df
JK
17003 while (child_die && child_die->tag)
17004 {
17005 process_die (child_die, cu);
17006 child_die = sibling_die (child_die);
17007 }
17008}
17009
38d518c9
EZ
17010/* Return the name of the namespace represented by DIE. Set
17011 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17012 namespace. */
17013
17014static const char *
e142c38c 17015namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17016{
17017 struct die_info *current_die;
17018 const char *name = NULL;
17019
17020 /* Loop through the extensions until we find a name. */
17021
17022 for (current_die = die;
17023 current_die != NULL;
f2f0e013 17024 current_die = dwarf2_extension (die, &cu))
38d518c9 17025 {
96553a0c
DE
17026 /* We don't use dwarf2_name here so that we can detect the absence
17027 of a name -> anonymous namespace. */
7d45c7c3 17028 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17029
38d518c9
EZ
17030 if (name != NULL)
17031 break;
17032 }
17033
17034 /* Is it an anonymous namespace? */
17035
17036 *is_anonymous = (name == NULL);
17037 if (*is_anonymous)
2b1dbab0 17038 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17039
17040 return name;
d9fa45fe
DC
17041}
17042
c906108c
SS
17043/* Extract all information from a DW_TAG_pointer_type DIE and add to
17044 the user defined type vector. */
17045
f792889a 17046static struct type *
e7c27a73 17047read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17048{
518817b3
SM
17049 struct gdbarch *gdbarch
17050 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17051 struct comp_unit_head *cu_header = &cu->header;
c906108c 17052 struct type *type;
8b2dbe47
KB
17053 struct attribute *attr_byte_size;
17054 struct attribute *attr_address_class;
17055 int byte_size, addr_class;
7e314c57
JK
17056 struct type *target_type;
17057
17058 target_type = die_type (die, cu);
c906108c 17059
7e314c57
JK
17060 /* The die_type call above may have already set the type for this DIE. */
17061 type = get_die_type (die, cu);
17062 if (type)
17063 return type;
17064
17065 type = lookup_pointer_type (target_type);
8b2dbe47 17066
e142c38c 17067 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17068 if (attr_byte_size)
17069 byte_size = DW_UNSND (attr_byte_size);
c906108c 17070 else
8b2dbe47
KB
17071 byte_size = cu_header->addr_size;
17072
e142c38c 17073 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17074 if (attr_address_class)
17075 addr_class = DW_UNSND (attr_address_class);
17076 else
17077 addr_class = DW_ADDR_none;
17078
2b4424c3
TT
17079 ULONGEST alignment = get_alignment (cu, die);
17080
17081 /* If the pointer size, alignment, or address class is different
17082 than the default, create a type variant marked as such and set
17083 the length accordingly. */
17084 if (TYPE_LENGTH (type) != byte_size
17085 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17086 && alignment != TYPE_RAW_ALIGN (type))
17087 || addr_class != DW_ADDR_none)
c906108c 17088 {
5e2b427d 17089 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17090 {
17091 int type_flags;
17092
849957d9 17093 type_flags = gdbarch_address_class_type_flags
5e2b427d 17094 (gdbarch, byte_size, addr_class);
876cecd0
TT
17095 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17096 == 0);
8b2dbe47
KB
17097 type = make_type_with_address_space (type, type_flags);
17098 }
17099 else if (TYPE_LENGTH (type) != byte_size)
17100 {
b98664d3 17101 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17102 }
2b4424c3
TT
17103 else if (TYPE_RAW_ALIGN (type) != alignment)
17104 {
b98664d3 17105 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17106 " - DIE at %s [in module %s]"),
17107 sect_offset_str (die->sect_off),
17108 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17109 }
6e70227d 17110 else
9a619af0
MS
17111 {
17112 /* Should we also complain about unhandled address classes? */
17113 }
c906108c 17114 }
8b2dbe47
KB
17115
17116 TYPE_LENGTH (type) = byte_size;
2b4424c3 17117 set_type_align (type, alignment);
f792889a 17118 return set_die_type (die, type, cu);
c906108c
SS
17119}
17120
17121/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17122 the user defined type vector. */
17123
f792889a 17124static struct type *
e7c27a73 17125read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17126{
17127 struct type *type;
17128 struct type *to_type;
17129 struct type *domain;
17130
e7c27a73
DJ
17131 to_type = die_type (die, cu);
17132 domain = die_containing_type (die, cu);
0d5de010 17133
7e314c57
JK
17134 /* The calls above may have already set the type for this DIE. */
17135 type = get_die_type (die, cu);
17136 if (type)
17137 return type;
17138
0d5de010
DJ
17139 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17140 type = lookup_methodptr_type (to_type);
7078baeb
TT
17141 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17142 {
518817b3
SM
17143 struct type *new_type
17144 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17145
17146 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17147 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17148 TYPE_VARARGS (to_type));
17149 type = lookup_methodptr_type (new_type);
17150 }
0d5de010
DJ
17151 else
17152 type = lookup_memberptr_type (to_type, domain);
c906108c 17153
f792889a 17154 return set_die_type (die, type, cu);
c906108c
SS
17155}
17156
4297a3f0 17157/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17158 the user defined type vector. */
17159
f792889a 17160static struct type *
4297a3f0
AV
17161read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17162 enum type_code refcode)
c906108c 17163{
e7c27a73 17164 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17165 struct type *type, *target_type;
c906108c
SS
17166 struct attribute *attr;
17167
4297a3f0
AV
17168 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17169
7e314c57
JK
17170 target_type = die_type (die, cu);
17171
17172 /* The die_type call above may have already set the type for this DIE. */
17173 type = get_die_type (die, cu);
17174 if (type)
17175 return type;
17176
4297a3f0 17177 type = lookup_reference_type (target_type, refcode);
e142c38c 17178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17179 if (attr != nullptr)
c906108c
SS
17180 {
17181 TYPE_LENGTH (type) = DW_UNSND (attr);
17182 }
17183 else
17184 {
107d2387 17185 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17186 }
2b4424c3 17187 maybe_set_alignment (cu, die, type);
f792889a 17188 return set_die_type (die, type, cu);
c906108c
SS
17189}
17190
cf363f18
MW
17191/* Add the given cv-qualifiers to the element type of the array. GCC
17192 outputs DWARF type qualifiers that apply to an array, not the
17193 element type. But GDB relies on the array element type to carry
17194 the cv-qualifiers. This mimics section 6.7.3 of the C99
17195 specification. */
17196
17197static struct type *
17198add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17199 struct type *base_type, int cnst, int voltl)
17200{
17201 struct type *el_type, *inner_array;
17202
17203 base_type = copy_type (base_type);
17204 inner_array = base_type;
17205
17206 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17207 {
17208 TYPE_TARGET_TYPE (inner_array) =
17209 copy_type (TYPE_TARGET_TYPE (inner_array));
17210 inner_array = TYPE_TARGET_TYPE (inner_array);
17211 }
17212
17213 el_type = TYPE_TARGET_TYPE (inner_array);
17214 cnst |= TYPE_CONST (el_type);
17215 voltl |= TYPE_VOLATILE (el_type);
17216 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17217
17218 return set_die_type (die, base_type, cu);
17219}
17220
f792889a 17221static struct type *
e7c27a73 17222read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17223{
f792889a 17224 struct type *base_type, *cv_type;
c906108c 17225
e7c27a73 17226 base_type = die_type (die, cu);
7e314c57
JK
17227
17228 /* The die_type call above may have already set the type for this DIE. */
17229 cv_type = get_die_type (die, cu);
17230 if (cv_type)
17231 return cv_type;
17232
2f608a3a
KW
17233 /* In case the const qualifier is applied to an array type, the element type
17234 is so qualified, not the array type (section 6.7.3 of C99). */
17235 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17236 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17237
f792889a
DJ
17238 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17239 return set_die_type (die, cv_type, cu);
c906108c
SS
17240}
17241
f792889a 17242static struct type *
e7c27a73 17243read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17244{
f792889a 17245 struct type *base_type, *cv_type;
c906108c 17246
e7c27a73 17247 base_type = die_type (die, cu);
7e314c57
JK
17248
17249 /* The die_type call above may have already set the type for this DIE. */
17250 cv_type = get_die_type (die, cu);
17251 if (cv_type)
17252 return cv_type;
17253
cf363f18
MW
17254 /* In case the volatile qualifier is applied to an array type, the
17255 element type is so qualified, not the array type (section 6.7.3
17256 of C99). */
17257 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17258 return add_array_cv_type (die, cu, base_type, 0, 1);
17259
f792889a
DJ
17260 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17261 return set_die_type (die, cv_type, cu);
c906108c
SS
17262}
17263
06d66ee9
TT
17264/* Handle DW_TAG_restrict_type. */
17265
17266static struct type *
17267read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17268{
17269 struct type *base_type, *cv_type;
17270
17271 base_type = die_type (die, cu);
17272
17273 /* The die_type call above may have already set the type for this DIE. */
17274 cv_type = get_die_type (die, cu);
17275 if (cv_type)
17276 return cv_type;
17277
17278 cv_type = make_restrict_type (base_type);
17279 return set_die_type (die, cv_type, cu);
17280}
17281
a2c2acaf
MW
17282/* Handle DW_TAG_atomic_type. */
17283
17284static struct type *
17285read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17286{
17287 struct type *base_type, *cv_type;
17288
17289 base_type = die_type (die, cu);
17290
17291 /* The die_type call above may have already set the type for this DIE. */
17292 cv_type = get_die_type (die, cu);
17293 if (cv_type)
17294 return cv_type;
17295
17296 cv_type = make_atomic_type (base_type);
17297 return set_die_type (die, cv_type, cu);
17298}
17299
c906108c
SS
17300/* Extract all information from a DW_TAG_string_type DIE and add to
17301 the user defined type vector. It isn't really a user defined type,
17302 but it behaves like one, with other DIE's using an AT_user_def_type
17303 attribute to reference it. */
17304
f792889a 17305static struct type *
e7c27a73 17306read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17307{
518817b3 17308 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17309 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17310 struct type *type, *range_type, *index_type, *char_type;
17311 struct attribute *attr;
216a7e6b
AB
17312 struct dynamic_prop prop;
17313 bool length_is_constant = true;
17314 LONGEST length;
17315
17316 /* There are a couple of places where bit sizes might be made use of
17317 when parsing a DW_TAG_string_type, however, no producer that we know
17318 of make use of these. Handling bit sizes that are a multiple of the
17319 byte size is easy enough, but what about other bit sizes? Lets deal
17320 with that problem when we have to. Warn about these attributes being
17321 unsupported, then parse the type and ignore them like we always
17322 have. */
17323 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17324 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17325 {
17326 static bool warning_printed = false;
17327 if (!warning_printed)
17328 {
17329 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17330 "currently supported on DW_TAG_string_type."));
17331 warning_printed = true;
17332 }
17333 }
c906108c 17334
e142c38c 17335 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17336 if (attr != nullptr && !attr_form_is_constant (attr))
17337 {
17338 /* The string length describes the location at which the length of
17339 the string can be found. The size of the length field can be
17340 specified with one of the attributes below. */
17341 struct type *prop_type;
17342 struct attribute *len
17343 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17344 if (len == nullptr)
17345 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17346 if (len != nullptr && attr_form_is_constant (len))
17347 {
17348 /* Pass 0 as the default as we know this attribute is constant
17349 and the default value will not be returned. */
17350 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17351 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17352 }
17353 else
17354 {
17355 /* If the size is not specified then we assume it is the size of
17356 an address on this target. */
17357 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17358 }
17359
17360 /* Convert the attribute into a dynamic property. */
17361 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17362 length = 1;
17363 else
17364 length_is_constant = false;
17365 }
17366 else if (attr != nullptr)
17367 {
17368 /* This DW_AT_string_length just contains the length with no
17369 indirection. There's no need to create a dynamic property in this
17370 case. Pass 0 for the default value as we know it will not be
17371 returned in this case. */
17372 length = dwarf2_get_attr_constant_value (attr, 0);
17373 }
17374 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17375 {
216a7e6b
AB
17376 /* We don't currently support non-constant byte sizes for strings. */
17377 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17378 }
17379 else
17380 {
216a7e6b
AB
17381 /* Use 1 as a fallback length if we have nothing else. */
17382 length = 1;
c906108c 17383 }
6ccb9162 17384
46bf5051 17385 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17386 if (length_is_constant)
17387 range_type = create_static_range_type (NULL, index_type, 1, length);
17388 else
17389 {
17390 struct dynamic_prop low_bound;
17391
17392 low_bound.kind = PROP_CONST;
17393 low_bound.data.const_val = 1;
17394 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17395 }
3b7538c0
UW
17396 char_type = language_string_char_type (cu->language_defn, gdbarch);
17397 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17398
f792889a 17399 return set_die_type (die, type, cu);
c906108c
SS
17400}
17401
4d804846
JB
17402/* Assuming that DIE corresponds to a function, returns nonzero
17403 if the function is prototyped. */
17404
17405static int
17406prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17407{
17408 struct attribute *attr;
17409
17410 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17411 if (attr && (DW_UNSND (attr) != 0))
17412 return 1;
17413
17414 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17415 is only meaningful for C, but the concept also extends to other
4d804846
JB
17416 languages that allow unprototyped functions (Eg: Objective C).
17417 For all other languages, assume that functions are always
17418 prototyped. */
17419 if (cu->language != language_c
17420 && cu->language != language_objc
17421 && cu->language != language_opencl)
17422 return 1;
17423
17424 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17425 prototyped and unprototyped functions; default to prototyped,
17426 since that is more common in modern code (and RealView warns
17427 about unprototyped functions). */
17428 if (producer_is_realview (cu->producer))
17429 return 1;
17430
17431 return 0;
17432}
17433
c906108c
SS
17434/* Handle DIES due to C code like:
17435
17436 struct foo
c5aa993b
JM
17437 {
17438 int (*funcp)(int a, long l);
17439 int b;
17440 };
c906108c 17441
0963b4bd 17442 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17443
f792889a 17444static struct type *
e7c27a73 17445read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17446{
518817b3 17447 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17448 struct type *type; /* Type that this function returns. */
17449 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17450 struct attribute *attr;
17451
e7c27a73 17452 type = die_type (die, cu);
7e314c57
JK
17453
17454 /* The die_type call above may have already set the type for this DIE. */
17455 ftype = get_die_type (die, cu);
17456 if (ftype)
17457 return ftype;
17458
0c8b41f1 17459 ftype = lookup_function_type (type);
c906108c 17460
4d804846 17461 if (prototyped_function_p (die, cu))
a6c727b2 17462 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17463
c055b101
CV
17464 /* Store the calling convention in the type if it's available in
17465 the subroutine die. Otherwise set the calling convention to
17466 the default value DW_CC_normal. */
17467 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17468 if (attr != nullptr
17469 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17470 TYPE_CALLING_CONVENTION (ftype)
17471 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17472 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17473 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17474 else
17475 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17476
743649fd
MW
17477 /* Record whether the function returns normally to its caller or not
17478 if the DWARF producer set that information. */
17479 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17480 if (attr && (DW_UNSND (attr) != 0))
17481 TYPE_NO_RETURN (ftype) = 1;
17482
76c10ea2
GM
17483 /* We need to add the subroutine type to the die immediately so
17484 we don't infinitely recurse when dealing with parameters
0963b4bd 17485 declared as the same subroutine type. */
76c10ea2 17486 set_die_type (die, ftype, cu);
6e70227d 17487
639d11d3 17488 if (die->child != NULL)
c906108c 17489 {
bb5ed363 17490 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17491 struct die_info *child_die;
8072405b 17492 int nparams, iparams;
c906108c
SS
17493
17494 /* Count the number of parameters.
17495 FIXME: GDB currently ignores vararg functions, but knows about
17496 vararg member functions. */
8072405b 17497 nparams = 0;
639d11d3 17498 child_die = die->child;
c906108c
SS
17499 while (child_die && child_die->tag)
17500 {
17501 if (child_die->tag == DW_TAG_formal_parameter)
17502 nparams++;
17503 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17504 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17505 child_die = sibling_die (child_die);
17506 }
17507
17508 /* Allocate storage for parameters and fill them in. */
17509 TYPE_NFIELDS (ftype) = nparams;
17510 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17511 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17512
8072405b
JK
17513 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17514 even if we error out during the parameters reading below. */
17515 for (iparams = 0; iparams < nparams; iparams++)
17516 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17517
17518 iparams = 0;
639d11d3 17519 child_die = die->child;
c906108c
SS
17520 while (child_die && child_die->tag)
17521 {
17522 if (child_die->tag == DW_TAG_formal_parameter)
17523 {
3ce3b1ba
PA
17524 struct type *arg_type;
17525
17526 /* DWARF version 2 has no clean way to discern C++
17527 static and non-static member functions. G++ helps
17528 GDB by marking the first parameter for non-static
17529 member functions (which is the this pointer) as
17530 artificial. We pass this information to
17531 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17532
17533 DWARF version 3 added DW_AT_object_pointer, which GCC
17534 4.5 does not yet generate. */
e142c38c 17535 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17536 if (attr != nullptr)
c906108c
SS
17537 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17538 else
9c37b5ae 17539 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17540 arg_type = die_type (child_die, cu);
17541
17542 /* RealView does not mark THIS as const, which the testsuite
17543 expects. GCC marks THIS as const in method definitions,
17544 but not in the class specifications (GCC PR 43053). */
17545 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17546 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17547 {
17548 int is_this = 0;
17549 struct dwarf2_cu *arg_cu = cu;
17550 const char *name = dwarf2_name (child_die, cu);
17551
17552 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17553 if (attr != nullptr)
3ce3b1ba
PA
17554 {
17555 /* If the compiler emits this, use it. */
17556 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17557 is_this = 1;
17558 }
17559 else if (name && strcmp (name, "this") == 0)
17560 /* Function definitions will have the argument names. */
17561 is_this = 1;
17562 else if (name == NULL && iparams == 0)
17563 /* Declarations may not have the names, so like
17564 elsewhere in GDB, assume an artificial first
17565 argument is "this". */
17566 is_this = 1;
17567
17568 if (is_this)
17569 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17570 arg_type, 0);
17571 }
17572
17573 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17574 iparams++;
17575 }
17576 child_die = sibling_die (child_die);
17577 }
17578 }
17579
76c10ea2 17580 return ftype;
c906108c
SS
17581}
17582
f792889a 17583static struct type *
e7c27a73 17584read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17585{
518817b3 17586 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17587 const char *name = NULL;
3c8e0968 17588 struct type *this_type, *target_type;
c906108c 17589
94af9270 17590 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17591 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17592 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17593 set_die_type (die, this_type, cu);
3c8e0968
DE
17594 target_type = die_type (die, cu);
17595 if (target_type != this_type)
17596 TYPE_TARGET_TYPE (this_type) = target_type;
17597 else
17598 {
17599 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17600 spec and cause infinite loops in GDB. */
b98664d3 17601 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17602 "- DIE at %s [in module %s]"),
17603 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17604 TYPE_TARGET_TYPE (this_type) = NULL;
17605 }
f792889a 17606 return this_type;
c906108c
SS
17607}
17608
9b790ce7
UW
17609/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17610 (which may be different from NAME) to the architecture back-end to allow
17611 it to guess the correct format if necessary. */
17612
17613static struct type *
17614dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17615 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17616{
17617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17618 const struct floatformat **format;
17619 struct type *type;
17620
17621 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17622 if (format)
103a685e 17623 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17624 else
77b7c781 17625 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17626
17627 return type;
17628}
17629
eb77c9df
AB
17630/* Allocate an integer type of size BITS and name NAME. */
17631
17632static struct type *
17633dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17634 int bits, int unsigned_p, const char *name)
17635{
17636 struct type *type;
17637
17638 /* Versions of Intel's C Compiler generate an integer type called "void"
17639 instead of using DW_TAG_unspecified_type. This has been seen on
17640 at least versions 14, 17, and 18. */
35ee2dc2
AB
17641 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17642 && strcmp (name, "void") == 0)
eb77c9df
AB
17643 type = objfile_type (objfile)->builtin_void;
17644 else
17645 type = init_integer_type (objfile, bits, unsigned_p, name);
17646
17647 return type;
17648}
17649
8bdc1658
AB
17650/* Initialise and return a floating point type of size BITS suitable for
17651 use as a component of a complex number. The NAME_HINT is passed through
17652 when initialising the floating point type and is the name of the complex
17653 type.
17654
17655 As DWARF doesn't currently provide an explicit name for the components
17656 of a complex number, but it can be helpful to have these components
17657 named, we try to select a suitable name based on the size of the
17658 component. */
17659static struct type *
17660dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17661 struct objfile *objfile,
103a685e
TT
17662 int bits, const char *name_hint,
17663 enum bfd_endian byte_order)
8bdc1658
AB
17664{
17665 gdbarch *gdbarch = get_objfile_arch (objfile);
17666 struct type *tt = nullptr;
17667
35add35e
AB
17668 /* Try to find a suitable floating point builtin type of size BITS.
17669 We're going to use the name of this type as the name for the complex
17670 target type that we are about to create. */
1db455a7 17671 switch (cu->language)
8bdc1658 17672 {
1db455a7
AB
17673 case language_fortran:
17674 switch (bits)
17675 {
17676 case 32:
17677 tt = builtin_f_type (gdbarch)->builtin_real;
17678 break;
17679 case 64:
17680 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17681 break;
17682 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17683 case 128:
17684 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17685 break;
17686 }
8bdc1658 17687 break;
1db455a7
AB
17688 default:
17689 switch (bits)
17690 {
17691 case 32:
17692 tt = builtin_type (gdbarch)->builtin_float;
17693 break;
17694 case 64:
17695 tt = builtin_type (gdbarch)->builtin_double;
17696 break;
17697 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17698 case 128:
17699 tt = builtin_type (gdbarch)->builtin_long_double;
17700 break;
17701 }
8bdc1658
AB
17702 break;
17703 }
17704
35add35e
AB
17705 /* If the type we found doesn't match the size we were looking for, then
17706 pretend we didn't find a type at all, the complex target type we
17707 create will then be nameless. */
a12e5744 17708 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17709 tt = nullptr;
17710
8bdc1658 17711 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17712 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17713}
17714
c906108c
SS
17715/* Find a representation of a given base type and install
17716 it in the TYPE field of the die. */
17717
f792889a 17718static struct type *
e7c27a73 17719read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17720{
518817b3 17721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17722 struct type *type;
17723 struct attribute *attr;
19f392bc 17724 int encoding = 0, bits = 0;
15d034d0 17725 const char *name;
34877895 17726 gdbarch *arch;
c906108c 17727
e142c38c 17728 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17729 if (attr != nullptr)
34877895 17730 encoding = DW_UNSND (attr);
e142c38c 17731 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17732 if (attr != nullptr)
34877895 17733 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17734 name = dwarf2_name (die, cu);
6ccb9162 17735 if (!name)
34877895 17736 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17737
17738 arch = get_objfile_arch (objfile);
17739 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17740
34877895
PJ
17741 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17742 if (attr)
103a685e
TT
17743 {
17744 int endianity = DW_UNSND (attr);
17745
17746 switch (endianity)
17747 {
17748 case DW_END_big:
17749 byte_order = BFD_ENDIAN_BIG;
17750 break;
17751 case DW_END_little:
17752 byte_order = BFD_ENDIAN_LITTLE;
17753 break;
17754 default:
17755 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17756 break;
17757 }
17758 }
6ccb9162
UW
17759
17760 switch (encoding)
c906108c 17761 {
6ccb9162
UW
17762 case DW_ATE_address:
17763 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17764 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17765 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17766 break;
17767 case DW_ATE_boolean:
19f392bc 17768 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17769 break;
17770 case DW_ATE_complex_float:
103a685e
TT
17771 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17772 byte_order);
19f392bc 17773 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17774 break;
17775 case DW_ATE_decimal_float:
19f392bc 17776 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17777 break;
17778 case DW_ATE_float:
103a685e 17779 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17780 break;
17781 case DW_ATE_signed:
eb77c9df 17782 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17783 break;
17784 case DW_ATE_unsigned:
3b2b8fea
TT
17785 if (cu->language == language_fortran
17786 && name
61012eef 17787 && startswith (name, "character("))
19f392bc
UW
17788 type = init_character_type (objfile, bits, 1, name);
17789 else
eb77c9df 17790 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17791 break;
17792 case DW_ATE_signed_char:
6e70227d 17793 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17794 || cu->language == language_pascal
17795 || cu->language == language_fortran)
19f392bc
UW
17796 type = init_character_type (objfile, bits, 0, name);
17797 else
eb77c9df 17798 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17799 break;
17800 case DW_ATE_unsigned_char:
868a0084 17801 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17802 || cu->language == language_pascal
c44af4eb
TT
17803 || cu->language == language_fortran
17804 || cu->language == language_rust)
19f392bc
UW
17805 type = init_character_type (objfile, bits, 1, name);
17806 else
eb77c9df 17807 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17808 break;
75079b2b 17809 case DW_ATE_UTF:
53e710ac 17810 {
53e710ac
PA
17811 if (bits == 16)
17812 type = builtin_type (arch)->builtin_char16;
17813 else if (bits == 32)
17814 type = builtin_type (arch)->builtin_char32;
17815 else
17816 {
b98664d3 17817 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17818 bits);
eb77c9df 17819 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17820 }
17821 return set_die_type (die, type, cu);
17822 }
75079b2b
TT
17823 break;
17824
6ccb9162 17825 default:
b98664d3 17826 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17827 dwarf_type_encoding_name (encoding));
77b7c781 17828 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17829 break;
c906108c 17830 }
6ccb9162 17831
0114d602 17832 if (name && strcmp (name, "char") == 0)
876cecd0 17833 TYPE_NOSIGN (type) = 1;
0114d602 17834
2b4424c3
TT
17835 maybe_set_alignment (cu, die, type);
17836
103a685e 17837 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17838
f792889a 17839 return set_die_type (die, type, cu);
c906108c
SS
17840}
17841
80180f79
SA
17842/* Parse dwarf attribute if it's a block, reference or constant and put the
17843 resulting value of the attribute into struct bound_prop.
17844 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17845
17846static int
17847attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17848 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17849 struct type *default_type)
80180f79
SA
17850{
17851 struct dwarf2_property_baton *baton;
518817b3
SM
17852 struct obstack *obstack
17853 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17854
9a49df9d
AB
17855 gdb_assert (default_type != NULL);
17856
80180f79
SA
17857 if (attr == NULL || prop == NULL)
17858 return 0;
17859
17860 if (attr_form_is_block (attr))
17861 {
8d749320 17862 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17863 baton->property_type = default_type;
80180f79
SA
17864 baton->locexpr.per_cu = cu->per_cu;
17865 baton->locexpr.size = DW_BLOCK (attr)->size;
17866 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17867 switch (attr->name)
17868 {
17869 case DW_AT_string_length:
17870 baton->locexpr.is_reference = true;
17871 break;
17872 default:
17873 baton->locexpr.is_reference = false;
17874 break;
17875 }
80180f79
SA
17876 prop->data.baton = baton;
17877 prop->kind = PROP_LOCEXPR;
17878 gdb_assert (prop->data.baton != NULL);
17879 }
17880 else if (attr_form_is_ref (attr))
17881 {
17882 struct dwarf2_cu *target_cu = cu;
17883 struct die_info *target_die;
17884 struct attribute *target_attr;
17885
17886 target_die = follow_die_ref (die, attr, &target_cu);
17887 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17888 if (target_attr == NULL)
17889 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17890 target_cu);
80180f79
SA
17891 if (target_attr == NULL)
17892 return 0;
17893
df25ebbd 17894 switch (target_attr->name)
80180f79 17895 {
df25ebbd
JB
17896 case DW_AT_location:
17897 if (attr_form_is_section_offset (target_attr))
17898 {
8d749320 17899 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17900 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17901 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17902 prop->data.baton = baton;
17903 prop->kind = PROP_LOCLIST;
17904 gdb_assert (prop->data.baton != NULL);
17905 }
17906 else if (attr_form_is_block (target_attr))
17907 {
8d749320 17908 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17909 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17910 baton->locexpr.per_cu = cu->per_cu;
17911 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17912 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17913 baton->locexpr.is_reference = true;
df25ebbd
JB
17914 prop->data.baton = baton;
17915 prop->kind = PROP_LOCEXPR;
17916 gdb_assert (prop->data.baton != NULL);
17917 }
17918 else
17919 {
17920 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17921 "dynamic property");
17922 return 0;
17923 }
17924 break;
17925 case DW_AT_data_member_location:
17926 {
17927 LONGEST offset;
17928
17929 if (!handle_data_member_location (target_die, target_cu,
17930 &offset))
17931 return 0;
17932
8d749320 17933 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17934 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17935 target_cu);
df25ebbd
JB
17936 baton->offset_info.offset = offset;
17937 baton->offset_info.type = die_type (target_die, target_cu);
17938 prop->data.baton = baton;
17939 prop->kind = PROP_ADDR_OFFSET;
17940 break;
17941 }
80180f79
SA
17942 }
17943 }
17944 else if (attr_form_is_constant (attr))
17945 {
17946 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17947 prop->kind = PROP_CONST;
17948 }
17949 else
17950 {
17951 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17952 dwarf2_name (die, cu));
17953 return 0;
17954 }
17955
17956 return 1;
17957}
17958
11a8b164
AB
17959/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17960 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17961
17962static struct type *
11a8b164
AB
17963dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17964 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17965{
17966 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17967 struct type *int_type;
17968
17969 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17970#define TRY_TYPE(F) \
17971 int_type = (unsigned_p \
17972 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17973 : objfile_type (objfile)->builtin_ ## F); \
17974 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17975 return int_type
17976
17977 TRY_TYPE (char);
17978 TRY_TYPE (short);
17979 TRY_TYPE (int);
17980 TRY_TYPE (long);
17981 TRY_TYPE (long_long);
17982
17983#undef TRY_TYPE
17984
17985 gdb_assert_not_reached ("unable to find suitable integer type");
17986}
17987
11a8b164
AB
17988/* Find an integer type the same size as the address size given in the
17989 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17990 is unsigned or not. */
17991
17992static struct type *
17993dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17994 bool unsigned_p)
17995{
17996 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17997 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17998}
17999
b86352cf
AB
18000/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18001 present (which is valid) then compute the default type based on the
18002 compilation units address size. */
18003
18004static struct type *
18005read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18006{
18007 struct type *index_type = die_type (die, cu);
18008
18009 /* Dwarf-2 specifications explicitly allows to create subrange types
18010 without specifying a base type.
18011 In that case, the base type must be set to the type of
18012 the lower bound, upper bound or count, in that order, if any of these
18013 three attributes references an object that has a type.
18014 If no base type is found, the Dwarf-2 specifications say that
18015 a signed integer type of size equal to the size of an address should
18016 be used.
18017 For the following C code: `extern char gdb_int [];'
18018 GCC produces an empty range DIE.
18019 FIXME: muller/2010-05-28: Possible references to object for low bound,
18020 high bound or count are not yet handled by this code. */
18021 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18022 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18023
18024 return index_type;
18025}
18026
a02abb62
JB
18027/* Read the given DW_AT_subrange DIE. */
18028
f792889a 18029static struct type *
a02abb62
JB
18030read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18031{
4c9ad8c2 18032 struct type *base_type, *orig_base_type;
a02abb62
JB
18033 struct type *range_type;
18034 struct attribute *attr;
729efb13 18035 struct dynamic_prop low, high;
4fae6e18 18036 int low_default_is_valid;
c451ebe5 18037 int high_bound_is_count = 0;
15d034d0 18038 const char *name;
d359392f 18039 ULONGEST negative_mask;
e77813c8 18040
b86352cf
AB
18041 orig_base_type = read_subrange_index_type (die, cu);
18042
4c9ad8c2
TT
18043 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18044 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18045 creating the range type, but we use the result of check_typedef
18046 when examining properties of the type. */
18047 base_type = check_typedef (orig_base_type);
a02abb62 18048
7e314c57
JK
18049 /* The die_type call above may have already set the type for this DIE. */
18050 range_type = get_die_type (die, cu);
18051 if (range_type)
18052 return range_type;
18053
729efb13
SA
18054 low.kind = PROP_CONST;
18055 high.kind = PROP_CONST;
18056 high.data.const_val = 0;
18057
4fae6e18
JK
18058 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18059 omitting DW_AT_lower_bound. */
18060 switch (cu->language)
6e70227d 18061 {
4fae6e18
JK
18062 case language_c:
18063 case language_cplus:
729efb13 18064 low.data.const_val = 0;
4fae6e18
JK
18065 low_default_is_valid = 1;
18066 break;
18067 case language_fortran:
729efb13 18068 low.data.const_val = 1;
4fae6e18
JK
18069 low_default_is_valid = 1;
18070 break;
18071 case language_d:
4fae6e18 18072 case language_objc:
c44af4eb 18073 case language_rust:
729efb13 18074 low.data.const_val = 0;
4fae6e18
JK
18075 low_default_is_valid = (cu->header.version >= 4);
18076 break;
18077 case language_ada:
18078 case language_m2:
18079 case language_pascal:
729efb13 18080 low.data.const_val = 1;
4fae6e18
JK
18081 low_default_is_valid = (cu->header.version >= 4);
18082 break;
18083 default:
729efb13 18084 low.data.const_val = 0;
4fae6e18
JK
18085 low_default_is_valid = 0;
18086 break;
a02abb62
JB
18087 }
18088
e142c38c 18089 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18090 if (attr != nullptr)
9a49df9d 18091 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18092 else if (!low_default_is_valid)
b98664d3 18093 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18094 "- DIE at %s [in module %s]"),
18095 sect_offset_str (die->sect_off),
518817b3 18096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18097
506f5c41
TV
18098 struct attribute *attr_ub, *attr_count;
18099 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18100 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18101 {
506f5c41 18102 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18103 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18104 {
c451ebe5
SA
18105 /* If bounds are constant do the final calculation here. */
18106 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18107 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18108 else
18109 high_bound_is_count = 1;
c2ff108b 18110 }
506f5c41
TV
18111 else
18112 {
18113 if (attr_ub != NULL)
18114 complaint (_("Unresolved DW_AT_upper_bound "
18115 "- DIE at %s [in module %s]"),
18116 sect_offset_str (die->sect_off),
18117 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18118 if (attr_count != NULL)
18119 complaint (_("Unresolved DW_AT_count "
18120 "- DIE at %s [in module %s]"),
18121 sect_offset_str (die->sect_off),
18122 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18123 }
e77813c8 18124 }
a02abb62 18125
4e962e74
TT
18126 LONGEST bias = 0;
18127 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18128 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18129 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18130
dbb9c2b1
JB
18131 /* Normally, the DWARF producers are expected to use a signed
18132 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18133 But this is unfortunately not always the case, as witnessed
18134 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18135 is used instead. To work around that ambiguity, we treat
18136 the bounds as signed, and thus sign-extend their values, when
18137 the base type is signed. */
6e70227d 18138 negative_mask =
d359392f 18139 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18140 if (low.kind == PROP_CONST
18141 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18142 low.data.const_val |= negative_mask;
18143 if (high.kind == PROP_CONST
18144 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18145 high.data.const_val |= negative_mask;
43bbcdc2 18146
5bbd8269
AB
18147 /* Check for bit and byte strides. */
18148 struct dynamic_prop byte_stride_prop;
18149 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18150 if (attr_byte_stride != nullptr)
18151 {
18152 struct type *prop_type
18153 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18154 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18155 prop_type);
18156 }
18157
18158 struct dynamic_prop bit_stride_prop;
18159 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18160 if (attr_bit_stride != nullptr)
18161 {
18162 /* It only makes sense to have either a bit or byte stride. */
18163 if (attr_byte_stride != nullptr)
18164 {
18165 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18166 "- DIE at %s [in module %s]"),
18167 sect_offset_str (die->sect_off),
18168 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18169 attr_bit_stride = nullptr;
18170 }
18171 else
18172 {
18173 struct type *prop_type
18174 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18175 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18176 prop_type);
18177 }
18178 }
18179
18180 if (attr_byte_stride != nullptr
18181 || attr_bit_stride != nullptr)
18182 {
18183 bool byte_stride_p = (attr_byte_stride != nullptr);
18184 struct dynamic_prop *stride
18185 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18186
18187 range_type
18188 = create_range_type_with_stride (NULL, orig_base_type, &low,
18189 &high, bias, stride, byte_stride_p);
18190 }
18191 else
18192 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18193
c451ebe5
SA
18194 if (high_bound_is_count)
18195 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18196
c2ff108b
JK
18197 /* Ada expects an empty array on no boundary attributes. */
18198 if (attr == NULL && cu->language != language_ada)
729efb13 18199 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18200
39cbfefa
DJ
18201 name = dwarf2_name (die, cu);
18202 if (name)
18203 TYPE_NAME (range_type) = name;
6e70227d 18204
e142c38c 18205 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18206 if (attr != nullptr)
a02abb62
JB
18207 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18208
2b4424c3
TT
18209 maybe_set_alignment (cu, die, range_type);
18210
7e314c57
JK
18211 set_die_type (die, range_type, cu);
18212
18213 /* set_die_type should be already done. */
b4ba55a1
JB
18214 set_descriptive_type (range_type, die, cu);
18215
7e314c57 18216 return range_type;
a02abb62 18217}
6e70227d 18218
f792889a 18219static struct type *
81a17f79
JB
18220read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18221{
18222 struct type *type;
81a17f79 18223
518817b3
SM
18224 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18225 NULL);
0114d602 18226 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18227
74a2f8ff 18228 /* In Ada, an unspecified type is typically used when the description
85102364 18229 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18230 such a type, we treat it as a stub, and try to resolve it later on,
18231 when needed. */
18232 if (cu->language == language_ada)
18233 TYPE_STUB (type) = 1;
18234
f792889a 18235 return set_die_type (die, type, cu);
81a17f79 18236}
a02abb62 18237
639d11d3
DC
18238/* Read a single die and all its descendents. Set the die's sibling
18239 field to NULL; set other fields in the die correctly, and set all
18240 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18241 location of the info_ptr after reading all of those dies. PARENT
18242 is the parent of the die in question. */
18243
18244static struct die_info *
dee91e82 18245read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18246 const gdb_byte *info_ptr,
18247 const gdb_byte **new_info_ptr,
dee91e82 18248 struct die_info *parent)
639d11d3
DC
18249{
18250 struct die_info *die;
d521ce57 18251 const gdb_byte *cur_ptr;
639d11d3
DC
18252 int has_children;
18253
bf6af496 18254 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18255 if (die == NULL)
18256 {
18257 *new_info_ptr = cur_ptr;
18258 return NULL;
18259 }
93311388 18260 store_in_ref_table (die, reader->cu);
639d11d3
DC
18261
18262 if (has_children)
bf6af496 18263 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18264 else
18265 {
18266 die->child = NULL;
18267 *new_info_ptr = cur_ptr;
18268 }
18269
18270 die->sibling = NULL;
18271 die->parent = parent;
18272 return die;
18273}
18274
18275/* Read a die, all of its descendents, and all of its siblings; set
18276 all of the fields of all of the dies correctly. Arguments are as
18277 in read_die_and_children. */
18278
18279static struct die_info *
bf6af496 18280read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18281 const gdb_byte *info_ptr,
18282 const gdb_byte **new_info_ptr,
bf6af496 18283 struct die_info *parent)
639d11d3
DC
18284{
18285 struct die_info *first_die, *last_sibling;
d521ce57 18286 const gdb_byte *cur_ptr;
639d11d3 18287
c906108c 18288 cur_ptr = info_ptr;
639d11d3
DC
18289 first_die = last_sibling = NULL;
18290
18291 while (1)
c906108c 18292 {
639d11d3 18293 struct die_info *die
dee91e82 18294 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18295
1d325ec1 18296 if (die == NULL)
c906108c 18297 {
639d11d3
DC
18298 *new_info_ptr = cur_ptr;
18299 return first_die;
c906108c 18300 }
1d325ec1
DJ
18301
18302 if (!first_die)
18303 first_die = die;
c906108c 18304 else
1d325ec1
DJ
18305 last_sibling->sibling = die;
18306
18307 last_sibling = die;
c906108c 18308 }
c906108c
SS
18309}
18310
bf6af496
DE
18311/* Read a die, all of its descendents, and all of its siblings; set
18312 all of the fields of all of the dies correctly. Arguments are as
18313 in read_die_and_children.
18314 This the main entry point for reading a DIE and all its children. */
18315
18316static struct die_info *
18317read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18318 const gdb_byte *info_ptr,
18319 const gdb_byte **new_info_ptr,
bf6af496
DE
18320 struct die_info *parent)
18321{
18322 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18323 new_info_ptr, parent);
18324
b4f54984 18325 if (dwarf_die_debug)
bf6af496
DE
18326 {
18327 fprintf_unfiltered (gdb_stdlog,
18328 "Read die from %s@0x%x of %s:\n",
a32a8923 18329 get_section_name (reader->die_section),
bf6af496
DE
18330 (unsigned) (info_ptr - reader->die_section->buffer),
18331 bfd_get_filename (reader->abfd));
b4f54984 18332 dump_die (die, dwarf_die_debug);
bf6af496
DE
18333 }
18334
18335 return die;
18336}
18337
3019eac3
DE
18338/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18339 attributes.
18340 The caller is responsible for filling in the extra attributes
18341 and updating (*DIEP)->num_attrs.
18342 Set DIEP to point to a newly allocated die with its information,
18343 except for its child, sibling, and parent fields.
18344 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18345
d521ce57 18346static const gdb_byte *
3019eac3 18347read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18348 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18349 int *has_children, int num_extra_attrs)
93311388 18350{
b64f50a1 18351 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18352 struct abbrev_info *abbrev;
18353 struct die_info *die;
18354 struct dwarf2_cu *cu = reader->cu;
18355 bfd *abfd = reader->abfd;
18356
9c541725 18357 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18359 info_ptr += bytes_read;
18360 if (!abbrev_number)
18361 {
18362 *diep = NULL;
18363 *has_children = 0;
18364 return info_ptr;
18365 }
18366
685af9cd 18367 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18368 if (!abbrev)
348e048f
DE
18369 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18370 abbrev_number,
18371 bfd_get_filename (abfd));
18372
3019eac3 18373 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18374 die->sect_off = sect_off;
93311388
DE
18375 die->tag = abbrev->tag;
18376 die->abbrev = abbrev_number;
18377
3019eac3
DE
18378 /* Make the result usable.
18379 The caller needs to update num_attrs after adding the extra
18380 attributes. */
93311388
DE
18381 die->num_attrs = abbrev->num_attrs;
18382
18a8505e 18383 std::vector<int> indexes_that_need_reprocess;
93311388 18384 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18385 {
18386 bool need_reprocess;
18387 info_ptr =
18388 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18389 info_ptr, &need_reprocess);
18390 if (need_reprocess)
18391 indexes_that_need_reprocess.push_back (i);
18392 }
18393
18394 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18395 if (attr != nullptr)
18396 cu->str_offsets_base = DW_UNSND (attr);
93311388 18397
18a8505e
AT
18398 auto maybe_addr_base = lookup_addr_base(die);
18399 if (maybe_addr_base.has_value ())
18400 cu->addr_base = *maybe_addr_base;
18401 for (int index : indexes_that_need_reprocess)
18402 read_attribute_reprocess (reader, &die->attrs[index]);
93311388
DE
18403 *diep = die;
18404 *has_children = abbrev->has_children;
18405 return info_ptr;
18406}
18407
3019eac3
DE
18408/* Read a die and all its attributes.
18409 Set DIEP to point to a newly allocated die with its information,
18410 except for its child, sibling, and parent fields.
18411 Set HAS_CHILDREN to tell whether the die has children or not. */
18412
d521ce57 18413static const gdb_byte *
3019eac3 18414read_full_die (const struct die_reader_specs *reader,
d521ce57 18415 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18416 int *has_children)
18417{
d521ce57 18418 const gdb_byte *result;
bf6af496
DE
18419
18420 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18421
b4f54984 18422 if (dwarf_die_debug)
bf6af496
DE
18423 {
18424 fprintf_unfiltered (gdb_stdlog,
18425 "Read die from %s@0x%x of %s:\n",
a32a8923 18426 get_section_name (reader->die_section),
bf6af496
DE
18427 (unsigned) (info_ptr - reader->die_section->buffer),
18428 bfd_get_filename (reader->abfd));
b4f54984 18429 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18430 }
18431
18432 return result;
3019eac3 18433}
433df2d4
DE
18434\f
18435/* Abbreviation tables.
3019eac3 18436
433df2d4 18437 In DWARF version 2, the description of the debugging information is
c906108c
SS
18438 stored in a separate .debug_abbrev section. Before we read any
18439 dies from a section we read in all abbreviations and install them
433df2d4
DE
18440 in a hash table. */
18441
18442/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18443
685af9cd
TT
18444struct abbrev_info *
18445abbrev_table::alloc_abbrev ()
433df2d4
DE
18446{
18447 struct abbrev_info *abbrev;
18448
685af9cd 18449 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18450 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18451
433df2d4
DE
18452 return abbrev;
18453}
18454
18455/* Add an abbreviation to the table. */
c906108c 18456
685af9cd
TT
18457void
18458abbrev_table::add_abbrev (unsigned int abbrev_number,
18459 struct abbrev_info *abbrev)
433df2d4
DE
18460{
18461 unsigned int hash_number;
18462
18463 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18464 abbrev->next = m_abbrevs[hash_number];
18465 m_abbrevs[hash_number] = abbrev;
433df2d4 18466}
dee91e82 18467
433df2d4
DE
18468/* Look up an abbrev in the table.
18469 Returns NULL if the abbrev is not found. */
18470
685af9cd
TT
18471struct abbrev_info *
18472abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18473{
433df2d4
DE
18474 unsigned int hash_number;
18475 struct abbrev_info *abbrev;
18476
18477 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18478 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18479
18480 while (abbrev)
18481 {
18482 if (abbrev->number == abbrev_number)
18483 return abbrev;
18484 abbrev = abbrev->next;
18485 }
18486 return NULL;
18487}
18488
18489/* Read in an abbrev table. */
18490
685af9cd 18491static abbrev_table_up
ed2dc618
SM
18492abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18493 struct dwarf2_section_info *section,
9c541725 18494 sect_offset sect_off)
433df2d4
DE
18495{
18496 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18497 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18498 const gdb_byte *abbrev_ptr;
c906108c
SS
18499 struct abbrev_info *cur_abbrev;
18500 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18501 unsigned int abbrev_form;
6dfa2fc2 18502 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18503
685af9cd 18504 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18505
433df2d4 18506 dwarf2_read_section (objfile, section);
9c541725 18507 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18508 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18509 abbrev_ptr += bytes_read;
18510
0963b4bd 18511 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18512 while (abbrev_number)
18513 {
6dfa2fc2 18514 cur_attrs.clear ();
685af9cd 18515 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18516
18517 /* read in abbrev header */
18518 cur_abbrev->number = abbrev_number;
aead7601
SM
18519 cur_abbrev->tag
18520 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18521 abbrev_ptr += bytes_read;
18522 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18523 abbrev_ptr += 1;
18524
18525 /* now read in declarations */
22d2f3ab 18526 for (;;)
c906108c 18527 {
43988095
JK
18528 LONGEST implicit_const;
18529
22d2f3ab
JK
18530 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18531 abbrev_ptr += bytes_read;
18532 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18533 abbrev_ptr += bytes_read;
43988095
JK
18534 if (abbrev_form == DW_FORM_implicit_const)
18535 {
18536 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18537 &bytes_read);
18538 abbrev_ptr += bytes_read;
18539 }
18540 else
18541 {
18542 /* Initialize it due to a false compiler warning. */
18543 implicit_const = -1;
18544 }
22d2f3ab
JK
18545
18546 if (abbrev_name == 0)
18547 break;
18548
6dfa2fc2
TT
18549 cur_attrs.emplace_back ();
18550 struct attr_abbrev &cur_attr = cur_attrs.back ();
18551 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18552 cur_attr.form = (enum dwarf_form) abbrev_form;
18553 cur_attr.implicit_const = implicit_const;
22d2f3ab 18554 ++cur_abbrev->num_attrs;
c906108c
SS
18555 }
18556
8d749320
SM
18557 cur_abbrev->attrs =
18558 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18559 cur_abbrev->num_attrs);
6dfa2fc2 18560 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18561 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18562
685af9cd 18563 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18564
18565 /* Get next abbreviation.
18566 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18567 always properly terminated with an abbrev number of 0.
18568 Exit loop if we encounter an abbreviation which we have
18569 already read (which means we are about to read the abbreviations
18570 for the next compile unit) or if the end of the abbreviation
18571 table is reached. */
433df2d4 18572 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18573 break;
18574 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18575 abbrev_ptr += bytes_read;
685af9cd 18576 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18577 break;
18578 }
f3dd6933 18579
433df2d4 18580 return abbrev_table;
c906108c
SS
18581}
18582
72bf9492
DJ
18583/* Returns nonzero if TAG represents a type that we might generate a partial
18584 symbol for. */
18585
18586static int
18587is_type_tag_for_partial (int tag)
18588{
18589 switch (tag)
18590 {
18591#if 0
18592 /* Some types that would be reasonable to generate partial symbols for,
18593 that we don't at present. */
18594 case DW_TAG_array_type:
18595 case DW_TAG_file_type:
18596 case DW_TAG_ptr_to_member_type:
18597 case DW_TAG_set_type:
18598 case DW_TAG_string_type:
18599 case DW_TAG_subroutine_type:
18600#endif
18601 case DW_TAG_base_type:
18602 case DW_TAG_class_type:
680b30c7 18603 case DW_TAG_interface_type:
72bf9492
DJ
18604 case DW_TAG_enumeration_type:
18605 case DW_TAG_structure_type:
18606 case DW_TAG_subrange_type:
18607 case DW_TAG_typedef:
18608 case DW_TAG_union_type:
18609 return 1;
18610 default:
18611 return 0;
18612 }
18613}
18614
18615/* Load all DIEs that are interesting for partial symbols into memory. */
18616
18617static struct partial_die_info *
dee91e82 18618load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18619 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18620{
dee91e82 18621 struct dwarf2_cu *cu = reader->cu;
518817b3 18622 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18623 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18624 unsigned int bytes_read;
5afb4e99 18625 unsigned int load_all = 0;
72bf9492
DJ
18626 int nesting_level = 1;
18627
18628 parent_die = NULL;
18629 last_die = NULL;
18630
7adf1e79
DE
18631 gdb_assert (cu->per_cu != NULL);
18632 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18633 load_all = 1;
18634
72bf9492
DJ
18635 cu->partial_dies
18636 = htab_create_alloc_ex (cu->header.length / 12,
18637 partial_die_hash,
18638 partial_die_eq,
18639 NULL,
18640 &cu->comp_unit_obstack,
18641 hashtab_obstack_allocate,
18642 dummy_obstack_deallocate);
18643
72bf9492
DJ
18644 while (1)
18645 {
685af9cd 18646 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18647
18648 /* A NULL abbrev means the end of a series of children. */
18649 if (abbrev == NULL)
18650 {
18651 if (--nesting_level == 0)
cd9983dd
YQ
18652 return first_die;
18653
72bf9492
DJ
18654 info_ptr += bytes_read;
18655 last_die = parent_die;
18656 parent_die = parent_die->die_parent;
18657 continue;
18658 }
18659
98bfdba5
PA
18660 /* Check for template arguments. We never save these; if
18661 they're seen, we just mark the parent, and go on our way. */
18662 if (parent_die != NULL
18663 && cu->language == language_cplus
18664 && (abbrev->tag == DW_TAG_template_type_param
18665 || abbrev->tag == DW_TAG_template_value_param))
18666 {
18667 parent_die->has_template_arguments = 1;
18668
18669 if (!load_all)
18670 {
18671 /* We don't need a partial DIE for the template argument. */
dee91e82 18672 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18673 continue;
18674 }
18675 }
18676
0d99eb77 18677 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18678 Skip their other children. */
18679 if (!load_all
18680 && cu->language == language_cplus
18681 && parent_die != NULL
18682 && parent_die->tag == DW_TAG_subprogram)
18683 {
dee91e82 18684 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18685 continue;
18686 }
18687
5afb4e99
DJ
18688 /* Check whether this DIE is interesting enough to save. Normally
18689 we would not be interested in members here, but there may be
18690 later variables referencing them via DW_AT_specification (for
18691 static members). */
18692 if (!load_all
18693 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18694 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18695 && abbrev->tag != DW_TAG_enumerator
18696 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18697 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18698 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18699 && abbrev->tag != DW_TAG_variable
5afb4e99 18700 && abbrev->tag != DW_TAG_namespace
f55ee35c 18701 && abbrev->tag != DW_TAG_module
95554aad 18702 && abbrev->tag != DW_TAG_member
74921315
KS
18703 && abbrev->tag != DW_TAG_imported_unit
18704 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18705 {
18706 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18707 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18708 continue;
18709 }
18710
6f06d47b
YQ
18711 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18712 abbrev);
cd9983dd 18713
48fbe735 18714 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18715
18716 /* This two-pass algorithm for processing partial symbols has a
18717 high cost in cache pressure. Thus, handle some simple cases
18718 here which cover the majority of C partial symbols. DIEs
18719 which neither have specification tags in them, nor could have
18720 specification tags elsewhere pointing at them, can simply be
18721 processed and discarded.
18722
18723 This segment is also optional; scan_partial_symbols and
18724 add_partial_symbol will handle these DIEs if we chain
18725 them in normally. When compilers which do not emit large
18726 quantities of duplicate debug information are more common,
18727 this code can probably be removed. */
18728
18729 /* Any complete simple types at the top level (pretty much all
18730 of them, for a language without namespaces), can be processed
18731 directly. */
18732 if (parent_die == NULL
cd9983dd
YQ
18733 && pdi.has_specification == 0
18734 && pdi.is_declaration == 0
18735 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18736 || pdi.tag == DW_TAG_base_type
18737 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18738 {
cd9983dd 18739 if (building_psymtab && pdi.name != NULL)
31edb802 18740 add_psymbol_to_list (pdi.name, false,
79748972 18741 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18742 psymbol_placement::STATIC,
1762568f 18743 0, cu->language, objfile);
cd9983dd 18744 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18745 continue;
18746 }
18747
d8228535
JK
18748 /* The exception for DW_TAG_typedef with has_children above is
18749 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18750 type_name_or_error will error on such types later.
d8228535
JK
18751
18752 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18753 it could not find the child DIEs referenced later, this is checked
18754 above. In correct DWARF DW_TAG_typedef should have no children. */
18755
cd9983dd 18756 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18757 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18758 "- DIE at %s [in module %s]"),
cd9983dd 18759 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18760
72bf9492
DJ
18761 /* If we're at the second level, and we're an enumerator, and
18762 our parent has no specification (meaning possibly lives in a
18763 namespace elsewhere), then we can add the partial symbol now
18764 instead of queueing it. */
cd9983dd 18765 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18766 && parent_die != NULL
18767 && parent_die->die_parent == NULL
18768 && parent_die->tag == DW_TAG_enumeration_type
18769 && parent_die->has_specification == 0)
18770 {
cd9983dd 18771 if (pdi.name == NULL)
b98664d3 18772 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18773 else if (building_psymtab)
31edb802 18774 add_psymbol_to_list (pdi.name, false,
79748972 18775 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18776 cu->language == language_cplus
75aedd27
TT
18777 ? psymbol_placement::GLOBAL
18778 : psymbol_placement::STATIC,
1762568f 18779 0, cu->language, objfile);
72bf9492 18780
cd9983dd 18781 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18782 continue;
18783 }
18784
cd9983dd 18785 struct partial_die_info *part_die
6f06d47b 18786 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18787
72bf9492
DJ
18788 /* We'll save this DIE so link it in. */
18789 part_die->die_parent = parent_die;
18790 part_die->die_sibling = NULL;
18791 part_die->die_child = NULL;
18792
18793 if (last_die && last_die == parent_die)
18794 last_die->die_child = part_die;
18795 else if (last_die)
18796 last_die->die_sibling = part_die;
18797
18798 last_die = part_die;
18799
18800 if (first_die == NULL)
18801 first_die = part_die;
18802
18803 /* Maybe add the DIE to the hash table. Not all DIEs that we
18804 find interesting need to be in the hash table, because we
18805 also have the parent/sibling/child chains; only those that we
18806 might refer to by offset later during partial symbol reading.
18807
18808 For now this means things that might have be the target of a
18809 DW_AT_specification, DW_AT_abstract_origin, or
18810 DW_AT_extension. DW_AT_extension will refer only to
18811 namespaces; DW_AT_abstract_origin refers to functions (and
18812 many things under the function DIE, but we do not recurse
18813 into function DIEs during partial symbol reading) and
18814 possibly variables as well; DW_AT_specification refers to
18815 declarations. Declarations ought to have the DW_AT_declaration
18816 flag. It happens that GCC forgets to put it in sometimes, but
18817 only for functions, not for types.
18818
18819 Adding more things than necessary to the hash table is harmless
18820 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18821 wasted time in find_partial_die, when we reread the compilation
18822 unit with load_all_dies set. */
72bf9492 18823
5afb4e99 18824 if (load_all
72929c62 18825 || abbrev->tag == DW_TAG_constant
5afb4e99 18826 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18827 || abbrev->tag == DW_TAG_variable
18828 || abbrev->tag == DW_TAG_namespace
18829 || part_die->is_declaration)
18830 {
18831 void **slot;
18832
18833 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18834 to_underlying (part_die->sect_off),
18835 INSERT);
72bf9492
DJ
18836 *slot = part_die;
18837 }
18838
72bf9492 18839 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18840 we have no reason to follow the children of structures; for other
98bfdba5
PA
18841 languages we have to, so that we can get at method physnames
18842 to infer fully qualified class names, for DW_AT_specification,
18843 and for C++ template arguments. For C++, we also look one level
18844 inside functions to find template arguments (if the name of the
18845 function does not already contain the template arguments).
bc30ff58 18846
0a4b0913
AB
18847 For Ada and Fortran, we need to scan the children of subprograms
18848 and lexical blocks as well because these languages allow the
18849 definition of nested entities that could be interesting for the
18850 debugger, such as nested subprograms for instance. */
72bf9492 18851 if (last_die->has_children
5afb4e99
DJ
18852 && (load_all
18853 || last_die->tag == DW_TAG_namespace
f55ee35c 18854 || last_die->tag == DW_TAG_module
72bf9492 18855 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18856 || (cu->language == language_cplus
18857 && last_die->tag == DW_TAG_subprogram
18858 && (last_die->name == NULL
18859 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18860 || (cu->language != language_c
18861 && (last_die->tag == DW_TAG_class_type
680b30c7 18862 || last_die->tag == DW_TAG_interface_type
72bf9492 18863 || last_die->tag == DW_TAG_structure_type
bc30ff58 18864 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18865 || ((cu->language == language_ada
18866 || cu->language == language_fortran)
bc30ff58
JB
18867 && (last_die->tag == DW_TAG_subprogram
18868 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18869 {
18870 nesting_level++;
18871 parent_die = last_die;
18872 continue;
18873 }
18874
18875 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18876 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18877
18878 /* Back to the top, do it again. */
18879 }
18880}
18881
6f06d47b
YQ
18882partial_die_info::partial_die_info (sect_offset sect_off_,
18883 struct abbrev_info *abbrev)
18884 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18885{
18886}
18887
35cc7ed7
YQ
18888/* Read a minimal amount of information into the minimal die structure.
18889 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18890
48fbe735
YQ
18891const gdb_byte *
18892partial_die_info::read (const struct die_reader_specs *reader,
18893 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18894{
dee91e82 18895 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18896 struct dwarf2_per_objfile *dwarf2_per_objfile
18897 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18898 unsigned int i;
c5aa993b 18899 int has_low_pc_attr = 0;
c906108c 18900 int has_high_pc_attr = 0;
91da1414 18901 int high_pc_relative = 0;
c906108c 18902
18a8505e 18903 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18904 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18905 {
18a8505e
AT
18906 bool need_reprocess;
18907 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18908 info_ptr, &need_reprocess);
18909 /* String and address offsets that need to do the reprocessing have
18910 already been read at this point, so there is no need to wait until
18911 the loop terminates to do the reprocessing. */
18912 if (need_reprocess)
18913 read_attribute_reprocess (reader, &attr_vec[i]);
18914 attribute &attr = attr_vec[i];
c906108c 18915 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18916 partial symbol table. */
c906108c
SS
18917 switch (attr.name)
18918 {
18919 case DW_AT_name:
48fbe735 18920 switch (tag)
71c25dea
TT
18921 {
18922 case DW_TAG_compile_unit:
95554aad 18923 case DW_TAG_partial_unit:
348e048f 18924 case DW_TAG_type_unit:
71c25dea
TT
18925 /* Compilation units have a DW_AT_name that is a filename, not
18926 a source language identifier. */
18927 case DW_TAG_enumeration_type:
18928 case DW_TAG_enumerator:
18929 /* These tags always have simple identifiers already; no need
18930 to canonicalize them. */
48fbe735 18931 name = DW_STRING (&attr);
71c25dea
TT
18932 break;
18933 default:
48fbe735
YQ
18934 {
18935 struct objfile *objfile = dwarf2_per_objfile->objfile;
18936
18937 name
18938 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18939 &objfile->per_bfd->storage_obstack);
18940 }
71c25dea
TT
18941 break;
18942 }
c906108c 18943 break;
31ef98ae 18944 case DW_AT_linkage_name:
c906108c 18945 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18946 /* Note that both forms of linkage name might appear. We
18947 assume they will be the same, and we only store the last
18948 one we see. */
48fbe735 18949 linkage_name = DW_STRING (&attr);
c906108c
SS
18950 break;
18951 case DW_AT_low_pc:
18952 has_low_pc_attr = 1;
48fbe735 18953 lowpc = attr_value_as_address (&attr);
c906108c
SS
18954 break;
18955 case DW_AT_high_pc:
18956 has_high_pc_attr = 1;
48fbe735 18957 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18958 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18959 high_pc_relative = 1;
c906108c
SS
18960 break;
18961 case DW_AT_location:
0963b4bd 18962 /* Support the .debug_loc offsets. */
8e19ed76
PS
18963 if (attr_form_is_block (&attr))
18964 {
48fbe735 18965 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18966 }
3690dd37 18967 else if (attr_form_is_section_offset (&attr))
8e19ed76 18968 {
4d3c2250 18969 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18970 }
18971 else
18972 {
4d3c2250
KB
18973 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18974 "partial symbol information");
8e19ed76 18975 }
c906108c 18976 break;
c906108c 18977 case DW_AT_external:
48fbe735 18978 is_external = DW_UNSND (&attr);
c906108c
SS
18979 break;
18980 case DW_AT_declaration:
48fbe735 18981 is_declaration = DW_UNSND (&attr);
c906108c
SS
18982 break;
18983 case DW_AT_type:
48fbe735 18984 has_type = 1;
c906108c
SS
18985 break;
18986 case DW_AT_abstract_origin:
18987 case DW_AT_specification:
72bf9492 18988 case DW_AT_extension:
48fbe735
YQ
18989 has_specification = 1;
18990 spec_offset = dwarf2_get_ref_die_offset (&attr);
18991 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18992 || cu->per_cu->is_dwz);
c906108c
SS
18993 break;
18994 case DW_AT_sibling:
18995 /* Ignore absolute siblings, they might point outside of
18996 the current compile unit. */
18997 if (attr.form == DW_FORM_ref_addr)
b98664d3 18998 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18999 else
b9502d3f 19000 {
48fbe735 19001 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19002 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19003 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19004
19005 if (sibling_ptr < info_ptr)
b98664d3 19006 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19007 else if (sibling_ptr > reader->buffer_end)
19008 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19009 else
48fbe735 19010 sibling = sibling_ptr;
b9502d3f 19011 }
c906108c 19012 break;
fa4028e9 19013 case DW_AT_byte_size:
48fbe735 19014 has_byte_size = 1;
fa4028e9 19015 break;
ff908ebf 19016 case DW_AT_const_value:
48fbe735 19017 has_const_value = 1;
ff908ebf 19018 break;
68511cec
CES
19019 case DW_AT_calling_convention:
19020 /* DWARF doesn't provide a way to identify a program's source-level
19021 entry point. DW_AT_calling_convention attributes are only meant
19022 to describe functions' calling conventions.
19023
19024 However, because it's a necessary piece of information in
0c1b455e
TT
19025 Fortran, and before DWARF 4 DW_CC_program was the only
19026 piece of debugging information whose definition refers to
19027 a 'main program' at all, several compilers marked Fortran
19028 main programs with DW_CC_program --- even when those
19029 functions use the standard calling conventions.
19030
19031 Although DWARF now specifies a way to provide this
19032 information, we support this practice for backward
19033 compatibility. */
68511cec 19034 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19035 && cu->language == language_fortran)
48fbe735 19036 main_subprogram = 1;
68511cec 19037 break;
481860b3
GB
19038 case DW_AT_inline:
19039 if (DW_UNSND (&attr) == DW_INL_inlined
19040 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19041 may_be_inlined = 1;
481860b3 19042 break;
95554aad
TT
19043
19044 case DW_AT_import:
48fbe735 19045 if (tag == DW_TAG_imported_unit)
36586728 19046 {
48fbe735
YQ
19047 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19048 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19049 || cu->per_cu->is_dwz);
19050 }
95554aad
TT
19051 break;
19052
0c1b455e 19053 case DW_AT_main_subprogram:
48fbe735 19054 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19055 break;
19056
05caa1d2
TT
19057 case DW_AT_ranges:
19058 {
19059 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19060 but that requires a full DIE, so instead we just
19061 reimplement it. */
19062 int need_ranges_base = tag != DW_TAG_compile_unit;
19063 unsigned int ranges_offset = (DW_UNSND (&attr)
19064 + (need_ranges_base
19065 ? cu->ranges_base
19066 : 0));
19067
19068 /* Value of the DW_AT_ranges attribute is the offset in the
19069 .debug_ranges section. */
19070 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19071 nullptr))
19072 has_pc_info = 1;
19073 }
19074 break;
19075
c906108c
SS
19076 default:
19077 break;
19078 }
19079 }
19080
10d06d82
TT
19081 /* For Ada, if both the name and the linkage name appear, we prefer
19082 the latter. This lets "catch exception" work better, regardless
19083 of the order in which the name and linkage name were emitted.
19084 Really, though, this is just a workaround for the fact that gdb
19085 doesn't store both the name and the linkage name. */
19086 if (cu->language == language_ada && linkage_name != nullptr)
19087 name = linkage_name;
19088
91da1414 19089 if (high_pc_relative)
48fbe735 19090 highpc += lowpc;
91da1414 19091
9373cf26
JK
19092 if (has_low_pc_attr && has_high_pc_attr)
19093 {
19094 /* When using the GNU linker, .gnu.linkonce. sections are used to
19095 eliminate duplicate copies of functions and vtables and such.
19096 The linker will arbitrarily choose one and discard the others.
19097 The AT_*_pc values for such functions refer to local labels in
19098 these sections. If the section from that file was discarded, the
19099 labels are not in the output, so the relocs get a value of 0.
19100 If this is a discarded function, mark the pc bounds as invalid,
19101 so that GDB will ignore it. */
48fbe735 19102 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19103 {
48fbe735 19104 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19106
b98664d3 19107 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19108 "for DIE at %s [in module %s]"),
48fbe735
YQ
19109 paddress (gdbarch, lowpc),
19110 sect_offset_str (sect_off),
9d8780f0 19111 objfile_name (objfile));
9373cf26
JK
19112 }
19113 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19114 else if (lowpc >= highpc)
9373cf26 19115 {
48fbe735 19116 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19118
b98664d3 19119 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19120 "for DIE at %s [in module %s]"),
48fbe735
YQ
19121 paddress (gdbarch, lowpc),
19122 paddress (gdbarch, highpc),
19123 sect_offset_str (sect_off),
9c541725 19124 objfile_name (objfile));
9373cf26
JK
19125 }
19126 else
48fbe735 19127 has_pc_info = 1;
9373cf26 19128 }
85cbf3d3 19129
c906108c
SS
19130 return info_ptr;
19131}
19132
72bf9492
DJ
19133/* Find a cached partial DIE at OFFSET in CU. */
19134
d590ff25
YQ
19135struct partial_die_info *
19136dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19137{
19138 struct partial_die_info *lookup_die = NULL;
6f06d47b 19139 struct partial_die_info part_die (sect_off);
72bf9492 19140
9a3c8263 19141 lookup_die = ((struct partial_die_info *)
d590ff25 19142 htab_find_with_hash (partial_dies, &part_die,
9c541725 19143 to_underlying (sect_off)));
72bf9492 19144
72bf9492
DJ
19145 return lookup_die;
19146}
19147
348e048f
DE
19148/* Find a partial DIE at OFFSET, which may or may not be in CU,
19149 except in the case of .debug_types DIEs which do not reference
19150 outside their CU (they do however referencing other types via
55f1336d 19151 DW_FORM_ref_sig8). */
72bf9492 19152
122cf0f2 19153static const struct cu_partial_die_info
9c541725 19154find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19155{
518817b3
SM
19156 struct dwarf2_per_objfile *dwarf2_per_objfile
19157 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19158 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19159 struct dwarf2_per_cu_data *per_cu = NULL;
19160 struct partial_die_info *pd = NULL;
72bf9492 19161
36586728 19162 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19163 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19164 {
d590ff25 19165 pd = cu->find_partial_die (sect_off);
5afb4e99 19166 if (pd != NULL)
fb816e8b 19167 return { cu, pd };
0d99eb77
DE
19168 /* We missed recording what we needed.
19169 Load all dies and try again. */
19170 per_cu = cu->per_cu;
5afb4e99 19171 }
0d99eb77
DE
19172 else
19173 {
19174 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19175 if (cu->per_cu->is_debug_types)
0d99eb77 19176 {
9d8780f0
SM
19177 error (_("Dwarf Error: Type Unit at offset %s contains"
19178 " external reference to offset %s [in module %s].\n"),
19179 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19180 bfd_get_filename (objfile->obfd));
19181 }
9c541725 19182 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19183 dwarf2_per_objfile);
72bf9492 19184
0d99eb77
DE
19185 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19186 load_partial_comp_unit (per_cu);
ae038cb0 19187
0d99eb77 19188 per_cu->cu->last_used = 0;
d590ff25 19189 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19190 }
5afb4e99 19191
dee91e82
DE
19192 /* If we didn't find it, and not all dies have been loaded,
19193 load them all and try again. */
19194
5afb4e99
DJ
19195 if (pd == NULL && per_cu->load_all_dies == 0)
19196 {
5afb4e99 19197 per_cu->load_all_dies = 1;
fd820528
DE
19198
19199 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19200 THIS_CU->cu may already be in use. So we can't just free it and
19201 replace its DIEs with the ones we read in. Instead, we leave those
19202 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19203 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19204 set. */
dee91e82 19205 load_partial_comp_unit (per_cu);
5afb4e99 19206
d590ff25 19207 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19208 }
19209
19210 if (pd == NULL)
19211 internal_error (__FILE__, __LINE__,
9d8780f0 19212 _("could not find partial DIE %s "
3e43a32a 19213 "in cache [from module %s]\n"),
9d8780f0 19214 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19215 return { per_cu->cu, pd };
72bf9492
DJ
19216}
19217
abc72ce4
DE
19218/* See if we can figure out if the class lives in a namespace. We do
19219 this by looking for a member function; its demangled name will
19220 contain namespace info, if there is any. */
19221
19222static void
19223guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19224 struct dwarf2_cu *cu)
19225{
19226 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19227 what template types look like, because the demangler
19228 frequently doesn't give the same name as the debug info. We
19229 could fix this by only using the demangled name to get the
19230 prefix (but see comment in read_structure_type). */
19231
19232 struct partial_die_info *real_pdi;
19233 struct partial_die_info *child_pdi;
19234
19235 /* If this DIE (this DIE's specification, if any) has a parent, then
19236 we should not do this. We'll prepend the parent's fully qualified
19237 name when we create the partial symbol. */
19238
19239 real_pdi = struct_pdi;
19240 while (real_pdi->has_specification)
fb816e8b 19241 {
122cf0f2
AB
19242 auto res = find_partial_die (real_pdi->spec_offset,
19243 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19244 real_pdi = res.pdi;
19245 cu = res.cu;
19246 }
abc72ce4
DE
19247
19248 if (real_pdi->die_parent != NULL)
19249 return;
19250
19251 for (child_pdi = struct_pdi->die_child;
19252 child_pdi != NULL;
19253 child_pdi = child_pdi->die_sibling)
19254 {
19255 if (child_pdi->tag == DW_TAG_subprogram
19256 && child_pdi->linkage_name != NULL)
19257 {
43816ebc
TT
19258 gdb::unique_xmalloc_ptr<char> actual_class_name
19259 (language_class_name_from_physname (cu->language_defn,
19260 child_pdi->linkage_name));
abc72ce4
DE
19261 if (actual_class_name != NULL)
19262 {
518817b3 19263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19264 struct_pdi->name
021887d8 19265 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19266 actual_class_name.get ());
abc72ce4
DE
19267 }
19268 break;
19269 }
19270 }
19271}
19272
52356b79
YQ
19273void
19274partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19275{
abc72ce4
DE
19276 /* Once we've fixed up a die, there's no point in doing so again.
19277 This also avoids a memory leak if we were to call
19278 guess_partial_die_structure_name multiple times. */
52356b79 19279 if (fixup_called)
abc72ce4
DE
19280 return;
19281
72bf9492
DJ
19282 /* If we found a reference attribute and the DIE has no name, try
19283 to find a name in the referred to DIE. */
19284
52356b79 19285 if (name == NULL && has_specification)
72bf9492
DJ
19286 {
19287 struct partial_die_info *spec_die;
72bf9492 19288
122cf0f2 19289 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19290 spec_die = res.pdi;
19291 cu = res.cu;
72bf9492 19292
52356b79 19293 spec_die->fixup (cu);
72bf9492
DJ
19294
19295 if (spec_die->name)
19296 {
52356b79 19297 name = spec_die->name;
72bf9492
DJ
19298
19299 /* Copy DW_AT_external attribute if it is set. */
19300 if (spec_die->is_external)
52356b79 19301 is_external = spec_die->is_external;
72bf9492
DJ
19302 }
19303 }
19304
19305 /* Set default names for some unnamed DIEs. */
72bf9492 19306
52356b79
YQ
19307 if (name == NULL && tag == DW_TAG_namespace)
19308 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19309
abc72ce4
DE
19310 /* If there is no parent die to provide a namespace, and there are
19311 children, see if we can determine the namespace from their linkage
122d1940 19312 name. */
abc72ce4 19313 if (cu->language == language_cplus
fd5866f6 19314 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19315 && die_parent == NULL
19316 && has_children
19317 && (tag == DW_TAG_class_type
19318 || tag == DW_TAG_structure_type
19319 || tag == DW_TAG_union_type))
19320 guess_partial_die_structure_name (this, cu);
abc72ce4 19321
53832f31
TT
19322 /* GCC might emit a nameless struct or union that has a linkage
19323 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19324 if (name == NULL
19325 && (tag == DW_TAG_class_type
19326 || tag == DW_TAG_interface_type
19327 || tag == DW_TAG_structure_type
19328 || tag == DW_TAG_union_type)
19329 && linkage_name != NULL)
53832f31 19330 {
43816ebc
TT
19331 gdb::unique_xmalloc_ptr<char> demangled
19332 (gdb_demangle (linkage_name, DMGL_TYPES));
19333 if (demangled != nullptr)
53832f31 19334 {
96408a79
SA
19335 const char *base;
19336
19337 /* Strip any leading namespaces/classes, keep only the base name.
19338 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19339 base = strrchr (demangled.get (), ':');
19340 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19341 base++;
19342 else
43816ebc 19343 base = demangled.get ();
96408a79 19344
518817b3 19345 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19346 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19347 }
19348 }
19349
52356b79 19350 fixup_called = 1;
72bf9492
DJ
19351}
19352
18a8505e
AT
19353/* Process the attributes that had to be skipped in the first round. These
19354 attributes are the ones that need str_offsets_base or addr_base attributes.
19355 They could not have been processed in the first round, because at the time
19356 the values of str_offsets_base or addr_base may not have been known. */
19357void read_attribute_reprocess (const struct die_reader_specs *reader,
19358 struct attribute *attr)
19359{
19360 struct dwarf2_cu *cu = reader->cu;
19361 switch (attr->form)
19362 {
19363 case DW_FORM_addrx:
19364 case DW_FORM_GNU_addr_index:
19365 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19366 break;
19367 case DW_FORM_strx:
19368 case DW_FORM_strx1:
19369 case DW_FORM_strx2:
19370 case DW_FORM_strx3:
19371 case DW_FORM_strx4:
19372 case DW_FORM_GNU_str_index:
19373 {
19374 unsigned int str_index = DW_UNSND (attr);
19375 if (reader->dwo_file != NULL)
19376 {
19377 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19378 DW_STRING_IS_CANONICAL (attr) = 0;
19379 }
19380 else
19381 {
19382 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19383 DW_STRING_IS_CANONICAL (attr) = 0;
19384 }
19385 break;
19386 }
19387 default:
19388 gdb_assert_not_reached (_("Unexpected DWARF form."));
19389 }
19390}
19391
a8329558 19392/* Read an attribute value described by an attribute form. */
c906108c 19393
d521ce57 19394static const gdb_byte *
dee91e82
DE
19395read_attribute_value (const struct die_reader_specs *reader,
19396 struct attribute *attr, unsigned form,
18a8505e
AT
19397 LONGEST implicit_const, const gdb_byte *info_ptr,
19398 bool *need_reprocess)
c906108c 19399{
dee91e82 19400 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19401 struct dwarf2_per_objfile *dwarf2_per_objfile
19402 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19403 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19404 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19405 bfd *abfd = reader->abfd;
e7c27a73 19406 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19407 unsigned int bytes_read;
19408 struct dwarf_block *blk;
18a8505e 19409 *need_reprocess = false;
c906108c 19410
aead7601 19411 attr->form = (enum dwarf_form) form;
a8329558 19412 switch (form)
c906108c 19413 {
c906108c 19414 case DW_FORM_ref_addr:
ae411497 19415 if (cu->header.version == 2)
4568ecf9 19416 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19417 else
4568ecf9
DE
19418 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19419 &cu->header, &bytes_read);
ae411497
TT
19420 info_ptr += bytes_read;
19421 break;
36586728
TT
19422 case DW_FORM_GNU_ref_alt:
19423 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19424 info_ptr += bytes_read;
19425 break;
ae411497 19426 case DW_FORM_addr:
e7c27a73 19427 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19428 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19429 info_ptr += bytes_read;
c906108c
SS
19430 break;
19431 case DW_FORM_block2:
7b5a2f43 19432 blk = dwarf_alloc_block (cu);
c906108c
SS
19433 blk->size = read_2_bytes (abfd, info_ptr);
19434 info_ptr += 2;
19435 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19436 info_ptr += blk->size;
19437 DW_BLOCK (attr) = blk;
19438 break;
19439 case DW_FORM_block4:
7b5a2f43 19440 blk = dwarf_alloc_block (cu);
c906108c
SS
19441 blk->size = read_4_bytes (abfd, info_ptr);
19442 info_ptr += 4;
19443 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19444 info_ptr += blk->size;
19445 DW_BLOCK (attr) = blk;
19446 break;
19447 case DW_FORM_data2:
19448 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19449 info_ptr += 2;
19450 break;
19451 case DW_FORM_data4:
19452 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19453 info_ptr += 4;
19454 break;
19455 case DW_FORM_data8:
19456 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19457 info_ptr += 8;
19458 break;
0224619f
JK
19459 case DW_FORM_data16:
19460 blk = dwarf_alloc_block (cu);
19461 blk->size = 16;
19462 blk->data = read_n_bytes (abfd, info_ptr, 16);
19463 info_ptr += 16;
19464 DW_BLOCK (attr) = blk;
19465 break;
2dc7f7b3
TT
19466 case DW_FORM_sec_offset:
19467 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19468 info_ptr += bytes_read;
19469 break;
c906108c 19470 case DW_FORM_string:
9b1c24c8 19471 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19472 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19473 info_ptr += bytes_read;
19474 break;
4bdf3d34 19475 case DW_FORM_strp:
36586728
TT
19476 if (!cu->per_cu->is_dwz)
19477 {
ed2dc618
SM
19478 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19479 abfd, info_ptr, cu_header,
36586728
TT
19480 &bytes_read);
19481 DW_STRING_IS_CANONICAL (attr) = 0;
19482 info_ptr += bytes_read;
19483 break;
19484 }
19485 /* FALLTHROUGH */
43988095
JK
19486 case DW_FORM_line_strp:
19487 if (!cu->per_cu->is_dwz)
19488 {
ed2dc618
SM
19489 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19490 abfd, info_ptr,
43988095
JK
19491 cu_header, &bytes_read);
19492 DW_STRING_IS_CANONICAL (attr) = 0;
19493 info_ptr += bytes_read;
19494 break;
19495 }
19496 /* FALLTHROUGH */
36586728
TT
19497 case DW_FORM_GNU_strp_alt:
19498 {
ed2dc618 19499 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19500 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19501 &bytes_read);
19502
ed2dc618
SM
19503 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19504 dwz, str_offset);
36586728
TT
19505 DW_STRING_IS_CANONICAL (attr) = 0;
19506 info_ptr += bytes_read;
19507 }
4bdf3d34 19508 break;
2dc7f7b3 19509 case DW_FORM_exprloc:
c906108c 19510 case DW_FORM_block:
7b5a2f43 19511 blk = dwarf_alloc_block (cu);
c906108c
SS
19512 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19513 info_ptr += bytes_read;
19514 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19515 info_ptr += blk->size;
19516 DW_BLOCK (attr) = blk;
19517 break;
19518 case DW_FORM_block1:
7b5a2f43 19519 blk = dwarf_alloc_block (cu);
c906108c
SS
19520 blk->size = read_1_byte (abfd, info_ptr);
19521 info_ptr += 1;
19522 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19523 info_ptr += blk->size;
19524 DW_BLOCK (attr) = blk;
19525 break;
19526 case DW_FORM_data1:
19527 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19528 info_ptr += 1;
19529 break;
19530 case DW_FORM_flag:
19531 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19532 info_ptr += 1;
19533 break;
2dc7f7b3
TT
19534 case DW_FORM_flag_present:
19535 DW_UNSND (attr) = 1;
19536 break;
c906108c
SS
19537 case DW_FORM_sdata:
19538 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19539 info_ptr += bytes_read;
19540 break;
19541 case DW_FORM_udata:
18a8505e 19542 case DW_FORM_rnglistx:
c906108c
SS
19543 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19544 info_ptr += bytes_read;
19545 break;
19546 case DW_FORM_ref1:
9c541725 19547 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19548 + read_1_byte (abfd, info_ptr));
c906108c
SS
19549 info_ptr += 1;
19550 break;
19551 case DW_FORM_ref2:
9c541725 19552 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19553 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19554 info_ptr += 2;
19555 break;
19556 case DW_FORM_ref4:
9c541725 19557 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19558 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19559 info_ptr += 4;
19560 break;
613e1657 19561 case DW_FORM_ref8:
9c541725 19562 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19563 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19564 info_ptr += 8;
19565 break;
55f1336d 19566 case DW_FORM_ref_sig8:
ac9ec31b 19567 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19568 info_ptr += 8;
19569 break;
c906108c 19570 case DW_FORM_ref_udata:
9c541725 19571 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19572 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19573 info_ptr += bytes_read;
19574 break;
c906108c 19575 case DW_FORM_indirect:
a8329558
KW
19576 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19577 info_ptr += bytes_read;
43988095
JK
19578 if (form == DW_FORM_implicit_const)
19579 {
19580 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19581 info_ptr += bytes_read;
19582 }
19583 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19584 info_ptr, need_reprocess);
43988095
JK
19585 break;
19586 case DW_FORM_implicit_const:
19587 DW_SND (attr) = implicit_const;
a8329558 19588 break;
336d760d 19589 case DW_FORM_addrx:
3019eac3 19590 case DW_FORM_GNU_addr_index:
18a8505e
AT
19591 *need_reprocess = true;
19592 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19593 info_ptr += bytes_read;
19594 break;
cf532bd1 19595 case DW_FORM_strx:
15f18d14
AT
19596 case DW_FORM_strx1:
19597 case DW_FORM_strx2:
19598 case DW_FORM_strx3:
19599 case DW_FORM_strx4:
3019eac3 19600 case DW_FORM_GNU_str_index:
3019eac3 19601 {
15f18d14
AT
19602 ULONGEST str_index;
19603 if (form == DW_FORM_strx1)
19604 {
19605 str_index = read_1_byte (abfd, info_ptr);
19606 info_ptr += 1;
19607 }
19608 else if (form == DW_FORM_strx2)
19609 {
19610 str_index = read_2_bytes (abfd, info_ptr);
19611 info_ptr += 2;
19612 }
19613 else if (form == DW_FORM_strx3)
19614 {
19615 str_index = read_3_bytes (abfd, info_ptr);
19616 info_ptr += 3;
19617 }
19618 else if (form == DW_FORM_strx4)
19619 {
19620 str_index = read_4_bytes (abfd, info_ptr);
19621 info_ptr += 4;
19622 }
19623 else
19624 {
19625 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19626 info_ptr += bytes_read;
19627 }
18a8505e
AT
19628 *need_reprocess = true;
19629 DW_UNSND (attr) = str_index;
19630 }
3019eac3 19631 break;
c906108c 19632 default:
8a3fe4f8 19633 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19634 dwarf_form_name (form),
19635 bfd_get_filename (abfd));
c906108c 19636 }
28e94949 19637
36586728 19638 /* Super hack. */
7771576e 19639 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19640 attr->form = DW_FORM_GNU_ref_alt;
19641
28e94949
JB
19642 /* We have seen instances where the compiler tried to emit a byte
19643 size attribute of -1 which ended up being encoded as an unsigned
19644 0xffffffff. Although 0xffffffff is technically a valid size value,
19645 an object of this size seems pretty unlikely so we can relatively
19646 safely treat these cases as if the size attribute was invalid and
19647 treat them as zero by default. */
19648 if (attr->name == DW_AT_byte_size
19649 && form == DW_FORM_data4
19650 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19651 {
19652 complaint
b98664d3 19653 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19654 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19655 DW_UNSND (attr) = 0;
19656 }
28e94949 19657
c906108c
SS
19658 return info_ptr;
19659}
19660
a8329558
KW
19661/* Read an attribute described by an abbreviated attribute. */
19662
d521ce57 19663static const gdb_byte *
dee91e82
DE
19664read_attribute (const struct die_reader_specs *reader,
19665 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19666 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19667{
19668 attr->name = abbrev->name;
43988095 19669 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19670 abbrev->implicit_const, info_ptr,
19671 need_reprocess);
a8329558
KW
19672}
19673
c906108c 19674static CORE_ADDR
d521ce57 19675read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19676 unsigned int *bytes_read)
c906108c 19677{
e7c27a73 19678 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19679 CORE_ADDR retval = 0;
19680
107d2387 19681 if (cu_header->signed_addr_p)
c906108c 19682 {
107d2387
AC
19683 switch (cu_header->addr_size)
19684 {
19685 case 2:
fe1b8b76 19686 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19687 break;
19688 case 4:
fe1b8b76 19689 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19690 break;
19691 case 8:
fe1b8b76 19692 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19693 break;
19694 default:
8e65ff28 19695 internal_error (__FILE__, __LINE__,
e2e0b3e5 19696 _("read_address: bad switch, signed [in module %s]"),
659b0389 19697 bfd_get_filename (abfd));
107d2387
AC
19698 }
19699 }
19700 else
19701 {
19702 switch (cu_header->addr_size)
19703 {
19704 case 2:
fe1b8b76 19705 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19706 break;
19707 case 4:
fe1b8b76 19708 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19709 break;
19710 case 8:
fe1b8b76 19711 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19712 break;
19713 default:
8e65ff28 19714 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19715 _("read_address: bad switch, "
19716 "unsigned [in module %s]"),
659b0389 19717 bfd_get_filename (abfd));
107d2387 19718 }
c906108c 19719 }
64367e0a 19720
107d2387
AC
19721 *bytes_read = cu_header->addr_size;
19722 return retval;
c906108c
SS
19723}
19724
f7ef9339 19725/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19726 specification allows the initial length to take up either 4 bytes
19727 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19728 bytes describe the length and all offsets will be 8 bytes in length
19729 instead of 4.
19730
f7ef9339
KB
19731 An older, non-standard 64-bit format is also handled by this
19732 function. The older format in question stores the initial length
19733 as an 8-byte quantity without an escape value. Lengths greater
19734 than 2^32 aren't very common which means that the initial 4 bytes
19735 is almost always zero. Since a length value of zero doesn't make
19736 sense for the 32-bit format, this initial zero can be considered to
19737 be an escape value which indicates the presence of the older 64-bit
19738 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19739 greater than 4GB. If it becomes necessary to handle lengths
19740 somewhat larger than 4GB, we could allow other small values (such
19741 as the non-sensical values of 1, 2, and 3) to also be used as
19742 escape values indicating the presence of the old format.
f7ef9339 19743
917c78fc
MK
19744 The value returned via bytes_read should be used to increment the
19745 relevant pointer after calling read_initial_length().
c764a876 19746
613e1657
KB
19747 [ Note: read_initial_length() and read_offset() are based on the
19748 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19749 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19750 from:
19751
f7ef9339 19752 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19753
613e1657
KB
19754 This document is only a draft and is subject to change. (So beware.)
19755
f7ef9339 19756 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19757 determined empirically by examining 64-bit ELF files produced by
19758 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19759
19760 - Kevin, July 16, 2002
613e1657
KB
19761 ] */
19762
19763static LONGEST
d521ce57 19764read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19765{
fe1b8b76 19766 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19767
dd373385 19768 if (length == 0xffffffff)
613e1657 19769 {
fe1b8b76 19770 length = bfd_get_64 (abfd, buf + 4);
613e1657 19771 *bytes_read = 12;
613e1657 19772 }
dd373385 19773 else if (length == 0)
f7ef9339 19774 {
dd373385 19775 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19776 length = bfd_get_64 (abfd, buf);
f7ef9339 19777 *bytes_read = 8;
f7ef9339 19778 }
613e1657
KB
19779 else
19780 {
19781 *bytes_read = 4;
613e1657
KB
19782 }
19783
c764a876
DE
19784 return length;
19785}
dd373385 19786
c764a876
DE
19787/* Cover function for read_initial_length.
19788 Returns the length of the object at BUF, and stores the size of the
19789 initial length in *BYTES_READ and stores the size that offsets will be in
19790 *OFFSET_SIZE.
19791 If the initial length size is not equivalent to that specified in
19792 CU_HEADER then issue a complaint.
19793 This is useful when reading non-comp-unit headers. */
dd373385 19794
c764a876 19795static LONGEST
d521ce57 19796read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19797 const struct comp_unit_head *cu_header,
19798 unsigned int *bytes_read,
19799 unsigned int *offset_size)
19800{
19801 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19802
19803 gdb_assert (cu_header->initial_length_size == 4
19804 || cu_header->initial_length_size == 8
19805 || cu_header->initial_length_size == 12);
19806
19807 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19808 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19809
c764a876 19810 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19811 return length;
613e1657
KB
19812}
19813
19814/* Read an offset from the data stream. The size of the offset is
917c78fc 19815 given by cu_header->offset_size. */
613e1657
KB
19816
19817static LONGEST
d521ce57
TT
19818read_offset (bfd *abfd, const gdb_byte *buf,
19819 const struct comp_unit_head *cu_header,
891d2f0b 19820 unsigned int *bytes_read)
c764a876
DE
19821{
19822 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19823
c764a876
DE
19824 *bytes_read = cu_header->offset_size;
19825 return offset;
19826}
19827
19828/* Read an offset from the data stream. */
19829
19830static LONGEST
d521ce57 19831read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19832{
19833 LONGEST retval = 0;
19834
c764a876 19835 switch (offset_size)
613e1657
KB
19836 {
19837 case 4:
fe1b8b76 19838 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19839 break;
19840 case 8:
fe1b8b76 19841 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19842 break;
19843 default:
8e65ff28 19844 internal_error (__FILE__, __LINE__,
c764a876 19845 _("read_offset_1: bad switch [in module %s]"),
659b0389 19846 bfd_get_filename (abfd));
613e1657
KB
19847 }
19848
917c78fc 19849 return retval;
613e1657
KB
19850}
19851
d521ce57
TT
19852static const gdb_byte *
19853read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19854{
19855 /* If the size of a host char is 8 bits, we can return a pointer
19856 to the buffer, otherwise we have to copy the data to a buffer
19857 allocated on the temporary obstack. */
4bdf3d34 19858 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19859 return buf;
c906108c
SS
19860}
19861
d521ce57
TT
19862static const char *
19863read_direct_string (bfd *abfd, const gdb_byte *buf,
19864 unsigned int *bytes_read_ptr)
c906108c
SS
19865{
19866 /* If the size of a host char is 8 bits, we can return a pointer
19867 to the string, otherwise we have to copy the string to a buffer
19868 allocated on the temporary obstack. */
4bdf3d34 19869 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19870 if (*buf == '\0')
19871 {
19872 *bytes_read_ptr = 1;
19873 return NULL;
19874 }
d521ce57
TT
19875 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19876 return (const char *) buf;
4bdf3d34
JJ
19877}
19878
43988095
JK
19879/* Return pointer to string at section SECT offset STR_OFFSET with error
19880 reporting strings FORM_NAME and SECT_NAME. */
19881
d521ce57 19882static const char *
ed2dc618
SM
19883read_indirect_string_at_offset_from (struct objfile *objfile,
19884 bfd *abfd, LONGEST str_offset,
43988095
JK
19885 struct dwarf2_section_info *sect,
19886 const char *form_name,
19887 const char *sect_name)
19888{
ed2dc618 19889 dwarf2_read_section (objfile, sect);
43988095
JK
19890 if (sect->buffer == NULL)
19891 error (_("%s used without %s section [in module %s]"),
19892 form_name, sect_name, bfd_get_filename (abfd));
19893 if (str_offset >= sect->size)
19894 error (_("%s pointing outside of %s section [in module %s]"),
19895 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19896 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19897 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19898 return NULL;
43988095
JK
19899 return (const char *) (sect->buffer + str_offset);
19900}
19901
19902/* Return pointer to string at .debug_str offset STR_OFFSET. */
19903
19904static const char *
ed2dc618
SM
19905read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19906 bfd *abfd, LONGEST str_offset)
43988095 19907{
ed2dc618
SM
19908 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19909 abfd, str_offset,
43988095
JK
19910 &dwarf2_per_objfile->str,
19911 "DW_FORM_strp", ".debug_str");
19912}
19913
19914/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19915
19916static const char *
ed2dc618
SM
19917read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19918 bfd *abfd, LONGEST str_offset)
43988095 19919{
ed2dc618
SM
19920 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19921 abfd, str_offset,
43988095
JK
19922 &dwarf2_per_objfile->line_str,
19923 "DW_FORM_line_strp",
19924 ".debug_line_str");
c906108c
SS
19925}
19926
36586728
TT
19927/* Read a string at offset STR_OFFSET in the .debug_str section from
19928 the .dwz file DWZ. Throw an error if the offset is too large. If
19929 the string consists of a single NUL byte, return NULL; otherwise
19930 return a pointer to the string. */
19931
d521ce57 19932static const char *
ed2dc618
SM
19933read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19934 LONGEST str_offset)
36586728 19935{
ed2dc618 19936 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19937
19938 if (dwz->str.buffer == NULL)
19939 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19940 "section [in module %s]"),
00f93c44 19941 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19942 if (str_offset >= dwz->str.size)
19943 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19944 ".debug_str section [in module %s]"),
00f93c44 19945 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19946 gdb_assert (HOST_CHAR_BIT == 8);
19947 if (dwz->str.buffer[str_offset] == '\0')
19948 return NULL;
d521ce57 19949 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19950}
19951
43988095
JK
19952/* Return pointer to string at .debug_str offset as read from BUF.
19953 BUF is assumed to be in a compilation unit described by CU_HEADER.
19954 Return *BYTES_READ_PTR count of bytes read from BUF. */
19955
d521ce57 19956static const char *
ed2dc618
SM
19957read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19958 const gdb_byte *buf,
cf2c3c16
TT
19959 const struct comp_unit_head *cu_header,
19960 unsigned int *bytes_read_ptr)
19961{
19962 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19963
ed2dc618 19964 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19965}
19966
43988095
JK
19967/* Return pointer to string at .debug_line_str offset as read from BUF.
19968 BUF is assumed to be in a compilation unit described by CU_HEADER.
19969 Return *BYTES_READ_PTR count of bytes read from BUF. */
19970
19971static const char *
ed2dc618
SM
19972read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19973 bfd *abfd, const gdb_byte *buf,
43988095
JK
19974 const struct comp_unit_head *cu_header,
19975 unsigned int *bytes_read_ptr)
19976{
19977 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19978
ed2dc618
SM
19979 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19980 str_offset);
43988095
JK
19981}
19982
3019eac3 19983/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19984 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19985 ADDR_SIZE is the size of addresses from the CU header. */
19986
19987static CORE_ADDR
ed2dc618 19988read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19989 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19990 int addr_size)
3019eac3
DE
19991{
19992 struct objfile *objfile = dwarf2_per_objfile->objfile;
19993 bfd *abfd = objfile->obfd;
19994 const gdb_byte *info_ptr;
18a8505e 19995 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3
DE
19996
19997 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19998 if (dwarf2_per_objfile->addr.buffer == NULL)
19999 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20000 objfile_name (objfile));
18a8505e
AT
20001 if (addr_base_or_zero + addr_index * addr_size
20002 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
20003 error (_("DW_FORM_addr_index pointing outside of "
20004 ".debug_addr section [in module %s]"),
4262abfb 20005 objfile_name (objfile));
3019eac3 20006 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 20007 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
20008 if (addr_size == 4)
20009 return bfd_get_32 (abfd, info_ptr);
20010 else
20011 return bfd_get_64 (abfd, info_ptr);
20012}
20013
20014/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20015
20016static CORE_ADDR
20017read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20018{
518817b3
SM
20019 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20020 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20021}
20022
20023/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20024
20025static CORE_ADDR
d521ce57 20026read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20027 unsigned int *bytes_read)
20028{
518817b3 20029 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20030 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20031
20032 return read_addr_index (cu, addr_index);
20033}
20034
3019eac3
DE
20035/* Given an index in .debug_addr, fetch the value.
20036 NOTE: This can be called during dwarf expression evaluation,
20037 long after the debug information has been read, and thus per_cu->cu
20038 may no longer exist. */
20039
20040CORE_ADDR
20041dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20042 unsigned int addr_index)
20043{
ed2dc618 20044 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 20045 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 20046 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20047 int addr_size;
20048
3019eac3
DE
20049 /* We need addr_base and addr_size.
20050 If we don't have PER_CU->cu, we have to get it.
20051 Nasty, but the alternative is storing the needed info in PER_CU,
20052 which at this point doesn't seem justified: it's not clear how frequently
20053 it would get used and it would increase the size of every PER_CU.
20054 Entry points like dwarf2_per_cu_addr_size do a similar thing
20055 so we're not in uncharted territory here.
20056 Alas we need to be a bit more complicated as addr_base is contained
20057 in the DIE.
20058
20059 We don't need to read the entire CU(/TU).
20060 We just need the header and top level die.
a1b64ce1 20061
3019eac3 20062 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20063 For now we skip this optimization. */
3019eac3
DE
20064
20065 if (cu != NULL)
20066 {
20067 addr_base = cu->addr_base;
20068 addr_size = cu->header.addr_size;
20069 }
20070 else
20071 {
c0ab21c2
TT
20072 cutu_reader reader (per_cu, NULL, 0, 0, false);
20073 addr_base = reader.cu->addr_base;
20074 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20075 }
20076
ed2dc618
SM
20077 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20078 addr_size);
3019eac3
DE
20079}
20080
18a8505e
AT
20081/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20082 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20083 DWO file. */
3019eac3 20084
d521ce57 20085static const char *
18a8505e
AT
20086read_str_index (struct dwarf2_cu *cu,
20087 struct dwarf2_section_info *str_section,
20088 struct dwarf2_section_info *str_offsets_section,
20089 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20090{
518817b3
SM
20091 struct dwarf2_per_objfile *dwarf2_per_objfile
20092 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20093 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20094 const char *objf_name = objfile_name (objfile);
3019eac3 20095 bfd *abfd = objfile->obfd;
d521ce57 20096 const gdb_byte *info_ptr;
3019eac3 20097 ULONGEST str_offset;
cf532bd1 20098 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20099
73869dc2
DE
20100 dwarf2_read_section (objfile, str_section);
20101 dwarf2_read_section (objfile, str_offsets_section);
20102 if (str_section->buffer == NULL)
18a8505e 20103 error (_("%s used without %s section"
9d8780f0 20104 " in CU at offset %s [in module %s]"),
18a8505e
AT
20105 form_name, get_section_name (str_section),
20106 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20107 if (str_offsets_section->buffer == NULL)
18a8505e 20108 error (_("%s used without %s section"
9d8780f0 20109 " in CU at offset %s [in module %s]"),
18a8505e
AT
20110 form_name, get_section_name (str_section),
20111 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20112 info_ptr = (str_offsets_section->buffer
18a8505e 20113 + str_offsets_base
3019eac3
DE
20114 + str_index * cu->header.offset_size);
20115 if (cu->header.offset_size == 4)
20116 str_offset = bfd_get_32 (abfd, info_ptr);
20117 else
20118 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20119 if (str_offset >= str_section->size)
57d63ce2 20120 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20121 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20122 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20123 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20124}
20125
18a8505e
AT
20126/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20127
20128static const char *
20129read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20130{
20131 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20132 ? reader->cu->header.addr_size : 0;
20133 return read_str_index (reader->cu,
20134 &reader->dwo_file->sections.str,
20135 &reader->dwo_file->sections.str_offsets,
20136 str_offsets_base, str_index);
20137}
20138
20139/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20140
20141static const char *
20142read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20143{
20144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20145 const char *objf_name = objfile_name (objfile);
20146 static const char form_name[] = "DW_FORM_GNU_str_index";
20147 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20148
20149 if (!cu->str_offsets_base.has_value ())
20150 error (_("%s used in Fission stub without %s"
20151 " in CU at offset 0x%lx [in module %s]"),
20152 form_name, str_offsets_attr_name,
20153 (long) cu->header.offset_size, objf_name);
20154
20155 return read_str_index (cu,
20156 &cu->per_cu->dwarf2_per_objfile->str,
20157 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20158 *cu->str_offsets_base, str_index);
20159}
20160
3019eac3
DE
20161/* Return the length of an LEB128 number in BUF. */
20162
20163static int
20164leb128_size (const gdb_byte *buf)
20165{
20166 const gdb_byte *begin = buf;
20167 gdb_byte byte;
20168
20169 while (1)
20170 {
20171 byte = *buf++;
20172 if ((byte & 128) == 0)
20173 return buf - begin;
20174 }
20175}
20176
c906108c 20177static void
e142c38c 20178set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20179{
20180 switch (lang)
20181 {
20182 case DW_LANG_C89:
76bee0cc 20183 case DW_LANG_C99:
0cfd832f 20184 case DW_LANG_C11:
c906108c 20185 case DW_LANG_C:
d1be3247 20186 case DW_LANG_UPC:
e142c38c 20187 cu->language = language_c;
c906108c 20188 break;
9c37b5ae 20189 case DW_LANG_Java:
c906108c 20190 case DW_LANG_C_plus_plus:
0cfd832f
MW
20191 case DW_LANG_C_plus_plus_11:
20192 case DW_LANG_C_plus_plus_14:
e142c38c 20193 cu->language = language_cplus;
c906108c 20194 break;
6aecb9c2
JB
20195 case DW_LANG_D:
20196 cu->language = language_d;
20197 break;
c906108c
SS
20198 case DW_LANG_Fortran77:
20199 case DW_LANG_Fortran90:
b21b22e0 20200 case DW_LANG_Fortran95:
f7de9aab
MW
20201 case DW_LANG_Fortran03:
20202 case DW_LANG_Fortran08:
e142c38c 20203 cu->language = language_fortran;
c906108c 20204 break;
a766d390
DE
20205 case DW_LANG_Go:
20206 cu->language = language_go;
20207 break;
c906108c 20208 case DW_LANG_Mips_Assembler:
e142c38c 20209 cu->language = language_asm;
c906108c
SS
20210 break;
20211 case DW_LANG_Ada83:
8aaf0b47 20212 case DW_LANG_Ada95:
bc5f45f8
JB
20213 cu->language = language_ada;
20214 break;
72019c9c
GM
20215 case DW_LANG_Modula2:
20216 cu->language = language_m2;
20217 break;
fe8e67fd
PM
20218 case DW_LANG_Pascal83:
20219 cu->language = language_pascal;
20220 break;
22566fbd
DJ
20221 case DW_LANG_ObjC:
20222 cu->language = language_objc;
20223 break;
c44af4eb
TT
20224 case DW_LANG_Rust:
20225 case DW_LANG_Rust_old:
20226 cu->language = language_rust;
20227 break;
c906108c
SS
20228 case DW_LANG_Cobol74:
20229 case DW_LANG_Cobol85:
c906108c 20230 default:
e142c38c 20231 cu->language = language_minimal;
c906108c
SS
20232 break;
20233 }
e142c38c 20234 cu->language_defn = language_def (cu->language);
c906108c
SS
20235}
20236
20237/* Return the named attribute or NULL if not there. */
20238
20239static struct attribute *
e142c38c 20240dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20241{
a48e046c 20242 for (;;)
c906108c 20243 {
a48e046c
TT
20244 unsigned int i;
20245 struct attribute *spec = NULL;
20246
20247 for (i = 0; i < die->num_attrs; ++i)
20248 {
20249 if (die->attrs[i].name == name)
20250 return &die->attrs[i];
20251 if (die->attrs[i].name == DW_AT_specification
20252 || die->attrs[i].name == DW_AT_abstract_origin)
20253 spec = &die->attrs[i];
20254 }
20255
20256 if (!spec)
20257 break;
c906108c 20258
f2f0e013 20259 die = follow_die_ref (die, spec, &cu);
f2f0e013 20260 }
c5aa993b 20261
c906108c
SS
20262 return NULL;
20263}
20264
348e048f
DE
20265/* Return the named attribute or NULL if not there,
20266 but do not follow DW_AT_specification, etc.
20267 This is for use in contexts where we're reading .debug_types dies.
20268 Following DW_AT_specification, DW_AT_abstract_origin will take us
20269 back up the chain, and we want to go down. */
20270
20271static struct attribute *
45e58e77 20272dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20273{
20274 unsigned int i;
20275
20276 for (i = 0; i < die->num_attrs; ++i)
20277 if (die->attrs[i].name == name)
20278 return &die->attrs[i];
20279
20280 return NULL;
20281}
20282
7d45c7c3
KB
20283/* Return the string associated with a string-typed attribute, or NULL if it
20284 is either not found or is of an incorrect type. */
20285
20286static const char *
20287dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20288{
20289 struct attribute *attr;
20290 const char *str = NULL;
20291
20292 attr = dwarf2_attr (die, name, cu);
20293
20294 if (attr != NULL)
20295 {
43988095 20296 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20297 || attr->form == DW_FORM_string
cf532bd1 20298 || attr->form == DW_FORM_strx
8fe0f950
AT
20299 || attr->form == DW_FORM_strx1
20300 || attr->form == DW_FORM_strx2
20301 || attr->form == DW_FORM_strx3
20302 || attr->form == DW_FORM_strx4
b3340438 20303 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20304 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20305 str = DW_STRING (attr);
20306 else
b98664d3 20307 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20308 "DIE at %s in module %s"),
20309 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20310 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20311 }
20312
20313 return str;
20314}
20315
a084a2a6 20316/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20317 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20318static const char *
20319dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20320{
20321 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20322 if (dwo_name == nullptr)
20323 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20324 return dwo_name;
20325}
20326
05cf31d1
JB
20327/* Return non-zero iff the attribute NAME is defined for the given DIE,
20328 and holds a non-zero value. This function should only be used for
2dc7f7b3 20329 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20330
20331static int
20332dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20333{
20334 struct attribute *attr = dwarf2_attr (die, name, cu);
20335
20336 return (attr && DW_UNSND (attr));
20337}
20338
3ca72b44 20339static int
e142c38c 20340die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20341{
05cf31d1
JB
20342 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20343 which value is non-zero. However, we have to be careful with
20344 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20345 (via dwarf2_flag_true_p) follows this attribute. So we may
20346 end up accidently finding a declaration attribute that belongs
20347 to a different DIE referenced by the specification attribute,
20348 even though the given DIE does not have a declaration attribute. */
20349 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20350 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20351}
20352
63d06c5c 20353/* Return the die giving the specification for DIE, if there is
f2f0e013 20354 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20355 containing the return value on output. If there is no
20356 specification, but there is an abstract origin, that is
20357 returned. */
63d06c5c
DC
20358
20359static struct die_info *
f2f0e013 20360die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20361{
f2f0e013
DJ
20362 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20363 *spec_cu);
63d06c5c 20364
edb3359d
DJ
20365 if (spec_attr == NULL)
20366 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20367
63d06c5c
DC
20368 if (spec_attr == NULL)
20369 return NULL;
20370 else
f2f0e013 20371 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20372}
c906108c 20373
527f3840
JK
20374/* Stub for free_line_header to match void * callback types. */
20375
20376static void
20377free_line_header_voidp (void *arg)
20378{
9a3c8263 20379 struct line_header *lh = (struct line_header *) arg;
527f3840 20380
fff8551c 20381 delete lh;
527f3840
JK
20382}
20383
fff8551c
PA
20384void
20385line_header::add_include_dir (const char *include_dir)
c906108c 20386{
27e0867f 20387 if (dwarf_line_debug >= 2)
7ba99d21
AT
20388 {
20389 size_t new_size;
20390 if (version >= 5)
20391 new_size = m_include_dirs.size ();
20392 else
20393 new_size = m_include_dirs.size () + 1;
20394 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20395 new_size, include_dir);
20396 }
20397 m_include_dirs.push_back (include_dir);
debd256d 20398}
6e70227d 20399
fff8551c
PA
20400void
20401line_header::add_file_name (const char *name,
ecfb656c 20402 dir_index d_index,
fff8551c
PA
20403 unsigned int mod_time,
20404 unsigned int length)
debd256d 20405{
27e0867f 20406 if (dwarf_line_debug >= 2)
7ba99d21
AT
20407 {
20408 size_t new_size;
20409 if (version >= 5)
20410 new_size = file_names_size ();
20411 else
20412 new_size = file_names_size () + 1;
20413 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20414 new_size, name);
20415 }
20416 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20417}
6e70227d 20418
83769d0b 20419/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20420
20421static struct dwarf2_section_info *
20422get_debug_line_section (struct dwarf2_cu *cu)
20423{
20424 struct dwarf2_section_info *section;
518817b3
SM
20425 struct dwarf2_per_objfile *dwarf2_per_objfile
20426 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20427
20428 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20429 DWO file. */
20430 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20431 section = &cu->dwo_unit->dwo_file->sections.line;
20432 else if (cu->per_cu->is_dwz)
20433 {
ed2dc618 20434 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20435
20436 section = &dwz->line;
20437 }
20438 else
20439 section = &dwarf2_per_objfile->line;
20440
20441 return section;
20442}
20443
43988095
JK
20444/* Read directory or file name entry format, starting with byte of
20445 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20446 entries count and the entries themselves in the described entry
20447 format. */
20448
20449static void
ed2dc618
SM
20450read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20451 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20452 struct line_header *lh,
20453 const struct comp_unit_head *cu_header,
20454 void (*callback) (struct line_header *lh,
20455 const char *name,
ecfb656c 20456 dir_index d_index,
43988095
JK
20457 unsigned int mod_time,
20458 unsigned int length))
20459{
20460 gdb_byte format_count, formati;
20461 ULONGEST data_count, datai;
20462 const gdb_byte *buf = *bufp;
20463 const gdb_byte *format_header_data;
43988095
JK
20464 unsigned int bytes_read;
20465
20466 format_count = read_1_byte (abfd, buf);
20467 buf += 1;
20468 format_header_data = buf;
20469 for (formati = 0; formati < format_count; formati++)
20470 {
20471 read_unsigned_leb128 (abfd, buf, &bytes_read);
20472 buf += bytes_read;
20473 read_unsigned_leb128 (abfd, buf, &bytes_read);
20474 buf += bytes_read;
20475 }
20476
20477 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20478 buf += bytes_read;
20479 for (datai = 0; datai < data_count; datai++)
20480 {
20481 const gdb_byte *format = format_header_data;
20482 struct file_entry fe;
20483
43988095
JK
20484 for (formati = 0; formati < format_count; formati++)
20485 {
ecfb656c 20486 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20487 format += bytes_read;
43988095 20488
ecfb656c 20489 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20490 format += bytes_read;
ecfb656c
PA
20491
20492 gdb::optional<const char *> string;
20493 gdb::optional<unsigned int> uint;
20494
43988095
JK
20495 switch (form)
20496 {
20497 case DW_FORM_string:
ecfb656c 20498 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20499 buf += bytes_read;
20500 break;
20501
20502 case DW_FORM_line_strp:
ed2dc618
SM
20503 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20504 abfd, buf,
ecfb656c
PA
20505 cu_header,
20506 &bytes_read));
43988095
JK
20507 buf += bytes_read;
20508 break;
20509
20510 case DW_FORM_data1:
ecfb656c 20511 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20512 buf += 1;
20513 break;
20514
20515 case DW_FORM_data2:
ecfb656c 20516 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20517 buf += 2;
20518 break;
20519
20520 case DW_FORM_data4:
ecfb656c 20521 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20522 buf += 4;
20523 break;
20524
20525 case DW_FORM_data8:
ecfb656c 20526 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20527 buf += 8;
20528 break;
20529
7ba99d21
AT
20530 case DW_FORM_data16:
20531 /* This is used for MD5, but file_entry does not record MD5s. */
20532 buf += 16;
20533 break;
20534
43988095 20535 case DW_FORM_udata:
ecfb656c 20536 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20537 buf += bytes_read;
20538 break;
20539
20540 case DW_FORM_block:
20541 /* It is valid only for DW_LNCT_timestamp which is ignored by
20542 current GDB. */
20543 break;
20544 }
ecfb656c
PA
20545
20546 switch (content_type)
20547 {
20548 case DW_LNCT_path:
20549 if (string.has_value ())
20550 fe.name = *string;
20551 break;
20552 case DW_LNCT_directory_index:
20553 if (uint.has_value ())
20554 fe.d_index = (dir_index) *uint;
20555 break;
20556 case DW_LNCT_timestamp:
20557 if (uint.has_value ())
20558 fe.mod_time = *uint;
20559 break;
20560 case DW_LNCT_size:
20561 if (uint.has_value ())
20562 fe.length = *uint;
20563 break;
20564 case DW_LNCT_MD5:
20565 break;
20566 default:
b98664d3 20567 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20568 pulongest (content_type));
20569 }
43988095
JK
20570 }
20571
ecfb656c 20572 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20573 }
20574
20575 *bufp = buf;
20576}
20577
debd256d 20578/* Read the statement program header starting at OFFSET in
3019eac3 20579 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20580 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20581 Returns NULL if there is a problem reading the header, e.g., if it
20582 has a version we don't understand.
debd256d
JB
20583
20584 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20585 the returned object point into the dwarf line section buffer,
20586 and must not be freed. */
ae2de4f8 20587
fff8551c 20588static line_header_up
9c541725 20589dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20590{
d521ce57 20591 const gdb_byte *line_ptr;
c764a876 20592 unsigned int bytes_read, offset_size;
debd256d 20593 int i;
d521ce57 20594 const char *cur_dir, *cur_file;
3019eac3
DE
20595 struct dwarf2_section_info *section;
20596 bfd *abfd;
518817b3
SM
20597 struct dwarf2_per_objfile *dwarf2_per_objfile
20598 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20599
36586728 20600 section = get_debug_line_section (cu);
3019eac3
DE
20601 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20602 if (section->buffer == NULL)
debd256d 20603 {
3019eac3 20604 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20605 complaint (_("missing .debug_line.dwo section"));
3019eac3 20606 else
b98664d3 20607 complaint (_("missing .debug_line section"));
debd256d
JB
20608 return 0;
20609 }
20610
fceca515
DE
20611 /* We can't do this until we know the section is non-empty.
20612 Only then do we know we have such a section. */
a32a8923 20613 abfd = get_section_bfd_owner (section);
fceca515 20614
a738430d
MK
20615 /* Make sure that at least there's room for the total_length field.
20616 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20617 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20618 {
4d3c2250 20619 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20620 return 0;
20621 }
20622
fff8551c 20623 line_header_up lh (new line_header ());
debd256d 20624
9c541725 20625 lh->sect_off = sect_off;
527f3840
JK
20626 lh->offset_in_dwz = cu->per_cu->is_dwz;
20627
9c541725 20628 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20629
a738430d 20630 /* Read in the header. */
6e70227d 20631 lh->total_length =
c764a876
DE
20632 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20633 &bytes_read, &offset_size);
debd256d 20634 line_ptr += bytes_read;
7ba99d21
AT
20635
20636 const gdb_byte *start_here = line_ptr;
20637
3019eac3 20638 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20639 {
4d3c2250 20640 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20641 return 0;
20642 }
7ba99d21 20643 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20644 lh->version = read_2_bytes (abfd, line_ptr);
20645 line_ptr += 2;
43988095 20646 if (lh->version > 5)
cd366ee8
DE
20647 {
20648 /* This is a version we don't understand. The format could have
20649 changed in ways we don't handle properly so just punt. */
b98664d3 20650 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20651 return NULL;
20652 }
43988095
JK
20653 if (lh->version >= 5)
20654 {
20655 gdb_byte segment_selector_size;
20656
20657 /* Skip address size. */
20658 read_1_byte (abfd, line_ptr);
20659 line_ptr += 1;
20660
20661 segment_selector_size = read_1_byte (abfd, line_ptr);
20662 line_ptr += 1;
20663 if (segment_selector_size != 0)
20664 {
b98664d3 20665 complaint (_("unsupported segment selector size %u "
43988095
JK
20666 "in .debug_line section"),
20667 segment_selector_size);
20668 return NULL;
20669 }
20670 }
c764a876
DE
20671 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20672 line_ptr += offset_size;
7ba99d21 20673 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20674 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20675 line_ptr += 1;
2dc7f7b3
TT
20676 if (lh->version >= 4)
20677 {
20678 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20679 line_ptr += 1;
20680 }
20681 else
20682 lh->maximum_ops_per_instruction = 1;
20683
20684 if (lh->maximum_ops_per_instruction == 0)
20685 {
20686 lh->maximum_ops_per_instruction = 1;
b98664d3 20687 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20688 "in `.debug_line' section"));
2dc7f7b3
TT
20689 }
20690
debd256d
JB
20691 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20692 line_ptr += 1;
20693 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20694 line_ptr += 1;
20695 lh->line_range = read_1_byte (abfd, line_ptr);
20696 line_ptr += 1;
20697 lh->opcode_base = read_1_byte (abfd, line_ptr);
20698 line_ptr += 1;
fff8551c 20699 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20700
20701 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20702 for (i = 1; i < lh->opcode_base; ++i)
20703 {
20704 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20705 line_ptr += 1;
20706 }
20707
43988095 20708 if (lh->version >= 5)
debd256d 20709 {
43988095 20710 /* Read directory table. */
ed2dc618
SM
20711 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20712 &cu->header,
b926417a 20713 [] (struct line_header *header, const char *name,
ecfb656c 20714 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20715 unsigned int length)
20716 {
b926417a 20717 header->add_include_dir (name);
fff8551c 20718 });
debd256d 20719
43988095 20720 /* Read file name table. */
ed2dc618
SM
20721 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20722 &cu->header,
b926417a 20723 [] (struct line_header *header, const char *name,
ecfb656c 20724 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20725 unsigned int length)
20726 {
b926417a 20727 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20728 });
43988095
JK
20729 }
20730 else
debd256d 20731 {
43988095
JK
20732 /* Read directory table. */
20733 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20734 {
20735 line_ptr += bytes_read;
fff8551c 20736 lh->add_include_dir (cur_dir);
43988095 20737 }
debd256d
JB
20738 line_ptr += bytes_read;
20739
43988095
JK
20740 /* Read file name table. */
20741 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20742 {
ecfb656c
PA
20743 unsigned int mod_time, length;
20744 dir_index d_index;
43988095
JK
20745
20746 line_ptr += bytes_read;
ecfb656c 20747 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20748 line_ptr += bytes_read;
20749 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20750 line_ptr += bytes_read;
20751 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20752 line_ptr += bytes_read;
20753
ecfb656c 20754 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20755 }
20756 line_ptr += bytes_read;
debd256d 20757 }
debd256d 20758
3019eac3 20759 if (line_ptr > (section->buffer + section->size))
b98664d3 20760 complaint (_("line number info header doesn't "
3e43a32a 20761 "fit in `.debug_line' section"));
debd256d 20762
debd256d
JB
20763 return lh;
20764}
c906108c 20765
c6da4cef 20766/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20767 Return the file name of the psymtab for the given file_entry.
c6da4cef 20768 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20769 If space for the result is malloc'd, *NAME_HOLDER will be set.
20770 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20771
d521ce57 20772static const char *
7ba99d21 20773psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20774 const dwarf2_psymtab *pst,
c89b44cd
TT
20775 const char *comp_dir,
20776 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20777{
d521ce57
TT
20778 const char *include_name = fe.name;
20779 const char *include_name_to_compare = include_name;
72b9f47f 20780 const char *pst_filename;
c6da4cef
DE
20781 int file_is_pst;
20782
8c43009f 20783 const char *dir_name = fe.include_dir (lh);
c6da4cef 20784
c89b44cd 20785 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20786 if (!IS_ABSOLUTE_PATH (include_name)
20787 && (dir_name != NULL || comp_dir != NULL))
20788 {
20789 /* Avoid creating a duplicate psymtab for PST.
20790 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20791 Before we do the comparison, however, we need to account
20792 for DIR_NAME and COMP_DIR.
20793 First prepend dir_name (if non-NULL). If we still don't
20794 have an absolute path prepend comp_dir (if non-NULL).
20795 However, the directory we record in the include-file's
20796 psymtab does not contain COMP_DIR (to match the
20797 corresponding symtab(s)).
20798
20799 Example:
20800
20801 bash$ cd /tmp
20802 bash$ gcc -g ./hello.c
20803 include_name = "hello.c"
20804 dir_name = "."
20805 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20806 DW_AT_name = "./hello.c"
20807
20808 */
c6da4cef
DE
20809
20810 if (dir_name != NULL)
20811 {
c89b44cd
TT
20812 name_holder->reset (concat (dir_name, SLASH_STRING,
20813 include_name, (char *) NULL));
20814 include_name = name_holder->get ();
c6da4cef 20815 include_name_to_compare = include_name;
c6da4cef
DE
20816 }
20817 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20818 {
c89b44cd
TT
20819 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20820 include_name, (char *) NULL));
20821 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20822 }
20823 }
20824
20825 pst_filename = pst->filename;
c89b44cd 20826 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20827 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20828 {
c89b44cd
TT
20829 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20830 pst_filename, (char *) NULL));
20831 pst_filename = copied_name.get ();
c6da4cef
DE
20832 }
20833
1e3fad37 20834 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20835
c6da4cef
DE
20836 if (file_is_pst)
20837 return NULL;
20838 return include_name;
20839}
20840
d9b3de22
DE
20841/* State machine to track the state of the line number program. */
20842
6f77053d 20843class lnp_state_machine
d9b3de22 20844{
6f77053d
PA
20845public:
20846 /* Initialize a machine state for the start of a line number
20847 program. */
804d2729
TT
20848 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20849 bool record_lines_p);
6f77053d 20850
8c43009f
PA
20851 file_entry *current_file ()
20852 {
20853 /* lh->file_names is 0-based, but the file name numbers in the
20854 statement program are 1-based. */
6f77053d
PA
20855 return m_line_header->file_name_at (m_file);
20856 }
20857
20858 /* Record the line in the state machine. END_SEQUENCE is true if
20859 we're processing the end of a sequence. */
20860 void record_line (bool end_sequence);
20861
7ab6656f
OJ
20862 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20863 nop-out rest of the lines in this sequence. */
6f77053d
PA
20864 void check_line_address (struct dwarf2_cu *cu,
20865 const gdb_byte *line_ptr,
7ab6656f 20866 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20867
20868 void handle_set_discriminator (unsigned int discriminator)
20869 {
20870 m_discriminator = discriminator;
20871 m_line_has_non_zero_discriminator |= discriminator != 0;
20872 }
20873
20874 /* Handle DW_LNE_set_address. */
20875 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20876 {
20877 m_op_index = 0;
20878 address += baseaddr;
20879 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20880 }
20881
20882 /* Handle DW_LNS_advance_pc. */
20883 void handle_advance_pc (CORE_ADDR adjust);
20884
20885 /* Handle a special opcode. */
20886 void handle_special_opcode (unsigned char op_code);
20887
20888 /* Handle DW_LNS_advance_line. */
20889 void handle_advance_line (int line_delta)
20890 {
20891 advance_line (line_delta);
20892 }
20893
20894 /* Handle DW_LNS_set_file. */
20895 void handle_set_file (file_name_index file);
20896
20897 /* Handle DW_LNS_negate_stmt. */
20898 void handle_negate_stmt ()
20899 {
20900 m_is_stmt = !m_is_stmt;
20901 }
20902
20903 /* Handle DW_LNS_const_add_pc. */
20904 void handle_const_add_pc ();
20905
20906 /* Handle DW_LNS_fixed_advance_pc. */
20907 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20908 {
20909 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20910 m_op_index = 0;
20911 }
20912
20913 /* Handle DW_LNS_copy. */
20914 void handle_copy ()
20915 {
20916 record_line (false);
20917 m_discriminator = 0;
20918 }
20919
20920 /* Handle DW_LNE_end_sequence. */
20921 void handle_end_sequence ()
20922 {
804d2729 20923 m_currently_recording_lines = true;
6f77053d
PA
20924 }
20925
20926private:
20927 /* Advance the line by LINE_DELTA. */
20928 void advance_line (int line_delta)
20929 {
20930 m_line += line_delta;
20931
20932 if (line_delta != 0)
20933 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20934 }
20935
804d2729
TT
20936 struct dwarf2_cu *m_cu;
20937
6f77053d
PA
20938 gdbarch *m_gdbarch;
20939
20940 /* True if we're recording lines.
20941 Otherwise we're building partial symtabs and are just interested in
20942 finding include files mentioned by the line number program. */
20943 bool m_record_lines_p;
20944
8c43009f 20945 /* The line number header. */
6f77053d 20946 line_header *m_line_header;
8c43009f 20947
6f77053d
PA
20948 /* These are part of the standard DWARF line number state machine,
20949 and initialized according to the DWARF spec. */
d9b3de22 20950
6f77053d 20951 unsigned char m_op_index = 0;
7ba99d21
AT
20952 /* The line table index of the current file. */
20953 file_name_index m_file = 1;
6f77053d
PA
20954 unsigned int m_line = 1;
20955
20956 /* These are initialized in the constructor. */
20957
20958 CORE_ADDR m_address;
20959 bool m_is_stmt;
20960 unsigned int m_discriminator;
d9b3de22
DE
20961
20962 /* Additional bits of state we need to track. */
20963
20964 /* The last file that we called dwarf2_start_subfile for.
20965 This is only used for TLLs. */
6f77053d 20966 unsigned int m_last_file = 0;
d9b3de22 20967 /* The last file a line number was recorded for. */
6f77053d 20968 struct subfile *m_last_subfile = NULL;
d9b3de22 20969
804d2729
TT
20970 /* When true, record the lines we decode. */
20971 bool m_currently_recording_lines = false;
d9b3de22
DE
20972
20973 /* The last line number that was recorded, used to coalesce
20974 consecutive entries for the same line. This can happen, for
20975 example, when discriminators are present. PR 17276. */
6f77053d
PA
20976 unsigned int m_last_line = 0;
20977 bool m_line_has_non_zero_discriminator = false;
8c43009f 20978};
d9b3de22 20979
6f77053d
PA
20980void
20981lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20982{
20983 CORE_ADDR addr_adj = (((m_op_index + adjust)
20984 / m_line_header->maximum_ops_per_instruction)
20985 * m_line_header->minimum_instruction_length);
20986 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20987 m_op_index = ((m_op_index + adjust)
20988 % m_line_header->maximum_ops_per_instruction);
20989}
d9b3de22 20990
6f77053d
PA
20991void
20992lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20993{
6f77053d
PA
20994 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20995 CORE_ADDR addr_adj = (((m_op_index
20996 + (adj_opcode / m_line_header->line_range))
20997 / m_line_header->maximum_ops_per_instruction)
20998 * m_line_header->minimum_instruction_length);
20999 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21000 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21001 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21002
6f77053d
PA
21003 int line_delta = (m_line_header->line_base
21004 + (adj_opcode % m_line_header->line_range));
21005 advance_line (line_delta);
21006 record_line (false);
21007 m_discriminator = 0;
21008}
d9b3de22 21009
6f77053d
PA
21010void
21011lnp_state_machine::handle_set_file (file_name_index file)
21012{
21013 m_file = file;
21014
21015 const file_entry *fe = current_file ();
21016 if (fe == NULL)
21017 dwarf2_debug_line_missing_file_complaint ();
21018 else if (m_record_lines_p)
21019 {
21020 const char *dir = fe->include_dir (m_line_header);
21021
c24bdb02 21022 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21023 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21024 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21025 }
21026}
21027
21028void
21029lnp_state_machine::handle_const_add_pc ()
21030{
21031 CORE_ADDR adjust
21032 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21033
21034 CORE_ADDR addr_adj
21035 = (((m_op_index + adjust)
21036 / m_line_header->maximum_ops_per_instruction)
21037 * m_line_header->minimum_instruction_length);
21038
21039 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21040 m_op_index = ((m_op_index + adjust)
21041 % m_line_header->maximum_ops_per_instruction);
21042}
d9b3de22 21043
a05a36a5
DE
21044/* Return non-zero if we should add LINE to the line number table.
21045 LINE is the line to add, LAST_LINE is the last line that was added,
21046 LAST_SUBFILE is the subfile for LAST_LINE.
21047 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21048 had a non-zero discriminator.
21049
21050 We have to be careful in the presence of discriminators.
21051 E.g., for this line:
21052
21053 for (i = 0; i < 100000; i++);
21054
21055 clang can emit four line number entries for that one line,
21056 each with a different discriminator.
21057 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21058
21059 However, we want gdb to coalesce all four entries into one.
21060 Otherwise the user could stepi into the middle of the line and
21061 gdb would get confused about whether the pc really was in the
21062 middle of the line.
21063
21064 Things are further complicated by the fact that two consecutive
21065 line number entries for the same line is a heuristic used by gcc
21066 to denote the end of the prologue. So we can't just discard duplicate
21067 entries, we have to be selective about it. The heuristic we use is
21068 that we only collapse consecutive entries for the same line if at least
21069 one of those entries has a non-zero discriminator. PR 17276.
21070
21071 Note: Addresses in the line number state machine can never go backwards
21072 within one sequence, thus this coalescing is ok. */
21073
21074static int
804d2729
TT
21075dwarf_record_line_p (struct dwarf2_cu *cu,
21076 unsigned int line, unsigned int last_line,
a05a36a5
DE
21077 int line_has_non_zero_discriminator,
21078 struct subfile *last_subfile)
21079{
c24bdb02 21080 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21081 return 1;
21082 if (line != last_line)
21083 return 1;
21084 /* Same line for the same file that we've seen already.
21085 As a last check, for pr 17276, only record the line if the line
21086 has never had a non-zero discriminator. */
21087 if (!line_has_non_zero_discriminator)
21088 return 1;
21089 return 0;
21090}
21091
804d2729
TT
21092/* Use the CU's builder to record line number LINE beginning at
21093 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21094
21095static void
d9b3de22
DE
21096dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21097 unsigned int line, CORE_ADDR address,
804d2729 21098 struct dwarf2_cu *cu)
252a6764
DE
21099{
21100 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21101
27e0867f
DE
21102 if (dwarf_line_debug)
21103 {
21104 fprintf_unfiltered (gdb_stdlog,
21105 "Recording line %u, file %s, address %s\n",
21106 line, lbasename (subfile->name),
21107 paddress (gdbarch, address));
21108 }
21109
804d2729 21110 if (cu != nullptr)
c24bdb02 21111 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21112}
21113
21114/* Subroutine of dwarf_decode_lines_1 to simplify it.
21115 Mark the end of a set of line number records.
d9b3de22 21116 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21117 If SUBFILE is NULL the request is ignored. */
21118
21119static void
21120dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21121 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21122{
27e0867f
DE
21123 if (subfile == NULL)
21124 return;
21125
21126 if (dwarf_line_debug)
21127 {
21128 fprintf_unfiltered (gdb_stdlog,
21129 "Finishing current line, file %s, address %s\n",
21130 lbasename (subfile->name),
21131 paddress (gdbarch, address));
21132 }
21133
804d2729 21134 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21135}
21136
6f77053d
PA
21137void
21138lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21139{
d9b3de22
DE
21140 if (dwarf_line_debug)
21141 {
21142 fprintf_unfiltered (gdb_stdlog,
21143 "Processing actual line %u: file %u,"
94a72be7 21144 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21145 m_line, m_file,
6f77053d 21146 paddress (m_gdbarch, m_address),
94a72be7
AB
21147 m_is_stmt, m_discriminator,
21148 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21149 }
21150
6f77053d 21151 file_entry *fe = current_file ();
8c43009f
PA
21152
21153 if (fe == NULL)
d9b3de22
DE
21154 dwarf2_debug_line_missing_file_complaint ();
21155 /* For now we ignore lines not starting on an instruction boundary.
21156 But not when processing end_sequence for compatibility with the
21157 previous version of the code. */
6f77053d 21158 else if (m_op_index == 0 || end_sequence)
d9b3de22 21159 {
8c43009f 21160 fe->included_p = 1;
94a72be7
AB
21161 if (m_record_lines_p
21162 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 21163 {
c24bdb02 21164 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21165 || end_sequence)
d9b3de22 21166 {
804d2729
TT
21167 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21168 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21169 }
21170
21171 if (!end_sequence)
21172 {
804d2729 21173 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21174 m_line_has_non_zero_discriminator,
21175 m_last_subfile))
d9b3de22 21176 {
c24bdb02 21177 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21178 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21179 builder->get_current_subfile (),
6f77053d 21180 m_line, m_address,
804d2729 21181 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21182 }
c24bdb02 21183 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21184 m_last_line = m_line;
d9b3de22
DE
21185 }
21186 }
21187 }
21188}
21189
804d2729
TT
21190lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21191 line_header *lh, bool record_lines_p)
d9b3de22 21192{
804d2729 21193 m_cu = cu;
6f77053d
PA
21194 m_gdbarch = arch;
21195 m_record_lines_p = record_lines_p;
21196 m_line_header = lh;
d9b3de22 21197
804d2729 21198 m_currently_recording_lines = true;
d9b3de22 21199
d9b3de22
DE
21200 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21201 was a line entry for it so that the backend has a chance to adjust it
21202 and also record it in case it needs it. This is currently used by MIPS
21203 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21204 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21205 m_is_stmt = lh->default_is_stmt;
21206 m_discriminator = 0;
252a6764
DE
21207}
21208
6f77053d
PA
21209void
21210lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21211 const gdb_byte *line_ptr,
7ab6656f 21212 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21213{
7ab6656f
OJ
21214 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21215 the pc range of the CU. However, we restrict the test to only ADDRESS
21216 values of zero to preserve GDB's previous behaviour which is to handle
21217 the specific case of a function being GC'd by the linker. */
924c2928 21218
7ab6656f 21219 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21220 {
21221 /* This line table is for a function which has been
21222 GCd by the linker. Ignore it. PR gdb/12528 */
21223
518817b3 21224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21225 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21226
b98664d3 21227 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21228 line_offset, objfile_name (objfile));
804d2729
TT
21229 m_currently_recording_lines = false;
21230 /* Note: m_currently_recording_lines is left as false until we see
21231 DW_LNE_end_sequence. */
924c2928
DE
21232 }
21233}
21234
f3f5162e 21235/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21236 Process the line number information in LH.
21237 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21238 program in order to set included_p for every referenced header. */
debd256d 21239
c906108c 21240static void
43f3e411
DE
21241dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21242 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21243{
d521ce57
TT
21244 const gdb_byte *line_ptr, *extended_end;
21245 const gdb_byte *line_end;
a8c50c1f 21246 unsigned int bytes_read, extended_len;
699ca60a 21247 unsigned char op_code, extended_op;
e142c38c 21248 CORE_ADDR baseaddr;
518817b3 21249 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21250 bfd *abfd = objfile->obfd;
fbf65064 21251 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21252 /* True if we're recording line info (as opposed to building partial
21253 symtabs and just interested in finding include files mentioned by
21254 the line number program). */
21255 bool record_lines_p = !decode_for_pst_p;
e142c38c 21256
b3b3bada 21257 baseaddr = objfile->text_section_offset ();
c906108c 21258
debd256d
JB
21259 line_ptr = lh->statement_program_start;
21260 line_end = lh->statement_program_end;
c906108c
SS
21261
21262 /* Read the statement sequences until there's nothing left. */
21263 while (line_ptr < line_end)
21264 {
6f77053d
PA
21265 /* The DWARF line number program state machine. Reset the state
21266 machine at the start of each sequence. */
804d2729 21267 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21268 bool end_sequence = false;
d9b3de22 21269
8c43009f 21270 if (record_lines_p)
c906108c 21271 {
8c43009f
PA
21272 /* Start a subfile for the current file of the state
21273 machine. */
21274 const file_entry *fe = state_machine.current_file ();
21275
21276 if (fe != NULL)
804d2729 21277 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21278 }
21279
a738430d 21280 /* Decode the table. */
d9b3de22 21281 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21282 {
21283 op_code = read_1_byte (abfd, line_ptr);
21284 line_ptr += 1;
9aa1fe7e 21285
debd256d 21286 if (op_code >= lh->opcode_base)
6e70227d 21287 {
8e07a239 21288 /* Special opcode. */
6f77053d 21289 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21290 }
21291 else switch (op_code)
c906108c
SS
21292 {
21293 case DW_LNS_extended_op:
3e43a32a
MS
21294 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21295 &bytes_read);
473b7be6 21296 line_ptr += bytes_read;
a8c50c1f 21297 extended_end = line_ptr + extended_len;
c906108c
SS
21298 extended_op = read_1_byte (abfd, line_ptr);
21299 line_ptr += 1;
21300 switch (extended_op)
21301 {
21302 case DW_LNE_end_sequence:
6f77053d
PA
21303 state_machine.handle_end_sequence ();
21304 end_sequence = true;
c906108c
SS
21305 break;
21306 case DW_LNE_set_address:
d9b3de22
DE
21307 {
21308 CORE_ADDR address
21309 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21310 line_ptr += bytes_read;
6f77053d
PA
21311
21312 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21313 lowpc - baseaddr, address);
6f77053d 21314 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21315 }
c906108c
SS
21316 break;
21317 case DW_LNE_define_file:
debd256d 21318 {
d521ce57 21319 const char *cur_file;
ecfb656c
PA
21320 unsigned int mod_time, length;
21321 dir_index dindex;
6e70227d 21322
3e43a32a
MS
21323 cur_file = read_direct_string (abfd, line_ptr,
21324 &bytes_read);
debd256d 21325 line_ptr += bytes_read;
ecfb656c 21326 dindex = (dir_index)
debd256d
JB
21327 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21328 line_ptr += bytes_read;
21329 mod_time =
21330 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21331 line_ptr += bytes_read;
21332 length =
21333 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21334 line_ptr += bytes_read;
ecfb656c 21335 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21336 }
c906108c 21337 break;
d0c6ba3d 21338 case DW_LNE_set_discriminator:
6f77053d
PA
21339 {
21340 /* The discriminator is not interesting to the
21341 debugger; just ignore it. We still need to
21342 check its value though:
21343 if there are consecutive entries for the same
21344 (non-prologue) line we want to coalesce them.
21345 PR 17276. */
21346 unsigned int discr
21347 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21348 line_ptr += bytes_read;
21349
21350 state_machine.handle_set_discriminator (discr);
21351 }
d0c6ba3d 21352 break;
c906108c 21353 default:
b98664d3 21354 complaint (_("mangled .debug_line section"));
debd256d 21355 return;
c906108c 21356 }
a8c50c1f
DJ
21357 /* Make sure that we parsed the extended op correctly. If e.g.
21358 we expected a different address size than the producer used,
21359 we may have read the wrong number of bytes. */
21360 if (line_ptr != extended_end)
21361 {
b98664d3 21362 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21363 return;
21364 }
c906108c
SS
21365 break;
21366 case DW_LNS_copy:
6f77053d 21367 state_machine.handle_copy ();
c906108c
SS
21368 break;
21369 case DW_LNS_advance_pc:
2dc7f7b3
TT
21370 {
21371 CORE_ADDR adjust
21372 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21373 line_ptr += bytes_read;
6f77053d
PA
21374
21375 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21376 }
c906108c
SS
21377 break;
21378 case DW_LNS_advance_line:
a05a36a5
DE
21379 {
21380 int line_delta
21381 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21382 line_ptr += bytes_read;
6f77053d
PA
21383
21384 state_machine.handle_advance_line (line_delta);
a05a36a5 21385 }
c906108c
SS
21386 break;
21387 case DW_LNS_set_file:
d9b3de22 21388 {
6f77053d 21389 file_name_index file
ecfb656c
PA
21390 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21391 &bytes_read);
d9b3de22 21392 line_ptr += bytes_read;
8c43009f 21393
6f77053d 21394 state_machine.handle_set_file (file);
d9b3de22 21395 }
c906108c
SS
21396 break;
21397 case DW_LNS_set_column:
0ad93d4f 21398 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21399 line_ptr += bytes_read;
21400 break;
21401 case DW_LNS_negate_stmt:
6f77053d 21402 state_machine.handle_negate_stmt ();
c906108c
SS
21403 break;
21404 case DW_LNS_set_basic_block:
c906108c 21405 break;
c2c6d25f
JM
21406 /* Add to the address register of the state machine the
21407 address increment value corresponding to special opcode
a738430d
MK
21408 255. I.e., this value is scaled by the minimum
21409 instruction length since special opcode 255 would have
b021a221 21410 scaled the increment. */
c906108c 21411 case DW_LNS_const_add_pc:
6f77053d 21412 state_machine.handle_const_add_pc ();
c906108c
SS
21413 break;
21414 case DW_LNS_fixed_advance_pc:
3e29f34a 21415 {
6f77053d 21416 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21417 line_ptr += 2;
6f77053d
PA
21418
21419 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21420 }
c906108c 21421 break;
9aa1fe7e 21422 default:
a738430d
MK
21423 {
21424 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21425 int i;
a738430d 21426
debd256d 21427 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21428 {
21429 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21430 line_ptr += bytes_read;
21431 }
21432 }
c906108c
SS
21433 }
21434 }
d9b3de22
DE
21435
21436 if (!end_sequence)
21437 dwarf2_debug_line_missing_end_sequence_complaint ();
21438
21439 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21440 in which case we still finish recording the last line). */
6f77053d 21441 state_machine.record_line (true);
c906108c 21442 }
f3f5162e
DE
21443}
21444
21445/* Decode the Line Number Program (LNP) for the given line_header
21446 structure and CU. The actual information extracted and the type
21447 of structures created from the LNP depends on the value of PST.
21448
21449 1. If PST is NULL, then this procedure uses the data from the program
21450 to create all necessary symbol tables, and their linetables.
21451
21452 2. If PST is not NULL, this procedure reads the program to determine
21453 the list of files included by the unit represented by PST, and
21454 builds all the associated partial symbol tables.
21455
21456 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21457 It is used for relative paths in the line table.
21458 NOTE: When processing partial symtabs (pst != NULL),
21459 comp_dir == pst->dirname.
21460
21461 NOTE: It is important that psymtabs have the same file name (via strcmp)
21462 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21463 symtab we don't use it in the name of the psymtabs we create.
21464 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21465 A good testcase for this is mb-inline.exp.
21466
527f3840
JK
21467 LOWPC is the lowest address in CU (or 0 if not known).
21468
21469 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21470 for its PC<->lines mapping information. Otherwise only the filename
21471 table is read in. */
f3f5162e
DE
21472
21473static void
21474dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21475 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21476 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21477{
518817b3 21478 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21479 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21480
527f3840
JK
21481 if (decode_mapping)
21482 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21483
21484 if (decode_for_pst_p)
21485 {
aaa75496
JB
21486 /* Now that we're done scanning the Line Header Program, we can
21487 create the psymtab of each included file. */
7ba99d21
AT
21488 for (auto &file_entry : lh->file_names ())
21489 if (file_entry.included_p == 1)
aaa75496 21490 {
c89b44cd 21491 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21492 const char *include_name =
7ba99d21
AT
21493 psymtab_include_file_name (lh, file_entry, pst,
21494 comp_dir, &name_holder);
c6da4cef 21495 if (include_name != NULL)
aaa75496
JB
21496 dwarf2_create_include_psymtab (include_name, pst, objfile);
21497 }
21498 }
cb1df416
DJ
21499 else
21500 {
21501 /* Make sure a symtab is created for every file, even files
21502 which contain only variables (i.e. no code with associated
21503 line numbers). */
c24bdb02
KS
21504 buildsym_compunit *builder = cu->get_builder ();
21505 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21506
7ba99d21 21507 for (auto &fe : lh->file_names ())
cb1df416 21508 {
804d2729 21509 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21510 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21511 {
c24bdb02 21512 builder->get_current_subfile ()->symtab
804d2729 21513 = allocate_symtab (cust,
c24bdb02 21514 builder->get_current_subfile ()->name);
43f3e411 21515 }
c24bdb02 21516 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21517 }
21518 }
c906108c
SS
21519}
21520
21521/* Start a subfile for DWARF. FILENAME is the name of the file and
21522 DIRNAME the name of the source directory which contains FILENAME
4d663531 21523 or NULL if not known.
c906108c
SS
21524 This routine tries to keep line numbers from identical absolute and
21525 relative file names in a common subfile.
21526
21527 Using the `list' example from the GDB testsuite, which resides in
21528 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21529 of /srcdir/list0.c yields the following debugging information for list0.c:
21530
c5aa993b 21531 DW_AT_name: /srcdir/list0.c
4d663531 21532 DW_AT_comp_dir: /compdir
357e46e7 21533 files.files[0].name: list0.h
c5aa993b 21534 files.files[0].dir: /srcdir
357e46e7 21535 files.files[1].name: list0.c
c5aa993b 21536 files.files[1].dir: /srcdir
c906108c
SS
21537
21538 The line number information for list0.c has to end up in a single
4f1520fb
FR
21539 subfile, so that `break /srcdir/list0.c:1' works as expected.
21540 start_subfile will ensure that this happens provided that we pass the
21541 concatenation of files.files[1].dir and files.files[1].name as the
21542 subfile's name. */
c906108c
SS
21543
21544static void
804d2729
TT
21545dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21546 const char *dirname)
c906108c 21547{
43816ebc 21548 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21549
4d663531 21550 /* In order not to lose the line information directory,
4f1520fb
FR
21551 we concatenate it to the filename when it makes sense.
21552 Note that the Dwarf3 standard says (speaking of filenames in line
21553 information): ``The directory index is ignored for file names
21554 that represent full path names''. Thus ignoring dirname in the
21555 `else' branch below isn't an issue. */
c906108c 21556
d5166ae1 21557 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21558 {
43816ebc
TT
21559 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21560 filename = copy.get ();
d521ce57 21561 }
c906108c 21562
c24bdb02 21563 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21564}
21565
804d2729
TT
21566/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21567 buildsym_compunit constructor. */
f4dc4d17 21568
c24bdb02
KS
21569struct compunit_symtab *
21570dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21571 CORE_ADDR low_pc)
f4dc4d17 21572{
c24bdb02 21573 gdb_assert (m_builder == nullptr);
43f3e411 21574
c24bdb02
KS
21575 m_builder.reset (new struct buildsym_compunit
21576 (per_cu->dwarf2_per_objfile->objfile,
21577 name, comp_dir, language, low_pc));
93b8bea4 21578
c24bdb02 21579 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21580
c24bdb02
KS
21581 get_builder ()->record_debugformat ("DWARF 2");
21582 get_builder ()->record_producer (producer);
f4dc4d17 21583
c24bdb02 21584 processing_has_namespace_info = false;
43f3e411 21585
c24bdb02 21586 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21587}
21588
4c2df51b
DJ
21589static void
21590var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21591 struct dwarf2_cu *cu)
4c2df51b 21592{
518817b3 21593 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21594 struct comp_unit_head *cu_header = &cu->header;
21595
4c2df51b
DJ
21596 /* NOTE drow/2003-01-30: There used to be a comment and some special
21597 code here to turn a symbol with DW_AT_external and a
21598 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21599 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21600 with some versions of binutils) where shared libraries could have
21601 relocations against symbols in their debug information - the
21602 minimal symbol would have the right address, but the debug info
21603 would not. It's no longer necessary, because we will explicitly
21604 apply relocations when we read in the debug information now. */
21605
21606 /* A DW_AT_location attribute with no contents indicates that a
21607 variable has been optimized away. */
21608 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21609 {
f1e6e072 21610 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21611 return;
21612 }
21613
21614 /* Handle one degenerate form of location expression specially, to
21615 preserve GDB's previous behavior when section offsets are
336d760d
AT
21616 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21617 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21618
21619 if (attr_form_is_block (attr)
3019eac3
DE
21620 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21621 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21622 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21623 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21624 && (DW_BLOCK (attr)->size
21625 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21626 {
891d2f0b 21627 unsigned int dummy;
4c2df51b 21628
3019eac3 21629 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21630 SET_SYMBOL_VALUE_ADDRESS (sym,
21631 read_address (objfile->obfd,
21632 DW_BLOCK (attr)->data + 1,
21633 cu, &dummy));
3019eac3 21634 else
38583298
TT
21635 SET_SYMBOL_VALUE_ADDRESS
21636 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21637 &dummy));
f1e6e072 21638 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21639 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21640 SET_SYMBOL_VALUE_ADDRESS
21641 (sym,
21642 SYMBOL_VALUE_ADDRESS (sym)
21643 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21644 return;
21645 }
21646
21647 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21648 expression evaluator, and use LOC_COMPUTED only when necessary
21649 (i.e. when the value of a register or memory location is
21650 referenced, or a thread-local block, etc.). Then again, it might
21651 not be worthwhile. I'm assuming that it isn't unless performance
21652 or memory numbers show me otherwise. */
21653
f1e6e072 21654 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21655
f1e6e072 21656 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21657 cu->has_loclist = true;
4c2df51b
DJ
21658}
21659
c906108c
SS
21660/* Given a pointer to a DWARF information entry, figure out if we need
21661 to make a symbol table entry for it, and if so, create a new entry
21662 and return a pointer to it.
21663 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21664 used the passed type.
21665 If SPACE is not NULL, use it to hold the new symbol. If it is
21666 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21667
21668static struct symbol *
5e2db402
TT
21669new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21670 struct symbol *space)
c906108c 21671{
518817b3
SM
21672 struct dwarf2_per_objfile *dwarf2_per_objfile
21673 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21674 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21675 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21676 struct symbol *sym = NULL;
15d034d0 21677 const char *name;
c906108c
SS
21678 struct attribute *attr = NULL;
21679 struct attribute *attr2 = NULL;
e142c38c 21680 CORE_ADDR baseaddr;
e37fd15a
SW
21681 struct pending **list_to_add = NULL;
21682
edb3359d 21683 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21684
b3b3bada 21685 baseaddr = objfile->text_section_offset ();
c906108c 21686
94af9270 21687 name = dwarf2_name (die, cu);
c906108c
SS
21688 if (name)
21689 {
94af9270 21690 const char *linkagename;
34eaf542 21691 int suppress_add = 0;
94af9270 21692
34eaf542
TT
21693 if (space)
21694 sym = space;
21695 else
e623cf5d 21696 sym = allocate_symbol (objfile);
c906108c 21697 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21698
21699 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21700 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21701 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21702 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21703
f55ee35c
JK
21704 /* Fortran does not have mangling standard and the mangling does differ
21705 between gfortran, iFort etc. */
21706 if (cu->language == language_fortran
468c0cbb
CB
21707 && symbol_get_demangled_name (sym) == NULL)
21708 symbol_set_demangled_name (sym,
cfc594ee 21709 dwarf2_full_name (name, die, cu),
29df156d 21710 NULL);
f55ee35c 21711
c906108c 21712 /* Default assumptions.
c5aa993b 21713 Use the passed type or decode it from the die. */
176620f1 21714 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21715 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21716 if (type != NULL)
21717 SYMBOL_TYPE (sym) = type;
21718 else
e7c27a73 21719 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21720 attr = dwarf2_attr (die,
21721 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21722 cu);
435d3d88 21723 if (attr != nullptr)
c906108c
SS
21724 {
21725 SYMBOL_LINE (sym) = DW_UNSND (attr);
21726 }
cb1df416 21727
edb3359d
DJ
21728 attr = dwarf2_attr (die,
21729 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21730 cu);
435d3d88 21731 if (attr != nullptr)
cb1df416 21732 {
ecfb656c 21733 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21734 struct file_entry *fe;
9a619af0 21735
ecfb656c
PA
21736 if (cu->line_header != NULL)
21737 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21738 else
21739 fe = NULL;
21740
21741 if (fe == NULL)
b98664d3 21742 complaint (_("file index out of range"));
8c43009f
PA
21743 else
21744 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21745 }
21746
c906108c
SS
21747 switch (die->tag)
21748 {
21749 case DW_TAG_label:
e142c38c 21750 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21751 if (attr != nullptr)
3e29f34a
MR
21752 {
21753 CORE_ADDR addr;
21754
21755 addr = attr_value_as_address (attr);
21756 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21757 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21758 }
0f5238ed
TT
21759 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21760 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21761 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21762 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21763 break;
21764 case DW_TAG_subprogram:
21765 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21766 finish_block. */
f1e6e072 21767 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21768 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21769 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21770 || cu->language == language_ada
21771 || cu->language == language_fortran)
c906108c 21772 {
2cfa0c8d 21773 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21774 Ada and Fortran subprograms, whether marked external or
21775 not, are always stored as a global symbol, because we want
21776 to be able to access them globally. For instance, we want
21777 to be able to break on a nested subprogram without having
21778 to specify the context. */
c24bdb02 21779 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21780 }
21781 else
21782 {
e37fd15a 21783 list_to_add = cu->list_in_scope;
c906108c
SS
21784 }
21785 break;
edb3359d
DJ
21786 case DW_TAG_inlined_subroutine:
21787 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21788 finish_block. */
f1e6e072 21789 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21790 SYMBOL_INLINED (sym) = 1;
481860b3 21791 list_to_add = cu->list_in_scope;
edb3359d 21792 break;
34eaf542
TT
21793 case DW_TAG_template_value_param:
21794 suppress_add = 1;
21795 /* Fall through. */
72929c62 21796 case DW_TAG_constant:
c906108c 21797 case DW_TAG_variable:
254e6b9e 21798 case DW_TAG_member:
0963b4bd
MS
21799 /* Compilation with minimal debug info may result in
21800 variables with missing type entries. Change the
21801 misleading `void' type to something sensible. */
c906108c 21802 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21803 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21804
e142c38c 21805 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21806 /* In the case of DW_TAG_member, we should only be called for
21807 static const members. */
21808 if (die->tag == DW_TAG_member)
21809 {
3863f96c
DE
21810 /* dwarf2_add_field uses die_is_declaration,
21811 so we do the same. */
254e6b9e
DE
21812 gdb_assert (die_is_declaration (die, cu));
21813 gdb_assert (attr);
21814 }
435d3d88 21815 if (attr != nullptr)
c906108c 21816 {
e7c27a73 21817 dwarf2_const_value (attr, sym, cu);
e142c38c 21818 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21819 if (!suppress_add)
34eaf542
TT
21820 {
21821 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21822 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21823 else
e37fd15a 21824 list_to_add = cu->list_in_scope;
34eaf542 21825 }
c906108c
SS
21826 break;
21827 }
e142c38c 21828 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21829 if (attr != nullptr)
c906108c 21830 {
e7c27a73 21831 var_decode_location (attr, sym, cu);
e142c38c 21832 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21833
21834 /* Fortran explicitly imports any global symbols to the local
21835 scope by DW_TAG_common_block. */
21836 if (cu->language == language_fortran && die->parent
21837 && die->parent->tag == DW_TAG_common_block)
21838 attr2 = NULL;
21839
caac4577
JG
21840 if (SYMBOL_CLASS (sym) == LOC_STATIC
21841 && SYMBOL_VALUE_ADDRESS (sym) == 0
21842 && !dwarf2_per_objfile->has_section_at_zero)
21843 {
21844 /* When a static variable is eliminated by the linker,
21845 the corresponding debug information is not stripped
21846 out, but the variable address is set to null;
21847 do not add such variables into symbol table. */
21848 }
21849 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21850 {
4b610737
TT
21851 if (SYMBOL_CLASS (sym) == LOC_STATIC
21852 && (objfile->flags & OBJF_MAINLINE) == 0
21853 && dwarf2_per_objfile->can_copy)
21854 {
21855 /* A global static variable might be subject to
21856 copy relocation. We first check for a local
21857 minsym, though, because maybe the symbol was
21858 marked hidden, in which case this would not
21859 apply. */
21860 bound_minimal_symbol found
21861 = (lookup_minimal_symbol_linkage
987012b8 21862 (sym->linkage_name (), objfile));
4b610737
TT
21863 if (found.minsym != nullptr)
21864 sym->maybe_copied = 1;
21865 }
f55ee35c 21866
1c809c68
TT
21867 /* A variable with DW_AT_external is never static,
21868 but it may be block-scoped. */
804d2729 21869 list_to_add
c24bdb02
KS
21870 = ((cu->list_in_scope
21871 == cu->get_builder ()->get_file_symbols ())
21872 ? cu->get_builder ()->get_global_symbols ()
804d2729 21873 : cu->list_in_scope);
1c809c68 21874 }
c906108c 21875 else
e37fd15a 21876 list_to_add = cu->list_in_scope;
c906108c
SS
21877 }
21878 else
21879 {
21880 /* We do not know the address of this symbol.
c5aa993b
JM
21881 If it is an external symbol and we have type information
21882 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21883 The address of the variable will then be determined from
21884 the minimal symbol table whenever the variable is
21885 referenced. */
e142c38c 21886 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21887
21888 /* Fortran explicitly imports any global symbols to the local
21889 scope by DW_TAG_common_block. */
21890 if (cu->language == language_fortran && die->parent
21891 && die->parent->tag == DW_TAG_common_block)
21892 {
21893 /* SYMBOL_CLASS doesn't matter here because
21894 read_common_block is going to reset it. */
21895 if (!suppress_add)
21896 list_to_add = cu->list_in_scope;
21897 }
21898 else if (attr2 && (DW_UNSND (attr2) != 0)
21899 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21900 {
0fe7935b
DJ
21901 /* A variable with DW_AT_external is never static, but it
21902 may be block-scoped. */
804d2729 21903 list_to_add
c24bdb02
KS
21904 = ((cu->list_in_scope
21905 == cu->get_builder ()->get_file_symbols ())
21906 ? cu->get_builder ()->get_global_symbols ()
804d2729 21907 : cu->list_in_scope);
0fe7935b 21908
f1e6e072 21909 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21910 }
442ddf59
JK
21911 else if (!die_is_declaration (die, cu))
21912 {
21913 /* Use the default LOC_OPTIMIZED_OUT class. */
21914 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21915 if (!suppress_add)
21916 list_to_add = cu->list_in_scope;
442ddf59 21917 }
c906108c
SS
21918 }
21919 break;
21920 case DW_TAG_formal_parameter:
a60f3166
TT
21921 {
21922 /* If we are inside a function, mark this as an argument. If
21923 not, we might be looking at an argument to an inlined function
21924 when we do not have enough information to show inlined frames;
21925 pretend it's a local variable in that case so that the user can
21926 still see it. */
804d2729 21927 struct context_stack *curr
c24bdb02 21928 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21929 if (curr != nullptr && curr->name != nullptr)
21930 SYMBOL_IS_ARGUMENT (sym) = 1;
21931 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21932 if (attr != nullptr)
a60f3166
TT
21933 {
21934 var_decode_location (attr, sym, cu);
21935 }
21936 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21937 if (attr != nullptr)
a60f3166
TT
21938 {
21939 dwarf2_const_value (attr, sym, cu);
21940 }
f346a30d 21941
a60f3166
TT
21942 list_to_add = cu->list_in_scope;
21943 }
c906108c
SS
21944 break;
21945 case DW_TAG_unspecified_parameters:
21946 /* From varargs functions; gdb doesn't seem to have any
21947 interest in this information, so just ignore it for now.
21948 (FIXME?) */
21949 break;
34eaf542
TT
21950 case DW_TAG_template_type_param:
21951 suppress_add = 1;
21952 /* Fall through. */
c906108c 21953 case DW_TAG_class_type:
680b30c7 21954 case DW_TAG_interface_type:
c906108c
SS
21955 case DW_TAG_structure_type:
21956 case DW_TAG_union_type:
72019c9c 21957 case DW_TAG_set_type:
c906108c 21958 case DW_TAG_enumeration_type:
f1e6e072 21959 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21960 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21961
63d06c5c 21962 {
9c37b5ae 21963 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21964 really ever be static objects: otherwise, if you try
21965 to, say, break of a class's method and you're in a file
21966 which doesn't mention that class, it won't work unless
21967 the check for all static symbols in lookup_symbol_aux
21968 saves you. See the OtherFileClass tests in
21969 gdb.c++/namespace.exp. */
21970
e37fd15a 21971 if (!suppress_add)
34eaf542 21972 {
c24bdb02 21973 buildsym_compunit *builder = cu->get_builder ();
804d2729 21974 list_to_add
c24bdb02 21975 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21976 && cu->language == language_cplus
c24bdb02 21977 ? builder->get_global_symbols ()
804d2729 21978 : cu->list_in_scope);
63d06c5c 21979
64382290 21980 /* The semantics of C++ state that "struct foo {
9c37b5ae 21981 ... }" also defines a typedef for "foo". */
64382290 21982 if (cu->language == language_cplus
45280282 21983 || cu->language == language_ada
c44af4eb
TT
21984 || cu->language == language_d
21985 || cu->language == language_rust)
64382290
TT
21986 {
21987 /* The symbol's name is already allocated along
21988 with this objfile, so we don't need to
21989 duplicate it for the type. */
21990 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 21991 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 21992 }
63d06c5c
DC
21993 }
21994 }
c906108c
SS
21995 break;
21996 case DW_TAG_typedef:
f1e6e072 21997 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21998 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21999 list_to_add = cu->list_in_scope;
63d06c5c 22000 break;
c906108c 22001 case DW_TAG_base_type:
a02abb62 22002 case DW_TAG_subrange_type:
f1e6e072 22003 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22004 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22005 list_to_add = cu->list_in_scope;
c906108c
SS
22006 break;
22007 case DW_TAG_enumerator:
e142c38c 22008 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22009 if (attr != nullptr)
c906108c 22010 {
e7c27a73 22011 dwarf2_const_value (attr, sym, cu);
c906108c 22012 }
63d06c5c
DC
22013 {
22014 /* NOTE: carlton/2003-11-10: See comment above in the
22015 DW_TAG_class_type, etc. block. */
22016
804d2729 22017 list_to_add
c24bdb02 22018 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22019 && cu->language == language_cplus
c24bdb02 22020 ? cu->get_builder ()->get_global_symbols ()
804d2729 22021 : cu->list_in_scope);
63d06c5c 22022 }
c906108c 22023 break;
74921315 22024 case DW_TAG_imported_declaration:
5c4e30ca 22025 case DW_TAG_namespace:
f1e6e072 22026 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22027 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22028 break;
530e8392
KB
22029 case DW_TAG_module:
22030 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22031 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22032 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22033 break;
4357ac6c 22034 case DW_TAG_common_block:
f1e6e072 22035 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22036 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22037 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22038 break;
c906108c
SS
22039 default:
22040 /* Not a tag we recognize. Hopefully we aren't processing
22041 trash data, but since we must specifically ignore things
22042 we don't recognize, there is nothing else we should do at
0963b4bd 22043 this point. */
b98664d3 22044 complaint (_("unsupported tag: '%s'"),
4d3c2250 22045 dwarf_tag_name (die->tag));
c906108c
SS
22046 break;
22047 }
df8a16a1 22048
e37fd15a
SW
22049 if (suppress_add)
22050 {
22051 sym->hash_next = objfile->template_symbols;
22052 objfile->template_symbols = sym;
22053 list_to_add = NULL;
22054 }
22055
22056 if (list_to_add != NULL)
d3cb6808 22057 add_symbol_to_list (sym, list_to_add);
e37fd15a 22058
df8a16a1
DJ
22059 /* For the benefit of old versions of GCC, check for anonymous
22060 namespaces based on the demangled name. */
4d4ec4e5 22061 if (!cu->processing_has_namespace_info
94af9270 22062 && cu->language == language_cplus)
c24bdb02 22063 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22064 }
22065 return (sym);
22066}
22067
98bfdba5
PA
22068/* Given an attr with a DW_FORM_dataN value in host byte order,
22069 zero-extend it as appropriate for the symbol's type. The DWARF
22070 standard (v4) is not entirely clear about the meaning of using
22071 DW_FORM_dataN for a constant with a signed type, where the type is
22072 wider than the data. The conclusion of a discussion on the DWARF
22073 list was that this is unspecified. We choose to always zero-extend
22074 because that is the interpretation long in use by GCC. */
c906108c 22075
98bfdba5 22076static gdb_byte *
ff39bb5e 22077dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22078 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22079{
518817b3 22080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22081 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22082 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22083 LONGEST l = DW_UNSND (attr);
22084
22085 if (bits < sizeof (*value) * 8)
22086 {
22087 l &= ((LONGEST) 1 << bits) - 1;
22088 *value = l;
22089 }
22090 else if (bits == sizeof (*value) * 8)
22091 *value = l;
22092 else
22093 {
224c3ddb 22094 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22095 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22096 return bytes;
22097 }
22098
22099 return NULL;
22100}
22101
22102/* Read a constant value from an attribute. Either set *VALUE, or if
22103 the value does not fit in *VALUE, set *BYTES - either already
22104 allocated on the objfile obstack, or newly allocated on OBSTACK,
22105 or, set *BATON, if we translated the constant to a location
22106 expression. */
22107
22108static void
ff39bb5e 22109dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22110 const char *name, struct obstack *obstack,
22111 struct dwarf2_cu *cu,
d521ce57 22112 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22113 struct dwarf2_locexpr_baton **baton)
22114{
518817b3 22115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22116 struct comp_unit_head *cu_header = &cu->header;
c906108c 22117 struct dwarf_block *blk;
98bfdba5
PA
22118 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22119 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22120
22121 *value = 0;
22122 *bytes = NULL;
22123 *baton = NULL;
c906108c
SS
22124
22125 switch (attr->form)
22126 {
22127 case DW_FORM_addr:
336d760d 22128 case DW_FORM_addrx:
3019eac3 22129 case DW_FORM_GNU_addr_index:
ac56253d 22130 {
ac56253d
TT
22131 gdb_byte *data;
22132
98bfdba5
PA
22133 if (TYPE_LENGTH (type) != cu_header->addr_size)
22134 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22135 cu_header->addr_size,
98bfdba5 22136 TYPE_LENGTH (type));
ac56253d
TT
22137 /* Symbols of this form are reasonably rare, so we just
22138 piggyback on the existing location code rather than writing
22139 a new implementation of symbol_computed_ops. */
8d749320 22140 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22141 (*baton)->per_cu = cu->per_cu;
22142 gdb_assert ((*baton)->per_cu);
ac56253d 22143
98bfdba5 22144 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22145 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22146 (*baton)->data = data;
ac56253d
TT
22147
22148 data[0] = DW_OP_addr;
22149 store_unsigned_integer (&data[1], cu_header->addr_size,
22150 byte_order, DW_ADDR (attr));
22151 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22152 }
c906108c 22153 break;
4ac36638 22154 case DW_FORM_string:
93b5768b 22155 case DW_FORM_strp:
cf532bd1 22156 case DW_FORM_strx:
3019eac3 22157 case DW_FORM_GNU_str_index:
36586728 22158 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22159 /* DW_STRING is already allocated on the objfile obstack, point
22160 directly to it. */
d521ce57 22161 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22162 break;
c906108c
SS
22163 case DW_FORM_block1:
22164 case DW_FORM_block2:
22165 case DW_FORM_block4:
22166 case DW_FORM_block:
2dc7f7b3 22167 case DW_FORM_exprloc:
0224619f 22168 case DW_FORM_data16:
c906108c 22169 blk = DW_BLOCK (attr);
98bfdba5
PA
22170 if (TYPE_LENGTH (type) != blk->size)
22171 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22172 TYPE_LENGTH (type));
22173 *bytes = blk->data;
c906108c 22174 break;
2df3850c
JM
22175
22176 /* The DW_AT_const_value attributes are supposed to carry the
22177 symbol's value "represented as it would be on the target
22178 architecture." By the time we get here, it's already been
22179 converted to host endianness, so we just need to sign- or
22180 zero-extend it as appropriate. */
22181 case DW_FORM_data1:
3aef2284 22182 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22183 break;
c906108c 22184 case DW_FORM_data2:
3aef2284 22185 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22186 break;
c906108c 22187 case DW_FORM_data4:
3aef2284 22188 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22189 break;
c906108c 22190 case DW_FORM_data8:
3aef2284 22191 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22192 break;
22193
c906108c 22194 case DW_FORM_sdata:
663c44ac 22195 case DW_FORM_implicit_const:
98bfdba5 22196 *value = DW_SND (attr);
2df3850c
JM
22197 break;
22198
c906108c 22199 case DW_FORM_udata:
98bfdba5 22200 *value = DW_UNSND (attr);
c906108c 22201 break;
2df3850c 22202
c906108c 22203 default:
b98664d3 22204 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22205 dwarf_form_name (attr->form));
98bfdba5 22206 *value = 0;
c906108c
SS
22207 break;
22208 }
22209}
22210
2df3850c 22211
98bfdba5
PA
22212/* Copy constant value from an attribute to a symbol. */
22213
2df3850c 22214static void
ff39bb5e 22215dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22216 struct dwarf2_cu *cu)
2df3850c 22217{
518817b3 22218 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22219 LONGEST value;
d521ce57 22220 const gdb_byte *bytes;
98bfdba5 22221 struct dwarf2_locexpr_baton *baton;
2df3850c 22222
98bfdba5 22223 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22224 sym->print_name (),
98bfdba5
PA
22225 &objfile->objfile_obstack, cu,
22226 &value, &bytes, &baton);
2df3850c 22227
98bfdba5
PA
22228 if (baton != NULL)
22229 {
98bfdba5 22230 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22231 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22232 }
22233 else if (bytes != NULL)
22234 {
22235 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22236 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22237 }
22238 else
22239 {
22240 SYMBOL_VALUE (sym) = value;
f1e6e072 22241 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22242 }
2df3850c
JM
22243}
22244
c906108c
SS
22245/* Return the type of the die in question using its DW_AT_type attribute. */
22246
22247static struct type *
e7c27a73 22248die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22249{
c906108c 22250 struct attribute *type_attr;
c906108c 22251
e142c38c 22252 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22253 if (!type_attr)
22254 {
518817b3 22255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22256 /* A missing DW_AT_type represents a void type. */
518817b3 22257 return objfile_type (objfile)->builtin_void;
c906108c 22258 }
348e048f 22259
673bfd45 22260 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22261}
22262
b4ba55a1
JB
22263/* True iff CU's producer generates GNAT Ada auxiliary information
22264 that allows to find parallel types through that information instead
22265 of having to do expensive parallel lookups by type name. */
22266
22267static int
22268need_gnat_info (struct dwarf2_cu *cu)
22269{
de4cb04a
JB
22270 /* Assume that the Ada compiler was GNAT, which always produces
22271 the auxiliary information. */
22272 return (cu->language == language_ada);
b4ba55a1
JB
22273}
22274
b4ba55a1
JB
22275/* Return the auxiliary type of the die in question using its
22276 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22277 attribute is not present. */
22278
22279static struct type *
22280die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22281{
b4ba55a1 22282 struct attribute *type_attr;
b4ba55a1
JB
22283
22284 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22285 if (!type_attr)
22286 return NULL;
22287
673bfd45 22288 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22289}
22290
22291/* If DIE has a descriptive_type attribute, then set the TYPE's
22292 descriptive type accordingly. */
22293
22294static void
22295set_descriptive_type (struct type *type, struct die_info *die,
22296 struct dwarf2_cu *cu)
22297{
22298 struct type *descriptive_type = die_descriptive_type (die, cu);
22299
22300 if (descriptive_type)
22301 {
22302 ALLOCATE_GNAT_AUX_TYPE (type);
22303 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22304 }
22305}
22306
c906108c
SS
22307/* Return the containing type of the die in question using its
22308 DW_AT_containing_type attribute. */
22309
22310static struct type *
e7c27a73 22311die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22312{
c906108c 22313 struct attribute *type_attr;
518817b3 22314 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22315
e142c38c 22316 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22317 if (!type_attr)
22318 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22319 "[in module %s]"), objfile_name (objfile));
33ac96f0 22320
673bfd45 22321 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22322}
22323
ac9ec31b
DE
22324/* Return an error marker type to use for the ill formed type in DIE/CU. */
22325
22326static struct type *
22327build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22328{
518817b3
SM
22329 struct dwarf2_per_objfile *dwarf2_per_objfile
22330 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22331 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22332 char *saved;
ac9ec31b 22333
528e1572
SM
22334 std::string message
22335 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22336 objfile_name (objfile),
22337 sect_offset_str (cu->header.sect_off),
22338 sect_offset_str (die->sect_off));
efba19b0 22339 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22340
19f392bc 22341 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22342}
22343
673bfd45 22344/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22345 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22346 DW_AT_containing_type.
673bfd45
DE
22347 If there is no type substitute an error marker. */
22348
c906108c 22349static struct type *
ff39bb5e 22350lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22351 struct dwarf2_cu *cu)
c906108c 22352{
518817b3
SM
22353 struct dwarf2_per_objfile *dwarf2_per_objfile
22354 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22355 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22356 struct type *this_type;
22357
ac9ec31b
DE
22358 gdb_assert (attr->name == DW_AT_type
22359 || attr->name == DW_AT_GNAT_descriptive_type
22360 || attr->name == DW_AT_containing_type);
22361
673bfd45
DE
22362 /* First see if we have it cached. */
22363
36586728
TT
22364 if (attr->form == DW_FORM_GNU_ref_alt)
22365 {
22366 struct dwarf2_per_cu_data *per_cu;
9c541725 22367 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22368
ed2dc618
SM
22369 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22370 dwarf2_per_objfile);
9c541725 22371 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22372 }
7771576e 22373 else if (attr_form_is_ref (attr))
673bfd45 22374 {
9c541725 22375 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22376
9c541725 22377 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22378 }
55f1336d 22379 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22380 {
ac9ec31b 22381 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22382
ac9ec31b 22383 return get_signatured_type (die, signature, cu);
673bfd45
DE
22384 }
22385 else
22386 {
b98664d3 22387 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22388 " at %s [in module %s]"),
22389 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22390 objfile_name (objfile));
ac9ec31b 22391 return build_error_marker_type (cu, die);
673bfd45
DE
22392 }
22393
22394 /* If not cached we need to read it in. */
22395
22396 if (this_type == NULL)
22397 {
ac9ec31b 22398 struct die_info *type_die = NULL;
673bfd45
DE
22399 struct dwarf2_cu *type_cu = cu;
22400
7771576e 22401 if (attr_form_is_ref (attr))
ac9ec31b
DE
22402 type_die = follow_die_ref (die, attr, &type_cu);
22403 if (type_die == NULL)
22404 return build_error_marker_type (cu, die);
22405 /* If we find the type now, it's probably because the type came
3019eac3
DE
22406 from an inter-CU reference and the type's CU got expanded before
22407 ours. */
ac9ec31b 22408 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22409 }
22410
22411 /* If we still don't have a type use an error marker. */
22412
22413 if (this_type == NULL)
ac9ec31b 22414 return build_error_marker_type (cu, die);
673bfd45 22415
f792889a 22416 return this_type;
c906108c
SS
22417}
22418
673bfd45
DE
22419/* Return the type in DIE, CU.
22420 Returns NULL for invalid types.
22421
02142a6c 22422 This first does a lookup in die_type_hash,
673bfd45
DE
22423 and only reads the die in if necessary.
22424
22425 NOTE: This can be called when reading in partial or full symbols. */
22426
f792889a 22427static struct type *
e7c27a73 22428read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22429{
f792889a
DJ
22430 struct type *this_type;
22431
22432 this_type = get_die_type (die, cu);
22433 if (this_type)
22434 return this_type;
22435
673bfd45
DE
22436 return read_type_die_1 (die, cu);
22437}
22438
22439/* Read the type in DIE, CU.
22440 Returns NULL for invalid types. */
22441
22442static struct type *
22443read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22444{
22445 struct type *this_type = NULL;
22446
c906108c
SS
22447 switch (die->tag)
22448 {
22449 case DW_TAG_class_type:
680b30c7 22450 case DW_TAG_interface_type:
c906108c
SS
22451 case DW_TAG_structure_type:
22452 case DW_TAG_union_type:
f792889a 22453 this_type = read_structure_type (die, cu);
c906108c
SS
22454 break;
22455 case DW_TAG_enumeration_type:
f792889a 22456 this_type = read_enumeration_type (die, cu);
c906108c
SS
22457 break;
22458 case DW_TAG_subprogram:
22459 case DW_TAG_subroutine_type:
edb3359d 22460 case DW_TAG_inlined_subroutine:
f792889a 22461 this_type = read_subroutine_type (die, cu);
c906108c
SS
22462 break;
22463 case DW_TAG_array_type:
f792889a 22464 this_type = read_array_type (die, cu);
c906108c 22465 break;
72019c9c 22466 case DW_TAG_set_type:
f792889a 22467 this_type = read_set_type (die, cu);
72019c9c 22468 break;
c906108c 22469 case DW_TAG_pointer_type:
f792889a 22470 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22471 break;
22472 case DW_TAG_ptr_to_member_type:
f792889a 22473 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22474 break;
22475 case DW_TAG_reference_type:
4297a3f0
AV
22476 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22477 break;
22478 case DW_TAG_rvalue_reference_type:
22479 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22480 break;
22481 case DW_TAG_const_type:
f792889a 22482 this_type = read_tag_const_type (die, cu);
c906108c
SS
22483 break;
22484 case DW_TAG_volatile_type:
f792889a 22485 this_type = read_tag_volatile_type (die, cu);
c906108c 22486 break;
06d66ee9
TT
22487 case DW_TAG_restrict_type:
22488 this_type = read_tag_restrict_type (die, cu);
22489 break;
c906108c 22490 case DW_TAG_string_type:
f792889a 22491 this_type = read_tag_string_type (die, cu);
c906108c
SS
22492 break;
22493 case DW_TAG_typedef:
f792889a 22494 this_type = read_typedef (die, cu);
c906108c 22495 break;
a02abb62 22496 case DW_TAG_subrange_type:
f792889a 22497 this_type = read_subrange_type (die, cu);
a02abb62 22498 break;
c906108c 22499 case DW_TAG_base_type:
f792889a 22500 this_type = read_base_type (die, cu);
c906108c 22501 break;
81a17f79 22502 case DW_TAG_unspecified_type:
f792889a 22503 this_type = read_unspecified_type (die, cu);
81a17f79 22504 break;
0114d602
DJ
22505 case DW_TAG_namespace:
22506 this_type = read_namespace_type (die, cu);
22507 break;
f55ee35c
JK
22508 case DW_TAG_module:
22509 this_type = read_module_type (die, cu);
22510 break;
a2c2acaf
MW
22511 case DW_TAG_atomic_type:
22512 this_type = read_tag_atomic_type (die, cu);
22513 break;
c906108c 22514 default:
b98664d3 22515 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22516 dwarf_tag_name (die->tag));
c906108c
SS
22517 break;
22518 }
63d06c5c 22519
f792889a 22520 return this_type;
63d06c5c
DC
22521}
22522
abc72ce4
DE
22523/* See if we can figure out if the class lives in a namespace. We do
22524 this by looking for a member function; its demangled name will
22525 contain namespace info, if there is any.
22526 Return the computed name or NULL.
22527 Space for the result is allocated on the objfile's obstack.
22528 This is the full-die version of guess_partial_die_structure_name.
22529 In this case we know DIE has no useful parent. */
22530
43816ebc 22531static const char *
abc72ce4
DE
22532guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22533{
22534 struct die_info *spec_die;
22535 struct dwarf2_cu *spec_cu;
22536 struct die_info *child;
518817b3 22537 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22538
22539 spec_cu = cu;
22540 spec_die = die_specification (die, &spec_cu);
22541 if (spec_die != NULL)
22542 {
22543 die = spec_die;
22544 cu = spec_cu;
22545 }
22546
22547 for (child = die->child;
22548 child != NULL;
22549 child = child->sibling)
22550 {
22551 if (child->tag == DW_TAG_subprogram)
22552 {
73b9be8b 22553 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22554
7d45c7c3 22555 if (linkage_name != NULL)
abc72ce4 22556 {
43816ebc
TT
22557 gdb::unique_xmalloc_ptr<char> actual_name
22558 (language_class_name_from_physname (cu->language_defn,
22559 linkage_name));
22560 const char *name = NULL;
abc72ce4
DE
22561
22562 if (actual_name != NULL)
22563 {
15d034d0 22564 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22565
22566 if (die_name != NULL
43816ebc 22567 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22568 {
22569 /* Strip off the class name from the full name.
22570 We want the prefix. */
22571 int die_name_len = strlen (die_name);
43816ebc
TT
22572 int actual_name_len = strlen (actual_name.get ());
22573 const char *ptr = actual_name.get ();
abc72ce4
DE
22574
22575 /* Test for '::' as a sanity check. */
22576 if (actual_name_len > die_name_len + 2
43816ebc 22577 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22578 name = obstack_strndup (
e3b94546 22579 &objfile->per_bfd->storage_obstack,
43816ebc 22580 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22581 }
22582 }
abc72ce4
DE
22583 return name;
22584 }
22585 }
22586 }
22587
22588 return NULL;
22589}
22590
96408a79
SA
22591/* GCC might emit a nameless typedef that has a linkage name. Determine the
22592 prefix part in such case. See
22593 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22594
a121b7c1 22595static const char *
96408a79
SA
22596anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22597{
22598 struct attribute *attr;
e6a959d6 22599 const char *base;
96408a79
SA
22600
22601 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22602 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22603 return NULL;
22604
7d45c7c3 22605 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22606 return NULL;
22607
73b9be8b 22608 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22609 if (attr == NULL || DW_STRING (attr) == NULL)
22610 return NULL;
22611
22612 /* dwarf2_name had to be already called. */
22613 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22614
22615 /* Strip the base name, keep any leading namespaces/classes. */
22616 base = strrchr (DW_STRING (attr), ':');
22617 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22618 return "";
22619
518817b3 22620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22621 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22622 DW_STRING (attr),
22623 &base[-1] - DW_STRING (attr));
96408a79
SA
22624}
22625
fdde2d81 22626/* Return the name of the namespace/class that DIE is defined within,
0114d602 22627 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22628
0114d602
DJ
22629 For example, if we're within the method foo() in the following
22630 code:
22631
22632 namespace N {
22633 class C {
22634 void foo () {
22635 }
22636 };
22637 }
22638
22639 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22640
0d5cff50 22641static const char *
e142c38c 22642determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22643{
518817b3
SM
22644 struct dwarf2_per_objfile *dwarf2_per_objfile
22645 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22646 struct die_info *parent, *spec_die;
22647 struct dwarf2_cu *spec_cu;
22648 struct type *parent_type;
a121b7c1 22649 const char *retval;
63d06c5c 22650
9c37b5ae 22651 if (cu->language != language_cplus
c44af4eb
TT
22652 && cu->language != language_fortran && cu->language != language_d
22653 && cu->language != language_rust)
0114d602
DJ
22654 return "";
22655
96408a79
SA
22656 retval = anonymous_struct_prefix (die, cu);
22657 if (retval)
22658 return retval;
22659
0114d602
DJ
22660 /* We have to be careful in the presence of DW_AT_specification.
22661 For example, with GCC 3.4, given the code
22662
22663 namespace N {
22664 void foo() {
22665 // Definition of N::foo.
22666 }
22667 }
22668
22669 then we'll have a tree of DIEs like this:
22670
22671 1: DW_TAG_compile_unit
22672 2: DW_TAG_namespace // N
22673 3: DW_TAG_subprogram // declaration of N::foo
22674 4: DW_TAG_subprogram // definition of N::foo
22675 DW_AT_specification // refers to die #3
22676
22677 Thus, when processing die #4, we have to pretend that we're in
22678 the context of its DW_AT_specification, namely the contex of die
22679 #3. */
22680 spec_cu = cu;
22681 spec_die = die_specification (die, &spec_cu);
22682 if (spec_die == NULL)
22683 parent = die->parent;
22684 else
63d06c5c 22685 {
0114d602
DJ
22686 parent = spec_die->parent;
22687 cu = spec_cu;
63d06c5c 22688 }
0114d602
DJ
22689
22690 if (parent == NULL)
22691 return "";
98bfdba5
PA
22692 else if (parent->building_fullname)
22693 {
22694 const char *name;
22695 const char *parent_name;
22696
22697 /* It has been seen on RealView 2.2 built binaries,
22698 DW_TAG_template_type_param types actually _defined_ as
22699 children of the parent class:
22700
22701 enum E {};
22702 template class <class Enum> Class{};
22703 Class<enum E> class_e;
22704
22705 1: DW_TAG_class_type (Class)
22706 2: DW_TAG_enumeration_type (E)
22707 3: DW_TAG_enumerator (enum1:0)
22708 3: DW_TAG_enumerator (enum2:1)
22709 ...
22710 2: DW_TAG_template_type_param
22711 DW_AT_type DW_FORM_ref_udata (E)
22712
22713 Besides being broken debug info, it can put GDB into an
22714 infinite loop. Consider:
22715
22716 When we're building the full name for Class<E>, we'll start
22717 at Class, and go look over its template type parameters,
22718 finding E. We'll then try to build the full name of E, and
22719 reach here. We're now trying to build the full name of E,
22720 and look over the parent DIE for containing scope. In the
22721 broken case, if we followed the parent DIE of E, we'd again
22722 find Class, and once again go look at its template type
22723 arguments, etc., etc. Simply don't consider such parent die
22724 as source-level parent of this die (it can't be, the language
22725 doesn't allow it), and break the loop here. */
22726 name = dwarf2_name (die, cu);
22727 parent_name = dwarf2_name (parent, cu);
b98664d3 22728 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22729 name ? name : "<unknown>",
22730 parent_name ? parent_name : "<unknown>");
22731 return "";
22732 }
63d06c5c 22733 else
0114d602
DJ
22734 switch (parent->tag)
22735 {
63d06c5c 22736 case DW_TAG_namespace:
0114d602 22737 parent_type = read_type_die (parent, cu);
acebe513
UW
22738 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22739 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22740 Work around this problem here. */
22741 if (cu->language == language_cplus
e86ca25f 22742 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22743 return "";
0114d602 22744 /* We give a name to even anonymous namespaces. */
e86ca25f 22745 return TYPE_NAME (parent_type);
63d06c5c 22746 case DW_TAG_class_type:
680b30c7 22747 case DW_TAG_interface_type:
63d06c5c 22748 case DW_TAG_structure_type:
0114d602 22749 case DW_TAG_union_type:
f55ee35c 22750 case DW_TAG_module:
0114d602 22751 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22752 if (TYPE_NAME (parent_type) != NULL)
22753 return TYPE_NAME (parent_type);
0114d602
DJ
22754 else
22755 /* An anonymous structure is only allowed non-static data
22756 members; no typedefs, no member functions, et cetera.
22757 So it does not need a prefix. */
22758 return "";
abc72ce4 22759 case DW_TAG_compile_unit:
95554aad 22760 case DW_TAG_partial_unit:
abc72ce4
DE
22761 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22762 if (cu->language == language_cplus
fd5866f6 22763 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22764 && die->child != NULL
22765 && (die->tag == DW_TAG_class_type
22766 || die->tag == DW_TAG_structure_type
22767 || die->tag == DW_TAG_union_type))
22768 {
43816ebc 22769 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22770 if (name != NULL)
22771 return name;
22772 }
22773 return "";
0a4b0913
AB
22774 case DW_TAG_subprogram:
22775 /* Nested subroutines in Fortran get a prefix with the name
22776 of the parent's subroutine. */
22777 if (cu->language == language_fortran)
22778 {
22779 if ((die->tag == DW_TAG_subprogram)
22780 && (dwarf2_name (parent, cu) != NULL))
22781 return dwarf2_name (parent, cu);
22782 }
22783 return determine_prefix (parent, cu);
3d567982
TT
22784 case DW_TAG_enumeration_type:
22785 parent_type = read_type_die (parent, cu);
22786 if (TYPE_DECLARED_CLASS (parent_type))
22787 {
e86ca25f
TT
22788 if (TYPE_NAME (parent_type) != NULL)
22789 return TYPE_NAME (parent_type);
3d567982
TT
22790 return "";
22791 }
22792 /* Fall through. */
63d06c5c 22793 default:
8176b9b8 22794 return determine_prefix (parent, cu);
63d06c5c 22795 }
63d06c5c
DC
22796}
22797
3e43a32a
MS
22798/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22799 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22800 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22801 an obconcat, otherwise allocate storage for the result. The CU argument is
22802 used to determine the language and hence, the appropriate separator. */
987504bb 22803
f55ee35c 22804#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22805
22806static char *
f55ee35c
JK
22807typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22808 int physname, struct dwarf2_cu *cu)
63d06c5c 22809{
f55ee35c 22810 const char *lead = "";
5c315b68 22811 const char *sep;
63d06c5c 22812
3e43a32a
MS
22813 if (suffix == NULL || suffix[0] == '\0'
22814 || prefix == NULL || prefix[0] == '\0')
987504bb 22815 sep = "";
45280282
IB
22816 else if (cu->language == language_d)
22817 {
22818 /* For D, the 'main' function could be defined in any module, but it
22819 should never be prefixed. */
22820 if (strcmp (suffix, "D main") == 0)
22821 {
22822 prefix = "";
22823 sep = "";
22824 }
22825 else
22826 sep = ".";
22827 }
f55ee35c
JK
22828 else if (cu->language == language_fortran && physname)
22829 {
22830 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22831 DW_AT_MIPS_linkage_name is preferred and used instead. */
22832
22833 lead = "__";
22834 sep = "_MOD_";
22835 }
987504bb
JJ
22836 else
22837 sep = "::";
63d06c5c 22838
6dd47d34
DE
22839 if (prefix == NULL)
22840 prefix = "";
22841 if (suffix == NULL)
22842 suffix = "";
22843
987504bb
JJ
22844 if (obs == NULL)
22845 {
3e43a32a 22846 char *retval
224c3ddb
SM
22847 = ((char *)
22848 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22849
f55ee35c
JK
22850 strcpy (retval, lead);
22851 strcat (retval, prefix);
6dd47d34
DE
22852 strcat (retval, sep);
22853 strcat (retval, suffix);
63d06c5c
DC
22854 return retval;
22855 }
987504bb
JJ
22856 else
22857 {
22858 /* We have an obstack. */
f55ee35c 22859 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22860 }
63d06c5c
DC
22861}
22862
c906108c
SS
22863/* Return sibling of die, NULL if no sibling. */
22864
f9aca02d 22865static struct die_info *
fba45db2 22866sibling_die (struct die_info *die)
c906108c 22867{
639d11d3 22868 return die->sibling;
c906108c
SS
22869}
22870
71c25dea
TT
22871/* Get name of a die, return NULL if not found. */
22872
15d034d0
TT
22873static const char *
22874dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22875 struct obstack *obstack)
22876{
22877 if (name && cu->language == language_cplus)
22878 {
2f408ecb 22879 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22880
2f408ecb 22881 if (!canon_name.empty ())
71c25dea 22882 {
2f408ecb 22883 if (canon_name != name)
efba19b0 22884 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22885 }
22886 }
22887
22888 return name;
c906108c
SS
22889}
22890
96553a0c
DE
22891/* Get name of a die, return NULL if not found.
22892 Anonymous namespaces are converted to their magic string. */
9219021c 22893
15d034d0 22894static const char *
e142c38c 22895dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22896{
22897 struct attribute *attr;
518817b3 22898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22899
e142c38c 22900 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22901 if ((!attr || !DW_STRING (attr))
96553a0c 22902 && die->tag != DW_TAG_namespace
53832f31
TT
22903 && die->tag != DW_TAG_class_type
22904 && die->tag != DW_TAG_interface_type
22905 && die->tag != DW_TAG_structure_type
22906 && die->tag != DW_TAG_union_type)
71c25dea
TT
22907 return NULL;
22908
22909 switch (die->tag)
22910 {
22911 case DW_TAG_compile_unit:
95554aad 22912 case DW_TAG_partial_unit:
71c25dea
TT
22913 /* Compilation units have a DW_AT_name that is a filename, not
22914 a source language identifier. */
22915 case DW_TAG_enumeration_type:
22916 case DW_TAG_enumerator:
22917 /* These tags always have simple identifiers already; no need
22918 to canonicalize them. */
22919 return DW_STRING (attr);
907af001 22920
96553a0c
DE
22921 case DW_TAG_namespace:
22922 if (attr != NULL && DW_STRING (attr) != NULL)
22923 return DW_STRING (attr);
22924 return CP_ANONYMOUS_NAMESPACE_STR;
22925
907af001
UW
22926 case DW_TAG_class_type:
22927 case DW_TAG_interface_type:
22928 case DW_TAG_structure_type:
22929 case DW_TAG_union_type:
22930 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22931 structures or unions. These were of the form "._%d" in GCC 4.1,
22932 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22933 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22934 if (attr && DW_STRING (attr)
61012eef
GB
22935 && (startswith (DW_STRING (attr), "._")
22936 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22937 return NULL;
53832f31
TT
22938
22939 /* GCC might emit a nameless typedef that has a linkage name. See
22940 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22941 if (!attr || DW_STRING (attr) == NULL)
22942 {
73b9be8b 22943 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22944 if (attr == NULL || DW_STRING (attr) == NULL)
22945 return NULL;
22946
df5c6c50
JK
22947 /* Avoid demangling DW_STRING (attr) the second time on a second
22948 call for the same DIE. */
22949 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22950 {
43816ebc
TT
22951 gdb::unique_xmalloc_ptr<char> demangled
22952 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22953
e6a959d6 22954 const char *base;
96408a79 22955
53832f31 22956 /* FIXME: we already did this for the partial symbol... */
34a68019 22957 DW_STRING (attr)
021887d8 22958 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 22959 demangled.get ());
53832f31 22960 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
22961
22962 /* Strip any leading namespaces/classes, keep only the base name.
22963 DW_AT_name for named DIEs does not contain the prefixes. */
22964 base = strrchr (DW_STRING (attr), ':');
22965 if (base && base > DW_STRING (attr) && base[-1] == ':')
22966 return &base[1];
22967 else
22968 return DW_STRING (attr);
53832f31
TT
22969 }
22970 }
907af001
UW
22971 break;
22972
71c25dea 22973 default:
907af001
UW
22974 break;
22975 }
22976
22977 if (!DW_STRING_IS_CANONICAL (attr))
22978 {
22979 DW_STRING (attr)
22980 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22981 &objfile->per_bfd->storage_obstack);
907af001 22982 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22983 }
907af001 22984 return DW_STRING (attr);
9219021c
DC
22985}
22986
22987/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22988 is none. *EXT_CU is the CU containing DIE on input, and the CU
22989 containing the return value on output. */
9219021c
DC
22990
22991static struct die_info *
f2f0e013 22992dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22993{
22994 struct attribute *attr;
9219021c 22995
f2f0e013 22996 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22997 if (attr == NULL)
22998 return NULL;
22999
f2f0e013 23000 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23001}
23002
fa9c3fa0
TT
23003/* A convenience function that returns an "unknown" DWARF name,
23004 including the value of V. STR is the name of the entity being
23005 printed, e.g., "TAG". */
23006
23007static const char *
23008dwarf_unknown (const char *str, unsigned v)
23009{
23010 char *cell = get_print_cell ();
23011 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23012 return cell;
23013}
23014
c906108c
SS
23015/* Convert a DIE tag into its string name. */
23016
f39c6ffd 23017static const char *
aa1ee363 23018dwarf_tag_name (unsigned tag)
c906108c 23019{
f39c6ffd
TT
23020 const char *name = get_DW_TAG_name (tag);
23021
23022 if (name == NULL)
fa9c3fa0 23023 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23024
23025 return name;
c906108c
SS
23026}
23027
23028/* Convert a DWARF attribute code into its string name. */
23029
f39c6ffd 23030static const char *
aa1ee363 23031dwarf_attr_name (unsigned attr)
c906108c 23032{
f39c6ffd
TT
23033 const char *name;
23034
c764a876 23035#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23036 if (attr == DW_AT_MIPS_fde)
23037 return "DW_AT_MIPS_fde";
23038#else
23039 if (attr == DW_AT_HP_block_index)
23040 return "DW_AT_HP_block_index";
c764a876 23041#endif
f39c6ffd
TT
23042
23043 name = get_DW_AT_name (attr);
23044
23045 if (name == NULL)
fa9c3fa0 23046 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23047
23048 return name;
c906108c
SS
23049}
23050
a084a2a6
AT
23051/* Convert a unit type to corresponding DW_UT name. */
23052
23053static const char *
23054dwarf_unit_type_name (int unit_type) {
23055 switch (unit_type)
23056 {
23057 case 0x01:
23058 return "DW_UT_compile (0x01)";
23059 case 0x02:
23060 return "DW_UT_type (0x02)";
23061 case 0x03:
23062 return "DW_UT_partial (0x03)";
23063 case 0x04:
23064 return "DW_UT_skeleton (0x04)";
23065 case 0x05:
23066 return "DW_UT_split_compile (0x05)";
23067 case 0x06:
23068 return "DW_UT_split_type (0x06)";
23069 case 0x80:
23070 return "DW_UT_lo_user (0x80)";
23071 case 0xff:
23072 return "DW_UT_hi_user (0xff)";
23073 default:
23074 return nullptr;
23075 }
23076}
23077
c906108c
SS
23078/* Convert a DWARF value form code into its string name. */
23079
f39c6ffd 23080static const char *
aa1ee363 23081dwarf_form_name (unsigned form)
c906108c 23082{
f39c6ffd
TT
23083 const char *name = get_DW_FORM_name (form);
23084
23085 if (name == NULL)
fa9c3fa0 23086 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23087
23088 return name;
c906108c
SS
23089}
23090
a121b7c1 23091static const char *
fba45db2 23092dwarf_bool_name (unsigned mybool)
c906108c
SS
23093{
23094 if (mybool)
23095 return "TRUE";
23096 else
23097 return "FALSE";
23098}
23099
23100/* Convert a DWARF type code into its string name. */
23101
f39c6ffd 23102static const char *
aa1ee363 23103dwarf_type_encoding_name (unsigned enc)
c906108c 23104{
f39c6ffd 23105 const char *name = get_DW_ATE_name (enc);
c906108c 23106
f39c6ffd 23107 if (name == NULL)
fa9c3fa0 23108 return dwarf_unknown ("ATE", enc);
c906108c 23109
f39c6ffd 23110 return name;
c906108c 23111}
c906108c 23112
f9aca02d 23113static void
d97bc12b 23114dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23115{
23116 unsigned int i;
23117
d97bc12b 23118 print_spaces (indent, f);
9d8780f0 23119 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23120 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23121 sect_offset_str (die->sect_off));
d97bc12b
DE
23122
23123 if (die->parent != NULL)
23124 {
23125 print_spaces (indent, f);
9d8780f0
SM
23126 fprintf_unfiltered (f, " parent at offset: %s\n",
23127 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23128 }
23129
23130 print_spaces (indent, f);
23131 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23132 dwarf_bool_name (die->child != NULL));
c906108c 23133
d97bc12b
DE
23134 print_spaces (indent, f);
23135 fprintf_unfiltered (f, " attributes:\n");
23136
c906108c
SS
23137 for (i = 0; i < die->num_attrs; ++i)
23138 {
d97bc12b
DE
23139 print_spaces (indent, f);
23140 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23141 dwarf_attr_name (die->attrs[i].name),
23142 dwarf_form_name (die->attrs[i].form));
d97bc12b 23143
c906108c
SS
23144 switch (die->attrs[i].form)
23145 {
c906108c 23146 case DW_FORM_addr:
336d760d 23147 case DW_FORM_addrx:
3019eac3 23148 case DW_FORM_GNU_addr_index:
d97bc12b 23149 fprintf_unfiltered (f, "address: ");
5af949e3 23150 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23151 break;
23152 case DW_FORM_block2:
23153 case DW_FORM_block4:
23154 case DW_FORM_block:
23155 case DW_FORM_block1:
56eb65bd
SP
23156 fprintf_unfiltered (f, "block: size %s",
23157 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23158 break;
2dc7f7b3 23159 case DW_FORM_exprloc:
56eb65bd
SP
23160 fprintf_unfiltered (f, "expression: size %s",
23161 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23162 break;
0224619f
JK
23163 case DW_FORM_data16:
23164 fprintf_unfiltered (f, "constant of 16 bytes");
23165 break;
4568ecf9
DE
23166 case DW_FORM_ref_addr:
23167 fprintf_unfiltered (f, "ref address: ");
23168 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23169 break;
36586728
TT
23170 case DW_FORM_GNU_ref_alt:
23171 fprintf_unfiltered (f, "alt ref address: ");
23172 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23173 break;
10b3939b
DJ
23174 case DW_FORM_ref1:
23175 case DW_FORM_ref2:
23176 case DW_FORM_ref4:
4568ecf9
DE
23177 case DW_FORM_ref8:
23178 case DW_FORM_ref_udata:
d97bc12b 23179 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23180 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23181 break;
c906108c
SS
23182 case DW_FORM_data1:
23183 case DW_FORM_data2:
23184 case DW_FORM_data4:
ce5d95e1 23185 case DW_FORM_data8:
c906108c
SS
23186 case DW_FORM_udata:
23187 case DW_FORM_sdata:
43bbcdc2
PH
23188 fprintf_unfiltered (f, "constant: %s",
23189 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23190 break;
2dc7f7b3
TT
23191 case DW_FORM_sec_offset:
23192 fprintf_unfiltered (f, "section offset: %s",
23193 pulongest (DW_UNSND (&die->attrs[i])));
23194 break;
55f1336d 23195 case DW_FORM_ref_sig8:
ac9ec31b
DE
23196 fprintf_unfiltered (f, "signature: %s",
23197 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23198 break;
c906108c 23199 case DW_FORM_string:
4bdf3d34 23200 case DW_FORM_strp:
43988095 23201 case DW_FORM_line_strp:
cf532bd1 23202 case DW_FORM_strx:
3019eac3 23203 case DW_FORM_GNU_str_index:
36586728 23204 case DW_FORM_GNU_strp_alt:
8285870a 23205 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23206 DW_STRING (&die->attrs[i])
8285870a
JK
23207 ? DW_STRING (&die->attrs[i]) : "",
23208 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23209 break;
23210 case DW_FORM_flag:
23211 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23212 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23213 else
d97bc12b 23214 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23215 break;
2dc7f7b3
TT
23216 case DW_FORM_flag_present:
23217 fprintf_unfiltered (f, "flag: TRUE");
23218 break;
a8329558 23219 case DW_FORM_indirect:
0963b4bd
MS
23220 /* The reader will have reduced the indirect form to
23221 the "base form" so this form should not occur. */
5f48f8f3 23222 fprintf_unfiltered (f,
3e43a32a 23223 "unexpected attribute form: DW_FORM_indirect");
a8329558 23224 break;
663c44ac
JK
23225 case DW_FORM_implicit_const:
23226 fprintf_unfiltered (f, "constant: %s",
23227 plongest (DW_SND (&die->attrs[i])));
23228 break;
c906108c 23229 default:
d97bc12b 23230 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23231 die->attrs[i].form);
d97bc12b 23232 break;
c906108c 23233 }
d97bc12b 23234 fprintf_unfiltered (f, "\n");
c906108c
SS
23235 }
23236}
23237
f9aca02d 23238static void
d97bc12b 23239dump_die_for_error (struct die_info *die)
c906108c 23240{
d97bc12b
DE
23241 dump_die_shallow (gdb_stderr, 0, die);
23242}
23243
23244static void
23245dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23246{
23247 int indent = level * 4;
23248
23249 gdb_assert (die != NULL);
23250
23251 if (level >= max_level)
23252 return;
23253
23254 dump_die_shallow (f, indent, die);
23255
23256 if (die->child != NULL)
c906108c 23257 {
d97bc12b
DE
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " Children:");
23260 if (level + 1 < max_level)
23261 {
23262 fprintf_unfiltered (f, "\n");
23263 dump_die_1 (f, level + 1, max_level, die->child);
23264 }
23265 else
23266 {
3e43a32a
MS
23267 fprintf_unfiltered (f,
23268 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23269 }
23270 }
23271
23272 if (die->sibling != NULL && level > 0)
23273 {
23274 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23275 }
23276}
23277
d97bc12b
DE
23278/* This is called from the pdie macro in gdbinit.in.
23279 It's not static so gcc will keep a copy callable from gdb. */
23280
23281void
23282dump_die (struct die_info *die, int max_level)
23283{
23284 dump_die_1 (gdb_stdlog, 0, max_level, die);
23285}
23286
f9aca02d 23287static void
51545339 23288store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23289{
51545339 23290 void **slot;
c906108c 23291
9c541725
PA
23292 slot = htab_find_slot_with_hash (cu->die_hash, die,
23293 to_underlying (die->sect_off),
b64f50a1 23294 INSERT);
51545339
DJ
23295
23296 *slot = die;
c906108c
SS
23297}
23298
b64f50a1
JK
23299/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23300 required kind. */
23301
23302static sect_offset
ff39bb5e 23303dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23304{
7771576e 23305 if (attr_form_is_ref (attr))
9c541725 23306 return (sect_offset) DW_UNSND (attr);
93311388 23307
b98664d3 23308 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23309 dwarf_form_name (attr->form));
9c541725 23310 return {};
c906108c
SS
23311}
23312
43bbcdc2
PH
23313/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23314 * the value held by the attribute is not constant. */
a02abb62 23315
43bbcdc2 23316static LONGEST
ff39bb5e 23317dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23318{
663c44ac 23319 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23320 return DW_SND (attr);
23321 else if (attr->form == DW_FORM_udata
23322 || attr->form == DW_FORM_data1
23323 || attr->form == DW_FORM_data2
23324 || attr->form == DW_FORM_data4
23325 || attr->form == DW_FORM_data8)
23326 return DW_UNSND (attr);
23327 else
23328 {
0224619f 23329 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23330 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23331 dwarf_form_name (attr->form));
23332 return default_value;
23333 }
23334}
23335
348e048f
DE
23336/* Follow reference or signature attribute ATTR of SRC_DIE.
23337 On entry *REF_CU is the CU of SRC_DIE.
23338 On exit *REF_CU is the CU of the result. */
23339
23340static struct die_info *
ff39bb5e 23341follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23342 struct dwarf2_cu **ref_cu)
23343{
23344 struct die_info *die;
23345
7771576e 23346 if (attr_form_is_ref (attr))
348e048f 23347 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23348 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23349 die = follow_die_sig (src_die, attr, ref_cu);
23350 else
23351 {
23352 dump_die_for_error (src_die);
23353 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23354 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23355 }
23356
23357 return die;
03dd20cc
DJ
23358}
23359
5c631832 23360/* Follow reference OFFSET.
673bfd45
DE
23361 On entry *REF_CU is the CU of the source die referencing OFFSET.
23362 On exit *REF_CU is the CU of the result.
23363 Returns NULL if OFFSET is invalid. */
f504f079 23364
f9aca02d 23365static struct die_info *
9c541725 23366follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23367 struct dwarf2_cu **ref_cu)
c906108c 23368{
10b3939b 23369 struct die_info temp_die;
f2f0e013 23370 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23371 struct dwarf2_per_objfile *dwarf2_per_objfile
23372 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23373
348e048f
DE
23374 gdb_assert (cu->per_cu != NULL);
23375
98bfdba5
PA
23376 target_cu = cu;
23377
3019eac3 23378 if (cu->per_cu->is_debug_types)
348e048f
DE
23379 {
23380 /* .debug_types CUs cannot reference anything outside their CU.
23381 If they need to, they have to reference a signatured type via
55f1336d 23382 DW_FORM_ref_sig8. */
9c541725 23383 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23384 return NULL;
348e048f 23385 }
36586728 23386 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23387 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23388 {
23389 struct dwarf2_per_cu_data *per_cu;
9a619af0 23390
9c541725 23391 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23392 dwarf2_per_objfile);
03dd20cc
DJ
23393
23394 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23395 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23396 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23397
10b3939b
DJ
23398 target_cu = per_cu->cu;
23399 }
98bfdba5
PA
23400 else if (cu->dies == NULL)
23401 {
23402 /* We're loading full DIEs during partial symbol reading. */
23403 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23404 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23405 }
c906108c 23406
f2f0e013 23407 *ref_cu = target_cu;
9c541725 23408 temp_die.sect_off = sect_off;
c24bdb02
KS
23409
23410 if (target_cu != cu)
23411 target_cu->ancestor = cu;
23412
9a3c8263 23413 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23414 &temp_die,
23415 to_underlying (sect_off));
5c631832 23416}
10b3939b 23417
5c631832
JK
23418/* Follow reference attribute ATTR of SRC_DIE.
23419 On entry *REF_CU is the CU of SRC_DIE.
23420 On exit *REF_CU is the CU of the result. */
23421
23422static struct die_info *
ff39bb5e 23423follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23424 struct dwarf2_cu **ref_cu)
23425{
9c541725 23426 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23427 struct dwarf2_cu *cu = *ref_cu;
23428 struct die_info *die;
23429
9c541725 23430 die = follow_die_offset (sect_off,
36586728
TT
23431 (attr->form == DW_FORM_GNU_ref_alt
23432 || cu->per_cu->is_dwz),
23433 ref_cu);
5c631832 23434 if (!die)
9d8780f0
SM
23435 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23436 "at %s [in module %s]"),
23437 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23438 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23439
5c631832
JK
23440 return die;
23441}
23442
9c541725 23443/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23444 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23445 dwarf2_locexpr_baton->data has lifetime of
23446 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23447
23448struct dwarf2_locexpr_baton
9c541725 23449dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23450 struct dwarf2_per_cu_data *per_cu,
23451 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23452 void *baton, bool resolve_abstract_p)
5c631832 23453{
918dd910 23454 struct dwarf2_cu *cu;
5c631832
JK
23455 struct die_info *die;
23456 struct attribute *attr;
23457 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23458 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23459 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23460
918dd910 23461 if (per_cu->cu == NULL)
58f0c718 23462 load_cu (per_cu, false);
918dd910 23463 cu = per_cu->cu;
cc12ce38
DE
23464 if (cu == NULL)
23465 {
23466 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23467 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23468 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23469 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23470 }
918dd910 23471
9c541725 23472 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23473 if (!die)
9d8780f0
SM
23474 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23475 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23476
23477 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23478 if (!attr && resolve_abstract_p
3360b6e7 23479 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23480 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23481 {
23482 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 23483 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 23484 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23485
3360b6e7
TV
23486 for (const auto &cand_off
23487 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23488 {
3360b6e7
TV
23489 struct dwarf2_cu *cand_cu = cu;
23490 struct die_info *cand
23491 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23492 if (!cand
23493 || !cand->parent
e4a62c65
TV
23494 || cand->parent->tag != DW_TAG_subprogram)
23495 continue;
23496
23497 CORE_ADDR pc_low, pc_high;
23498 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23499 if (pc_low == ((CORE_ADDR) -1))
23500 continue;
23501 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23502 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23503 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23504 continue;
23505
23506 die = cand;
23507 attr = dwarf2_attr (die, DW_AT_location, cu);
23508 break;
23509 }
23510 }
23511
5c631832
JK
23512 if (!attr)
23513 {
e103e986
JK
23514 /* DWARF: "If there is no such attribute, then there is no effect.".
23515 DATA is ignored if SIZE is 0. */
5c631832 23516
e103e986 23517 retval.data = NULL;
5c631832
JK
23518 retval.size = 0;
23519 }
8cf6f0b1
TT
23520 else if (attr_form_is_section_offset (attr))
23521 {
23522 struct dwarf2_loclist_baton loclist_baton;
23523 CORE_ADDR pc = (*get_frame_pc) (baton);
23524 size_t size;
23525
23526 fill_in_loclist_baton (cu, &loclist_baton, attr);
23527
23528 retval.data = dwarf2_find_location_expression (&loclist_baton,
23529 &size, pc);
23530 retval.size = size;
23531 }
5c631832
JK
23532 else
23533 {
23534 if (!attr_form_is_block (attr))
9d8780f0 23535 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23536 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23537 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23538
23539 retval.data = DW_BLOCK (attr)->data;
23540 retval.size = DW_BLOCK (attr)->size;
23541 }
23542 retval.per_cu = cu->per_cu;
918dd910 23543
ed2dc618 23544 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23545
5c631832 23546 return retval;
348e048f
DE
23547}
23548
8b9737bf
TT
23549/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23550 offset. */
23551
23552struct dwarf2_locexpr_baton
23553dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23554 struct dwarf2_per_cu_data *per_cu,
23555 CORE_ADDR (*get_frame_pc) (void *baton),
23556 void *baton)
23557{
9c541725 23558 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23559
9c541725 23560 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23561}
23562
b6807d98
TT
23563/* Write a constant of a given type as target-ordered bytes into
23564 OBSTACK. */
23565
23566static const gdb_byte *
23567write_constant_as_bytes (struct obstack *obstack,
23568 enum bfd_endian byte_order,
23569 struct type *type,
23570 ULONGEST value,
23571 LONGEST *len)
23572{
23573 gdb_byte *result;
23574
23575 *len = TYPE_LENGTH (type);
224c3ddb 23576 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23577 store_unsigned_integer (result, *len, byte_order, value);
23578
23579 return result;
23580}
23581
23582/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23583 pointer to the constant bytes and set LEN to the length of the
23584 data. If memory is needed, allocate it on OBSTACK. If the DIE
23585 does not have a DW_AT_const_value, return NULL. */
23586
23587const gdb_byte *
9c541725 23588dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23589 struct dwarf2_per_cu_data *per_cu,
23590 struct obstack *obstack,
23591 LONGEST *len)
23592{
23593 struct dwarf2_cu *cu;
23594 struct die_info *die;
23595 struct attribute *attr;
23596 const gdb_byte *result = NULL;
23597 struct type *type;
23598 LONGEST value;
23599 enum bfd_endian byte_order;
e3b94546 23600 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23601
b6807d98 23602 if (per_cu->cu == NULL)
58f0c718 23603 load_cu (per_cu, false);
b6807d98 23604 cu = per_cu->cu;
cc12ce38
DE
23605 if (cu == NULL)
23606 {
23607 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23608 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23609 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23610 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23611 }
b6807d98 23612
9c541725 23613 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23614 if (!die)
9d8780f0
SM
23615 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23616 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23617
23618 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23619 if (attr == NULL)
23620 return NULL;
23621
e3b94546 23622 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23623 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23624
23625 switch (attr->form)
23626 {
23627 case DW_FORM_addr:
336d760d 23628 case DW_FORM_addrx:
b6807d98
TT
23629 case DW_FORM_GNU_addr_index:
23630 {
23631 gdb_byte *tem;
23632
23633 *len = cu->header.addr_size;
224c3ddb 23634 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23635 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23636 result = tem;
23637 }
23638 break;
23639 case DW_FORM_string:
23640 case DW_FORM_strp:
cf532bd1 23641 case DW_FORM_strx:
b6807d98
TT
23642 case DW_FORM_GNU_str_index:
23643 case DW_FORM_GNU_strp_alt:
23644 /* DW_STRING is already allocated on the objfile obstack, point
23645 directly to it. */
23646 result = (const gdb_byte *) DW_STRING (attr);
23647 *len = strlen (DW_STRING (attr));
23648 break;
23649 case DW_FORM_block1:
23650 case DW_FORM_block2:
23651 case DW_FORM_block4:
23652 case DW_FORM_block:
23653 case DW_FORM_exprloc:
0224619f 23654 case DW_FORM_data16:
b6807d98
TT
23655 result = DW_BLOCK (attr)->data;
23656 *len = DW_BLOCK (attr)->size;
23657 break;
23658
23659 /* The DW_AT_const_value attributes are supposed to carry the
23660 symbol's value "represented as it would be on the target
23661 architecture." By the time we get here, it's already been
23662 converted to host endianness, so we just need to sign- or
23663 zero-extend it as appropriate. */
23664 case DW_FORM_data1:
23665 type = die_type (die, cu);
23666 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23667 if (result == NULL)
23668 result = write_constant_as_bytes (obstack, byte_order,
23669 type, value, len);
23670 break;
23671 case DW_FORM_data2:
23672 type = die_type (die, cu);
23673 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23674 if (result == NULL)
23675 result = write_constant_as_bytes (obstack, byte_order,
23676 type, value, len);
23677 break;
23678 case DW_FORM_data4:
23679 type = die_type (die, cu);
23680 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23681 if (result == NULL)
23682 result = write_constant_as_bytes (obstack, byte_order,
23683 type, value, len);
23684 break;
23685 case DW_FORM_data8:
23686 type = die_type (die, cu);
23687 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23688 if (result == NULL)
23689 result = write_constant_as_bytes (obstack, byte_order,
23690 type, value, len);
23691 break;
23692
23693 case DW_FORM_sdata:
663c44ac 23694 case DW_FORM_implicit_const:
b6807d98
TT
23695 type = die_type (die, cu);
23696 result = write_constant_as_bytes (obstack, byte_order,
23697 type, DW_SND (attr), len);
23698 break;
23699
23700 case DW_FORM_udata:
23701 type = die_type (die, cu);
23702 result = write_constant_as_bytes (obstack, byte_order,
23703 type, DW_UNSND (attr), len);
23704 break;
23705
23706 default:
b98664d3 23707 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23708 dwarf_form_name (attr->form));
23709 break;
23710 }
23711
23712 return result;
23713}
23714
7942e96e
AA
23715/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23716 valid type for this die is found. */
23717
23718struct type *
9c541725 23719dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23720 struct dwarf2_per_cu_data *per_cu)
23721{
23722 struct dwarf2_cu *cu;
23723 struct die_info *die;
23724
7942e96e 23725 if (per_cu->cu == NULL)
58f0c718 23726 load_cu (per_cu, false);
7942e96e
AA
23727 cu = per_cu->cu;
23728 if (!cu)
23729 return NULL;
23730
9c541725 23731 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23732 if (!die)
23733 return NULL;
23734
23735 return die_type (die, cu);
23736}
23737
8a9b8146
TT
23738/* Return the type of the DIE at DIE_OFFSET in the CU named by
23739 PER_CU. */
23740
23741struct type *
b64f50a1 23742dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23743 struct dwarf2_per_cu_data *per_cu)
23744{
9c541725 23745 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23746 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23747}
23748
ac9ec31b 23749/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23750 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23751 On exit *REF_CU is the CU of the result.
23752 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23753
23754static struct die_info *
ac9ec31b
DE
23755follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23756 struct dwarf2_cu **ref_cu)
348e048f 23757{
348e048f 23758 struct die_info temp_die;
c24bdb02 23759 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23760 struct die_info *die;
23761
ac9ec31b
DE
23762 /* While it might be nice to assert sig_type->type == NULL here,
23763 we can get here for DW_AT_imported_declaration where we need
23764 the DIE not the type. */
348e048f
DE
23765
23766 /* If necessary, add it to the queue and load its DIEs. */
23767
95554aad 23768 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23769 read_signatured_type (sig_type);
348e048f 23770
348e048f 23771 sig_cu = sig_type->per_cu.cu;
69d751e3 23772 gdb_assert (sig_cu != NULL);
9c541725
PA
23773 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23774 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23775 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23776 to_underlying (temp_die.sect_off));
348e048f
DE
23777 if (die)
23778 {
ed2dc618 23779 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23780 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23781
796a7ff8
DE
23782 /* For .gdb_index version 7 keep track of included TUs.
23783 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23784 if (dwarf2_per_objfile->index_table != NULL
23785 && dwarf2_per_objfile->index_table->version <= 7)
23786 {
ae640021 23787 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23788 }
23789
348e048f 23790 *ref_cu = sig_cu;
c24bdb02
KS
23791 if (sig_cu != cu)
23792 sig_cu->ancestor = cu;
23793
348e048f
DE
23794 return die;
23795 }
23796
ac9ec31b
DE
23797 return NULL;
23798}
23799
23800/* Follow signatured type referenced by ATTR in SRC_DIE.
23801 On entry *REF_CU is the CU of SRC_DIE.
23802 On exit *REF_CU is the CU of the result.
23803 The result is the DIE of the type.
23804 If the referenced type cannot be found an error is thrown. */
23805
23806static struct die_info *
ff39bb5e 23807follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23808 struct dwarf2_cu **ref_cu)
23809{
23810 ULONGEST signature = DW_SIGNATURE (attr);
23811 struct signatured_type *sig_type;
23812 struct die_info *die;
23813
23814 gdb_assert (attr->form == DW_FORM_ref_sig8);
23815
a2ce51a0 23816 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23817 /* sig_type will be NULL if the signatured type is missing from
23818 the debug info. */
23819 if (sig_type == NULL)
23820 {
23821 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23822 " from DIE at %s [in module %s]"),
23823 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23824 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23825 }
23826
23827 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23828 if (die == NULL)
23829 {
23830 dump_die_for_error (src_die);
23831 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23832 " from DIE at %s [in module %s]"),
23833 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23834 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23835 }
23836
23837 return die;
23838}
23839
23840/* Get the type specified by SIGNATURE referenced in DIE/CU,
23841 reading in and processing the type unit if necessary. */
23842
23843static struct type *
23844get_signatured_type (struct die_info *die, ULONGEST signature,
23845 struct dwarf2_cu *cu)
23846{
518817b3
SM
23847 struct dwarf2_per_objfile *dwarf2_per_objfile
23848 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23849 struct signatured_type *sig_type;
23850 struct dwarf2_cu *type_cu;
23851 struct die_info *type_die;
23852 struct type *type;
23853
a2ce51a0 23854 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23855 /* sig_type will be NULL if the signatured type is missing from
23856 the debug info. */
23857 if (sig_type == NULL)
23858 {
b98664d3 23859 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23860 " from DIE at %s [in module %s]"),
23861 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23862 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23863 return build_error_marker_type (cu, die);
23864 }
23865
23866 /* If we already know the type we're done. */
23867 if (sig_type->type != NULL)
23868 return sig_type->type;
23869
23870 type_cu = cu;
23871 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23872 if (type_die != NULL)
23873 {
23874 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23875 is created. This is important, for example, because for c++ classes
23876 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23877 type = read_type_die (type_die, type_cu);
23878 if (type == NULL)
23879 {
b98664d3 23880 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23881 " referenced from DIE at %s [in module %s]"),
23882 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23883 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23884 type = build_error_marker_type (cu, die);
23885 }
23886 }
23887 else
23888 {
b98664d3 23889 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23890 " from DIE at %s [in module %s]"),
23891 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23892 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23893 type = build_error_marker_type (cu, die);
23894 }
23895 sig_type->type = type;
23896
23897 return type;
23898}
23899
23900/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23901 reading in and processing the type unit if necessary. */
23902
23903static struct type *
ff39bb5e 23904get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23905 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23906{
23907 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23908 if (attr_form_is_ref (attr))
ac9ec31b
DE
23909 {
23910 struct dwarf2_cu *type_cu = cu;
23911 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23912
23913 return read_type_die (type_die, type_cu);
23914 }
23915 else if (attr->form == DW_FORM_ref_sig8)
23916 {
23917 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23918 }
23919 else
23920 {
518817b3
SM
23921 struct dwarf2_per_objfile *dwarf2_per_objfile
23922 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23923
b98664d3 23924 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23925 " at %s [in module %s]"),
23926 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23927 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23928 return build_error_marker_type (cu, die);
23929 }
348e048f
DE
23930}
23931
e5fe5e75 23932/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23933
23934static void
e5fe5e75 23935load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23936{
52dc124a 23937 struct signatured_type *sig_type;
348e048f 23938
f4dc4d17
DE
23939 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23940 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23941
6721b2ec
DE
23942 /* We have the per_cu, but we need the signatured_type.
23943 Fortunately this is an easy translation. */
23944 gdb_assert (per_cu->is_debug_types);
23945 sig_type = (struct signatured_type *) per_cu;
348e048f 23946
6721b2ec 23947 gdb_assert (per_cu->cu == NULL);
348e048f 23948
52dc124a 23949 read_signatured_type (sig_type);
348e048f 23950
6721b2ec 23951 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23952}
23953
3019eac3
DE
23954/* Read in a signatured type and build its CU and DIEs.
23955 If the type is a stub for the real type in a DWO file,
23956 read in the real type from the DWO file as well. */
dee91e82
DE
23957
23958static void
23959read_signatured_type (struct signatured_type *sig_type)
23960{
23961 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23962
3019eac3 23963 gdb_assert (per_cu->is_debug_types);
dee91e82 23964 gdb_assert (per_cu->cu == NULL);
348e048f 23965
c0ab21c2
TT
23966 cutu_reader reader (per_cu, NULL, 0, 1, false);
23967
23968 if (!reader.dummy_p)
23969 {
23970 struct dwarf2_cu *cu = reader.cu;
23971 const gdb_byte *info_ptr = reader.info_ptr;
23972
23973 gdb_assert (cu->die_hash == NULL);
23974 cu->die_hash =
23975 htab_create_alloc_ex (cu->header.length / 12,
23976 die_hash,
23977 die_eq,
23978 NULL,
23979 &cu->comp_unit_obstack,
23980 hashtab_obstack_allocate,
23981 dummy_obstack_deallocate);
23982
23983 if (reader.has_children)
23984 reader.comp_unit_die->child
23985 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23986 reader.comp_unit_die);
23987 cu->dies = reader.comp_unit_die;
23988 /* comp_unit_die is not stored in die_hash, no need. */
23989
23990 /* We try not to read any attributes in this function, because
23991 not all CUs needed for references have been loaded yet, and
23992 symbol table processing isn't initialized. But we have to
23993 set the CU language, or we won't be able to build types
23994 correctly. Similarly, if we do not read the producer, we can
23995 not apply producer-specific interpretation. */
23996 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23997 }
23998
7ee85ab1 23999 sig_type->per_cu.tu_read = 1;
c906108c
SS
24000}
24001
c906108c
SS
24002/* Decode simple location descriptions.
24003 Given a pointer to a dwarf block that defines a location, compute
24004 the location and return the value.
24005
4cecd739
DJ
24006 NOTE drow/2003-11-18: This function is called in two situations
24007 now: for the address of static or global variables (partial symbols
24008 only) and for offsets into structures which are expected to be
24009 (more or less) constant. The partial symbol case should go away,
24010 and only the constant case should remain. That will let this
24011 function complain more accurately. A few special modes are allowed
24012 without complaint for global variables (for instance, global
24013 register values and thread-local values).
c906108c
SS
24014
24015 A location description containing no operations indicates that the
4cecd739 24016 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24017 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24018 callers will only want a very basic result and this can become a
21ae7a4d
JK
24019 complaint.
24020
24021 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24022
24023static CORE_ADDR
e7c27a73 24024decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24025{
518817b3 24026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24027 size_t i;
24028 size_t size = blk->size;
d521ce57 24029 const gdb_byte *data = blk->data;
21ae7a4d
JK
24030 CORE_ADDR stack[64];
24031 int stacki;
24032 unsigned int bytes_read, unsnd;
24033 gdb_byte op;
c906108c 24034
21ae7a4d
JK
24035 i = 0;
24036 stacki = 0;
24037 stack[stacki] = 0;
24038 stack[++stacki] = 0;
24039
24040 while (i < size)
24041 {
24042 op = data[i++];
24043 switch (op)
24044 {
24045 case DW_OP_lit0:
24046 case DW_OP_lit1:
24047 case DW_OP_lit2:
24048 case DW_OP_lit3:
24049 case DW_OP_lit4:
24050 case DW_OP_lit5:
24051 case DW_OP_lit6:
24052 case DW_OP_lit7:
24053 case DW_OP_lit8:
24054 case DW_OP_lit9:
24055 case DW_OP_lit10:
24056 case DW_OP_lit11:
24057 case DW_OP_lit12:
24058 case DW_OP_lit13:
24059 case DW_OP_lit14:
24060 case DW_OP_lit15:
24061 case DW_OP_lit16:
24062 case DW_OP_lit17:
24063 case DW_OP_lit18:
24064 case DW_OP_lit19:
24065 case DW_OP_lit20:
24066 case DW_OP_lit21:
24067 case DW_OP_lit22:
24068 case DW_OP_lit23:
24069 case DW_OP_lit24:
24070 case DW_OP_lit25:
24071 case DW_OP_lit26:
24072 case DW_OP_lit27:
24073 case DW_OP_lit28:
24074 case DW_OP_lit29:
24075 case DW_OP_lit30:
24076 case DW_OP_lit31:
24077 stack[++stacki] = op - DW_OP_lit0;
24078 break;
f1bea926 24079
21ae7a4d
JK
24080 case DW_OP_reg0:
24081 case DW_OP_reg1:
24082 case DW_OP_reg2:
24083 case DW_OP_reg3:
24084 case DW_OP_reg4:
24085 case DW_OP_reg5:
24086 case DW_OP_reg6:
24087 case DW_OP_reg7:
24088 case DW_OP_reg8:
24089 case DW_OP_reg9:
24090 case DW_OP_reg10:
24091 case DW_OP_reg11:
24092 case DW_OP_reg12:
24093 case DW_OP_reg13:
24094 case DW_OP_reg14:
24095 case DW_OP_reg15:
24096 case DW_OP_reg16:
24097 case DW_OP_reg17:
24098 case DW_OP_reg18:
24099 case DW_OP_reg19:
24100 case DW_OP_reg20:
24101 case DW_OP_reg21:
24102 case DW_OP_reg22:
24103 case DW_OP_reg23:
24104 case DW_OP_reg24:
24105 case DW_OP_reg25:
24106 case DW_OP_reg26:
24107 case DW_OP_reg27:
24108 case DW_OP_reg28:
24109 case DW_OP_reg29:
24110 case DW_OP_reg30:
24111 case DW_OP_reg31:
24112 stack[++stacki] = op - DW_OP_reg0;
24113 if (i < size)
24114 dwarf2_complex_location_expr_complaint ();
24115 break;
c906108c 24116
21ae7a4d
JK
24117 case DW_OP_regx:
24118 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24119 i += bytes_read;
24120 stack[++stacki] = unsnd;
24121 if (i < size)
24122 dwarf2_complex_location_expr_complaint ();
24123 break;
c906108c 24124
21ae7a4d
JK
24125 case DW_OP_addr:
24126 stack[++stacki] = read_address (objfile->obfd, &data[i],
24127 cu, &bytes_read);
24128 i += bytes_read;
24129 break;
d53d4ac5 24130
21ae7a4d
JK
24131 case DW_OP_const1u:
24132 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24133 i += 1;
24134 break;
24135
24136 case DW_OP_const1s:
24137 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24138 i += 1;
24139 break;
24140
24141 case DW_OP_const2u:
24142 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24143 i += 2;
24144 break;
24145
24146 case DW_OP_const2s:
24147 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24148 i += 2;
24149 break;
d53d4ac5 24150
21ae7a4d
JK
24151 case DW_OP_const4u:
24152 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24153 i += 4;
24154 break;
24155
24156 case DW_OP_const4s:
24157 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24158 i += 4;
24159 break;
24160
585861ea
JK
24161 case DW_OP_const8u:
24162 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24163 i += 8;
24164 break;
24165
21ae7a4d
JK
24166 case DW_OP_constu:
24167 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24168 &bytes_read);
24169 i += bytes_read;
24170 break;
24171
24172 case DW_OP_consts:
24173 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24174 i += bytes_read;
24175 break;
24176
24177 case DW_OP_dup:
24178 stack[stacki + 1] = stack[stacki];
24179 stacki++;
24180 break;
24181
24182 case DW_OP_plus:
24183 stack[stacki - 1] += stack[stacki];
24184 stacki--;
24185 break;
24186
24187 case DW_OP_plus_uconst:
24188 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24189 &bytes_read);
24190 i += bytes_read;
24191 break;
24192
24193 case DW_OP_minus:
24194 stack[stacki - 1] -= stack[stacki];
24195 stacki--;
24196 break;
24197
24198 case DW_OP_deref:
24199 /* If we're not the last op, then we definitely can't encode
24200 this using GDB's address_class enum. This is valid for partial
24201 global symbols, although the variable's address will be bogus
24202 in the psymtab. */
24203 if (i < size)
24204 dwarf2_complex_location_expr_complaint ();
24205 break;
24206
24207 case DW_OP_GNU_push_tls_address:
4aa4e28b 24208 case DW_OP_form_tls_address:
21ae7a4d
JK
24209 /* The top of the stack has the offset from the beginning
24210 of the thread control block at which the variable is located. */
24211 /* Nothing should follow this operator, so the top of stack would
24212 be returned. */
24213 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24214 address will be bogus in the psymtab. Make it always at least
24215 non-zero to not look as a variable garbage collected by linker
24216 which have DW_OP_addr 0. */
21ae7a4d
JK
24217 if (i < size)
24218 dwarf2_complex_location_expr_complaint ();
585861ea 24219 stack[stacki]++;
21ae7a4d
JK
24220 break;
24221
24222 case DW_OP_GNU_uninit:
24223 break;
24224
336d760d 24225 case DW_OP_addrx:
3019eac3 24226 case DW_OP_GNU_addr_index:
49f6c839 24227 case DW_OP_GNU_const_index:
3019eac3
DE
24228 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24229 &bytes_read);
24230 i += bytes_read;
24231 break;
24232
21ae7a4d
JK
24233 default:
24234 {
f39c6ffd 24235 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24236
24237 if (name)
b98664d3 24238 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24239 name);
24240 else
b98664d3 24241 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24242 op);
24243 }
24244
24245 return (stack[stacki]);
d53d4ac5 24246 }
3c6e0cb3 24247
21ae7a4d
JK
24248 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24249 outside of the allocated space. Also enforce minimum>0. */
24250 if (stacki >= ARRAY_SIZE (stack) - 1)
24251 {
b98664d3 24252 complaint (_("location description stack overflow"));
21ae7a4d
JK
24253 return 0;
24254 }
24255
24256 if (stacki <= 0)
24257 {
b98664d3 24258 complaint (_("location description stack underflow"));
21ae7a4d
JK
24259 return 0;
24260 }
24261 }
24262 return (stack[stacki]);
c906108c
SS
24263}
24264
24265/* memory allocation interface */
24266
c906108c 24267static struct dwarf_block *
7b5a2f43 24268dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24269{
8d749320 24270 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24271}
24272
c906108c 24273static struct die_info *
b60c80d6 24274dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24275{
24276 struct die_info *die;
b60c80d6
DJ
24277 size_t size = sizeof (struct die_info);
24278
24279 if (num_attrs > 1)
24280 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24281
b60c80d6 24282 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24283 memset (die, 0, sizeof (struct die_info));
24284 return (die);
24285}
2e276125
JB
24286
24287\f
24288/* Macro support. */
24289
233d95b5
JK
24290/* Return file name relative to the compilation directory of file number I in
24291 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24292 responsible for freeing it. */
233d95b5 24293
2e276125 24294static char *
233d95b5 24295file_file_name (int file, struct line_header *lh)
2e276125 24296{
6a83a1e6
EZ
24297 /* Is the file number a valid index into the line header's file name
24298 table? Remember that file numbers start with one, not zero. */
7ba99d21 24299 if (lh->is_valid_file_index (file))
6a83a1e6 24300 {
7ba99d21 24301 const file_entry *fe = lh->file_name_at (file);
6e70227d 24302
7ba99d21 24303 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24304 {
7ba99d21 24305 const char *dir = fe->include_dir (lh);
8c43009f 24306 if (dir != NULL)
7ba99d21 24307 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24308 }
7ba99d21 24309 return xstrdup (fe->name);
6a83a1e6 24310 }
2e276125
JB
24311 else
24312 {
6a83a1e6
EZ
24313 /* The compiler produced a bogus file number. We can at least
24314 record the macro definitions made in the file, even if we
24315 won't be able to find the file by name. */
24316 char fake_name[80];
9a619af0 24317
8c042590
PM
24318 xsnprintf (fake_name, sizeof (fake_name),
24319 "<bad macro file number %d>", file);
2e276125 24320
b98664d3 24321 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24322 file);
2e276125 24323
6a83a1e6 24324 return xstrdup (fake_name);
2e276125
JB
24325 }
24326}
24327
233d95b5
JK
24328/* Return the full name of file number I in *LH's file name table.
24329 Use COMP_DIR as the name of the current directory of the
24330 compilation. The result is allocated using xmalloc; the caller is
24331 responsible for freeing it. */
24332static char *
24333file_full_name (int file, struct line_header *lh, const char *comp_dir)
24334{
24335 /* Is the file number a valid index into the line header's file name
24336 table? Remember that file numbers start with one, not zero. */
7ba99d21 24337 if (lh->is_valid_file_index (file))
233d95b5
JK
24338 {
24339 char *relative = file_file_name (file, lh);
24340
24341 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24342 return relative;
b36cec19
PA
24343 return reconcat (relative, comp_dir, SLASH_STRING,
24344 relative, (char *) NULL);
233d95b5
JK
24345 }
24346 else
24347 return file_file_name (file, lh);
24348}
24349
2e276125
JB
24350
24351static struct macro_source_file *
804d2729
TT
24352macro_start_file (struct dwarf2_cu *cu,
24353 int file, int line,
2e276125 24354 struct macro_source_file *current_file,
43f3e411 24355 struct line_header *lh)
2e276125 24356{
233d95b5
JK
24357 /* File name relative to the compilation directory of this source file. */
24358 char *file_name = file_file_name (file, lh);
2e276125 24359
2e276125 24360 if (! current_file)
abc9d0dc 24361 {
fc474241
DE
24362 /* Note: We don't create a macro table for this compilation unit
24363 at all until we actually get a filename. */
c24bdb02 24364 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24365
abc9d0dc
TT
24366 /* If we have no current file, then this must be the start_file
24367 directive for the compilation unit's main source file. */
fc474241
DE
24368 current_file = macro_set_main (macro_table, file_name);
24369 macro_define_special (macro_table);
abc9d0dc 24370 }
2e276125 24371 else
233d95b5 24372 current_file = macro_include (current_file, line, file_name);
2e276125 24373
233d95b5 24374 xfree (file_name);
6e70227d 24375
2e276125
JB
24376 return current_file;
24377}
24378
2e276125
JB
24379static const char *
24380consume_improper_spaces (const char *p, const char *body)
24381{
24382 if (*p == ' ')
24383 {
b98664d3 24384 complaint (_("macro definition contains spaces "
3e43a32a 24385 "in formal argument list:\n`%s'"),
4d3c2250 24386 body);
2e276125
JB
24387
24388 while (*p == ' ')
24389 p++;
24390 }
24391
24392 return p;
24393}
24394
24395
24396static void
24397parse_macro_definition (struct macro_source_file *file, int line,
24398 const char *body)
24399{
24400 const char *p;
24401
24402 /* The body string takes one of two forms. For object-like macro
24403 definitions, it should be:
24404
24405 <macro name> " " <definition>
24406
24407 For function-like macro definitions, it should be:
24408
24409 <macro name> "() " <definition>
24410 or
24411 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24412
24413 Spaces may appear only where explicitly indicated, and in the
24414 <definition>.
24415
24416 The Dwarf 2 spec says that an object-like macro's name is always
24417 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24418 the space when the macro's definition is the empty string.
2e276125
JB
24419
24420 The Dwarf 2 spec says that there should be no spaces between the
24421 formal arguments in a function-like macro's formal argument list,
24422 but versions of GCC around March 2002 include spaces after the
24423 commas. */
24424
24425
24426 /* Find the extent of the macro name. The macro name is terminated
24427 by either a space or null character (for an object-like macro) or
24428 an opening paren (for a function-like macro). */
24429 for (p = body; *p; p++)
24430 if (*p == ' ' || *p == '(')
24431 break;
24432
24433 if (*p == ' ' || *p == '\0')
24434 {
24435 /* It's an object-like macro. */
24436 int name_len = p - body;
456e800a 24437 std::string name (body, name_len);
2e276125
JB
24438 const char *replacement;
24439
24440 if (*p == ' ')
24441 replacement = body + name_len + 1;
24442 else
24443 {
4d3c2250 24444 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24445 replacement = body + name_len;
24446 }
6e70227d 24447
456e800a 24448 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24449 }
24450 else if (*p == '(')
24451 {
24452 /* It's a function-like macro. */
456e800a 24453 std::string name (body, p - body);
2e276125
JB
24454 int argc = 0;
24455 int argv_size = 1;
8d749320 24456 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24457
24458 p++;
24459
24460 p = consume_improper_spaces (p, body);
24461
24462 /* Parse the formal argument list. */
24463 while (*p && *p != ')')
24464 {
24465 /* Find the extent of the current argument name. */
24466 const char *arg_start = p;
24467
24468 while (*p && *p != ',' && *p != ')' && *p != ' ')
24469 p++;
24470
24471 if (! *p || p == arg_start)
4d3c2250 24472 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24473 else
24474 {
24475 /* Make sure argv has room for the new argument. */
24476 if (argc >= argv_size)
24477 {
24478 argv_size *= 2;
224c3ddb 24479 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24480 }
24481
3f8a7804 24482 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24483 }
24484
24485 p = consume_improper_spaces (p, body);
24486
24487 /* Consume the comma, if present. */
24488 if (*p == ',')
24489 {
24490 p++;
24491
24492 p = consume_improper_spaces (p, body);
24493 }
24494 }
24495
24496 if (*p == ')')
24497 {
24498 p++;
24499
24500 if (*p == ' ')
24501 /* Perfectly formed definition, no complaints. */
456e800a 24502 macro_define_function (file, line, name.c_str (),
6e70227d 24503 argc, (const char **) argv,
2e276125
JB
24504 p + 1);
24505 else if (*p == '\0')
24506 {
24507 /* Complain, but do define it. */
4d3c2250 24508 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24509 macro_define_function (file, line, name.c_str (),
6e70227d 24510 argc, (const char **) argv,
2e276125
JB
24511 p);
24512 }
24513 else
24514 /* Just complain. */
4d3c2250 24515 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24516 }
24517 else
24518 /* Just complain. */
4d3c2250 24519 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24520
2e276125
JB
24521 {
24522 int i;
24523
24524 for (i = 0; i < argc; i++)
24525 xfree (argv[i]);
24526 }
24527 xfree (argv);
24528 }
24529 else
4d3c2250 24530 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24531}
24532
cf2c3c16
TT
24533/* Skip some bytes from BYTES according to the form given in FORM.
24534 Returns the new pointer. */
2e276125 24535
d521ce57
TT
24536static const gdb_byte *
24537skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24538 enum dwarf_form form,
24539 unsigned int offset_size,
24540 struct dwarf2_section_info *section)
2e276125 24541{
cf2c3c16 24542 unsigned int bytes_read;
2e276125 24543
cf2c3c16 24544 switch (form)
2e276125 24545 {
cf2c3c16
TT
24546 case DW_FORM_data1:
24547 case DW_FORM_flag:
24548 ++bytes;
24549 break;
24550
24551 case DW_FORM_data2:
24552 bytes += 2;
24553 break;
24554
24555 case DW_FORM_data4:
24556 bytes += 4;
24557 break;
24558
24559 case DW_FORM_data8:
24560 bytes += 8;
24561 break;
24562
0224619f
JK
24563 case DW_FORM_data16:
24564 bytes += 16;
24565 break;
24566
cf2c3c16
TT
24567 case DW_FORM_string:
24568 read_direct_string (abfd, bytes, &bytes_read);
24569 bytes += bytes_read;
24570 break;
24571
24572 case DW_FORM_sec_offset:
24573 case DW_FORM_strp:
36586728 24574 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24575 bytes += offset_size;
24576 break;
24577
24578 case DW_FORM_block:
24579 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24580 bytes += bytes_read;
24581 break;
24582
24583 case DW_FORM_block1:
24584 bytes += 1 + read_1_byte (abfd, bytes);
24585 break;
24586 case DW_FORM_block2:
24587 bytes += 2 + read_2_bytes (abfd, bytes);
24588 break;
24589 case DW_FORM_block4:
24590 bytes += 4 + read_4_bytes (abfd, bytes);
24591 break;
24592
336d760d 24593 case DW_FORM_addrx:
cf2c3c16 24594 case DW_FORM_sdata:
cf532bd1 24595 case DW_FORM_strx:
cf2c3c16 24596 case DW_FORM_udata:
3019eac3
DE
24597 case DW_FORM_GNU_addr_index:
24598 case DW_FORM_GNU_str_index:
d521ce57 24599 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24600 if (bytes == NULL)
24601 {
24602 dwarf2_section_buffer_overflow_complaint (section);
24603 return NULL;
24604 }
cf2c3c16
TT
24605 break;
24606
663c44ac
JK
24607 case DW_FORM_implicit_const:
24608 break;
24609
cf2c3c16
TT
24610 default:
24611 {
b98664d3 24612 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24613 form, get_section_name (section));
cf2c3c16
TT
24614 return NULL;
24615 }
2e276125
JB
24616 }
24617
cf2c3c16
TT
24618 return bytes;
24619}
757a13d0 24620
cf2c3c16
TT
24621/* A helper for dwarf_decode_macros that handles skipping an unknown
24622 opcode. Returns an updated pointer to the macro data buffer; or,
24623 on error, issues a complaint and returns NULL. */
757a13d0 24624
d521ce57 24625static const gdb_byte *
cf2c3c16 24626skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24627 const gdb_byte **opcode_definitions,
24628 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24629 bfd *abfd,
24630 unsigned int offset_size,
24631 struct dwarf2_section_info *section)
24632{
24633 unsigned int bytes_read, i;
24634 unsigned long arg;
d521ce57 24635 const gdb_byte *defn;
2e276125 24636
cf2c3c16 24637 if (opcode_definitions[opcode] == NULL)
2e276125 24638 {
b98664d3 24639 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24640 opcode);
24641 return NULL;
24642 }
2e276125 24643
cf2c3c16
TT
24644 defn = opcode_definitions[opcode];
24645 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24646 defn += bytes_read;
2e276125 24647
cf2c3c16
TT
24648 for (i = 0; i < arg; ++i)
24649 {
aead7601
SM
24650 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24651 (enum dwarf_form) defn[i], offset_size,
f664829e 24652 section);
cf2c3c16
TT
24653 if (mac_ptr == NULL)
24654 {
24655 /* skip_form_bytes already issued the complaint. */
24656 return NULL;
24657 }
24658 }
757a13d0 24659
cf2c3c16
TT
24660 return mac_ptr;
24661}
757a13d0 24662
cf2c3c16
TT
24663/* A helper function which parses the header of a macro section.
24664 If the macro section is the extended (for now called "GNU") type,
24665 then this updates *OFFSET_SIZE. Returns a pointer to just after
24666 the header, or issues a complaint and returns NULL on error. */
757a13d0 24667
d521ce57
TT
24668static const gdb_byte *
24669dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24670 bfd *abfd,
d521ce57 24671 const gdb_byte *mac_ptr,
cf2c3c16
TT
24672 unsigned int *offset_size,
24673 int section_is_gnu)
24674{
24675 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24676
cf2c3c16
TT
24677 if (section_is_gnu)
24678 {
24679 unsigned int version, flags;
757a13d0 24680
cf2c3c16 24681 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24682 if (version != 4 && version != 5)
cf2c3c16 24683 {
b98664d3 24684 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24685 version);
24686 return NULL;
24687 }
24688 mac_ptr += 2;
757a13d0 24689
cf2c3c16
TT
24690 flags = read_1_byte (abfd, mac_ptr);
24691 ++mac_ptr;
24692 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24693
cf2c3c16
TT
24694 if ((flags & 2) != 0)
24695 /* We don't need the line table offset. */
24696 mac_ptr += *offset_size;
757a13d0 24697
cf2c3c16
TT
24698 /* Vendor opcode descriptions. */
24699 if ((flags & 4) != 0)
24700 {
24701 unsigned int i, count;
757a13d0 24702
cf2c3c16
TT
24703 count = read_1_byte (abfd, mac_ptr);
24704 ++mac_ptr;
24705 for (i = 0; i < count; ++i)
24706 {
24707 unsigned int opcode, bytes_read;
24708 unsigned long arg;
24709
24710 opcode = read_1_byte (abfd, mac_ptr);
24711 ++mac_ptr;
24712 opcode_definitions[opcode] = mac_ptr;
24713 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24714 mac_ptr += bytes_read;
24715 mac_ptr += arg;
24716 }
757a13d0 24717 }
cf2c3c16 24718 }
757a13d0 24719
cf2c3c16
TT
24720 return mac_ptr;
24721}
757a13d0 24722
cf2c3c16 24723/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24724 including DW_MACRO_import. */
cf2c3c16
TT
24725
24726static void
804d2729 24727dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24728 bfd *abfd,
d521ce57 24729 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24730 struct macro_source_file *current_file,
43f3e411 24731 struct line_header *lh,
cf2c3c16 24732 struct dwarf2_section_info *section,
36586728 24733 int section_is_gnu, int section_is_dwz,
cf2c3c16 24734 unsigned int offset_size,
8fc3fc34 24735 htab_t include_hash)
cf2c3c16 24736{
804d2729
TT
24737 struct dwarf2_per_objfile *dwarf2_per_objfile
24738 = cu->per_cu->dwarf2_per_objfile;
4d663531 24739 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24740 enum dwarf_macro_record_type macinfo_type;
24741 int at_commandline;
d521ce57 24742 const gdb_byte *opcode_definitions[256];
757a13d0 24743
cf2c3c16
TT
24744 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24745 &offset_size, section_is_gnu);
24746 if (mac_ptr == NULL)
24747 {
24748 /* We already issued a complaint. */
24749 return;
24750 }
757a13d0
JK
24751
24752 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24753 GDB is still reading the definitions from command line. First
24754 DW_MACINFO_start_file will need to be ignored as it was already executed
24755 to create CURRENT_FILE for the main source holding also the command line
24756 definitions. On first met DW_MACINFO_start_file this flag is reset to
24757 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24758
24759 at_commandline = 1;
24760
24761 do
24762 {
24763 /* Do we at least have room for a macinfo type byte? */
24764 if (mac_ptr >= mac_end)
24765 {
f664829e 24766 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24767 break;
24768 }
24769
aead7601 24770 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24771 mac_ptr++;
24772
cf2c3c16
TT
24773 /* Note that we rely on the fact that the corresponding GNU and
24774 DWARF constants are the same. */
132448f8
SM
24775 DIAGNOSTIC_PUSH
24776 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24777 switch (macinfo_type)
24778 {
24779 /* A zero macinfo type indicates the end of the macro
24780 information. */
24781 case 0:
24782 break;
2e276125 24783
0af92d60
JK
24784 case DW_MACRO_define:
24785 case DW_MACRO_undef:
24786 case DW_MACRO_define_strp:
24787 case DW_MACRO_undef_strp:
24788 case DW_MACRO_define_sup:
24789 case DW_MACRO_undef_sup:
2e276125 24790 {
891d2f0b 24791 unsigned int bytes_read;
2e276125 24792 int line;
d521ce57 24793 const char *body;
cf2c3c16 24794 int is_define;
2e276125 24795
cf2c3c16
TT
24796 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24797 mac_ptr += bytes_read;
24798
0af92d60
JK
24799 if (macinfo_type == DW_MACRO_define
24800 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24801 {
24802 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24803 mac_ptr += bytes_read;
24804 }
24805 else
24806 {
24807 LONGEST str_offset;
24808
24809 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24810 mac_ptr += offset_size;
2e276125 24811
0af92d60
JK
24812 if (macinfo_type == DW_MACRO_define_sup
24813 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24814 || section_is_dwz)
36586728 24815 {
ed2dc618
SM
24816 struct dwz_file *dwz
24817 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24818
ed2dc618
SM
24819 body = read_indirect_string_from_dwz (objfile,
24820 dwz, str_offset);
36586728
TT
24821 }
24822 else
ed2dc618
SM
24823 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24824 abfd, str_offset);
cf2c3c16
TT
24825 }
24826
0af92d60
JK
24827 is_define = (macinfo_type == DW_MACRO_define
24828 || macinfo_type == DW_MACRO_define_strp
24829 || macinfo_type == DW_MACRO_define_sup);
2e276125 24830 if (! current_file)
757a13d0
JK
24831 {
24832 /* DWARF violation as no main source is present. */
b98664d3 24833 complaint (_("debug info with no main source gives macro %s "
757a13d0 24834 "on line %d: %s"),
cf2c3c16
TT
24835 is_define ? _("definition") : _("undefinition"),
24836 line, body);
757a13d0
JK
24837 break;
24838 }
3e43a32a
MS
24839 if ((line == 0 && !at_commandline)
24840 || (line != 0 && at_commandline))
b98664d3 24841 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24842 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24843 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24844 line == 0 ? _("zero") : _("non-zero"), line, body);
24845
955b06fa 24846 if (body == NULL)
7bede828 24847 {
955b06fa
SDJ
24848 /* Fedora's rpm-build's "debugedit" binary
24849 corrupted .debug_macro sections.
24850
24851 For more info, see
24852 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24853 complaint (_("debug info gives %s invalid macro %s "
24854 "without body (corrupted?) at line %d "
24855 "on file %s"),
24856 at_commandline ? _("command-line") : _("in-file"),
24857 is_define ? _("definition") : _("undefinition"),
24858 line, current_file->filename);
7bede828 24859 }
955b06fa
SDJ
24860 else if (is_define)
24861 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24862 else
24863 {
0af92d60
JK
24864 gdb_assert (macinfo_type == DW_MACRO_undef
24865 || macinfo_type == DW_MACRO_undef_strp
24866 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24867 macro_undef (current_file, line, body);
24868 }
2e276125
JB
24869 }
24870 break;
24871
0af92d60 24872 case DW_MACRO_start_file:
2e276125 24873 {
891d2f0b 24874 unsigned int bytes_read;
2e276125
JB
24875 int line, file;
24876
24877 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24878 mac_ptr += bytes_read;
24879 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24880 mac_ptr += bytes_read;
24881
3e43a32a
MS
24882 if ((line == 0 && !at_commandline)
24883 || (line != 0 && at_commandline))
b98664d3 24884 complaint (_("debug info gives source %d included "
757a13d0
JK
24885 "from %s at %s line %d"),
24886 file, at_commandline ? _("command-line") : _("file"),
24887 line == 0 ? _("zero") : _("non-zero"), line);
24888
24889 if (at_commandline)
24890 {
0af92d60 24891 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24892 pass one. */
757a13d0
JK
24893 at_commandline = 0;
24894 }
24895 else
804d2729
TT
24896 current_file = macro_start_file (cu, file, line, current_file,
24897 lh);
2e276125
JB
24898 }
24899 break;
24900
0af92d60 24901 case DW_MACRO_end_file:
2e276125 24902 if (! current_file)
b98664d3 24903 complaint (_("macro debug info has an unmatched "
3e43a32a 24904 "`close_file' directive"));
2e276125
JB
24905 else
24906 {
24907 current_file = current_file->included_by;
24908 if (! current_file)
24909 {
cf2c3c16 24910 enum dwarf_macro_record_type next_type;
2e276125
JB
24911
24912 /* GCC circa March 2002 doesn't produce the zero
24913 type byte marking the end of the compilation
24914 unit. Complain if it's not there, but exit no
24915 matter what. */
24916
24917 /* Do we at least have room for a macinfo type byte? */
24918 if (mac_ptr >= mac_end)
24919 {
f664829e 24920 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24921 return;
24922 }
24923
24924 /* We don't increment mac_ptr here, so this is just
24925 a look-ahead. */
aead7601
SM
24926 next_type
24927 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24928 mac_ptr);
2e276125 24929 if (next_type != 0)
b98664d3 24930 complaint (_("no terminating 0-type entry for "
3e43a32a 24931 "macros in `.debug_macinfo' section"));
2e276125
JB
24932
24933 return;
24934 }
24935 }
24936 break;
24937
0af92d60
JK
24938 case DW_MACRO_import:
24939 case DW_MACRO_import_sup:
cf2c3c16
TT
24940 {
24941 LONGEST offset;
8fc3fc34 24942 void **slot;
a036ba48
TT
24943 bfd *include_bfd = abfd;
24944 struct dwarf2_section_info *include_section = section;
d521ce57 24945 const gdb_byte *include_mac_end = mac_end;
a036ba48 24946 int is_dwz = section_is_dwz;
d521ce57 24947 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24948
24949 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24950 mac_ptr += offset_size;
24951
0af92d60 24952 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24953 {
ed2dc618 24954 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24955
4d663531 24956 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24957
a036ba48 24958 include_section = &dwz->macro;
a32a8923 24959 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24960 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24961 is_dwz = 1;
24962 }
24963
24964 new_mac_ptr = include_section->buffer + offset;
24965 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24966
8fc3fc34
TT
24967 if (*slot != NULL)
24968 {
24969 /* This has actually happened; see
24970 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24971 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24972 ".debug_macro section"));
24973 }
24974 else
24975 {
d521ce57 24976 *slot = (void *) new_mac_ptr;
36586728 24977
804d2729 24978 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24979 include_mac_end, current_file, lh,
36586728 24980 section, section_is_gnu, is_dwz,
4d663531 24981 offset_size, include_hash);
8fc3fc34 24982
d521ce57 24983 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24984 }
cf2c3c16
TT
24985 }
24986 break;
24987
2e276125 24988 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24989 if (!section_is_gnu)
24990 {
24991 unsigned int bytes_read;
2e276125 24992
ac298888
TT
24993 /* This reads the constant, but since we don't recognize
24994 any vendor extensions, we ignore it. */
24995 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24996 mac_ptr += bytes_read;
24997 read_direct_string (abfd, mac_ptr, &bytes_read);
24998 mac_ptr += bytes_read;
2e276125 24999
cf2c3c16
TT
25000 /* We don't recognize any vendor extensions. */
25001 break;
25002 }
25003 /* FALLTHROUGH */
25004
25005 default:
25006 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25007 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25008 section);
25009 if (mac_ptr == NULL)
25010 return;
25011 break;
2e276125 25012 }
132448f8 25013 DIAGNOSTIC_POP
757a13d0 25014 } while (macinfo_type != 0);
2e276125 25015}
8e19ed76 25016
cf2c3c16 25017static void
09262596 25018dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25019 int section_is_gnu)
cf2c3c16 25020{
518817b3
SM
25021 struct dwarf2_per_objfile *dwarf2_per_objfile
25022 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25023 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25024 struct line_header *lh = cu->line_header;
25025 bfd *abfd;
d521ce57 25026 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25027 struct macro_source_file *current_file = 0;
25028 enum dwarf_macro_record_type macinfo_type;
25029 unsigned int offset_size = cu->header.offset_size;
d521ce57 25030 const gdb_byte *opcode_definitions[256];
8fc3fc34 25031 void **slot;
09262596
DE
25032 struct dwarf2_section_info *section;
25033 const char *section_name;
25034
25035 if (cu->dwo_unit != NULL)
25036 {
25037 if (section_is_gnu)
25038 {
25039 section = &cu->dwo_unit->dwo_file->sections.macro;
25040 section_name = ".debug_macro.dwo";
25041 }
25042 else
25043 {
25044 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25045 section_name = ".debug_macinfo.dwo";
25046 }
25047 }
25048 else
25049 {
25050 if (section_is_gnu)
25051 {
25052 section = &dwarf2_per_objfile->macro;
25053 section_name = ".debug_macro";
25054 }
25055 else
25056 {
25057 section = &dwarf2_per_objfile->macinfo;
25058 section_name = ".debug_macinfo";
25059 }
25060 }
cf2c3c16 25061
bb5ed363 25062 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25063 if (section->buffer == NULL)
25064 {
b98664d3 25065 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25066 return;
25067 }
a32a8923 25068 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25069
25070 /* First pass: Find the name of the base filename.
25071 This filename is needed in order to process all macros whose definition
25072 (or undefinition) comes from the command line. These macros are defined
25073 before the first DW_MACINFO_start_file entry, and yet still need to be
25074 associated to the base file.
25075
25076 To determine the base file name, we scan the macro definitions until we
25077 reach the first DW_MACINFO_start_file entry. We then initialize
25078 CURRENT_FILE accordingly so that any macro definition found before the
25079 first DW_MACINFO_start_file can still be associated to the base file. */
25080
25081 mac_ptr = section->buffer + offset;
25082 mac_end = section->buffer + section->size;
25083
25084 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25085 &offset_size, section_is_gnu);
25086 if (mac_ptr == NULL)
25087 {
25088 /* We already issued a complaint. */
25089 return;
25090 }
25091
25092 do
25093 {
25094 /* Do we at least have room for a macinfo type byte? */
25095 if (mac_ptr >= mac_end)
25096 {
25097 /* Complaint is printed during the second pass as GDB will probably
25098 stop the first pass earlier upon finding
25099 DW_MACINFO_start_file. */
25100 break;
25101 }
25102
aead7601 25103 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25104 mac_ptr++;
25105
25106 /* Note that we rely on the fact that the corresponding GNU and
25107 DWARF constants are the same. */
132448f8
SM
25108 DIAGNOSTIC_PUSH
25109 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25110 switch (macinfo_type)
25111 {
25112 /* A zero macinfo type indicates the end of the macro
25113 information. */
25114 case 0:
25115 break;
25116
0af92d60
JK
25117 case DW_MACRO_define:
25118 case DW_MACRO_undef:
cf2c3c16
TT
25119 /* Only skip the data by MAC_PTR. */
25120 {
25121 unsigned int bytes_read;
25122
25123 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25124 mac_ptr += bytes_read;
25125 read_direct_string (abfd, mac_ptr, &bytes_read);
25126 mac_ptr += bytes_read;
25127 }
25128 break;
25129
0af92d60 25130 case DW_MACRO_start_file:
cf2c3c16
TT
25131 {
25132 unsigned int bytes_read;
25133 int line, file;
25134
25135 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25136 mac_ptr += bytes_read;
25137 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25138 mac_ptr += bytes_read;
25139
804d2729 25140 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25141 }
25142 break;
25143
0af92d60 25144 case DW_MACRO_end_file:
cf2c3c16
TT
25145 /* No data to skip by MAC_PTR. */
25146 break;
25147
0af92d60
JK
25148 case DW_MACRO_define_strp:
25149 case DW_MACRO_undef_strp:
25150 case DW_MACRO_define_sup:
25151 case DW_MACRO_undef_sup:
cf2c3c16
TT
25152 {
25153 unsigned int bytes_read;
25154
25155 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25156 mac_ptr += bytes_read;
25157 mac_ptr += offset_size;
25158 }
25159 break;
25160
0af92d60
JK
25161 case DW_MACRO_import:
25162 case DW_MACRO_import_sup:
cf2c3c16 25163 /* Note that, according to the spec, a transparent include
0af92d60 25164 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25165 skip this opcode. */
25166 mac_ptr += offset_size;
25167 break;
25168
25169 case DW_MACINFO_vendor_ext:
25170 /* Only skip the data by MAC_PTR. */
25171 if (!section_is_gnu)
25172 {
25173 unsigned int bytes_read;
25174
25175 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25176 mac_ptr += bytes_read;
25177 read_direct_string (abfd, mac_ptr, &bytes_read);
25178 mac_ptr += bytes_read;
25179 }
25180 /* FALLTHROUGH */
25181
25182 default:
25183 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25184 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25185 section);
25186 if (mac_ptr == NULL)
25187 return;
25188 break;
25189 }
132448f8 25190 DIAGNOSTIC_POP
cf2c3c16
TT
25191 } while (macinfo_type != 0 && current_file == NULL);
25192
25193 /* Second pass: Process all entries.
25194
25195 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25196 command-line macro definitions/undefinitions. This flag is unset when we
25197 reach the first DW_MACINFO_start_file entry. */
25198
fc4007c9
TT
25199 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25200 htab_eq_pointer,
25201 NULL, xcalloc, xfree));
8fc3fc34 25202 mac_ptr = section->buffer + offset;
fc4007c9 25203 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25204 *slot = (void *) mac_ptr;
804d2729 25205 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25206 current_file, lh, section,
fc4007c9
TT
25207 section_is_gnu, 0, offset_size,
25208 include_hash.get ());
cf2c3c16
TT
25209}
25210
8e19ed76 25211/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25212 if so return true else false. */
380bca97 25213
8e19ed76 25214static int
6e5a29e1 25215attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25216{
25217 return (attr == NULL ? 0 :
25218 attr->form == DW_FORM_block1
25219 || attr->form == DW_FORM_block2
25220 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25221 || attr->form == DW_FORM_block
25222 || attr->form == DW_FORM_exprloc);
8e19ed76 25223}
4c2df51b 25224
c6a0999f
JB
25225/* Return non-zero if ATTR's value is a section offset --- classes
25226 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25227 You may use DW_UNSND (attr) to retrieve such offsets.
25228
25229 Section 7.5.4, "Attribute Encodings", explains that no attribute
25230 may have a value that belongs to more than one of these classes; it
25231 would be ambiguous if we did, because we use the same forms for all
25232 of them. */
380bca97 25233
3690dd37 25234static int
6e5a29e1 25235attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25236{
25237 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25238 || attr->form == DW_FORM_data8
25239 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25240}
25241
3690dd37
JB
25242/* Return non-zero if ATTR's value falls in the 'constant' class, or
25243 zero otherwise. When this function returns true, you can apply
25244 dwarf2_get_attr_constant_value to it.
25245
25246 However, note that for some attributes you must check
25247 attr_form_is_section_offset before using this test. DW_FORM_data4
25248 and DW_FORM_data8 are members of both the constant class, and of
25249 the classes that contain offsets into other debug sections
25250 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25251 that, if an attribute's can be either a constant or one of the
25252 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25253 taken as section offsets, not constants.
25254
25255 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25256 cannot handle that. */
380bca97 25257
3690dd37 25258static int
6e5a29e1 25259attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25260{
25261 switch (attr->form)
25262 {
25263 case DW_FORM_sdata:
25264 case DW_FORM_udata:
25265 case DW_FORM_data1:
25266 case DW_FORM_data2:
25267 case DW_FORM_data4:
25268 case DW_FORM_data8:
663c44ac 25269 case DW_FORM_implicit_const:
3690dd37
JB
25270 return 1;
25271 default:
25272 return 0;
25273 }
25274}
25275
7771576e
SA
25276
25277/* DW_ADDR is always stored already as sect_offset; despite for the forms
25278 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25279
25280static int
6e5a29e1 25281attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25282{
25283 switch (attr->form)
25284 {
25285 case DW_FORM_ref_addr:
25286 case DW_FORM_ref1:
25287 case DW_FORM_ref2:
25288 case DW_FORM_ref4:
25289 case DW_FORM_ref8:
25290 case DW_FORM_ref_udata:
25291 case DW_FORM_GNU_ref_alt:
25292 return 1;
25293 default:
25294 return 0;
25295 }
25296}
25297
3019eac3
DE
25298/* Return the .debug_loc section to use for CU.
25299 For DWO files use .debug_loc.dwo. */
25300
25301static struct dwarf2_section_info *
25302cu_debug_loc_section (struct dwarf2_cu *cu)
25303{
518817b3
SM
25304 struct dwarf2_per_objfile *dwarf2_per_objfile
25305 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25306
3019eac3 25307 if (cu->dwo_unit)
43988095
JK
25308 {
25309 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25310
43988095
JK
25311 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25312 }
25313 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25314 : &dwarf2_per_objfile->loc);
3019eac3
DE
25315}
25316
8cf6f0b1
TT
25317/* A helper function that fills in a dwarf2_loclist_baton. */
25318
25319static void
25320fill_in_loclist_baton (struct dwarf2_cu *cu,
25321 struct dwarf2_loclist_baton *baton,
ff39bb5e 25322 const struct attribute *attr)
8cf6f0b1 25323{
518817b3
SM
25324 struct dwarf2_per_objfile *dwarf2_per_objfile
25325 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25326 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25327
25328 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25329
25330 baton->per_cu = cu->per_cu;
25331 gdb_assert (baton->per_cu);
25332 /* We don't know how long the location list is, but make sure we
25333 don't run off the edge of the section. */
3019eac3
DE
25334 baton->size = section->size - DW_UNSND (attr);
25335 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25336 baton->base_address = cu->base_address;
f664829e 25337 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25338}
25339
4c2df51b 25340static void
ff39bb5e 25341dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25342 struct dwarf2_cu *cu, int is_block)
4c2df51b 25343{
518817b3
SM
25344 struct dwarf2_per_objfile *dwarf2_per_objfile
25345 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25346 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25347 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25348
3690dd37 25349 if (attr_form_is_section_offset (attr)
3019eac3 25350 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25351 the section. If so, fall through to the complaint in the
25352 other branch. */
3019eac3 25353 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25354 {
0d53c4c4 25355 struct dwarf2_loclist_baton *baton;
4c2df51b 25356
8d749320 25357 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25358
8cf6f0b1 25359 fill_in_loclist_baton (cu, baton, attr);
be391dca 25360
d00adf39 25361 if (cu->base_known == 0)
b98664d3 25362 complaint (_("Location list used without "
3e43a32a 25363 "specifying the CU base address."));
4c2df51b 25364
f1e6e072
TT
25365 SYMBOL_ACLASS_INDEX (sym) = (is_block
25366 ? dwarf2_loclist_block_index
25367 : dwarf2_loclist_index);
0d53c4c4
DJ
25368 SYMBOL_LOCATION_BATON (sym) = baton;
25369 }
25370 else
25371 {
25372 struct dwarf2_locexpr_baton *baton;
25373
8d749320 25374 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25375 baton->per_cu = cu->per_cu;
25376 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25377
25378 if (attr_form_is_block (attr))
25379 {
25380 /* Note that we're just copying the block's data pointer
25381 here, not the actual data. We're still pointing into the
6502dd73
DJ
25382 info_buffer for SYM's objfile; right now we never release
25383 that buffer, but when we do clean up properly this may
25384 need to change. */
0d53c4c4
DJ
25385 baton->size = DW_BLOCK (attr)->size;
25386 baton->data = DW_BLOCK (attr)->data;
25387 }
25388 else
25389 {
25390 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25391 sym->natural_name ());
0d53c4c4 25392 baton->size = 0;
0d53c4c4 25393 }
6e70227d 25394
f1e6e072
TT
25395 SYMBOL_ACLASS_INDEX (sym) = (is_block
25396 ? dwarf2_locexpr_block_index
25397 : dwarf2_locexpr_index);
0d53c4c4
DJ
25398 SYMBOL_LOCATION_BATON (sym) = baton;
25399 }
4c2df51b 25400}
6502dd73 25401
9aa1f1e3
TT
25402/* Return the OBJFILE associated with the compilation unit CU. If CU
25403 came from a separate debuginfo file, then the master objfile is
25404 returned. */
ae0d2f24
UW
25405
25406struct objfile *
25407dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25408{
e3b94546 25409 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25410
25411 /* Return the master objfile, so that we can report and look up the
25412 correct file containing this variable. */
25413 if (objfile->separate_debug_objfile_backlink)
25414 objfile = objfile->separate_debug_objfile_backlink;
25415
25416 return objfile;
25417}
25418
96408a79
SA
25419/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25420 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25421 CU_HEADERP first. */
25422
25423static const struct comp_unit_head *
25424per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25425 struct dwarf2_per_cu_data *per_cu)
25426{
d521ce57 25427 const gdb_byte *info_ptr;
96408a79
SA
25428
25429 if (per_cu->cu)
25430 return &per_cu->cu->header;
25431
9c541725 25432 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25433
25434 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25435 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25436 rcuh_kind::COMPILE);
96408a79
SA
25437
25438 return cu_headerp;
25439}
25440
ae0d2f24
UW
25441/* Return the address size given in the compilation unit header for CU. */
25442
98714339 25443int
ae0d2f24
UW
25444dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25445{
96408a79
SA
25446 struct comp_unit_head cu_header_local;
25447 const struct comp_unit_head *cu_headerp;
c471e790 25448
96408a79
SA
25449 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25450
25451 return cu_headerp->addr_size;
ae0d2f24
UW
25452}
25453
9eae7c52
TT
25454/* Return the offset size given in the compilation unit header for CU. */
25455
25456int
25457dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25458{
96408a79
SA
25459 struct comp_unit_head cu_header_local;
25460 const struct comp_unit_head *cu_headerp;
9c6c53f7 25461
96408a79
SA
25462 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25463
25464 return cu_headerp->offset_size;
25465}
25466
25467/* See its dwarf2loc.h declaration. */
25468
25469int
25470dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25471{
25472 struct comp_unit_head cu_header_local;
25473 const struct comp_unit_head *cu_headerp;
25474
25475 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25476
25477 if (cu_headerp->version == 2)
25478 return cu_headerp->addr_size;
25479 else
25480 return cu_headerp->offset_size;
181cebd4
JK
25481}
25482
9aa1f1e3
TT
25483/* Return the text offset of the CU. The returned offset comes from
25484 this CU's objfile. If this objfile came from a separate debuginfo
25485 file, then the offset may be different from the corresponding
25486 offset in the parent objfile. */
25487
25488CORE_ADDR
25489dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25490{
b3b3bada 25491 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
25492}
25493
9a49df9d
AB
25494/* Return a type that is a generic pointer type, the size of which matches
25495 the address size given in the compilation unit header for PER_CU. */
25496static struct type *
25497dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25498{
25499 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25500 struct type *void_type = objfile_type (objfile)->builtin_void;
25501 struct type *addr_type = lookup_pointer_type (void_type);
25502 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25503
25504 if (TYPE_LENGTH (addr_type) == addr_size)
25505 return addr_type;
25506
25507 addr_type
25508 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25509 return addr_type;
25510}
25511
43988095
JK
25512/* Return DWARF version number of PER_CU. */
25513
25514short
25515dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25516{
25517 return per_cu->dwarf_version;
25518}
25519
348e048f
DE
25520/* Locate the .debug_info compilation unit from CU's objfile which contains
25521 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25522
25523static struct dwarf2_per_cu_data *
9c541725 25524dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25525 unsigned int offset_in_dwz,
ed2dc618 25526 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25527{
25528 struct dwarf2_per_cu_data *this_cu;
25529 int low, high;
25530
ae038cb0 25531 low = 0;
b76e467d 25532 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25533 while (high > low)
25534 {
36586728 25535 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25536 int mid = low + (high - low) / 2;
9a619af0 25537
36586728 25538 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25539 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25540 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25541 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25542 high = mid;
25543 else
25544 low = mid + 1;
25545 }
25546 gdb_assert (low == high);
36586728 25547 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25548 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25549 {
36586728 25550 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25551 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25552 "offset %s [in module %s]"),
25553 sect_offset_str (sect_off),
ed2dc618 25554 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25555
9c541725
PA
25556 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25557 <= sect_off);
ae038cb0
DJ
25558 return dwarf2_per_objfile->all_comp_units[low-1];
25559 }
25560 else
25561 {
b76e467d 25562 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25563 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25564 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25565 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25566 return this_cu;
25567 }
25568}
25569
23745b47 25570/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25571
fcd3b13d
SM
25572dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25573 : per_cu (per_cu_),
9068261f
AB
25574 mark (false),
25575 has_loclist (false),
25576 checked_producer (false),
25577 producer_is_gxx_lt_4_6 (false),
25578 producer_is_gcc_lt_4_3 (false),
eb77c9df 25579 producer_is_icc (false),
9068261f 25580 producer_is_icc_lt_14 (false),
c258c396 25581 producer_is_codewarrior (false),
9068261f 25582 processing_has_namespace_info (false)
93311388 25583{
fcd3b13d
SM
25584 per_cu->cu = this;
25585}
25586
25587/* Destroy a dwarf2_cu. */
25588
25589dwarf2_cu::~dwarf2_cu ()
25590{
25591 per_cu->cu = NULL;
9816fde3
JK
25592}
25593
25594/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25595
25596static void
95554aad
TT
25597prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25598 enum language pretend_language)
9816fde3
JK
25599{
25600 struct attribute *attr;
25601
25602 /* Set the language we're debugging. */
25603 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25604 if (attr != nullptr)
9816fde3
JK
25605 set_cu_language (DW_UNSND (attr), cu);
25606 else
9cded63f 25607 {
95554aad 25608 cu->language = pretend_language;
9cded63f
TT
25609 cu->language_defn = language_def (cu->language);
25610 }
dee91e82 25611
7d45c7c3 25612 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25613}
25614
ae038cb0
DJ
25615/* Increase the age counter on each cached compilation unit, and free
25616 any that are too old. */
25617
25618static void
ed2dc618 25619age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25620{
25621 struct dwarf2_per_cu_data *per_cu, **last_chain;
25622
25623 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25624 per_cu = dwarf2_per_objfile->read_in_chain;
25625 while (per_cu != NULL)
25626 {
25627 per_cu->cu->last_used ++;
b4f54984 25628 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25629 dwarf2_mark (per_cu->cu);
25630 per_cu = per_cu->cu->read_in_chain;
25631 }
25632
25633 per_cu = dwarf2_per_objfile->read_in_chain;
25634 last_chain = &dwarf2_per_objfile->read_in_chain;
25635 while (per_cu != NULL)
25636 {
25637 struct dwarf2_per_cu_data *next_cu;
25638
25639 next_cu = per_cu->cu->read_in_chain;
25640
25641 if (!per_cu->cu->mark)
25642 {
fcd3b13d 25643 delete per_cu->cu;
ae038cb0
DJ
25644 *last_chain = next_cu;
25645 }
25646 else
25647 last_chain = &per_cu->cu->read_in_chain;
25648
25649 per_cu = next_cu;
25650 }
25651}
25652
25653/* Remove a single compilation unit from the cache. */
25654
25655static void
dee91e82 25656free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25657{
25658 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25659 struct dwarf2_per_objfile *dwarf2_per_objfile
25660 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25661
25662 per_cu = dwarf2_per_objfile->read_in_chain;
25663 last_chain = &dwarf2_per_objfile->read_in_chain;
25664 while (per_cu != NULL)
25665 {
25666 struct dwarf2_per_cu_data *next_cu;
25667
25668 next_cu = per_cu->cu->read_in_chain;
25669
dee91e82 25670 if (per_cu == target_per_cu)
ae038cb0 25671 {
fcd3b13d 25672 delete per_cu->cu;
dee91e82 25673 per_cu->cu = NULL;
ae038cb0
DJ
25674 *last_chain = next_cu;
25675 break;
25676 }
25677 else
25678 last_chain = &per_cu->cu->read_in_chain;
25679
25680 per_cu = next_cu;
25681 }
25682}
25683
dee91e82
DE
25684/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25685 We store these in a hash table separate from the DIEs, and preserve them
25686 when the DIEs are flushed out of cache.
25687
25688 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25689 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25690 or the type may come from a DWO file. Furthermore, while it's more logical
25691 to use per_cu->section+offset, with Fission the section with the data is in
25692 the DWO file but we don't know that section at the point we need it.
25693 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25694 because we can enter the lookup routine, get_die_type_at_offset, from
25695 outside this file, and thus won't necessarily have PER_CU->cu.
25696 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25697
dee91e82 25698struct dwarf2_per_cu_offset_and_type
1c379e20 25699{
dee91e82 25700 const struct dwarf2_per_cu_data *per_cu;
9c541725 25701 sect_offset sect_off;
1c379e20
DJ
25702 struct type *type;
25703};
25704
dee91e82 25705/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25706
25707static hashval_t
dee91e82 25708per_cu_offset_and_type_hash (const void *item)
1c379e20 25709{
9a3c8263
SM
25710 const struct dwarf2_per_cu_offset_and_type *ofs
25711 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25712
9c541725 25713 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25714}
25715
dee91e82 25716/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25717
25718static int
dee91e82 25719per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25720{
9a3c8263
SM
25721 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25722 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25723 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25724 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25725
dee91e82 25726 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25727 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25728}
25729
25730/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25731 table if necessary. For convenience, return TYPE.
25732
25733 The DIEs reading must have careful ordering to:
85102364 25734 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25735 reading current DIE.
25736 * Not trying to dereference contents of still incompletely read in types
25737 while reading in other DIEs.
25738 * Enable referencing still incompletely read in types just by a pointer to
25739 the type without accessing its fields.
25740
25741 Therefore caller should follow these rules:
25742 * Try to fetch any prerequisite types we may need to build this DIE type
25743 before building the type and calling set_die_type.
e71ec853 25744 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25745 possible before fetching more types to complete the current type.
25746 * Make the type as complete as possible before fetching more types. */
1c379e20 25747
f792889a 25748static struct type *
1c379e20
DJ
25749set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25750{
518817b3
SM
25751 struct dwarf2_per_objfile *dwarf2_per_objfile
25752 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25753 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25754 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25755 struct attribute *attr;
25756 struct dynamic_prop prop;
1c379e20 25757
b4ba55a1
JB
25758 /* For Ada types, make sure that the gnat-specific data is always
25759 initialized (if not already set). There are a few types where
25760 we should not be doing so, because the type-specific area is
25761 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25762 where the type-specific area is used to store the floatformat).
25763 But this is not a problem, because the gnat-specific information
25764 is actually not needed for these types. */
25765 if (need_gnat_info (cu)
25766 && TYPE_CODE (type) != TYPE_CODE_FUNC
25767 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25768 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25769 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25770 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25771 && !HAVE_GNAT_AUX_INFO (type))
25772 INIT_GNAT_SPECIFIC (type);
25773
3f2f83dd
KB
25774 /* Read DW_AT_allocated and set in type. */
25775 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25776 if (attr_form_is_block (attr))
25777 {
9a49df9d
AB
25778 struct type *prop_type
25779 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25780 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25781 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25782 }
25783 else if (attr != NULL)
25784 {
b98664d3 25785 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25786 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25787 sect_offset_str (die->sect_off));
3f2f83dd
KB
25788 }
25789
25790 /* Read DW_AT_associated and set in type. */
25791 attr = dwarf2_attr (die, DW_AT_associated, cu);
25792 if (attr_form_is_block (attr))
25793 {
9a49df9d
AB
25794 struct type *prop_type
25795 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25796 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25797 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25798 }
25799 else if (attr != NULL)
25800 {
b98664d3 25801 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25802 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25803 sect_offset_str (die->sect_off));
3f2f83dd
KB
25804 }
25805
3cdcd0ce
JB
25806 /* Read DW_AT_data_location and set in type. */
25807 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25808 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25809 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25810 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25811
dee91e82 25812 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25813 {
dee91e82
DE
25814 dwarf2_per_objfile->die_type_hash =
25815 htab_create_alloc_ex (127,
25816 per_cu_offset_and_type_hash,
25817 per_cu_offset_and_type_eq,
25818 NULL,
25819 &objfile->objfile_obstack,
25820 hashtab_obstack_allocate,
25821 dummy_obstack_deallocate);
f792889a 25822 }
1c379e20 25823
dee91e82 25824 ofs.per_cu = cu->per_cu;
9c541725 25825 ofs.sect_off = die->sect_off;
1c379e20 25826 ofs.type = type;
dee91e82
DE
25827 slot = (struct dwarf2_per_cu_offset_and_type **)
25828 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25829 if (*slot)
b98664d3 25830 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25831 sect_offset_str (die->sect_off));
8d749320
SM
25832 *slot = XOBNEW (&objfile->objfile_obstack,
25833 struct dwarf2_per_cu_offset_and_type);
1c379e20 25834 **slot = ofs;
f792889a 25835 return type;
1c379e20
DJ
25836}
25837
9c541725 25838/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25839 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25840
25841static struct type *
9c541725 25842get_die_type_at_offset (sect_offset sect_off,
673bfd45 25843 struct dwarf2_per_cu_data *per_cu)
1c379e20 25844{
dee91e82 25845 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25846 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25847
dee91e82 25848 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25849 return NULL;
1c379e20 25850
dee91e82 25851 ofs.per_cu = per_cu;
9c541725 25852 ofs.sect_off = sect_off;
9a3c8263
SM
25853 slot = ((struct dwarf2_per_cu_offset_and_type *)
25854 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25855 if (slot)
25856 return slot->type;
25857 else
25858 return NULL;
25859}
25860
02142a6c 25861/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25862 or return NULL if DIE does not have a saved type. */
25863
25864static struct type *
25865get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25866{
9c541725 25867 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25868}
25869
10b3939b
DJ
25870/* Add a dependence relationship from CU to REF_PER_CU. */
25871
25872static void
25873dwarf2_add_dependence (struct dwarf2_cu *cu,
25874 struct dwarf2_per_cu_data *ref_per_cu)
25875{
25876 void **slot;
25877
25878 if (cu->dependencies == NULL)
25879 cu->dependencies
25880 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25881 NULL, &cu->comp_unit_obstack,
25882 hashtab_obstack_allocate,
25883 dummy_obstack_deallocate);
25884
25885 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25886 if (*slot == NULL)
25887 *slot = ref_per_cu;
25888}
1c379e20 25889
f504f079
DE
25890/* Subroutine of dwarf2_mark to pass to htab_traverse.
25891 Set the mark field in every compilation unit in the
ae038cb0
DJ
25892 cache that we must keep because we are keeping CU. */
25893
10b3939b
DJ
25894static int
25895dwarf2_mark_helper (void **slot, void *data)
25896{
25897 struct dwarf2_per_cu_data *per_cu;
25898
25899 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25900
25901 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25902 reading of the chain. As such dependencies remain valid it is not much
25903 useful to track and undo them during QUIT cleanups. */
25904 if (per_cu->cu == NULL)
25905 return 1;
25906
10b3939b
DJ
25907 if (per_cu->cu->mark)
25908 return 1;
9068261f 25909 per_cu->cu->mark = true;
10b3939b
DJ
25910
25911 if (per_cu->cu->dependencies != NULL)
25912 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25913
25914 return 1;
25915}
25916
f504f079
DE
25917/* Set the mark field in CU and in every other compilation unit in the
25918 cache that we must keep because we are keeping CU. */
25919
ae038cb0
DJ
25920static void
25921dwarf2_mark (struct dwarf2_cu *cu)
25922{
25923 if (cu->mark)
25924 return;
9068261f 25925 cu->mark = true;
10b3939b
DJ
25926 if (cu->dependencies != NULL)
25927 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25928}
25929
25930static void
25931dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25932{
25933 while (per_cu)
25934 {
9068261f 25935 per_cu->cu->mark = false;
ae038cb0
DJ
25936 per_cu = per_cu->cu->read_in_chain;
25937 }
72bf9492
DJ
25938}
25939
72bf9492
DJ
25940/* Trivial hash function for partial_die_info: the hash value of a DIE
25941 is its offset in .debug_info for this objfile. */
25942
25943static hashval_t
25944partial_die_hash (const void *item)
25945{
9a3c8263
SM
25946 const struct partial_die_info *part_die
25947 = (const struct partial_die_info *) item;
9a619af0 25948
9c541725 25949 return to_underlying (part_die->sect_off);
72bf9492
DJ
25950}
25951
25952/* Trivial comparison function for partial_die_info structures: two DIEs
25953 are equal if they have the same offset. */
25954
25955static int
25956partial_die_eq (const void *item_lhs, const void *item_rhs)
25957{
9a3c8263
SM
25958 const struct partial_die_info *part_die_lhs
25959 = (const struct partial_die_info *) item_lhs;
25960 const struct partial_die_info *part_die_rhs
25961 = (const struct partial_die_info *) item_rhs;
9a619af0 25962
9c541725 25963 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25964}
25965
3c3bb058
AB
25966struct cmd_list_element *set_dwarf_cmdlist;
25967struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25968
25969static void
981a3fb3 25970set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25971{
b4f54984 25972 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25973 gdb_stdout);
ae038cb0
DJ
25974}
25975
25976static void
981a3fb3 25977show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25978{
b4f54984 25979 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25980}
25981
491144b5 25982bool dwarf_always_disassemble;
437afbb8 25983
437afbb8 25984static void
cd4fb1b2
SM
25985show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25986 struct cmd_list_element *c, const char *value)
9291a0cd 25987{
cd4fb1b2
SM
25988 fprintf_filtered (file,
25989 _("Whether to always disassemble "
25990 "DWARF expressions is %s.\n"),
25991 value);
9291a0cd
TT
25992}
25993
9291a0cd 25994static void
cd4fb1b2
SM
25995show_check_physname (struct ui_file *file, int from_tty,
25996 struct cmd_list_element *c, const char *value)
9291a0cd 25997{
cd4fb1b2
SM
25998 fprintf_filtered (file,
25999 _("Whether to check \"physname\" is %s.\n"),
26000 value);
9291a0cd
TT
26001}
26002
6c265988 26003void _initialize_dwarf2_read ();
cd4fb1b2 26004void
6c265988 26005_initialize_dwarf2_read ()
9291a0cd 26006{
cd4fb1b2
SM
26007 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26008Set DWARF specific variables.\n\
590042fc 26009Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26010 &set_dwarf_cmdlist, "maintenance set dwarf ",
26011 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26012
cd4fb1b2 26013 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26014Show DWARF specific variables.\n\
26015Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26016 &show_dwarf_cmdlist, "maintenance show dwarf ",
26017 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26018
cd4fb1b2
SM
26019 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26020 &dwarf_max_cache_age, _("\
26021Set the upper bound on the age of cached DWARF compilation units."), _("\
26022Show the upper bound on the age of cached DWARF compilation units."), _("\
26023A higher limit means that cached compilation units will be stored\n\
26024in memory longer, and more total memory will be used. Zero disables\n\
26025caching, which can slow down startup."),
26026 NULL,
26027 show_dwarf_max_cache_age,
26028 &set_dwarf_cmdlist,
26029 &show_dwarf_cmdlist);
156942c7 26030
cd4fb1b2
SM
26031 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26032 &dwarf_always_disassemble, _("\
26033Set whether `info address' always disassembles DWARF expressions."), _("\
26034Show whether `info address' always disassembles DWARF expressions."), _("\
26035When enabled, DWARF expressions are always printed in an assembly-like\n\
26036syntax. When disabled, expressions will be printed in a more\n\
26037conversational style, when possible."),
26038 NULL,
26039 show_dwarf_always_disassemble,
26040 &set_dwarf_cmdlist,
26041 &show_dwarf_cmdlist);
9291a0cd 26042
cd4fb1b2
SM
26043 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26044Set debugging of the DWARF reader."), _("\
26045Show debugging of the DWARF reader."), _("\
26046When enabled (non-zero), debugging messages are printed during DWARF\n\
26047reading and symtab expansion. A value of 1 (one) provides basic\n\
26048information. A value greater than 1 provides more verbose information."),
26049 NULL,
26050 NULL,
26051 &setdebuglist, &showdebuglist);
9291a0cd 26052
cd4fb1b2
SM
26053 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26054Set debugging of the DWARF DIE reader."), _("\
26055Show debugging of the DWARF DIE reader."), _("\
26056When enabled (non-zero), DIEs are dumped after they are read in.\n\
26057The value is the maximum depth to print."),
26058 NULL,
26059 NULL,
26060 &setdebuglist, &showdebuglist);
9291a0cd 26061
cd4fb1b2
SM
26062 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26063Set debugging of the dwarf line reader."), _("\
26064Show debugging of the dwarf line reader."), _("\
26065When enabled (non-zero), line number entries are dumped as they are read in.\n\
26066A value of 1 (one) provides basic information.\n\
26067A value greater than 1 provides more verbose information."),
26068 NULL,
26069 NULL,
26070 &setdebuglist, &showdebuglist);
437afbb8 26071
cd4fb1b2
SM
26072 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26073Set cross-checking of \"physname\" code against demangler."), _("\
26074Show cross-checking of \"physname\" code against demangler."), _("\
26075When enabled, GDB's internal \"physname\" code is checked against\n\
26076the demangler."),
26077 NULL, show_check_physname,
26078 &setdebuglist, &showdebuglist);
900e11f9 26079
e615022a
DE
26080 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26081 no_class, &use_deprecated_index_sections, _("\
26082Set whether to use deprecated gdb_index sections."), _("\
26083Show whether to use deprecated gdb_index sections."), _("\
26084When enabled, deprecated .gdb_index sections are used anyway.\n\
26085Normally they are ignored either because of a missing feature or\n\
26086performance issue.\n\
26087Warning: This option must be enabled before gdb reads the file."),
26088 NULL,
26089 NULL,
26090 &setlist, &showlist);
26091
f1e6e072
TT
26092 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26093 &dwarf2_locexpr_funcs);
26094 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26095 &dwarf2_loclist_funcs);
26096
26097 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26098 &dwarf2_block_frame_base_locexpr_funcs);
26099 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26100 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26101
26102#if GDB_SELF_TEST
26103 selftests::register_test ("dw2_expand_symtabs_matching",
26104 selftests::dw2_expand_symtabs_matching::run_test);
26105#endif
6502dd73 26106}
This page took 5.801643 seconds and 4 git commands to generate.