Fix stop_reason reporting
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
11bc5fe4 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
ca9af5a1 4 Copyright (C) 2019-2020 Advanced Micro Devices, Inc. All rights reserved.
c906108c
SS
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 11 support.
c906108c 12
c5aa993b 13 This file is part of GDB.
c906108c 14
c5aa993b
JM
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
a9762ec7
JB
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
c906108c 19
a9762ec7
JB
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
c906108c 24
c5aa993b 25 You should have received a copy of the GNU General Public License
a9762ec7 26 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 27
21b2bd31
DE
28/* FIXME: Various die-reading functions need to be more careful with
29 reading off the end of the section.
30 E.g., load_partial_dies, read_partial_die. */
31
c906108c 32#include "defs.h"
4de283e4 33#include "dwarf2read.h"
d55e5aa6
TT
34#include "dwarf-index-cache.h"
35#include "dwarf-index-common.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" },
43988095 291 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 292 { ".debug_ranges", ".zdebug_ranges" },
43988095 293 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 294 { ".debug_types", ".zdebug_types" },
3019eac3 295 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
296 { ".debug_frame", ".zdebug_frame" },
297 { ".eh_frame", NULL },
24d3216f 298 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
299 { ".debug_names", ".zdebug_names" },
300 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 301 23
251d32d9 302};
c906108c 303
80626a55 304/* List of DWO/DWP sections. */
3019eac3 305
80626a55 306static const struct dwop_section_names
3019eac3
DE
307{
308 struct dwarf2_section_names abbrev_dwo;
309 struct dwarf2_section_names info_dwo;
310 struct dwarf2_section_names line_dwo;
311 struct dwarf2_section_names loc_dwo;
43988095 312 struct dwarf2_section_names loclists_dwo;
09262596
DE
313 struct dwarf2_section_names macinfo_dwo;
314 struct dwarf2_section_names macro_dwo;
3019eac3
DE
315 struct dwarf2_section_names str_dwo;
316 struct dwarf2_section_names str_offsets_dwo;
317 struct dwarf2_section_names types_dwo;
80626a55
DE
318 struct dwarf2_section_names cu_index;
319 struct dwarf2_section_names tu_index;
3019eac3 320}
80626a55 321dwop_section_names =
3019eac3
DE
322{
323 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
324 { ".debug_info.dwo", ".zdebug_info.dwo" },
325 { ".debug_line.dwo", ".zdebug_line.dwo" },
326 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 327 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
335};
336
c906108c
SS
337/* local data types */
338
107d2387
AC
339/* The data in a compilation unit header, after target2host
340 translation, looks like this. */
c906108c 341struct comp_unit_head
a738430d 342{
c764a876 343 unsigned int length;
a738430d 344 short version;
a738430d
MK
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
9c541725 347 sect_offset abbrev_sect_off;
57349743 348
a738430d
MK
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
57349743 351
a738430d
MK
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
57349743 354
43988095
JK
355 enum dwarf_unit_type unit_type;
356
a738430d
MK
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
9c541725 359 sect_offset sect_off;
57349743 360
d00adf39
DE
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
9c541725 363 cu_offset first_die_cu_offset;
43988095 364
a084a2a6
AT
365
366 /* 64-bit signature of this unit. For type units, it denotes the signature of
367 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
368 Also used in DWARF 5, to denote the dwo id when the unit type is
369 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
370 ULONGEST signature;
371
372 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 373 cu_offset type_cu_offset_in_tu;
a738430d 374};
c906108c 375
3da10d80
KS
376/* Type used for delaying computation of method physnames.
377 See comments for compute_delayed_physnames. */
378struct delayed_method_info
379{
380 /* The type to which the method is attached, i.e., its parent class. */
381 struct type *type;
382
383 /* The index of the method in the type's function fieldlists. */
384 int fnfield_index;
385
386 /* The index of the method in the fieldlist. */
387 int index;
388
389 /* The name of the DIE. */
390 const char *name;
391
392 /* The DIE associated with this method. */
393 struct die_info *die;
394};
395
e7c27a73
DJ
396/* Internal state when decoding a particular compilation unit. */
397struct dwarf2_cu
398{
fcd3b13d
SM
399 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
400 ~dwarf2_cu ();
401
402 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403
c24bdb02
KS
404 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
405 Create the set of symtabs used by this TU, or if this TU is sharing
406 symtabs with another TU and the symtabs have already been created
407 then restore those symtabs in the line header.
408 We don't need the pc/line-number mapping for type units. */
409 void setup_type_unit_groups (struct die_info *die);
410
411 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
412 buildsym_compunit constructor. */
413 struct compunit_symtab *start_symtab (const char *name,
414 const char *comp_dir,
415 CORE_ADDR low_pc);
416
417 /* Reset the builder. */
418 void reset_builder () { m_builder.reset (); }
419
d00adf39 420 /* The header of the compilation unit. */
fcd3b13d 421 struct comp_unit_head header {};
e142c38c 422
d00adf39 423 /* Base address of this compilation unit. */
fcd3b13d 424 CORE_ADDR base_address = 0;
d00adf39
DE
425
426 /* Non-zero if base_address has been set. */
fcd3b13d 427 int base_known = 0;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
510
511 /* The DW_AT_addr_base attribute if present, zero otherwise
512 (zero is a valid value though).
1dbab08b 513 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 514 ULONGEST addr_base = 0;
3019eac3 515
2e3cf129
DE
516 /* The DW_AT_ranges_base attribute if present, zero otherwise
517 (zero is a valid value though).
1dbab08b 518 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 519 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
520 be used without needing to know whether DWO files are in use or not.
521 N.B. This does not apply to DW_AT_ranges appearing in
522 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
523 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
524 DW_AT_ranges_base *would* have to be applied, and we'd have to care
525 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 526 ULONGEST ranges_base = 0;
2e3cf129 527
c9317f21
TT
528 /* When reading debug info generated by older versions of rustc, we
529 have to rewrite some union types to be struct types with a
530 variant part. This rewriting must be done after the CU is fully
531 read in, because otherwise at the point of rewriting some struct
532 type might not have been fully processed. So, we keep a list of
533 all such types here and process them after expansion. */
534 std::vector<struct type *> rust_unions;
535
ae038cb0 536 /* Mark used when releasing cached dies. */
9068261f 537 bool mark : 1;
ae038cb0 538
8be455d7
JK
539 /* This CU references .debug_loc. See the symtab->locations_valid field.
540 This test is imperfect as there may exist optimized debug code not using
541 any location list and still facing inlining issues if handled as
542 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 543 bool has_loclist : 1;
ba919b58 544
9068261f 545 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
546 if all the producer_is_* fields are valid. This information is cached
547 because profiling CU expansion showed excessive time spent in
548 producer_is_gxx_lt_4_6. */
9068261f
AB
549 bool checked_producer : 1;
550 bool producer_is_gxx_lt_4_6 : 1;
551 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 552 bool producer_is_icc : 1;
9068261f 553 bool producer_is_icc_lt_14 : 1;
c258c396 554 bool producer_is_codewarrior : 1;
4d4ec4e5 555
9068261f 556 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
557 debugging info for C++ namespaces. GCC 3.3.x did not produce
558 this information, but later versions do. */
559
9068261f 560 bool processing_has_namespace_info : 1;
d590ff25
YQ
561
562 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
563
564 /* If this CU was inherited by another CU (via specification,
565 abstract_origin, etc), this is the ancestor CU. */
566 dwarf2_cu *ancestor;
567
568 /* Get the buildsym_compunit for this CU. */
569 buildsym_compunit *get_builder ()
570 {
571 /* If this CU has a builder associated with it, use that. */
572 if (m_builder != nullptr)
573 return m_builder.get ();
574
575 /* Otherwise, search ancestors for a valid builder. */
576 if (ancestor != nullptr)
577 return ancestor->get_builder ();
578
579 return nullptr;
580 }
e7c27a73
DJ
581};
582
094b34ac
DE
583/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
584 This includes type_unit_group and quick_file_names. */
585
586struct stmt_list_hash
587{
588 /* The DWO unit this table is from or NULL if there is none. */
589 struct dwo_unit *dwo_unit;
590
591 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 592 sect_offset line_sect_off;
094b34ac
DE
593};
594
f4dc4d17
DE
595/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
596 an object of this type. */
597
598struct type_unit_group
599{
0186c6a7 600 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
601 To simplify things we create an artificial CU that "includes" all the
602 type units using this stmt_list so that the rest of the code still has
603 a "per_cu" handle on the symtab.
604 This PER_CU is recognized by having no section. */
8a0459fd 605#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
606 struct dwarf2_per_cu_data per_cu;
607
0186c6a7
DE
608 /* The TUs that share this DW_AT_stmt_list entry.
609 This is added to while parsing type units to build partial symtabs,
610 and is deleted afterwards and not used again. */
a8b3b8e9 611 std::vector<signatured_type *> *tus;
f4dc4d17 612
43f3e411 613 /* The compunit symtab.
094b34ac 614 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
615 so we create an essentially anonymous symtab as the compunit symtab. */
616 struct compunit_symtab *compunit_symtab;
f4dc4d17 617
094b34ac
DE
618 /* The data used to construct the hash key. */
619 struct stmt_list_hash hash;
f4dc4d17
DE
620
621 /* The number of symtabs from the line header.
622 The value here must match line_header.num_file_names. */
623 unsigned int num_symtabs;
624
625 /* The symbol tables for this TU (obtained from the files listed in
626 DW_AT_stmt_list).
627 WARNING: The order of entries here must match the order of entries
628 in the line header. After the first TU using this type_unit_group, the
629 line header for the subsequent TUs is recreated from this. This is done
630 because we need to use the same symtabs for each TU using the same
631 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
632 there's no guarantee the line header doesn't have duplicate entries. */
633 struct symtab **symtabs;
634};
635
73869dc2 636/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
637
638struct dwo_sections
639{
640 struct dwarf2_section_info abbrev;
3019eac3
DE
641 struct dwarf2_section_info line;
642 struct dwarf2_section_info loc;
43988095 643 struct dwarf2_section_info loclists;
09262596
DE
644 struct dwarf2_section_info macinfo;
645 struct dwarf2_section_info macro;
3019eac3
DE
646 struct dwarf2_section_info str;
647 struct dwarf2_section_info str_offsets;
80626a55
DE
648 /* In the case of a virtual DWO file, these two are unused. */
649 struct dwarf2_section_info info;
fd5866f6 650 std::vector<dwarf2_section_info> types;
3019eac3
DE
651};
652
c88ee1f0 653/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
654
655struct dwo_unit
656{
657 /* Backlink to the containing struct dwo_file. */
658 struct dwo_file *dwo_file;
659
660 /* The "id" that distinguishes this CU/TU.
661 .debug_info calls this "dwo_id", .debug_types calls this "signature".
662 Since signatures came first, we stick with it for consistency. */
663 ULONGEST signature;
664
665 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 666 struct dwarf2_section_info *section;
3019eac3 667
9c541725
PA
668 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
669 sect_offset sect_off;
3019eac3
DE
670 unsigned int length;
671
672 /* For types, offset in the type's DIE of the type defined by this TU. */
673 cu_offset type_offset_in_tu;
674};
675
73869dc2
DE
676/* include/dwarf2.h defines the DWP section codes.
677 It defines a max value but it doesn't define a min value, which we
678 use for error checking, so provide one. */
679
680enum dwp_v2_section_ids
681{
682 DW_SECT_MIN = 1
683};
684
80626a55 685/* Data for one DWO file.
57d63ce2
DE
686
687 This includes virtual DWO files (a virtual DWO file is a DWO file as it
688 appears in a DWP file). DWP files don't really have DWO files per se -
689 comdat folding of types "loses" the DWO file they came from, and from
690 a high level view DWP files appear to contain a mass of random types.
691 However, to maintain consistency with the non-DWP case we pretend DWP
692 files contain virtual DWO files, and we assign each TU with one virtual
693 DWO file (generally based on the line and abbrev section offsets -
694 a heuristic that seems to work in practice). */
3019eac3
DE
695
696struct dwo_file
697{
51ac9db5
SM
698 dwo_file () = default;
699 DISABLE_COPY_AND_ASSIGN (dwo_file);
700
0ac5b59e 701 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
702 For virtual DWO files the name is constructed from the section offsets
703 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
704 from related CU+TUs. */
51ac9db5 705 const char *dwo_name = nullptr;
0ac5b59e
DE
706
707 /* The DW_AT_comp_dir attribute. */
51ac9db5 708 const char *comp_dir = nullptr;
3019eac3 709
80626a55
DE
710 /* The bfd, when the file is open. Otherwise this is NULL.
711 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 712 gdb_bfd_ref_ptr dbfd;
3019eac3 713
73869dc2
DE
714 /* The sections that make up this DWO file.
715 Remember that for virtual DWO files in DWP V2, these are virtual
716 sections (for lack of a better name). */
51ac9db5 717 struct dwo_sections sections {};
3019eac3 718
33c5cd75
DB
719 /* The CUs in the file.
720 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
721 an extension to handle LLVM's Link Time Optimization output (where
722 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 723 htab_t cus {};
3019eac3
DE
724
725 /* Table of TUs in the file.
726 Each element is a struct dwo_unit. */
51ac9db5 727 htab_t tus {};
3019eac3
DE
728};
729
80626a55
DE
730/* These sections are what may appear in a DWP file. */
731
732struct dwp_sections
733{
73869dc2 734 /* These are used by both DWP version 1 and 2. */
80626a55
DE
735 struct dwarf2_section_info str;
736 struct dwarf2_section_info cu_index;
737 struct dwarf2_section_info tu_index;
73869dc2
DE
738
739 /* These are only used by DWP version 2 files.
740 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
741 sections are referenced by section number, and are not recorded here.
742 In DWP version 2 there is at most one copy of all these sections, each
743 section being (effectively) comprised of the concatenation of all of the
744 individual sections that exist in the version 1 format.
745 To keep the code simple we treat each of these concatenated pieces as a
746 section itself (a virtual section?). */
747 struct dwarf2_section_info abbrev;
748 struct dwarf2_section_info info;
749 struct dwarf2_section_info line;
750 struct dwarf2_section_info loc;
751 struct dwarf2_section_info macinfo;
752 struct dwarf2_section_info macro;
753 struct dwarf2_section_info str_offsets;
754 struct dwarf2_section_info types;
80626a55
DE
755};
756
73869dc2
DE
757/* These sections are what may appear in a virtual DWO file in DWP version 1.
758 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 759
73869dc2 760struct virtual_v1_dwo_sections
80626a55
DE
761{
762 struct dwarf2_section_info abbrev;
763 struct dwarf2_section_info line;
764 struct dwarf2_section_info loc;
765 struct dwarf2_section_info macinfo;
766 struct dwarf2_section_info macro;
767 struct dwarf2_section_info str_offsets;
768 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 769 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
770 struct dwarf2_section_info info_or_types;
771};
772
73869dc2
DE
773/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
774 In version 2, the sections of the DWO files are concatenated together
775 and stored in one section of that name. Thus each ELF section contains
776 several "virtual" sections. */
777
778struct virtual_v2_dwo_sections
779{
780 bfd_size_type abbrev_offset;
781 bfd_size_type abbrev_size;
782
783 bfd_size_type line_offset;
784 bfd_size_type line_size;
785
786 bfd_size_type loc_offset;
787 bfd_size_type loc_size;
788
789 bfd_size_type macinfo_offset;
790 bfd_size_type macinfo_size;
791
792 bfd_size_type macro_offset;
793 bfd_size_type macro_size;
794
795 bfd_size_type str_offsets_offset;
796 bfd_size_type str_offsets_size;
797
798 /* Each DWP hash table entry records one CU or one TU.
799 That is recorded here, and copied to dwo_unit.section. */
800 bfd_size_type info_or_types_offset;
801 bfd_size_type info_or_types_size;
802};
803
80626a55
DE
804/* Contents of DWP hash tables. */
805
806struct dwp_hash_table
807{
73869dc2 808 uint32_t version, nr_columns;
80626a55 809 uint32_t nr_units, nr_slots;
73869dc2
DE
810 const gdb_byte *hash_table, *unit_table;
811 union
812 {
813 struct
814 {
815 const gdb_byte *indices;
816 } v1;
817 struct
818 {
819 /* This is indexed by column number and gives the id of the section
820 in that column. */
821#define MAX_NR_V2_DWO_SECTIONS \
822 (1 /* .debug_info or .debug_types */ \
823 + 1 /* .debug_abbrev */ \
824 + 1 /* .debug_line */ \
825 + 1 /* .debug_loc */ \
826 + 1 /* .debug_str_offsets */ \
827 + 1 /* .debug_macro or .debug_macinfo */)
828 int section_ids[MAX_NR_V2_DWO_SECTIONS];
829 const gdb_byte *offsets;
830 const gdb_byte *sizes;
831 } v2;
832 } section_pool;
80626a55
DE
833};
834
835/* Data for one DWP file. */
836
837struct dwp_file
838{
400174b1
TT
839 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
840 : name (name_),
841 dbfd (std::move (abfd))
842 {
843 }
844
80626a55
DE
845 /* Name of the file. */
846 const char *name;
847
73869dc2 848 /* File format version. */
400174b1 849 int version = 0;
73869dc2 850
93417882 851 /* The bfd. */
400174b1 852 gdb_bfd_ref_ptr dbfd;
80626a55
DE
853
854 /* Section info for this file. */
400174b1 855 struct dwp_sections sections {};
80626a55 856
57d63ce2 857 /* Table of CUs in the file. */
400174b1 858 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
859
860 /* Table of TUs in the file. */
400174b1 861 const struct dwp_hash_table *tus = nullptr;
80626a55 862
19ac8c2e 863 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
864 htab_t loaded_cus {};
865 htab_t loaded_tus {};
80626a55 866
73869dc2
DE
867 /* Table to map ELF section numbers to their sections.
868 This is only needed for the DWP V1 file format. */
400174b1
TT
869 unsigned int num_sections = 0;
870 asection **elf_sections = nullptr;
80626a55
DE
871};
872
0963b4bd
MS
873/* Struct used to pass misc. parameters to read_die_and_children, et
874 al. which are used for both .debug_info and .debug_types dies.
875 All parameters here are unchanging for the life of the call. This
dee91e82 876 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
877
878struct die_reader_specs
879{
a32a8923 880 /* The bfd of die_section. */
93311388
DE
881 bfd* abfd;
882
883 /* The CU of the DIE we are parsing. */
884 struct dwarf2_cu *cu;
885
80626a55 886 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
887 struct dwo_file *dwo_file;
888
dee91e82 889 /* The section the die comes from.
3019eac3 890 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
891 struct dwarf2_section_info *die_section;
892
893 /* die_section->buffer. */
d521ce57 894 const gdb_byte *buffer;
f664829e
DE
895
896 /* The end of the buffer. */
897 const gdb_byte *buffer_end;
a2ce51a0
DE
898
899 /* The value of the DW_AT_comp_dir attribute. */
900 const char *comp_dir;
685af9cd
TT
901
902 /* The abbreviation table to use when reading the DIEs. */
903 struct abbrev_table *abbrev_table;
93311388
DE
904};
905
fd820528 906/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 907typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 908 const gdb_byte *info_ptr,
dee91e82
DE
909 struct die_info *comp_unit_die,
910 int has_children,
911 void *data);
912
7ba99d21
AT
913/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
914 later. */
915typedef int dir_index;
ecfb656c 916
7ba99d21
AT
917/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
918 and later. */
919typedef int file_name_index;
ecfb656c 920
52059ffd
TT
921struct file_entry
922{
fff8551c
PA
923 file_entry () = default;
924
ecfb656c 925 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
926 unsigned int mod_time_, unsigned int length_)
927 : name (name_),
ecfb656c 928 d_index (d_index_),
fff8551c
PA
929 mod_time (mod_time_),
930 length (length_)
931 {}
932
ecfb656c
PA
933 /* Return the include directory at D_INDEX stored in LH. Returns
934 NULL if D_INDEX is out of bounds. */
8c43009f
PA
935 const char *include_dir (const line_header *lh) const;
936
fff8551c
PA
937 /* The file name. Note this is an observing pointer. The memory is
938 owned by debug_line_buffer. */
939 const char *name {};
940
8c43009f 941 /* The directory index (1-based). */
ecfb656c 942 dir_index d_index {};
fff8551c
PA
943
944 unsigned int mod_time {};
945
946 unsigned int length {};
947
948 /* True if referenced by the Line Number Program. */
949 bool included_p {};
950
83769d0b 951 /* The associated symbol table, if any. */
fff8551c 952 struct symtab *symtab {};
52059ffd
TT
953};
954
debd256d
JB
955/* The line number information for a compilation unit (found in the
956 .debug_line section) begins with a "statement program header",
957 which contains the following information. */
958struct line_header
959{
fff8551c
PA
960 line_header ()
961 : offset_in_dwz {}
962 {}
963
964 /* Add an entry to the include directory table. */
965 void add_include_dir (const char *include_dir);
966
967 /* Add an entry to the file name table. */
ecfb656c 968 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
969 unsigned int mod_time, unsigned int length);
970
7ba99d21
AT
971 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
972 Returns NULL if INDEX is out of bounds. */
ecfb656c 973 const char *include_dir_at (dir_index index) const
8c43009f 974 {
7ba99d21
AT
975 int vec_index;
976 if (version >= 5)
977 vec_index = index;
978 else
979 vec_index = index - 1;
980 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 981 return NULL;
7ba99d21 982 return m_include_dirs[vec_index];
8c43009f
PA
983 }
984
7ba99d21 985 bool is_valid_file_index (int file_index)
8c43009f 986 {
7ba99d21
AT
987 if (version >= 5)
988 return 0 <= file_index && file_index < file_names_size ();
989 return 1 <= file_index && file_index <= file_names_size ();
990 }
ecfb656c 991
7ba99d21
AT
992 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
993 Returns NULL if INDEX is out of bounds. */
994 file_entry *file_name_at (file_name_index index)
995 {
996 int vec_index;
997 if (version >= 5)
998 vec_index = index;
999 else
1000 vec_index = index - 1;
1001 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1002 return NULL;
7ba99d21 1003 return &m_file_names[vec_index];
fff8551c
PA
1004 }
1005
7ba99d21
AT
1006 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1007 this method should only be used to iterate through all file entries in an
1008 index-agnostic manner. */
1009 std::vector<file_entry> &file_names ()
1010 { return m_file_names; }
1011
527f3840 1012 /* Offset of line number information in .debug_line section. */
9c541725 1013 sect_offset sect_off {};
527f3840
JK
1014
1015 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1016 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1017
1018 unsigned int total_length {};
1019 unsigned short version {};
1020 unsigned int header_length {};
1021 unsigned char minimum_instruction_length {};
1022 unsigned char maximum_ops_per_instruction {};
1023 unsigned char default_is_stmt {};
1024 int line_base {};
1025 unsigned char line_range {};
1026 unsigned char opcode_base {};
debd256d
JB
1027
1028 /* standard_opcode_lengths[i] is the number of operands for the
1029 standard opcode whose value is i. This means that
1030 standard_opcode_lengths[0] is unused, and the last meaningful
1031 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1032 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1033
7ba99d21
AT
1034 int file_names_size ()
1035 { return m_file_names.size(); }
debd256d
JB
1036
1037 /* The start and end of the statement program following this
6502dd73 1038 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1039 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1040
1041 private:
1042 /* The include_directories table. Note these are observing
1043 pointers. The memory is owned by debug_line_buffer. */
1044 std::vector<const char *> m_include_dirs;
1045
1046 /* The file_names table. This is private because the meaning of indexes
1047 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1048 before, and is 0 in DWARF 5 and later). So the client should use
1049 file_name_at method for access. */
1050 std::vector<file_entry> m_file_names;
debd256d 1051};
c906108c 1052
fff8551c
PA
1053typedef std::unique_ptr<line_header> line_header_up;
1054
8c43009f
PA
1055const char *
1056file_entry::include_dir (const line_header *lh) const
1057{
ecfb656c 1058 return lh->include_dir_at (d_index);
8c43009f
PA
1059}
1060
c906108c 1061/* When we construct a partial symbol table entry we only
0963b4bd 1062 need this much information. */
6f06d47b 1063struct partial_die_info : public allocate_on_obstack
c906108c 1064 {
6f06d47b
YQ
1065 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1066
1067 /* Disable assign but still keep copy ctor, which is needed
1068 load_partial_dies. */
1069 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1070
52356b79
YQ
1071 /* Adjust the partial die before generating a symbol for it. This
1072 function may set the is_external flag or change the DIE's
1073 name. */
1074 void fixup (struct dwarf2_cu *cu);
1075
48fbe735
YQ
1076 /* Read a minimal amount of information into the minimal die
1077 structure. */
1078 const gdb_byte *read (const struct die_reader_specs *reader,
1079 const struct abbrev_info &abbrev,
1080 const gdb_byte *info_ptr);
1081
72bf9492 1082 /* Offset of this DIE. */
6f06d47b 1083 const sect_offset sect_off;
72bf9492
DJ
1084
1085 /* DWARF-2 tag for this DIE. */
6f06d47b 1086 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1087
72bf9492 1088 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1089 const unsigned int has_children : 1;
1090
72bf9492
DJ
1091 unsigned int is_external : 1;
1092 unsigned int is_declaration : 1;
1093 unsigned int has_type : 1;
1094 unsigned int has_specification : 1;
1095 unsigned int has_pc_info : 1;
481860b3 1096 unsigned int may_be_inlined : 1;
72bf9492 1097
0c1b455e
TT
1098 /* This DIE has been marked DW_AT_main_subprogram. */
1099 unsigned int main_subprogram : 1;
1100
72bf9492
DJ
1101 /* Flag set if the SCOPE field of this structure has been
1102 computed. */
1103 unsigned int scope_set : 1;
1104
fa4028e9
JB
1105 /* Flag set if the DIE has a byte_size attribute. */
1106 unsigned int has_byte_size : 1;
1107
ff908ebf
AW
1108 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1109 unsigned int has_const_value : 1;
1110
98bfdba5
PA
1111 /* Flag set if any of the DIE's children are template arguments. */
1112 unsigned int has_template_arguments : 1;
1113
52356b79 1114 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1115 unsigned int fixup_called : 1;
1116
36586728
TT
1117 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1118 unsigned int is_dwz : 1;
1119
1120 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1121 unsigned int spec_is_dwz : 1;
1122
72bf9492 1123 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1124 sometimes a default name for unnamed DIEs. */
6f06d47b 1125 const char *name = nullptr;
72bf9492 1126
abc72ce4 1127 /* The linkage name, if present. */
6f06d47b 1128 const char *linkage_name = nullptr;
abc72ce4 1129
72bf9492
DJ
1130 /* The scope to prepend to our children. This is generally
1131 allocated on the comp_unit_obstack, so will disappear
1132 when this compilation unit leaves the cache. */
6f06d47b 1133 const char *scope = nullptr;
72bf9492 1134
95554aad
TT
1135 /* Some data associated with the partial DIE. The tag determines
1136 which field is live. */
1137 union
1138 {
1139 /* The location description associated with this DIE, if any. */
1140 struct dwarf_block *locdesc;
1141 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1142 sect_offset sect_off;
6f06d47b 1143 } d {};
72bf9492
DJ
1144
1145 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1146 CORE_ADDR lowpc = 0;
1147 CORE_ADDR highpc = 0;
72bf9492 1148
93311388 1149 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1150 DW_AT_sibling, if any. */
48fbe735
YQ
1151 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1152 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1153 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1154
1155 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1156 DW_AT_specification (or DW_AT_abstract_origin or
1157 DW_AT_extension). */
6f06d47b 1158 sect_offset spec_offset {};
72bf9492
DJ
1159
1160 /* Pointers to this DIE's parent, first child, and next sibling,
1161 if any. */
6f06d47b
YQ
1162 struct partial_die_info *die_parent = nullptr;
1163 struct partial_die_info *die_child = nullptr;
1164 struct partial_die_info *die_sibling = nullptr;
1165
1166 friend struct partial_die_info *
1167 dwarf2_cu::find_partial_die (sect_offset sect_off);
1168
1169 private:
1170 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1171 partial_die_info (sect_offset sect_off)
1172 : partial_die_info (sect_off, DW_TAG_padding, 0)
1173 {
1174 }
1175
1176 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1177 int has_children_)
1178 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1179 {
1180 is_external = 0;
1181 is_declaration = 0;
1182 has_type = 0;
1183 has_specification = 0;
1184 has_pc_info = 0;
1185 may_be_inlined = 0;
1186 main_subprogram = 0;
1187 scope_set = 0;
1188 has_byte_size = 0;
1189 has_const_value = 0;
1190 has_template_arguments = 0;
1191 fixup_called = 0;
1192 is_dwz = 0;
1193 spec_is_dwz = 0;
1194 }
c906108c
SS
1195 };
1196
0963b4bd 1197/* This data structure holds the information of an abbrev. */
c906108c
SS
1198struct abbrev_info
1199 {
1200 unsigned int number; /* number identifying abbrev */
1201 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1202 unsigned short has_children; /* boolean */
1203 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1204 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1205 struct abbrev_info *next; /* next in chain */
1206 };
1207
1208struct attr_abbrev
1209 {
9d25dd43
DE
1210 ENUM_BITFIELD(dwarf_attribute) name : 16;
1211 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1212
1213 /* It is valid only if FORM is DW_FORM_implicit_const. */
1214 LONGEST implicit_const;
c906108c
SS
1215 };
1216
433df2d4
DE
1217/* Size of abbrev_table.abbrev_hash_table. */
1218#define ABBREV_HASH_SIZE 121
1219
1220/* Top level data structure to contain an abbreviation table. */
1221
1222struct abbrev_table
1223{
685af9cd
TT
1224 explicit abbrev_table (sect_offset off)
1225 : sect_off (off)
1226 {
4a17f768 1227 m_abbrevs =
685af9cd 1228 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1229 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1230 }
1231
1232 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1233
1234 /* Allocate space for a struct abbrev_info object in
1235 ABBREV_TABLE. */
1236 struct abbrev_info *alloc_abbrev ();
1237
1238 /* Add an abbreviation to the table. */
1239 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1240
1241 /* Look up an abbrev in the table.
1242 Returns NULL if the abbrev is not found. */
1243
1244 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1245
1246
f4dc4d17
DE
1247 /* Where the abbrev table came from.
1248 This is used as a sanity check when the table is used. */
685af9cd 1249 const sect_offset sect_off;
433df2d4
DE
1250
1251 /* Storage for the abbrev table. */
685af9cd 1252 auto_obstack abbrev_obstack;
433df2d4 1253
4a17f768
YQ
1254private:
1255
433df2d4
DE
1256 /* Hash table of abbrevs.
1257 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1258 It could be statically allocated, but the previous code didn't so we
1259 don't either. */
4a17f768 1260 struct abbrev_info **m_abbrevs;
433df2d4
DE
1261};
1262
685af9cd
TT
1263typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1264
0963b4bd 1265/* Attributes have a name and a value. */
b60c80d6
DJ
1266struct attribute
1267 {
9d25dd43 1268 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1269 ENUM_BITFIELD(dwarf_form) form : 15;
1270
1271 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1272 field should be in u.str (existing only for DW_STRING) but it is kept
1273 here for better struct attribute alignment. */
1274 unsigned int string_is_canonical : 1;
1275
b60c80d6
DJ
1276 union
1277 {
15d034d0 1278 const char *str;
b60c80d6 1279 struct dwarf_block *blk;
43bbcdc2
PH
1280 ULONGEST unsnd;
1281 LONGEST snd;
b60c80d6 1282 CORE_ADDR addr;
ac9ec31b 1283 ULONGEST signature;
b60c80d6
DJ
1284 }
1285 u;
1286 };
1287
0963b4bd 1288/* This data structure holds a complete die structure. */
c906108c
SS
1289struct die_info
1290 {
76815b17
DE
1291 /* DWARF-2 tag for this DIE. */
1292 ENUM_BITFIELD(dwarf_tag) tag : 16;
1293
1294 /* Number of attributes */
98bfdba5
PA
1295 unsigned char num_attrs;
1296
1297 /* True if we're presently building the full type name for the
1298 type derived from this DIE. */
1299 unsigned char building_fullname : 1;
76815b17 1300
adde2bff
DE
1301 /* True if this die is in process. PR 16581. */
1302 unsigned char in_process : 1;
1303
76815b17
DE
1304 /* Abbrev number */
1305 unsigned int abbrev;
1306
93311388 1307 /* Offset in .debug_info or .debug_types section. */
9c541725 1308 sect_offset sect_off;
78ba4af6
JB
1309
1310 /* The dies in a compilation unit form an n-ary tree. PARENT
1311 points to this die's parent; CHILD points to the first child of
1312 this node; and all the children of a given node are chained
4950bc1c 1313 together via their SIBLING fields. */
639d11d3
DC
1314 struct die_info *child; /* Its first child, if any. */
1315 struct die_info *sibling; /* Its next sibling, if any. */
1316 struct die_info *parent; /* Its parent, if any. */
c906108c 1317
b60c80d6
DJ
1318 /* An array of attributes, with NUM_ATTRS elements. There may be
1319 zero, but it's not common and zero-sized arrays are not
1320 sufficiently portable C. */
1321 struct attribute attrs[1];
c906108c
SS
1322 };
1323
0963b4bd 1324/* Get at parts of an attribute structure. */
c906108c
SS
1325
1326#define DW_STRING(attr) ((attr)->u.str)
8285870a 1327#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1328#define DW_UNSND(attr) ((attr)->u.unsnd)
1329#define DW_BLOCK(attr) ((attr)->u.blk)
1330#define DW_SND(attr) ((attr)->u.snd)
1331#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1332#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1333
0963b4bd 1334/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1335struct dwarf_block
1336 {
56eb65bd 1337 size_t size;
1d6edc3c
JK
1338
1339 /* Valid only if SIZE is not zero. */
d521ce57 1340 const gdb_byte *data;
c906108c
SS
1341 };
1342
c906108c
SS
1343#ifndef ATTR_ALLOC_CHUNK
1344#define ATTR_ALLOC_CHUNK 4
1345#endif
1346
c906108c
SS
1347/* Allocate fields for structs, unions and enums in this size. */
1348#ifndef DW_FIELD_ALLOC_CHUNK
1349#define DW_FIELD_ALLOC_CHUNK 4
1350#endif
1351
c906108c
SS
1352/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1353 but this would require a corresponding change in unpack_field_as_long
1354 and friends. */
1355static int bits_per_byte = 8;
1356
2ddeaf8a
TT
1357/* When reading a variant or variant part, we track a bit more
1358 information about the field, and store it in an object of this
1359 type. */
1360
1361struct variant_field
1362{
1363 /* If we see a DW_TAG_variant, then this will be the discriminant
1364 value. */
1365 ULONGEST discriminant_value;
1366 /* If we see a DW_TAG_variant, then this will be set if this is the
1367 default branch. */
1368 bool default_branch;
1369 /* While reading a DW_TAG_variant_part, this will be set if this
1370 field is the discriminant. */
1371 bool is_discriminant;
1372};
1373
52059ffd
TT
1374struct nextfield
1375{
be2daae6
TT
1376 int accessibility = 0;
1377 int virtuality = 0;
2ddeaf8a 1378 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1379 struct variant_field variant {};
1380 struct field field {};
52059ffd
TT
1381};
1382
1383struct fnfieldlist
1384{
be2daae6
TT
1385 const char *name = nullptr;
1386 std::vector<struct fn_field> fnfields;
52059ffd
TT
1387};
1388
c906108c
SS
1389/* The routines that read and process dies for a C struct or C++ class
1390 pass lists of data member fields and lists of member function fields
1391 in an instance of a field_info structure, as defined below. */
1392struct field_info
c5aa993b 1393 {
0963b4bd 1394 /* List of data member and baseclasses fields. */
be2daae6
TT
1395 std::vector<struct nextfield> fields;
1396 std::vector<struct nextfield> baseclasses;
c906108c 1397
7d0ccb61 1398 /* Number of fields (including baseclasses). */
be2daae6 1399 int nfields = 0;
c906108c 1400
85102364 1401 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1402 int non_public_fields = 0;
c906108c 1403
c5aa993b
JM
1404 /* Member function fieldlist array, contains name of possibly overloaded
1405 member function, number of overloaded member functions and a pointer
1406 to the head of the member function field chain. */
be2daae6 1407 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1408
1409 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1410 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1411 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1412
1413 /* Nested types defined by this class and the number of elements in this
1414 list. */
be2daae6 1415 std::vector<struct decl_field> nested_types_list;
c5aa993b 1416 };
c906108c 1417
10b3939b
DJ
1418/* One item on the queue of compilation units to read in full symbols
1419 for. */
1420struct dwarf2_queue_item
1421{
1422 struct dwarf2_per_cu_data *per_cu;
95554aad 1423 enum language pretend_language;
10b3939b
DJ
1424 struct dwarf2_queue_item *next;
1425};
1426
1427/* The current queue. */
1428static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1429
ae038cb0
DJ
1430/* Loaded secondary compilation units are kept in memory until they
1431 have not been referenced for the processing of this many
1432 compilation units. Set this to zero to disable caching. Cache
1433 sizes of up to at least twenty will improve startup time for
1434 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1435static int dwarf_max_cache_age = 5;
920d2a44 1436static void
b4f54984
DE
1437show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1438 struct cmd_list_element *c, const char *value)
920d2a44 1439{
3e43a32a 1440 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1441 "DWARF compilation units is %s.\n"),
920d2a44
AC
1442 value);
1443}
4390d890 1444\f
c906108c
SS
1445/* local function prototypes */
1446
a32a8923
DE
1447static const char *get_section_name (const struct dwarf2_section_info *);
1448
1449static const char *get_section_file_name (const struct dwarf2_section_info *);
1450
918dd910
JK
1451static void dwarf2_find_base_address (struct die_info *die,
1452 struct dwarf2_cu *cu);
1453
0018ea6f
DE
1454static struct partial_symtab *create_partial_symtab
1455 (struct dwarf2_per_cu_data *per_cu, const char *name);
1456
f1902523
JK
1457static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1458 const gdb_byte *info_ptr,
1459 struct die_info *type_unit_die,
1460 int has_children, void *data);
1461
ed2dc618
SM
1462static void dwarf2_build_psymtabs_hard
1463 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1464
72bf9492
DJ
1465static void scan_partial_symbols (struct partial_die_info *,
1466 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1467 int, struct dwarf2_cu *);
c906108c 1468
72bf9492
DJ
1469static void add_partial_symbol (struct partial_die_info *,
1470 struct dwarf2_cu *);
63d06c5c 1471
72bf9492
DJ
1472static void add_partial_namespace (struct partial_die_info *pdi,
1473 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1474 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1475
5d7cb8df 1476static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1477 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1478 struct dwarf2_cu *cu);
1479
72bf9492
DJ
1480static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1481 struct dwarf2_cu *cu);
91c24f0a 1482
bc30ff58
JB
1483static void add_partial_subprogram (struct partial_die_info *pdi,
1484 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1485 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1486
257e7a09
YQ
1487static void dwarf2_read_symtab (struct partial_symtab *,
1488 struct objfile *);
c906108c 1489
a14ed312 1490static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1491
685af9cd 1492static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1493 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1494 sect_offset);
433df2d4 1495
d521ce57 1496static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1497
dee91e82 1498static struct partial_die_info *load_partial_dies
d521ce57 1499 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1500
fb816e8b
TV
1501/* A pair of partial_die_info and compilation unit. */
1502struct cu_partial_die_info
1503{
1504 /* The compilation unit of the partial_die_info. */
1505 struct dwarf2_cu *cu;
1506 /* A partial_die_info. */
1507 struct partial_die_info *pdi;
122cf0f2
AB
1508
1509 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1510 : cu (cu),
1511 pdi (pdi)
405feb71 1512 { /* Nothing. */ }
122cf0f2
AB
1513
1514private:
1515 cu_partial_die_info () = delete;
fb816e8b
TV
1516};
1517
122cf0f2
AB
1518static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1519 struct dwarf2_cu *);
72bf9492 1520
d521ce57
TT
1521static const gdb_byte *read_attribute (const struct die_reader_specs *,
1522 struct attribute *, struct attr_abbrev *,
1523 const gdb_byte *);
a8329558 1524
a1855c1d 1525static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1526
a1855c1d 1527static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1528
a1855c1d 1529static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1530
15f18d14
AT
1531/* Read the next three bytes (little-endian order) as an unsigned integer. */
1532static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1533
a1855c1d 1534static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1535
a1855c1d 1536static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1537
d521ce57 1538static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1539 unsigned int *);
c906108c 1540
d521ce57 1541static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1542
1543static LONGEST read_checked_initial_length_and_offset
d521ce57 1544 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1545 unsigned int *, unsigned int *);
613e1657 1546
d521ce57
TT
1547static LONGEST read_offset (bfd *, const gdb_byte *,
1548 const struct comp_unit_head *,
c764a876
DE
1549 unsigned int *);
1550
d521ce57 1551static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1552
ed2dc618
SM
1553static sect_offset read_abbrev_offset
1554 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1555 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1556
d521ce57 1557static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1558
d521ce57 1559static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1560
ed2dc618
SM
1561static const char *read_indirect_string
1562 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1563 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1564
ed2dc618
SM
1565static const char *read_indirect_line_string
1566 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1567 const struct comp_unit_head *, unsigned int *);
36586728 1568
ed2dc618
SM
1569static const char *read_indirect_string_at_offset
1570 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1571 LONGEST str_offset);
927aa2e7 1572
ed2dc618
SM
1573static const char *read_indirect_string_from_dwz
1574 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1575
d521ce57 1576static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1577
d521ce57
TT
1578static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1579 const gdb_byte *,
3019eac3
DE
1580 unsigned int *);
1581
d521ce57 1582static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1583 ULONGEST str_index);
3019eac3 1584
e142c38c 1585static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1586
e142c38c
DJ
1587static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1588 struct dwarf2_cu *);
c906108c 1589
348e048f 1590static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1591 unsigned int);
348e048f 1592
7d45c7c3
KB
1593static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1594 struct dwarf2_cu *cu);
1595
a084a2a6
AT
1596static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1597
05cf31d1
JB
1598static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1599 struct dwarf2_cu *cu);
1600
e142c38c 1601static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1602
e142c38c 1603static struct die_info *die_specification (struct die_info *die,
f2f0e013 1604 struct dwarf2_cu **);
63d06c5c 1605
9c541725 1606static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1607 struct dwarf2_cu *cu);
debd256d 1608
f3f5162e 1609static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1610 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1611 CORE_ADDR, int decode_mapping);
c906108c 1612
804d2729
TT
1613static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1614 const char *);
c906108c 1615
a14ed312 1616static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1617 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1618
ff39bb5e 1619static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1620 struct dwarf2_cu *);
c906108c 1621
ff39bb5e 1622static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1623 struct type *type,
1624 const char *name,
1625 struct obstack *obstack,
12df843f 1626 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1627 const gdb_byte **bytes,
98bfdba5 1628 struct dwarf2_locexpr_baton **baton);
2df3850c 1629
e7c27a73 1630static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1631
b4ba55a1
JB
1632static int need_gnat_info (struct dwarf2_cu *);
1633
3e43a32a
MS
1634static struct type *die_descriptive_type (struct die_info *,
1635 struct dwarf2_cu *);
b4ba55a1
JB
1636
1637static void set_descriptive_type (struct type *, struct die_info *,
1638 struct dwarf2_cu *);
1639
e7c27a73
DJ
1640static struct type *die_containing_type (struct die_info *,
1641 struct dwarf2_cu *);
c906108c 1642
ff39bb5e 1643static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1644 struct dwarf2_cu *);
c906108c 1645
f792889a 1646static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1647
673bfd45
DE
1648static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1649
0d5cff50 1650static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1651
6e70227d 1652static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1653 const char *suffix, int physname,
1654 struct dwarf2_cu *cu);
63d06c5c 1655
e7c27a73 1656static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1657
348e048f
DE
1658static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1659
e7c27a73 1660static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1661
e7c27a73 1662static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1663
96408a79
SA
1664static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1665
71a3c369
TT
1666static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1667
ff013f42
JK
1668static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1669 struct dwarf2_cu *, struct partial_symtab *);
1670
3a2b436a 1671/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1672 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1673enum pc_bounds_kind
1674{
e385593e 1675 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1676 PC_BOUNDS_NOT_PRESENT,
1677
e385593e
JK
1678 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1679 were present but they do not form a valid range of PC addresses. */
1680 PC_BOUNDS_INVALID,
1681
3a2b436a
JK
1682 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1683 PC_BOUNDS_RANGES,
1684
1685 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1686 PC_BOUNDS_HIGH_LOW,
1687};
1688
1689static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1690 CORE_ADDR *, CORE_ADDR *,
1691 struct dwarf2_cu *,
1692 struct partial_symtab *);
c906108c 1693
fae299cd
DC
1694static void get_scope_pc_bounds (struct die_info *,
1695 CORE_ADDR *, CORE_ADDR *,
1696 struct dwarf2_cu *);
1697
801e3a5b
JB
1698static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1699 CORE_ADDR, struct dwarf2_cu *);
1700
a14ed312 1701static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1702 struct dwarf2_cu *);
c906108c 1703
a14ed312 1704static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1705 struct type *, struct dwarf2_cu *);
c906108c 1706
a14ed312 1707static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1708 struct die_info *, struct type *,
e7c27a73 1709 struct dwarf2_cu *);
c906108c 1710
a14ed312 1711static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1712 struct type *,
1713 struct dwarf2_cu *);
c906108c 1714
134d01f1 1715static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1716
e7c27a73 1717static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1718
e7c27a73 1719static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1720
5d7cb8df
JK
1721static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1722
804d2729 1723static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1724
27aa8d6a
SW
1725static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1726
74921315
KS
1727static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1728
f55ee35c
JK
1729static struct type *read_module_type (struct die_info *die,
1730 struct dwarf2_cu *cu);
1731
38d518c9 1732static const char *namespace_name (struct die_info *die,
e142c38c 1733 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1734
134d01f1 1735static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1736
e7c27a73 1737static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1738
6e70227d 1739static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1740 struct dwarf2_cu *);
1741
bf6af496 1742static struct die_info *read_die_and_siblings_1
d521ce57 1743 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1744 struct die_info *);
639d11d3 1745
dee91e82 1746static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1747 const gdb_byte *info_ptr,
1748 const gdb_byte **new_info_ptr,
639d11d3
DC
1749 struct die_info *parent);
1750
d521ce57
TT
1751static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1752 struct die_info **, const gdb_byte *,
1753 int *, int);
3019eac3 1754
d521ce57
TT
1755static const gdb_byte *read_full_die (const struct die_reader_specs *,
1756 struct die_info **, const gdb_byte *,
1757 int *);
93311388 1758
e7c27a73 1759static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1760
15d034d0
TT
1761static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1762 struct obstack *);
71c25dea 1763
15d034d0 1764static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1765
15d034d0 1766static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1767 struct die_info *die,
1768 struct dwarf2_cu *cu);
1769
ca69b9e6
DE
1770static const char *dwarf2_physname (const char *name, struct die_info *die,
1771 struct dwarf2_cu *cu);
1772
e142c38c 1773static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1774 struct dwarf2_cu **);
9219021c 1775
f39c6ffd 1776static const char *dwarf_tag_name (unsigned int);
c906108c 1777
f39c6ffd 1778static const char *dwarf_attr_name (unsigned int);
c906108c 1779
a084a2a6
AT
1780static const char *dwarf_unit_type_name (int unit_type);
1781
f39c6ffd 1782static const char *dwarf_form_name (unsigned int);
c906108c 1783
a121b7c1 1784static const char *dwarf_bool_name (unsigned int);
c906108c 1785
f39c6ffd 1786static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1787
f9aca02d 1788static struct die_info *sibling_die (struct die_info *);
c906108c 1789
d97bc12b
DE
1790static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1791
1792static void dump_die_for_error (struct die_info *);
1793
1794static void dump_die_1 (struct ui_file *, int level, int max_level,
1795 struct die_info *);
c906108c 1796
d97bc12b 1797/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1798
51545339 1799static void store_in_ref_table (struct die_info *,
10b3939b 1800 struct dwarf2_cu *);
c906108c 1801
ff39bb5e 1802static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1803
ff39bb5e 1804static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1805
348e048f 1806static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1807 const struct attribute *,
348e048f
DE
1808 struct dwarf2_cu **);
1809
10b3939b 1810static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1811 const struct attribute *,
f2f0e013 1812 struct dwarf2_cu **);
c906108c 1813
348e048f 1814static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1815 const struct attribute *,
348e048f
DE
1816 struct dwarf2_cu **);
1817
ac9ec31b
DE
1818static struct type *get_signatured_type (struct die_info *, ULONGEST,
1819 struct dwarf2_cu *);
1820
1821static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1822 const struct attribute *,
ac9ec31b
DE
1823 struct dwarf2_cu *);
1824
e5fe5e75 1825static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1826
52dc124a 1827static void read_signatured_type (struct signatured_type *);
348e048f 1828
63e43d3a
PMR
1829static int attr_to_dynamic_prop (const struct attribute *attr,
1830 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1831 struct dynamic_prop *prop, struct type *type);
63e43d3a 1832
c906108c
SS
1833/* memory allocation interface */
1834
7b5a2f43 1835static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1836
b60c80d6 1837static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1838
43f3e411 1839static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1840
6e5a29e1 1841static int attr_form_is_block (const struct attribute *);
8e19ed76 1842
6e5a29e1 1843static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1844
6e5a29e1 1845static int attr_form_is_constant (const struct attribute *);
3690dd37 1846
6e5a29e1 1847static int attr_form_is_ref (const struct attribute *);
7771576e 1848
8cf6f0b1
TT
1849static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1850 struct dwarf2_loclist_baton *baton,
ff39bb5e 1851 const struct attribute *attr);
8cf6f0b1 1852
ff39bb5e 1853static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1854 struct symbol *sym,
f1e6e072
TT
1855 struct dwarf2_cu *cu,
1856 int is_block);
4c2df51b 1857
d521ce57
TT
1858static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1859 const gdb_byte *info_ptr,
1860 struct abbrev_info *abbrev);
4bb7a0a7 1861
72bf9492
DJ
1862static hashval_t partial_die_hash (const void *item);
1863
1864static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1865
ae038cb0 1866static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1867 (sect_offset sect_off, unsigned int offset_in_dwz,
1868 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1869
9816fde3 1870static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1871 struct die_info *comp_unit_die,
1872 enum language pretend_language);
93311388 1873
ed2dc618 1874static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1875
dee91e82 1876static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1877
f792889a
DJ
1878static struct type *set_die_type (struct die_info *, struct type *,
1879 struct dwarf2_cu *);
1c379e20 1880
ed2dc618 1881static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1882
ed2dc618 1883static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1884
58f0c718 1885static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1886 enum language);
10b3939b 1887
95554aad
TT
1888static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1889 enum language);
10b3939b 1890
f4dc4d17
DE
1891static void process_full_type_unit (struct dwarf2_per_cu_data *,
1892 enum language);
1893
10b3939b
DJ
1894static void dwarf2_add_dependence (struct dwarf2_cu *,
1895 struct dwarf2_per_cu_data *);
1896
ae038cb0
DJ
1897static void dwarf2_mark (struct dwarf2_cu *);
1898
1899static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1900
b64f50a1 1901static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1902 struct dwarf2_per_cu_data *);
673bfd45 1903
f792889a 1904static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1905
95554aad
TT
1906static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1907 enum language pretend_language);
1908
ed2dc618 1909static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1910
9a49df9d
AB
1911static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1912static struct type *dwarf2_per_cu_addr_sized_int_type
1913 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1914static struct type *dwarf2_per_cu_int_type
1915 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1916 bool unsigned_p);
9a49df9d 1917
b303c6f6
AB
1918/* Class, the destructor of which frees all allocated queue entries. This
1919 will only have work to do if an error was thrown while processing the
1920 dwarf. If no error was thrown then the queue entries should have all
1921 been processed, and freed, as we went along. */
1922
1923class dwarf2_queue_guard
1924{
1925public:
1926 dwarf2_queue_guard () = default;
1927
1928 /* Free any entries remaining on the queue. There should only be
1929 entries left if we hit an error while processing the dwarf. */
1930 ~dwarf2_queue_guard ()
1931 {
1932 struct dwarf2_queue_item *item, *last;
1933
1934 item = dwarf2_queue;
1935 while (item)
1936 {
1937 /* Anything still marked queued is likely to be in an
1938 inconsistent state, so discard it. */
1939 if (item->per_cu->queued)
1940 {
1941 if (item->per_cu->cu != NULL)
1942 free_one_cached_comp_unit (item->per_cu);
1943 item->per_cu->queued = 0;
1944 }
1945
1946 last = item;
1947 item = item->next;
1948 xfree (last);
1949 }
1950
1951 dwarf2_queue = dwarf2_queue_tail = NULL;
1952 }
1953};
1954
d721ba37
PA
1955/* The return type of find_file_and_directory. Note, the enclosed
1956 string pointers are only valid while this object is valid. */
1957
1958struct file_and_directory
1959{
1960 /* The filename. This is never NULL. */
1961 const char *name;
1962
1963 /* The compilation directory. NULL if not known. If we needed to
1964 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1965 points directly to the DW_AT_comp_dir string attribute owned by
1966 the obstack that owns the DIE. */
1967 const char *comp_dir;
1968
1969 /* If we needed to build a new string for comp_dir, this is what
1970 owns the storage. */
1971 std::string comp_dir_storage;
1972};
1973
1974static file_and_directory find_file_and_directory (struct die_info *die,
1975 struct dwarf2_cu *cu);
9291a0cd
TT
1976
1977static char *file_full_name (int file, struct line_header *lh,
1978 const char *comp_dir);
1979
43988095
JK
1980/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1981enum class rcuh_kind { COMPILE, TYPE };
1982
d521ce57 1983static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1984 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1985 struct comp_unit_head *header,
36586728 1986 struct dwarf2_section_info *section,
d521ce57 1987 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1988 rcuh_kind section_kind);
36586728 1989
fd820528 1990static void init_cutu_and_read_dies
f4dc4d17 1991 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1992 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1993 die_reader_func_ftype *die_reader_func, void *data);
1994
dee91e82
DE
1995static void init_cutu_and_read_dies_simple
1996 (struct dwarf2_per_cu_data *this_cu,
1997 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1998
673bfd45 1999static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 2000
3019eac3
DE
2001static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2002
57d63ce2 2003static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2004 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2005 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2006 ULONGEST signature, int is_debug_types);
a2ce51a0 2007
ed2dc618
SM
2008static struct dwp_file *get_dwp_file
2009 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2010
3019eac3 2011static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2012 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2013
2014static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2015 (struct signatured_type *, const char *, const char *);
3019eac3 2016
89e63ee4
DE
2017static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2018
263db9a1
TT
2019/* A unique pointer to a dwo_file. */
2020
51ac9db5 2021typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2022
ed2dc618 2023static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2024
1b80a9fa 2025static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2026
2027static void free_line_header_voidp (void *arg);
4390d890
DE
2028\f
2029/* Various complaints about symbol reading that don't abort the process. */
2030
2031static void
2032dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2033{
b98664d3 2034 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2035}
2036
2037static void
2038dwarf2_debug_line_missing_file_complaint (void)
2039{
b98664d3 2040 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2041}
2042
2043static void
2044dwarf2_debug_line_missing_end_sequence_complaint (void)
2045{
b98664d3 2046 complaint (_(".debug_line section has line "
4390d890
DE
2047 "program sequence without an end"));
2048}
2049
2050static void
2051dwarf2_complex_location_expr_complaint (void)
2052{
b98664d3 2053 complaint (_("location expression too complex"));
4390d890
DE
2054}
2055
2056static void
2057dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2058 int arg3)
2059{
b98664d3 2060 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2061 arg1, arg2, arg3);
2062}
2063
2064static void
2065dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2066{
b98664d3 2067 complaint (_("debug info runs off end of %s section"
4390d890 2068 " [in module %s]"),
a32a8923
DE
2069 get_section_name (section),
2070 get_section_file_name (section));
4390d890 2071}
1b80a9fa 2072
4390d890
DE
2073static void
2074dwarf2_macro_malformed_definition_complaint (const char *arg1)
2075{
b98664d3 2076 complaint (_("macro debug info contains a "
4390d890
DE
2077 "malformed macro definition:\n`%s'"),
2078 arg1);
2079}
2080
2081static void
2082dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2083{
b98664d3 2084 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2085 arg1, arg2);
2086}
527f3840
JK
2087
2088/* Hash function for line_header_hash. */
2089
2090static hashval_t
2091line_header_hash (const struct line_header *ofs)
2092{
9c541725 2093 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2094}
2095
2096/* Hash function for htab_create_alloc_ex for line_header_hash. */
2097
2098static hashval_t
2099line_header_hash_voidp (const void *item)
2100{
9a3c8263 2101 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2102
2103 return line_header_hash (ofs);
2104}
2105
2106/* Equality function for line_header_hash. */
2107
2108static int
2109line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2110{
9a3c8263
SM
2111 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2112 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2113
9c541725 2114 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2115 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2116}
2117
4390d890 2118\f
9291a0cd 2119
31aa7e4e
JB
2120/* Read the given attribute value as an address, taking the attribute's
2121 form into account. */
2122
2123static CORE_ADDR
2124attr_value_as_address (struct attribute *attr)
2125{
2126 CORE_ADDR addr;
2127
336d760d
AT
2128 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2129 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2130 {
2131 /* Aside from a few clearly defined exceptions, attributes that
2132 contain an address must always be in DW_FORM_addr form.
2133 Unfortunately, some compilers happen to be violating this
2134 requirement by encoding addresses using other forms, such
2135 as DW_FORM_data4 for example. For those broken compilers,
2136 we try to do our best, without any guarantee of success,
2137 to interpret the address correctly. It would also be nice
2138 to generate a complaint, but that would require us to maintain
2139 a list of legitimate cases where a non-address form is allowed,
2140 as well as update callers to pass in at least the CU's DWARF
2141 version. This is more overhead than what we're willing to
2142 expand for a pretty rare case. */
2143 addr = DW_UNSND (attr);
2144 }
2145 else
2146 addr = DW_ADDR (attr);
2147
2148 return addr;
2149}
2150
330cdd98
PA
2151/* See declaration. */
2152
2153dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2154 const dwarf2_debug_sections *names,
2155 bool can_copy_)
2156 : objfile (objfile_),
2157 can_copy (can_copy_)
330cdd98
PA
2158{
2159 if (names == NULL)
2160 names = &dwarf2_elf_names;
2161
2162 bfd *obfd = objfile->obfd;
2163
2164 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2165 locate_sections (obfd, sec, *names);
2166}
2167
2168dwarf2_per_objfile::~dwarf2_per_objfile ()
2169{
2170 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2171 free_cached_comp_units ();
2172
2173 if (quick_file_names_table)
2174 htab_delete (quick_file_names_table);
2175
2176 if (line_header_hash)
2177 htab_delete (line_header_hash);
2178
b76e467d 2179 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2180 per_cu->imported_symtabs_free ();
fc8e7e75 2181
b2bdb8cf 2182 for (signatured_type *sig_type : all_type_units)
ae640021 2183 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2184
330cdd98
PA
2185 /* Everything else should be on the objfile obstack. */
2186}
2187
2188/* See declaration. */
2189
2190void
2191dwarf2_per_objfile::free_cached_comp_units ()
2192{
2193 dwarf2_per_cu_data *per_cu = read_in_chain;
2194 dwarf2_per_cu_data **last_chain = &read_in_chain;
2195 while (per_cu != NULL)
2196 {
2197 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2198
fcd3b13d 2199 delete per_cu->cu;
330cdd98
PA
2200 *last_chain = next_cu;
2201 per_cu = next_cu;
2202 }
2203}
2204
11ed8cad
TT
2205/* A helper class that calls free_cached_comp_units on
2206 destruction. */
2207
2208class free_cached_comp_units
2209{
2210public:
2211
2212 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2213 : m_per_objfile (per_objfile)
2214 {
2215 }
2216
2217 ~free_cached_comp_units ()
2218 {
2219 m_per_objfile->free_cached_comp_units ();
2220 }
2221
2222 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2223
2224private:
2225
2226 dwarf2_per_objfile *m_per_objfile;
2227};
2228
c906108c 2229/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2230 information and return true if we have enough to do something.
2231 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2232 ELF names are used. CAN_COPY is true for formats where symbol
2233 interposition is possible and so symbol values must follow copy
2234 relocation rules. */
c906108c
SS
2235
2236int
251d32d9 2237dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2238 const struct dwarf2_debug_sections *names,
2239 bool can_copy)
c906108c 2240{
97cbe998
SDJ
2241 if (objfile->flags & OBJF_READNEVER)
2242 return 0;
2243
ed2dc618
SM
2244 struct dwarf2_per_objfile *dwarf2_per_objfile
2245 = get_dwarf2_per_objfile (objfile);
2246
2247 if (dwarf2_per_objfile == NULL)
5bfd760d 2248 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2249 names,
2250 can_copy);
5bfd760d 2251
73869dc2 2252 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2253 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2254 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2255 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2256}
2257
2258/* Return the containing section of virtual section SECTION. */
2259
2260static struct dwarf2_section_info *
2261get_containing_section (const struct dwarf2_section_info *section)
2262{
2263 gdb_assert (section->is_virtual);
2264 return section->s.containing_section;
c906108c
SS
2265}
2266
a32a8923
DE
2267/* Return the bfd owner of SECTION. */
2268
2269static struct bfd *
2270get_section_bfd_owner (const struct dwarf2_section_info *section)
2271{
73869dc2
DE
2272 if (section->is_virtual)
2273 {
2274 section = get_containing_section (section);
2275 gdb_assert (!section->is_virtual);
2276 }
049412e3 2277 return section->s.section->owner;
a32a8923
DE
2278}
2279
2280/* Return the bfd section of SECTION.
2281 Returns NULL if the section is not present. */
2282
2283static asection *
2284get_section_bfd_section (const struct dwarf2_section_info *section)
2285{
73869dc2
DE
2286 if (section->is_virtual)
2287 {
2288 section = get_containing_section (section);
2289 gdb_assert (!section->is_virtual);
2290 }
049412e3 2291 return section->s.section;
a32a8923
DE
2292}
2293
2294/* Return the name of SECTION. */
2295
2296static const char *
2297get_section_name (const struct dwarf2_section_info *section)
2298{
2299 asection *sectp = get_section_bfd_section (section);
2300
2301 gdb_assert (sectp != NULL);
fd361982 2302 return bfd_section_name (sectp);
a32a8923
DE
2303}
2304
2305/* Return the name of the file SECTION is in. */
2306
2307static const char *
2308get_section_file_name (const struct dwarf2_section_info *section)
2309{
2310 bfd *abfd = get_section_bfd_owner (section);
2311
2312 return bfd_get_filename (abfd);
2313}
2314
2315/* Return the id of SECTION.
2316 Returns 0 if SECTION doesn't exist. */
2317
2318static int
2319get_section_id (const struct dwarf2_section_info *section)
2320{
2321 asection *sectp = get_section_bfd_section (section);
2322
2323 if (sectp == NULL)
2324 return 0;
2325 return sectp->id;
2326}
2327
2328/* Return the flags of SECTION.
73869dc2 2329 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2330
2331static int
2332get_section_flags (const struct dwarf2_section_info *section)
2333{
2334 asection *sectp = get_section_bfd_section (section);
2335
2336 gdb_assert (sectp != NULL);
fd361982 2337 return bfd_section_flags (sectp);
a32a8923
DE
2338}
2339
251d32d9
TG
2340/* When loading sections, we look either for uncompressed section or for
2341 compressed section names. */
233a11ab
CS
2342
2343static int
251d32d9
TG
2344section_is_p (const char *section_name,
2345 const struct dwarf2_section_names *names)
233a11ab 2346{
251d32d9
TG
2347 if (names->normal != NULL
2348 && strcmp (section_name, names->normal) == 0)
2349 return 1;
2350 if (names->compressed != NULL
2351 && strcmp (section_name, names->compressed) == 0)
2352 return 1;
2353 return 0;
233a11ab
CS
2354}
2355
330cdd98 2356/* See declaration. */
c906108c 2357
330cdd98
PA
2358void
2359dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2360 const dwarf2_debug_sections &names)
c906108c 2361{
fd361982 2362 flagword aflag = bfd_section_flags (sectp);
251d32d9 2363
dc7650b8
JK
2364 if ((aflag & SEC_HAS_CONTENTS) == 0)
2365 {
2366 }
950b7495
KS
2367 else if (elf_section_data (sectp)->this_hdr.sh_size
2368 > bfd_get_file_size (abfd))
2369 {
2370 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2371 warning (_("Discarding section %s which has a section size (%s"
2372 ") larger than the file size [in module %s]"),
2373 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2374 bfd_get_filename (abfd));
2375 }
330cdd98 2376 else if (section_is_p (sectp->name, &names.info))
c906108c 2377 {
330cdd98 2378 this->info.s.section = sectp;
fd361982 2379 this->info.size = bfd_section_size (sectp);
c906108c 2380 }
330cdd98 2381 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2382 {
330cdd98 2383 this->abbrev.s.section = sectp;
fd361982 2384 this->abbrev.size = bfd_section_size (sectp);
c906108c 2385 }
330cdd98 2386 else if (section_is_p (sectp->name, &names.line))
c906108c 2387 {
330cdd98 2388 this->line.s.section = sectp;
fd361982 2389 this->line.size = bfd_section_size (sectp);
c906108c 2390 }
330cdd98 2391 else if (section_is_p (sectp->name, &names.loc))
c906108c 2392 {
330cdd98 2393 this->loc.s.section = sectp;
fd361982 2394 this->loc.size = bfd_section_size (sectp);
c906108c 2395 }
330cdd98 2396 else if (section_is_p (sectp->name, &names.loclists))
43988095 2397 {
330cdd98 2398 this->loclists.s.section = sectp;
fd361982 2399 this->loclists.size = bfd_section_size (sectp);
43988095 2400 }
330cdd98 2401 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2402 {
330cdd98 2403 this->macinfo.s.section = sectp;
fd361982 2404 this->macinfo.size = bfd_section_size (sectp);
c906108c 2405 }
330cdd98 2406 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2407 {
330cdd98 2408 this->macro.s.section = sectp;
fd361982 2409 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2410 }
330cdd98 2411 else if (section_is_p (sectp->name, &names.str))
c906108c 2412 {
330cdd98 2413 this->str.s.section = sectp;
fd361982 2414 this->str.size = bfd_section_size (sectp);
c906108c 2415 }
330cdd98 2416 else if (section_is_p (sectp->name, &names.line_str))
43988095 2417 {
330cdd98 2418 this->line_str.s.section = sectp;
fd361982 2419 this->line_str.size = bfd_section_size (sectp);
43988095 2420 }
330cdd98 2421 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2422 {
330cdd98 2423 this->addr.s.section = sectp;
fd361982 2424 this->addr.size = bfd_section_size (sectp);
3019eac3 2425 }
330cdd98 2426 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2427 {
330cdd98 2428 this->frame.s.section = sectp;
fd361982 2429 this->frame.size = bfd_section_size (sectp);
b6af0555 2430 }
330cdd98 2431 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2432 {
330cdd98 2433 this->eh_frame.s.section = sectp;
fd361982 2434 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2435 }
330cdd98 2436 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2437 {
330cdd98 2438 this->ranges.s.section = sectp;
fd361982 2439 this->ranges.size = bfd_section_size (sectp);
af34e669 2440 }
330cdd98 2441 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2442 {
330cdd98 2443 this->rnglists.s.section = sectp;
fd361982 2444 this->rnglists.size = bfd_section_size (sectp);
43988095 2445 }
330cdd98 2446 else if (section_is_p (sectp->name, &names.types))
348e048f 2447 {
8b70b953
TT
2448 struct dwarf2_section_info type_section;
2449
2450 memset (&type_section, 0, sizeof (type_section));
049412e3 2451 type_section.s.section = sectp;
fd361982 2452 type_section.size = bfd_section_size (sectp);
8b70b953 2453
fd5866f6 2454 this->types.push_back (type_section);
348e048f 2455 }
330cdd98 2456 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2457 {
330cdd98 2458 this->gdb_index.s.section = sectp;
fd361982 2459 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2460 }
927aa2e7
JK
2461 else if (section_is_p (sectp->name, &names.debug_names))
2462 {
2463 this->debug_names.s.section = sectp;
fd361982 2464 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2465 }
2466 else if (section_is_p (sectp->name, &names.debug_aranges))
2467 {
2468 this->debug_aranges.s.section = sectp;
fd361982 2469 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2470 }
dce234bc 2471
fd361982
AM
2472 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2473 && bfd_section_vma (sectp) == 0)
330cdd98 2474 this->has_section_at_zero = true;
c906108c
SS
2475}
2476
fceca515
DE
2477/* A helper function that decides whether a section is empty,
2478 or not present. */
9e0ac564
TT
2479
2480static int
19ac8c2e 2481dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2482{
73869dc2
DE
2483 if (section->is_virtual)
2484 return section->size == 0;
049412e3 2485 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2486}
2487
cd4fb1b2 2488/* See dwarf2read.h. */
c906108c 2489
cd4fb1b2
SM
2490void
2491dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2492{
a32a8923 2493 asection *sectp;
3019eac3 2494 bfd *abfd;
dce234bc 2495 gdb_byte *buf, *retbuf;
c906108c 2496
be391dca
TT
2497 if (info->readin)
2498 return;
dce234bc 2499 info->buffer = NULL;
dc4ccb6f 2500 info->readin = true;
188dd5d6 2501
9e0ac564 2502 if (dwarf2_section_empty_p (info))
dce234bc 2503 return;
c906108c 2504
a32a8923 2505 sectp = get_section_bfd_section (info);
3019eac3 2506
73869dc2
DE
2507 /* If this is a virtual section we need to read in the real one first. */
2508 if (info->is_virtual)
2509 {
2510 struct dwarf2_section_info *containing_section =
2511 get_containing_section (info);
2512
2513 gdb_assert (sectp != NULL);
2514 if ((sectp->flags & SEC_RELOC) != 0)
2515 {
2516 error (_("Dwarf Error: DWP format V2 with relocations is not"
2517 " supported in section %s [in module %s]"),
2518 get_section_name (info), get_section_file_name (info));
2519 }
2520 dwarf2_read_section (objfile, containing_section);
2521 /* Other code should have already caught virtual sections that don't
2522 fit. */
2523 gdb_assert (info->virtual_offset + info->size
2524 <= containing_section->size);
2525 /* If the real section is empty or there was a problem reading the
2526 section we shouldn't get here. */
2527 gdb_assert (containing_section->buffer != NULL);
2528 info->buffer = containing_section->buffer + info->virtual_offset;
2529 return;
2530 }
2531
4bf44c1c
TT
2532 /* If the section has relocations, we must read it ourselves.
2533 Otherwise we attach it to the BFD. */
2534 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2535 {
d521ce57 2536 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2537 return;
dce234bc 2538 }
dce234bc 2539
224c3ddb 2540 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2541 info->buffer = buf;
dce234bc
PP
2542
2543 /* When debugging .o files, we may need to apply relocations; see
2544 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2545 We never compress sections in .o files, so we only need to
2546 try this when the section is not compressed. */
ac8035ab 2547 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2548 if (retbuf != NULL)
2549 {
2550 info->buffer = retbuf;
2551 return;
2552 }
2553
a32a8923
DE
2554 abfd = get_section_bfd_owner (info);
2555 gdb_assert (abfd != NULL);
2556
dce234bc
PP
2557 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2558 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2559 {
2560 error (_("Dwarf Error: Can't read DWARF data"
2561 " in section %s [in module %s]"),
fd361982 2562 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2563 }
dce234bc
PP
2564}
2565
9e0ac564
TT
2566/* A helper function that returns the size of a section in a safe way.
2567 If you are positive that the section has been read before using the
2568 size, then it is safe to refer to the dwarf2_section_info object's
2569 "size" field directly. In other cases, you must call this
2570 function, because for compressed sections the size field is not set
2571 correctly until the section has been read. */
2572
2573static bfd_size_type
2574dwarf2_section_size (struct objfile *objfile,
2575 struct dwarf2_section_info *info)
2576{
2577 if (!info->readin)
2578 dwarf2_read_section (objfile, info);
2579 return info->size;
2580}
2581
dce234bc 2582/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2583 SECTION_NAME. */
af34e669 2584
dce234bc 2585void
3017a003
TG
2586dwarf2_get_section_info (struct objfile *objfile,
2587 enum dwarf2_section_enum sect,
d521ce57 2588 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2589 bfd_size_type *sizep)
2590{
5bfd760d 2591 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2592 struct dwarf2_section_info *info;
a3b2a86b
TT
2593
2594 /* We may see an objfile without any DWARF, in which case we just
2595 return nothing. */
2596 if (data == NULL)
2597 {
2598 *sectp = NULL;
2599 *bufp = NULL;
2600 *sizep = 0;
2601 return;
2602 }
3017a003
TG
2603 switch (sect)
2604 {
2605 case DWARF2_DEBUG_FRAME:
2606 info = &data->frame;
2607 break;
2608 case DWARF2_EH_FRAME:
2609 info = &data->eh_frame;
2610 break;
2611 default:
2612 gdb_assert_not_reached ("unexpected section");
2613 }
dce234bc 2614
9e0ac564 2615 dwarf2_read_section (objfile, info);
dce234bc 2616
a32a8923 2617 *sectp = get_section_bfd_section (info);
dce234bc
PP
2618 *bufp = info->buffer;
2619 *sizep = info->size;
2620}
2621
36586728
TT
2622/* A helper function to find the sections for a .dwz file. */
2623
2624static void
2625locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2626{
9a3c8263 2627 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2628
2629 /* Note that we only support the standard ELF names, because .dwz
2630 is ELF-only (at the time of writing). */
2631 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2632 {
049412e3 2633 dwz_file->abbrev.s.section = sectp;
fd361982 2634 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2635 }
2636 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2637 {
049412e3 2638 dwz_file->info.s.section = sectp;
fd361982 2639 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2640 }
2641 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2642 {
049412e3 2643 dwz_file->str.s.section = sectp;
fd361982 2644 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2645 }
2646 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2647 {
049412e3 2648 dwz_file->line.s.section = sectp;
fd361982 2649 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2650 }
2651 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2652 {
049412e3 2653 dwz_file->macro.s.section = sectp;
fd361982 2654 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2655 }
2ec9a5e0
TT
2656 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2657 {
049412e3 2658 dwz_file->gdb_index.s.section = sectp;
fd361982 2659 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2660 }
927aa2e7
JK
2661 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2662 {
2663 dwz_file->debug_names.s.section = sectp;
fd361982 2664 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2665 }
36586728
TT
2666}
2667
c4973306 2668/* See dwarf2read.h. */
36586728 2669
c4973306 2670struct dwz_file *
ed2dc618 2671dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2672{
36586728 2673 const char *filename;
acd13123 2674 bfd_size_type buildid_len_arg;
dc294be5
TT
2675 size_t buildid_len;
2676 bfd_byte *buildid;
36586728
TT
2677
2678 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2679 return dwarf2_per_objfile->dwz_file.get ();
36586728 2680
4db1a1dc 2681 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2682 gdb::unique_xmalloc_ptr<char> data
2683 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2684 &buildid_len_arg, &buildid));
4db1a1dc
TT
2685 if (data == NULL)
2686 {
2687 if (bfd_get_error () == bfd_error_no_error)
2688 return NULL;
2689 error (_("could not read '.gnu_debugaltlink' section: %s"),
2690 bfd_errmsg (bfd_get_error ()));
2691 }
791afaa2
TT
2692
2693 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2694
acd13123
TT
2695 buildid_len = (size_t) buildid_len_arg;
2696
791afaa2 2697 filename = data.get ();
d721ba37
PA
2698
2699 std::string abs_storage;
36586728
TT
2700 if (!IS_ABSOLUTE_PATH (filename))
2701 {
14278e1f
TT
2702 gdb::unique_xmalloc_ptr<char> abs
2703 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2704
14278e1f 2705 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2706 filename = abs_storage.c_str ();
36586728
TT
2707 }
2708
dc294be5
TT
2709 /* First try the file name given in the section. If that doesn't
2710 work, try to use the build-id instead. */
192b62ce 2711 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2712 if (dwz_bfd != NULL)
36586728 2713 {
192b62ce 2714 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2715 dwz_bfd.reset (nullptr);
36586728
TT
2716 }
2717
dc294be5
TT
2718 if (dwz_bfd == NULL)
2719 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2720
2721 if (dwz_bfd == NULL)
2722 error (_("could not find '.gnu_debugaltlink' file for %s"),
2723 objfile_name (dwarf2_per_objfile->objfile));
2724
7ff8cb8c
TT
2725 std::unique_ptr<struct dwz_file> result
2726 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2727
7ff8cb8c
TT
2728 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2729 result.get ());
36586728 2730
7ff8cb8c
TT
2731 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2732 result->dwz_bfd.get ());
2733 dwarf2_per_objfile->dwz_file = std::move (result);
2734 return dwarf2_per_objfile->dwz_file.get ();
36586728 2735}
9291a0cd 2736\f
7b9f3c50
DE
2737/* DWARF quick_symbols_functions support. */
2738
2739/* TUs can share .debug_line entries, and there can be a lot more TUs than
2740 unique line tables, so we maintain a separate table of all .debug_line
2741 derived entries to support the sharing.
2742 All the quick functions need is the list of file names. We discard the
2743 line_header when we're done and don't need to record it here. */
2744struct quick_file_names
2745{
094b34ac
DE
2746 /* The data used to construct the hash key. */
2747 struct stmt_list_hash hash;
7b9f3c50
DE
2748
2749 /* The number of entries in file_names, real_names. */
2750 unsigned int num_file_names;
2751
2752 /* The file names from the line table, after being run through
2753 file_full_name. */
2754 const char **file_names;
2755
2756 /* The file names from the line table after being run through
2757 gdb_realpath. These are computed lazily. */
2758 const char **real_names;
2759};
2760
2761/* When using the index (and thus not using psymtabs), each CU has an
2762 object of this type. This is used to hold information needed by
2763 the various "quick" methods. */
2764struct dwarf2_per_cu_quick_data
2765{
2766 /* The file table. This can be NULL if there was no file table
2767 or it's currently not read in.
2768 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2769 struct quick_file_names *file_names;
2770
2771 /* The corresponding symbol table. This is NULL if symbols for this
2772 CU have not yet been read. */
43f3e411 2773 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2774
2775 /* A temporary mark bit used when iterating over all CUs in
2776 expand_symtabs_matching. */
2777 unsigned int mark : 1;
2778
2779 /* True if we've tried to read the file table and found there isn't one.
2780 There will be no point in trying to read it again next time. */
2781 unsigned int no_file_data : 1;
2782};
2783
094b34ac
DE
2784/* Utility hash function for a stmt_list_hash. */
2785
2786static hashval_t
2787hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2788{
2789 hashval_t v = 0;
2790
2791 if (stmt_list_hash->dwo_unit != NULL)
2792 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2793 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2794 return v;
2795}
2796
2797/* Utility equality function for a stmt_list_hash. */
2798
2799static int
2800eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2801 const struct stmt_list_hash *rhs)
2802{
2803 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2804 return 0;
2805 if (lhs->dwo_unit != NULL
2806 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2807 return 0;
2808
9c541725 2809 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2810}
2811
7b9f3c50
DE
2812/* Hash function for a quick_file_names. */
2813
2814static hashval_t
2815hash_file_name_entry (const void *e)
2816{
9a3c8263
SM
2817 const struct quick_file_names *file_data
2818 = (const struct quick_file_names *) e;
7b9f3c50 2819
094b34ac 2820 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2821}
2822
2823/* Equality function for a quick_file_names. */
2824
2825static int
2826eq_file_name_entry (const void *a, const void *b)
2827{
9a3c8263
SM
2828 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2829 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2830
094b34ac 2831 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2832}
2833
2834/* Delete function for a quick_file_names. */
2835
2836static void
2837delete_file_name_entry (void *e)
2838{
9a3c8263 2839 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2840 int i;
2841
2842 for (i = 0; i < file_data->num_file_names; ++i)
2843 {
2844 xfree ((void*) file_data->file_names[i]);
2845 if (file_data->real_names)
2846 xfree ((void*) file_data->real_names[i]);
2847 }
2848
2849 /* The space for the struct itself lives on objfile_obstack,
2850 so we don't free it here. */
2851}
2852
2853/* Create a quick_file_names hash table. */
2854
2855static htab_t
2856create_quick_file_names_table (unsigned int nr_initial_entries)
2857{
2858 return htab_create_alloc (nr_initial_entries,
2859 hash_file_name_entry, eq_file_name_entry,
2860 delete_file_name_entry, xcalloc, xfree);
2861}
9291a0cd 2862
918dd910
JK
2863/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2864 have to be created afterwards. You should call age_cached_comp_units after
2865 processing PER_CU->CU. dw2_setup must have been already called. */
2866
2867static void
58f0c718 2868load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2869{
3019eac3 2870 if (per_cu->is_debug_types)
e5fe5e75 2871 load_full_type_unit (per_cu);
918dd910 2872 else
58f0c718 2873 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2874
cc12ce38
DE
2875 if (per_cu->cu == NULL)
2876 return; /* Dummy CU. */
2dc860c0
DE
2877
2878 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2879}
2880
a0f42c21 2881/* Read in the symbols for PER_CU. */
2fdf6df6 2882
9291a0cd 2883static void
58f0c718 2884dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2885{
ed2dc618 2886 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2887
f4dc4d17
DE
2888 /* Skip type_unit_groups, reading the type units they contain
2889 is handled elsewhere. */
2890 if (IS_TYPE_UNIT_GROUP (per_cu))
2891 return;
2892
b303c6f6
AB
2893 /* The destructor of dwarf2_queue_guard frees any entries left on
2894 the queue. After this point we're guaranteed to leave this function
2895 with the dwarf queue empty. */
2896 dwarf2_queue_guard q_guard;
9291a0cd 2897
95554aad 2898 if (dwarf2_per_objfile->using_index
43f3e411 2899 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2900 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2901 {
2902 queue_comp_unit (per_cu, language_minimal);
58f0c718 2903 load_cu (per_cu, skip_partial);
89e63ee4
DE
2904
2905 /* If we just loaded a CU from a DWO, and we're working with an index
2906 that may badly handle TUs, load all the TUs in that DWO as well.
2907 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2908 if (!per_cu->is_debug_types
cc12ce38 2909 && per_cu->cu != NULL
89e63ee4
DE
2910 && per_cu->cu->dwo_unit != NULL
2911 && dwarf2_per_objfile->index_table != NULL
2912 && dwarf2_per_objfile->index_table->version <= 7
2913 /* DWP files aren't supported yet. */
ed2dc618 2914 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2915 queue_and_load_all_dwo_tus (per_cu);
95554aad 2916 }
9291a0cd 2917
ed2dc618 2918 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2919
2920 /* Age the cache, releasing compilation units that have not
2921 been used recently. */
ed2dc618 2922 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2923}
2924
2925/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2926 the objfile from which this CU came. Returns the resulting symbol
2927 table. */
2fdf6df6 2928
43f3e411 2929static struct compunit_symtab *
58f0c718 2930dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2931{
ed2dc618
SM
2932 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2933
95554aad 2934 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2935 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2936 {
11ed8cad 2937 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2938 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2939 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2940 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2941 }
f194fefb 2942
43f3e411 2943 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2944}
2945
ff4c9fec 2946/* See declaration. */
f4dc4d17 2947
ff4c9fec
SM
2948dwarf2_per_cu_data *
2949dwarf2_per_objfile::get_cutu (int index)
2950{
b76e467d 2951 if (index >= this->all_comp_units.size ())
ff4c9fec 2952 {
b76e467d 2953 index -= this->all_comp_units.size ();
b2bdb8cf 2954 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2955 return &this->all_type_units[index]->per_cu;
2956 }
f4dc4d17 2957
ff4c9fec
SM
2958 return this->all_comp_units[index];
2959}
f4dc4d17 2960
ff4c9fec 2961/* See declaration. */
2fdf6df6 2962
ff4c9fec
SM
2963dwarf2_per_cu_data *
2964dwarf2_per_objfile::get_cu (int index)
1fd400ff 2965{
b76e467d 2966 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2967
ff4c9fec 2968 return this->all_comp_units[index];
f4dc4d17
DE
2969}
2970
ff4c9fec 2971/* See declaration. */
f4dc4d17 2972
ff4c9fec
SM
2973signatured_type *
2974dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2975{
b2bdb8cf 2976 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2977
ff4c9fec 2978 return this->all_type_units[index];
1fd400ff
TT
2979}
2980
4b514bc8
JK
2981/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2982 objfile_obstack, and constructed with the specified field
2983 values. */
2984
2985static dwarf2_per_cu_data *
ed2dc618 2986create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2987 struct dwarf2_section_info *section,
2988 int is_dwz,
2989 sect_offset sect_off, ULONGEST length)
2990{
ed2dc618 2991 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2992 dwarf2_per_cu_data *the_cu
2993 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2994 struct dwarf2_per_cu_data);
2995 the_cu->sect_off = sect_off;
2996 the_cu->length = length;
e3b94546 2997 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2998 the_cu->section = section;
2999 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3000 struct dwarf2_per_cu_quick_data);
3001 the_cu->is_dwz = is_dwz;
3002 return the_cu;
3003}
3004
2ec9a5e0
TT
3005/* A helper for create_cus_from_index that handles a given list of
3006 CUs. */
2fdf6df6 3007
74a0d9f6 3008static void
12359b5e 3009create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3010 const gdb_byte *cu_list, offset_type n_elements,
3011 struct dwarf2_section_info *section,
b76e467d 3012 int is_dwz)
9291a0cd 3013{
12359b5e 3014 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3015 {
74a0d9f6 3016 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3017
3018 sect_offset sect_off
3019 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3020 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3021 cu_list += 2 * 8;
3022
b76e467d 3023 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3024 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3025 sect_off, length);
b76e467d 3026 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3027 }
9291a0cd
TT
3028}
3029
2ec9a5e0 3030/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3031 the CU objects for this objfile. */
2ec9a5e0 3032
74a0d9f6 3033static void
12359b5e 3034create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3035 const gdb_byte *cu_list, offset_type cu_list_elements,
3036 const gdb_byte *dwz_list, offset_type dwz_elements)
3037{
b76e467d
SM
3038 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3039 dwarf2_per_objfile->all_comp_units.reserve
3040 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3041
12359b5e 3042 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3043 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3044
3045 if (dwz_elements == 0)
74a0d9f6 3046 return;
2ec9a5e0 3047
12359b5e
SM
3048 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3049 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3050 &dwz->info, 1);
2ec9a5e0
TT
3051}
3052
1fd400ff 3053/* Create the signatured type hash table from the index. */
673bfd45 3054
74a0d9f6 3055static void
12359b5e
SM
3056create_signatured_type_table_from_index
3057 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3058 struct dwarf2_section_info *section,
3059 const gdb_byte *bytes,
3060 offset_type elements)
1fd400ff 3061{
12359b5e 3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3063
b2bdb8cf
SM
3064 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3065 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3066
12359b5e 3067 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3068
12359b5e 3069 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3070 {
52dc124a 3071 struct signatured_type *sig_type;
9c541725 3072 ULONGEST signature;
1fd400ff 3073 void **slot;
9c541725 3074 cu_offset type_offset_in_tu;
1fd400ff 3075
74a0d9f6 3076 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3077 sect_offset sect_off
3078 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3079 type_offset_in_tu
3080 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3081 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3082 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3083 bytes += 3 * 8;
3084
52dc124a 3085 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3086 struct signatured_type);
52dc124a 3087 sig_type->signature = signature;
9c541725 3088 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3089 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3090 sig_type->per_cu.section = section;
9c541725 3091 sig_type->per_cu.sect_off = sect_off;
e3b94546 3092 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3093 sig_type->per_cu.v.quick
1fd400ff
TT
3094 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3095 struct dwarf2_per_cu_quick_data);
3096
52dc124a
DE
3097 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3098 *slot = sig_type;
1fd400ff 3099
b2bdb8cf 3100 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3101 }
3102
673bfd45 3103 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3104}
3105
927aa2e7
JK
3106/* Create the signatured type hash table from .debug_names. */
3107
3108static void
3109create_signatured_type_table_from_debug_names
ed2dc618 3110 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3111 const mapped_debug_names &map,
3112 struct dwarf2_section_info *section,
3113 struct dwarf2_section_info *abbrev_section)
3114{
ed2dc618
SM
3115 struct objfile *objfile = dwarf2_per_objfile->objfile;
3116
927aa2e7
JK
3117 dwarf2_read_section (objfile, section);
3118 dwarf2_read_section (objfile, abbrev_section);
3119
b2bdb8cf
SM
3120 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3121 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3122
3123 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3124
3125 for (uint32_t i = 0; i < map.tu_count; ++i)
3126 {
3127 struct signatured_type *sig_type;
927aa2e7 3128 void **slot;
927aa2e7
JK
3129
3130 sect_offset sect_off
3131 = (sect_offset) (extract_unsigned_integer
3132 (map.tu_table_reordered + i * map.offset_size,
3133 map.offset_size,
3134 map.dwarf5_byte_order));
3135
3136 comp_unit_head cu_header;
ed2dc618
SM
3137 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3138 abbrev_section,
927aa2e7
JK
3139 section->buffer + to_underlying (sect_off),
3140 rcuh_kind::TYPE);
3141
3142 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3143 struct signatured_type);
3144 sig_type->signature = cu_header.signature;
3145 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3146 sig_type->per_cu.is_debug_types = 1;
3147 sig_type->per_cu.section = section;
3148 sig_type->per_cu.sect_off = sect_off;
e3b94546 3149 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3150 sig_type->per_cu.v.quick
3151 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3152 struct dwarf2_per_cu_quick_data);
3153
3154 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3155 *slot = sig_type;
3156
b2bdb8cf 3157 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3158 }
3159
3160 dwarf2_per_objfile->signatured_types = sig_types_hash;
3161}
3162
9291a0cd
TT
3163/* Read the address map data from the mapped index, and use it to
3164 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3165
9291a0cd 3166static void
ed2dc618
SM
3167create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3168 struct mapped_index *index)
9291a0cd 3169{
ed2dc618 3170 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3171 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3172 const gdb_byte *iter, *end;
9291a0cd 3173 struct addrmap *mutable_map;
9291a0cd
TT
3174 CORE_ADDR baseaddr;
3175
8268c778
PA
3176 auto_obstack temp_obstack;
3177
9291a0cd
TT
3178 mutable_map = addrmap_create_mutable (&temp_obstack);
3179
f00a2de2
PA
3180 iter = index->address_table.data ();
3181 end = iter + index->address_table.size ();
9291a0cd
TT
3182
3183 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3184
3185 while (iter < end)
3186 {
3187 ULONGEST hi, lo, cu_index;
3188 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3189 iter += 8;
3190 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3191 iter += 8;
3192 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3193 iter += 4;
f652bce2 3194
24a55014 3195 if (lo > hi)
f652bce2 3196 {
b98664d3 3197 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3198 hex_string (lo), hex_string (hi));
24a55014 3199 continue;
f652bce2 3200 }
24a55014 3201
b76e467d 3202 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3203 {
b98664d3 3204 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3205 (unsigned) cu_index);
24a55014 3206 continue;
f652bce2 3207 }
24a55014 3208
79748972
TT
3209 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3210 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3211 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3212 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3213 }
3214
d320c2b5 3215 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3216 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3217}
3218
927aa2e7
JK
3219/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3220 populate the objfile's psymtabs_addrmap. */
3221
3222static void
ed2dc618 3223create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3224 struct dwarf2_section_info *section)
3225{
ed2dc618 3226 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3227 bfd *abfd = objfile->obfd;
3228 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3229 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3230 SECT_OFF_TEXT (objfile));
3231
3232 auto_obstack temp_obstack;
3233 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3234
3235 std::unordered_map<sect_offset,
3236 dwarf2_per_cu_data *,
3237 gdb::hash_enum<sect_offset>>
3238 debug_info_offset_to_per_cu;
b76e467d 3239 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3240 {
927aa2e7
JK
3241 const auto insertpair
3242 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3243 if (!insertpair.second)
3244 {
3245 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3246 "debug_info_offset %s, ignoring .debug_aranges."),
3247 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3248 return;
3249 }
3250 }
3251
3252 dwarf2_read_section (objfile, section);
3253
3254 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3255
3256 const gdb_byte *addr = section->buffer;
3257
3258 while (addr < section->buffer + section->size)
3259 {
3260 const gdb_byte *const entry_addr = addr;
3261 unsigned int bytes_read;
3262
3263 const LONGEST entry_length = read_initial_length (abfd, addr,
3264 &bytes_read);
3265 addr += bytes_read;
3266
3267 const gdb_byte *const entry_end = addr + entry_length;
3268 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3269 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3270 if (addr + entry_length > section->buffer + section->size)
3271 {
47e3f474 3272 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3273 "length %s exceeds section length %s, "
3274 "ignoring .debug_aranges."),
47e3f474
TV
3275 objfile_name (objfile),
3276 plongest (entry_addr - section->buffer),
927aa2e7
JK
3277 plongest (bytes_read + entry_length),
3278 pulongest (section->size));
3279 return;
3280 }
3281
3282 /* The version number. */
3283 const uint16_t version = read_2_bytes (abfd, addr);
3284 addr += 2;
3285 if (version != 2)
3286 {
47e3f474 3287 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3288 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3289 objfile_name (objfile),
3290 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3291 return;
3292 }
3293
3294 const uint64_t debug_info_offset
3295 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3296 addr += offset_size;
3297 const auto per_cu_it
3298 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3299 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3300 {
47e3f474 3301 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3302 "debug_info_offset %s does not exists, "
3303 "ignoring .debug_aranges."),
47e3f474
TV
3304 objfile_name (objfile),
3305 plongest (entry_addr - section->buffer),
927aa2e7
JK
3306 pulongest (debug_info_offset));
3307 return;
3308 }
3309 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3310
3311 const uint8_t address_size = *addr++;
3312 if (address_size < 1 || address_size > 8)
3313 {
47e3f474 3314 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3315 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3318 return;
3319 }
3320
3321 const uint8_t segment_selector_size = *addr++;
3322 if (segment_selector_size != 0)
3323 {
47e3f474 3324 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3325 "segment_selector_size %u is not supported, "
3326 "ignoring .debug_aranges."),
47e3f474
TV
3327 objfile_name (objfile),
3328 plongest (entry_addr - section->buffer),
927aa2e7
JK
3329 segment_selector_size);
3330 return;
3331 }
3332
3333 /* Must pad to an alignment boundary that is twice the address
3334 size. It is undocumented by the DWARF standard but GCC does
3335 use it. */
3336 for (size_t padding = ((-(addr - section->buffer))
3337 & (2 * address_size - 1));
3338 padding > 0; padding--)
3339 if (*addr++ != 0)
3340 {
47e3f474 3341 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3342 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3343 objfile_name (objfile),
3344 plongest (entry_addr - section->buffer));
927aa2e7
JK
3345 return;
3346 }
3347
3348 for (;;)
3349 {
3350 if (addr + 2 * address_size > entry_end)
3351 {
47e3f474 3352 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3353 "address list is not properly terminated, "
3354 "ignoring .debug_aranges."),
47e3f474
TV
3355 objfile_name (objfile),
3356 plongest (entry_addr - section->buffer));
927aa2e7
JK
3357 return;
3358 }
3359 ULONGEST start = extract_unsigned_integer (addr, address_size,
3360 dwarf5_byte_order);
3361 addr += address_size;
3362 ULONGEST length = extract_unsigned_integer (addr, address_size,
3363 dwarf5_byte_order);
3364 addr += address_size;
3365 if (start == 0 && length == 0)
3366 break;
3367 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3368 {
3369 /* Symbol was eliminated due to a COMDAT group. */
3370 continue;
3371 }
3372 ULONGEST end = start + length;
79748972
TT
3373 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3374 - baseaddr);
3375 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3376 - baseaddr);
927aa2e7
JK
3377 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3378 }
3379 }
3380
d320c2b5 3381 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3382 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3383}
3384
9291a0cd
TT
3385/* Find a slot in the mapped index INDEX for the object named NAME.
3386 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3387 constant pool and return true. If NAME cannot be found, return
3388 false. */
2fdf6df6 3389
109483d9 3390static bool
9291a0cd
TT
3391find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3392 offset_type **vec_out)
3393{
0cf03b49 3394 offset_type hash;
9291a0cd 3395 offset_type slot, step;
559a7a62 3396 int (*cmp) (const char *, const char *);
9291a0cd 3397
791afaa2 3398 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3399 if (current_language->la_language == language_cplus
45280282
IB
3400 || current_language->la_language == language_fortran
3401 || current_language->la_language == language_d)
0cf03b49
JK
3402 {
3403 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3404 not contain any. */
a8719064 3405
72998fb3 3406 if (strchr (name, '(') != NULL)
0cf03b49 3407 {
109483d9 3408 without_params = cp_remove_params (name);
0cf03b49 3409
72998fb3 3410 if (without_params != NULL)
791afaa2 3411 name = without_params.get ();
0cf03b49
JK
3412 }
3413 }
3414
559a7a62 3415 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3416 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3417 simulate our NAME being searched is also lowercased. */
3418 hash = mapped_index_string_hash ((index->version == 4
3419 && case_sensitivity == case_sensitive_off
3420 ? 5 : index->version),
3421 name);
3422
f00a2de2
PA
3423 slot = hash & (index->symbol_table.size () - 1);
3424 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3425 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3426
3427 for (;;)
3428 {
9291a0cd 3429 const char *str;
f00a2de2
PA
3430
3431 const auto &bucket = index->symbol_table[slot];
3432 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3433 return false;
9291a0cd 3434
f00a2de2 3435 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3436 if (!cmp (name, str))
9291a0cd
TT
3437 {
3438 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3439 + MAYBE_SWAP (bucket.vec));
109483d9 3440 return true;
9291a0cd
TT
3441 }
3442
f00a2de2 3443 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3444 }
3445}
3446
4485a1c1
SM
3447/* A helper function that reads the .gdb_index from BUFFER and fills
3448 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3449 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3450 ok to use deprecated sections.
3451
3452 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3453 out parameters that are filled in with information about the CU and
3454 TU lists in the section.
3455
4485a1c1 3456 Returns true if all went well, false otherwise. */
2fdf6df6 3457
d33bc52e 3458static bool
4485a1c1
SM
3459read_gdb_index_from_buffer (struct objfile *objfile,
3460 const char *filename,
3461 bool deprecated_ok,
3462 gdb::array_view<const gdb_byte> buffer,
3463 struct mapped_index *map,
3464 const gdb_byte **cu_list,
3465 offset_type *cu_list_elements,
3466 const gdb_byte **types_list,
3467 offset_type *types_list_elements)
3468{
3469 const gdb_byte *addr = &buffer[0];
82430852 3470
9291a0cd 3471 /* Version check. */
4485a1c1 3472 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3473 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3474 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3475 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3476 indices. */
831adc1f 3477 if (version < 4)
481860b3
GB
3478 {
3479 static int warning_printed = 0;
3480 if (!warning_printed)
3481 {
3482 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3483 filename);
481860b3
GB
3484 warning_printed = 1;
3485 }
3486 return 0;
3487 }
3488 /* Index version 4 uses a different hash function than index version
3489 5 and later.
3490
3491 Versions earlier than 6 did not emit psymbols for inlined
3492 functions. Using these files will cause GDB not to be able to
3493 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3494 indices unless the user has done
3495 "set use-deprecated-index-sections on". */
2ec9a5e0 3496 if (version < 6 && !deprecated_ok)
481860b3
GB
3497 {
3498 static int warning_printed = 0;
3499 if (!warning_printed)
3500 {
e615022a
DE
3501 warning (_("\
3502Skipping deprecated .gdb_index section in %s.\n\
3503Do \"set use-deprecated-index-sections on\" before the file is read\n\
3504to use the section anyway."),
2ec9a5e0 3505 filename);
481860b3
GB
3506 warning_printed = 1;
3507 }
3508 return 0;
3509 }
796a7ff8 3510 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3511 of the TU (for symbols coming from TUs),
3512 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3513 Plus gold-generated indices can have duplicate entries for global symbols,
3514 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3515 These are just performance bugs, and we can't distinguish gdb-generated
3516 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3517
481860b3 3518 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3519 longer backward compatible. */
796a7ff8 3520 if (version > 8)
594e8718 3521 return 0;
9291a0cd 3522
559a7a62 3523 map->version = version;
9291a0cd 3524
4485a1c1 3525 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3526
4485a1c1 3527 int i = 0;
2ec9a5e0
TT
3528 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3529 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3530 / 8);
1fd400ff
TT
3531 ++i;
3532
2ec9a5e0
TT
3533 *types_list = addr + MAYBE_SWAP (metadata[i]);
3534 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3535 - MAYBE_SWAP (metadata[i]))
3536 / 8);
987d643c 3537 ++i;
1fd400ff 3538
f00a2de2
PA
3539 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3540 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3541 map->address_table
3542 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3543 ++i;
3544
f00a2de2
PA
3545 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3546 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3547 map->symbol_table
3548 = gdb::array_view<mapped_index::symbol_table_slot>
3549 ((mapped_index::symbol_table_slot *) symbol_table,
3550 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3551
f00a2de2 3552 ++i;
f9d83a0b 3553 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3554
2ec9a5e0
TT
3555 return 1;
3556}
3557
4485a1c1
SM
3558/* Callback types for dwarf2_read_gdb_index. */
3559
3560typedef gdb::function_view
3561 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3562 get_gdb_index_contents_ftype;
3563typedef gdb::function_view
3564 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3565 get_gdb_index_contents_dwz_ftype;
3566
927aa2e7 3567/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3568 elements of all the CUs and return 1. Otherwise, return 0. */
3569
3570static int
4485a1c1
SM
3571dwarf2_read_gdb_index
3572 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3573 get_gdb_index_contents_ftype get_gdb_index_contents,
3574 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3575{
2ec9a5e0
TT
3576 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3577 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3578 struct dwz_file *dwz;
12359b5e 3579 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3580
4485a1c1
SM
3581 gdb::array_view<const gdb_byte> main_index_contents
3582 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3583
3584 if (main_index_contents.empty ())
3585 return 0;
3586
3063847f 3587 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3588 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3589 use_deprecated_index_sections,
3590 main_index_contents, map.get (), &cu_list,
3591 &cu_list_elements, &types_list,
3592 &types_list_elements))
2ec9a5e0
TT
3593 return 0;
3594
0fefef59 3595 /* Don't use the index if it's empty. */
3063847f 3596 if (map->symbol_table.empty ())
0fefef59
DE
3597 return 0;
3598
2ec9a5e0
TT
3599 /* If there is a .dwz file, read it so we can get its CU list as
3600 well. */
ed2dc618 3601 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3602 if (dwz != NULL)
2ec9a5e0 3603 {
2ec9a5e0
TT
3604 struct mapped_index dwz_map;
3605 const gdb_byte *dwz_types_ignore;
3606 offset_type dwz_types_elements_ignore;
3607
4485a1c1
SM
3608 gdb::array_view<const gdb_byte> dwz_index_content
3609 = get_gdb_index_contents_dwz (objfile, dwz);
3610
3611 if (dwz_index_content.empty ())
3612 return 0;
3613
3614 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3615 bfd_get_filename (dwz->dwz_bfd.get ()),
3616 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3617 &dwz_list, &dwz_list_elements,
3618 &dwz_types_ignore,
3619 &dwz_types_elements_ignore))
2ec9a5e0
TT
3620 {
3621 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3622 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3623 return 0;
3624 }
3625 }
3626
12359b5e
SM
3627 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3628 dwz_list, dwz_list_elements);
1fd400ff 3629
8b70b953
TT
3630 if (types_list_elements)
3631 {
8b70b953
TT
3632 /* We can only handle a single .debug_types when we have an
3633 index. */
fd5866f6 3634 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3635 return 0;
3636
fd5866f6 3637 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3638
12359b5e
SM
3639 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3640 types_list, types_list_elements);
8b70b953 3641 }
9291a0cd 3642
3063847f 3643 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3644
3063847f 3645 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3646 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3647 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3648 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3649
3650 return 1;
3651}
3652
dee91e82 3653/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3654
dee91e82
DE
3655static void
3656dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3657 const gdb_byte *info_ptr,
dee91e82
DE
3658 struct die_info *comp_unit_die,
3659 int has_children,
3660 void *data)
9291a0cd 3661{
dee91e82 3662 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3663 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3664 struct dwarf2_per_objfile *dwarf2_per_objfile
3665 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3666 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3667 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3668 struct attribute *attr;
7b9f3c50
DE
3669 void **slot;
3670 struct quick_file_names *qfn;
9291a0cd 3671
0186c6a7
DE
3672 gdb_assert (! this_cu->is_debug_types);
3673
07261596
TT
3674 /* Our callers never want to match partial units -- instead they
3675 will match the enclosing full CU. */
3676 if (comp_unit_die->tag == DW_TAG_partial_unit)
3677 {
3678 this_cu->v.quick->no_file_data = 1;
3679 return;
3680 }
3681
0186c6a7 3682 lh_cu = this_cu;
7b9f3c50 3683 slot = NULL;
dee91e82 3684
fff8551c 3685 line_header_up lh;
9c541725 3686 sect_offset line_offset {};
fff8551c 3687
dee91e82 3688 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3689 if (attr != nullptr)
9291a0cd 3690 {
7b9f3c50
DE
3691 struct quick_file_names find_entry;
3692
9c541725 3693 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3694
3695 /* We may have already read in this line header (TU line header sharing).
3696 If we have we're done. */
094b34ac 3697 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3698 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3699 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3700 &find_entry, INSERT);
3701 if (*slot != NULL)
3702 {
9a3c8263 3703 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3704 return;
7b9f3c50
DE
3705 }
3706
3019eac3 3707 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3708 }
3709 if (lh == NULL)
3710 {
094b34ac 3711 lh_cu->v.quick->no_file_data = 1;
dee91e82 3712 return;
9291a0cd
TT
3713 }
3714
8d749320 3715 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3716 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3717 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3718 gdb_assert (slot != NULL);
3719 *slot = qfn;
9291a0cd 3720
d721ba37 3721 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3722
aa391654
TT
3723 int offset = 0;
3724 if (strcmp (fnd.name, "<unknown>") != 0)
3725 ++offset;
3726
7ba99d21 3727 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3728 qfn->file_names =
aa391654
TT
3729 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3730 if (offset != 0)
3731 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3732 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3733 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3734 qfn->real_names = NULL;
9291a0cd 3735
094b34ac 3736 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3737}
3738
3739/* A helper for the "quick" functions which attempts to read the line
3740 table for THIS_CU. */
3741
3742static struct quick_file_names *
e4a48d9d 3743dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3744{
0186c6a7
DE
3745 /* This should never be called for TUs. */
3746 gdb_assert (! this_cu->is_debug_types);
3747 /* Nor type unit groups. */
3748 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3749
dee91e82
DE
3750 if (this_cu->v.quick->file_names != NULL)
3751 return this_cu->v.quick->file_names;
3752 /* If we know there is no line data, no point in looking again. */
3753 if (this_cu->v.quick->no_file_data)
3754 return NULL;
3755
0186c6a7 3756 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3757
3758 if (this_cu->v.quick->no_file_data)
3759 return NULL;
3760 return this_cu->v.quick->file_names;
9291a0cd
TT
3761}
3762
3763/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3764 real path for a given file name from the line table. */
2fdf6df6 3765
9291a0cd 3766static const char *
7b9f3c50
DE
3767dw2_get_real_path (struct objfile *objfile,
3768 struct quick_file_names *qfn, int index)
9291a0cd 3769{
7b9f3c50
DE
3770 if (qfn->real_names == NULL)
3771 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3772 qfn->num_file_names, const char *);
9291a0cd 3773
7b9f3c50 3774 if (qfn->real_names[index] == NULL)
14278e1f 3775 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3776
7b9f3c50 3777 return qfn->real_names[index];
9291a0cd
TT
3778}
3779
3780static struct symtab *
3781dw2_find_last_source_symtab (struct objfile *objfile)
3782{
ed2dc618
SM
3783 struct dwarf2_per_objfile *dwarf2_per_objfile
3784 = get_dwarf2_per_objfile (objfile);
b76e467d 3785 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3786 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3787
43f3e411
DE
3788 if (cust == NULL)
3789 return NULL;
ed2dc618 3790
43f3e411 3791 return compunit_primary_filetab (cust);
9291a0cd
TT
3792}
3793
7b9f3c50
DE
3794/* Traversal function for dw2_forget_cached_source_info. */
3795
3796static int
3797dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3798{
7b9f3c50 3799 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3800
7b9f3c50 3801 if (file_data->real_names)
9291a0cd 3802 {
7b9f3c50 3803 int i;
9291a0cd 3804
7b9f3c50 3805 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3806 {
7b9f3c50
DE
3807 xfree ((void*) file_data->real_names[i]);
3808 file_data->real_names[i] = NULL;
9291a0cd
TT
3809 }
3810 }
7b9f3c50
DE
3811
3812 return 1;
3813}
3814
3815static void
3816dw2_forget_cached_source_info (struct objfile *objfile)
3817{
ed2dc618
SM
3818 struct dwarf2_per_objfile *dwarf2_per_objfile
3819 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3820
3821 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3822 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3823}
3824
f8eba3c6
TT
3825/* Helper function for dw2_map_symtabs_matching_filename that expands
3826 the symtabs and calls the iterator. */
3827
3828static int
3829dw2_map_expand_apply (struct objfile *objfile,
3830 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3831 const char *name, const char *real_path,
14bc53a8 3832 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3833{
43f3e411 3834 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3835
3836 /* Don't visit already-expanded CUs. */
43f3e411 3837 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3838 return 0;
3839
3840 /* This may expand more than one symtab, and we want to iterate over
3841 all of them. */
58f0c718 3842 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3843
14bc53a8
PA
3844 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3845 last_made, callback);
f8eba3c6
TT
3846}
3847
3848/* Implementation of the map_symtabs_matching_filename method. */
3849
14bc53a8
PA
3850static bool
3851dw2_map_symtabs_matching_filename
3852 (struct objfile *objfile, const char *name, const char *real_path,
3853 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3854{
c011a4f4 3855 const char *name_basename = lbasename (name);
ed2dc618
SM
3856 struct dwarf2_per_objfile *dwarf2_per_objfile
3857 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3858
848e3e78
DE
3859 /* The rule is CUs specify all the files, including those used by
3860 any TU, so there's no need to scan TUs here. */
f4dc4d17 3861
b76e467d 3862 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3863 {
3d7bb9d9 3864 /* We only need to look at symtabs not already expanded. */
43f3e411 3865 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3866 continue;
3867
b76e467d 3868 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3869 if (file_data == NULL)
9291a0cd
TT
3870 continue;
3871
b76e467d 3872 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3873 {
7b9f3c50 3874 const char *this_name = file_data->file_names[j];
da235a7c 3875 const char *this_real_name;
9291a0cd 3876
af529f8f 3877 if (compare_filenames_for_search (this_name, name))
9291a0cd 3878 {
f5b95b50 3879 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3880 callback))
3881 return true;
288e77a7 3882 continue;
4aac40c8 3883 }
9291a0cd 3884
c011a4f4
DE
3885 /* Before we invoke realpath, which can get expensive when many
3886 files are involved, do a quick comparison of the basenames. */
3887 if (! basenames_may_differ
3888 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3889 continue;
3890
da235a7c
JK
3891 this_real_name = dw2_get_real_path (objfile, file_data, j);
3892 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3893 {
da235a7c 3894 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3895 callback))
3896 return true;
288e77a7 3897 continue;
da235a7c 3898 }
9291a0cd 3899
da235a7c
JK
3900 if (real_path != NULL)
3901 {
af529f8f
JK
3902 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3903 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3904 if (this_real_name != NULL
af529f8f 3905 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3906 {
f5b95b50 3907 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3908 callback))
3909 return true;
288e77a7 3910 continue;
9291a0cd
TT
3911 }
3912 }
3913 }
3914 }
3915
14bc53a8 3916 return false;
9291a0cd
TT
3917}
3918
da51c347
DE
3919/* Struct used to manage iterating over all CUs looking for a symbol. */
3920
3921struct dw2_symtab_iterator
9291a0cd 3922{
ed2dc618
SM
3923 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3924 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3925 /* If set, only look for symbols that match that block. Valid values are
3926 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3927 gdb::optional<block_enum> block_index;
da51c347
DE
3928 /* The kind of symbol we're looking for. */
3929 domain_enum domain;
3930 /* The list of CUs from the index entry of the symbol,
3931 or NULL if not found. */
3932 offset_type *vec;
3933 /* The next element in VEC to look at. */
3934 int next;
3935 /* The number of elements in VEC, or zero if there is no match. */
3936 int length;
8943b874
DE
3937 /* Have we seen a global version of the symbol?
3938 If so we can ignore all further global instances.
3939 This is to work around gold/15646, inefficient gold-generated
3940 indices. */
3941 int global_seen;
da51c347 3942};
9291a0cd 3943
2b79f376 3944/* Initialize the index symtab iterator ITER. */
2fdf6df6 3945
9291a0cd 3946static void
da51c347 3947dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3948 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3949 gdb::optional<block_enum> block_index,
da51c347
DE
3950 domain_enum domain,
3951 const char *name)
3952{
ed2dc618 3953 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3954 iter->block_index = block_index;
3955 iter->domain = domain;
3956 iter->next = 0;
8943b874 3957 iter->global_seen = 0;
da51c347 3958
3063847f 3959 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3960
3961 /* index is NULL if OBJF_READNOW. */
3962 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3963 iter->length = MAYBE_SWAP (*iter->vec);
3964 else
3965 {
3966 iter->vec = NULL;
3967 iter->length = 0;
3968 }
3969}
3970
3971/* Return the next matching CU or NULL if there are no more. */
3972
3973static struct dwarf2_per_cu_data *
3974dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3975{
ed2dc618
SM
3976 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3977
da51c347
DE
3978 for ( ; iter->next < iter->length; ++iter->next)
3979 {
3980 offset_type cu_index_and_attrs =
3981 MAYBE_SWAP (iter->vec[iter->next + 1]);
3982 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3983 gdb_index_symbol_kind symbol_kind =
3984 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3985 /* Only check the symbol attributes if they're present.
3986 Indices prior to version 7 don't record them,
3987 and indices >= 7 may elide them for certain symbols
3988 (gold does this). */
3989 int attrs_valid =
ed2dc618 3990 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3991 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3992
3190f0c6 3993 /* Don't crash on bad data. */
b76e467d 3994 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3995 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3996 {
b98664d3 3997 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3998 " [in module %s]"),
3999 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
4000 continue;
4001 }
4002
ff4c9fec 4003 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4004
da51c347 4005 /* Skip if already read in. */
43f3e411 4006 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4007 continue;
4008
8943b874
DE
4009 /* Check static vs global. */
4010 if (attrs_valid)
4011 {
2b79f376
SM
4012 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4013
4014 if (iter->block_index.has_value ())
4015 {
4016 bool want_static = *iter->block_index == STATIC_BLOCK;
4017
4018 if (is_static != want_static)
4019 continue;
4020 }
4021
8943b874
DE
4022 /* Work around gold/15646. */
4023 if (!is_static && iter->global_seen)
4024 continue;
4025 if (!is_static)
4026 iter->global_seen = 1;
4027 }
da51c347
DE
4028
4029 /* Only check the symbol's kind if it has one. */
4030 if (attrs_valid)
4031 {
4032 switch (iter->domain)
4033 {
4034 case VAR_DOMAIN:
4035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4037 /* Some types are also in VAR_DOMAIN. */
4038 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 case STRUCT_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4043 continue;
4044 break;
4045 case LABEL_DOMAIN:
4046 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4047 continue;
4048 break;
59c35742
AB
4049 case MODULE_DOMAIN:
4050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4051 continue;
4052 break;
da51c347
DE
4053 default:
4054 break;
4055 }
4056 }
4057
4058 ++iter->next;
4059 return per_cu;
4060 }
4061
4062 return NULL;
4063}
4064
43f3e411 4065static struct compunit_symtab *
c7f839cb 4066dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4067 const char *name, domain_enum domain)
9291a0cd 4068{
43f3e411 4069 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4070 struct dwarf2_per_objfile *dwarf2_per_objfile
4071 = get_dwarf2_per_objfile (objfile);
9291a0cd 4072
b5ec771e
PA
4073 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4074
ed2dc618
SM
4075 struct dw2_symtab_iterator iter;
4076 struct dwarf2_per_cu_data *per_cu;
da51c347 4077
2b79f376 4078 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4079
ed2dc618
SM
4080 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4081 {
4082 struct symbol *sym, *with_opaque = NULL;
58f0c718 4083 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4084 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4085 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4086
ed2dc618
SM
4087 sym = block_find_symbol (block, name, domain,
4088 block_find_non_opaque_type_preferred,
4089 &with_opaque);
b2e2f908 4090
ed2dc618
SM
4091 /* Some caution must be observed with overloaded functions
4092 and methods, since the index will not contain any overload
4093 information (but NAME might contain it). */
da51c347 4094
ed2dc618
SM
4095 if (sym != NULL
4096 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4097 return stab;
4098 if (with_opaque != NULL
4099 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4100 stab_best = stab;
da51c347 4101
ed2dc618 4102 /* Keep looking through other CUs. */
9291a0cd 4103 }
9291a0cd 4104
da51c347 4105 return stab_best;
9291a0cd
TT
4106}
4107
4108static void
4109dw2_print_stats (struct objfile *objfile)
4110{
ed2dc618
SM
4111 struct dwarf2_per_objfile *dwarf2_per_objfile
4112 = get_dwarf2_per_objfile (objfile);
b76e467d 4113 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4114 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4115 int count = 0;
9291a0cd 4116
ed2dc618 4117 for (int i = 0; i < total; ++i)
9291a0cd 4118 {
ff4c9fec 4119 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4120
43f3e411 4121 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4122 ++count;
4123 }
e4a48d9d 4124 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4125 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4126}
4127
779bd270
DE
4128/* This dumps minimal information about the index.
4129 It is called via "mt print objfiles".
4130 One use is to verify .gdb_index has been loaded by the
4131 gdb.dwarf2/gdb-index.exp testcase. */
4132
9291a0cd
TT
4133static void
4134dw2_dump (struct objfile *objfile)
4135{
ed2dc618
SM
4136 struct dwarf2_per_objfile *dwarf2_per_objfile
4137 = get_dwarf2_per_objfile (objfile);
4138
779bd270
DE
4139 gdb_assert (dwarf2_per_objfile->using_index);
4140 printf_filtered (".gdb_index:");
4141 if (dwarf2_per_objfile->index_table != NULL)
4142 {
4143 printf_filtered (" version %d\n",
4144 dwarf2_per_objfile->index_table->version);
4145 }
4146 else
4147 printf_filtered (" faked for \"readnow\"\n");
4148 printf_filtered ("\n");
9291a0cd
TT
4149}
4150
9291a0cd
TT
4151static void
4152dw2_expand_symtabs_for_function (struct objfile *objfile,
4153 const char *func_name)
4154{
ed2dc618
SM
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
da51c347 4157
ed2dc618
SM
4158 struct dw2_symtab_iterator iter;
4159 struct dwarf2_per_cu_data *per_cu;
da51c347 4160
2b79f376 4161 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4162
ed2dc618 4163 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4164 dw2_instantiate_symtab (per_cu, false);
da51c347 4165
9291a0cd
TT
4166}
4167
4168static void
4169dw2_expand_all_symtabs (struct objfile *objfile)
4170{
ed2dc618
SM
4171 struct dwarf2_per_objfile *dwarf2_per_objfile
4172 = get_dwarf2_per_objfile (objfile);
b76e467d 4173 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4174 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4175
ed2dc618 4176 for (int i = 0; i < total_units; ++i)
9291a0cd 4177 {
ff4c9fec 4178 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4179
58f0c718
TT
4180 /* We don't want to directly expand a partial CU, because if we
4181 read it with the wrong language, then assertion failures can
4182 be triggered later on. See PR symtab/23010. So, tell
4183 dw2_instantiate_symtab to skip partial CUs -- any important
4184 partial CU will be read via DW_TAG_imported_unit anyway. */
4185 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4186 }
4187}
4188
4189static void
652a8996
JK
4190dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4191 const char *fullname)
9291a0cd 4192{
ed2dc618
SM
4193 struct dwarf2_per_objfile *dwarf2_per_objfile
4194 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4195
4196 /* We don't need to consider type units here.
4197 This is only called for examining code, e.g. expand_line_sal.
4198 There can be an order of magnitude (or more) more type units
4199 than comp units, and we avoid them if we can. */
4200
b76e467d 4201 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4202 {
3d7bb9d9 4203 /* We only need to look at symtabs not already expanded. */
43f3e411 4204 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4205 continue;
4206
b76e467d 4207 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4208 if (file_data == NULL)
9291a0cd
TT
4209 continue;
4210
b76e467d 4211 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4212 {
652a8996
JK
4213 const char *this_fullname = file_data->file_names[j];
4214
4215 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4216 {
58f0c718 4217 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4218 break;
4219 }
4220 }
4221 }
4222}
4223
9291a0cd 4224static void
199b4314
TT
4225dw2_map_matching_symbols
4226 (struct objfile *objfile,
b054970d 4227 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4228 int global,
4229 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4230 symbol_compare_ftype *ordered_compare)
9291a0cd 4231{
40658b94 4232 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4233 current language is Ada for a non-Ada objfile using GNU index. As Ada
4234 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4235}
4236
e1ef7d7a
PA
4237/* Starting from a search name, return the string that finds the upper
4238 bound of all strings that start with SEARCH_NAME in a sorted name
4239 list. Returns the empty string to indicate that the upper bound is
4240 the end of the list. */
4241
4242static std::string
4243make_sort_after_prefix_name (const char *search_name)
4244{
4245 /* When looking to complete "func", we find the upper bound of all
4246 symbols that start with "func" by looking for where we'd insert
4247 the closest string that would follow "func" in lexicographical
4248 order. Usually, that's "func"-with-last-character-incremented,
4249 i.e. "fund". Mind non-ASCII characters, though. Usually those
4250 will be UTF-8 multi-byte sequences, but we can't be certain.
4251 Especially mind the 0xff character, which is a valid character in
4252 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4253 rule out compilers allowing it in identifiers. Note that
4254 conveniently, strcmp/strcasecmp are specified to compare
4255 characters interpreted as unsigned char. So what we do is treat
4256 the whole string as a base 256 number composed of a sequence of
4257 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4258 to 0, and carries 1 to the following more-significant position.
4259 If the very first character in SEARCH_NAME ends up incremented
4260 and carries/overflows, then the upper bound is the end of the
4261 list. The string after the empty string is also the empty
4262 string.
4263
4264 Some examples of this operation:
4265
4266 SEARCH_NAME => "+1" RESULT
4267
4268 "abc" => "abd"
4269 "ab\xff" => "ac"
4270 "\xff" "a" "\xff" => "\xff" "b"
4271 "\xff" => ""
4272 "\xff\xff" => ""
4273 "" => ""
4274
4275 Then, with these symbols for example:
4276
4277 func
4278 func1
4279 fund
4280
4281 completing "func" looks for symbols between "func" and
4282 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4283 which finds "func" and "func1", but not "fund".
4284
4285 And with:
4286
4287 funcÿ (Latin1 'ÿ' [0xff])
4288 funcÿ1
4289 fund
4290
4291 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4292 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4293
4294 And with:
4295
4296 ÿÿ (Latin1 'ÿ' [0xff])
4297 ÿÿ1
4298
4299 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4300 the end of the list.
4301 */
4302 std::string after = search_name;
4303 while (!after.empty () && (unsigned char) after.back () == 0xff)
4304 after.pop_back ();
4305 if (!after.empty ())
4306 after.back () = (unsigned char) after.back () + 1;
4307 return after;
4308}
4309
5c58de74 4310/* See declaration. */
61d96d7e 4311
5c58de74
PA
4312std::pair<std::vector<name_component>::const_iterator,
4313 std::vector<name_component>::const_iterator>
44ed8f3e 4314mapped_index_base::find_name_components_bounds
3b00ef10 4315 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4316{
5c58de74
PA
4317 auto *name_cmp
4318 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4319
3b00ef10
TT
4320 const char *lang_name
4321 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4322
3f563c84
PA
4323 /* Comparison function object for lower_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_lower = [&] (const name_component &elem,
4326 const char *name)
4327 {
5c58de74 4328 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4329 const char *elem_name = elem_qualified + elem.name_offset;
4330 return name_cmp (elem_name, name) < 0;
4331 };
4332
4333 /* Comparison function object for upper_bound that matches against a
4334 given symbol name. */
4335 auto lookup_compare_upper = [&] (const char *name,
4336 const name_component &elem)
4337 {
5c58de74 4338 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4339 const char *elem_name = elem_qualified + elem.name_offset;
4340 return name_cmp (name, elem_name) < 0;
4341 };
4342
5c58de74
PA
4343 auto begin = this->name_components.begin ();
4344 auto end = this->name_components.end ();
3f563c84
PA
4345
4346 /* Find the lower bound. */
4347 auto lower = [&] ()
4348 {
3b00ef10 4349 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4350 return begin;
4351 else
3b00ef10 4352 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4353 } ();
4354
4355 /* Find the upper bound. */
4356 auto upper = [&] ()
4357 {
5c58de74 4358 if (lookup_name_without_params.completion_mode ())
3f563c84 4359 {
e1ef7d7a
PA
4360 /* In completion mode, we want UPPER to point past all
4361 symbols names that have the same prefix. I.e., with
4362 these symbols, and completing "func":
4363
4364 function << lower bound
4365 function1
4366 other_function << upper bound
4367
4368 We find the upper bound by looking for the insertion
4369 point of "func"-with-last-character-incremented,
4370 i.e. "fund". */
3b00ef10 4371 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4372 if (after.empty ())
3f563c84 4373 return end;
e6b2f5ef
PA
4374 return std::lower_bound (lower, end, after.c_str (),
4375 lookup_compare_lower);
3f563c84
PA
4376 }
4377 else
3b00ef10 4378 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4379 } ();
4380
5c58de74
PA
4381 return {lower, upper};
4382}
4383
4384/* See declaration. */
4385
4386void
44ed8f3e 4387mapped_index_base::build_name_components ()
5c58de74
PA
4388{
4389 if (!this->name_components.empty ())
4390 return;
4391
4392 this->name_components_casing = case_sensitivity;
4393 auto *name_cmp
4394 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4395
4396 /* The code below only knows how to break apart components of C++
4397 symbol names (and other languages that use '::' as
3b00ef10 4398 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4399 auto count = this->symbol_name_count ();
4400 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4401 {
44ed8f3e 4402 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4403 continue;
4404
4405 const char *name = this->symbol_name_at (idx);
4406
4407 /* Add each name component to the name component table. */
4408 unsigned int previous_len = 0;
3b00ef10
TT
4409
4410 if (strstr (name, "::") != nullptr)
4411 {
4412 for (unsigned int current_len = cp_find_first_component (name);
4413 name[current_len] != '\0';
4414 current_len += cp_find_first_component (name + current_len))
4415 {
4416 gdb_assert (name[current_len] == ':');
4417 this->name_components.push_back ({previous_len, idx});
4418 /* Skip the '::'. */
4419 current_len += 2;
4420 previous_len = current_len;
4421 }
4422 }
4423 else
5c58de74 4424 {
3b00ef10
TT
4425 /* Handle the Ada encoded (aka mangled) form here. */
4426 for (const char *iter = strstr (name, "__");
4427 iter != nullptr;
4428 iter = strstr (iter, "__"))
4429 {
4430 this->name_components.push_back ({previous_len, idx});
4431 iter += 2;
4432 previous_len = iter - name;
4433 }
5c58de74 4434 }
3b00ef10 4435
5c58de74
PA
4436 this->name_components.push_back ({previous_len, idx});
4437 }
4438
4439 /* Sort name_components elements by name. */
4440 auto name_comp_compare = [&] (const name_component &left,
4441 const name_component &right)
4442 {
4443 const char *left_qualified = this->symbol_name_at (left.idx);
4444 const char *right_qualified = this->symbol_name_at (right.idx);
4445
4446 const char *left_name = left_qualified + left.name_offset;
4447 const char *right_name = right_qualified + right.name_offset;
4448
4449 return name_cmp (left_name, right_name) < 0;
4450 };
4451
4452 std::sort (this->name_components.begin (),
4453 this->name_components.end (),
4454 name_comp_compare);
4455}
4456
4457/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4458 mapped_index_base instead of the containing objfile. This is split
4459 to a separate function in order to be able to unit test the
4460 name_components matching using a mock mapped_index_base. For each
5c58de74 4461 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4462 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4463
4464static void
4465dw2_expand_symtabs_matching_symbol
44ed8f3e 4466 (mapped_index_base &index,
5c58de74
PA
4467 const lookup_name_info &lookup_name_in,
4468 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4469 enum search_domain kind,
3b00ef10 4470 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4471{
4472 lookup_name_info lookup_name_without_params
4473 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4474
4475 /* Build the symbol name component sorted vector, if we haven't
4476 yet. */
4477 index.build_name_components ();
4478
3f563c84
PA
4479 /* The same symbol may appear more than once in the range though.
4480 E.g., if we're looking for symbols that complete "w", and we have
4481 a symbol named "w1::w2", we'll find the two name components for
4482 that same symbol in the range. To be sure we only call the
4483 callback once per symbol, we first collect the symbol name
4484 indexes that matched in a temporary vector and ignore
4485 duplicates. */
4486 std::vector<offset_type> matches;
3f563c84 4487
3b00ef10
TT
4488 struct name_and_matcher
4489 {
4490 symbol_name_matcher_ftype *matcher;
4491 const std::string &name;
4492
4493 bool operator== (const name_and_matcher &other) const
3f563c84 4494 {
3b00ef10
TT
4495 return matcher == other.matcher && name == other.name;
4496 }
4497 };
4498
4499 /* A vector holding all the different symbol name matchers, for all
4500 languages. */
4501 std::vector<name_and_matcher> matchers;
4502
4503 for (int i = 0; i < nr_languages; i++)
4504 {
4505 enum language lang_e = (enum language) i;
4506
4507 const language_defn *lang = language_def (lang_e);
4508 symbol_name_matcher_ftype *name_matcher
4509 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4510
3b00ef10
TT
4511 name_and_matcher key {
4512 name_matcher,
4513 lookup_name_without_params.language_lookup_name (lang_e)
4514 };
4515
4516 /* Don't insert the same comparison routine more than once.
4517 Note that we do this linear walk. This is not a problem in
4518 practice because the number of supported languages is
4519 low. */
4520 if (std::find (matchers.begin (), matchers.end (), key)
4521 != matchers.end ())
9291a0cd 4522 continue;
3b00ef10
TT
4523 matchers.push_back (std::move (key));
4524
4525 auto bounds
4526 = index.find_name_components_bounds (lookup_name_without_params,
4527 lang_e);
4528
4529 /* Now for each symbol name in range, check to see if we have a name
4530 match, and if so, call the MATCH_CALLBACK callback. */
4531
4532 for (; bounds.first != bounds.second; ++bounds.first)
4533 {
4534 const char *qualified = index.symbol_name_at (bounds.first->idx);
4535
4536 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4537 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4538 continue;
9291a0cd 4539
3b00ef10
TT
4540 matches.push_back (bounds.first->idx);
4541 }
3f563c84
PA
4542 }
4543
4544 std::sort (matches.begin (), matches.end ());
4545
4546 /* Finally call the callback, once per match. */
4547 ULONGEST prev = -1;
4548 for (offset_type idx : matches)
4549 {
4550 if (prev != idx)
4551 {
3b00ef10
TT
4552 if (!match_callback (idx))
4553 break;
3f563c84
PA
4554 prev = idx;
4555 }
4556 }
4557
4558 /* Above we use a type wider than idx's for 'prev', since 0 and
4559 (offset_type)-1 are both possible values. */
4560 static_assert (sizeof (prev) > sizeof (offset_type), "");
4561}
4562
c62446b1
PA
4563#if GDB_SELF_TEST
4564
4565namespace selftests { namespace dw2_expand_symtabs_matching {
4566
a3c5fafd
PA
4567/* A mock .gdb_index/.debug_names-like name index table, enough to
4568 exercise dw2_expand_symtabs_matching_symbol, which works with the
4569 mapped_index_base interface. Builds an index from the symbol list
4570 passed as parameter to the constructor. */
4571class mock_mapped_index : public mapped_index_base
c62446b1
PA
4572{
4573public:
a3c5fafd
PA
4574 mock_mapped_index (gdb::array_view<const char *> symbols)
4575 : m_symbol_table (symbols)
c62446b1
PA
4576 {}
4577
a3c5fafd 4578 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4579
a3c5fafd 4580 /* Return the number of names in the symbol table. */
632e107b 4581 size_t symbol_name_count () const override
c62446b1 4582 {
a3c5fafd 4583 return m_symbol_table.size ();
c62446b1
PA
4584 }
4585
a3c5fafd 4586 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4587 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4588 {
4589 return m_symbol_table[idx];
4590 }
c62446b1 4591
a3c5fafd
PA
4592private:
4593 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4594};
4595
4596/* Convenience function that converts a NULL pointer to a "<null>"
4597 string, to pass to print routines. */
4598
4599static const char *
4600string_or_null (const char *str)
4601{
4602 return str != NULL ? str : "<null>";
4603}
4604
4605/* Check if a lookup_name_info built from
4606 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4607 index. EXPECTED_LIST is the list of expected matches, in expected
4608 matching order. If no match expected, then an empty list is
4609 specified. Returns true on success. On failure prints a warning
4610 indicating the file:line that failed, and returns false. */
4611
4612static bool
4613check_match (const char *file, int line,
4614 mock_mapped_index &mock_index,
4615 const char *name, symbol_name_match_type match_type,
4616 bool completion_mode,
4617 std::initializer_list<const char *> expected_list)
4618{
4619 lookup_name_info lookup_name (name, match_type, completion_mode);
4620
4621 bool matched = true;
4622
4623 auto mismatch = [&] (const char *expected_str,
4624 const char *got)
4625 {
4626 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4627 "expected=\"%s\", got=\"%s\"\n"),
4628 file, line,
4629 (match_type == symbol_name_match_type::FULL
4630 ? "FULL" : "WILD"),
4631 name, string_or_null (expected_str), string_or_null (got));
4632 matched = false;
4633 };
4634
4635 auto expected_it = expected_list.begin ();
4636 auto expected_end = expected_list.end ();
4637
a3c5fafd 4638 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4639 NULL, ALL_DOMAIN,
4640 [&] (offset_type idx)
4641 {
a3c5fafd 4642 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645
4646 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4647 mismatch (expected_str, matched_name);
3b00ef10 4648 return true;
c62446b1
PA
4649 });
4650
4651 const char *expected_str
4652 = expected_it == expected_end ? NULL : *expected_it++;
4653 if (expected_str != NULL)
4654 mismatch (expected_str, NULL);
4655
4656 return matched;
4657}
4658
4659/* The symbols added to the mock mapped_index for testing (in
4660 canonical form). */
4661static const char *test_symbols[] = {
4662 "function",
4663 "std::bar",
4664 "std::zfunction",
4665 "std::zfunction2",
4666 "w1::w2",
4667 "ns::foo<char*>",
4668 "ns::foo<int>",
4669 "ns::foo<long>",
a20714ff
PA
4670 "ns2::tmpl<int>::foo2",
4671 "(anonymous namespace)::A::B::C",
c62446b1 4672
e1ef7d7a
PA
4673 /* These are used to check that the increment-last-char in the
4674 matching algorithm for completion doesn't match "t1_fund" when
4675 completing "t1_func". */
4676 "t1_func",
4677 "t1_func1",
4678 "t1_fund",
4679 "t1_fund1",
4680
4681 /* A UTF-8 name with multi-byte sequences to make sure that
4682 cp-name-parser understands this as a single identifier ("função"
4683 is "function" in PT). */
4684 u8"u8função",
4685
4686 /* \377 (0xff) is Latin1 'ÿ'. */
4687 "yfunc\377",
4688
4689 /* \377 (0xff) is Latin1 'ÿ'. */
4690 "\377",
4691 "\377\377123",
4692
c62446b1
PA
4693 /* A name with all sorts of complications. Starts with "z" to make
4694 it easier for the completion tests below. */
4695#define Z_SYM_NAME \
4696 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4697 "::tuple<(anonymous namespace)::ui*, " \
4698 "std::default_delete<(anonymous namespace)::ui>, void>"
4699
4700 Z_SYM_NAME
4701};
4702
a3c5fafd
PA
4703/* Returns true if the mapped_index_base::find_name_component_bounds
4704 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4705 in completion mode. */
5c58de74
PA
4706
4707static bool
a3c5fafd 4708check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4709 const char *search_name,
4710 gdb::array_view<const char *> expected_syms)
4711{
4712 lookup_name_info lookup_name (search_name,
4713 symbol_name_match_type::FULL, true);
4714
3b00ef10
TT
4715 auto bounds = index.find_name_components_bounds (lookup_name,
4716 language_cplus);
5c58de74
PA
4717
4718 size_t distance = std::distance (bounds.first, bounds.second);
4719 if (distance != expected_syms.size ())
4720 return false;
4721
4722 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4723 {
4724 auto nc_elem = bounds.first + exp_elem;
4725 const char *qualified = index.symbol_name_at (nc_elem->idx);
4726 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4727 return false;
4728 }
4729
4730 return true;
4731}
4732
4733/* Test the lower-level mapped_index::find_name_component_bounds
4734 method. */
4735
c62446b1 4736static void
5c58de74
PA
4737test_mapped_index_find_name_component_bounds ()
4738{
4739 mock_mapped_index mock_index (test_symbols);
4740
a3c5fafd 4741 mock_index.build_name_components ();
5c58de74
PA
4742
4743 /* Test the lower-level mapped_index::find_name_component_bounds
4744 method in completion mode. */
4745 {
4746 static const char *expected_syms[] = {
4747 "t1_func",
4748 "t1_func1",
5c58de74
PA
4749 };
4750
a3c5fafd 4751 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4752 "t1_func", expected_syms));
4753 }
4754
4755 /* Check that the increment-last-char in the name matching algorithm
4756 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4757 {
4758 static const char *expected_syms1[] = {
4759 "\377",
4760 "\377\377123",
4761 };
a3c5fafd 4762 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4763 "\377", expected_syms1));
4764
4765 static const char *expected_syms2[] = {
4766 "\377\377123",
4767 };
a3c5fafd 4768 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4769 "\377\377", expected_syms2));
4770 }
4771}
4772
4773/* Test dw2_expand_symtabs_matching_symbol. */
4774
4775static void
4776test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4777{
4778 mock_mapped_index mock_index (test_symbols);
4779
4780 /* We let all tests run until the end even if some fails, for debug
4781 convenience. */
4782 bool any_mismatch = false;
4783
4784 /* Create the expected symbols list (an initializer_list). Needed
4785 because lists have commas, and we need to pass them to CHECK,
4786 which is a macro. */
4787#define EXPECT(...) { __VA_ARGS__ }
4788
4789 /* Wrapper for check_match that passes down the current
4790 __FILE__/__LINE__. */
4791#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4792 any_mismatch |= !check_match (__FILE__, __LINE__, \
4793 mock_index, \
4794 NAME, MATCH_TYPE, COMPLETION_MODE, \
4795 EXPECTED_LIST)
4796
4797 /* Identity checks. */
4798 for (const char *sym : test_symbols)
4799 {
4800 /* Should be able to match all existing symbols. */
4801 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4802 EXPECT (sym));
4803
4804 /* Should be able to match all existing symbols with
4805 parameters. */
4806 std::string with_params = std::string (sym) + "(int)";
4807 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4808 EXPECT (sym));
4809
4810 /* Should be able to match all existing symbols with
4811 parameters and qualifiers. */
4812 with_params = std::string (sym) + " ( int ) const";
4813 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4814 EXPECT (sym));
4815
4816 /* This should really find sym, but cp-name-parser.y doesn't
4817 know about lvalue/rvalue qualifiers yet. */
4818 with_params = std::string (sym) + " ( int ) &&";
4819 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4820 {});
4821 }
4822
e1ef7d7a
PA
4823 /* Check that the name matching algorithm for completion doesn't get
4824 confused with Latin1 'ÿ' / 0xff. */
4825 {
4826 static const char str[] = "\377";
4827 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4828 EXPECT ("\377", "\377\377123"));
4829 }
4830
4831 /* Check that the increment-last-char in the matching algorithm for
4832 completion doesn't match "t1_fund" when completing "t1_func". */
4833 {
4834 static const char str[] = "t1_func";
4835 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4836 EXPECT ("t1_func", "t1_func1"));
4837 }
4838
c62446b1
PA
4839 /* Check that completion mode works at each prefix of the expected
4840 symbol name. */
4841 {
4842 static const char str[] = "function(int)";
4843 size_t len = strlen (str);
4844 std::string lookup;
4845
4846 for (size_t i = 1; i < len; i++)
4847 {
4848 lookup.assign (str, i);
4849 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4850 EXPECT ("function"));
4851 }
4852 }
4853
4854 /* While "w" is a prefix of both components, the match function
4855 should still only be called once. */
4856 {
4857 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4858 EXPECT ("w1::w2"));
a20714ff
PA
4859 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4860 EXPECT ("w1::w2"));
c62446b1
PA
4861 }
4862
4863 /* Same, with a "complicated" symbol. */
4864 {
4865 static const char str[] = Z_SYM_NAME;
4866 size_t len = strlen (str);
4867 std::string lookup;
4868
4869 for (size_t i = 1; i < len; i++)
4870 {
4871 lookup.assign (str, i);
4872 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4873 EXPECT (Z_SYM_NAME));
4874 }
4875 }
4876
4877 /* In FULL mode, an incomplete symbol doesn't match. */
4878 {
4879 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4880 {});
4881 }
4882
4883 /* A complete symbol with parameters matches any overload, since the
4884 index has no overload info. */
4885 {
4886 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4888 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4889 EXPECT ("std::zfunction", "std::zfunction2"));
4890 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4891 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list. */
4896 {
4897 static const char expected[] = "ns::foo<int>";
4898 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4899 EXPECT (expected));
a20714ff
PA
4900 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4901 EXPECT (expected));
c62446b1
PA
4902 }
4903
4904 /* Check that whitespace is ignored appropriately. A symbol with a
4905 template argument list that includes a pointer. */
4906 {
4907 static const char expected[] = "ns::foo<char*>";
4908 /* Try both completion and non-completion modes. */
4909 static const bool completion_mode[2] = {false, true};
4910 for (size_t i = 0; i < 2; i++)
4911 {
4912 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4913 completion_mode[i], EXPECT (expected));
a20714ff
PA
4914 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4915 completion_mode[i], EXPECT (expected));
c62446b1
PA
4916
4917 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4918 completion_mode[i], EXPECT (expected));
a20714ff
PA
4919 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4920 completion_mode[i], EXPECT (expected));
c62446b1
PA
4921 }
4922 }
4923
4924 {
4925 /* Check method qualifiers are ignored. */
4926 static const char expected[] = "ns::foo<char*>";
4927 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4928 symbol_name_match_type::FULL, true, EXPECT (expected));
4929 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4930 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4931 CHECK_MATCH ("foo < char * > ( int ) const",
4932 symbol_name_match_type::WILD, true, EXPECT (expected));
4933 CHECK_MATCH ("foo < char * > ( int ) &&",
4934 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4935 }
4936
4937 /* Test lookup names that don't match anything. */
4938 {
a20714ff
PA
4939 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4940 {});
4941
c62446b1
PA
4942 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4943 {});
4944 }
4945
a20714ff
PA
4946 /* Some wild matching tests, exercising "(anonymous namespace)",
4947 which should not be confused with a parameter list. */
4948 {
4949 static const char *syms[] = {
4950 "A::B::C",
4951 "B::C",
4952 "C",
4953 "A :: B :: C ( int )",
4954 "B :: C ( int )",
4955 "C ( int )",
4956 };
4957
4958 for (const char *s : syms)
4959 {
4960 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4961 EXPECT ("(anonymous namespace)::A::B::C"));
4962 }
4963 }
4964
4965 {
4966 static const char expected[] = "ns2::tmpl<int>::foo2";
4967 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4968 EXPECT (expected));
4969 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4970 EXPECT (expected));
4971 }
4972
c62446b1
PA
4973 SELF_CHECK (!any_mismatch);
4974
4975#undef EXPECT
4976#undef CHECK_MATCH
4977}
4978
5c58de74
PA
4979static void
4980run_test ()
4981{
4982 test_mapped_index_find_name_component_bounds ();
4983 test_dw2_expand_symtabs_matching_symbol ();
4984}
4985
c62446b1
PA
4986}} // namespace selftests::dw2_expand_symtabs_matching
4987
4988#endif /* GDB_SELF_TEST */
4989
4b514bc8
JK
4990/* If FILE_MATCHER is NULL or if PER_CU has
4991 dwarf2_per_cu_quick_data::MARK set (see
4992 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4993 EXPANSION_NOTIFY on it. */
4994
4995static void
4996dw2_expand_symtabs_matching_one
4997 (struct dwarf2_per_cu_data *per_cu,
4998 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4999 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5000{
5001 if (file_matcher == NULL || per_cu->v.quick->mark)
5002 {
5003 bool symtab_was_null
5004 = (per_cu->v.quick->compunit_symtab == NULL);
5005
58f0c718 5006 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5007
5008 if (expansion_notify != NULL
5009 && symtab_was_null
5010 && per_cu->v.quick->compunit_symtab != NULL)
5011 expansion_notify (per_cu->v.quick->compunit_symtab);
5012 }
5013}
5014
3f563c84
PA
5015/* Helper for dw2_expand_matching symtabs. Called on each symbol
5016 matched, to expand corresponding CUs that were marked. IDX is the
5017 index of the symbol name that matched. */
5018
5019static void
5020dw2_expand_marked_cus
ed2dc618 5021 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5022 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5023 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5024 search_domain kind)
5025{
3f563c84
PA
5026 offset_type *vec, vec_len, vec_idx;
5027 bool global_seen = false;
ed2dc618 5028 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5029
61920122 5030 vec = (offset_type *) (index.constant_pool
f00a2de2 5031 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5032 vec_len = MAYBE_SWAP (vec[0]);
5033 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5034 {
61920122
PA
5035 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5036 /* This value is only valid for index versions >= 7. */
5037 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5038 gdb_index_symbol_kind symbol_kind =
5039 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5040 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5041 /* Only check the symbol attributes if they're present.
5042 Indices prior to version 7 don't record them,
5043 and indices >= 7 may elide them for certain symbols
5044 (gold does this). */
5045 int attrs_valid =
5046 (index.version >= 7
5047 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5048
5049 /* Work around gold/15646. */
5050 if (attrs_valid)
9291a0cd 5051 {
61920122
PA
5052 if (!is_static && global_seen)
5053 continue;
5054 if (!is_static)
5055 global_seen = true;
5056 }
3190f0c6 5057
61920122
PA
5058 /* Only check the symbol's kind if it has one. */
5059 if (attrs_valid)
5060 {
5061 switch (kind)
8943b874 5062 {
61920122
PA
5063 case VARIABLES_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5065 continue;
5066 break;
5067 case FUNCTIONS_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5069 continue;
61920122
PA
5070 break;
5071 case TYPES_DOMAIN:
5072 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5073 continue;
5074 break;
59c35742
AB
5075 case MODULES_DOMAIN:
5076 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5077 continue;
5078 break;
61920122
PA
5079 default:
5080 break;
8943b874 5081 }
61920122 5082 }
8943b874 5083
61920122 5084 /* Don't crash on bad data. */
b76e467d 5085 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5086 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5087 {
b98664d3 5088 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5089 " [in module %s]"),
5090 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5091 continue;
5092 }
5093
ff4c9fec 5094 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5095 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5096 expansion_notify);
61920122
PA
5097 }
5098}
5099
4b514bc8
JK
5100/* If FILE_MATCHER is non-NULL, set all the
5101 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5102 that match FILE_MATCHER. */
5103
61920122 5104static void
4b514bc8 5105dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5106 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5107 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5108{
4b514bc8 5109 if (file_matcher == NULL)
61920122
PA
5110 return;
5111
4b514bc8
JK
5112 objfile *const objfile = dwarf2_per_objfile->objfile;
5113
5114 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5115 htab_eq_pointer,
5116 NULL, xcalloc, xfree));
5117 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5118 htab_eq_pointer,
5119 NULL, xcalloc, xfree));
61920122 5120
4b514bc8
JK
5121 /* The rule is CUs specify all the files, including those used by
5122 any TU, so there's no need to scan TUs here. */
61920122 5123
b76e467d 5124 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5125 {
927aa2e7
JK
5126 QUIT;
5127
5128 per_cu->v.quick->mark = 0;
5129
5130 /* We only need to look at symtabs not already expanded. */
5131 if (per_cu->v.quick->compunit_symtab)
5132 continue;
5133
b76e467d 5134 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5135 if (file_data == NULL)
5136 continue;
5137
5138 if (htab_find (visited_not_found.get (), file_data) != NULL)
5139 continue;
5140 else if (htab_find (visited_found.get (), file_data) != NULL)
5141 {
5142 per_cu->v.quick->mark = 1;
5143 continue;
5144 }
5145
b76e467d 5146 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5147 {
5148 const char *this_real_name;
5149
5150 if (file_matcher (file_data->file_names[j], false))
5151 {
5152 per_cu->v.quick->mark = 1;
5153 break;
5154 }
5155
5156 /* Before we invoke realpath, which can get expensive when many
5157 files are involved, do a quick comparison of the basenames. */
5158 if (!basenames_may_differ
5159 && !file_matcher (lbasename (file_data->file_names[j]),
5160 true))
5161 continue;
5162
5163 this_real_name = dw2_get_real_path (objfile, file_data, j);
5164 if (file_matcher (this_real_name, false))
5165 {
5166 per_cu->v.quick->mark = 1;
5167 break;
5168 }
5169 }
5170
b76e467d
SM
5171 void **slot = htab_find_slot (per_cu->v.quick->mark
5172 ? visited_found.get ()
5173 : visited_not_found.get (),
5174 file_data, INSERT);
927aa2e7
JK
5175 *slot = file_data;
5176 }
5177}
5178
5179static void
5180dw2_expand_symtabs_matching
5181 (struct objfile *objfile,
5182 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5183 const lookup_name_info &lookup_name,
5184 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5185 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5186 enum search_domain kind)
5187{
ed2dc618
SM
5188 struct dwarf2_per_objfile *dwarf2_per_objfile
5189 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5190
5191 /* index_table is NULL if OBJF_READNOW. */
5192 if (!dwarf2_per_objfile->index_table)
5193 return;
5194
ed2dc618 5195 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5196
5197 mapped_index &index = *dwarf2_per_objfile->index_table;
5198
5199 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5200 symbol_matcher,
5201 kind, [&] (offset_type idx)
5202 {
ed2dc618 5203 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5204 expansion_notify, kind);
3b00ef10 5205 return true;
927aa2e7
JK
5206 });
5207}
5208
5209/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5210 symtab. */
5211
5212static struct compunit_symtab *
5213recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5214 CORE_ADDR pc)
5215{
5216 int i;
5217
5218 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5219 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5220 return cust;
5221
5222 if (cust->includes == NULL)
5223 return NULL;
5224
5225 for (i = 0; cust->includes[i]; ++i)
5226 {
5227 struct compunit_symtab *s = cust->includes[i];
5228
5229 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5230 if (s != NULL)
5231 return s;
5232 }
5233
5234 return NULL;
5235}
5236
5237static struct compunit_symtab *
5238dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5239 struct bound_minimal_symbol msymbol,
5240 CORE_ADDR pc,
5241 struct obj_section *section,
5242 int warn_if_readin)
5243{
5244 struct dwarf2_per_cu_data *data;
5245 struct compunit_symtab *result;
5246
d320c2b5 5247 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5248 return NULL;
5249
79748972
TT
5250 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5251 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5252 data = (struct dwarf2_per_cu_data *) addrmap_find
5253 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5254 if (!data)
5255 return NULL;
5256
5257 if (warn_if_readin && data->v.quick->compunit_symtab)
5258 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5259 paddress (get_objfile_arch (objfile), pc));
5260
5261 result
58f0c718
TT
5262 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5263 false),
927aa2e7
JK
5264 pc);
5265 gdb_assert (result != NULL);
5266 return result;
5267}
5268
5269static void
5270dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5271 void *data, int need_fullname)
5272{
ed2dc618
SM
5273 struct dwarf2_per_objfile *dwarf2_per_objfile
5274 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5275
5276 if (!dwarf2_per_objfile->filenames_cache)
5277 {
5278 dwarf2_per_objfile->filenames_cache.emplace ();
5279
5280 htab_up visited (htab_create_alloc (10,
5281 htab_hash_pointer, htab_eq_pointer,
5282 NULL, xcalloc, xfree));
5283
5284 /* The rule is CUs specify all the files, including those used
5285 by any TU, so there's no need to scan TUs here. We can
5286 ignore file names coming from already-expanded CUs. */
5287
b76e467d 5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5289 {
927aa2e7
JK
5290 if (per_cu->v.quick->compunit_symtab)
5291 {
5292 void **slot = htab_find_slot (visited.get (),
5293 per_cu->v.quick->file_names,
5294 INSERT);
5295
5296 *slot = per_cu->v.quick->file_names;
5297 }
5298 }
5299
b76e467d 5300 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5301 {
927aa2e7
JK
5302 /* We only need to look at symtabs not already expanded. */
5303 if (per_cu->v.quick->compunit_symtab)
5304 continue;
5305
b76e467d 5306 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5307 if (file_data == NULL)
5308 continue;
5309
b76e467d 5310 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5311 if (*slot)
5312 {
5313 /* Already visited. */
5314 continue;
5315 }
5316 *slot = file_data;
5317
5318 for (int j = 0; j < file_data->num_file_names; ++j)
5319 {
5320 const char *filename = file_data->file_names[j];
5321 dwarf2_per_objfile->filenames_cache->seen (filename);
5322 }
5323 }
5324 }
5325
5326 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5327 {
5328 gdb::unique_xmalloc_ptr<char> this_real_name;
5329
5330 if (need_fullname)
5331 this_real_name = gdb_realpath (filename);
5332 (*fun) (filename, this_real_name.get (), data);
5333 });
5334}
5335
5336static int
5337dw2_has_symbols (struct objfile *objfile)
5338{
5339 return 1;
5340}
5341
5342const struct quick_symbol_functions dwarf2_gdb_index_functions =
5343{
5344 dw2_has_symbols,
5345 dw2_find_last_source_symtab,
5346 dw2_forget_cached_source_info,
5347 dw2_map_symtabs_matching_filename,
5348 dw2_lookup_symbol,
5349 dw2_print_stats,
5350 dw2_dump,
927aa2e7
JK
5351 dw2_expand_symtabs_for_function,
5352 dw2_expand_all_symtabs,
5353 dw2_expand_symtabs_with_fullname,
5354 dw2_map_matching_symbols,
5355 dw2_expand_symtabs_matching,
5356 dw2_find_pc_sect_compunit_symtab,
5357 NULL,
5358 dw2_map_symbol_filenames
5359};
5360
5361/* DWARF-5 debug_names reader. */
5362
5363/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5364static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5365
5366/* A helper function that reads the .debug_names section in SECTION
5367 and fills in MAP. FILENAME is the name of the file containing the
5368 section; it is used for error reporting.
5369
5370 Returns true if all went well, false otherwise. */
5371
5372static bool
5373read_debug_names_from_section (struct objfile *objfile,
5374 const char *filename,
5375 struct dwarf2_section_info *section,
5376 mapped_debug_names &map)
5377{
5378 if (dwarf2_section_empty_p (section))
5379 return false;
5380
5381 /* Older elfutils strip versions could keep the section in the main
5382 executable while splitting it for the separate debug info file. */
5383 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5384 return false;
5385
5386 dwarf2_read_section (objfile, section);
5387
5388 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5389
5390 const gdb_byte *addr = section->buffer;
5391
5392 bfd *const abfd = get_section_bfd_owner (section);
5393
5394 unsigned int bytes_read;
5395 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5396 addr += bytes_read;
5397
5398 map.dwarf5_is_dwarf64 = bytes_read != 4;
5399 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5400 if (bytes_read + length != section->size)
5401 {
5402 /* There may be multiple per-CU indices. */
5403 warning (_("Section .debug_names in %s length %s does not match "
5404 "section length %s, ignoring .debug_names."),
5405 filename, plongest (bytes_read + length),
5406 pulongest (section->size));
5407 return false;
5408 }
5409
5410 /* The version number. */
5411 uint16_t version = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (version != 5)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported version %d, "
5416 "ignoring .debug_names."),
5417 filename, version);
5418 return false;
5419 }
5420
5421 /* Padding. */
5422 uint16_t padding = read_2_bytes (abfd, addr);
5423 addr += 2;
5424 if (padding != 0)
5425 {
5426 warning (_("Section .debug_names in %s has unsupported padding %d, "
5427 "ignoring .debug_names."),
5428 filename, padding);
5429 return false;
5430 }
5431
5432 /* comp_unit_count - The number of CUs in the CU list. */
5433 map.cu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435
5436 /* local_type_unit_count - The number of TUs in the local TU
5437 list. */
5438 map.tu_count = read_4_bytes (abfd, addr);
5439 addr += 4;
5440
5441 /* foreign_type_unit_count - The number of TUs in the foreign TU
5442 list. */
5443 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445 if (foreign_tu_count != 0)
5446 {
5447 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5448 "ignoring .debug_names."),
5449 filename, static_cast<unsigned long> (foreign_tu_count));
5450 return false;
5451 }
5452
5453 /* bucket_count - The number of hash buckets in the hash lookup
5454 table. */
5455 map.bucket_count = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* name_count - The number of unique names in the index. */
5459 map.name_count = read_4_bytes (abfd, addr);
5460 addr += 4;
5461
5462 /* abbrev_table_size - The size in bytes of the abbreviations
5463 table. */
5464 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5465 addr += 4;
5466
5467 /* augmentation_string_size - The size in bytes of the augmentation
5468 string. This value is rounded up to a multiple of 4. */
5469 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5470 addr += 4;
5471 map.augmentation_is_gdb = ((augmentation_string_size
5472 == sizeof (dwarf5_augmentation))
5473 && memcmp (addr, dwarf5_augmentation,
5474 sizeof (dwarf5_augmentation)) == 0);
5475 augmentation_string_size += (-augmentation_string_size) & 3;
5476 addr += augmentation_string_size;
5477
5478 /* List of CUs */
5479 map.cu_table_reordered = addr;
5480 addr += map.cu_count * map.offset_size;
5481
5482 /* List of Local TUs */
5483 map.tu_table_reordered = addr;
5484 addr += map.tu_count * map.offset_size;
5485
5486 /* Hash Lookup Table */
5487 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5488 addr += map.bucket_count * 4;
5489 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5490 addr += map.name_count * 4;
5491
5492 /* Name Table */
5493 map.name_table_string_offs_reordered = addr;
5494 addr += map.name_count * map.offset_size;
5495 map.name_table_entry_offs_reordered = addr;
5496 addr += map.name_count * map.offset_size;
5497
5498 const gdb_byte *abbrev_table_start = addr;
5499 for (;;)
5500 {
927aa2e7
JK
5501 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503 if (index_num == 0)
5504 break;
5505
5506 const auto insertpair
5507 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5508 if (!insertpair.second)
5509 {
5510 warning (_("Section .debug_names in %s has duplicate index %s, "
5511 "ignoring .debug_names."),
5512 filename, pulongest (index_num));
5513 return false;
5514 }
5515 mapped_debug_names::index_val &indexval = insertpair.first->second;
5516 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518
5519 for (;;)
5520 {
5521 mapped_debug_names::index_val::attr attr;
5522 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5523 addr += bytes_read;
5524 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5525 addr += bytes_read;
5526 if (attr.form == DW_FORM_implicit_const)
5527 {
5528 attr.implicit_const = read_signed_leb128 (abfd, addr,
5529 &bytes_read);
5530 addr += bytes_read;
5531 }
5532 if (attr.dw_idx == 0 && attr.form == 0)
5533 break;
5534 indexval.attr_vec.push_back (std::move (attr));
5535 }
5536 }
5537 if (addr != abbrev_table_start + abbrev_table_size)
5538 {
5539 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5540 "of size %s vs. written as %u, ignoring .debug_names."),
5541 filename, plongest (addr - abbrev_table_start),
5542 abbrev_table_size);
927aa2e7
JK
5543 return false;
5544 }
5545 map.entry_pool = addr;
5546
5547 return true;
5548}
5549
5550/* A helper for create_cus_from_debug_names that handles the MAP's CU
5551 list. */
5552
5553static void
ed2dc618 5554create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5555 const mapped_debug_names &map,
5556 dwarf2_section_info &section,
b76e467d 5557 bool is_dwz)
927aa2e7
JK
5558{
5559 sect_offset sect_off_prev;
5560 for (uint32_t i = 0; i <= map.cu_count; ++i)
5561 {
5562 sect_offset sect_off_next;
5563 if (i < map.cu_count)
5564 {
5565 sect_off_next
5566 = (sect_offset) (extract_unsigned_integer
5567 (map.cu_table_reordered + i * map.offset_size,
5568 map.offset_size,
5569 map.dwarf5_byte_order));
5570 }
5571 else
5572 sect_off_next = (sect_offset) section.size;
5573 if (i >= 1)
5574 {
5575 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5576 dwarf2_per_cu_data *per_cu
ed2dc618 5577 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5578 sect_off_prev, length);
b76e467d 5579 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5580 }
5581 sect_off_prev = sect_off_next;
5582 }
5583}
5584
5585/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5586 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5587
5588static void
ed2dc618 5589create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5590 const mapped_debug_names &map,
5591 const mapped_debug_names &dwz_map)
5592{
b76e467d
SM
5593 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5594 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5595
ed2dc618
SM
5596 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5597 dwarf2_per_objfile->info,
b76e467d 5598 false /* is_dwz */);
927aa2e7
JK
5599
5600 if (dwz_map.cu_count == 0)
5601 return;
5602
ed2dc618
SM
5603 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5604 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5605 true /* is_dwz */);
927aa2e7
JK
5606}
5607
5608/* Read .debug_names. If everything went ok, initialize the "quick"
5609 elements of all the CUs and return true. Otherwise, return false. */
5610
5611static bool
ed2dc618 5612dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5613{
22ca247e
TT
5614 std::unique_ptr<mapped_debug_names> map
5615 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5616 mapped_debug_names dwz_map (dwarf2_per_objfile);
5617 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5618
5619 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5620 &dwarf2_per_objfile->debug_names,
22ca247e 5621 *map))
927aa2e7
JK
5622 return false;
5623
5624 /* Don't use the index if it's empty. */
22ca247e 5625 if (map->name_count == 0)
927aa2e7
JK
5626 return false;
5627
5628 /* If there is a .dwz file, read it so we can get its CU list as
5629 well. */
ed2dc618 5630 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5631 if (dwz != NULL)
5632 {
5633 if (!read_debug_names_from_section (objfile,
00f93c44 5634 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5635 &dwz->debug_names, dwz_map))
5636 {
5637 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5638 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5639 return false;
5640 }
5641 }
5642
22ca247e 5643 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5644
22ca247e 5645 if (map->tu_count != 0)
927aa2e7
JK
5646 {
5647 /* We can only handle a single .debug_types when we have an
5648 index. */
fd5866f6 5649 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5650 return false;
5651
fd5866f6 5652 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5653
5654 create_signatured_type_table_from_debug_names
22ca247e 5655 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5656 }
5657
ed2dc618
SM
5658 create_addrmap_from_aranges (dwarf2_per_objfile,
5659 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5660
22ca247e 5661 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5662 dwarf2_per_objfile->using_index = 1;
5663 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5664 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5665
5666 return true;
5667}
5668
927aa2e7
JK
5669/* Type used to manage iterating over all CUs looking for a symbol for
5670 .debug_names. */
5671
5672class dw2_debug_names_iterator
5673{
5674public:
927aa2e7 5675 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5676 gdb::optional<block_enum> block_index,
5677 domain_enum domain,
927aa2e7 5678 const char *name)
2b79f376 5679 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5680 m_addr (find_vec_in_debug_names (map, name))
5681 {}
5682
5683 dw2_debug_names_iterator (const mapped_debug_names &map,
5684 search_domain search, uint32_t namei)
5685 : m_map (map),
5686 m_search (search),
5687 m_addr (find_vec_in_debug_names (map, namei))
5688 {}
5689
3b00ef10
TT
5690 dw2_debug_names_iterator (const mapped_debug_names &map,
5691 block_enum block_index, domain_enum domain,
5692 uint32_t namei)
5693 : m_map (map), m_block_index (block_index), m_domain (domain),
5694 m_addr (find_vec_in_debug_names (map, namei))
5695 {}
5696
927aa2e7
JK
5697 /* Return the next matching CU or NULL if there are no more. */
5698 dwarf2_per_cu_data *next ();
5699
5700private:
5701 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5702 const char *name);
5703 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5704 uint32_t namei);
5705
5706 /* The internalized form of .debug_names. */
5707 const mapped_debug_names &m_map;
5708
2b79f376
SM
5709 /* If set, only look for symbols that match that block. Valid values are
5710 GLOBAL_BLOCK and STATIC_BLOCK. */
5711 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5712
5713 /* The kind of symbol we're looking for. */
5714 const domain_enum m_domain = UNDEF_DOMAIN;
5715 const search_domain m_search = ALL_DOMAIN;
5716
5717 /* The list of CUs from the index entry of the symbol, or NULL if
5718 not found. */
5719 const gdb_byte *m_addr;
5720};
5721
5722const char *
5723mapped_debug_names::namei_to_name (uint32_t namei) const
5724{
5725 const ULONGEST namei_string_offs
5726 = extract_unsigned_integer ((name_table_string_offs_reordered
5727 + namei * offset_size),
5728 offset_size,
5729 dwarf5_byte_order);
5730 return read_indirect_string_at_offset
ed2dc618 5731 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5732}
5733
5734/* Find a slot in .debug_names for the object named NAME. If NAME is
5735 found, return pointer to its pool data. If NAME cannot be found,
5736 return NULL. */
5737
5738const gdb_byte *
5739dw2_debug_names_iterator::find_vec_in_debug_names
5740 (const mapped_debug_names &map, const char *name)
5741{
5742 int (*cmp) (const char *, const char *);
5743
54ee4252 5744 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5745 if (current_language->la_language == language_cplus
5746 || current_language->la_language == language_fortran
5747 || current_language->la_language == language_d)
5748 {
5749 /* NAME is already canonical. Drop any qualifiers as
5750 .debug_names does not contain any. */
5751
5752 if (strchr (name, '(') != NULL)
5753 {
54ee4252 5754 without_params = cp_remove_params (name);
927aa2e7 5755 if (without_params != NULL)
54ee4252 5756 name = without_params.get ();
927aa2e7
JK
5757 }
5758 }
5759
5760 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5761
5762 const uint32_t full_hash = dwarf5_djb_hash (name);
5763 uint32_t namei
5764 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5765 (map.bucket_table_reordered
5766 + (full_hash % map.bucket_count)), 4,
5767 map.dwarf5_byte_order);
5768 if (namei == 0)
5769 return NULL;
5770 --namei;
5771 if (namei >= map.name_count)
5772 {
b98664d3 5773 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5774 "[in module %s]"),
5775 namei, map.name_count,
ed2dc618 5776 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5777 return NULL;
5778 }
5779
5780 for (;;)
5781 {
5782 const uint32_t namei_full_hash
5783 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5784 (map.hash_table_reordered + namei), 4,
5785 map.dwarf5_byte_order);
5786 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5787 return NULL;
5788
5789 if (full_hash == namei_full_hash)
5790 {
5791 const char *const namei_string = map.namei_to_name (namei);
5792
5793#if 0 /* An expensive sanity check. */
5794 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5795 {
b98664d3 5796 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5797 "[in module %s]"),
5798 namei, objfile_name (dwarf2_per_objfile->objfile));
5799 return NULL;
5800 }
5801#endif
5802
5803 if (cmp (namei_string, name) == 0)
5804 {
5805 const ULONGEST namei_entry_offs
5806 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5807 + namei * map.offset_size),
5808 map.offset_size, map.dwarf5_byte_order);
5809 return map.entry_pool + namei_entry_offs;
5810 }
5811 }
5812
5813 ++namei;
5814 if (namei >= map.name_count)
5815 return NULL;
5816 }
5817}
5818
5819const gdb_byte *
5820dw2_debug_names_iterator::find_vec_in_debug_names
5821 (const mapped_debug_names &map, uint32_t namei)
5822{
5823 if (namei >= map.name_count)
5824 {
b98664d3 5825 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5826 "[in module %s]"),
5827 namei, map.name_count,
ed2dc618 5828 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5829 return NULL;
5830 }
5831
5832 const ULONGEST namei_entry_offs
5833 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5834 + namei * map.offset_size),
5835 map.offset_size, map.dwarf5_byte_order);
5836 return map.entry_pool + namei_entry_offs;
5837}
5838
5839/* See dw2_debug_names_iterator. */
5840
5841dwarf2_per_cu_data *
5842dw2_debug_names_iterator::next ()
5843{
5844 if (m_addr == NULL)
5845 return NULL;
5846
ed2dc618
SM
5847 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5848 struct objfile *objfile = dwarf2_per_objfile->objfile;
5849 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5850
5851 again:
5852
5853 unsigned int bytes_read;
5854 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5855 m_addr += bytes_read;
5856 if (abbrev == 0)
5857 return NULL;
5858
5859 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5860 if (indexval_it == m_map.abbrev_map.cend ())
5861 {
b98664d3 5862 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5863 "[in module %s]"),
ed2dc618 5864 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5865 return NULL;
5866 }
5867 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5868 enum class symbol_linkage {
5869 unknown,
5870 static_,
5871 extern_,
23c13d42 5872 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5873 dwarf2_per_cu_data *per_cu = NULL;
5874 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5875 {
5876 ULONGEST ull;
5877 switch (attr.form)
5878 {
5879 case DW_FORM_implicit_const:
5880 ull = attr.implicit_const;
5881 break;
5882 case DW_FORM_flag_present:
5883 ull = 1;
5884 break;
5885 case DW_FORM_udata:
5886 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5887 m_addr += bytes_read;
5888 break;
5889 default:
b98664d3 5890 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5891 dwarf_form_name (attr.form),
ed2dc618 5892 objfile_name (objfile));
927aa2e7
JK
5893 return NULL;
5894 }
5895 switch (attr.dw_idx)
5896 {
5897 case DW_IDX_compile_unit:
5898 /* Don't crash on bad data. */
b76e467d 5899 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5900 {
b98664d3 5901 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
ff4c9fec 5907 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5908 break;
8af5c486
JK
5909 case DW_IDX_type_unit:
5910 /* Don't crash on bad data. */
b2bdb8cf 5911 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5912 {
b98664d3 5913 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5914 " [in module %s]"),
5915 pulongest (ull),
5916 objfile_name (dwarf2_per_objfile->objfile));
5917 continue;
5918 }
ff4c9fec 5919 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5920 break;
927aa2e7
JK
5921 case DW_IDX_GNU_internal:
5922 if (!m_map.augmentation_is_gdb)
5923 break;
23c13d42 5924 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5925 break;
5926 case DW_IDX_GNU_external:
5927 if (!m_map.augmentation_is_gdb)
5928 break;
23c13d42 5929 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5930 break;
5931 }
5932 }
5933
5934 /* Skip if already read in. */
5935 if (per_cu->v.quick->compunit_symtab)
5936 goto again;
5937
5938 /* Check static vs global. */
23c13d42 5939 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5940 {
2b79f376 5941 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5942 const bool symbol_is_static =
5943 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5944 if (want_static != symbol_is_static)
2b79f376 5945 goto again;
927aa2e7
JK
5946 }
5947
5948 /* Match dw2_symtab_iter_next, symbol_kind
5949 and debug_names::psymbol_tag. */
5950 switch (m_domain)
5951 {
5952 case VAR_DOMAIN:
5953 switch (indexval.dwarf_tag)
5954 {
5955 case DW_TAG_variable:
5956 case DW_TAG_subprogram:
5957 /* Some types are also in VAR_DOMAIN. */
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case STRUCT_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case DW_TAG_typedef:
5969 case DW_TAG_structure_type:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 case LABEL_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case 0:
5979 case DW_TAG_variable:
5980 break;
5981 default:
5982 goto again;
5983 }
5984 break;
59c35742
AB
5985 case MODULE_DOMAIN:
5986 switch (indexval.dwarf_tag)
5987 {
5988 case DW_TAG_module:
5989 break;
5990 default:
5991 goto again;
5992 }
5993 break;
927aa2e7
JK
5994 default:
5995 break;
5996 }
5997
5998 /* Match dw2_expand_symtabs_matching, symbol_kind and
5999 debug_names::psymbol_tag. */
6000 switch (m_search)
4b514bc8 6001 {
927aa2e7
JK
6002 case VARIABLES_DOMAIN:
6003 switch (indexval.dwarf_tag)
4b514bc8 6004 {
927aa2e7
JK
6005 case DW_TAG_variable:
6006 break;
6007 default:
6008 goto again;
4b514bc8 6009 }
927aa2e7
JK
6010 break;
6011 case FUNCTIONS_DOMAIN:
6012 switch (indexval.dwarf_tag)
4b514bc8 6013 {
927aa2e7
JK
6014 case DW_TAG_subprogram:
6015 break;
6016 default:
6017 goto again;
4b514bc8 6018 }
927aa2e7
JK
6019 break;
6020 case TYPES_DOMAIN:
6021 switch (indexval.dwarf_tag)
6022 {
6023 case DW_TAG_typedef:
6024 case DW_TAG_structure_type:
6025 break;
6026 default:
6027 goto again;
6028 }
6029 break;
59c35742
AB
6030 case MODULES_DOMAIN:
6031 switch (indexval.dwarf_tag)
6032 {
6033 case DW_TAG_module:
6034 break;
6035 default:
6036 goto again;
6037 }
927aa2e7
JK
6038 default:
6039 break;
4b514bc8 6040 }
927aa2e7
JK
6041
6042 return per_cu;
4b514bc8 6043}
61920122 6044
927aa2e7 6045static struct compunit_symtab *
c7f839cb 6046dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6047 const char *name, domain_enum domain)
4b514bc8 6048{
ed2dc618
SM
6049 struct dwarf2_per_objfile *dwarf2_per_objfile
6050 = get_dwarf2_per_objfile (objfile);
61920122 6051
927aa2e7
JK
6052 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6053 if (!mapp)
61920122 6054 {
927aa2e7
JK
6055 /* index is NULL if OBJF_READNOW. */
6056 return NULL;
6057 }
6058 const auto &map = *mapp;
9291a0cd 6059
2b79f376 6060 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6061
927aa2e7
JK
6062 struct compunit_symtab *stab_best = NULL;
6063 struct dwarf2_per_cu_data *per_cu;
6064 while ((per_cu = iter.next ()) != NULL)
6065 {
6066 struct symbol *sym, *with_opaque = NULL;
58f0c718 6067 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6068 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6069 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6070
927aa2e7
JK
6071 sym = block_find_symbol (block, name, domain,
6072 block_find_non_opaque_type_preferred,
6073 &with_opaque);
9703b513 6074
927aa2e7
JK
6075 /* Some caution must be observed with overloaded functions and
6076 methods, since the index will not contain any overload
6077 information (but NAME might contain it). */
a3ec0bb1 6078
927aa2e7 6079 if (sym != NULL
987012b8 6080 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6081 return stab;
6082 if (with_opaque != NULL
987012b8 6083 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6084 stab_best = stab;
9703b513 6085
927aa2e7 6086 /* Keep looking through other CUs. */
9703b513
TT
6087 }
6088
927aa2e7 6089 return stab_best;
9703b513
TT
6090}
6091
927aa2e7
JK
6092/* This dumps minimal information about .debug_names. It is called
6093 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6094 uses this to verify that .debug_names has been loaded. */
9291a0cd 6095
927aa2e7
JK
6096static void
6097dw2_debug_names_dump (struct objfile *objfile)
6098{
ed2dc618
SM
6099 struct dwarf2_per_objfile *dwarf2_per_objfile
6100 = get_dwarf2_per_objfile (objfile);
6101
927aa2e7
JK
6102 gdb_assert (dwarf2_per_objfile->using_index);
6103 printf_filtered (".debug_names:");
6104 if (dwarf2_per_objfile->debug_names_table)
6105 printf_filtered (" exists\n");
6106 else
6107 printf_filtered (" faked for \"readnow\"\n");
6108 printf_filtered ("\n");
9291a0cd
TT
6109}
6110
9291a0cd 6111static void
927aa2e7
JK
6112dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6113 const char *func_name)
9291a0cd 6114{
ed2dc618
SM
6115 struct dwarf2_per_objfile *dwarf2_per_objfile
6116 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6117
927aa2e7
JK
6118 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6119 if (dwarf2_per_objfile->debug_names_table)
24c79950 6120 {
927aa2e7 6121 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6122
2b79f376 6123 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6124
927aa2e7
JK
6125 struct dwarf2_per_cu_data *per_cu;
6126 while ((per_cu = iter.next ()) != NULL)
58f0c718 6127 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6128 }
6129}
24c79950 6130
3b00ef10
TT
6131static void
6132dw2_debug_names_map_matching_symbols
6133 (struct objfile *objfile,
6134 const lookup_name_info &name, domain_enum domain,
6135 int global,
6136 gdb::function_view<symbol_found_callback_ftype> callback,
6137 symbol_compare_ftype *ordered_compare)
6138{
6139 struct dwarf2_per_objfile *dwarf2_per_objfile
6140 = get_dwarf2_per_objfile (objfile);
6141
6142 /* debug_names_table is NULL if OBJF_READNOW. */
6143 if (!dwarf2_per_objfile->debug_names_table)
6144 return;
6145
6146 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6147 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6148
6149 const char *match_name = name.ada ().lookup_name ().c_str ();
6150 auto matcher = [&] (const char *symname)
6151 {
6152 if (ordered_compare == nullptr)
6153 return true;
6154 return ordered_compare (symname, match_name) == 0;
6155 };
6156
6157 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6158 [&] (offset_type namei)
6159 {
6160 /* The name was matched, now expand corresponding CUs that were
6161 marked. */
6162 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6163
6164 struct dwarf2_per_cu_data *per_cu;
6165 while ((per_cu = iter.next ()) != NULL)
6166 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6167 return true;
6168 });
6169
6170 /* It's a shame we couldn't do this inside the
6171 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6172 that have already been expanded. Instead, this loop matches what
6173 the psymtab code does. */
6174 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6175 {
6176 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6177 if (cust != nullptr)
6178 {
6179 const struct block *block
6180 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6181 if (!iterate_over_symbols_terminated (block, name,
6182 domain, callback))
6183 break;
6184 }
6185 }
6186}
6187
927aa2e7
JK
6188static void
6189dw2_debug_names_expand_symtabs_matching
6190 (struct objfile *objfile,
6191 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6192 const lookup_name_info &lookup_name,
6193 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6194 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6195 enum search_domain kind)
6196{
ed2dc618
SM
6197 struct dwarf2_per_objfile *dwarf2_per_objfile
6198 = get_dwarf2_per_objfile (objfile);
9291a0cd 6199
927aa2e7
JK
6200 /* debug_names_table is NULL if OBJF_READNOW. */
6201 if (!dwarf2_per_objfile->debug_names_table)
6202 return;
9291a0cd 6203
ed2dc618 6204 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6205
44ed8f3e 6206 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6207
44ed8f3e
PA
6208 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6209 symbol_matcher,
6210 kind, [&] (offset_type namei)
927aa2e7 6211 {
927aa2e7
JK
6212 /* The name was matched, now expand corresponding CUs that were
6213 marked. */
6214 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6215
927aa2e7
JK
6216 struct dwarf2_per_cu_data *per_cu;
6217 while ((per_cu = iter.next ()) != NULL)
6218 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6219 expansion_notify);
3b00ef10 6220 return true;
44ed8f3e 6221 });
9291a0cd
TT
6222}
6223
927aa2e7 6224const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6225{
6226 dw2_has_symbols,
6227 dw2_find_last_source_symtab,
6228 dw2_forget_cached_source_info,
f8eba3c6 6229 dw2_map_symtabs_matching_filename,
927aa2e7 6230 dw2_debug_names_lookup_symbol,
9291a0cd 6231 dw2_print_stats,
927aa2e7 6232 dw2_debug_names_dump,
927aa2e7 6233 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6234 dw2_expand_all_symtabs,
652a8996 6235 dw2_expand_symtabs_with_fullname,
3b00ef10 6236 dw2_debug_names_map_matching_symbols,
927aa2e7 6237 dw2_debug_names_expand_symtabs_matching,
43f3e411 6238 dw2_find_pc_sect_compunit_symtab,
71a3c369 6239 NULL,
9291a0cd
TT
6240 dw2_map_symbol_filenames
6241};
6242
4485a1c1
SM
6243/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6244 to either a dwarf2_per_objfile or dwz_file object. */
6245
6246template <typename T>
6247static gdb::array_view<const gdb_byte>
6248get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6249{
6250 dwarf2_section_info *section = &section_owner->gdb_index;
6251
6252 if (dwarf2_section_empty_p (section))
6253 return {};
6254
6255 /* Older elfutils strip versions could keep the section in the main
6256 executable while splitting it for the separate debug info file. */
6257 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6258 return {};
6259
6260 dwarf2_read_section (obj, section);
6261
8bebfcda
PA
6262 /* dwarf2_section_info::size is a bfd_size_type, while
6263 gdb::array_view works with size_t. On 32-bit hosts, with
6264 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6265 is 32-bit. So we need an explicit narrowing conversion here.
6266 This is fine, because it's impossible to allocate or mmap an
6267 array/buffer larger than what size_t can represent. */
6268 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6269}
6270
87d6a7aa
SM
6271/* Lookup the index cache for the contents of the index associated to
6272 DWARF2_OBJ. */
6273
6274static gdb::array_view<const gdb_byte>
6275get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6276{
6277 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6278 if (build_id == nullptr)
6279 return {};
6280
6281 return global_index_cache.lookup_gdb_index (build_id,
6282 &dwarf2_obj->index_cache_res);
6283}
6284
6285/* Same as the above, but for DWZ. */
6286
6287static gdb::array_view<const gdb_byte>
6288get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6289{
6290 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6291 if (build_id == nullptr)
6292 return {};
6293
6294 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6295}
6296
3c0aa29a 6297/* See symfile.h. */
9291a0cd 6298
3c0aa29a
PA
6299bool
6300dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6301{
ed2dc618
SM
6302 struct dwarf2_per_objfile *dwarf2_per_objfile
6303 = get_dwarf2_per_objfile (objfile);
6304
9291a0cd
TT
6305 /* If we're about to read full symbols, don't bother with the
6306 indices. In this case we also don't care if some other debug
6307 format is making psymtabs, because they are all about to be
6308 expanded anyway. */
6309 if ((objfile->flags & OBJF_READNOW))
6310 {
9291a0cd 6311 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6312 create_all_comp_units (dwarf2_per_objfile);
6313 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6314 dwarf2_per_objfile->quick_file_names_table
6315 = create_quick_file_names_table
6316 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6317
b76e467d 6318 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6319 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6320 {
ff4c9fec 6321 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6322
e254ef6a
DE
6323 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6324 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6325 }
6326
6327 /* Return 1 so that gdb sees the "quick" functions. However,
6328 these functions will be no-ops because we will have expanded
6329 all symtabs. */
3c0aa29a
PA
6330 *index_kind = dw_index_kind::GDB_INDEX;
6331 return true;
9291a0cd
TT
6332 }
6333
ed2dc618 6334 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6335 {
6336 *index_kind = dw_index_kind::DEBUG_NAMES;
6337 return true;
6338 }
927aa2e7 6339
4485a1c1
SM
6340 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6341 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6342 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6343 {
6344 *index_kind = dw_index_kind::GDB_INDEX;
6345 return true;
6346 }
9291a0cd 6347
87d6a7aa
SM
6348 /* ... otherwise, try to find the index in the index cache. */
6349 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6350 get_gdb_index_contents_from_cache,
6351 get_gdb_index_contents_from_cache_dwz))
6352 {
6353 global_index_cache.hit ();
6354 *index_kind = dw_index_kind::GDB_INDEX;
6355 return true;
6356 }
6357
6358 global_index_cache.miss ();
3c0aa29a 6359 return false;
9291a0cd
TT
6360}
6361
6362\f
6363
dce234bc
PP
6364/* Build a partial symbol table. */
6365
6366void
f29dff0a 6367dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6368{
ed2dc618
SM
6369 struct dwarf2_per_objfile *dwarf2_per_objfile
6370 = get_dwarf2_per_objfile (objfile);
c9bf0622 6371
6eee24ce 6372 init_psymbol_list (objfile, 1024);
c906108c 6373
a70b8144 6374 try
c9bf0622
TT
6375 {
6376 /* This isn't really ideal: all the data we allocate on the
6377 objfile's obstack is still uselessly kept around. However,
6378 freeing it seems unsafe. */
906768f9 6379 psymtab_discarder psymtabs (objfile);
ed2dc618 6380 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6381 psymtabs.keep ();
87d6a7aa
SM
6382
6383 /* (maybe) store an index in the cache. */
6384 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6385 }
230d2906 6386 catch (const gdb_exception_error &except)
492d29ea
PA
6387 {
6388 exception_print (gdb_stderr, except);
6389 }
c906108c 6390}
c906108c 6391
1ce1cefd
DE
6392/* Return the total length of the CU described by HEADER. */
6393
6394static unsigned int
6395get_cu_length (const struct comp_unit_head *header)
6396{
6397 return header->initial_length_size + header->length;
6398}
6399
9c541725 6400/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6401
9c541725
PA
6402static inline bool
6403offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6404{
9c541725
PA
6405 sect_offset bottom = cu_header->sect_off;
6406 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6407
9c541725 6408 return sect_off >= bottom && sect_off < top;
45452591
DE
6409}
6410
3b80fe9b
DE
6411/* Find the base address of the compilation unit for range lists and
6412 location lists. It will normally be specified by DW_AT_low_pc.
6413 In DWARF-3 draft 4, the base address could be overridden by
6414 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6415 compilation units with discontinuous ranges. */
6416
6417static void
6418dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6419{
6420 struct attribute *attr;
6421
6422 cu->base_known = 0;
6423 cu->base_address = 0;
6424
6425 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6426 if (attr != nullptr)
3b80fe9b 6427 {
31aa7e4e 6428 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6429 cu->base_known = 1;
6430 }
6431 else
6432 {
6433 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6434 if (attr != nullptr)
3b80fe9b 6435 {
31aa7e4e 6436 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6437 cu->base_known = 1;
6438 }
6439 }
6440}
6441
93311388 6442/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6443 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6444 NOTE: This leaves members offset, first_die_offset to be filled in
6445 by the caller. */
107d2387 6446
d521ce57 6447static const gdb_byte *
107d2387 6448read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6449 const gdb_byte *info_ptr,
6450 struct dwarf2_section_info *section,
6451 rcuh_kind section_kind)
107d2387
AC
6452{
6453 int signed_addr;
891d2f0b 6454 unsigned int bytes_read;
43988095
JK
6455 const char *filename = get_section_file_name (section);
6456 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6457
6458 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6459 cu_header->initial_length_size = bytes_read;
6460 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6461 info_ptr += bytes_read;
107d2387 6462 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6463 if (cu_header->version < 2 || cu_header->version > 5)
6464 error (_("Dwarf Error: wrong version in compilation unit header "
6465 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6466 cu_header->version, filename);
107d2387 6467 info_ptr += 2;
43988095
JK
6468 if (cu_header->version < 5)
6469 switch (section_kind)
6470 {
6471 case rcuh_kind::COMPILE:
6472 cu_header->unit_type = DW_UT_compile;
6473 break;
6474 case rcuh_kind::TYPE:
6475 cu_header->unit_type = DW_UT_type;
6476 break;
6477 default:
6478 internal_error (__FILE__, __LINE__,
6479 _("read_comp_unit_head: invalid section_kind"));
6480 }
6481 else
6482 {
6483 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6484 (read_1_byte (abfd, info_ptr));
6485 info_ptr += 1;
6486 switch (cu_header->unit_type)
6487 {
6488 case DW_UT_compile:
a084a2a6
AT
6489 case DW_UT_partial:
6490 case DW_UT_skeleton:
6491 case DW_UT_split_compile:
43988095
JK
6492 if (section_kind != rcuh_kind::COMPILE)
6493 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6494 "(is %s, should be %s) [in module %s]"),
6495 dwarf_unit_type_name (cu_header->unit_type),
6496 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6497 break;
6498 case DW_UT_type:
a084a2a6 6499 case DW_UT_split_type:
43988095
JK
6500 section_kind = rcuh_kind::TYPE;
6501 break;
6502 default:
6503 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6504 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6505 "[in module %s]"), cu_header->unit_type,
6506 dwarf_unit_type_name (DW_UT_compile),
6507 dwarf_unit_type_name (DW_UT_skeleton),
6508 dwarf_unit_type_name (DW_UT_split_compile),
6509 dwarf_unit_type_name (DW_UT_type),
6510 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6511 }
6512
6513 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6514 info_ptr += 1;
6515 }
9c541725
PA
6516 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6517 cu_header,
6518 &bytes_read);
613e1657 6519 info_ptr += bytes_read;
43988095
JK
6520 if (cu_header->version < 5)
6521 {
6522 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6523 info_ptr += 1;
6524 }
107d2387
AC
6525 signed_addr = bfd_get_sign_extend_vma (abfd);
6526 if (signed_addr < 0)
8e65ff28 6527 internal_error (__FILE__, __LINE__,
e2e0b3e5 6528 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6529 cu_header->signed_addr_p = signed_addr;
c764a876 6530
a084a2a6
AT
6531 bool header_has_signature = section_kind == rcuh_kind::TYPE
6532 || cu_header->unit_type == DW_UT_skeleton
6533 || cu_header->unit_type == DW_UT_split_compile;
43988095 6534
a084a2a6
AT
6535 if (header_has_signature)
6536 {
43988095
JK
6537 cu_header->signature = read_8_bytes (abfd, info_ptr);
6538 info_ptr += 8;
a084a2a6 6539 }
43988095 6540
a084a2a6
AT
6541 if (section_kind == rcuh_kind::TYPE)
6542 {
6543 LONGEST type_offset;
43988095
JK
6544 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6545 info_ptr += bytes_read;
9c541725
PA
6546 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6547 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6548 error (_("Dwarf Error: Too big type_offset in compilation unit "
6549 "header (is %s) [in module %s]"), plongest (type_offset),
6550 filename);
6551 }
6552
107d2387
AC
6553 return info_ptr;
6554}
6555
36586728
TT
6556/* Helper function that returns the proper abbrev section for
6557 THIS_CU. */
6558
6559static struct dwarf2_section_info *
6560get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6561{
6562 struct dwarf2_section_info *abbrev;
ed2dc618 6563 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6564
6565 if (this_cu->is_dwz)
ed2dc618 6566 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6567 else
6568 abbrev = &dwarf2_per_objfile->abbrev;
6569
6570 return abbrev;
6571}
6572
9ff913ba
DE
6573/* Subroutine of read_and_check_comp_unit_head and
6574 read_and_check_type_unit_head to simplify them.
6575 Perform various error checking on the header. */
6576
6577static void
ed2dc618
SM
6578error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6579 struct comp_unit_head *header,
4bdcc0c1
DE
6580 struct dwarf2_section_info *section,
6581 struct dwarf2_section_info *abbrev_section)
9ff913ba 6582{
a32a8923 6583 const char *filename = get_section_file_name (section);
9ff913ba 6584
9c541725 6585 if (to_underlying (header->abbrev_sect_off)
36586728 6586 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6587 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6588 "(offset %s + 6) [in module %s]"),
6589 sect_offset_str (header->abbrev_sect_off),
6590 sect_offset_str (header->sect_off),
9ff913ba
DE
6591 filename);
6592
9c541725 6593 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6594 avoid potential 32-bit overflow. */
9c541725 6595 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6596 > section->size)
9c541725 6597 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6598 "(offset %s + 0) [in module %s]"),
6599 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6600 filename);
6601}
6602
6603/* Read in a CU/TU header and perform some basic error checking.
6604 The contents of the header are stored in HEADER.
6605 The result is a pointer to the start of the first DIE. */
adabb602 6606
d521ce57 6607static const gdb_byte *
ed2dc618
SM
6608read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6609 struct comp_unit_head *header,
9ff913ba 6610 struct dwarf2_section_info *section,
4bdcc0c1 6611 struct dwarf2_section_info *abbrev_section,
d521ce57 6612 const gdb_byte *info_ptr,
43988095 6613 rcuh_kind section_kind)
72bf9492 6614{
d521ce57 6615 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6616
9c541725 6617 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6618
43988095 6619 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6620
9c541725 6621 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6622
ed2dc618
SM
6623 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6624 abbrev_section);
9ff913ba
DE
6625
6626 return info_ptr;
348e048f
DE
6627}
6628
f4dc4d17
DE
6629/* Fetch the abbreviation table offset from a comp or type unit header. */
6630
6631static sect_offset
ed2dc618
SM
6632read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6633 struct dwarf2_section_info *section,
9c541725 6634 sect_offset sect_off)
f4dc4d17 6635{
a32a8923 6636 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6637 const gdb_byte *info_ptr;
ac298888 6638 unsigned int initial_length_size, offset_size;
43988095 6639 uint16_t version;
f4dc4d17
DE
6640
6641 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6642 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6643 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6644 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6645 info_ptr += initial_length_size;
6646
6647 version = read_2_bytes (abfd, info_ptr);
6648 info_ptr += 2;
6649 if (version >= 5)
6650 {
6651 /* Skip unit type and address size. */
6652 info_ptr += 2;
6653 }
6654
9c541725 6655 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6656}
6657
aaa75496
JB
6658/* Allocate a new partial symtab for file named NAME and mark this new
6659 partial symtab as being an include of PST. */
6660
6661static void
d521ce57 6662dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6663 struct objfile *objfile)
6664{
6665 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6666
fbd9ab74
JK
6667 if (!IS_ABSOLUTE_PATH (subpst->filename))
6668 {
6669 /* It shares objfile->objfile_obstack. */
6670 subpst->dirname = pst->dirname;
6671 }
6672
a9342b62 6673 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6674 subpst->dependencies[0] = pst;
6675 subpst->number_of_dependencies = 1;
6676
aaa75496 6677 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6678
6679 /* No private part is necessary for include psymtabs. This property
6680 can be used to differentiate between such include psymtabs and
10b3939b 6681 the regular ones. */
58a9656e 6682 subpst->read_symtab_private = NULL;
aaa75496
JB
6683}
6684
6685/* Read the Line Number Program data and extract the list of files
6686 included by the source file represented by PST. Build an include
d85a05f0 6687 partial symtab for each of these included files. */
aaa75496
JB
6688
6689static void
6690dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6691 struct die_info *die,
6692 struct partial_symtab *pst)
aaa75496 6693{
fff8551c 6694 line_header_up lh;
d85a05f0 6695 struct attribute *attr;
aaa75496 6696
d85a05f0 6697 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6698 if (attr != nullptr)
9c541725 6699 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6700 if (lh == NULL)
6701 return; /* No linetable, so no includes. */
6702
79748972
TT
6703 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6704 that we pass in the raw text_low here; that is ok because we're
6705 only decoding the line table to make include partial symtabs, and
6706 so the addresses aren't really used. */
4ae976d1 6707 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6708 pst->raw_text_low (), 1);
aaa75496
JB
6709}
6710
348e048f 6711static hashval_t
52dc124a 6712hash_signatured_type (const void *item)
348e048f 6713{
9a3c8263
SM
6714 const struct signatured_type *sig_type
6715 = (const struct signatured_type *) item;
9a619af0 6716
348e048f 6717 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6718 return sig_type->signature;
348e048f
DE
6719}
6720
6721static int
52dc124a 6722eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6723{
9a3c8263
SM
6724 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6725 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6726
348e048f
DE
6727 return lhs->signature == rhs->signature;
6728}
6729
1fd400ff
TT
6730/* Allocate a hash table for signatured types. */
6731
6732static htab_t
673bfd45 6733allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6734{
6735 return htab_create_alloc_ex (41,
52dc124a
DE
6736 hash_signatured_type,
6737 eq_signatured_type,
1fd400ff
TT
6738 NULL,
6739 &objfile->objfile_obstack,
6740 hashtab_obstack_allocate,
6741 dummy_obstack_deallocate);
6742}
6743
d467dd73 6744/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6745
6746static int
d467dd73 6747add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6748{
9a3c8263 6749 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6750 std::vector<signatured_type *> *all_type_units
6751 = (std::vector<signatured_type *> *) datum;
1fd400ff 6752
b2bdb8cf 6753 all_type_units->push_back (sigt);
1fd400ff
TT
6754
6755 return 1;
6756}
6757
78d4d2c5 6758/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6759 and fill them into TYPES_HTAB. It will process only type units,
6760 therefore DW_UT_type. */
c88ee1f0 6761
78d4d2c5 6762static void
ed2dc618
SM
6763create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6764 struct dwo_file *dwo_file,
43988095
JK
6765 dwarf2_section_info *section, htab_t &types_htab,
6766 rcuh_kind section_kind)
348e048f 6767{
3019eac3 6768 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6769 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6770 bfd *abfd;
6771 const gdb_byte *info_ptr, *end_ptr;
348e048f 6772
4bdcc0c1
DE
6773 abbrev_section = (dwo_file != NULL
6774 ? &dwo_file->sections.abbrev
6775 : &dwarf2_per_objfile->abbrev);
6776
b4f54984 6777 if (dwarf_read_debug)
43988095
JK
6778 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6779 get_section_name (section),
a32a8923 6780 get_section_file_name (abbrev_section));
09406207 6781
78d4d2c5
JK
6782 dwarf2_read_section (objfile, section);
6783 info_ptr = section->buffer;
348e048f 6784
78d4d2c5
JK
6785 if (info_ptr == NULL)
6786 return;
348e048f 6787
78d4d2c5
JK
6788 /* We can't set abfd until now because the section may be empty or
6789 not present, in which case the bfd is unknown. */
6790 abfd = get_section_bfd_owner (section);
348e048f 6791
78d4d2c5
JK
6792 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6793 because we don't need to read any dies: the signature is in the
6794 header. */
3019eac3 6795
78d4d2c5
JK
6796 end_ptr = info_ptr + section->size;
6797 while (info_ptr < end_ptr)
6798 {
78d4d2c5
JK
6799 struct signatured_type *sig_type;
6800 struct dwo_unit *dwo_tu;
6801 void **slot;
6802 const gdb_byte *ptr = info_ptr;
6803 struct comp_unit_head header;
6804 unsigned int length;
8b70b953 6805
9c541725 6806 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6807
a49dd8dd
JK
6808 /* Initialize it due to a false compiler warning. */
6809 header.signature = -1;
9c541725 6810 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6811
78d4d2c5
JK
6812 /* We need to read the type's signature in order to build the hash
6813 table, but we don't need anything else just yet. */
348e048f 6814
ed2dc618 6815 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6816 abbrev_section, ptr, section_kind);
348e048f 6817
78d4d2c5 6818 length = get_cu_length (&header);
6caca83c 6819
78d4d2c5
JK
6820 /* Skip dummy type units. */
6821 if (ptr >= info_ptr + length
43988095
JK
6822 || peek_abbrev_code (abfd, ptr) == 0
6823 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6824 {
6825 info_ptr += length;
6826 continue;
6827 }
dee91e82 6828
78d4d2c5
JK
6829 if (types_htab == NULL)
6830 {
6831 if (dwo_file)
6832 types_htab = allocate_dwo_unit_table (objfile);
6833 else
6834 types_htab = allocate_signatured_type_table (objfile);
6835 }
8b70b953 6836
78d4d2c5
JK
6837 if (dwo_file)
6838 {
6839 sig_type = NULL;
6840 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6841 struct dwo_unit);
6842 dwo_tu->dwo_file = dwo_file;
43988095 6843 dwo_tu->signature = header.signature;
9c541725 6844 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6845 dwo_tu->section = section;
9c541725 6846 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6847 dwo_tu->length = length;
6848 }
6849 else
6850 {
6851 /* N.B.: type_offset is not usable if this type uses a DWO file.
6852 The real type_offset is in the DWO file. */
6853 dwo_tu = NULL;
6854 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6855 struct signatured_type);
43988095 6856 sig_type->signature = header.signature;
9c541725 6857 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6858 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6859 sig_type->per_cu.is_debug_types = 1;
6860 sig_type->per_cu.section = section;
9c541725 6861 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6862 sig_type->per_cu.length = length;
6863 }
6864
6865 slot = htab_find_slot (types_htab,
6866 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6867 INSERT);
6868 gdb_assert (slot != NULL);
6869 if (*slot != NULL)
6870 {
9c541725 6871 sect_offset dup_sect_off;
0349ea22 6872
3019eac3
DE
6873 if (dwo_file)
6874 {
78d4d2c5
JK
6875 const struct dwo_unit *dup_tu
6876 = (const struct dwo_unit *) *slot;
6877
9c541725 6878 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6879 }
6880 else
6881 {
78d4d2c5
JK
6882 const struct signatured_type *dup_tu
6883 = (const struct signatured_type *) *slot;
6884
9c541725 6885 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6886 }
8b70b953 6887
b98664d3 6888 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6889 " the entry at offset %s, signature %s"),
6890 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6891 hex_string (header.signature));
78d4d2c5
JK
6892 }
6893 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6894
78d4d2c5 6895 if (dwarf_read_debug > 1)
9d8780f0
SM
6896 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6897 sect_offset_str (sect_off),
43988095 6898 hex_string (header.signature));
3019eac3 6899
78d4d2c5
JK
6900 info_ptr += length;
6901 }
6902}
3019eac3 6903
78d4d2c5
JK
6904/* Create the hash table of all entries in the .debug_types
6905 (or .debug_types.dwo) section(s).
6906 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6907 otherwise it is NULL.
b3c8eb43 6908
78d4d2c5 6909 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6910
78d4d2c5 6911 Note: This function processes DWO files only, not DWP files. */
348e048f 6912
78d4d2c5 6913static void
ed2dc618
SM
6914create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6915 struct dwo_file *dwo_file,
fd5866f6 6916 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6917 htab_t &types_htab)
6918{
fd5866f6
SM
6919 for (dwarf2_section_info &section : type_sections)
6920 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6921 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6922}
6923
6924/* Create the hash table of all entries in the .debug_types section,
6925 and initialize all_type_units.
6926 The result is zero if there is an error (e.g. missing .debug_types section),
6927 otherwise non-zero. */
6928
6929static int
ed2dc618 6930create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6931{
78d4d2c5 6932 htab_t types_htab = NULL;
3019eac3 6933
ed2dc618
SM
6934 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6935 &dwarf2_per_objfile->info, types_htab,
43988095 6936 rcuh_kind::COMPILE);
ed2dc618
SM
6937 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6938 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6939 if (types_htab == NULL)
6940 {
6941 dwarf2_per_objfile->signatured_types = NULL;
6942 return 0;
6943 }
6944
348e048f
DE
6945 dwarf2_per_objfile->signatured_types = types_htab;
6946
b2bdb8cf
SM
6947 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6948 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6949
6950 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6951 &dwarf2_per_objfile->all_type_units);
1fd400ff 6952
348e048f
DE
6953 return 1;
6954}
6955
6aa5f3a6
DE
6956/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6957 If SLOT is non-NULL, it is the entry to use in the hash table.
6958 Otherwise we find one. */
6959
6960static struct signatured_type *
ed2dc618
SM
6961add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6962 void **slot)
6aa5f3a6
DE
6963{
6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6965
b2bdb8cf
SM
6966 if (dwarf2_per_objfile->all_type_units.size ()
6967 == dwarf2_per_objfile->all_type_units.capacity ())
6968 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6969
b2bdb8cf
SM
6970 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6971 struct signatured_type);
6972
6973 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6974 sig_type->signature = sig;
6975 sig_type->per_cu.is_debug_types = 1;
6976 if (dwarf2_per_objfile->using_index)
6977 {
6978 sig_type->per_cu.v.quick =
6979 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6980 struct dwarf2_per_cu_quick_data);
6981 }
6982
6983 if (slot == NULL)
6984 {
6985 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6986 sig_type, INSERT);
6987 }
6988 gdb_assert (*slot == NULL);
6989 *slot = sig_type;
6990 /* The rest of sig_type must be filled in by the caller. */
6991 return sig_type;
6992}
6993
a2ce51a0
DE
6994/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6995 Fill in SIG_ENTRY with DWO_ENTRY. */
6996
6997static void
ed2dc618 6998fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6999 struct signatured_type *sig_entry,
7000 struct dwo_unit *dwo_entry)
7001{
7ee85ab1 7002 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
7003 gdb_assert (! sig_entry->per_cu.queued);
7004 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
7005 if (dwarf2_per_objfile->using_index)
7006 {
7007 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 7008 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
7009 }
7010 else
7011 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7012 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7013 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7014 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7015 gdb_assert (sig_entry->dwo_unit == NULL);
7016
7017 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7018 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7019 sig_entry->per_cu.length = dwo_entry->length;
7020 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7021 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7022 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7023 sig_entry->dwo_unit = dwo_entry;
7024}
7025
7026/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7027 If we haven't read the TU yet, create the signatured_type data structure
7028 for a TU to be read in directly from a DWO file, bypassing the stub.
7029 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7030 using .gdb_index, then when reading a CU we want to stay in the DWO file
7031 containing that CU. Otherwise we could end up reading several other DWO
7032 files (due to comdat folding) to process the transitive closure of all the
7033 mentioned TUs, and that can be slow. The current DWO file will have every
7034 type signature that it needs.
a2ce51a0
DE
7035 We only do this for .gdb_index because in the psymtab case we already have
7036 to read all the DWOs to build the type unit groups. */
7037
7038static struct signatured_type *
7039lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7040{
518817b3
SM
7041 struct dwarf2_per_objfile *dwarf2_per_objfile
7042 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7043 struct objfile *objfile = dwarf2_per_objfile->objfile;
7044 struct dwo_file *dwo_file;
7045 struct dwo_unit find_dwo_entry, *dwo_entry;
7046 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7047 void **slot;
a2ce51a0
DE
7048
7049 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7050
6aa5f3a6
DE
7051 /* If TU skeletons have been removed then we may not have read in any
7052 TUs yet. */
7053 if (dwarf2_per_objfile->signatured_types == NULL)
7054 {
7055 dwarf2_per_objfile->signatured_types
7056 = allocate_signatured_type_table (objfile);
7057 }
a2ce51a0
DE
7058
7059 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7060 Use the global signatured_types array to do our own comdat-folding
7061 of types. If this is the first time we're reading this TU, and
7062 the TU has an entry in .gdb_index, replace the recorded data from
7063 .gdb_index with this TU. */
a2ce51a0 7064
a2ce51a0 7065 find_sig_entry.signature = sig;
6aa5f3a6
DE
7066 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7067 &find_sig_entry, INSERT);
9a3c8263 7068 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7069
7070 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7071 read. Don't reassign the global entry to point to this DWO if that's
7072 the case. Also note that if the TU is already being read, it may not
7073 have come from a DWO, the program may be a mix of Fission-compiled
7074 code and non-Fission-compiled code. */
7075
7076 /* Have we already tried to read this TU?
7077 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7078 needn't exist in the global table yet). */
7079 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7080 return sig_entry;
7081
6aa5f3a6
DE
7082 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7083 dwo_unit of the TU itself. */
7084 dwo_file = cu->dwo_unit->dwo_file;
7085
a2ce51a0
DE
7086 /* Ok, this is the first time we're reading this TU. */
7087 if (dwo_file->tus == NULL)
7088 return NULL;
7089 find_dwo_entry.signature = sig;
9a3c8263 7090 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7091 if (dwo_entry == NULL)
7092 return NULL;
7093
6aa5f3a6
DE
7094 /* If the global table doesn't have an entry for this TU, add one. */
7095 if (sig_entry == NULL)
ed2dc618 7096 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7097
ed2dc618 7098 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7099 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7100 return sig_entry;
7101}
7102
a2ce51a0
DE
7103/* Subroutine of lookup_signatured_type.
7104 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7105 then try the DWP file. If the TU stub (skeleton) has been removed then
7106 it won't be in .gdb_index. */
a2ce51a0
DE
7107
7108static struct signatured_type *
7109lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7110{
518817b3
SM
7111 struct dwarf2_per_objfile *dwarf2_per_objfile
7112 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7113 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7114 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7115 struct dwo_unit *dwo_entry;
7116 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7117 void **slot;
a2ce51a0
DE
7118
7119 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7120 gdb_assert (dwp_file != NULL);
7121
6aa5f3a6
DE
7122 /* If TU skeletons have been removed then we may not have read in any
7123 TUs yet. */
7124 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7125 {
6aa5f3a6
DE
7126 dwarf2_per_objfile->signatured_types
7127 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7128 }
7129
6aa5f3a6
DE
7130 find_sig_entry.signature = sig;
7131 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7132 &find_sig_entry, INSERT);
9a3c8263 7133 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7134
7135 /* Have we already tried to read this TU?
7136 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7137 needn't exist in the global table yet). */
7138 if (sig_entry != NULL)
7139 return sig_entry;
7140
a2ce51a0
DE
7141 if (dwp_file->tus == NULL)
7142 return NULL;
ed2dc618 7143 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7144 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7145 if (dwo_entry == NULL)
7146 return NULL;
7147
ed2dc618
SM
7148 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7149 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7150
a2ce51a0
DE
7151 return sig_entry;
7152}
7153
380bca97 7154/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7155 Returns NULL if signature SIG is not present in the table.
7156 It is up to the caller to complain about this. */
348e048f
DE
7157
7158static struct signatured_type *
a2ce51a0 7159lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7160{
518817b3
SM
7161 struct dwarf2_per_objfile *dwarf2_per_objfile
7162 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7163
a2ce51a0
DE
7164 if (cu->dwo_unit
7165 && dwarf2_per_objfile->using_index)
7166 {
7167 /* We're in a DWO/DWP file, and we're using .gdb_index.
7168 These cases require special processing. */
ed2dc618 7169 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7170 return lookup_dwo_signatured_type (cu, sig);
7171 else
7172 return lookup_dwp_signatured_type (cu, sig);
7173 }
7174 else
7175 {
7176 struct signatured_type find_entry, *entry;
348e048f 7177
a2ce51a0
DE
7178 if (dwarf2_per_objfile->signatured_types == NULL)
7179 return NULL;
7180 find_entry.signature = sig;
9a3c8263
SM
7181 entry = ((struct signatured_type *)
7182 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7183 return entry;
7184 }
348e048f 7185}
42e7ad6c
DE
7186\f
7187/* Low level DIE reading support. */
348e048f 7188
d85a05f0
DJ
7189/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7190
7191static void
7192init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7193 struct dwarf2_cu *cu,
3019eac3 7194 struct dwarf2_section_info *section,
685af9cd
TT
7195 struct dwo_file *dwo_file,
7196 struct abbrev_table *abbrev_table)
d85a05f0 7197{
fceca515 7198 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7199 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7200 reader->cu = cu;
3019eac3 7201 reader->dwo_file = dwo_file;
dee91e82
DE
7202 reader->die_section = section;
7203 reader->buffer = section->buffer;
f664829e 7204 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7205 reader->comp_dir = NULL;
685af9cd 7206 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7207}
7208
b0c7bfa9
DE
7209/* Subroutine of init_cutu_and_read_dies to simplify it.
7210 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7211 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7212 already.
7213
7214 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7215 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7216 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7217 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7218 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7219 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7220 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7221 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7222 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7223 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7224 kept around for at least as long as *RESULT_READER.
7225
b0c7bfa9
DE
7226 The result is non-zero if a valid (non-dummy) DIE was found. */
7227
7228static int
7229read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7230 struct dwo_unit *dwo_unit,
b0c7bfa9 7231 struct die_info *stub_comp_unit_die,
a2ce51a0 7232 const char *stub_comp_dir,
b0c7bfa9 7233 struct die_reader_specs *result_reader,
d521ce57 7234 const gdb_byte **result_info_ptr,
b0c7bfa9 7235 struct die_info **result_comp_unit_die,
685af9cd
TT
7236 int *result_has_children,
7237 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7238{
ed2dc618 7239 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7240 struct objfile *objfile = dwarf2_per_objfile->objfile;
7241 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7242 bfd *abfd;
d521ce57 7243 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7244 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7245 int i,num_extra_attrs;
7246 struct dwarf2_section_info *dwo_abbrev_section;
7247 struct attribute *attr;
7248 struct die_info *comp_unit_die;
7249
b0aeadb3
DE
7250 /* At most one of these may be provided. */
7251 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7252
b0c7bfa9
DE
7253 /* These attributes aren't processed until later:
7254 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7255 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7256 referenced later. However, these attributes are found in the stub
7257 which we won't have later. In order to not impose this complication
7258 on the rest of the code, we read them here and copy them to the
7259 DWO CU/TU die. */
b0c7bfa9
DE
7260
7261 stmt_list = NULL;
7262 low_pc = NULL;
7263 high_pc = NULL;
7264 ranges = NULL;
7265 comp_dir = NULL;
7266
7267 if (stub_comp_unit_die != NULL)
7268 {
7269 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7270 DWO file. */
7271 if (! this_cu->is_debug_types)
7272 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7273 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7274 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7275 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7276 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7277
7278 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7279 We need the value before we can process DW_FORM_GNU_addr_index
7280 or DW_FORM_addrx. */
b0c7bfa9
DE
7281 cu->addr_base = 0;
7282 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7283 if (attr != nullptr)
b0c7bfa9
DE
7284 cu->addr_base = DW_UNSND (attr);
7285
7286 /* There should be a DW_AT_ranges_base attribute here (if needed).
7287 We need the value before we can process DW_AT_ranges. */
7288 cu->ranges_base = 0;
7289 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7290 if (attr != nullptr)
b0c7bfa9
DE
7291 cu->ranges_base = DW_UNSND (attr);
7292 }
a2ce51a0
DE
7293 else if (stub_comp_dir != NULL)
7294 {
7295 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7296 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7297 comp_dir->name = DW_AT_comp_dir;
7298 comp_dir->form = DW_FORM_string;
7299 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7300 DW_STRING (comp_dir) = stub_comp_dir;
7301 }
b0c7bfa9
DE
7302
7303 /* Set up for reading the DWO CU/TU. */
7304 cu->dwo_unit = dwo_unit;
685af9cd 7305 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7306 dwarf2_read_section (objfile, section);
a32a8923 7307 abfd = get_section_bfd_owner (section);
9c541725
PA
7308 begin_info_ptr = info_ptr = (section->buffer
7309 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7310 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7311
7312 if (this_cu->is_debug_types)
7313 {
b0c7bfa9
DE
7314 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7315
ed2dc618
SM
7316 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7317 &cu->header, section,
b0c7bfa9 7318 dwo_abbrev_section,
43988095 7319 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7320 /* This is not an assert because it can be caused by bad debug info. */
43988095 7321 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7322 {
7323 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7324 " TU at offset %s [in module %s]"),
a2ce51a0 7325 hex_string (sig_type->signature),
43988095 7326 hex_string (cu->header.signature),
9d8780f0 7327 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7328 bfd_get_filename (abfd));
7329 }
9c541725 7330 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7331 /* For DWOs coming from DWP files, we don't know the CU length
7332 nor the type's offset in the TU until now. */
7333 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7334 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7335
7336 /* Establish the type offset that can be used to lookup the type.
7337 For DWO files, we don't know it until now. */
9c541725
PA
7338 sig_type->type_offset_in_section
7339 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7340 }
7341 else
7342 {
ed2dc618
SM
7343 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7344 &cu->header, section,
b0c7bfa9 7345 dwo_abbrev_section,
43988095 7346 info_ptr, rcuh_kind::COMPILE);
9c541725 7347 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7348 /* For DWOs coming from DWP files, we don't know the CU length
7349 until now. */
7350 dwo_unit->length = get_cu_length (&cu->header);
7351 }
7352
685af9cd
TT
7353 *result_dwo_abbrev_table
7354 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7355 cu->header.abbrev_sect_off);
7356 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7357 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7358
7359 /* Read in the die, but leave space to copy over the attributes
7360 from the stub. This has the benefit of simplifying the rest of
7361 the code - all the work to maintain the illusion of a single
7362 DW_TAG_{compile,type}_unit DIE is done here. */
7363 num_extra_attrs = ((stmt_list != NULL)
7364 + (low_pc != NULL)
7365 + (high_pc != NULL)
7366 + (ranges != NULL)
7367 + (comp_dir != NULL));
7368 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7369 result_has_children, num_extra_attrs);
7370
7371 /* Copy over the attributes from the stub to the DIE we just read in. */
7372 comp_unit_die = *result_comp_unit_die;
7373 i = comp_unit_die->num_attrs;
7374 if (stmt_list != NULL)
7375 comp_unit_die->attrs[i++] = *stmt_list;
7376 if (low_pc != NULL)
7377 comp_unit_die->attrs[i++] = *low_pc;
7378 if (high_pc != NULL)
7379 comp_unit_die->attrs[i++] = *high_pc;
7380 if (ranges != NULL)
7381 comp_unit_die->attrs[i++] = *ranges;
7382 if (comp_dir != NULL)
7383 comp_unit_die->attrs[i++] = *comp_dir;
7384 comp_unit_die->num_attrs += num_extra_attrs;
7385
b4f54984 7386 if (dwarf_die_debug)
bf6af496
DE
7387 {
7388 fprintf_unfiltered (gdb_stdlog,
7389 "Read die from %s@0x%x of %s:\n",
a32a8923 7390 get_section_name (section),
bf6af496
DE
7391 (unsigned) (begin_info_ptr - section->buffer),
7392 bfd_get_filename (abfd));
b4f54984 7393 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7394 }
7395
a2ce51a0
DE
7396 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7397 TUs by skipping the stub and going directly to the entry in the DWO file.
7398 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7399 to get it via circuitous means. Blech. */
7400 if (comp_dir != NULL)
7401 result_reader->comp_dir = DW_STRING (comp_dir);
7402
b0c7bfa9
DE
7403 /* Skip dummy compilation units. */
7404 if (info_ptr >= begin_info_ptr + dwo_unit->length
7405 || peek_abbrev_code (abfd, info_ptr) == 0)
7406 return 0;
7407
7408 *result_info_ptr = info_ptr;
7409 return 1;
7410}
7411
a084a2a6
AT
7412/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7413 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7414 signature is part of the header. */
7415static gdb::optional<ULONGEST>
7416lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7417{
7418 if (cu->header.version >= 5)
7419 return cu->header.signature;
7420 struct attribute *attr;
7421 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7422 if (attr == nullptr)
7423 return gdb::optional<ULONGEST> ();
7424 return DW_UNSND (attr);
7425}
7426
b0c7bfa9
DE
7427/* Subroutine of init_cutu_and_read_dies to simplify it.
7428 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7429 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7430
7431static struct dwo_unit *
7432lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7433 struct die_info *comp_unit_die)
7434{
7435 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7436 struct dwo_unit *dwo_unit;
7437 const char *comp_dir, *dwo_name;
7438
a2ce51a0
DE
7439 gdb_assert (cu != NULL);
7440
b0c7bfa9 7441 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7442 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7443 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7444
7445 if (this_cu->is_debug_types)
7446 {
7447 struct signatured_type *sig_type;
7448
7449 /* Since this_cu is the first member of struct signatured_type,
7450 we can go from a pointer to one to a pointer to the other. */
7451 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7452 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7453 }
7454 else
7455 {
a084a2a6
AT
7456 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7457 if (!signature.has_value ())
b0c7bfa9
DE
7458 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7459 " [in module %s]"),
e3b94546 7460 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7461 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7462 *signature);
b0c7bfa9
DE
7463 }
7464
b0c7bfa9
DE
7465 return dwo_unit;
7466}
7467
a2ce51a0 7468/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7469 See it for a description of the parameters.
fcd3b13d 7470 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7471
7472static void
6aa5f3a6
DE
7473init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7474 int use_existing_cu, int keep,
a2ce51a0
DE
7475 die_reader_func_ftype *die_reader_func,
7476 void *data)
7477{
fcd3b13d 7478 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7479 struct signatured_type *sig_type;
a2ce51a0
DE
7480 struct die_reader_specs reader;
7481 const gdb_byte *info_ptr;
7482 struct die_info *comp_unit_die;
7483 int has_children;
ed2dc618 7484 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7485
7486 /* Verify we can do the following downcast, and that we have the
7487 data we need. */
7488 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7489 sig_type = (struct signatured_type *) this_cu;
7490 gdb_assert (sig_type->dwo_unit != NULL);
7491
6aa5f3a6
DE
7492 if (use_existing_cu && this_cu->cu != NULL)
7493 {
7494 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7495 /* There's no need to do the rereading_dwo_cu handling that
7496 init_cutu_and_read_dies does since we don't read the stub. */
7497 }
7498 else
7499 {
7500 /* If !use_existing_cu, this_cu->cu must be NULL. */
7501 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7502 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7503 }
7504
7505 /* A future optimization, if needed, would be to use an existing
7506 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7507 could share abbrev tables. */
a2ce51a0 7508
685af9cd
TT
7509 /* The abbreviation table used by READER, this must live at least as long as
7510 READER. */
7511 abbrev_table_up dwo_abbrev_table;
7512
a2ce51a0 7513 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7514 NULL /* stub_comp_unit_die */,
7515 sig_type->dwo_unit->dwo_file->comp_dir,
7516 &reader, &info_ptr,
685af9cd
TT
7517 &comp_unit_die, &has_children,
7518 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7519 {
7520 /* Dummy die. */
a2ce51a0
DE
7521 return;
7522 }
7523
7524 /* All the "real" work is done here. */
7525 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7526
6aa5f3a6 7527 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7528 but the alternative is making the latter more complex.
7529 This function is only for the special case of using DWO files directly:
7530 no point in overly complicating the general case just to handle this. */
fcd3b13d 7531 if (new_cu != NULL && keep)
a2ce51a0 7532 {
fcd3b13d
SM
7533 /* Link this CU into read_in_chain. */
7534 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7535 dwarf2_per_objfile->read_in_chain = this_cu;
7536 /* The chain owns it now. */
7537 new_cu.release ();
a2ce51a0 7538 }
a2ce51a0
DE
7539}
7540
fd820528 7541/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7542 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7543
f4dc4d17
DE
7544 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7545 Otherwise the table specified in the comp unit header is read in and used.
7546 This is an optimization for when we already have the abbrev table.
7547
dee91e82
DE
7548 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7549 Otherwise, a new CU is allocated with xmalloc.
7550
7551 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7552 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7553
7554 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7555 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7556
70221824 7557static void
fd820528 7558init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7559 struct abbrev_table *abbrev_table,
fd820528 7560 int use_existing_cu, int keep,
58f0c718 7561 bool skip_partial,
fd820528
DE
7562 die_reader_func_ftype *die_reader_func,
7563 void *data)
c906108c 7564{
ed2dc618 7565 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7566 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7567 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7568 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7569 struct dwarf2_cu *cu;
d521ce57 7570 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7571 struct die_reader_specs reader;
d85a05f0 7572 struct die_info *comp_unit_die;
dee91e82 7573 int has_children;
dee91e82 7574 struct signatured_type *sig_type = NULL;
4bdcc0c1 7575 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7576 /* Non-zero if CU currently points to a DWO file and we need to
7577 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7578 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7579 int rereading_dwo_cu = 0;
c906108c 7580
b4f54984 7581 if (dwarf_die_debug)
9d8780f0 7582 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7583 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7584 sect_offset_str (this_cu->sect_off));
09406207 7585
dee91e82
DE
7586 if (use_existing_cu)
7587 gdb_assert (keep);
23745b47 7588
a2ce51a0
DE
7589 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7590 file (instead of going through the stub), short-circuit all of this. */
7591 if (this_cu->reading_dwo_directly)
7592 {
7593 /* Narrow down the scope of possibilities to have to understand. */
7594 gdb_assert (this_cu->is_debug_types);
7595 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7596 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7597 die_reader_func, data);
a2ce51a0
DE
7598 return;
7599 }
7600
dee91e82
DE
7601 /* This is cheap if the section is already read in. */
7602 dwarf2_read_section (objfile, section);
7603
9c541725 7604 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7605
7606 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7607
fcd3b13d 7608 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7609 if (use_existing_cu && this_cu->cu != NULL)
7610 {
7611 cu = this_cu->cu;
42e7ad6c
DE
7612 /* If this CU is from a DWO file we need to start over, we need to
7613 refetch the attributes from the skeleton CU.
7614 This could be optimized by retrieving those attributes from when we
7615 were here the first time: the previous comp_unit_die was stored in
7616 comp_unit_obstack. But there's no data yet that we need this
7617 optimization. */
7618 if (cu->dwo_unit != NULL)
7619 rereading_dwo_cu = 1;
dee91e82
DE
7620 }
7621 else
7622 {
7623 /* If !use_existing_cu, this_cu->cu must be NULL. */
7624 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7625 new_cu.reset (new dwarf2_cu (this_cu));
7626 cu = new_cu.get ();
42e7ad6c 7627 }
dee91e82 7628
b0c7bfa9 7629 /* Get the header. */
9c541725 7630 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7631 {
7632 /* We already have the header, there's no need to read it in again. */
9c541725 7633 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7634 }
7635 else
7636 {
3019eac3 7637 if (this_cu->is_debug_types)
dee91e82 7638 {
ed2dc618
SM
7639 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7640 &cu->header, section,
4bdcc0c1 7641 abbrev_section, info_ptr,
43988095 7642 rcuh_kind::TYPE);
dee91e82 7643
42e7ad6c
DE
7644 /* Since per_cu is the first member of struct signatured_type,
7645 we can go from a pointer to one to a pointer to the other. */
7646 sig_type = (struct signatured_type *) this_cu;
43988095 7647 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7648 gdb_assert (sig_type->type_offset_in_tu
7649 == cu->header.type_cu_offset_in_tu);
7650 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7651
42e7ad6c
DE
7652 /* LENGTH has not been set yet for type units if we're
7653 using .gdb_index. */
1ce1cefd 7654 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7655
7656 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7657 sig_type->type_offset_in_section =
7658 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7659
7660 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7661 }
7662 else
7663 {
ed2dc618
SM
7664 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7665 &cu->header, section,
4bdcc0c1 7666 abbrev_section,
43988095
JK
7667 info_ptr,
7668 rcuh_kind::COMPILE);
dee91e82 7669
9c541725 7670 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7671 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7672 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7673 }
7674 }
10b3939b 7675
6caca83c 7676 /* Skip dummy compilation units. */
dee91e82 7677 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7678 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7679 return;
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
685af9cd
TT
7683 done (own the table through ABBREV_TABLE_HOLDER). */
7684 abbrev_table_up abbrev_table_holder;
f4dc4d17 7685 if (abbrev_table != NULL)
685af9cd
TT
7686 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7687 else
f4dc4d17 7688 {
685af9cd
TT
7689 abbrev_table_holder
7690 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7691 cu->header.abbrev_sect_off);
7692 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7693 }
af703f96 7694
dee91e82 7695 /* Read the top level CU/TU die. */
685af9cd 7696 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7697 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7698
58f0c718
TT
7699 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7700 return;
7701
b0c7bfa9 7702 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7703 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7704 table from the DWO file and pass the ownership over to us. It will be
7705 referenced from READER, so we must make sure to free it after we're done
7706 with READER.
7707
b0c7bfa9
DE
7708 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7709 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7710 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7711 abbrev_table_up dwo_abbrev_table;
a084a2a6 7712 if (dwo_name != nullptr)
3019eac3 7713 {
3019eac3 7714 struct dwo_unit *dwo_unit;
b0c7bfa9 7715 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7716
7717 if (has_children)
6a506a2d 7718 {
b98664d3 7719 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7720 " has children (offset %s) [in module %s]"),
7721 sect_offset_str (this_cu->sect_off),
7722 bfd_get_filename (abfd));
6a506a2d 7723 }
b0c7bfa9 7724 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7725 if (dwo_unit != NULL)
3019eac3 7726 {
6a506a2d 7727 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7728 comp_unit_die, NULL,
6a506a2d 7729 &reader, &info_ptr,
685af9cd
TT
7730 &dwo_comp_unit_die, &has_children,
7731 &dwo_abbrev_table) == 0)
6a506a2d
DE
7732 {
7733 /* Dummy die. */
6a506a2d
DE
7734 return;
7735 }
7736 comp_unit_die = dwo_comp_unit_die;
7737 }
7738 else
7739 {
7740 /* Yikes, we couldn't find the rest of the DIE, we only have
7741 the stub. A complaint has already been logged. There's
7742 not much more we can do except pass on the stub DIE to
7743 die_reader_func. We don't want to throw an error on bad
7744 debug info. */
3019eac3
DE
7745 }
7746 }
7747
b0c7bfa9 7748 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7749 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7750
b0c7bfa9 7751 /* Done, clean up. */
fcd3b13d 7752 if (new_cu != NULL && keep)
348e048f 7753 {
fcd3b13d
SM
7754 /* Link this CU into read_in_chain. */
7755 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7756 dwarf2_per_objfile->read_in_chain = this_cu;
7757 /* The chain owns it now. */
7758 new_cu.release ();
348e048f 7759 }
dee91e82
DE
7760}
7761
33e80786
DE
7762/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7763 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7764 to have already done the lookup to find the DWO file).
dee91e82
DE
7765
7766 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7767 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7768
7769 We fill in THIS_CU->length.
7770
7771 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7772 linker) then DIE_READER_FUNC will not get called.
7773
7774 THIS_CU->cu is always freed when done.
3019eac3
DE
7775 This is done in order to not leave THIS_CU->cu in a state where we have
7776 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7777
7778static void
7779init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7780 struct dwo_file *dwo_file,
dee91e82
DE
7781 die_reader_func_ftype *die_reader_func,
7782 void *data)
7783{
ed2dc618 7784 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7785 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7786 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7787 bfd *abfd = get_section_bfd_owner (section);
33e80786 7788 struct dwarf2_section_info *abbrev_section;
d521ce57 7789 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7790 struct die_reader_specs reader;
dee91e82
DE
7791 struct die_info *comp_unit_die;
7792 int has_children;
7793
b4f54984 7794 if (dwarf_die_debug)
9d8780f0 7795 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7796 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7797 sect_offset_str (this_cu->sect_off));
09406207 7798
dee91e82
DE
7799 gdb_assert (this_cu->cu == NULL);
7800
33e80786
DE
7801 abbrev_section = (dwo_file != NULL
7802 ? &dwo_file->sections.abbrev
7803 : get_abbrev_section_for_cu (this_cu));
7804
dee91e82
DE
7805 /* This is cheap if the section is already read in. */
7806 dwarf2_read_section (objfile, section);
7807
fcd3b13d 7808 struct dwarf2_cu cu (this_cu);
dee91e82 7809
9c541725 7810 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7811 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7812 &cu.header, section,
4bdcc0c1 7813 abbrev_section, info_ptr,
43988095
JK
7814 (this_cu->is_debug_types
7815 ? rcuh_kind::TYPE
7816 : rcuh_kind::COMPILE));
dee91e82 7817
1ce1cefd 7818 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7819
7820 /* Skip dummy compilation units. */
7821 if (info_ptr >= begin_info_ptr + this_cu->length
7822 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7823 return;
72bf9492 7824
685af9cd
TT
7825 abbrev_table_up abbrev_table
7826 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7827 cu.header.abbrev_sect_off);
dee91e82 7828
685af9cd 7829 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7830 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7831
7832 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7833}
7834
3019eac3
DE
7835/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7836 does not lookup the specified DWO file.
7837 This cannot be used to read DWO files.
dee91e82
DE
7838
7839 THIS_CU->cu is always freed when done.
3019eac3
DE
7840 This is done in order to not leave THIS_CU->cu in a state where we have
7841 to care whether it refers to the "main" CU or the DWO CU.
7842 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7843
7844static void
7845init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7846 die_reader_func_ftype *die_reader_func,
7847 void *data)
7848{
33e80786 7849 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7850}
0018ea6f
DE
7851\f
7852/* Type Unit Groups.
dee91e82 7853
0018ea6f
DE
7854 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7855 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7856 so that all types coming from the same compilation (.o file) are grouped
7857 together. A future step could be to put the types in the same symtab as
7858 the CU the types ultimately came from. */
ff013f42 7859
f4dc4d17
DE
7860static hashval_t
7861hash_type_unit_group (const void *item)
7862{
9a3c8263
SM
7863 const struct type_unit_group *tu_group
7864 = (const struct type_unit_group *) item;
f4dc4d17 7865
094b34ac 7866 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7867}
348e048f
DE
7868
7869static int
f4dc4d17 7870eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7871{
9a3c8263
SM
7872 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7873 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7874
094b34ac 7875 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7876}
348e048f 7877
f4dc4d17
DE
7878/* Allocate a hash table for type unit groups. */
7879
7880static htab_t
ed2dc618 7881allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7882{
7883 return htab_create_alloc_ex (3,
7884 hash_type_unit_group,
7885 eq_type_unit_group,
7886 NULL,
ed2dc618 7887 &objfile->objfile_obstack,
f4dc4d17
DE
7888 hashtab_obstack_allocate,
7889 dummy_obstack_deallocate);
7890}
dee91e82 7891
f4dc4d17
DE
7892/* Type units that don't have DW_AT_stmt_list are grouped into their own
7893 partial symtabs. We combine several TUs per psymtab to not let the size
7894 of any one psymtab grow too big. */
7895#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7896#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7897
094b34ac 7898/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7899 Create the type_unit_group object used to hold one or more TUs. */
7900
7901static struct type_unit_group *
094b34ac 7902create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7903{
518817b3
SM
7904 struct dwarf2_per_objfile *dwarf2_per_objfile
7905 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7906 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7907 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7908 struct type_unit_group *tu_group;
f4dc4d17
DE
7909
7910 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7911 struct type_unit_group);
094b34ac 7912 per_cu = &tu_group->per_cu;
518817b3 7913 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7914
094b34ac
DE
7915 if (dwarf2_per_objfile->using_index)
7916 {
7917 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7918 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7919 }
7920 else
7921 {
9c541725 7922 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7923 struct partial_symtab *pst;
528e1572 7924 std::string name;
094b34ac
DE
7925
7926 /* Give the symtab a useful name for debug purposes. */
7927 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7928 name = string_printf ("<type_units_%d>",
7929 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7930 else
528e1572 7931 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7932
528e1572 7933 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7934 pst->anonymous = 1;
094b34ac 7935 }
f4dc4d17 7936
094b34ac 7937 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7938 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7939
7940 return tu_group;
7941}
7942
094b34ac
DE
7943/* Look up the type_unit_group for type unit CU, and create it if necessary.
7944 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7945
7946static struct type_unit_group *
ff39bb5e 7947get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7948{
518817b3
SM
7949 struct dwarf2_per_objfile *dwarf2_per_objfile
7950 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7951 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7952 struct type_unit_group *tu_group;
7953 void **slot;
7954 unsigned int line_offset;
7955 struct type_unit_group type_unit_group_for_lookup;
7956
7957 if (dwarf2_per_objfile->type_unit_groups == NULL)
7958 {
7959 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7960 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7961 }
7962
7963 /* Do we need to create a new group, or can we use an existing one? */
7964
7965 if (stmt_list)
7966 {
7967 line_offset = DW_UNSND (stmt_list);
7968 ++tu_stats->nr_symtab_sharers;
7969 }
7970 else
7971 {
7972 /* Ugh, no stmt_list. Rare, but we have to handle it.
7973 We can do various things here like create one group per TU or
7974 spread them over multiple groups to split up the expansion work.
7975 To avoid worst case scenarios (too many groups or too large groups)
7976 we, umm, group them in bunches. */
7977 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7978 | (tu_stats->nr_stmt_less_type_units
7979 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7980 ++tu_stats->nr_stmt_less_type_units;
7981 }
7982
094b34ac 7983 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7984 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7985 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7986 &type_unit_group_for_lookup, INSERT);
7987 if (*slot != NULL)
7988 {
9a3c8263 7989 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7990 gdb_assert (tu_group != NULL);
7991 }
7992 else
7993 {
9c541725 7994 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7995 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7996 *slot = tu_group;
7997 ++tu_stats->nr_symtabs;
7998 }
7999
8000 return tu_group;
8001}
0018ea6f
DE
8002\f
8003/* Partial symbol tables. */
8004
8005/* Create a psymtab named NAME and assign it to PER_CU.
8006
8007 The caller must fill in the following details:
8008 dirname, textlow, texthigh. */
8009
8010static struct partial_symtab *
8011create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8012{
e3b94546 8013 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8014 struct partial_symtab *pst;
8015
939652a5 8016 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8017
8018 pst->psymtabs_addrmap_supported = 1;
8019
8020 /* This is the glue that links PST into GDB's symbol API. */
8021 pst->read_symtab_private = per_cu;
8022 pst->read_symtab = dwarf2_read_symtab;
8023 per_cu->v.psymtab = pst;
8024
8025 return pst;
8026}
8027
b93601f3
TT
8028/* The DATA object passed to process_psymtab_comp_unit_reader has this
8029 type. */
8030
8031struct process_psymtab_comp_unit_data
8032{
8033 /* True if we are reading a DW_TAG_partial_unit. */
8034
8035 int want_partial_unit;
8036
8037 /* The "pretend" language that is used if the CU doesn't declare a
8038 language. */
8039
8040 enum language pretend_language;
8041};
8042
0018ea6f
DE
8043/* die_reader_func for process_psymtab_comp_unit. */
8044
8045static void
8046process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8047 const gdb_byte *info_ptr,
0018ea6f
DE
8048 struct die_info *comp_unit_die,
8049 int has_children,
8050 void *data)
8051{
8052 struct dwarf2_cu *cu = reader->cu;
518817b3 8053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8055 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8056 CORE_ADDR baseaddr;
8057 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8058 struct partial_symtab *pst;
3a2b436a 8059 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8060 const char *filename;
9a3c8263
SM
8061 struct process_psymtab_comp_unit_data *info
8062 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8063
b93601f3 8064 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8065 return;
8066
8067 gdb_assert (! per_cu->is_debug_types);
8068
b93601f3 8069 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8070
0018ea6f 8071 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8072 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8073 if (filename == NULL)
0018ea6f 8074 filename = "";
0018ea6f
DE
8075
8076 pst = create_partial_symtab (per_cu, filename);
8077
8078 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8079 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8080
8081 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8082
8083 dwarf2_find_base_address (comp_unit_die, cu);
8084
8085 /* Possibly set the default values of LOWPC and HIGHPC from
8086 `DW_AT_ranges'. */
3a2b436a
JK
8087 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8088 &best_highpc, cu, pst);
8089 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8090 {
8091 CORE_ADDR low
8092 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8093 - baseaddr);
8094 CORE_ADDR high
8095 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8096 - baseaddr - 1);
8097 /* Store the contiguous range if it is not empty; it can be
8098 empty for CUs with no code. */
d320c2b5
TT
8099 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8100 low, high, pst);
79748972 8101 }
0018ea6f
DE
8102
8103 /* Check if comp unit has_children.
8104 If so, read the rest of the partial symbols from this comp unit.
8105 If not, there's no more debug_info for this comp unit. */
8106 if (has_children)
8107 {
8108 struct partial_die_info *first_die;
8109 CORE_ADDR lowpc, highpc;
8110
8111 lowpc = ((CORE_ADDR) -1);
8112 highpc = ((CORE_ADDR) 0);
8113
8114 first_die = load_partial_dies (reader, info_ptr, 1);
8115
8116 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8117 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8118
8119 /* If we didn't find a lowpc, set it to highpc to avoid
8120 complaints from `maint check'. */
8121 if (lowpc == ((CORE_ADDR) -1))
8122 lowpc = highpc;
8123
8124 /* If the compilation unit didn't have an explicit address range,
8125 then use the information extracted from its child dies. */
e385593e 8126 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8127 {
8128 best_lowpc = lowpc;
8129 best_highpc = highpc;
8130 }
8131 }
4ae976d1 8132 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8133 best_lowpc + baseaddr)
8134 - baseaddr);
4ae976d1 8135 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8136 best_highpc + baseaddr)
8137 - baseaddr);
0018ea6f 8138
8763cede 8139 end_psymtab_common (objfile, pst);
0018ea6f 8140
ae640021 8141 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8142 {
8143 int i;
ae640021 8144 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8145
8146 /* Fill in 'dependencies' here; we fill in 'users' in a
8147 post-pass. */
8148 pst->number_of_dependencies = len;
a9342b62
TT
8149 pst->dependencies
8150 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8151 for (i = 0; i < len; ++i)
8152 {
8153 pst->dependencies[i]
8154 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8155 }
0018ea6f 8156
ae640021 8157 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8158 }
8159
8160 /* Get the list of files included in the current compilation unit,
8161 and build a psymtab for each of them. */
8162 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8163
b4f54984 8164 if (dwarf_read_debug)
b926417a
TT
8165 fprintf_unfiltered (gdb_stdlog,
8166 "Psymtab for %s unit @%s: %s - %s"
8167 ", %d global, %d static syms\n",
8168 per_cu->is_debug_types ? "type" : "comp",
8169 sect_offset_str (per_cu->sect_off),
8170 paddress (gdbarch, pst->text_low (objfile)),
8171 paddress (gdbarch, pst->text_high (objfile)),
8172 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8173}
8174
8175/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8176 Process compilation unit THIS_CU for a psymtab. */
8177
8178static void
8179process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8180 int want_partial_unit,
8181 enum language pretend_language)
0018ea6f
DE
8182{
8183 /* If this compilation unit was already read in, free the
8184 cached copy in order to read it in again. This is
8185 necessary because we skipped some symbols when we first
8186 read in the compilation unit (see load_partial_dies).
8187 This problem could be avoided, but the benefit is unclear. */
8188 if (this_cu->cu != NULL)
8189 free_one_cached_comp_unit (this_cu);
8190
f1902523 8191 if (this_cu->is_debug_types)
58f0c718
TT
8192 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8193 build_type_psymtabs_reader, NULL);
f1902523
JK
8194 else
8195 {
8196 process_psymtab_comp_unit_data info;
8197 info.want_partial_unit = want_partial_unit;
8198 info.pretend_language = pretend_language;
58f0c718 8199 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8200 process_psymtab_comp_unit_reader, &info);
8201 }
0018ea6f
DE
8202
8203 /* Age out any secondary CUs. */
ed2dc618 8204 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8205}
f4dc4d17
DE
8206
8207/* Reader function for build_type_psymtabs. */
8208
8209static void
8210build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8211 const gdb_byte *info_ptr,
f4dc4d17
DE
8212 struct die_info *type_unit_die,
8213 int has_children,
8214 void *data)
8215{
ed2dc618 8216 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8217 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8218 struct objfile *objfile = dwarf2_per_objfile->objfile;
8219 struct dwarf2_cu *cu = reader->cu;
8220 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8221 struct signatured_type *sig_type;
f4dc4d17
DE
8222 struct type_unit_group *tu_group;
8223 struct attribute *attr;
8224 struct partial_die_info *first_die;
8225 CORE_ADDR lowpc, highpc;
8226 struct partial_symtab *pst;
8227
8228 gdb_assert (data == NULL);
0186c6a7
DE
8229 gdb_assert (per_cu->is_debug_types);
8230 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8231
8232 if (! has_children)
8233 return;
8234
8235 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8236 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8237
df07e2c7 8238 if (tu_group->tus == nullptr)
a8b3b8e9 8239 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8240 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8241
8242 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8243 pst = create_partial_symtab (per_cu, "");
8244 pst->anonymous = 1;
8245
8246 first_die = load_partial_dies (reader, info_ptr, 1);
8247
8248 lowpc = (CORE_ADDR) -1;
8249 highpc = (CORE_ADDR) 0;
8250 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8251
8763cede 8252 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8253}
8254
73051182
DE
8255/* Struct used to sort TUs by their abbreviation table offset. */
8256
8257struct tu_abbrev_offset
8258{
b2bdb8cf
SM
8259 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8260 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8261 {}
8262
8263 signatured_type *sig_type;
73051182
DE
8264 sect_offset abbrev_offset;
8265};
8266
484cf504 8267/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8268
484cf504
TT
8269static bool
8270sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8271 const struct tu_abbrev_offset &b)
73051182 8272{
484cf504 8273 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8274}
8275
8276/* Efficiently read all the type units.
8277 This does the bulk of the work for build_type_psymtabs.
8278
8279 The efficiency is because we sort TUs by the abbrev table they use and
8280 only read each abbrev table once. In one program there are 200K TUs
8281 sharing 8K abbrev tables.
8282
8283 The main purpose of this function is to support building the
8284 dwarf2_per_objfile->type_unit_groups table.
8285 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8286 can collapse the search space by grouping them by stmt_list.
8287 The savings can be significant, in the same program from above the 200K TUs
8288 share 8K stmt_list tables.
8289
8290 FUNC is expected to call get_type_unit_group, which will create the
8291 struct type_unit_group if necessary and add it to
8292 dwarf2_per_objfile->type_unit_groups. */
8293
8294static void
ed2dc618 8295build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8296{
73051182 8297 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8298 abbrev_table_up abbrev_table;
73051182 8299 sect_offset abbrev_offset;
73051182
DE
8300
8301 /* It's up to the caller to not call us multiple times. */
8302 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8303
b2bdb8cf 8304 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8305 return;
8306
8307 /* TUs typically share abbrev tables, and there can be way more TUs than
8308 abbrev tables. Sort by abbrev table to reduce the number of times we
8309 read each abbrev table in.
8310 Alternatives are to punt or to maintain a cache of abbrev tables.
8311 This is simpler and efficient enough for now.
8312
8313 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8314 symtab to use). Typically TUs with the same abbrev offset have the same
8315 stmt_list value too so in practice this should work well.
8316
8317 The basic algorithm here is:
8318
8319 sort TUs by abbrev table
8320 for each TU with same abbrev table:
8321 read abbrev table if first user
8322 read TU top level DIE
8323 [IWBN if DWO skeletons had DW_AT_stmt_list]
8324 call FUNC */
8325
b4f54984 8326 if (dwarf_read_debug)
73051182
DE
8327 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8328
8329 /* Sort in a separate table to maintain the order of all_type_units
8330 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8331 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8332 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8333
8334 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8335 sorted_by_abbrev.emplace_back
8336 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8337 sig_type->per_cu.section,
8338 sig_type->per_cu.sect_off));
73051182 8339
484cf504
TT
8340 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8341 sort_tu_by_abbrev_offset);
73051182 8342
9c541725 8343 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8344
b2bdb8cf 8345 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8346 {
73051182
DE
8347 /* Switch to the next abbrev table if necessary. */
8348 if (abbrev_table == NULL
b2bdb8cf 8349 || tu.abbrev_offset != abbrev_offset)
73051182 8350 {
b2bdb8cf 8351 abbrev_offset = tu.abbrev_offset;
73051182 8352 abbrev_table =
ed2dc618
SM
8353 abbrev_table_read_table (dwarf2_per_objfile,
8354 &dwarf2_per_objfile->abbrev,
73051182
DE
8355 abbrev_offset);
8356 ++tu_stats->nr_uniq_abbrev_tables;
8357 }
8358
b2bdb8cf 8359 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8360 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8361 }
6aa5f3a6 8362}
73051182 8363
6aa5f3a6
DE
8364/* Print collected type unit statistics. */
8365
8366static void
ed2dc618 8367print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8368{
8369 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8370
8371 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8372 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8373 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8374 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8375 tu_stats->nr_uniq_abbrev_tables);
8376 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8377 tu_stats->nr_symtabs);
8378 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8379 tu_stats->nr_symtab_sharers);
8380 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8381 tu_stats->nr_stmt_less_type_units);
8382 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8383 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8384}
8385
f4dc4d17
DE
8386/* Traversal function for build_type_psymtabs. */
8387
8388static int
8389build_type_psymtab_dependencies (void **slot, void *info)
8390{
ed2dc618
SM
8391 struct dwarf2_per_objfile *dwarf2_per_objfile
8392 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8393 struct objfile *objfile = dwarf2_per_objfile->objfile;
8394 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8395 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8396 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8397 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8398 int i;
8399
8400 gdb_assert (len > 0);
0186c6a7 8401 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8402
8403 pst->number_of_dependencies = len;
a9342b62 8404 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8405 for (i = 0; i < len; ++i)
f4dc4d17 8406 {
df07e2c7 8407 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8408 gdb_assert (iter->per_cu.is_debug_types);
8409 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8410 iter->type_unit_group = tu_group;
f4dc4d17
DE
8411 }
8412
df07e2c7
AB
8413 delete tu_group->tus;
8414 tu_group->tus = nullptr;
348e048f
DE
8415
8416 return 1;
8417}
8418
8419/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8420 Build partial symbol tables for the .debug_types comp-units. */
8421
8422static void
ed2dc618 8423build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8424{
ed2dc618 8425 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8426 return;
8427
ed2dc618 8428 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8429}
f4dc4d17 8430
6aa5f3a6
DE
8431/* Traversal function for process_skeletonless_type_unit.
8432 Read a TU in a DWO file and build partial symbols for it. */
8433
8434static int
8435process_skeletonless_type_unit (void **slot, void *info)
8436{
8437 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8438 struct dwarf2_per_objfile *dwarf2_per_objfile
8439 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8440 struct signatured_type find_entry, *entry;
8441
8442 /* If this TU doesn't exist in the global table, add it and read it in. */
8443
8444 if (dwarf2_per_objfile->signatured_types == NULL)
8445 {
8446 dwarf2_per_objfile->signatured_types
ed2dc618 8447 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8448 }
8449
8450 find_entry.signature = dwo_unit->signature;
8451 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8452 INSERT);
8453 /* If we've already seen this type there's nothing to do. What's happening
8454 is we're doing our own version of comdat-folding here. */
8455 if (*slot != NULL)
8456 return 1;
8457
8458 /* This does the job that create_all_type_units would have done for
8459 this TU. */
ed2dc618
SM
8460 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8461 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8462 *slot = entry;
8463
8464 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8465 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8466 build_type_psymtabs_reader, NULL);
8467
8468 return 1;
8469}
8470
8471/* Traversal function for process_skeletonless_type_units. */
8472
8473static int
8474process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8475{
8476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8477
8478 if (dwo_file->tus != NULL)
8479 {
8480 htab_traverse_noresize (dwo_file->tus,
8481 process_skeletonless_type_unit, info);
8482 }
8483
8484 return 1;
8485}
8486
8487/* Scan all TUs of DWO files, verifying we've processed them.
8488 This is needed in case a TU was emitted without its skeleton.
8489 Note: This can't be done until we know what all the DWO files are. */
8490
8491static void
ed2dc618 8492process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8493{
8494 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8495 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8496 && dwarf2_per_objfile->dwo_files != NULL)
8497 {
51ac9db5 8498 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8499 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8500 dwarf2_per_objfile);
6aa5f3a6 8501 }
348e048f
DE
8502}
8503
ed2dc618 8504/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8505
8506static void
ed2dc618 8507set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8508{
b76e467d 8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8510 {
95554aad 8511 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8512
36586728
TT
8513 if (pst == NULL)
8514 continue;
8515
b76e467d 8516 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8517 {
8518 /* Set the 'user' field only if it is not already set. */
8519 if (pst->dependencies[j]->user == NULL)
8520 pst->dependencies[j]->user = pst;
8521 }
8522 }
8523}
8524
93311388
DE
8525/* Build the partial symbol table by doing a quick pass through the
8526 .debug_info and .debug_abbrev sections. */
72bf9492 8527
93311388 8528static void
ed2dc618 8529dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8530{
ed2dc618 8531 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8532
b4f54984 8533 if (dwarf_read_debug)
45cfd468
DE
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8536 objfile_name (objfile));
45cfd468
DE
8537 }
8538
98bfdba5
PA
8539 dwarf2_per_objfile->reading_partial_symbols = 1;
8540
be391dca 8541 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8542
93311388
DE
8543 /* Any cached compilation units will be linked by the per-objfile
8544 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8545 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8546
ed2dc618 8547 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8548
ed2dc618 8549 create_all_comp_units (dwarf2_per_objfile);
c906108c 8550
60606b2c
TT
8551 /* Create a temporary address map on a temporary obstack. We later
8552 copy this to the final obstack. */
8268c778 8553 auto_obstack temp_obstack;
791afaa2
TT
8554
8555 scoped_restore save_psymtabs_addrmap
d320c2b5 8556 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8557 addrmap_create_mutable (&temp_obstack));
72bf9492 8558
b76e467d
SM
8559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8560 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8561
6aa5f3a6 8562 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8563 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8564
8565 /* Now that all TUs have been processed we can fill in the dependencies. */
8566 if (dwarf2_per_objfile->type_unit_groups != NULL)
8567 {
8568 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8569 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8570 }
8571
b4f54984 8572 if (dwarf_read_debug)
ed2dc618 8573 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8574
ed2dc618 8575 set_partial_user (dwarf2_per_objfile);
95554aad 8576
d320c2b5
TT
8577 objfile->partial_symtabs->psymtabs_addrmap
8578 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8579 objfile->partial_symtabs->obstack ());
791afaa2
TT
8580 /* At this point we want to keep the address map. */
8581 save_psymtabs_addrmap.release ();
ff013f42 8582
b4f54984 8583 if (dwarf_read_debug)
45cfd468 8584 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8585 objfile_name (objfile));
ae038cb0
DJ
8586}
8587
3019eac3 8588/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8589
8590static void
dee91e82 8591load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8592 const gdb_byte *info_ptr,
dee91e82
DE
8593 struct die_info *comp_unit_die,
8594 int has_children,
8595 void *data)
ae038cb0 8596{
dee91e82 8597 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8598
95554aad 8599 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8600
ae038cb0
DJ
8601 /* Check if comp unit has_children.
8602 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8603 If not, there's no more debug_info for this comp unit. */
d85a05f0 8604 if (has_children)
dee91e82
DE
8605 load_partial_dies (reader, info_ptr, 0);
8606}
98bfdba5 8607
dee91e82
DE
8608/* Load the partial DIEs for a secondary CU into memory.
8609 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8610
dee91e82
DE
8611static void
8612load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8613{
58f0c718 8614 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8615 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8616}
8617
ae038cb0 8618static void
ed2dc618 8619read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8620 struct dwarf2_section_info *section,
f1902523 8621 struct dwarf2_section_info *abbrev_section,
b76e467d 8622 unsigned int is_dwz)
ae038cb0 8623{
d521ce57 8624 const gdb_byte *info_ptr;
ed2dc618 8625 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8626
b4f54984 8627 if (dwarf_read_debug)
bf6af496 8628 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8629 get_section_name (section),
8630 get_section_file_name (section));
bf6af496 8631
36586728 8632 dwarf2_read_section (objfile, section);
ae038cb0 8633
36586728 8634 info_ptr = section->buffer;
6e70227d 8635
36586728 8636 while (info_ptr < section->buffer + section->size)
ae038cb0 8637 {
ae038cb0 8638 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8639
9c541725 8640 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8641
f1902523 8642 comp_unit_head cu_header;
ed2dc618
SM
8643 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8644 abbrev_section, info_ptr,
8645 rcuh_kind::COMPILE);
ae038cb0
DJ
8646
8647 /* Save the compilation unit for later lookup. */
f1902523
JK
8648 if (cu_header.unit_type != DW_UT_type)
8649 {
8650 this_cu = XOBNEW (&objfile->objfile_obstack,
8651 struct dwarf2_per_cu_data);
8652 memset (this_cu, 0, sizeof (*this_cu));
8653 }
8654 else
8655 {
8656 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8657 struct signatured_type);
8658 memset (sig_type, 0, sizeof (*sig_type));
8659 sig_type->signature = cu_header.signature;
8660 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8661 this_cu = &sig_type->per_cu;
8662 }
8663 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8664 this_cu->sect_off = sect_off;
f1902523 8665 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8666 this_cu->is_dwz = is_dwz;
e3b94546 8667 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8668 this_cu->section = section;
ae038cb0 8669
b76e467d 8670 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8671
8672 info_ptr = info_ptr + this_cu->length;
8673 }
36586728
TT
8674}
8675
8676/* Create a list of all compilation units in OBJFILE.
8677 This is only done for -readnow and building partial symtabs. */
8678
8679static void
ed2dc618 8680create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8681{
b76e467d 8682 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8683 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8684 &dwarf2_per_objfile->abbrev, 0);
36586728 8685
b76e467d 8686 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8687 if (dwz != NULL)
ed2dc618 8688 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8689 1);
c906108c
SS
8690}
8691
5734ee8b 8692/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8693 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8694 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8695 DW_AT_ranges). See the comments of add_partial_subprogram on how
8696 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8697
72bf9492
DJ
8698static void
8699scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8700 CORE_ADDR *highpc, int set_addrmap,
8701 struct dwarf2_cu *cu)
c906108c 8702{
72bf9492 8703 struct partial_die_info *pdi;
c906108c 8704
91c24f0a
DC
8705 /* Now, march along the PDI's, descending into ones which have
8706 interesting children but skipping the children of the other ones,
8707 until we reach the end of the compilation unit. */
c906108c 8708
72bf9492 8709 pdi = first_die;
91c24f0a 8710
72bf9492
DJ
8711 while (pdi != NULL)
8712 {
52356b79 8713 pdi->fixup (cu);
c906108c 8714
f55ee35c 8715 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8716 children, so we need to look at them. Ditto for anonymous
8717 enums. */
933c6fe4 8718
72bf9492 8719 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8720 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8721 || pdi->tag == DW_TAG_imported_unit
8722 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8723 {
72bf9492 8724 switch (pdi->tag)
c906108c
SS
8725 {
8726 case DW_TAG_subprogram:
b1dc1806 8727 case DW_TAG_inlined_subroutine:
cdc07690 8728 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8729 break;
72929c62 8730 case DW_TAG_constant:
c906108c
SS
8731 case DW_TAG_variable:
8732 case DW_TAG_typedef:
91c24f0a 8733 case DW_TAG_union_type:
72bf9492 8734 if (!pdi->is_declaration)
63d06c5c 8735 {
72bf9492 8736 add_partial_symbol (pdi, cu);
63d06c5c
DC
8737 }
8738 break;
c906108c 8739 case DW_TAG_class_type:
680b30c7 8740 case DW_TAG_interface_type:
c906108c 8741 case DW_TAG_structure_type:
72bf9492 8742 if (!pdi->is_declaration)
c906108c 8743 {
72bf9492 8744 add_partial_symbol (pdi, cu);
c906108c 8745 }
b7fee5a3
KS
8746 if ((cu->language == language_rust
8747 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8748 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8749 set_addrmap, cu);
c906108c 8750 break;
91c24f0a 8751 case DW_TAG_enumeration_type:
72bf9492
DJ
8752 if (!pdi->is_declaration)
8753 add_partial_enumeration (pdi, cu);
c906108c
SS
8754 break;
8755 case DW_TAG_base_type:
a02abb62 8756 case DW_TAG_subrange_type:
c906108c 8757 /* File scope base type definitions are added to the partial
c5aa993b 8758 symbol table. */
72bf9492 8759 add_partial_symbol (pdi, cu);
c906108c 8760 break;
d9fa45fe 8761 case DW_TAG_namespace:
cdc07690 8762 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8763 break;
5d7cb8df 8764 case DW_TAG_module:
59c35742
AB
8765 if (!pdi->is_declaration)
8766 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8767 break;
95554aad
TT
8768 case DW_TAG_imported_unit:
8769 {
8770 struct dwarf2_per_cu_data *per_cu;
8771
f4dc4d17
DE
8772 /* For now we don't handle imported units in type units. */
8773 if (cu->per_cu->is_debug_types)
8774 {
8775 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8776 " supported in type units [in module %s]"),
518817b3 8777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8778 }
8779
e3b94546
SM
8780 per_cu = dwarf2_find_containing_comp_unit
8781 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8782 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8783
8784 /* Go read the partial unit, if needed. */
8785 if (per_cu->v.psymtab == NULL)
b93601f3 8786 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8787
ae640021 8788 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8789 }
8790 break;
74921315
KS
8791 case DW_TAG_imported_declaration:
8792 add_partial_symbol (pdi, cu);
8793 break;
c906108c
SS
8794 default:
8795 break;
8796 }
8797 }
8798
72bf9492
DJ
8799 /* If the die has a sibling, skip to the sibling. */
8800
8801 pdi = pdi->die_sibling;
8802 }
8803}
8804
8805/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8806
72bf9492 8807 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8808 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8809 Enumerators are an exception; they use the scope of their parent
8810 enumeration type, i.e. the name of the enumeration type is not
8811 prepended to the enumerator.
91c24f0a 8812
72bf9492
DJ
8813 There are two complexities. One is DW_AT_specification; in this
8814 case "parent" means the parent of the target of the specification,
8815 instead of the direct parent of the DIE. The other is compilers
8816 which do not emit DW_TAG_namespace; in this case we try to guess
8817 the fully qualified name of structure types from their members'
8818 linkage names. This must be done using the DIE's children rather
8819 than the children of any DW_AT_specification target. We only need
8820 to do this for structures at the top level, i.e. if the target of
8821 any DW_AT_specification (if any; otherwise the DIE itself) does not
8822 have a parent. */
8823
8824/* Compute the scope prefix associated with PDI's parent, in
8825 compilation unit CU. The result will be allocated on CU's
8826 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8827 field. NULL is returned if no prefix is necessary. */
15d034d0 8828static const char *
72bf9492
DJ
8829partial_die_parent_scope (struct partial_die_info *pdi,
8830 struct dwarf2_cu *cu)
8831{
15d034d0 8832 const char *grandparent_scope;
72bf9492 8833 struct partial_die_info *parent, *real_pdi;
91c24f0a 8834
72bf9492
DJ
8835 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8836 then this means the parent of the specification DIE. */
8837
8838 real_pdi = pdi;
72bf9492 8839 while (real_pdi->has_specification)
fb816e8b 8840 {
122cf0f2
AB
8841 auto res = find_partial_die (real_pdi->spec_offset,
8842 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8843 real_pdi = res.pdi;
8844 cu = res.cu;
8845 }
72bf9492
DJ
8846
8847 parent = real_pdi->die_parent;
8848 if (parent == NULL)
8849 return NULL;
8850
8851 if (parent->scope_set)
8852 return parent->scope;
8853
52356b79 8854 parent->fixup (cu);
72bf9492 8855
10b3939b 8856 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8857
acebe513
UW
8858 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8859 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8860 Work around this problem here. */
8861 if (cu->language == language_cplus
6e70227d 8862 && parent->tag == DW_TAG_namespace
acebe513
UW
8863 && strcmp (parent->name, "::") == 0
8864 && grandparent_scope == NULL)
8865 {
8866 parent->scope = NULL;
8867 parent->scope_set = 1;
8868 return NULL;
8869 }
8870
0a4b0913 8871 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8872 if (pdi->tag == DW_TAG_enumerator)
8873 /* Enumerators should not get the name of the enumeration as a prefix. */
8874 parent->scope = grandparent_scope;
8875 else if (parent->tag == DW_TAG_namespace
f55ee35c 8876 || parent->tag == DW_TAG_module
72bf9492
DJ
8877 || parent->tag == DW_TAG_structure_type
8878 || parent->tag == DW_TAG_class_type
680b30c7 8879 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8880 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8881 || parent->tag == DW_TAG_enumeration_type
8882 || (cu->language == language_fortran
8883 && parent->tag == DW_TAG_subprogram
8884 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8885 {
8886 if (grandparent_scope == NULL)
8887 parent->scope = parent->name;
8888 else
3e43a32a
MS
8889 parent->scope = typename_concat (&cu->comp_unit_obstack,
8890 grandparent_scope,
f55ee35c 8891 parent->name, 0, cu);
72bf9492 8892 }
72bf9492
DJ
8893 else
8894 {
8895 /* FIXME drow/2004-04-01: What should we be doing with
8896 function-local names? For partial symbols, we should probably be
8897 ignoring them. */
fa9c3fa0
TT
8898 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8899 dwarf_tag_name (parent->tag),
8900 sect_offset_str (pdi->sect_off));
72bf9492 8901 parent->scope = grandparent_scope;
c906108c
SS
8902 }
8903
72bf9492
DJ
8904 parent->scope_set = 1;
8905 return parent->scope;
8906}
8907
8908/* Return the fully scoped name associated with PDI, from compilation unit
8909 CU. The result will be allocated with malloc. */
4568ecf9 8910
72bf9492
DJ
8911static char *
8912partial_die_full_name (struct partial_die_info *pdi,
8913 struct dwarf2_cu *cu)
8914{
15d034d0 8915 const char *parent_scope;
72bf9492 8916
98bfdba5
PA
8917 /* If this is a template instantiation, we can not work out the
8918 template arguments from partial DIEs. So, unfortunately, we have
8919 to go through the full DIEs. At least any work we do building
8920 types here will be reused if full symbols are loaded later. */
8921 if (pdi->has_template_arguments)
8922 {
52356b79 8923 pdi->fixup (cu);
98bfdba5
PA
8924
8925 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8926 {
8927 struct die_info *die;
8928 struct attribute attr;
8929 struct dwarf2_cu *ref_cu = cu;
8930
b64f50a1 8931 /* DW_FORM_ref_addr is using section offset. */
b4069958 8932 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8933 attr.form = DW_FORM_ref_addr;
9c541725 8934 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8935 die = follow_die_ref (NULL, &attr, &ref_cu);
8936
8937 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8938 }
8939 }
8940
72bf9492
DJ
8941 parent_scope = partial_die_parent_scope (pdi, cu);
8942 if (parent_scope == NULL)
8943 return NULL;
8944 else
f55ee35c 8945 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8946}
8947
8948static void
72bf9492 8949add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8950{
518817b3
SM
8951 struct dwarf2_per_objfile *dwarf2_per_objfile
8952 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8953 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8954 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8955 CORE_ADDR addr = 0;
15d034d0 8956 const char *actual_name = NULL;
e142c38c 8957 CORE_ADDR baseaddr;
15d034d0 8958 char *built_actual_name;
e142c38c
DJ
8959
8960 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8961
15d034d0
TT
8962 built_actual_name = partial_die_full_name (pdi, cu);
8963 if (built_actual_name != NULL)
8964 actual_name = built_actual_name;
63d06c5c 8965
72bf9492
DJ
8966 if (actual_name == NULL)
8967 actual_name = pdi->name;
8968
c906108c
SS
8969 switch (pdi->tag)
8970 {
b1dc1806 8971 case DW_TAG_inlined_subroutine:
c906108c 8972 case DW_TAG_subprogram:
79748972
TT
8973 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8974 - baseaddr);
0a4b0913
AB
8975 if (pdi->is_external
8976 || cu->language == language_ada
8977 || (cu->language == language_fortran
8978 && pdi->die_parent != NULL
8979 && pdi->die_parent->tag == DW_TAG_subprogram))
8980 {
8981 /* Normally, only "external" DIEs are part of the global scope.
8982 But in Ada and Fortran, we want to be able to access nested
8983 procedures globally. So all Ada and Fortran subprograms are
8984 stored in the global scope. */
31edb802 8985 add_psymbol_to_list (actual_name,
15d034d0 8986 built_actual_name != NULL,
f47fb265 8987 VAR_DOMAIN, LOC_BLOCK,
79748972 8988 SECT_OFF_TEXT (objfile),
75aedd27 8989 psymbol_placement::GLOBAL,
79748972
TT
8990 addr,
8991 cu->language, objfile);
c906108c
SS
8992 }
8993 else
8994 {
31edb802 8995 add_psymbol_to_list (actual_name,
15d034d0 8996 built_actual_name != NULL,
f47fb265 8997 VAR_DOMAIN, LOC_BLOCK,
79748972 8998 SECT_OFF_TEXT (objfile),
75aedd27 8999 psymbol_placement::STATIC,
1762568f 9000 addr, cu->language, objfile);
c906108c 9001 }
0c1b455e
TT
9002
9003 if (pdi->main_subprogram && actual_name != NULL)
9004 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 9005 break;
72929c62 9006 case DW_TAG_constant:
31edb802 9007 add_psymbol_to_list (actual_name,
75aedd27
TT
9008 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9009 -1, (pdi->is_external
9010 ? psymbol_placement::GLOBAL
9011 : psymbol_placement::STATIC),
9012 0, cu->language, objfile);
72929c62 9013 break;
c906108c 9014 case DW_TAG_variable:
95554aad
TT
9015 if (pdi->d.locdesc)
9016 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9017
95554aad 9018 if (pdi->d.locdesc
caac4577
JG
9019 && addr == 0
9020 && !dwarf2_per_objfile->has_section_at_zero)
9021 {
9022 /* A global or static variable may also have been stripped
9023 out by the linker if unused, in which case its address
9024 will be nullified; do not add such variables into partial
9025 symbol table then. */
9026 }
9027 else if (pdi->is_external)
c906108c
SS
9028 {
9029 /* Global Variable.
9030 Don't enter into the minimal symbol tables as there is
9031 a minimal symbol table entry from the ELF symbols already.
9032 Enter into partial symbol table if it has a location
9033 descriptor or a type.
9034 If the location descriptor is missing, new_symbol will create
9035 a LOC_UNRESOLVED symbol, the address of the variable will then
9036 be determined from the minimal symbol table whenever the variable
9037 is referenced.
9038 The address for the partial symbol table entry is not
9039 used by GDB, but it comes in handy for debugging partial symbol
9040 table building. */
9041
95554aad 9042 if (pdi->d.locdesc || pdi->has_type)
31edb802 9043 add_psymbol_to_list (actual_name,
15d034d0 9044 built_actual_name != NULL,
f47fb265 9045 VAR_DOMAIN, LOC_STATIC,
79748972 9046 SECT_OFF_TEXT (objfile),
75aedd27 9047 psymbol_placement::GLOBAL,
79748972 9048 addr, cu->language, objfile);
c906108c
SS
9049 }
9050 else
9051 {
ff908ebf
AW
9052 int has_loc = pdi->d.locdesc != NULL;
9053
9054 /* Static Variable. Skip symbols whose value we cannot know (those
9055 without location descriptors or constant values). */
9056 if (!has_loc && !pdi->has_const_value)
decbce07 9057 {
15d034d0 9058 xfree (built_actual_name);
decbce07
MS
9059 return;
9060 }
ff908ebf 9061
31edb802 9062 add_psymbol_to_list (actual_name,
15d034d0 9063 built_actual_name != NULL,
f47fb265 9064 VAR_DOMAIN, LOC_STATIC,
79748972 9065 SECT_OFF_TEXT (objfile),
75aedd27 9066 psymbol_placement::STATIC,
79748972 9067 has_loc ? addr : 0,
f47fb265 9068 cu->language, objfile);
c906108c
SS
9069 }
9070 break;
9071 case DW_TAG_typedef:
9072 case DW_TAG_base_type:
a02abb62 9073 case DW_TAG_subrange_type:
31edb802 9074 add_psymbol_to_list (actual_name,
15d034d0 9075 built_actual_name != NULL,
79748972 9076 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9077 psymbol_placement::STATIC,
1762568f 9078 0, cu->language, objfile);
c906108c 9079 break;
74921315 9080 case DW_TAG_imported_declaration:
72bf9492 9081 case DW_TAG_namespace:
31edb802 9082 add_psymbol_to_list (actual_name,
15d034d0 9083 built_actual_name != NULL,
79748972 9084 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9085 psymbol_placement::GLOBAL,
1762568f 9086 0, cu->language, objfile);
72bf9492 9087 break;
530e8392 9088 case DW_TAG_module:
a5fd13a9
BH
9089 /* With Fortran 77 there might be a "BLOCK DATA" module
9090 available without any name. If so, we skip the module as it
9091 doesn't bring any value. */
9092 if (actual_name != nullptr)
31edb802 9093 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9094 built_actual_name != NULL,
9095 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9096 psymbol_placement::GLOBAL,
9097 0, cu->language, objfile);
530e8392 9098 break;
c906108c 9099 case DW_TAG_class_type:
680b30c7 9100 case DW_TAG_interface_type:
c906108c
SS
9101 case DW_TAG_structure_type:
9102 case DW_TAG_union_type:
9103 case DW_TAG_enumeration_type:
fa4028e9
JB
9104 /* Skip external references. The DWARF standard says in the section
9105 about "Structure, Union, and Class Type Entries": "An incomplete
9106 structure, union or class type is represented by a structure,
9107 union or class entry that does not have a byte size attribute
9108 and that has a DW_AT_declaration attribute." */
9109 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9110 {
15d034d0 9111 xfree (built_actual_name);
decbce07
MS
9112 return;
9113 }
fa4028e9 9114
63d06c5c
DC
9115 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9116 static vs. global. */
31edb802 9117 add_psymbol_to_list (actual_name,
15d034d0 9118 built_actual_name != NULL,
79748972 9119 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9120 cu->language == language_cplus
75aedd27
TT
9121 ? psymbol_placement::GLOBAL
9122 : psymbol_placement::STATIC,
1762568f 9123 0, cu->language, objfile);
c906108c 9124
c906108c
SS
9125 break;
9126 case DW_TAG_enumerator:
31edb802 9127 add_psymbol_to_list (actual_name,
15d034d0 9128 built_actual_name != NULL,
79748972 9129 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9130 cu->language == language_cplus
75aedd27
TT
9131 ? psymbol_placement::GLOBAL
9132 : psymbol_placement::STATIC,
1762568f 9133 0, cu->language, objfile);
c906108c
SS
9134 break;
9135 default:
9136 break;
9137 }
5c4e30ca 9138
15d034d0 9139 xfree (built_actual_name);
c906108c
SS
9140}
9141
5c4e30ca
DC
9142/* Read a partial die corresponding to a namespace; also, add a symbol
9143 corresponding to that namespace to the symbol table. NAMESPACE is
9144 the name of the enclosing namespace. */
91c24f0a 9145
72bf9492
DJ
9146static void
9147add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9149 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9150{
72bf9492 9151 /* Add a symbol for the namespace. */
e7c27a73 9152
72bf9492 9153 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9154
9155 /* Now scan partial symbols in that namespace. */
9156
91c24f0a 9157 if (pdi->has_children)
cdc07690 9158 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9159}
9160
5d7cb8df
JK
9161/* Read a partial die corresponding to a Fortran module. */
9162
9163static void
9164add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9165 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9166{
530e8392
KB
9167 /* Add a symbol for the namespace. */
9168
9169 add_partial_symbol (pdi, cu);
9170
f55ee35c 9171 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9172
9173 if (pdi->has_children)
cdc07690 9174 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9175}
9176
b1dc1806
XR
9177/* Read a partial die corresponding to a subprogram or an inlined
9178 subprogram and create a partial symbol for that subprogram.
9179 When the CU language allows it, this routine also defines a partial
9180 symbol for each nested subprogram that this subprogram contains.
9181 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9182 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9183
cdc07690
YQ
9184 PDI may also be a lexical block, in which case we simply search
9185 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9186 Again, this is only performed when the CU language allows this
9187 type of definitions. */
9188
9189static void
9190add_partial_subprogram (struct partial_die_info *pdi,
9191 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9192 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9193{
b1dc1806 9194 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9195 {
9196 if (pdi->has_pc_info)
9197 {
9198 if (pdi->lowpc < *lowpc)
9199 *lowpc = pdi->lowpc;
9200 if (pdi->highpc > *highpc)
9201 *highpc = pdi->highpc;
cdc07690 9202 if (set_addrmap)
5734ee8b 9203 {
518817b3 9204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9206 CORE_ADDR baseaddr;
b926417a
TT
9207 CORE_ADDR this_highpc;
9208 CORE_ADDR this_lowpc;
5734ee8b
DJ
9209
9210 baseaddr = ANOFFSET (objfile->section_offsets,
9211 SECT_OFF_TEXT (objfile));
b926417a
TT
9212 this_lowpc
9213 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9214 pdi->lowpc + baseaddr)
9215 - baseaddr);
9216 this_highpc
9217 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9218 pdi->highpc + baseaddr)
9219 - baseaddr);
d320c2b5 9220 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9221 this_lowpc, this_highpc - 1,
9291a0cd 9222 cu->per_cu->v.psymtab);
5734ee8b 9223 }
481860b3
GB
9224 }
9225
9226 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9227 {
bc30ff58 9228 if (!pdi->is_declaration)
e8d05480
JB
9229 /* Ignore subprogram DIEs that do not have a name, they are
9230 illegal. Do not emit a complaint at this point, we will
9231 do so when we convert this psymtab into a symtab. */
9232 if (pdi->name)
9233 add_partial_symbol (pdi, cu);
bc30ff58
JB
9234 }
9235 }
6e70227d 9236
bc30ff58
JB
9237 if (! pdi->has_children)
9238 return;
9239
0a4b0913 9240 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9241 {
9242 pdi = pdi->die_child;
9243 while (pdi != NULL)
9244 {
52356b79 9245 pdi->fixup (cu);
bc30ff58 9246 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9247 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9248 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9249 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9250 pdi = pdi->die_sibling;
9251 }
9252 }
9253}
9254
91c24f0a
DC
9255/* Read a partial die corresponding to an enumeration type. */
9256
72bf9492
DJ
9257static void
9258add_partial_enumeration (struct partial_die_info *enum_pdi,
9259 struct dwarf2_cu *cu)
91c24f0a 9260{
72bf9492 9261 struct partial_die_info *pdi;
91c24f0a
DC
9262
9263 if (enum_pdi->name != NULL)
72bf9492
DJ
9264 add_partial_symbol (enum_pdi, cu);
9265
9266 pdi = enum_pdi->die_child;
9267 while (pdi)
91c24f0a 9268 {
72bf9492 9269 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9270 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9271 else
72bf9492
DJ
9272 add_partial_symbol (pdi, cu);
9273 pdi = pdi->die_sibling;
91c24f0a 9274 }
91c24f0a
DC
9275}
9276
6caca83c
CC
9277/* Return the initial uleb128 in the die at INFO_PTR. */
9278
9279static unsigned int
d521ce57 9280peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9281{
9282 unsigned int bytes_read;
9283
9284 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9285}
9286
685af9cd
TT
9287/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9288 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9289
4bb7a0a7
DJ
9290 Return the corresponding abbrev, or NULL if the number is zero (indicating
9291 an empty DIE). In either case *BYTES_READ will be set to the length of
9292 the initial number. */
9293
9294static struct abbrev_info *
685af9cd
TT
9295peek_die_abbrev (const die_reader_specs &reader,
9296 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9297{
685af9cd 9298 dwarf2_cu *cu = reader.cu;
518817b3 9299 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9300 unsigned int abbrev_number
9301 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9302
9303 if (abbrev_number == 0)
9304 return NULL;
9305
685af9cd 9306 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9307 if (!abbrev)
9308 {
422b9917 9309 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9310 " at offset %s [in module %s]"),
422b9917 9311 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9312 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9313 }
9314
9315 return abbrev;
9316}
9317
93311388
DE
9318/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9319 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9320 DIE. Any children of the skipped DIEs will also be skipped. */
9321
d521ce57
TT
9322static const gdb_byte *
9323skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9324{
4bb7a0a7
DJ
9325 while (1)
9326 {
685af9cd
TT
9327 unsigned int bytes_read;
9328 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9329
4bb7a0a7
DJ
9330 if (abbrev == NULL)
9331 return info_ptr + bytes_read;
9332 else
dee91e82 9333 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9334 }
9335}
9336
93311388
DE
9337/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9338 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9339 abbrev corresponding to that skipped uleb128 should be passed in
9340 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9341 children. */
9342
d521ce57
TT
9343static const gdb_byte *
9344skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9345 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9346{
9347 unsigned int bytes_read;
9348 struct attribute attr;
dee91e82
DE
9349 bfd *abfd = reader->abfd;
9350 struct dwarf2_cu *cu = reader->cu;
d521ce57 9351 const gdb_byte *buffer = reader->buffer;
f664829e 9352 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9353 unsigned int form, i;
9354
9355 for (i = 0; i < abbrev->num_attrs; i++)
9356 {
9357 /* The only abbrev we care about is DW_AT_sibling. */
9358 if (abbrev->attrs[i].name == DW_AT_sibling)
9359 {
dee91e82 9360 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9361 if (attr.form == DW_FORM_ref_addr)
b98664d3 9362 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9363 else
b9502d3f 9364 {
9c541725
PA
9365 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9366 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9367
9368 if (sibling_ptr < info_ptr)
b98664d3 9369 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9370 else if (sibling_ptr > reader->buffer_end)
9371 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9372 else
9373 return sibling_ptr;
9374 }
4bb7a0a7
DJ
9375 }
9376
9377 /* If it isn't DW_AT_sibling, skip this attribute. */
9378 form = abbrev->attrs[i].form;
9379 skip_attribute:
9380 switch (form)
9381 {
4bb7a0a7 9382 case DW_FORM_ref_addr:
ae411497
TT
9383 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9384 and later it is offset sized. */
9385 if (cu->header.version == 2)
9386 info_ptr += cu->header.addr_size;
9387 else
9388 info_ptr += cu->header.offset_size;
9389 break;
36586728
TT
9390 case DW_FORM_GNU_ref_alt:
9391 info_ptr += cu->header.offset_size;
9392 break;
ae411497 9393 case DW_FORM_addr:
4bb7a0a7
DJ
9394 info_ptr += cu->header.addr_size;
9395 break;
9396 case DW_FORM_data1:
9397 case DW_FORM_ref1:
9398 case DW_FORM_flag:
8fe0f950 9399 case DW_FORM_strx1:
4bb7a0a7
DJ
9400 info_ptr += 1;
9401 break;
2dc7f7b3 9402 case DW_FORM_flag_present:
43988095 9403 case DW_FORM_implicit_const:
2dc7f7b3 9404 break;
4bb7a0a7
DJ
9405 case DW_FORM_data2:
9406 case DW_FORM_ref2:
8fe0f950 9407 case DW_FORM_strx2:
4bb7a0a7
DJ
9408 info_ptr += 2;
9409 break;
8fe0f950
AT
9410 case DW_FORM_strx3:
9411 info_ptr += 3;
9412 break;
4bb7a0a7
DJ
9413 case DW_FORM_data4:
9414 case DW_FORM_ref4:
8fe0f950 9415 case DW_FORM_strx4:
4bb7a0a7
DJ
9416 info_ptr += 4;
9417 break;
9418 case DW_FORM_data8:
9419 case DW_FORM_ref8:
55f1336d 9420 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9421 info_ptr += 8;
9422 break;
0224619f
JK
9423 case DW_FORM_data16:
9424 info_ptr += 16;
9425 break;
4bb7a0a7 9426 case DW_FORM_string:
9b1c24c8 9427 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9428 info_ptr += bytes_read;
9429 break;
2dc7f7b3 9430 case DW_FORM_sec_offset:
4bb7a0a7 9431 case DW_FORM_strp:
36586728 9432 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9433 info_ptr += cu->header.offset_size;
9434 break;
2dc7f7b3 9435 case DW_FORM_exprloc:
4bb7a0a7
DJ
9436 case DW_FORM_block:
9437 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9438 info_ptr += bytes_read;
9439 break;
9440 case DW_FORM_block1:
9441 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9442 break;
9443 case DW_FORM_block2:
9444 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9445 break;
9446 case DW_FORM_block4:
9447 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9448 break;
336d760d 9449 case DW_FORM_addrx:
cf532bd1 9450 case DW_FORM_strx:
4bb7a0a7
DJ
9451 case DW_FORM_sdata:
9452 case DW_FORM_udata:
9453 case DW_FORM_ref_udata:
3019eac3
DE
9454 case DW_FORM_GNU_addr_index:
9455 case DW_FORM_GNU_str_index:
d521ce57 9456 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9457 break;
9458 case DW_FORM_indirect:
9459 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9460 info_ptr += bytes_read;
9461 /* We need to continue parsing from here, so just go back to
9462 the top. */
9463 goto skip_attribute;
9464
9465 default:
3e43a32a
MS
9466 error (_("Dwarf Error: Cannot handle %s "
9467 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9468 dwarf_form_name (form),
9469 bfd_get_filename (abfd));
9470 }
9471 }
9472
9473 if (abbrev->has_children)
dee91e82 9474 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9475 else
9476 return info_ptr;
9477}
9478
93311388 9479/* Locate ORIG_PDI's sibling.
dee91e82 9480 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9481
d521ce57 9482static const gdb_byte *
dee91e82
DE
9483locate_pdi_sibling (const struct die_reader_specs *reader,
9484 struct partial_die_info *orig_pdi,
d521ce57 9485 const gdb_byte *info_ptr)
91c24f0a
DC
9486{
9487 /* Do we know the sibling already? */
72bf9492 9488
91c24f0a
DC
9489 if (orig_pdi->sibling)
9490 return orig_pdi->sibling;
9491
9492 /* Are there any children to deal with? */
9493
9494 if (!orig_pdi->has_children)
9495 return info_ptr;
9496
4bb7a0a7 9497 /* Skip the children the long way. */
91c24f0a 9498
dee91e82 9499 return skip_children (reader, info_ptr);
91c24f0a
DC
9500}
9501
257e7a09 9502/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9503 not NULL. */
c906108c
SS
9504
9505static void
257e7a09
YQ
9506dwarf2_read_symtab (struct partial_symtab *self,
9507 struct objfile *objfile)
c906108c 9508{
ed2dc618
SM
9509 struct dwarf2_per_objfile *dwarf2_per_objfile
9510 = get_dwarf2_per_objfile (objfile);
9511
257e7a09 9512 if (self->readin)
c906108c 9513 {
442e4d9c 9514 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9515 self->filename);
442e4d9c
YQ
9516 }
9517 else
9518 {
9519 if (info_verbose)
c906108c 9520 {
442e4d9c 9521 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9522 self->filename);
442e4d9c 9523 gdb_flush (gdb_stdout);
c906108c 9524 }
c906108c 9525
442e4d9c
YQ
9526 /* If this psymtab is constructed from a debug-only objfile, the
9527 has_section_at_zero flag will not necessarily be correct. We
9528 can get the correct value for this flag by looking at the data
9529 associated with the (presumably stripped) associated objfile. */
9530 if (objfile->separate_debug_objfile_backlink)
9531 {
9532 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9533 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9534
442e4d9c
YQ
9535 dwarf2_per_objfile->has_section_at_zero
9536 = dpo_backlink->has_section_at_zero;
9537 }
b2ab525c 9538
442e4d9c 9539 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9540
257e7a09 9541 psymtab_to_symtab_1 (self);
c906108c 9542
442e4d9c
YQ
9543 /* Finish up the debug error message. */
9544 if (info_verbose)
9545 printf_filtered (_("done.\n"));
c906108c 9546 }
95554aad 9547
ed2dc618 9548 process_cu_includes (dwarf2_per_objfile);
c906108c 9549}
9cdd5dbd
DE
9550\f
9551/* Reading in full CUs. */
c906108c 9552
10b3939b
DJ
9553/* Add PER_CU to the queue. */
9554
9555static void
95554aad
TT
9556queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9557 enum language pretend_language)
10b3939b
DJ
9558{
9559 struct dwarf2_queue_item *item;
9560
9561 per_cu->queued = 1;
8d749320 9562 item = XNEW (struct dwarf2_queue_item);
10b3939b 9563 item->per_cu = per_cu;
95554aad 9564 item->pretend_language = pretend_language;
10b3939b
DJ
9565 item->next = NULL;
9566
9567 if (dwarf2_queue == NULL)
9568 dwarf2_queue = item;
9569 else
9570 dwarf2_queue_tail->next = item;
9571
9572 dwarf2_queue_tail = item;
9573}
9574
89e63ee4
DE
9575/* If PER_CU is not yet queued, add it to the queue.
9576 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9577 dependency.
0907af0c 9578 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9579 meaning either PER_CU is already queued or it is already loaded.
9580
9581 N.B. There is an invariant here that if a CU is queued then it is loaded.
9582 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9583
9584static int
89e63ee4 9585maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9586 struct dwarf2_per_cu_data *per_cu,
9587 enum language pretend_language)
9588{
9589 /* We may arrive here during partial symbol reading, if we need full
9590 DIEs to process an unusual case (e.g. template arguments). Do
9591 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9592 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9593 {
9594 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9595 return 1;
9596 return 0;
9597 }
9598
9599 /* Mark the dependence relation so that we don't flush PER_CU
9600 too early. */
89e63ee4
DE
9601 if (dependent_cu != NULL)
9602 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9603
9604 /* If it's already on the queue, we have nothing to do. */
9605 if (per_cu->queued)
9606 return 0;
9607
9608 /* If the compilation unit is already loaded, just mark it as
9609 used. */
9610 if (per_cu->cu != NULL)
9611 {
9612 per_cu->cu->last_used = 0;
9613 return 0;
9614 }
9615
9616 /* Add it to the queue. */
9617 queue_comp_unit (per_cu, pretend_language);
9618
9619 return 1;
9620}
9621
10b3939b
DJ
9622/* Process the queue. */
9623
9624static void
ed2dc618 9625process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9626{
9627 struct dwarf2_queue_item *item, *next_item;
9628
b4f54984 9629 if (dwarf_read_debug)
45cfd468
DE
9630 {
9631 fprintf_unfiltered (gdb_stdlog,
9632 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9633 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9634 }
9635
03dd20cc
DJ
9636 /* The queue starts out with one item, but following a DIE reference
9637 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9638 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9639 {
cc12ce38
DE
9640 if ((dwarf2_per_objfile->using_index
9641 ? !item->per_cu->v.quick->compunit_symtab
9642 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9643 /* Skip dummy CUs. */
9644 && item->per_cu->cu != NULL)
f4dc4d17
DE
9645 {
9646 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9647 unsigned int debug_print_threshold;
247f5c4f 9648 char buf[100];
f4dc4d17 9649
247f5c4f 9650 if (per_cu->is_debug_types)
f4dc4d17 9651 {
247f5c4f
DE
9652 struct signatured_type *sig_type =
9653 (struct signatured_type *) per_cu;
9654
9d8780f0 9655 sprintf (buf, "TU %s at offset %s",
73be47f5 9656 hex_string (sig_type->signature),
9d8780f0 9657 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9658 /* There can be 100s of TUs.
9659 Only print them in verbose mode. */
9660 debug_print_threshold = 2;
f4dc4d17 9661 }
247f5c4f 9662 else
73be47f5 9663 {
9d8780f0
SM
9664 sprintf (buf, "CU at offset %s",
9665 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9666 debug_print_threshold = 1;
9667 }
247f5c4f 9668
b4f54984 9669 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9670 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9671
9672 if (per_cu->is_debug_types)
9673 process_full_type_unit (per_cu, item->pretend_language);
9674 else
9675 process_full_comp_unit (per_cu, item->pretend_language);
9676
b4f54984 9677 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9678 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9679 }
10b3939b
DJ
9680
9681 item->per_cu->queued = 0;
9682 next_item = item->next;
9683 xfree (item);
9684 }
9685
9686 dwarf2_queue_tail = NULL;
45cfd468 9687
b4f54984 9688 if (dwarf_read_debug)
45cfd468
DE
9689 {
9690 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9691 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9692 }
10b3939b
DJ
9693}
9694
10b3939b
DJ
9695/* Read in full symbols for PST, and anything it depends on. */
9696
c906108c 9697static void
fba45db2 9698psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9699{
10b3939b 9700 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9701 int i;
9702
95554aad
TT
9703 if (pst->readin)
9704 return;
9705
aaa75496 9706 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9707 if (!pst->dependencies[i]->readin
9708 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9709 {
9710 /* Inform about additional files that need to be read in. */
9711 if (info_verbose)
9712 {
a3f17187 9713 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9714 fputs_filtered (" ", gdb_stdout);
9715 wrap_here ("");
9716 fputs_filtered ("and ", gdb_stdout);
9717 wrap_here ("");
9718 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9719 wrap_here (""); /* Flush output. */
aaa75496
JB
9720 gdb_flush (gdb_stdout);
9721 }
9722 psymtab_to_symtab_1 (pst->dependencies[i]);
9723 }
9724
9a3c8263 9725 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9726
9727 if (per_cu == NULL)
aaa75496
JB
9728 {
9729 /* It's an include file, no symbols to read for it.
9730 Everything is in the parent symtab. */
9731 pst->readin = 1;
9732 return;
9733 }
c906108c 9734
58f0c718 9735 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9736}
9737
dee91e82
DE
9738/* Trivial hash function for die_info: the hash value of a DIE
9739 is its offset in .debug_info for this objfile. */
10b3939b 9740
dee91e82
DE
9741static hashval_t
9742die_hash (const void *item)
10b3939b 9743{
9a3c8263 9744 const struct die_info *die = (const struct die_info *) item;
6502dd73 9745
9c541725 9746 return to_underlying (die->sect_off);
dee91e82 9747}
63d06c5c 9748
dee91e82
DE
9749/* Trivial comparison function for die_info structures: two DIEs
9750 are equal if they have the same offset. */
98bfdba5 9751
dee91e82
DE
9752static int
9753die_eq (const void *item_lhs, const void *item_rhs)
9754{
9a3c8263
SM
9755 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9756 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9757
9c541725 9758 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9759}
c906108c 9760
dee91e82
DE
9761/* die_reader_func for load_full_comp_unit.
9762 This is identical to read_signatured_type_reader,
9763 but is kept separate for now. */
c906108c 9764
dee91e82
DE
9765static void
9766load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9767 const gdb_byte *info_ptr,
dee91e82
DE
9768 struct die_info *comp_unit_die,
9769 int has_children,
9770 void *data)
9771{
9772 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9773 enum language *language_ptr = (enum language *) data;
6caca83c 9774
dee91e82
DE
9775 gdb_assert (cu->die_hash == NULL);
9776 cu->die_hash =
9777 htab_create_alloc_ex (cu->header.length / 12,
9778 die_hash,
9779 die_eq,
9780 NULL,
9781 &cu->comp_unit_obstack,
9782 hashtab_obstack_allocate,
9783 dummy_obstack_deallocate);
e142c38c 9784
dee91e82
DE
9785 if (has_children)
9786 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9787 &info_ptr, comp_unit_die);
9788 cu->dies = comp_unit_die;
9789 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9790
9791 /* We try not to read any attributes in this function, because not
9cdd5dbd 9792 all CUs needed for references have been loaded yet, and symbol
10b3939b 9793 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9794 or we won't be able to build types correctly.
9795 Similarly, if we do not read the producer, we can not apply
9796 producer-specific interpretation. */
95554aad 9797 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9798}
10b3939b 9799
dee91e82 9800/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9801
dee91e82 9802static void
95554aad 9803load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9804 bool skip_partial,
95554aad 9805 enum language pretend_language)
dee91e82 9806{
3019eac3 9807 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9808
58f0c718 9809 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9810 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9811}
9812
3da10d80
KS
9813/* Add a DIE to the delayed physname list. */
9814
9815static void
9816add_to_method_list (struct type *type, int fnfield_index, int index,
9817 const char *name, struct die_info *die,
9818 struct dwarf2_cu *cu)
9819{
9820 struct delayed_method_info mi;
9821 mi.type = type;
9822 mi.fnfield_index = fnfield_index;
9823 mi.index = index;
9824 mi.name = name;
9825 mi.die = die;
c89b44cd 9826 cu->method_list.push_back (mi);
3da10d80
KS
9827}
9828
3693fdb3
PA
9829/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9830 "const" / "volatile". If so, decrements LEN by the length of the
9831 modifier and return true. Otherwise return false. */
9832
9833template<size_t N>
9834static bool
9835check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9836{
9837 size_t mod_len = sizeof (mod) - 1;
9838 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9839 {
9840 len -= mod_len;
9841 return true;
9842 }
9843 return false;
9844}
9845
3da10d80
KS
9846/* Compute the physnames of any methods on the CU's method list.
9847
9848 The computation of method physnames is delayed in order to avoid the
9849 (bad) condition that one of the method's formal parameters is of an as yet
9850 incomplete type. */
9851
9852static void
9853compute_delayed_physnames (struct dwarf2_cu *cu)
9854{
3693fdb3 9855 /* Only C++ delays computing physnames. */
c89b44cd 9856 if (cu->method_list.empty ())
3693fdb3
PA
9857 return;
9858 gdb_assert (cu->language == language_cplus);
9859
52941706 9860 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9861 {
1d06ead6 9862 const char *physname;
3da10d80 9863 struct fn_fieldlist *fn_flp
c89b44cd
TT
9864 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9865 physname = dwarf2_physname (mi.name, mi.die, cu);
9866 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9867 = physname ? physname : "";
3693fdb3
PA
9868
9869 /* Since there's no tag to indicate whether a method is a
9870 const/volatile overload, extract that information out of the
9871 demangled name. */
9872 if (physname != NULL)
9873 {
9874 size_t len = strlen (physname);
9875
9876 while (1)
9877 {
9878 if (physname[len] == ')') /* shortcut */
9879 break;
9880 else if (check_modifier (physname, len, " const"))
c89b44cd 9881 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9882 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9883 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9884 else
9885 break;
9886 }
9887 }
3da10d80 9888 }
c89b44cd
TT
9889
9890 /* The list is no longer needed. */
9891 cu->method_list.clear ();
3da10d80
KS
9892}
9893
a766d390
DE
9894/* Go objects should be embedded in a DW_TAG_module DIE,
9895 and it's not clear if/how imported objects will appear.
9896 To keep Go support simple until that's worked out,
9897 go back through what we've read and create something usable.
9898 We could do this while processing each DIE, and feels kinda cleaner,
9899 but that way is more invasive.
9900 This is to, for example, allow the user to type "p var" or "b main"
9901 without having to specify the package name, and allow lookups
9902 of module.object to work in contexts that use the expression
9903 parser. */
9904
9905static void
9906fixup_go_packaging (struct dwarf2_cu *cu)
9907{
9908 char *package_name = NULL;
9909 struct pending *list;
9910 int i;
9911
c24bdb02 9912 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9913 list != NULL;
9914 list = list->next)
a766d390
DE
9915 {
9916 for (i = 0; i < list->nsyms; ++i)
9917 {
9918 struct symbol *sym = list->symbol[i];
9919
9920 if (SYMBOL_LANGUAGE (sym) == language_go
9921 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9922 {
9923 char *this_package_name = go_symbol_package_name (sym);
9924
9925 if (this_package_name == NULL)
9926 continue;
9927 if (package_name == NULL)
9928 package_name = this_package_name;
9929 else
9930 {
518817b3
SM
9931 struct objfile *objfile
9932 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9933 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9934 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9935 (symbol_symtab (sym) != NULL
9936 ? symtab_to_filename_for_display
9937 (symbol_symtab (sym))
e3b94546 9938 : objfile_name (objfile)),
a766d390
DE
9939 this_package_name, package_name);
9940 xfree (this_package_name);
9941 }
9942 }
9943 }
9944 }
9945
9946 if (package_name != NULL)
9947 {
518817b3 9948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9949 const char *saved_package_name
021887d8 9950 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9951 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9952 saved_package_name);
a766d390
DE
9953 struct symbol *sym;
9954
e623cf5d 9955 sym = allocate_symbol (objfile);
f85f34ed 9956 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
31edb802 9957 SYMBOL_SET_NAMES (sym, saved_package_name, false, objfile);
a766d390
DE
9958 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9959 e.g., "main" finds the "main" module and not C's main(). */
9960 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9961 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9962 SYMBOL_TYPE (sym) = type;
9963
c24bdb02 9964 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9965
9966 xfree (package_name);
9967 }
9968}
9969
c9317f21
TT
9970/* Allocate a fully-qualified name consisting of the two parts on the
9971 obstack. */
9972
9973static const char *
9974rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9975{
9976 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9977}
9978
9979/* A helper that allocates a struct discriminant_info to attach to a
9980 union type. */
9981
9982static struct discriminant_info *
9983alloc_discriminant_info (struct type *type, int discriminant_index,
9984 int default_index)
9985{
9986 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9987 gdb_assert (discriminant_index == -1
9988 || (discriminant_index >= 0
9989 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9990 gdb_assert (default_index == -1
c7b15a66 9991 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9992
9993 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9994
9995 struct discriminant_info *disc
9996 = ((struct discriminant_info *)
9997 TYPE_ZALLOC (type,
9998 offsetof (struct discriminant_info, discriminants)
9999 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
10000 disc->default_index = default_index;
10001 disc->discriminant_index = discriminant_index;
10002
10003 struct dynamic_prop prop;
10004 prop.kind = PROP_UNDEFINED;
10005 prop.data.baton = disc;
10006
10007 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
10008
10009 return disc;
10010}
10011
10012/* Some versions of rustc emitted enums in an unusual way.
10013
10014 Ordinary enums were emitted as unions. The first element of each
10015 structure in the union was named "RUST$ENUM$DISR". This element
10016 held the discriminant.
10017
10018 These versions of Rust also implemented the "non-zero"
10019 optimization. When the enum had two values, and one is empty and
10020 the other holds a pointer that cannot be zero, the pointer is used
10021 as the discriminant, with a zero value meaning the empty variant.
10022 Here, the union's first member is of the form
10023 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10024 where the fieldnos are the indices of the fields that should be
10025 traversed in order to find the field (which may be several fields deep)
10026 and the variantname is the name of the variant of the case when the
10027 field is zero.
10028
10029 This function recognizes whether TYPE is of one of these forms,
10030 and, if so, smashes it to be a variant type. */
10031
10032static void
10033quirk_rust_enum (struct type *type, struct objfile *objfile)
10034{
10035 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10036
10037 /* We don't need to deal with empty enums. */
10038 if (TYPE_NFIELDS (type) == 0)
10039 return;
10040
10041#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10042 if (TYPE_NFIELDS (type) == 1
10043 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10044 {
10045 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10046
10047 /* Decode the field name to find the offset of the
10048 discriminant. */
10049 ULONGEST bit_offset = 0;
10050 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10051 while (name[0] >= '0' && name[0] <= '9')
10052 {
10053 char *tail;
10054 unsigned long index = strtoul (name, &tail, 10);
10055 name = tail;
10056 if (*name != '$'
10057 || index >= TYPE_NFIELDS (field_type)
10058 || (TYPE_FIELD_LOC_KIND (field_type, index)
10059 != FIELD_LOC_KIND_BITPOS))
10060 {
b98664d3 10061 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10062 "[in module %s]"),
10063 TYPE_FIELD_NAME (type, 0),
10064 objfile_name (objfile));
10065 return;
10066 }
10067 ++name;
10068
10069 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10070 field_type = TYPE_FIELD_TYPE (field_type, index);
10071 }
10072
10073 /* Make a union to hold the variants. */
10074 struct type *union_type = alloc_type (objfile);
10075 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10076 TYPE_NFIELDS (union_type) = 3;
10077 TYPE_FIELDS (union_type)
10078 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10079 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10080 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10081
10082 /* Put the discriminant must at index 0. */
10083 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10084 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10085 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10086 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10087
10088 /* The order of fields doesn't really matter, so put the real
10089 field at index 1 and the data-less field at index 2. */
10090 struct discriminant_info *disc
10091 = alloc_discriminant_info (union_type, 0, 1);
10092 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10093 TYPE_FIELD_NAME (union_type, 1)
10094 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10095 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10096 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10097 TYPE_FIELD_NAME (union_type, 1));
10098
10099 const char *dataless_name
10100 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10101 name);
10102 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10103 dataless_name);
10104 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10105 /* NAME points into the original discriminant name, which
10106 already has the correct lifetime. */
10107 TYPE_FIELD_NAME (union_type, 2) = name;
10108 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10109 disc->discriminants[2] = 0;
10110
10111 /* Smash this type to be a structure type. We have to do this
10112 because the type has already been recorded. */
10113 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10114 TYPE_NFIELDS (type) = 1;
10115 TYPE_FIELDS (type)
10116 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10117
10118 /* Install the variant part. */
10119 TYPE_FIELD_TYPE (type, 0) = union_type;
10120 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10121 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10122 }
77c2dba3
TT
10123 /* A union with a single anonymous field is probably an old-style
10124 univariant enum. */
10125 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10126 {
c9317f21
TT
10127 /* Smash this type to be a structure type. We have to do this
10128 because the type has already been recorded. */
10129 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10130
10131 /* Make a union to hold the variants. */
10132 struct type *union_type = alloc_type (objfile);
10133 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10134 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10135 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10136 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10137 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10138
10139 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10140 const char *variant_name
10141 = rust_last_path_segment (TYPE_NAME (field_type));
10142 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10143 TYPE_NAME (field_type)
10144 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10145 TYPE_NAME (type), variant_name);
c9317f21
TT
10146
10147 /* Install the union in the outer struct type. */
10148 TYPE_NFIELDS (type) = 1;
10149 TYPE_FIELDS (type)
10150 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10151 TYPE_FIELD_TYPE (type, 0) = union_type;
10152 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10153 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10154
10155 alloc_discriminant_info (union_type, -1, 0);
10156 }
10157 else
10158 {
10159 struct type *disr_type = nullptr;
10160 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10161 {
10162 disr_type = TYPE_FIELD_TYPE (type, i);
10163
a037790e
TT
10164 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10165 {
10166 /* All fields of a true enum will be structs. */
10167 return;
10168 }
10169 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10170 {
10171 /* Could be data-less variant, so keep going. */
a037790e 10172 disr_type = nullptr;
c9317f21
TT
10173 }
10174 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10175 "RUST$ENUM$DISR") != 0)
10176 {
10177 /* Not a Rust enum. */
10178 return;
10179 }
10180 else
10181 {
10182 /* Found one. */
10183 break;
10184 }
10185 }
10186
10187 /* If we got here without a discriminant, then it's probably
10188 just a union. */
10189 if (disr_type == nullptr)
10190 return;
10191
10192 /* Smash this type to be a structure type. We have to do this
10193 because the type has already been recorded. */
10194 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10195
10196 /* Make a union to hold the variants. */
10197 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10198 struct type *union_type = alloc_type (objfile);
10199 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10200 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10201 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10202 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10203 TYPE_FIELDS (union_type)
10204 = (struct field *) TYPE_ZALLOC (union_type,
10205 (TYPE_NFIELDS (union_type)
10206 * sizeof (struct field)));
10207
10208 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10209 TYPE_NFIELDS (type) * sizeof (struct field));
10210
10211 /* Install the discriminant at index 0 in the union. */
10212 TYPE_FIELD (union_type, 0) = *disr_field;
10213 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10214 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10215
10216 /* Install the union in the outer struct type. */
10217 TYPE_FIELD_TYPE (type, 0) = union_type;
10218 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10219 TYPE_NFIELDS (type) = 1;
10220
10221 /* Set the size and offset of the union type. */
10222 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10223
10224 /* We need a way to find the correct discriminant given a
10225 variant name. For convenience we build a map here. */
10226 struct type *enum_type = FIELD_TYPE (*disr_field);
10227 std::unordered_map<std::string, ULONGEST> discriminant_map;
10228 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10229 {
10230 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10231 {
10232 const char *name
10233 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10234 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10235 }
10236 }
10237
10238 int n_fields = TYPE_NFIELDS (union_type);
10239 struct discriminant_info *disc
10240 = alloc_discriminant_info (union_type, 0, -1);
10241 /* Skip the discriminant here. */
10242 for (int i = 1; i < n_fields; ++i)
10243 {
10244 /* Find the final word in the name of this variant's type.
10245 That name can be used to look up the correct
10246 discriminant. */
10247 const char *variant_name
10248 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10249 i)));
10250
10251 auto iter = discriminant_map.find (variant_name);
10252 if (iter != discriminant_map.end ())
10253 disc->discriminants[i] = iter->second;
10254
bedda9ac 10255 /* Remove the discriminant field, if it exists. */
c9317f21 10256 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10257 if (TYPE_NFIELDS (sub_type) > 0)
10258 {
10259 --TYPE_NFIELDS (sub_type);
10260 ++TYPE_FIELDS (sub_type);
10261 }
c9317f21
TT
10262 TYPE_FIELD_NAME (union_type, i) = variant_name;
10263 TYPE_NAME (sub_type)
10264 = rust_fully_qualify (&objfile->objfile_obstack,
10265 TYPE_NAME (type), variant_name);
10266 }
10267 }
10268}
10269
10270/* Rewrite some Rust unions to be structures with variants parts. */
10271
10272static void
10273rust_union_quirks (struct dwarf2_cu *cu)
10274{
10275 gdb_assert (cu->language == language_rust);
52941706
SM
10276 for (type *type_ : cu->rust_unions)
10277 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10278 /* We don't need this any more. */
10279 cu->rust_unions.clear ();
c9317f21
TT
10280}
10281
95554aad
TT
10282/* Return the symtab for PER_CU. This works properly regardless of
10283 whether we're using the index or psymtabs. */
10284
43f3e411
DE
10285static struct compunit_symtab *
10286get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10287{
ed2dc618 10288 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10289 ? per_cu->v.quick->compunit_symtab
10290 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10291}
10292
10293/* A helper function for computing the list of all symbol tables
10294 included by PER_CU. */
10295
10296static void
4c39bc03 10297recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10298 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10299 struct dwarf2_per_cu_data *per_cu,
43f3e411 10300 struct compunit_symtab *immediate_parent)
95554aad
TT
10301{
10302 void **slot;
43f3e411 10303 struct compunit_symtab *cust;
95554aad
TT
10304
10305 slot = htab_find_slot (all_children, per_cu, INSERT);
10306 if (*slot != NULL)
10307 {
10308 /* This inclusion and its children have been processed. */
10309 return;
10310 }
10311
10312 *slot = per_cu;
10313 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10314 cust = get_compunit_symtab (per_cu);
10315 if (cust != NULL)
ec94af83
DE
10316 {
10317 /* If this is a type unit only add its symbol table if we haven't
10318 seen it yet (type unit per_cu's can share symtabs). */
10319 if (per_cu->is_debug_types)
10320 {
43f3e411 10321 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10322 if (*slot == NULL)
10323 {
43f3e411 10324 *slot = cust;
4c39bc03 10325 result->push_back (cust);
43f3e411
DE
10326 if (cust->user == NULL)
10327 cust->user = immediate_parent;
ec94af83
DE
10328 }
10329 }
10330 else
f9125b6c 10331 {
4c39bc03 10332 result->push_back (cust);
43f3e411
DE
10333 if (cust->user == NULL)
10334 cust->user = immediate_parent;
f9125b6c 10335 }
ec94af83 10336 }
95554aad 10337
ae640021
AB
10338 if (!per_cu->imported_symtabs_empty ())
10339 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10340 {
10341 recursively_compute_inclusions (result, all_children,
10342 all_type_symtabs, ptr, cust);
10343 }
95554aad
TT
10344}
10345
43f3e411 10346/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10347 PER_CU. */
10348
10349static void
43f3e411 10350compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10351{
f4dc4d17
DE
10352 gdb_assert (! per_cu->is_debug_types);
10353
ae640021 10354 if (!per_cu->imported_symtabs_empty ())
95554aad 10355 {
ae640021 10356 int len;
4c39bc03 10357 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10358 htab_t all_children, all_type_symtabs;
43f3e411 10359 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10360
10361 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10362 if (cust == NULL)
95554aad
TT
10363 return;
10364
10365 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10366 NULL, xcalloc, xfree);
ec94af83
DE
10367 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10368 NULL, xcalloc, xfree);
95554aad 10369
ae640021 10370 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10371 {
10372 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10373 all_type_symtabs, ptr, cust);
ec94af83 10374 }
95554aad 10375
ec94af83 10376 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10377 len = result_symtabs.size ();
43f3e411 10378 cust->includes
ed2dc618 10379 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10380 struct compunit_symtab *, len + 1);
4c39bc03
TT
10381 memcpy (cust->includes, result_symtabs.data (),
10382 len * sizeof (compunit_symtab *));
43f3e411 10383 cust->includes[len] = NULL;
95554aad 10384
95554aad 10385 htab_delete (all_children);
ec94af83 10386 htab_delete (all_type_symtabs);
95554aad
TT
10387 }
10388}
10389
10390/* Compute the 'includes' field for the symtabs of all the CUs we just
10391 read. */
10392
10393static void
ed2dc618 10394process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10395{
71b73764 10396 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10397 {
10398 if (! iter->is_debug_types)
43f3e411 10399 compute_compunit_symtab_includes (iter);
f4dc4d17 10400 }
95554aad 10401
c5d0225d 10402 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10403}
10404
9cdd5dbd 10405/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10406 already been loaded into memory. */
10407
10408static void
95554aad
TT
10409process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10410 enum language pretend_language)
10b3939b 10411{
10b3939b 10412 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10413 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10414 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10415 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10416 CORE_ADDR lowpc, highpc;
43f3e411 10417 struct compunit_symtab *cust;
10b3939b 10418 CORE_ADDR baseaddr;
4359dff1 10419 struct block *static_block;
3e29f34a 10420 CORE_ADDR addr;
10b3939b
DJ
10421
10422 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10423
c89b44cd
TT
10424 /* Clear the list here in case something was left over. */
10425 cu->method_list.clear ();
10b3939b 10426
95554aad
TT
10427 cu->language = pretend_language;
10428 cu->language_defn = language_def (cu->language);
10429
c906108c 10430 /* Do line number decoding in read_file_scope () */
10b3939b 10431 process_die (cu->dies, cu);
c906108c 10432
a766d390
DE
10433 /* For now fudge the Go package. */
10434 if (cu->language == language_go)
10435 fixup_go_packaging (cu);
10436
5f48f8f3 10437 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10438 should be complete, and it should now be safe to compute all of the
10439 physnames. */
10440 compute_delayed_physnames (cu);
3da10d80 10441
c9317f21
TT
10442 if (cu->language == language_rust)
10443 rust_union_quirks (cu);
10444
fae299cd
DC
10445 /* Some compilers don't define a DW_AT_high_pc attribute for the
10446 compilation unit. If the DW_AT_high_pc is missing, synthesize
10447 it, by scanning the DIE's below the compilation unit. */
10b3939b 10448 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10449
3e29f34a 10450 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10451 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10452
10453 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10454 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10455 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10456 addrmap to help ensure it has an accurate map of pc values belonging to
10457 this comp unit. */
10458 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10459
c24bdb02 10460 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10461 SECT_OFF_TEXT (objfile),
10462 0);
c906108c 10463
43f3e411 10464 if (cust != NULL)
c906108c 10465 {
df15bd07 10466 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10467
8be455d7
JK
10468 /* Set symtab language to language from DW_AT_language. If the
10469 compilation is from a C file generated by language preprocessors, do
10470 not set the language if it was already deduced by start_subfile. */
43f3e411 10471 if (!(cu->language == language_c
40e3ad0e 10472 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10473 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10474
10475 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10476 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10477 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10478 there were bugs in prologue debug info, fixed later in GCC-4.5
10479 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10480
10481 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10482 needed, it would be wrong due to missing DW_AT_producer there.
10483
10484 Still one can confuse GDB by using non-standard GCC compilation
10485 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10486 */
ab260dad 10487 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10488 cust->locations_valid = 1;
e0d00bc7
JK
10489
10490 if (gcc_4_minor >= 5)
43f3e411 10491 cust->epilogue_unwind_valid = 1;
96408a79 10492
43f3e411 10493 cust->call_site_htab = cu->call_site_htab;
c906108c 10494 }
9291a0cd
TT
10495
10496 if (dwarf2_per_objfile->using_index)
43f3e411 10497 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10498 else
10499 {
10500 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10501 pst->compunit_symtab = cust;
9291a0cd
TT
10502 pst->readin = 1;
10503 }
c906108c 10504
95554aad 10505 /* Push it for inclusion processing later. */
c5d0225d 10506 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10507
10508 /* Not needed any more. */
c24bdb02 10509 cu->reset_builder ();
f4dc4d17 10510}
45cfd468 10511
f4dc4d17
DE
10512/* Generate full symbol information for type unit PER_CU, whose DIEs have
10513 already been loaded into memory. */
10514
10515static void
10516process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10517 enum language pretend_language)
10518{
10519 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10520 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10521 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10522 struct compunit_symtab *cust;
0186c6a7
DE
10523 struct signatured_type *sig_type;
10524
10525 gdb_assert (per_cu->is_debug_types);
10526 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10527
c89b44cd
TT
10528 /* Clear the list here in case something was left over. */
10529 cu->method_list.clear ();
f4dc4d17 10530
f4dc4d17
DE
10531 cu->language = pretend_language;
10532 cu->language_defn = language_def (cu->language);
10533
10534 /* The symbol tables are set up in read_type_unit_scope. */
10535 process_die (cu->dies, cu);
10536
10537 /* For now fudge the Go package. */
10538 if (cu->language == language_go)
10539 fixup_go_packaging (cu);
10540
5f48f8f3 10541 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10542 should be complete, and it should now be safe to compute all of the
10543 physnames. */
10544 compute_delayed_physnames (cu);
f4dc4d17 10545
c9317f21
TT
10546 if (cu->language == language_rust)
10547 rust_union_quirks (cu);
10548
f4dc4d17
DE
10549 /* TUs share symbol tables.
10550 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10551 of it with end_expandable_symtab. Otherwise, complete the addition of
10552 this TU's symbols to the existing symtab. */
43f3e411 10553 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10554 {
c24bdb02
KS
10555 buildsym_compunit *builder = cu->get_builder ();
10556 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10557 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10558
43f3e411 10559 if (cust != NULL)
f4dc4d17
DE
10560 {
10561 /* Set symtab language to language from DW_AT_language. If the
10562 compilation is from a C file generated by language preprocessors,
10563 do not set the language if it was already deduced by
10564 start_subfile. */
43f3e411
DE
10565 if (!(cu->language == language_c
10566 && COMPUNIT_FILETABS (cust)->language != language_c))
10567 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10568 }
10569 }
10570 else
10571 {
c24bdb02 10572 cu->get_builder ()->augment_type_symtab ();
43f3e411 10573 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10574 }
10575
10576 if (dwarf2_per_objfile->using_index)
43f3e411 10577 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10578 else
10579 {
10580 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10581 pst->compunit_symtab = cust;
f4dc4d17 10582 pst->readin = 1;
45cfd468 10583 }
804d2729
TT
10584
10585 /* Not needed any more. */
c24bdb02 10586 cu->reset_builder ();
c906108c
SS
10587}
10588
95554aad
TT
10589/* Process an imported unit DIE. */
10590
10591static void
10592process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10593{
10594 struct attribute *attr;
10595
f4dc4d17
DE
10596 /* For now we don't handle imported units in type units. */
10597 if (cu->per_cu->is_debug_types)
10598 {
10599 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10600 " supported in type units [in module %s]"),
518817b3 10601 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10602 }
10603
95554aad
TT
10604 attr = dwarf2_attr (die, DW_AT_import, cu);
10605 if (attr != NULL)
10606 {
9c541725
PA
10607 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10608 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10609 dwarf2_per_cu_data *per_cu
e3b94546 10610 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10611 cu->per_cu->dwarf2_per_objfile);
95554aad 10612
69d751e3 10613 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10614 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10615 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10616
ae640021 10617 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10618 }
10619}
10620
4c8aa72d
PA
10621/* RAII object that represents a process_die scope: i.e.,
10622 starts/finishes processing a DIE. */
10623class process_die_scope
adde2bff 10624{
4c8aa72d
PA
10625public:
10626 process_die_scope (die_info *die, dwarf2_cu *cu)
10627 : m_die (die), m_cu (cu)
10628 {
10629 /* We should only be processing DIEs not already in process. */
10630 gdb_assert (!m_die->in_process);
10631 m_die->in_process = true;
10632 }
8c3cb9fa 10633
4c8aa72d
PA
10634 ~process_die_scope ()
10635 {
10636 m_die->in_process = false;
10637
10638 /* If we're done processing the DIE for the CU that owns the line
10639 header, we don't need the line header anymore. */
10640 if (m_cu->line_header_die_owner == m_die)
10641 {
10642 delete m_cu->line_header;
10643 m_cu->line_header = NULL;
10644 m_cu->line_header_die_owner = NULL;
10645 }
10646 }
10647
10648private:
10649 die_info *m_die;
10650 dwarf2_cu *m_cu;
10651};
adde2bff 10652
c906108c
SS
10653/* Process a die and its children. */
10654
10655static void
e7c27a73 10656process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10657{
4c8aa72d 10658 process_die_scope scope (die, cu);
adde2bff 10659
c906108c
SS
10660 switch (die->tag)
10661 {
10662 case DW_TAG_padding:
10663 break;
10664 case DW_TAG_compile_unit:
95554aad 10665 case DW_TAG_partial_unit:
e7c27a73 10666 read_file_scope (die, cu);
c906108c 10667 break;
348e048f
DE
10668 case DW_TAG_type_unit:
10669 read_type_unit_scope (die, cu);
10670 break;
c906108c 10671 case DW_TAG_subprogram:
0a4b0913
AB
10672 /* Nested subprograms in Fortran get a prefix. */
10673 if (cu->language == language_fortran
10674 && die->parent != NULL
10675 && die->parent->tag == DW_TAG_subprogram)
10676 cu->processing_has_namespace_info = true;
10677 /* Fall through. */
c906108c 10678 case DW_TAG_inlined_subroutine:
edb3359d 10679 read_func_scope (die, cu);
c906108c
SS
10680 break;
10681 case DW_TAG_lexical_block:
14898363
L
10682 case DW_TAG_try_block:
10683 case DW_TAG_catch_block:
e7c27a73 10684 read_lexical_block_scope (die, cu);
c906108c 10685 break;
216f72a1 10686 case DW_TAG_call_site:
96408a79
SA
10687 case DW_TAG_GNU_call_site:
10688 read_call_site_scope (die, cu);
10689 break;
c906108c 10690 case DW_TAG_class_type:
680b30c7 10691 case DW_TAG_interface_type:
c906108c
SS
10692 case DW_TAG_structure_type:
10693 case DW_TAG_union_type:
134d01f1 10694 process_structure_scope (die, cu);
c906108c
SS
10695 break;
10696 case DW_TAG_enumeration_type:
134d01f1 10697 process_enumeration_scope (die, cu);
c906108c 10698 break;
134d01f1 10699
f792889a
DJ
10700 /* These dies have a type, but processing them does not create
10701 a symbol or recurse to process the children. Therefore we can
10702 read them on-demand through read_type_die. */
c906108c 10703 case DW_TAG_subroutine_type:
72019c9c 10704 case DW_TAG_set_type:
c906108c 10705 case DW_TAG_array_type:
c906108c 10706 case DW_TAG_pointer_type:
c906108c 10707 case DW_TAG_ptr_to_member_type:
c906108c 10708 case DW_TAG_reference_type:
4297a3f0 10709 case DW_TAG_rvalue_reference_type:
c906108c 10710 case DW_TAG_string_type:
c906108c 10711 break;
134d01f1 10712
c906108c 10713 case DW_TAG_base_type:
a02abb62 10714 case DW_TAG_subrange_type:
cb249c71 10715 case DW_TAG_typedef:
134d01f1
DJ
10716 /* Add a typedef symbol for the type definition, if it has a
10717 DW_AT_name. */
f792889a 10718 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10719 break;
c906108c 10720 case DW_TAG_common_block:
e7c27a73 10721 read_common_block (die, cu);
c906108c
SS
10722 break;
10723 case DW_TAG_common_inclusion:
10724 break;
d9fa45fe 10725 case DW_TAG_namespace:
9068261f 10726 cu->processing_has_namespace_info = true;
e7c27a73 10727 read_namespace (die, cu);
d9fa45fe 10728 break;
5d7cb8df 10729 case DW_TAG_module:
9068261f 10730 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10731 read_module (die, cu);
10732 break;
d9fa45fe 10733 case DW_TAG_imported_declaration:
9068261f 10734 cu->processing_has_namespace_info = true;
74921315
KS
10735 if (read_namespace_alias (die, cu))
10736 break;
86a73007
TT
10737 /* The declaration is not a global namespace alias. */
10738 /* Fall through. */
d9fa45fe 10739 case DW_TAG_imported_module:
9068261f 10740 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10741 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10742 || cu->language != language_fortran))
b98664d3 10743 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10744 dwarf_tag_name (die->tag));
10745 read_import_statement (die, cu);
d9fa45fe 10746 break;
95554aad
TT
10747
10748 case DW_TAG_imported_unit:
10749 process_imported_unit_die (die, cu);
10750 break;
10751
71a3c369
TT
10752 case DW_TAG_variable:
10753 read_variable (die, cu);
10754 break;
10755
c906108c 10756 default:
e7c27a73 10757 new_symbol (die, NULL, cu);
c906108c
SS
10758 break;
10759 }
10760}
ca69b9e6
DE
10761\f
10762/* DWARF name computation. */
c906108c 10763
94af9270
KS
10764/* A helper function for dwarf2_compute_name which determines whether DIE
10765 needs to have the name of the scope prepended to the name listed in the
10766 die. */
10767
10768static int
10769die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10770{
1c809c68
TT
10771 struct attribute *attr;
10772
94af9270
KS
10773 switch (die->tag)
10774 {
10775 case DW_TAG_namespace:
10776 case DW_TAG_typedef:
10777 case DW_TAG_class_type:
10778 case DW_TAG_interface_type:
10779 case DW_TAG_structure_type:
10780 case DW_TAG_union_type:
10781 case DW_TAG_enumeration_type:
10782 case DW_TAG_enumerator:
10783 case DW_TAG_subprogram:
08a76f8a 10784 case DW_TAG_inlined_subroutine:
94af9270 10785 case DW_TAG_member:
74921315 10786 case DW_TAG_imported_declaration:
94af9270
KS
10787 return 1;
10788
10789 case DW_TAG_variable:
c2b0a229 10790 case DW_TAG_constant:
94af9270
KS
10791 /* We only need to prefix "globally" visible variables. These include
10792 any variable marked with DW_AT_external or any variable that
10793 lives in a namespace. [Variables in anonymous namespaces
10794 require prefixing, but they are not DW_AT_external.] */
10795
10796 if (dwarf2_attr (die, DW_AT_specification, cu))
10797 {
10798 struct dwarf2_cu *spec_cu = cu;
9a619af0 10799
94af9270
KS
10800 return die_needs_namespace (die_specification (die, &spec_cu),
10801 spec_cu);
10802 }
10803
1c809c68 10804 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10805 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10806 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10807 return 0;
10808 /* A variable in a lexical block of some kind does not need a
10809 namespace, even though in C++ such variables may be external
10810 and have a mangled name. */
10811 if (die->parent->tag == DW_TAG_lexical_block
10812 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10813 || die->parent->tag == DW_TAG_catch_block
10814 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10815 return 0;
10816 return 1;
94af9270
KS
10817
10818 default:
10819 return 0;
10820 }
10821}
10822
73b9be8b
KS
10823/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10824 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10825 defined for the given DIE. */
10826
10827static struct attribute *
10828dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10829{
10830 struct attribute *attr;
10831
10832 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10833 if (attr == NULL)
10834 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10835
10836 return attr;
10837}
10838
10839/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10840 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10841 defined for the given DIE. */
10842
10843static const char *
10844dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10845{
10846 const char *linkage_name;
10847
10848 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10849 if (linkage_name == NULL)
10850 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10851
10852 return linkage_name;
10853}
10854
94af9270 10855/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10856 compute the physname for the object, which include a method's:
9c37b5ae 10857 - formal parameters (C++),
a766d390 10858 - receiver type (Go),
a766d390
DE
10859
10860 The term "physname" is a bit confusing.
10861 For C++, for example, it is the demangled name.
10862 For Go, for example, it's the mangled name.
94af9270 10863
af6b7be1
JB
10864 For Ada, return the DIE's linkage name rather than the fully qualified
10865 name. PHYSNAME is ignored..
10866
94af9270
KS
10867 The result is allocated on the objfile_obstack and canonicalized. */
10868
10869static const char *
15d034d0
TT
10870dwarf2_compute_name (const char *name,
10871 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10872 int physname)
10873{
518817b3 10874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10875
94af9270
KS
10876 if (name == NULL)
10877 name = dwarf2_name (die, cu);
10878
2ee7123e
DE
10879 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10880 but otherwise compute it by typename_concat inside GDB.
10881 FIXME: Actually this is not really true, or at least not always true.
10882 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10883 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10884 will set the demangled name to the result of dwarf2_full_name, and it is
10885 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10886 if (cu->language == language_ada
10887 || (cu->language == language_fortran && physname))
10888 {
10889 /* For Ada unit, we prefer the linkage name over the name, as
10890 the former contains the exported name, which the user expects
10891 to be able to reference. Ideally, we want the user to be able
10892 to reference this entity using either natural or linkage name,
10893 but we haven't started looking at this enhancement yet. */
73b9be8b 10894 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10895
2ee7123e
DE
10896 if (linkage_name != NULL)
10897 return linkage_name;
f55ee35c
JK
10898 }
10899
94af9270
KS
10900 /* These are the only languages we know how to qualify names in. */
10901 if (name != NULL
9c37b5ae 10902 && (cu->language == language_cplus
c44af4eb
TT
10903 || cu->language == language_fortran || cu->language == language_d
10904 || cu->language == language_rust))
94af9270
KS
10905 {
10906 if (die_needs_namespace (die, cu))
10907 {
0d5cff50 10908 const char *prefix;
34a68019 10909 const char *canonical_name = NULL;
94af9270 10910
d7e74731
PA
10911 string_file buf;
10912
94af9270 10913 prefix = determine_prefix (die, cu);
94af9270
KS
10914 if (*prefix != '\0')
10915 {
f55ee35c
JK
10916 char *prefixed_name = typename_concat (NULL, prefix, name,
10917 physname, cu);
9a619af0 10918
d7e74731 10919 buf.puts (prefixed_name);
94af9270
KS
10920 xfree (prefixed_name);
10921 }
10922 else
d7e74731 10923 buf.puts (name);
94af9270 10924
98bfdba5
PA
10925 /* Template parameters may be specified in the DIE's DW_AT_name, or
10926 as children with DW_TAG_template_type_param or
10927 DW_TAG_value_type_param. If the latter, add them to the name
10928 here. If the name already has template parameters, then
10929 skip this step; some versions of GCC emit both, and
10930 it is more efficient to use the pre-computed name.
10931
10932 Something to keep in mind about this process: it is very
10933 unlikely, or in some cases downright impossible, to produce
10934 something that will match the mangled name of a function.
10935 If the definition of the function has the same debug info,
10936 we should be able to match up with it anyway. But fallbacks
10937 using the minimal symbol, for instance to find a method
10938 implemented in a stripped copy of libstdc++, will not work.
10939 If we do not have debug info for the definition, we will have to
10940 match them up some other way.
10941
10942 When we do name matching there is a related problem with function
10943 templates; two instantiated function templates are allowed to
10944 differ only by their return types, which we do not add here. */
10945
10946 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10947 {
10948 struct attribute *attr;
10949 struct die_info *child;
10950 int first = 1;
10951
10952 die->building_fullname = 1;
10953
10954 for (child = die->child; child != NULL; child = child->sibling)
10955 {
10956 struct type *type;
12df843f 10957 LONGEST value;
d521ce57 10958 const gdb_byte *bytes;
98bfdba5
PA
10959 struct dwarf2_locexpr_baton *baton;
10960 struct value *v;
10961
10962 if (child->tag != DW_TAG_template_type_param
10963 && child->tag != DW_TAG_template_value_param)
10964 continue;
10965
10966 if (first)
10967 {
d7e74731 10968 buf.puts ("<");
98bfdba5
PA
10969 first = 0;
10970 }
10971 else
d7e74731 10972 buf.puts (", ");
98bfdba5
PA
10973
10974 attr = dwarf2_attr (child, DW_AT_type, cu);
10975 if (attr == NULL)
10976 {
b98664d3 10977 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10978 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10979 continue;
10980 }
10981 type = die_type (child, cu);
10982
10983 if (child->tag == DW_TAG_template_type_param)
10984 {
c1ec8cea
TT
10985 c_print_type (type, "", &buf, -1, 0, cu->language,
10986 &type_print_raw_options);
98bfdba5
PA
10987 continue;
10988 }
10989
10990 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10991 if (attr == NULL)
10992 {
b98664d3 10993 complaint (_("template parameter missing "
3e43a32a 10994 "DW_AT_const_value"));
d7e74731 10995 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10996 continue;
10997 }
10998
10999 dwarf2_const_value_attr (attr, type, name,
11000 &cu->comp_unit_obstack, cu,
11001 &value, &bytes, &baton);
11002
11003 if (TYPE_NOSIGN (type))
11004 /* GDB prints characters as NUMBER 'CHAR'. If that's
11005 changed, this can use value_print instead. */
d7e74731 11006 c_printchar (value, type, &buf);
98bfdba5
PA
11007 else
11008 {
11009 struct value_print_options opts;
11010
11011 if (baton != NULL)
11012 v = dwarf2_evaluate_loc_desc (type, NULL,
11013 baton->data,
11014 baton->size,
11015 baton->per_cu);
11016 else if (bytes != NULL)
11017 {
11018 v = allocate_value (type);
11019 memcpy (value_contents_writeable (v), bytes,
11020 TYPE_LENGTH (type));
11021 }
11022 else
11023 v = value_from_longest (type, value);
11024
3e43a32a
MS
11025 /* Specify decimal so that we do not depend on
11026 the radix. */
98bfdba5
PA
11027 get_formatted_print_options (&opts, 'd');
11028 opts.raw = 1;
d7e74731 11029 value_print (v, &buf, &opts);
98bfdba5 11030 release_value (v);
98bfdba5
PA
11031 }
11032 }
11033
11034 die->building_fullname = 0;
11035
11036 if (!first)
11037 {
11038 /* Close the argument list, with a space if necessary
11039 (nested templates). */
d7e74731
PA
11040 if (!buf.empty () && buf.string ().back () == '>')
11041 buf.puts (" >");
98bfdba5 11042 else
d7e74731 11043 buf.puts (">");
98bfdba5
PA
11044 }
11045 }
11046
9c37b5ae 11047 /* For C++ methods, append formal parameter type
94af9270 11048 information, if PHYSNAME. */
6e70227d 11049
94af9270 11050 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11051 && cu->language == language_cplus)
94af9270
KS
11052 {
11053 struct type *type = read_type_die (die, cu);
11054
d7e74731 11055 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11056 &type_print_raw_options);
94af9270 11057
9c37b5ae 11058 if (cu->language == language_cplus)
94af9270 11059 {
60430eff
DJ
11060 /* Assume that an artificial first parameter is
11061 "this", but do not crash if it is not. RealView
11062 marks unnamed (and thus unused) parameters as
11063 artificial; there is no way to differentiate
11064 the two cases. */
94af9270
KS
11065 if (TYPE_NFIELDS (type) > 0
11066 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11067 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11068 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11069 0))))
d7e74731 11070 buf.puts (" const");
94af9270
KS
11071 }
11072 }
11073
d7e74731 11074 const std::string &intermediate_name = buf.string ();
94af9270
KS
11075
11076 if (cu->language == language_cplus)
34a68019 11077 canonical_name
322a8516 11078 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11079 &objfile->per_bfd->storage_obstack);
11080
11081 /* If we only computed INTERMEDIATE_NAME, or if
11082 INTERMEDIATE_NAME is already canonical, then we need to
11083 copy it to the appropriate obstack. */
322a8516 11084 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11085 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11086 intermediate_name);
34a68019
TT
11087 else
11088 name = canonical_name;
94af9270
KS
11089 }
11090 }
11091
11092 return name;
11093}
11094
0114d602
DJ
11095/* Return the fully qualified name of DIE, based on its DW_AT_name.
11096 If scope qualifiers are appropriate they will be added. The result
34a68019 11097 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11098 not have a name. NAME may either be from a previous call to
11099 dwarf2_name or NULL.
11100
9c37b5ae 11101 The output string will be canonicalized (if C++). */
0114d602
DJ
11102
11103static const char *
15d034d0 11104dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11105{
94af9270
KS
11106 return dwarf2_compute_name (name, die, cu, 0);
11107}
0114d602 11108
94af9270
KS
11109/* Construct a physname for the given DIE in CU. NAME may either be
11110 from a previous call to dwarf2_name or NULL. The result will be
11111 allocated on the objfile_objstack or NULL if the DIE does not have a
11112 name.
0114d602 11113
9c37b5ae 11114 The output string will be canonicalized (if C++). */
0114d602 11115
94af9270 11116static const char *
15d034d0 11117dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11118{
518817b3 11119 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11120 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11121 int need_copy = 1;
11122
11123 /* In this case dwarf2_compute_name is just a shortcut not building anything
11124 on its own. */
11125 if (!die_needs_namespace (die, cu))
11126 return dwarf2_compute_name (name, die, cu, 1);
11127
73b9be8b 11128 mangled = dw2_linkage_name (die, cu);
900e11f9 11129
e98c9e7c
TT
11130 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11131 See https://github.com/rust-lang/rust/issues/32925. */
11132 if (cu->language == language_rust && mangled != NULL
11133 && strchr (mangled, '{') != NULL)
11134 mangled = NULL;
11135
900e11f9
JK
11136 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11137 has computed. */
791afaa2 11138 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11139 if (mangled != NULL)
900e11f9 11140 {
900e11f9 11141
59cc4834
JB
11142 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11143 {
11144 /* Do nothing (do not demangle the symbol name). */
11145 }
11146 else if (cu->language == language_go)
a766d390 11147 {
5e2db402
TT
11148 /* This is a lie, but we already lie to the caller new_symbol.
11149 new_symbol assumes we return the mangled name.
a766d390 11150 This just undoes that lie until things are cleaned up. */
a766d390
DE
11151 }
11152 else
11153 {
0eb876f5
JB
11154 /* Use DMGL_RET_DROP for C++ template functions to suppress
11155 their return type. It is easier for GDB users to search
11156 for such functions as `name(params)' than `long name(params)'.
11157 In such case the minimal symbol names do not match the full
11158 symbol names but for template functions there is never a need
11159 to look up their definition from their declaration so
11160 the only disadvantage remains the minimal symbol variant
11161 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11162 demangled.reset (gdb_demangle (mangled,
11163 (DMGL_PARAMS | DMGL_ANSI
11164 | DMGL_RET_DROP)));
a766d390 11165 }
900e11f9 11166 if (demangled)
791afaa2 11167 canon = demangled.get ();
900e11f9
JK
11168 else
11169 {
11170 canon = mangled;
11171 need_copy = 0;
11172 }
11173 }
11174
11175 if (canon == NULL || check_physname)
11176 {
11177 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11178
11179 if (canon != NULL && strcmp (physname, canon) != 0)
11180 {
11181 /* It may not mean a bug in GDB. The compiler could also
11182 compute DW_AT_linkage_name incorrectly. But in such case
11183 GDB would need to be bug-to-bug compatible. */
11184
b98664d3 11185 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11186 "(from linkage <%s>) - DIE at %s [in module %s]"),
11187 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11188 objfile_name (objfile));
900e11f9
JK
11189
11190 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11191 is available here - over computed PHYSNAME. It is safer
11192 against both buggy GDB and buggy compilers. */
11193
11194 retval = canon;
11195 }
11196 else
11197 {
11198 retval = physname;
11199 need_copy = 0;
11200 }
11201 }
11202 else
11203 retval = canon;
11204
11205 if (need_copy)
021887d8 11206 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11207
900e11f9 11208 return retval;
0114d602
DJ
11209}
11210
74921315
KS
11211/* Inspect DIE in CU for a namespace alias. If one exists, record
11212 a new symbol for it.
11213
11214 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11215
11216static int
11217read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11218{
11219 struct attribute *attr;
11220
11221 /* If the die does not have a name, this is not a namespace
11222 alias. */
11223 attr = dwarf2_attr (die, DW_AT_name, cu);
11224 if (attr != NULL)
11225 {
11226 int num;
11227 struct die_info *d = die;
11228 struct dwarf2_cu *imported_cu = cu;
11229
11230 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11231 keep inspecting DIEs until we hit the underlying import. */
11232#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11233 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11234 {
11235 attr = dwarf2_attr (d, DW_AT_import, cu);
11236 if (attr == NULL)
11237 break;
11238
11239 d = follow_die_ref (d, attr, &imported_cu);
11240 if (d->tag != DW_TAG_imported_declaration)
11241 break;
11242 }
11243
11244 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11245 {
b98664d3 11246 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11247 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11248 return 0;
11249 }
11250
11251 if (attr != NULL)
11252 {
11253 struct type *type;
9c541725 11254 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11255
9c541725 11256 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11257 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11258 {
11259 /* This declaration is a global namespace alias. Add
11260 a symbol for it whose type is the aliased namespace. */
11261 new_symbol (die, type, cu);
11262 return 1;
11263 }
11264 }
11265 }
11266
11267 return 0;
11268}
11269
22cee43f 11270/* Return the using directives repository (global or local?) to use in the
804d2729 11271 current context for CU.
22cee43f
PMR
11272
11273 For Ada, imported declarations can materialize renamings, which *may* be
11274 global. However it is impossible (for now?) in DWARF to distinguish
11275 "external" imported declarations and "static" ones. As all imported
11276 declarations seem to be static in all other languages, make them all CU-wide
11277 global only in Ada. */
11278
11279static struct using_direct **
804d2729 11280using_directives (struct dwarf2_cu *cu)
22cee43f 11281{
c24bdb02
KS
11282 if (cu->language == language_ada
11283 && cu->get_builder ()->outermost_context_p ())
11284 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11285 else
c24bdb02 11286 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11287}
11288
27aa8d6a
SW
11289/* Read the import statement specified by the given die and record it. */
11290
11291static void
11292read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11293{
518817b3 11294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11295 struct attribute *import_attr;
32019081 11296 struct die_info *imported_die, *child_die;
de4affc9 11297 struct dwarf2_cu *imported_cu;
27aa8d6a 11298 const char *imported_name;
794684b6 11299 const char *imported_name_prefix;
13387711
SW
11300 const char *canonical_name;
11301 const char *import_alias;
11302 const char *imported_declaration = NULL;
794684b6 11303 const char *import_prefix;
eb1e02fd 11304 std::vector<const char *> excludes;
13387711 11305
27aa8d6a
SW
11306 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11307 if (import_attr == NULL)
11308 {
b98664d3 11309 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11310 dwarf_tag_name (die->tag));
11311 return;
11312 }
11313
de4affc9
CC
11314 imported_cu = cu;
11315 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11316 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11317 if (imported_name == NULL)
11318 {
11319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11320
11321 The import in the following code:
11322 namespace A
11323 {
11324 typedef int B;
11325 }
11326
11327 int main ()
11328 {
11329 using A::B;
11330 B b;
11331 return b;
11332 }
11333
11334 ...
11335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11336 <52> DW_AT_decl_file : 1
11337 <53> DW_AT_decl_line : 6
11338 <54> DW_AT_import : <0x75>
11339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11340 <59> DW_AT_name : B
11341 <5b> DW_AT_decl_file : 1
11342 <5c> DW_AT_decl_line : 2
11343 <5d> DW_AT_type : <0x6e>
11344 ...
11345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11346 <76> DW_AT_byte_size : 4
11347 <77> DW_AT_encoding : 5 (signed)
11348
11349 imports the wrong die ( 0x75 instead of 0x58 ).
11350 This case will be ignored until the gcc bug is fixed. */
11351 return;
11352 }
11353
82856980
SW
11354 /* Figure out the local name after import. */
11355 import_alias = dwarf2_name (die, cu);
27aa8d6a 11356
794684b6
SW
11357 /* Figure out where the statement is being imported to. */
11358 import_prefix = determine_prefix (die, cu);
11359
11360 /* Figure out what the scope of the imported die is and prepend it
11361 to the name of the imported die. */
de4affc9 11362 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11363
f55ee35c
JK
11364 if (imported_die->tag != DW_TAG_namespace
11365 && imported_die->tag != DW_TAG_module)
794684b6 11366 {
13387711
SW
11367 imported_declaration = imported_name;
11368 canonical_name = imported_name_prefix;
794684b6 11369 }
13387711 11370 else if (strlen (imported_name_prefix) > 0)
12aaed36 11371 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11372 imported_name_prefix,
11373 (cu->language == language_d ? "." : "::"),
11374 imported_name, (char *) NULL);
13387711
SW
11375 else
11376 canonical_name = imported_name;
794684b6 11377
32019081
JK
11378 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11379 for (child_die = die->child; child_die && child_die->tag;
11380 child_die = sibling_die (child_die))
11381 {
11382 /* DWARF-4: A Fortran use statement with a “rename list” may be
11383 represented by an imported module entry with an import attribute
11384 referring to the module and owned entries corresponding to those
11385 entities that are renamed as part of being imported. */
11386
11387 if (child_die->tag != DW_TAG_imported_declaration)
11388 {
b98664d3 11389 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11390 "- DIE at %s [in module %s]"),
11391 sect_offset_str (child_die->sect_off),
11392 objfile_name (objfile));
32019081
JK
11393 continue;
11394 }
11395
11396 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11397 if (import_attr == NULL)
11398 {
b98664d3 11399 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11400 dwarf_tag_name (child_die->tag));
11401 continue;
11402 }
11403
11404 imported_cu = cu;
11405 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11406 &imported_cu);
11407 imported_name = dwarf2_name (imported_die, imported_cu);
11408 if (imported_name == NULL)
11409 {
b98664d3 11410 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11411 "imported name - DIE at %s [in module %s]"),
11412 sect_offset_str (child_die->sect_off),
11413 objfile_name (objfile));
32019081
JK
11414 continue;
11415 }
11416
eb1e02fd 11417 excludes.push_back (imported_name);
32019081
JK
11418
11419 process_die (child_die, cu);
11420 }
11421
804d2729 11422 add_using_directive (using_directives (cu),
22cee43f
PMR
11423 import_prefix,
11424 canonical_name,
11425 import_alias,
11426 imported_declaration,
11427 excludes,
11428 0,
11429 &objfile->objfile_obstack);
27aa8d6a
SW
11430}
11431
5230b05a
WT
11432/* ICC<14 does not output the required DW_AT_declaration on incomplete
11433 types, but gives them a size of zero. Starting with version 14,
11434 ICC is compatible with GCC. */
11435
9068261f 11436static bool
5230b05a
WT
11437producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11438{
11439 if (!cu->checked_producer)
11440 check_producer (cu);
11441
11442 return cu->producer_is_icc_lt_14;
11443}
11444
eb77c9df
AB
11445/* ICC generates a DW_AT_type for C void functions. This was observed on
11446 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11447 which says that void functions should not have a DW_AT_type. */
11448
11449static bool
11450producer_is_icc (struct dwarf2_cu *cu)
11451{
11452 if (!cu->checked_producer)
11453 check_producer (cu);
11454
11455 return cu->producer_is_icc;
11456}
11457
1b80a9fa
JK
11458/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11459 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11460 this, it was first present in GCC release 4.3.0. */
11461
9068261f 11462static bool
1b80a9fa
JK
11463producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11464{
11465 if (!cu->checked_producer)
11466 check_producer (cu);
11467
11468 return cu->producer_is_gcc_lt_4_3;
11469}
11470
d721ba37
PA
11471static file_and_directory
11472find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11473{
d721ba37
PA
11474 file_and_directory res;
11475
9291a0cd
TT
11476 /* Find the filename. Do not use dwarf2_name here, since the filename
11477 is not a source language identifier. */
d721ba37
PA
11478 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11479 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11480
d721ba37
PA
11481 if (res.comp_dir == NULL
11482 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11483 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11484 {
d721ba37
PA
11485 res.comp_dir_storage = ldirname (res.name);
11486 if (!res.comp_dir_storage.empty ())
11487 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11488 }
d721ba37 11489 if (res.comp_dir != NULL)
9291a0cd
TT
11490 {
11491 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11492 directory, get rid of it. */
d721ba37 11493 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11494
d721ba37
PA
11495 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11496 res.comp_dir = cp + 1;
9291a0cd
TT
11497 }
11498
d721ba37
PA
11499 if (res.name == NULL)
11500 res.name = "<unknown>";
11501
11502 return res;
9291a0cd
TT
11503}
11504
f4dc4d17
DE
11505/* Handle DW_AT_stmt_list for a compilation unit.
11506 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11507 COMP_DIR is the compilation directory. LOWPC is passed to
11508 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11509
11510static void
11511handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11512 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11513{
518817b3
SM
11514 struct dwarf2_per_objfile *dwarf2_per_objfile
11515 = cu->per_cu->dwarf2_per_objfile;
527f3840 11516 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11517 struct attribute *attr;
527f3840
JK
11518 struct line_header line_header_local;
11519 hashval_t line_header_local_hash;
527f3840
JK
11520 void **slot;
11521 int decode_mapping;
2ab95328 11522
f4dc4d17
DE
11523 gdb_assert (! cu->per_cu->is_debug_types);
11524
2ab95328 11525 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11526 if (attr == NULL)
11527 return;
11528
9c541725 11529 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11530
11531 /* The line header hash table is only created if needed (it exists to
11532 prevent redundant reading of the line table for partial_units).
11533 If we're given a partial_unit, we'll need it. If we're given a
11534 compile_unit, then use the line header hash table if it's already
11535 created, but don't create one just yet. */
11536
11537 if (dwarf2_per_objfile->line_header_hash == NULL
11538 && die->tag == DW_TAG_partial_unit)
2ab95328 11539 {
527f3840
JK
11540 dwarf2_per_objfile->line_header_hash
11541 = htab_create_alloc_ex (127, line_header_hash_voidp,
11542 line_header_eq_voidp,
11543 free_line_header_voidp,
11544 &objfile->objfile_obstack,
11545 hashtab_obstack_allocate,
11546 dummy_obstack_deallocate);
11547 }
2ab95328 11548
9c541725 11549 line_header_local.sect_off = line_offset;
527f3840
JK
11550 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11551 line_header_local_hash = line_header_hash (&line_header_local);
11552 if (dwarf2_per_objfile->line_header_hash != NULL)
11553 {
11554 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11555 &line_header_local,
11556 line_header_local_hash, NO_INSERT);
11557
11558 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11559 is not present in *SLOT (since if there is something in *SLOT then
11560 it will be for a partial_unit). */
11561 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11562 {
527f3840 11563 gdb_assert (*slot != NULL);
9a3c8263 11564 cu->line_header = (struct line_header *) *slot;
527f3840 11565 return;
dee91e82 11566 }
2ab95328 11567 }
527f3840
JK
11568
11569 /* dwarf_decode_line_header does not yet provide sufficient information.
11570 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11571 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11572 if (lh == NULL)
527f3840 11573 return;
4c8aa72d
PA
11574
11575 cu->line_header = lh.release ();
11576 cu->line_header_die_owner = die;
527f3840
JK
11577
11578 if (dwarf2_per_objfile->line_header_hash == NULL)
11579 slot = NULL;
11580 else
11581 {
11582 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11583 &line_header_local,
11584 line_header_local_hash, INSERT);
11585 gdb_assert (slot != NULL);
11586 }
11587 if (slot != NULL && *slot == NULL)
11588 {
11589 /* This newly decoded line number information unit will be owned
11590 by line_header_hash hash table. */
11591 *slot = cu->line_header;
4c8aa72d 11592 cu->line_header_die_owner = NULL;
527f3840
JK
11593 }
11594 else
11595 {
11596 /* We cannot free any current entry in (*slot) as that struct line_header
11597 may be already used by multiple CUs. Create only temporary decoded
11598 line_header for this CU - it may happen at most once for each line
11599 number information unit. And if we're not using line_header_hash
11600 then this is what we want as well. */
11601 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11602 }
11603 decode_mapping = (die->tag != DW_TAG_partial_unit);
11604 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11605 decode_mapping);
fff8551c 11606
2ab95328
TT
11607}
11608
95554aad 11609/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11610
c906108c 11611static void
e7c27a73 11612read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11613{
518817b3
SM
11614 struct dwarf2_per_objfile *dwarf2_per_objfile
11615 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11616 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11618 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11619 CORE_ADDR highpc = ((CORE_ADDR) 0);
11620 struct attribute *attr;
c906108c 11621 struct die_info *child_die;
e142c38c 11622 CORE_ADDR baseaddr;
6e70227d 11623
380618d6 11624 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11625 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11626
fae299cd 11627 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11628
11629 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11630 from finish_block. */
2acceee2 11631 if (lowpc == ((CORE_ADDR) -1))
c906108c 11632 lowpc = highpc;
3e29f34a 11633 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11634
d721ba37 11635 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11636
f4b8a18d
KW
11637 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11638 standardised yet. As a workaround for the language detection we fall
11639 back to the DW_AT_producer string. */
11640 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11641 cu->language = language_opencl;
11642
3019eac3
DE
11643 /* Similar hack for Go. */
11644 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11645 set_cu_language (DW_LANG_Go, cu);
11646
c24bdb02 11647 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11648
11649 /* Decode line number information if present. We do this before
11650 processing child DIEs, so that the line header table is available
11651 for DW_AT_decl_file. */
d721ba37 11652 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11653
11654 /* Process all dies in compilation unit. */
11655 if (die->child != NULL)
11656 {
11657 child_die = die->child;
11658 while (child_die && child_die->tag)
11659 {
11660 process_die (child_die, cu);
11661 child_die = sibling_die (child_die);
11662 }
11663 }
11664
11665 /* Decode macro information, if present. Dwarf 2 macro information
11666 refers to information in the line number info statement program
11667 header, so we can only read it if we've read the header
11668 successfully. */
0af92d60
JK
11669 attr = dwarf2_attr (die, DW_AT_macros, cu);
11670 if (attr == NULL)
11671 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11672 if (attr && cu->line_header)
11673 {
11674 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11675 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11676
43f3e411 11677 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11678 }
11679 else
11680 {
11681 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11682 if (attr && cu->line_header)
11683 {
11684 unsigned int macro_offset = DW_UNSND (attr);
11685
43f3e411 11686 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11687 }
11688 }
3019eac3
DE
11689}
11690
c24bdb02
KS
11691void
11692dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11693{
f4dc4d17
DE
11694 struct type_unit_group *tu_group;
11695 int first_time;
3019eac3 11696 struct attribute *attr;
9c541725 11697 unsigned int i;
0186c6a7 11698 struct signatured_type *sig_type;
3019eac3 11699
f4dc4d17 11700 gdb_assert (per_cu->is_debug_types);
0186c6a7 11701 sig_type = (struct signatured_type *) per_cu;
3019eac3 11702
c24bdb02 11703 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11704
f4dc4d17 11705 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11706 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11707 if (sig_type->type_unit_group == NULL)
c24bdb02 11708 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11709 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11710
11711 /* If we've already processed this stmt_list there's no real need to
11712 do it again, we could fake it and just recreate the part we need
11713 (file name,index -> symtab mapping). If data shows this optimization
11714 is useful we can do it then. */
43f3e411 11715 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11716
11717 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11718 debug info. */
fff8551c 11719 line_header_up lh;
f4dc4d17 11720 if (attr != NULL)
3019eac3 11721 {
9c541725 11722 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11723 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11724 }
11725 if (lh == NULL)
11726 {
11727 if (first_time)
c24bdb02 11728 start_symtab ("", NULL, 0);
f4dc4d17
DE
11729 else
11730 {
11731 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11732 gdb_assert (m_builder == nullptr);
804d2729 11733 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11734 m_builder.reset (new struct buildsym_compunit
11735 (COMPUNIT_OBJFILE (cust), "",
11736 COMPUNIT_DIRNAME (cust),
11737 compunit_language (cust),
11738 0, cust));
f4dc4d17 11739 }
f4dc4d17 11740 return;
3019eac3
DE
11741 }
11742
c24bdb02
KS
11743 line_header = lh.release ();
11744 line_header_die_owner = die;
3019eac3 11745
f4dc4d17
DE
11746 if (first_time)
11747 {
c24bdb02 11748 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11749
1fd60fc0
DE
11750 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11751 still initializing it, and our caller (a few levels up)
11752 process_full_type_unit still needs to know if this is the first
11753 time. */
11754
7ba99d21 11755 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11756 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11757 line_header->file_names_size ());
3019eac3 11758
7ba99d21
AT
11759 auto &file_names = line_header->file_names ();
11760 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11761 {
7ba99d21 11762 file_entry &fe = file_names[i];
c24bdb02
KS
11763 dwarf2_start_subfile (this, fe.name,
11764 fe.include_dir (line_header));
11765 buildsym_compunit *b = get_builder ();
11766 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11767 {
4c8aa72d
PA
11768 /* NOTE: start_subfile will recognize when it's been
11769 passed a file it has already seen. So we can't
11770 assume there's a simple mapping from
11771 cu->line_header->file_names to subfiles, plus
11772 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11773 b->get_current_subfile ()->symtab
11774 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11775 }
11776
c24bdb02 11777 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11778 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11779 }
11780 }
11781 else
3019eac3 11782 {
c24bdb02 11783 gdb_assert (m_builder == nullptr);
804d2729 11784 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11785 m_builder.reset (new struct buildsym_compunit
11786 (COMPUNIT_OBJFILE (cust), "",
11787 COMPUNIT_DIRNAME (cust),
11788 compunit_language (cust),
11789 0, cust));
f4dc4d17 11790
7ba99d21
AT
11791 auto &file_names = line_header->file_names ();
11792 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11793 {
7ba99d21 11794 file_entry &fe = file_names[i];
4c8aa72d 11795 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11796 }
3019eac3
DE
11797 }
11798
f4dc4d17
DE
11799 /* The main symtab is allocated last. Type units don't have DW_AT_name
11800 so they don't have a "real" (so to speak) symtab anyway.
11801 There is later code that will assign the main symtab to all symbols
11802 that don't have one. We need to handle the case of a symbol with a
11803 missing symtab (DW_AT_decl_file) anyway. */
11804}
3019eac3 11805
f4dc4d17
DE
11806/* Process DW_TAG_type_unit.
11807 For TUs we want to skip the first top level sibling if it's not the
11808 actual type being defined by this TU. In this case the first top
11809 level sibling is there to provide context only. */
3019eac3 11810
f4dc4d17
DE
11811static void
11812read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11813{
11814 struct die_info *child_die;
3019eac3 11815
f4dc4d17
DE
11816 prepare_one_comp_unit (cu, die, language_minimal);
11817
11818 /* Initialize (or reinitialize) the machinery for building symtabs.
11819 We do this before processing child DIEs, so that the line header table
11820 is available for DW_AT_decl_file. */
c24bdb02 11821 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11822
11823 if (die->child != NULL)
11824 {
11825 child_die = die->child;
11826 while (child_die && child_die->tag)
11827 {
11828 process_die (child_die, cu);
11829 child_die = sibling_die (child_die);
11830 }
11831 }
3019eac3
DE
11832}
11833\f
80626a55
DE
11834/* DWO/DWP files.
11835
11836 http://gcc.gnu.org/wiki/DebugFission
11837 http://gcc.gnu.org/wiki/DebugFissionDWP
11838
11839 To simplify handling of both DWO files ("object" files with the DWARF info)
11840 and DWP files (a file with the DWOs packaged up into one file), we treat
11841 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11842
11843static hashval_t
11844hash_dwo_file (const void *item)
11845{
9a3c8263 11846 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11847 hashval_t hash;
3019eac3 11848
a2ce51a0
DE
11849 hash = htab_hash_string (dwo_file->dwo_name);
11850 if (dwo_file->comp_dir != NULL)
11851 hash += htab_hash_string (dwo_file->comp_dir);
11852 return hash;
3019eac3
DE
11853}
11854
11855static int
11856eq_dwo_file (const void *item_lhs, const void *item_rhs)
11857{
9a3c8263
SM
11858 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11859 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11860
a2ce51a0
DE
11861 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11862 return 0;
11863 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11864 return lhs->comp_dir == rhs->comp_dir;
11865 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11866}
11867
11868/* Allocate a hash table for DWO files. */
11869
51ac9db5 11870static htab_up
ed2dc618 11871allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11872{
51ac9db5
SM
11873 auto delete_dwo_file = [] (void *item)
11874 {
11875 struct dwo_file *dwo_file = (struct dwo_file *) item;
11876
11877 delete dwo_file;
11878 };
11879
11880 return htab_up (htab_create_alloc_ex (41,
11881 hash_dwo_file,
11882 eq_dwo_file,
11883 delete_dwo_file,
11884 &objfile->objfile_obstack,
11885 hashtab_obstack_allocate,
11886 dummy_obstack_deallocate));
3019eac3
DE
11887}
11888
80626a55
DE
11889/* Lookup DWO file DWO_NAME. */
11890
11891static void **
ed2dc618
SM
11892lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11893 const char *dwo_name,
11894 const char *comp_dir)
80626a55
DE
11895{
11896 struct dwo_file find_entry;
11897 void **slot;
11898
11899 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11900 dwarf2_per_objfile->dwo_files
11901 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11902
0ac5b59e
DE
11903 find_entry.dwo_name = dwo_name;
11904 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11905 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11906 INSERT);
80626a55
DE
11907
11908 return slot;
11909}
11910
3019eac3
DE
11911static hashval_t
11912hash_dwo_unit (const void *item)
11913{
9a3c8263 11914 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11915
11916 /* This drops the top 32 bits of the id, but is ok for a hash. */
11917 return dwo_unit->signature;
11918}
11919
11920static int
11921eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11922{
9a3c8263
SM
11923 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11924 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11925
11926 /* The signature is assumed to be unique within the DWO file.
11927 So while object file CU dwo_id's always have the value zero,
11928 that's OK, assuming each object file DWO file has only one CU,
11929 and that's the rule for now. */
11930 return lhs->signature == rhs->signature;
11931}
11932
11933/* Allocate a hash table for DWO CUs,TUs.
11934 There is one of these tables for each of CUs,TUs for each DWO file. */
11935
11936static htab_t
11937allocate_dwo_unit_table (struct objfile *objfile)
11938{
11939 /* Start out with a pretty small number.
11940 Generally DWO files contain only one CU and maybe some TUs. */
11941 return htab_create_alloc_ex (3,
11942 hash_dwo_unit,
11943 eq_dwo_unit,
11944 NULL,
11945 &objfile->objfile_obstack,
11946 hashtab_obstack_allocate,
11947 dummy_obstack_deallocate);
11948}
11949
80626a55 11950/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11951
19c3d4c9 11952struct create_dwo_cu_data
3019eac3
DE
11953{
11954 struct dwo_file *dwo_file;
19c3d4c9 11955 struct dwo_unit dwo_unit;
3019eac3
DE
11956};
11957
19c3d4c9 11958/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11959
11960static void
19c3d4c9
DE
11961create_dwo_cu_reader (const struct die_reader_specs *reader,
11962 const gdb_byte *info_ptr,
11963 struct die_info *comp_unit_die,
11964 int has_children,
11965 void *datap)
3019eac3
DE
11966{
11967 struct dwarf2_cu *cu = reader->cu;
9c541725 11968 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11969 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11970 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11971 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11972 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11973
a084a2a6
AT
11974 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11975 if (!signature.has_value ())
3019eac3 11976 {
b98664d3 11977 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11978 " its dwo_id [in module %s]"),
9d8780f0 11979 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11980 return;
11981 }
11982
3019eac3 11983 dwo_unit->dwo_file = dwo_file;
a084a2a6 11984 dwo_unit->signature = *signature;
8a0459fd 11985 dwo_unit->section = section;
9c541725 11986 dwo_unit->sect_off = sect_off;
3019eac3
DE
11987 dwo_unit->length = cu->per_cu->length;
11988
b4f54984 11989 if (dwarf_read_debug)
9d8780f0
SM
11990 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11991 sect_offset_str (sect_off),
9c541725 11992 hex_string (dwo_unit->signature));
3019eac3
DE
11993}
11994
33c5cd75 11995/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11996 Note: This function processes DWO files only, not DWP files. */
3019eac3 11997
33c5cd75 11998static void
ed2dc618
SM
11999create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12000 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 12001 htab_t &cus_htab)
3019eac3
DE
12002{
12003 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 12004 const gdb_byte *info_ptr, *end_ptr;
3019eac3 12005
33c5cd75
DB
12006 dwarf2_read_section (objfile, &section);
12007 info_ptr = section.buffer;
3019eac3
DE
12008
12009 if (info_ptr == NULL)
33c5cd75 12010 return;
3019eac3 12011
b4f54984 12012 if (dwarf_read_debug)
19c3d4c9
DE
12013 {
12014 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
12015 get_section_name (&section),
12016 get_section_file_name (&section));
19c3d4c9 12017 }
3019eac3 12018
33c5cd75 12019 end_ptr = info_ptr + section.size;
3019eac3
DE
12020 while (info_ptr < end_ptr)
12021 {
12022 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12023 struct create_dwo_cu_data create_dwo_cu_data;
12024 struct dwo_unit *dwo_unit;
12025 void **slot;
12026 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12027
19c3d4c9
DE
12028 memset (&create_dwo_cu_data.dwo_unit, 0,
12029 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12030 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12031 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12032 per_cu.is_debug_types = 0;
33c5cd75
DB
12033 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12034 per_cu.section = &section;
c5ed0576 12035 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12036
12037 init_cutu_and_read_dies_no_follow (
12038 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12039 info_ptr += per_cu.length;
12040
12041 // If the unit could not be parsed, skip it.
12042 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12043 continue;
3019eac3 12044
33c5cd75
DB
12045 if (cus_htab == NULL)
12046 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12047
33c5cd75
DB
12048 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12049 *dwo_unit = create_dwo_cu_data.dwo_unit;
12050 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12051 gdb_assert (slot != NULL);
12052 if (*slot != NULL)
19c3d4c9 12053 {
33c5cd75
DB
12054 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12055 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12056
b98664d3 12057 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12058 " the entry at offset %s, signature %s"),
12059 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12060 hex_string (dwo_unit->signature));
19c3d4c9 12061 }
33c5cd75 12062 *slot = (void *)dwo_unit;
3019eac3 12063 }
3019eac3
DE
12064}
12065
80626a55
DE
12066/* DWP file .debug_{cu,tu}_index section format:
12067 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12068
d2415c6c
DE
12069 DWP Version 1:
12070
80626a55
DE
12071 Both index sections have the same format, and serve to map a 64-bit
12072 signature to a set of section numbers. Each section begins with a header,
12073 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12074 indexes, and a pool of 32-bit section numbers. The index sections will be
12075 aligned at 8-byte boundaries in the file.
12076
d2415c6c
DE
12077 The index section header consists of:
12078
12079 V, 32 bit version number
12080 -, 32 bits unused
12081 N, 32 bit number of compilation units or type units in the index
12082 M, 32 bit number of slots in the hash table
80626a55 12083
d2415c6c 12084 Numbers are recorded using the byte order of the application binary.
80626a55 12085
d2415c6c
DE
12086 The hash table begins at offset 16 in the section, and consists of an array
12087 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12088 order of the application binary). Unused slots in the hash table are 0.
12089 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12090
d2415c6c
DE
12091 The parallel table begins immediately after the hash table
12092 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12093 array of 32-bit indexes (using the byte order of the application binary),
12094 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12095 table contains a 32-bit index into the pool of section numbers. For unused
12096 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12097
73869dc2
DE
12098 The pool of section numbers begins immediately following the hash table
12099 (at offset 16 + 12 * M from the beginning of the section). The pool of
12100 section numbers consists of an array of 32-bit words (using the byte order
12101 of the application binary). Each item in the array is indexed starting
12102 from 0. The hash table entry provides the index of the first section
12103 number in the set. Additional section numbers in the set follow, and the
12104 set is terminated by a 0 entry (section number 0 is not used in ELF).
12105
12106 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12107 section must be the first entry in the set, and the .debug_abbrev.dwo must
12108 be the second entry. Other members of the set may follow in any order.
12109
12110 ---
12111
12112 DWP Version 2:
12113
12114 DWP Version 2 combines all the .debug_info, etc. sections into one,
12115 and the entries in the index tables are now offsets into these sections.
12116 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12117 section.
12118
12119 Index Section Contents:
12120 Header
12121 Hash Table of Signatures dwp_hash_table.hash_table
12122 Parallel Table of Indices dwp_hash_table.unit_table
12123 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12124 Table of Section Sizes dwp_hash_table.v2.sizes
12125
12126 The index section header consists of:
12127
12128 V, 32 bit version number
12129 L, 32 bit number of columns in the table of section offsets
12130 N, 32 bit number of compilation units or type units in the index
12131 M, 32 bit number of slots in the hash table
12132
12133 Numbers are recorded using the byte order of the application binary.
12134
12135 The hash table has the same format as version 1.
12136 The parallel table of indices has the same format as version 1,
12137 except that the entries are origin-1 indices into the table of sections
12138 offsets and the table of section sizes.
12139
12140 The table of offsets begins immediately following the parallel table
12141 (at offset 16 + 12 * M from the beginning of the section). The table is
12142 a two-dimensional array of 32-bit words (using the byte order of the
12143 application binary), with L columns and N+1 rows, in row-major order.
12144 Each row in the array is indexed starting from 0. The first row provides
12145 a key to the remaining rows: each column in this row provides an identifier
12146 for a debug section, and the offsets in the same column of subsequent rows
12147 refer to that section. The section identifiers are:
12148
12149 DW_SECT_INFO 1 .debug_info.dwo
12150 DW_SECT_TYPES 2 .debug_types.dwo
12151 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12152 DW_SECT_LINE 4 .debug_line.dwo
12153 DW_SECT_LOC 5 .debug_loc.dwo
12154 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12155 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12156 DW_SECT_MACRO 8 .debug_macro.dwo
12157
12158 The offsets provided by the CU and TU index sections are the base offsets
12159 for the contributions made by each CU or TU to the corresponding section
12160 in the package file. Each CU and TU header contains an abbrev_offset
12161 field, used to find the abbreviations table for that CU or TU within the
12162 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12163 be interpreted as relative to the base offset given in the index section.
12164 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12165 should be interpreted as relative to the base offset for .debug_line.dwo,
12166 and offsets into other debug sections obtained from DWARF attributes should
12167 also be interpreted as relative to the corresponding base offset.
12168
12169 The table of sizes begins immediately following the table of offsets.
12170 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12171 with L columns and N rows, in row-major order. Each row in the array is
12172 indexed starting from 1 (row 0 is shared by the two tables).
12173
12174 ---
12175
12176 Hash table lookup is handled the same in version 1 and 2:
12177
12178 We assume that N and M will not exceed 2^32 - 1.
12179 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12180
d2415c6c
DE
12181 Given a 64-bit compilation unit signature or a type signature S, an entry
12182 in the hash table is located as follows:
80626a55 12183
d2415c6c
DE
12184 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12185 the low-order k bits all set to 1.
80626a55 12186
d2415c6c 12187 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12188
d2415c6c
DE
12189 3) If the hash table entry at index H matches the signature, use that
12190 entry. If the hash table entry at index H is unused (all zeroes),
12191 terminate the search: the signature is not present in the table.
80626a55 12192
d2415c6c 12193 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12194
d2415c6c 12195 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12196 to stop at an unused slot or find the match. */
80626a55
DE
12197
12198/* Create a hash table to map DWO IDs to their CU/TU entry in
12199 .debug_{info,types}.dwo in DWP_FILE.
12200 Returns NULL if there isn't one.
12201 Note: This function processes DWP files only, not DWO files. */
12202
12203static struct dwp_hash_table *
ed2dc618
SM
12204create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12205 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12206{
12207 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12208 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12209 const gdb_byte *index_ptr, *index_end;
80626a55 12210 struct dwarf2_section_info *index;
73869dc2 12211 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12212 struct dwp_hash_table *htab;
12213
12214 if (is_debug_types)
12215 index = &dwp_file->sections.tu_index;
12216 else
12217 index = &dwp_file->sections.cu_index;
12218
12219 if (dwarf2_section_empty_p (index))
12220 return NULL;
12221 dwarf2_read_section (objfile, index);
12222
12223 index_ptr = index->buffer;
12224 index_end = index_ptr + index->size;
12225
12226 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12227 index_ptr += 4;
12228 if (version == 2)
12229 nr_columns = read_4_bytes (dbfd, index_ptr);
12230 else
12231 nr_columns = 0;
12232 index_ptr += 4;
80626a55
DE
12233 nr_units = read_4_bytes (dbfd, index_ptr);
12234 index_ptr += 4;
12235 nr_slots = read_4_bytes (dbfd, index_ptr);
12236 index_ptr += 4;
12237
73869dc2 12238 if (version != 1 && version != 2)
80626a55 12239 {
21aa081e 12240 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12241 " [in module %s]"),
21aa081e 12242 pulongest (version), dwp_file->name);
80626a55
DE
12243 }
12244 if (nr_slots != (nr_slots & -nr_slots))
12245 {
21aa081e 12246 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12247 " is not power of 2 [in module %s]"),
21aa081e 12248 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12249 }
12250
12251 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12252 htab->version = version;
12253 htab->nr_columns = nr_columns;
80626a55
DE
12254 htab->nr_units = nr_units;
12255 htab->nr_slots = nr_slots;
12256 htab->hash_table = index_ptr;
12257 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12258
12259 /* Exit early if the table is empty. */
12260 if (nr_slots == 0 || nr_units == 0
12261 || (version == 2 && nr_columns == 0))
12262 {
12263 /* All must be zero. */
12264 if (nr_slots != 0 || nr_units != 0
12265 || (version == 2 && nr_columns != 0))
12266 {
b98664d3 12267 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12268 " all zero [in modules %s]"),
12269 dwp_file->name);
12270 }
12271 return htab;
12272 }
12273
12274 if (version == 1)
12275 {
12276 htab->section_pool.v1.indices =
12277 htab->unit_table + sizeof (uint32_t) * nr_slots;
12278 /* It's harder to decide whether the section is too small in v1.
12279 V1 is deprecated anyway so we punt. */
12280 }
12281 else
12282 {
12283 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12284 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12285 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12286 /* Reverse map for error checking. */
12287 int ids_seen[DW_SECT_MAX + 1];
12288 int i;
12289
12290 if (nr_columns < 2)
12291 {
12292 error (_("Dwarf Error: bad DWP hash table, too few columns"
12293 " in section table [in module %s]"),
12294 dwp_file->name);
12295 }
12296 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12297 {
12298 error (_("Dwarf Error: bad DWP hash table, too many columns"
12299 " in section table [in module %s]"),
12300 dwp_file->name);
12301 }
04fd5eed
GB
12302 memset (ids, 255, sizeof_ids);
12303 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12304 for (i = 0; i < nr_columns; ++i)
12305 {
12306 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12307
12308 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12309 {
12310 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12311 " in section table [in module %s]"),
12312 id, dwp_file->name);
12313 }
12314 if (ids_seen[id] != -1)
12315 {
12316 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12317 " id %d in section table [in module %s]"),
12318 id, dwp_file->name);
12319 }
12320 ids_seen[id] = i;
12321 ids[i] = id;
12322 }
12323 /* Must have exactly one info or types section. */
12324 if (((ids_seen[DW_SECT_INFO] != -1)
12325 + (ids_seen[DW_SECT_TYPES] != -1))
12326 != 1)
12327 {
12328 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12329 " DWO info/types section [in module %s]"),
12330 dwp_file->name);
12331 }
12332 /* Must have an abbrev section. */
12333 if (ids_seen[DW_SECT_ABBREV] == -1)
12334 {
12335 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12336 " section [in module %s]"),
12337 dwp_file->name);
12338 }
12339 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12340 htab->section_pool.v2.sizes =
12341 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12342 * nr_units * nr_columns);
12343 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12344 * nr_units * nr_columns))
12345 > index_end)
12346 {
12347 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12348 " [in module %s]"),
12349 dwp_file->name);
12350 }
12351 }
80626a55
DE
12352
12353 return htab;
12354}
12355
12356/* Update SECTIONS with the data from SECTP.
12357
12358 This function is like the other "locate" section routines that are
12359 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12360 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12361
12362 The result is non-zero for success, or zero if an error was found. */
12363
12364static int
73869dc2
DE
12365locate_v1_virtual_dwo_sections (asection *sectp,
12366 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12367{
12368 const struct dwop_section_names *names = &dwop_section_names;
12369
12370 if (section_is_p (sectp->name, &names->abbrev_dwo))
12371 {
12372 /* There can be only one. */
049412e3 12373 if (sections->abbrev.s.section != NULL)
80626a55 12374 return 0;
049412e3 12375 sections->abbrev.s.section = sectp;
fd361982 12376 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12377 }
12378 else if (section_is_p (sectp->name, &names->info_dwo)
12379 || section_is_p (sectp->name, &names->types_dwo))
12380 {
12381 /* There can be only one. */
049412e3 12382 if (sections->info_or_types.s.section != NULL)
80626a55 12383 return 0;
049412e3 12384 sections->info_or_types.s.section = sectp;
fd361982 12385 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12386 }
12387 else if (section_is_p (sectp->name, &names->line_dwo))
12388 {
12389 /* There can be only one. */
049412e3 12390 if (sections->line.s.section != NULL)
80626a55 12391 return 0;
049412e3 12392 sections->line.s.section = sectp;
fd361982 12393 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12394 }
12395 else if (section_is_p (sectp->name, &names->loc_dwo))
12396 {
12397 /* There can be only one. */
049412e3 12398 if (sections->loc.s.section != NULL)
80626a55 12399 return 0;
049412e3 12400 sections->loc.s.section = sectp;
fd361982 12401 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12402 }
12403 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12404 {
12405 /* There can be only one. */
049412e3 12406 if (sections->macinfo.s.section != NULL)
80626a55 12407 return 0;
049412e3 12408 sections->macinfo.s.section = sectp;
fd361982 12409 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12410 }
12411 else if (section_is_p (sectp->name, &names->macro_dwo))
12412 {
12413 /* There can be only one. */
049412e3 12414 if (sections->macro.s.section != NULL)
80626a55 12415 return 0;
049412e3 12416 sections->macro.s.section = sectp;
fd361982 12417 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12418 }
12419 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12420 {
12421 /* There can be only one. */
049412e3 12422 if (sections->str_offsets.s.section != NULL)
80626a55 12423 return 0;
049412e3 12424 sections->str_offsets.s.section = sectp;
fd361982 12425 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12426 }
12427 else
12428 {
12429 /* No other kind of section is valid. */
12430 return 0;
12431 }
12432
12433 return 1;
12434}
12435
73869dc2
DE
12436/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12437 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12438 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12439 This is for DWP version 1 files. */
80626a55
DE
12440
12441static struct dwo_unit *
ed2dc618
SM
12442create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12443 struct dwp_file *dwp_file,
73869dc2
DE
12444 uint32_t unit_index,
12445 const char *comp_dir,
12446 ULONGEST signature, int is_debug_types)
80626a55
DE
12447{
12448 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12449 const struct dwp_hash_table *dwp_htab =
12450 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12451 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12452 const char *kind = is_debug_types ? "TU" : "CU";
12453 struct dwo_file *dwo_file;
12454 struct dwo_unit *dwo_unit;
73869dc2 12455 struct virtual_v1_dwo_sections sections;
80626a55 12456 void **dwo_file_slot;
80626a55
DE
12457 int i;
12458
73869dc2
DE
12459 gdb_assert (dwp_file->version == 1);
12460
b4f54984 12461 if (dwarf_read_debug)
80626a55 12462 {
73869dc2 12463 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12464 kind,
73869dc2 12465 pulongest (unit_index), hex_string (signature),
80626a55
DE
12466 dwp_file->name);
12467 }
12468
19ac8c2e 12469 /* Fetch the sections of this DWO unit.
80626a55
DE
12470 Put a limit on the number of sections we look for so that bad data
12471 doesn't cause us to loop forever. */
12472
73869dc2 12473#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12474 (1 /* .debug_info or .debug_types */ \
12475 + 1 /* .debug_abbrev */ \
12476 + 1 /* .debug_line */ \
12477 + 1 /* .debug_loc */ \
12478 + 1 /* .debug_str_offsets */ \
19ac8c2e 12479 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12480 + 1 /* trailing zero */)
12481
12482 memset (&sections, 0, sizeof (sections));
80626a55 12483
73869dc2 12484 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12485 {
12486 asection *sectp;
12487 uint32_t section_nr =
12488 read_4_bytes (dbfd,
73869dc2
DE
12489 dwp_htab->section_pool.v1.indices
12490 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12491
12492 if (section_nr == 0)
12493 break;
12494 if (section_nr >= dwp_file->num_sections)
12495 {
12496 error (_("Dwarf Error: bad DWP hash table, section number too large"
12497 " [in module %s]"),
12498 dwp_file->name);
12499 }
12500
12501 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12502 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12503 {
12504 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12505 " [in module %s]"),
12506 dwp_file->name);
12507 }
12508 }
12509
12510 if (i < 2
a32a8923
DE
12511 || dwarf2_section_empty_p (&sections.info_or_types)
12512 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12513 {
12514 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12515 " [in module %s]"),
12516 dwp_file->name);
12517 }
73869dc2 12518 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12519 {
12520 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12521 " [in module %s]"),
12522 dwp_file->name);
12523 }
12524
12525 /* It's easier for the rest of the code if we fake a struct dwo_file and
12526 have dwo_unit "live" in that. At least for now.
12527
12528 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12529 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12530 file, we can combine them back into a virtual DWO file to save space
12531 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12532 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12533
791afaa2
TT
12534 std::string virtual_dwo_name =
12535 string_printf ("virtual-dwo/%d-%d-%d-%d",
12536 get_section_id (&sections.abbrev),
12537 get_section_id (&sections.line),
12538 get_section_id (&sections.loc),
12539 get_section_id (&sections.str_offsets));
80626a55 12540 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12541 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12542 virtual_dwo_name.c_str (),
12543 comp_dir);
80626a55
DE
12544 /* Create one if necessary. */
12545 if (*dwo_file_slot == NULL)
12546 {
b4f54984 12547 if (dwarf_read_debug)
80626a55
DE
12548 {
12549 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12550 virtual_dwo_name.c_str ());
80626a55 12551 }
51ac9db5 12552 dwo_file = new struct dwo_file;
efba19b0
TT
12553 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12554 virtual_dwo_name);
0ac5b59e 12555 dwo_file->comp_dir = comp_dir;
80626a55
DE
12556 dwo_file->sections.abbrev = sections.abbrev;
12557 dwo_file->sections.line = sections.line;
12558 dwo_file->sections.loc = sections.loc;
12559 dwo_file->sections.macinfo = sections.macinfo;
12560 dwo_file->sections.macro = sections.macro;
12561 dwo_file->sections.str_offsets = sections.str_offsets;
12562 /* The "str" section is global to the entire DWP file. */
12563 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12564 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12565 there's no need to record it in dwo_file.
12566 Also, we can't simply record type sections in dwo_file because
12567 we record a pointer into the vector in dwo_unit. As we collect more
12568 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12569 for it, invalidating all copies of pointers into the previous
12570 contents. */
80626a55
DE
12571 *dwo_file_slot = dwo_file;
12572 }
12573 else
12574 {
b4f54984 12575 if (dwarf_read_debug)
80626a55
DE
12576 {
12577 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12578 virtual_dwo_name.c_str ());
80626a55 12579 }
9a3c8263 12580 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12581 }
80626a55
DE
12582
12583 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12584 dwo_unit->dwo_file = dwo_file;
12585 dwo_unit->signature = signature;
8d749320
SM
12586 dwo_unit->section =
12587 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12588 *dwo_unit->section = sections.info_or_types;
57d63ce2 12589 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12590
12591 return dwo_unit;
12592}
12593
73869dc2
DE
12594/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12595 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12596 piece within that section used by a TU/CU, return a virtual section
12597 of just that piece. */
12598
12599static struct dwarf2_section_info
ed2dc618
SM
12600create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12601 struct dwarf2_section_info *section,
73869dc2
DE
12602 bfd_size_type offset, bfd_size_type size)
12603{
12604 struct dwarf2_section_info result;
12605 asection *sectp;
12606
12607 gdb_assert (section != NULL);
12608 gdb_assert (!section->is_virtual);
12609
12610 memset (&result, 0, sizeof (result));
12611 result.s.containing_section = section;
dc4ccb6f 12612 result.is_virtual = true;
73869dc2
DE
12613
12614 if (size == 0)
12615 return result;
12616
12617 sectp = get_section_bfd_section (section);
12618
12619 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12620 bounds of the real section. This is a pretty-rare event, so just
12621 flag an error (easier) instead of a warning and trying to cope. */
12622 if (sectp == NULL
fd361982 12623 || offset + size > bfd_section_size (sectp))
73869dc2 12624 {
73869dc2
DE
12625 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12626 " in section %s [in module %s]"),
fd361982 12627 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12628 objfile_name (dwarf2_per_objfile->objfile));
12629 }
12630
12631 result.virtual_offset = offset;
12632 result.size = size;
12633 return result;
12634}
12635
12636/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12637 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12638 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12639 This is for DWP version 2 files. */
12640
12641static struct dwo_unit *
ed2dc618
SM
12642create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12643 struct dwp_file *dwp_file,
73869dc2
DE
12644 uint32_t unit_index,
12645 const char *comp_dir,
12646 ULONGEST signature, int is_debug_types)
12647{
12648 struct objfile *objfile = dwarf2_per_objfile->objfile;
12649 const struct dwp_hash_table *dwp_htab =
12650 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12651 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12652 const char *kind = is_debug_types ? "TU" : "CU";
12653 struct dwo_file *dwo_file;
12654 struct dwo_unit *dwo_unit;
12655 struct virtual_v2_dwo_sections sections;
12656 void **dwo_file_slot;
73869dc2
DE
12657 int i;
12658
12659 gdb_assert (dwp_file->version == 2);
12660
b4f54984 12661 if (dwarf_read_debug)
73869dc2
DE
12662 {
12663 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12664 kind,
12665 pulongest (unit_index), hex_string (signature),
12666 dwp_file->name);
12667 }
12668
12669 /* Fetch the section offsets of this DWO unit. */
12670
12671 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12672
12673 for (i = 0; i < dwp_htab->nr_columns; ++i)
12674 {
12675 uint32_t offset = read_4_bytes (dbfd,
12676 dwp_htab->section_pool.v2.offsets
12677 + (((unit_index - 1) * dwp_htab->nr_columns
12678 + i)
12679 * sizeof (uint32_t)));
12680 uint32_t size = read_4_bytes (dbfd,
12681 dwp_htab->section_pool.v2.sizes
12682 + (((unit_index - 1) * dwp_htab->nr_columns
12683 + i)
12684 * sizeof (uint32_t)));
12685
12686 switch (dwp_htab->section_pool.v2.section_ids[i])
12687 {
12688 case DW_SECT_INFO:
12689 case DW_SECT_TYPES:
12690 sections.info_or_types_offset = offset;
12691 sections.info_or_types_size = size;
12692 break;
12693 case DW_SECT_ABBREV:
12694 sections.abbrev_offset = offset;
12695 sections.abbrev_size = size;
12696 break;
12697 case DW_SECT_LINE:
12698 sections.line_offset = offset;
12699 sections.line_size = size;
12700 break;
12701 case DW_SECT_LOC:
12702 sections.loc_offset = offset;
12703 sections.loc_size = size;
12704 break;
12705 case DW_SECT_STR_OFFSETS:
12706 sections.str_offsets_offset = offset;
12707 sections.str_offsets_size = size;
12708 break;
12709 case DW_SECT_MACINFO:
12710 sections.macinfo_offset = offset;
12711 sections.macinfo_size = size;
12712 break;
12713 case DW_SECT_MACRO:
12714 sections.macro_offset = offset;
12715 sections.macro_size = size;
12716 break;
12717 }
12718 }
12719
12720 /* It's easier for the rest of the code if we fake a struct dwo_file and
12721 have dwo_unit "live" in that. At least for now.
12722
12723 The DWP file can be made up of a random collection of CUs and TUs.
12724 However, for each CU + set of TUs that came from the same original DWO
12725 file, we can combine them back into a virtual DWO file to save space
12726 (fewer struct dwo_file objects to allocate). Remember that for really
12727 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12728
791afaa2
TT
12729 std::string virtual_dwo_name =
12730 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12731 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12732 (long) (sections.line_size ? sections.line_offset : 0),
12733 (long) (sections.loc_size ? sections.loc_offset : 0),
12734 (long) (sections.str_offsets_size
12735 ? sections.str_offsets_offset : 0));
73869dc2 12736 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12737 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12738 virtual_dwo_name.c_str (),
12739 comp_dir);
73869dc2
DE
12740 /* Create one if necessary. */
12741 if (*dwo_file_slot == NULL)
12742 {
b4f54984 12743 if (dwarf_read_debug)
73869dc2
DE
12744 {
12745 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12746 virtual_dwo_name.c_str ());
73869dc2 12747 }
51ac9db5 12748 dwo_file = new struct dwo_file;
efba19b0
TT
12749 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12750 virtual_dwo_name);
73869dc2
DE
12751 dwo_file->comp_dir = comp_dir;
12752 dwo_file->sections.abbrev =
ed2dc618 12753 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12754 sections.abbrev_offset, sections.abbrev_size);
12755 dwo_file->sections.line =
ed2dc618 12756 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12757 sections.line_offset, sections.line_size);
12758 dwo_file->sections.loc =
ed2dc618 12759 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12760 sections.loc_offset, sections.loc_size);
12761 dwo_file->sections.macinfo =
ed2dc618 12762 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12763 sections.macinfo_offset, sections.macinfo_size);
12764 dwo_file->sections.macro =
ed2dc618 12765 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12766 sections.macro_offset, sections.macro_size);
12767 dwo_file->sections.str_offsets =
ed2dc618
SM
12768 create_dwp_v2_section (dwarf2_per_objfile,
12769 &dwp_file->sections.str_offsets,
73869dc2
DE
12770 sections.str_offsets_offset,
12771 sections.str_offsets_size);
12772 /* The "str" section is global to the entire DWP file. */
12773 dwo_file->sections.str = dwp_file->sections.str;
12774 /* The info or types section is assigned below to dwo_unit,
12775 there's no need to record it in dwo_file.
12776 Also, we can't simply record type sections in dwo_file because
12777 we record a pointer into the vector in dwo_unit. As we collect more
12778 types we'll grow the vector and eventually have to reallocate space
12779 for it, invalidating all copies of pointers into the previous
12780 contents. */
12781 *dwo_file_slot = dwo_file;
12782 }
12783 else
12784 {
b4f54984 12785 if (dwarf_read_debug)
73869dc2
DE
12786 {
12787 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12788 virtual_dwo_name.c_str ());
73869dc2 12789 }
9a3c8263 12790 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12791 }
73869dc2
DE
12792
12793 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12794 dwo_unit->dwo_file = dwo_file;
12795 dwo_unit->signature = signature;
8d749320
SM
12796 dwo_unit->section =
12797 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12798 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12799 is_debug_types
73869dc2
DE
12800 ? &dwp_file->sections.types
12801 : &dwp_file->sections.info,
12802 sections.info_or_types_offset,
12803 sections.info_or_types_size);
12804 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12805
12806 return dwo_unit;
12807}
12808
57d63ce2
DE
12809/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12810 Returns NULL if the signature isn't found. */
80626a55
DE
12811
12812static struct dwo_unit *
ed2dc618
SM
12813lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12814 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12815 ULONGEST signature, int is_debug_types)
80626a55 12816{
57d63ce2
DE
12817 const struct dwp_hash_table *dwp_htab =
12818 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12819 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12820 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12821 uint32_t hash = signature & mask;
12822 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12823 unsigned int i;
12824 void **slot;
870f88f7 12825 struct dwo_unit find_dwo_cu;
80626a55
DE
12826
12827 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12828 find_dwo_cu.signature = signature;
19ac8c2e
DE
12829 slot = htab_find_slot (is_debug_types
12830 ? dwp_file->loaded_tus
12831 : dwp_file->loaded_cus,
12832 &find_dwo_cu, INSERT);
80626a55
DE
12833
12834 if (*slot != NULL)
9a3c8263 12835 return (struct dwo_unit *) *slot;
80626a55
DE
12836
12837 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12838 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12839 {
12840 ULONGEST signature_in_table;
12841
12842 signature_in_table =
57d63ce2 12843 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12844 if (signature_in_table == signature)
12845 {
57d63ce2
DE
12846 uint32_t unit_index =
12847 read_4_bytes (dbfd,
12848 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12849
73869dc2
DE
12850 if (dwp_file->version == 1)
12851 {
ed2dc618
SM
12852 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12853 dwp_file, unit_index,
73869dc2
DE
12854 comp_dir, signature,
12855 is_debug_types);
12856 }
12857 else
12858 {
ed2dc618
SM
12859 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12860 dwp_file, unit_index,
73869dc2
DE
12861 comp_dir, signature,
12862 is_debug_types);
12863 }
9a3c8263 12864 return (struct dwo_unit *) *slot;
80626a55
DE
12865 }
12866 if (signature_in_table == 0)
12867 return NULL;
12868 hash = (hash + hash2) & mask;
12869 }
12870
12871 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12872 " [in module %s]"),
12873 dwp_file->name);
12874}
12875
ab5088bf 12876/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12877 Open the file specified by FILE_NAME and hand it off to BFD for
12878 preliminary analysis. Return a newly initialized bfd *, which
12879 includes a canonicalized copy of FILE_NAME.
80626a55 12880 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12881 SEARCH_CWD is true if the current directory is to be searched.
12882 It will be searched before debug-file-directory.
13aaf454
DE
12883 If successful, the file is added to the bfd include table of the
12884 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12885 If unable to find/open the file, return NULL.
3019eac3
DE
12886 NOTE: This function is derived from symfile_bfd_open. */
12887
192b62ce 12888static gdb_bfd_ref_ptr
ed2dc618
SM
12889try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12890 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12891{
24b9144d 12892 int desc;
9c02c129
DE
12893 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12894 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12895 to debug_file_directory. */
e0cc99a6 12896 const char *search_path;
9c02c129
DE
12897 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12898
e0cc99a6 12899 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12900 if (search_cwd)
12901 {
12902 if (*debug_file_directory != '\0')
e0cc99a6
TT
12903 {
12904 search_path_holder.reset (concat (".", dirname_separator_string,
12905 debug_file_directory,
12906 (char *) NULL));
12907 search_path = search_path_holder.get ();
12908 }
6ac97d4c 12909 else
e0cc99a6 12910 search_path = ".";
6ac97d4c 12911 }
9c02c129 12912 else
e0cc99a6 12913 search_path = debug_file_directory;
3019eac3 12914
24b9144d 12915 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12916 if (is_dwp)
12917 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12918
12919 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12920 desc = openp (search_path, flags, file_name,
3019eac3
DE
12921 O_RDONLY | O_BINARY, &absolute_name);
12922 if (desc < 0)
12923 return NULL;
12924
e0cc99a6
TT
12925 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12926 gnutarget, desc));
9c02c129
DE
12927 if (sym_bfd == NULL)
12928 return NULL;
192b62ce 12929 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12930
192b62ce
TT
12931 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12932 return NULL;
3019eac3 12933
13aaf454
DE
12934 /* Success. Record the bfd as having been included by the objfile's bfd.
12935 This is important because things like demangled_names_hash lives in the
12936 objfile's per_bfd space and may have references to things like symbol
12937 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12938 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12939
3019eac3
DE
12940 return sym_bfd;
12941}
12942
ab5088bf 12943/* Try to open DWO file FILE_NAME.
3019eac3
DE
12944 COMP_DIR is the DW_AT_comp_dir attribute.
12945 The result is the bfd handle of the file.
12946 If there is a problem finding or opening the file, return NULL.
12947 Upon success, the canonicalized path of the file is stored in the bfd,
12948 same as symfile_bfd_open. */
12949
192b62ce 12950static gdb_bfd_ref_ptr
ed2dc618
SM
12951open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12952 const char *file_name, const char *comp_dir)
3019eac3 12953{
80626a55 12954 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12955 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12956 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12957
12958 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12959
12960 if (comp_dir != NULL)
12961 {
b36cec19
PA
12962 char *path_to_try = concat (comp_dir, SLASH_STRING,
12963 file_name, (char *) NULL);
3019eac3
DE
12964
12965 /* NOTE: If comp_dir is a relative path, this will also try the
12966 search path, which seems useful. */
ed2dc618
SM
12967 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12968 path_to_try,
12969 0 /*is_dwp*/,
192b62ce 12970 1 /*search_cwd*/));
3019eac3
DE
12971 xfree (path_to_try);
12972 if (abfd != NULL)
12973 return abfd;
12974 }
12975
12976 /* That didn't work, try debug-file-directory, which, despite its name,
12977 is a list of paths. */
12978
12979 if (*debug_file_directory == '\0')
12980 return NULL;
12981
ed2dc618
SM
12982 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12983 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12984}
12985
80626a55
DE
12986/* This function is mapped across the sections and remembers the offset and
12987 size of each of the DWO debugging sections we are interested in. */
12988
12989static void
12990dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12991{
9a3c8263 12992 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12993 const struct dwop_section_names *names = &dwop_section_names;
12994
12995 if (section_is_p (sectp->name, &names->abbrev_dwo))
12996 {
049412e3 12997 dwo_sections->abbrev.s.section = sectp;
fd361982 12998 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12999 }
13000 else if (section_is_p (sectp->name, &names->info_dwo))
13001 {
049412e3 13002 dwo_sections->info.s.section = sectp;
fd361982 13003 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
13004 }
13005 else if (section_is_p (sectp->name, &names->line_dwo))
13006 {
049412e3 13007 dwo_sections->line.s.section = sectp;
fd361982 13008 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
13009 }
13010 else if (section_is_p (sectp->name, &names->loc_dwo))
13011 {
049412e3 13012 dwo_sections->loc.s.section = sectp;
fd361982 13013 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
13014 }
13015 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13016 {
049412e3 13017 dwo_sections->macinfo.s.section = sectp;
fd361982 13018 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
13019 }
13020 else if (section_is_p (sectp->name, &names->macro_dwo))
13021 {
049412e3 13022 dwo_sections->macro.s.section = sectp;
fd361982 13023 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13024 }
13025 else if (section_is_p (sectp->name, &names->str_dwo))
13026 {
049412e3 13027 dwo_sections->str.s.section = sectp;
fd361982 13028 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13029 }
13030 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13031 {
049412e3 13032 dwo_sections->str_offsets.s.section = sectp;
fd361982 13033 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13034 }
13035 else if (section_is_p (sectp->name, &names->types_dwo))
13036 {
13037 struct dwarf2_section_info type_section;
13038
13039 memset (&type_section, 0, sizeof (type_section));
049412e3 13040 type_section.s.section = sectp;
fd361982 13041 type_section.size = bfd_section_size (sectp);
fd5866f6 13042 dwo_sections->types.push_back (type_section);
80626a55
DE
13043 }
13044}
13045
ab5088bf 13046/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13047 by PER_CU. This is for the non-DWP case.
80626a55 13048 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13049
13050static struct dwo_file *
0ac5b59e
DE
13051open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13052 const char *dwo_name, const char *comp_dir)
3019eac3 13053{
ed2dc618 13054 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13055
fb1eb2f9 13056 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13057 if (dbfd == NULL)
13058 {
b4f54984 13059 if (dwarf_read_debug)
80626a55
DE
13060 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13061 return NULL;
13062 }
263db9a1 13063
51ac9db5 13064 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13065 dwo_file->dwo_name = dwo_name;
13066 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13067 dwo_file->dbfd = std::move (dbfd);
3019eac3 13068
fb1eb2f9 13069 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13070 &dwo_file->sections);
3019eac3 13071
ed2dc618
SM
13072 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13073 dwo_file->cus);
3019eac3 13074
263db9a1 13075 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13076 dwo_file->sections.types, dwo_file->tus);
3019eac3 13077
b4f54984 13078 if (dwarf_read_debug)
80626a55
DE
13079 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13080
263db9a1 13081 return dwo_file.release ();
3019eac3
DE
13082}
13083
80626a55 13084/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13085 size of each of the DWP debugging sections common to version 1 and 2 that
13086 we are interested in. */
3019eac3 13087
80626a55 13088static void
73869dc2
DE
13089dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13090 void *dwp_file_ptr)
3019eac3 13091{
9a3c8263 13092 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13093 const struct dwop_section_names *names = &dwop_section_names;
13094 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13095
80626a55 13096 /* Record the ELF section number for later lookup: this is what the
73869dc2 13097 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13098 gdb_assert (elf_section_nr < dwp_file->num_sections);
13099 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13100
80626a55
DE
13101 /* Look for specific sections that we need. */
13102 if (section_is_p (sectp->name, &names->str_dwo))
13103 {
049412e3 13104 dwp_file->sections.str.s.section = sectp;
fd361982 13105 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13106 }
13107 else if (section_is_p (sectp->name, &names->cu_index))
13108 {
049412e3 13109 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13110 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13111 }
13112 else if (section_is_p (sectp->name, &names->tu_index))
13113 {
049412e3 13114 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13115 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13116 }
13117}
3019eac3 13118
73869dc2
DE
13119/* This function is mapped across the sections and remembers the offset and
13120 size of each of the DWP version 2 debugging sections that we are interested
13121 in. This is split into a separate function because we don't know if we
13122 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13123
13124static void
13125dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13126{
9a3c8263 13127 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13128 const struct dwop_section_names *names = &dwop_section_names;
13129 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13130
13131 /* Record the ELF section number for later lookup: this is what the
13132 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13133 gdb_assert (elf_section_nr < dwp_file->num_sections);
13134 dwp_file->elf_sections[elf_section_nr] = sectp;
13135
13136 /* Look for specific sections that we need. */
13137 if (section_is_p (sectp->name, &names->abbrev_dwo))
13138 {
049412e3 13139 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13140 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13141 }
13142 else if (section_is_p (sectp->name, &names->info_dwo))
13143 {
049412e3 13144 dwp_file->sections.info.s.section = sectp;
fd361982 13145 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13146 }
13147 else if (section_is_p (sectp->name, &names->line_dwo))
13148 {
049412e3 13149 dwp_file->sections.line.s.section = sectp;
fd361982 13150 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13151 }
13152 else if (section_is_p (sectp->name, &names->loc_dwo))
13153 {
049412e3 13154 dwp_file->sections.loc.s.section = sectp;
fd361982 13155 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13156 }
13157 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13158 {
049412e3 13159 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13160 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13161 }
13162 else if (section_is_p (sectp->name, &names->macro_dwo))
13163 {
049412e3 13164 dwp_file->sections.macro.s.section = sectp;
fd361982 13165 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13166 }
13167 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13168 {
049412e3 13169 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13170 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13171 }
13172 else if (section_is_p (sectp->name, &names->types_dwo))
13173 {
049412e3 13174 dwp_file->sections.types.s.section = sectp;
fd361982 13175 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13176 }
13177}
13178
80626a55 13179/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13180
80626a55
DE
13181static hashval_t
13182hash_dwp_loaded_cutus (const void *item)
13183{
9a3c8263 13184 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13185
80626a55
DE
13186 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13187 return dwo_unit->signature;
3019eac3
DE
13188}
13189
80626a55 13190/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13191
80626a55
DE
13192static int
13193eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13194{
9a3c8263
SM
13195 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13196 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13197
80626a55
DE
13198 return dua->signature == dub->signature;
13199}
3019eac3 13200
80626a55 13201/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13202
80626a55
DE
13203static htab_t
13204allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13205{
13206 return htab_create_alloc_ex (3,
13207 hash_dwp_loaded_cutus,
13208 eq_dwp_loaded_cutus,
13209 NULL,
13210 &objfile->objfile_obstack,
13211 hashtab_obstack_allocate,
13212 dummy_obstack_deallocate);
13213}
3019eac3 13214
ab5088bf
DE
13215/* Try to open DWP file FILE_NAME.
13216 The result is the bfd handle of the file.
13217 If there is a problem finding or opening the file, return NULL.
13218 Upon success, the canonicalized path of the file is stored in the bfd,
13219 same as symfile_bfd_open. */
13220
192b62ce 13221static gdb_bfd_ref_ptr
ed2dc618
SM
13222open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13223 const char *file_name)
ab5088bf 13224{
ed2dc618
SM
13225 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13226 1 /*is_dwp*/,
192b62ce 13227 1 /*search_cwd*/));
6ac97d4c
DE
13228 if (abfd != NULL)
13229 return abfd;
13230
13231 /* Work around upstream bug 15652.
13232 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13233 [Whether that's a "bug" is debatable, but it is getting in our way.]
13234 We have no real idea where the dwp file is, because gdb's realpath-ing
13235 of the executable's path may have discarded the needed info.
13236 [IWBN if the dwp file name was recorded in the executable, akin to
13237 .gnu_debuglink, but that doesn't exist yet.]
13238 Strip the directory from FILE_NAME and search again. */
13239 if (*debug_file_directory != '\0')
13240 {
13241 /* Don't implicitly search the current directory here.
13242 If the user wants to search "." to handle this case,
13243 it must be added to debug-file-directory. */
ed2dc618
SM
13244 return try_open_dwop_file (dwarf2_per_objfile,
13245 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13246 0 /*search_cwd*/);
13247 }
13248
13249 return NULL;
ab5088bf
DE
13250}
13251
80626a55
DE
13252/* Initialize the use of the DWP file for the current objfile.
13253 By convention the name of the DWP file is ${objfile}.dwp.
13254 The result is NULL if it can't be found. */
a766d390 13255
400174b1 13256static std::unique_ptr<struct dwp_file>
ed2dc618 13257open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13258{
13259 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13260
82bf32bc
JK
13261 /* Try to find first .dwp for the binary file before any symbolic links
13262 resolving. */
6c447423
DE
13263
13264 /* If the objfile is a debug file, find the name of the real binary
13265 file and get the name of dwp file from there. */
d721ba37 13266 std::string dwp_name;
6c447423
DE
13267 if (objfile->separate_debug_objfile_backlink != NULL)
13268 {
13269 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13270 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13271
d721ba37 13272 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13273 }
13274 else
d721ba37
PA
13275 dwp_name = objfile->original_name;
13276
13277 dwp_name += ".dwp";
80626a55 13278
ed2dc618 13279 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13280 if (dbfd == NULL
13281 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13282 {
13283 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13284 dwp_name = objfile_name (objfile);
13285 dwp_name += ".dwp";
ed2dc618 13286 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13287 }
13288
80626a55
DE
13289 if (dbfd == NULL)
13290 {
b4f54984 13291 if (dwarf_read_debug)
d721ba37 13292 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13293 return std::unique_ptr<dwp_file> ();
3019eac3 13294 }
400174b1
TT
13295
13296 const char *name = bfd_get_filename (dbfd.get ());
13297 std::unique_ptr<struct dwp_file> dwp_file
13298 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13299
0a0f4c01 13300 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13301 dwp_file->elf_sections =
13302 OBSTACK_CALLOC (&objfile->objfile_obstack,
13303 dwp_file->num_sections, asection *);
13304
400174b1
TT
13305 bfd_map_over_sections (dwp_file->dbfd.get (),
13306 dwarf2_locate_common_dwp_sections,
13307 dwp_file.get ());
80626a55 13308
400174b1
TT
13309 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13310 0);
80626a55 13311
400174b1
TT
13312 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13313 1);
80626a55 13314
73869dc2 13315 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13316 if (dwp_file->cus && dwp_file->tus
13317 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13318 {
13319 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13320 pretty bizarre. We use pulongest here because that's the established
4d65956b 13321 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13322 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13323 " TU version %s [in DWP file %s]"),
13324 pulongest (dwp_file->cus->version),
d721ba37 13325 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13326 }
08302ed2
DE
13327
13328 if (dwp_file->cus)
13329 dwp_file->version = dwp_file->cus->version;
13330 else if (dwp_file->tus)
13331 dwp_file->version = dwp_file->tus->version;
13332 else
13333 dwp_file->version = 2;
73869dc2
DE
13334
13335 if (dwp_file->version == 2)
400174b1
TT
13336 bfd_map_over_sections (dwp_file->dbfd.get (),
13337 dwarf2_locate_v2_dwp_sections,
13338 dwp_file.get ());
73869dc2 13339
19ac8c2e
DE
13340 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13341 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13342
b4f54984 13343 if (dwarf_read_debug)
80626a55
DE
13344 {
13345 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13346 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13347 " %s CUs, %s TUs\n",
13348 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13349 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13350 }
13351
13352 return dwp_file;
3019eac3 13353}
c906108c 13354
ab5088bf
DE
13355/* Wrapper around open_and_init_dwp_file, only open it once. */
13356
13357static struct dwp_file *
ed2dc618 13358get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13359{
13360 if (! dwarf2_per_objfile->dwp_checked)
13361 {
ed2dc618
SM
13362 dwarf2_per_objfile->dwp_file
13363 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13364 dwarf2_per_objfile->dwp_checked = 1;
13365 }
400174b1 13366 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13367}
13368
80626a55
DE
13369/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13370 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13371 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13372 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13373 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13374
13375 This is called, for example, when wanting to read a variable with a
13376 complex location. Therefore we don't want to do file i/o for every call.
13377 Therefore we don't want to look for a DWO file on every call.
13378 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13379 then we check if we've already seen DWO_NAME, and only THEN do we check
13380 for a DWO file.
13381
1c658ad5 13382 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13383 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13384
3019eac3 13385static struct dwo_unit *
80626a55
DE
13386lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13387 const char *dwo_name, const char *comp_dir,
13388 ULONGEST signature, int is_debug_types)
3019eac3 13389{
ed2dc618 13390 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13391 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13392 const char *kind = is_debug_types ? "TU" : "CU";
13393 void **dwo_file_slot;
3019eac3 13394 struct dwo_file *dwo_file;
80626a55 13395 struct dwp_file *dwp_file;
cb1df416 13396
6a506a2d
DE
13397 /* First see if there's a DWP file.
13398 If we have a DWP file but didn't find the DWO inside it, don't
13399 look for the original DWO file. It makes gdb behave differently
13400 depending on whether one is debugging in the build tree. */
cf2c3c16 13401
ed2dc618 13402 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13403 if (dwp_file != NULL)
cf2c3c16 13404 {
80626a55
DE
13405 const struct dwp_hash_table *dwp_htab =
13406 is_debug_types ? dwp_file->tus : dwp_file->cus;
13407
13408 if (dwp_htab != NULL)
13409 {
13410 struct dwo_unit *dwo_cutu =
ed2dc618 13411 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13412 signature, is_debug_types);
80626a55
DE
13413
13414 if (dwo_cutu != NULL)
13415 {
b4f54984 13416 if (dwarf_read_debug)
80626a55
DE
13417 {
13418 fprintf_unfiltered (gdb_stdlog,
13419 "Virtual DWO %s %s found: @%s\n",
13420 kind, hex_string (signature),
13421 host_address_to_string (dwo_cutu));
13422 }
13423 return dwo_cutu;
13424 }
13425 }
13426 }
6a506a2d 13427 else
80626a55 13428 {
6a506a2d 13429 /* No DWP file, look for the DWO file. */
80626a55 13430
ed2dc618
SM
13431 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13432 dwo_name, comp_dir);
6a506a2d 13433 if (*dwo_file_slot == NULL)
80626a55 13434 {
6a506a2d
DE
13435 /* Read in the file and build a table of the CUs/TUs it contains. */
13436 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13437 }
6a506a2d 13438 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13439 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13440
6a506a2d 13441 if (dwo_file != NULL)
19c3d4c9 13442 {
6a506a2d
DE
13443 struct dwo_unit *dwo_cutu = NULL;
13444
13445 if (is_debug_types && dwo_file->tus)
13446 {
13447 struct dwo_unit find_dwo_cutu;
13448
13449 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13450 find_dwo_cutu.signature = signature;
9a3c8263
SM
13451 dwo_cutu
13452 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13453 }
33c5cd75 13454 else if (!is_debug_types && dwo_file->cus)
80626a55 13455 {
33c5cd75
DB
13456 struct dwo_unit find_dwo_cutu;
13457
13458 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13459 find_dwo_cutu.signature = signature;
13460 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13461 &find_dwo_cutu);
6a506a2d
DE
13462 }
13463
13464 if (dwo_cutu != NULL)
13465 {
b4f54984 13466 if (dwarf_read_debug)
6a506a2d
DE
13467 {
13468 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13469 kind, dwo_name, hex_string (signature),
13470 host_address_to_string (dwo_cutu));
13471 }
13472 return dwo_cutu;
80626a55
DE
13473 }
13474 }
2e276125 13475 }
9cdd5dbd 13476
80626a55
DE
13477 /* We didn't find it. This could mean a dwo_id mismatch, or
13478 someone deleted the DWO/DWP file, or the search path isn't set up
13479 correctly to find the file. */
13480
b4f54984 13481 if (dwarf_read_debug)
80626a55
DE
13482 {
13483 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13484 kind, dwo_name, hex_string (signature));
13485 }
3019eac3 13486
6656a72d
DE
13487 /* This is a warning and not a complaint because it can be caused by
13488 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13489 {
13490 /* Print the name of the DWP file if we looked there, helps the user
13491 better diagnose the problem. */
791afaa2 13492 std::string dwp_text;
43942612
DE
13493
13494 if (dwp_file != NULL)
791afaa2
TT
13495 dwp_text = string_printf (" [in DWP file %s]",
13496 lbasename (dwp_file->name));
43942612 13497
9d8780f0 13498 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13499 " [in module %s]"),
13500 kind, dwo_name, hex_string (signature),
791afaa2 13501 dwp_text.c_str (),
43942612 13502 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13503 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13504 }
3019eac3 13505 return NULL;
5fb290d7
DJ
13506}
13507
80626a55
DE
13508/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13509 See lookup_dwo_cutu_unit for details. */
13510
13511static struct dwo_unit *
13512lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13513 const char *dwo_name, const char *comp_dir,
13514 ULONGEST signature)
13515{
13516 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13517}
13518
13519/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13520 See lookup_dwo_cutu_unit for details. */
13521
13522static struct dwo_unit *
13523lookup_dwo_type_unit (struct signatured_type *this_tu,
13524 const char *dwo_name, const char *comp_dir)
13525{
13526 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13527}
13528
89e63ee4
DE
13529/* Traversal function for queue_and_load_all_dwo_tus. */
13530
13531static int
13532queue_and_load_dwo_tu (void **slot, void *info)
13533{
13534 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13535 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13536 ULONGEST signature = dwo_unit->signature;
13537 struct signatured_type *sig_type =
13538 lookup_dwo_signatured_type (per_cu->cu, signature);
13539
13540 if (sig_type != NULL)
13541 {
13542 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13543
13544 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13545 a real dependency of PER_CU on SIG_TYPE. That is detected later
13546 while processing PER_CU. */
13547 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13548 load_full_type_unit (sig_cu);
ae640021 13549 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13550 }
13551
13552 return 1;
13553}
13554
13555/* Queue all TUs contained in the DWO of PER_CU to be read in.
13556 The DWO may have the only definition of the type, though it may not be
13557 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13558 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13559
13560static void
13561queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13562{
13563 struct dwo_unit *dwo_unit;
13564 struct dwo_file *dwo_file;
13565
13566 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13567 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13568 gdb_assert (per_cu->cu != NULL);
13569
13570 dwo_unit = per_cu->cu->dwo_unit;
13571 gdb_assert (dwo_unit != NULL);
13572
13573 dwo_file = dwo_unit->dwo_file;
13574 if (dwo_file->tus != NULL)
13575 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13576}
13577
3019eac3 13578/* Read in various DIEs. */
348e048f 13579
d389af10 13580/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13581 Inherit only the children of the DW_AT_abstract_origin DIE not being
13582 already referenced by DW_AT_abstract_origin from the children of the
13583 current DIE. */
d389af10
JK
13584
13585static void
13586inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13587{
13588 struct die_info *child_die;
791afaa2 13589 sect_offset *offsetp;
d389af10
JK
13590 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13591 struct die_info *origin_die;
13592 /* Iterator of the ORIGIN_DIE children. */
13593 struct die_info *origin_child_die;
d389af10 13594 struct attribute *attr;
cd02d79d
PA
13595 struct dwarf2_cu *origin_cu;
13596 struct pending **origin_previous_list_in_scope;
d389af10
JK
13597
13598 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13599 if (!attr)
13600 return;
13601
cd02d79d
PA
13602 /* Note that following die references may follow to a die in a
13603 different cu. */
13604
13605 origin_cu = cu;
13606 origin_die = follow_die_ref (die, attr, &origin_cu);
13607
13608 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13609 symbols in. */
13610 origin_previous_list_in_scope = origin_cu->list_in_scope;
13611 origin_cu->list_in_scope = cu->list_in_scope;
13612
edb3359d
DJ
13613 if (die->tag != origin_die->tag
13614 && !(die->tag == DW_TAG_inlined_subroutine
13615 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13616 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13617 sect_offset_str (die->sect_off),
13618 sect_offset_str (origin_die->sect_off));
d389af10 13619
791afaa2 13620 std::vector<sect_offset> offsets;
d389af10 13621
3ea89b92
PMR
13622 for (child_die = die->child;
13623 child_die && child_die->tag;
13624 child_die = sibling_die (child_die))
13625 {
13626 struct die_info *child_origin_die;
13627 struct dwarf2_cu *child_origin_cu;
13628
13629 /* We are trying to process concrete instance entries:
216f72a1 13630 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13631 it's not relevant to our analysis here. i.e. detecting DIEs that are
13632 present in the abstract instance but not referenced in the concrete
13633 one. */
216f72a1
JK
13634 if (child_die->tag == DW_TAG_call_site
13635 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13636 continue;
13637
c38f313d
DJ
13638 /* For each CHILD_DIE, find the corresponding child of
13639 ORIGIN_DIE. If there is more than one layer of
13640 DW_AT_abstract_origin, follow them all; there shouldn't be,
13641 but GCC versions at least through 4.4 generate this (GCC PR
13642 40573). */
3ea89b92
PMR
13643 child_origin_die = child_die;
13644 child_origin_cu = cu;
c38f313d
DJ
13645 while (1)
13646 {
cd02d79d
PA
13647 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13648 child_origin_cu);
c38f313d
DJ
13649 if (attr == NULL)
13650 break;
cd02d79d
PA
13651 child_origin_die = follow_die_ref (child_origin_die, attr,
13652 &child_origin_cu);
c38f313d
DJ
13653 }
13654
d389af10
JK
13655 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13656 counterpart may exist. */
c38f313d 13657 if (child_origin_die != child_die)
d389af10 13658 {
edb3359d
DJ
13659 if (child_die->tag != child_origin_die->tag
13660 && !(child_die->tag == DW_TAG_inlined_subroutine
13661 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13662 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13663 "different tags"),
9d8780f0
SM
13664 sect_offset_str (child_die->sect_off),
13665 sect_offset_str (child_origin_die->sect_off));
c38f313d 13666 if (child_origin_die->parent != origin_die)
b98664d3 13667 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13668 "different parents"),
9d8780f0
SM
13669 sect_offset_str (child_die->sect_off),
13670 sect_offset_str (child_origin_die->sect_off));
c38f313d 13671 else
791afaa2 13672 offsets.push_back (child_origin_die->sect_off);
d389af10 13673 }
d389af10 13674 }
791afaa2
TT
13675 std::sort (offsets.begin (), offsets.end ());
13676 sect_offset *offsets_end = offsets.data () + offsets.size ();
13677 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13678 if (offsetp[-1] == *offsetp)
b98664d3 13679 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13680 "to DIE %s as their abstract origin"),
13681 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13682
791afaa2 13683 offsetp = offsets.data ();
d389af10
JK
13684 origin_child_die = origin_die->child;
13685 while (origin_child_die && origin_child_die->tag)
13686 {
13687 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13688 while (offsetp < offsets_end
9c541725 13689 && *offsetp < origin_child_die->sect_off)
d389af10 13690 offsetp++;
b64f50a1 13691 if (offsetp >= offsets_end
9c541725 13692 || *offsetp > origin_child_die->sect_off)
d389af10 13693 {
adde2bff
DE
13694 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13695 Check whether we're already processing ORIGIN_CHILD_DIE.
13696 This can happen with mutually referenced abstract_origins.
13697 PR 16581. */
13698 if (!origin_child_die->in_process)
13699 process_die (origin_child_die, origin_cu);
d389af10
JK
13700 }
13701 origin_child_die = sibling_die (origin_child_die);
13702 }
cd02d79d 13703 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13704
13705 if (cu != origin_cu)
13706 compute_delayed_physnames (origin_cu);
d389af10
JK
13707}
13708
c906108c 13709static void
e7c27a73 13710read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13711{
518817b3 13712 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13714 struct context_stack *newobj;
c906108c
SS
13715 CORE_ADDR lowpc;
13716 CORE_ADDR highpc;
13717 struct die_info *child_die;
edb3359d 13718 struct attribute *attr, *call_line, *call_file;
15d034d0 13719 const char *name;
e142c38c 13720 CORE_ADDR baseaddr;
801e3a5b 13721 struct block *block;
edb3359d 13722 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13723 std::vector<struct symbol *> template_args;
34eaf542 13724 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13725
13726 if (inlined_func)
13727 {
13728 /* If we do not have call site information, we can't show the
13729 caller of this inlined function. That's too confusing, so
13730 only use the scope for local variables. */
13731 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13732 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13733 if (call_line == NULL || call_file == NULL)
13734 {
13735 read_lexical_block_scope (die, cu);
13736 return;
13737 }
13738 }
c906108c 13739
e142c38c
DJ
13740 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13741
94af9270 13742 name = dwarf2_name (die, cu);
c906108c 13743
e8d05480
JB
13744 /* Ignore functions with missing or empty names. These are actually
13745 illegal according to the DWARF standard. */
13746 if (name == NULL)
13747 {
b98664d3 13748 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13749 sect_offset_str (die->sect_off));
e8d05480
JB
13750 return;
13751 }
13752
13753 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13754 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13755 <= PC_BOUNDS_INVALID)
e8d05480 13756 {
ae4d0c03
PM
13757 attr = dwarf2_attr (die, DW_AT_external, cu);
13758 if (!attr || !DW_UNSND (attr))
b98664d3 13759 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13760 "for subprogram DIE at %s"),
13761 sect_offset_str (die->sect_off));
e8d05480
JB
13762 return;
13763 }
c906108c 13764
3e29f34a
MR
13765 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13766 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13767
34eaf542
TT
13768 /* If we have any template arguments, then we must allocate a
13769 different sort of symbol. */
13770 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13771 {
13772 if (child_die->tag == DW_TAG_template_type_param
13773 || child_die->tag == DW_TAG_template_value_param)
13774 {
e623cf5d 13775 templ_func = allocate_template_symbol (objfile);
cf724bc9 13776 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13777 break;
13778 }
13779 }
13780
c24bdb02 13781 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13782 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13783 (struct symbol *) templ_func);
4c2df51b 13784
81873cc8 13785 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13786 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13787 cu->language);
13788
4cecd739
DJ
13789 /* If there is a location expression for DW_AT_frame_base, record
13790 it. */
e142c38c 13791 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13792 if (attr != nullptr)
fe978cb0 13793 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13794
63e43d3a
PMR
13795 /* If there is a location for the static link, record it. */
13796 newobj->static_link = NULL;
13797 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13798 if (attr != nullptr)
63e43d3a 13799 {
224c3ddb
SM
13800 newobj->static_link
13801 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13802 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13803 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13804 }
13805
c24bdb02 13806 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13807
639d11d3 13808 if (die->child != NULL)
c906108c 13809 {
639d11d3 13810 child_die = die->child;
c906108c
SS
13811 while (child_die && child_die->tag)
13812 {
34eaf542
TT
13813 if (child_die->tag == DW_TAG_template_type_param
13814 || child_die->tag == DW_TAG_template_value_param)
13815 {
13816 struct symbol *arg = new_symbol (child_die, NULL, cu);
13817
f1078f66 13818 if (arg != NULL)
2f4732b0 13819 template_args.push_back (arg);
34eaf542
TT
13820 }
13821 else
13822 process_die (child_die, cu);
c906108c
SS
13823 child_die = sibling_die (child_die);
13824 }
13825 }
13826
d389af10
JK
13827 inherit_abstract_dies (die, cu);
13828
4a811a97
UW
13829 /* If we have a DW_AT_specification, we might need to import using
13830 directives from the context of the specification DIE. See the
13831 comment in determine_prefix. */
13832 if (cu->language == language_cplus
13833 && dwarf2_attr (die, DW_AT_specification, cu))
13834 {
13835 struct dwarf2_cu *spec_cu = cu;
13836 struct die_info *spec_die = die_specification (die, &spec_cu);
13837
13838 while (spec_die)
13839 {
13840 child_die = spec_die->child;
13841 while (child_die && child_die->tag)
13842 {
13843 if (child_die->tag == DW_TAG_imported_module)
13844 process_die (child_die, spec_cu);
13845 child_die = sibling_die (child_die);
13846 }
13847
13848 /* In some cases, GCC generates specification DIEs that
13849 themselves contain DW_AT_specification attributes. */
13850 spec_die = die_specification (spec_die, &spec_cu);
13851 }
13852 }
13853
c24bdb02 13854 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13855 /* Make a block for the local symbols within. */
c24bdb02 13856 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13857 cstk.static_link, lowpc, highpc);
801e3a5b 13858
df8a16a1 13859 /* For C++, set the block's scope. */
45280282
IB
13860 if ((cu->language == language_cplus
13861 || cu->language == language_fortran
c44af4eb
TT
13862 || cu->language == language_d
13863 || cu->language == language_rust)
4d4ec4e5 13864 && cu->processing_has_namespace_info)
195a3f6c
TT
13865 block_set_scope (block, determine_prefix (die, cu),
13866 &objfile->objfile_obstack);
df8a16a1 13867
801e3a5b
JB
13868 /* If we have address ranges, record them. */
13869 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13870
a60f3166 13871 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13872
34eaf542 13873 /* Attach template arguments to function. */
2f4732b0 13874 if (!template_args.empty ())
34eaf542
TT
13875 {
13876 gdb_assert (templ_func != NULL);
13877
2f4732b0 13878 templ_func->n_template_arguments = template_args.size ();
34eaf542 13879 templ_func->template_arguments
8d749320
SM
13880 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13881 templ_func->n_template_arguments);
34eaf542 13882 memcpy (templ_func->template_arguments,
2f4732b0 13883 template_args.data (),
34eaf542 13884 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13885
13886 /* Make sure that the symtab is set on the new symbols. Even
13887 though they don't appear in this symtab directly, other parts
13888 of gdb assume that symbols do, and this is reasonably
13889 true. */
8634679f 13890 for (symbol *sym : template_args)
3e1d3d8c 13891 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13892 }
13893
208d8187
JB
13894 /* In C++, we can have functions nested inside functions (e.g., when
13895 a function declares a class that has methods). This means that
13896 when we finish processing a function scope, we may need to go
13897 back to building a containing block's symbol lists. */
c24bdb02
KS
13898 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13899 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13900
921e78cf
JB
13901 /* If we've finished processing a top-level function, subsequent
13902 symbols go in the file symbol list. */
c24bdb02
KS
13903 if (cu->get_builder ()->outermost_context_p ())
13904 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13905}
13906
13907/* Process all the DIES contained within a lexical block scope. Start
13908 a new scope, process the dies, and then close the scope. */
13909
13910static void
e7c27a73 13911read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13912{
518817b3 13913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13914 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13915 CORE_ADDR lowpc, highpc;
13916 struct die_info *child_die;
e142c38c
DJ
13917 CORE_ADDR baseaddr;
13918
13919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13920
13921 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13922 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13923 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13924 be nasty. Might be easier to properly extend generic blocks to
af34e669 13925 describe ranges. */
e385593e
JK
13926 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13927 {
13928 case PC_BOUNDS_NOT_PRESENT:
13929 /* DW_TAG_lexical_block has no attributes, process its children as if
13930 there was no wrapping by that DW_TAG_lexical_block.
13931 GCC does no longer produces such DWARF since GCC r224161. */
13932 for (child_die = die->child;
13933 child_die != NULL && child_die->tag;
13934 child_die = sibling_die (child_die))
13935 process_die (child_die, cu);
13936 return;
13937 case PC_BOUNDS_INVALID:
13938 return;
13939 }
3e29f34a
MR
13940 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13941 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13942
c24bdb02 13943 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13944 if (die->child != NULL)
c906108c 13945 {
639d11d3 13946 child_die = die->child;
c906108c
SS
13947 while (child_die && child_die->tag)
13948 {
e7c27a73 13949 process_die (child_die, cu);
c906108c
SS
13950 child_die = sibling_die (child_die);
13951 }
13952 }
3ea89b92 13953 inherit_abstract_dies (die, cu);
c24bdb02 13954 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13955
c24bdb02
KS
13956 if (*cu->get_builder ()->get_local_symbols () != NULL
13957 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13958 {
801e3a5b 13959 struct block *block
c24bdb02 13960 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13961 cstk.start_addr, highpc);
801e3a5b
JB
13962
13963 /* Note that recording ranges after traversing children, as we
13964 do here, means that recording a parent's ranges entails
13965 walking across all its children's ranges as they appear in
13966 the address map, which is quadratic behavior.
13967
13968 It would be nicer to record the parent's ranges before
13969 traversing its children, simply overriding whatever you find
13970 there. But since we don't even decide whether to create a
13971 block until after we've traversed its children, that's hard
13972 to do. */
13973 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13974 }
c24bdb02
KS
13975 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13976 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13977}
13978
216f72a1 13979/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13980
13981static void
13982read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13983{
518817b3 13984 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13986 CORE_ADDR pc, baseaddr;
13987 struct attribute *attr;
13988 struct call_site *call_site, call_site_local;
13989 void **slot;
13990 int nparams;
13991 struct die_info *child_die;
13992
13993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13994
216f72a1
JK
13995 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13996 if (attr == NULL)
13997 {
13998 /* This was a pre-DWARF-5 GNU extension alias
13999 for DW_AT_call_return_pc. */
14000 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14001 }
96408a79
SA
14002 if (!attr)
14003 {
b98664d3 14004 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
14005 "DIE %s [in module %s]"),
14006 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14007 return;
14008 }
31aa7e4e 14009 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 14010 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
14011
14012 if (cu->call_site_htab == NULL)
14013 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
14014 NULL, &objfile->objfile_obstack,
14015 hashtab_obstack_allocate, NULL);
14016 call_site_local.pc = pc;
14017 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
14018 if (*slot != NULL)
14019 {
b98664d3 14020 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
14021 "DIE %s [in module %s]"),
14022 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14023 objfile_name (objfile));
96408a79
SA
14024 return;
14025 }
14026
14027 /* Count parameters at the caller. */
14028
14029 nparams = 0;
14030 for (child_die = die->child; child_die && child_die->tag;
14031 child_die = sibling_die (child_die))
14032 {
216f72a1
JK
14033 if (child_die->tag != DW_TAG_call_site_parameter
14034 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14035 {
b98664d3 14036 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14037 "DW_TAG_call_site child DIE %s [in module %s]"),
14038 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14039 objfile_name (objfile));
96408a79
SA
14040 continue;
14041 }
14042
14043 nparams++;
14044 }
14045
224c3ddb
SM
14046 call_site
14047 = ((struct call_site *)
14048 obstack_alloc (&objfile->objfile_obstack,
14049 sizeof (*call_site)
14050 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14051 *slot = call_site;
14052 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14053 call_site->pc = pc;
14054
216f72a1
JK
14055 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14056 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14057 {
14058 struct die_info *func_die;
14059
14060 /* Skip also over DW_TAG_inlined_subroutine. */
14061 for (func_die = die->parent;
14062 func_die && func_die->tag != DW_TAG_subprogram
14063 && func_die->tag != DW_TAG_subroutine_type;
14064 func_die = func_die->parent);
14065
216f72a1
JK
14066 /* DW_AT_call_all_calls is a superset
14067 of DW_AT_call_all_tail_calls. */
96408a79 14068 if (func_die
216f72a1 14069 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14070 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14071 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14072 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14073 {
14074 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14075 not complete. But keep CALL_SITE for look ups via call_site_htab,
14076 both the initial caller containing the real return address PC and
14077 the final callee containing the current PC of a chain of tail
14078 calls do not need to have the tail call list complete. But any
14079 function candidate for a virtual tail call frame searched via
14080 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14081 determined unambiguously. */
14082 }
14083 else
14084 {
14085 struct type *func_type = NULL;
14086
14087 if (func_die)
14088 func_type = get_die_type (func_die, cu);
14089 if (func_type != NULL)
14090 {
14091 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14092
14093 /* Enlist this call site to the function. */
14094 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14095 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14096 }
14097 else
b98664d3 14098 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14099 "DIE %s [in module %s]"),
14100 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14101 }
14102 }
14103
216f72a1
JK
14104 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14105 if (attr == NULL)
14106 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14107 if (attr == NULL)
14108 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14109 if (attr == NULL)
216f72a1
JK
14110 {
14111 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14112 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14113 }
96408a79
SA
14114 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14115 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14116 /* Keep NULL DWARF_BLOCK. */;
14117 else if (attr_form_is_block (attr))
14118 {
14119 struct dwarf2_locexpr_baton *dlbaton;
14120
8d749320 14121 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14122 dlbaton->data = DW_BLOCK (attr)->data;
14123 dlbaton->size = DW_BLOCK (attr)->size;
14124 dlbaton->per_cu = cu->per_cu;
14125
14126 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14127 }
7771576e 14128 else if (attr_form_is_ref (attr))
96408a79 14129 {
96408a79
SA
14130 struct dwarf2_cu *target_cu = cu;
14131 struct die_info *target_die;
14132
ac9ec31b 14133 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14134 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14135 if (die_is_declaration (target_die, target_cu))
14136 {
7d45c7c3 14137 const char *target_physname;
9112db09
JK
14138
14139 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14140 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14141 if (target_physname == NULL)
9112db09 14142 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14143 if (target_physname == NULL)
b98664d3 14144 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14145 "physname, for referencing DIE %s [in module %s]"),
14146 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14147 else
7d455152 14148 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14149 }
14150 else
14151 {
14152 CORE_ADDR lowpc;
14153
14154 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14155 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14156 <= PC_BOUNDS_INVALID)
b98664d3 14157 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14158 "low pc, for referencing DIE %s [in module %s]"),
14159 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14160 else
3e29f34a
MR
14161 {
14162 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14163 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14164 }
96408a79
SA
14165 }
14166 }
14167 else
b98664d3 14168 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14169 "block nor reference, for DIE %s [in module %s]"),
14170 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14171
14172 call_site->per_cu = cu->per_cu;
14173
14174 for (child_die = die->child;
14175 child_die && child_die->tag;
14176 child_die = sibling_die (child_die))
14177 {
96408a79 14178 struct call_site_parameter *parameter;
1788b2d3 14179 struct attribute *loc, *origin;
96408a79 14180
216f72a1
JK
14181 if (child_die->tag != DW_TAG_call_site_parameter
14182 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14183 {
14184 /* Already printed the complaint above. */
14185 continue;
14186 }
14187
14188 gdb_assert (call_site->parameter_count < nparams);
14189 parameter = &call_site->parameter[call_site->parameter_count];
14190
1788b2d3
JK
14191 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14192 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14193 register is contained in DW_AT_call_value. */
96408a79 14194
24c5c679 14195 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14196 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14197 if (origin == NULL)
14198 {
14199 /* This was a pre-DWARF-5 GNU extension alias
14200 for DW_AT_call_parameter. */
14201 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14202 }
7771576e 14203 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14204 {
1788b2d3 14205 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14206
14207 sect_offset sect_off
14208 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14209 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14210 {
14211 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14212 binding can be done only inside one CU. Such referenced DIE
14213 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14214 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14215 "DW_TAG_call_site child DIE %s [in module %s]"),
14216 sect_offset_str (child_die->sect_off),
9c541725 14217 objfile_name (objfile));
d76b7dbc
JK
14218 continue;
14219 }
9c541725
PA
14220 parameter->u.param_cu_off
14221 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14222 }
14223 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14224 {
b98664d3 14225 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14228 continue;
14229 }
24c5c679 14230 else
96408a79 14231 {
24c5c679
JK
14232 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14233 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14234 if (parameter->u.dwarf_reg != -1)
14235 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14236 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14237 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14238 &parameter->u.fb_offset))
14239 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14240 else
14241 {
b98664d3 14242 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14243 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14244 "DW_TAG_call_site child DIE %s "
24c5c679 14245 "[in module %s]"),
9d8780f0 14246 sect_offset_str (child_die->sect_off),
9c541725 14247 objfile_name (objfile));
24c5c679
JK
14248 continue;
14249 }
96408a79
SA
14250 }
14251
216f72a1
JK
14252 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14253 if (attr == NULL)
14254 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14255 if (!attr_form_is_block (attr))
14256 {
b98664d3 14257 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14258 "DW_TAG_call_site child DIE %s [in module %s]"),
14259 sect_offset_str (child_die->sect_off),
9c541725 14260 objfile_name (objfile));
96408a79
SA
14261 continue;
14262 }
14263 parameter->value = DW_BLOCK (attr)->data;
14264 parameter->value_size = DW_BLOCK (attr)->size;
14265
14266 /* Parameters are not pre-cleared by memset above. */
14267 parameter->data_value = NULL;
14268 parameter->data_value_size = 0;
14269 call_site->parameter_count++;
14270
216f72a1
JK
14271 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14272 if (attr == NULL)
14273 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14274 if (attr != nullptr)
96408a79
SA
14275 {
14276 if (!attr_form_is_block (attr))
b98664d3 14277 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14278 "DW_TAG_call_site child DIE %s [in module %s]"),
14279 sect_offset_str (child_die->sect_off),
9c541725 14280 objfile_name (objfile));
96408a79
SA
14281 else
14282 {
14283 parameter->data_value = DW_BLOCK (attr)->data;
14284 parameter->data_value_size = DW_BLOCK (attr)->size;
14285 }
14286 }
14287 }
14288}
14289
71a3c369
TT
14290/* Helper function for read_variable. If DIE represents a virtual
14291 table, then return the type of the concrete object that is
14292 associated with the virtual table. Otherwise, return NULL. */
14293
14294static struct type *
14295rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14296{
14297 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14298 if (attr == NULL)
14299 return NULL;
14300
14301 /* Find the type DIE. */
14302 struct die_info *type_die = NULL;
14303 struct dwarf2_cu *type_cu = cu;
14304
14305 if (attr_form_is_ref (attr))
14306 type_die = follow_die_ref (die, attr, &type_cu);
14307 if (type_die == NULL)
14308 return NULL;
14309
14310 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14311 return NULL;
14312 return die_containing_type (type_die, type_cu);
14313}
14314
14315/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14316
14317static void
14318read_variable (struct die_info *die, struct dwarf2_cu *cu)
14319{
14320 struct rust_vtable_symbol *storage = NULL;
14321
14322 if (cu->language == language_rust)
14323 {
14324 struct type *containing_type = rust_containing_type (die, cu);
14325
14326 if (containing_type != NULL)
14327 {
518817b3 14328 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14329
468c0cbb 14330 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14331 initialize_objfile_symbol (storage);
14332 storage->concrete_type = containing_type;
cf724bc9 14333 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14334 }
14335 }
14336
e4a62c65
TV
14337 struct symbol *res = new_symbol (die, NULL, cu, storage);
14338 struct attribute *abstract_origin
14339 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14340 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14341 if (res == NULL && loc && abstract_origin)
14342 {
14343 /* We have a variable without a name, but with a location and an abstract
14344 origin. This may be a concrete instance of an abstract variable
14345 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14346 later. */
14347 struct dwarf2_cu *origin_cu = cu;
14348 struct die_info *origin_die
14349 = follow_die_ref (die, abstract_origin, &origin_cu);
14350 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14351 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14352 }
71a3c369
TT
14353}
14354
43988095
JK
14355/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14356 reading .debug_rnglists.
14357 Callback's type should be:
14358 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14359 Return true if the attributes are present and valid, otherwise,
14360 return false. */
14361
14362template <typename Callback>
14363static bool
14364dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14365 Callback &&callback)
14366{
ed2dc618 14367 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14368 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14369 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14370 bfd *obfd = objfile->obfd;
43988095
JK
14371 /* Base address selection entry. */
14372 CORE_ADDR base;
14373 int found_base;
43988095 14374 const gdb_byte *buffer;
43988095
JK
14375 CORE_ADDR baseaddr;
14376 bool overflow = false;
14377
14378 found_base = cu->base_known;
14379 base = cu->base_address;
14380
14381 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14382 if (offset >= dwarf2_per_objfile->rnglists.size)
14383 {
b98664d3 14384 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14385 offset);
14386 return false;
14387 }
14388 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14389
14390 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14391
14392 while (1)
14393 {
7814882a
JK
14394 /* Initialize it due to a false compiler warning. */
14395 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14396 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14397 + dwarf2_per_objfile->rnglists.size);
14398 unsigned int bytes_read;
14399
14400 if (buffer == buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14406 switch (rlet)
14407 {
14408 case DW_RLE_end_of_list:
14409 break;
14410 case DW_RLE_base_address:
14411 if (buffer + cu->header.addr_size > buf_end)
14412 {
14413 overflow = true;
14414 break;
14415 }
14416 base = read_address (obfd, buffer, cu, &bytes_read);
14417 found_base = 1;
14418 buffer += bytes_read;
14419 break;
14420 case DW_RLE_start_length:
14421 if (buffer + cu->header.addr_size > buf_end)
14422 {
14423 overflow = true;
14424 break;
14425 }
14426 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14427 buffer += bytes_read;
14428 range_end = (range_beginning
14429 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14430 buffer += bytes_read;
14431 if (buffer > buf_end)
14432 {
14433 overflow = true;
14434 break;
14435 }
14436 break;
14437 case DW_RLE_offset_pair:
14438 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14439 buffer += bytes_read;
14440 if (buffer > buf_end)
14441 {
14442 overflow = true;
14443 break;
14444 }
14445 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14446 buffer += bytes_read;
14447 if (buffer > buf_end)
14448 {
14449 overflow = true;
14450 break;
14451 }
14452 break;
14453 case DW_RLE_start_end:
14454 if (buffer + 2 * cu->header.addr_size > buf_end)
14455 {
14456 overflow = true;
14457 break;
14458 }
14459 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14460 buffer += bytes_read;
14461 range_end = read_address (obfd, buffer, cu, &bytes_read);
14462 buffer += bytes_read;
14463 break;
14464 default:
b98664d3 14465 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14466 return false;
14467 }
14468 if (rlet == DW_RLE_end_of_list || overflow)
14469 break;
14470 if (rlet == DW_RLE_base_address)
14471 continue;
14472
14473 if (!found_base)
14474 {
14475 /* We have no valid base address for the ranges
14476 data. */
b98664d3 14477 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14478 return false;
14479 }
14480
14481 if (range_beginning > range_end)
14482 {
14483 /* Inverted range entries are invalid. */
b98664d3 14484 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14485 return false;
14486 }
14487
14488 /* Empty range entries have no effect. */
14489 if (range_beginning == range_end)
14490 continue;
14491
14492 range_beginning += base;
14493 range_end += base;
14494
14495 /* A not-uncommon case of bad debug info.
14496 Don't pollute the addrmap with bad data. */
14497 if (range_beginning + baseaddr == 0
14498 && !dwarf2_per_objfile->has_section_at_zero)
14499 {
b98664d3 14500 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14501 " [in module %s]"), objfile_name (objfile));
14502 continue;
14503 }
14504
14505 callback (range_beginning, range_end);
14506 }
14507
14508 if (overflow)
14509 {
b98664d3 14510 complaint (_("Offset %d is not terminated "
43988095
JK
14511 "for DW_AT_ranges attribute"),
14512 offset);
14513 return false;
14514 }
14515
14516 return true;
14517}
14518
14519/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14520 Callback's type should be:
14521 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14522 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14523
43988095 14524template <typename Callback>
43039443 14525static int
5f46c5a5 14526dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14527 Callback &&callback)
43039443 14528{
ed2dc618 14529 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14530 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14531 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14532 struct comp_unit_head *cu_header = &cu->header;
14533 bfd *obfd = objfile->obfd;
14534 unsigned int addr_size = cu_header->addr_size;
14535 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14536 /* Base address selection entry. */
14537 CORE_ADDR base;
14538 int found_base;
14539 unsigned int dummy;
d521ce57 14540 const gdb_byte *buffer;
ff013f42 14541 CORE_ADDR baseaddr;
43039443 14542
43988095
JK
14543 if (cu_header->version >= 5)
14544 return dwarf2_rnglists_process (offset, cu, callback);
14545
d00adf39
DE
14546 found_base = cu->base_known;
14547 base = cu->base_address;
43039443 14548
be391dca 14549 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14550 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14551 {
b98664d3 14552 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14553 offset);
14554 return 0;
14555 }
dce234bc 14556 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14557
e7030f15 14558 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14559
43039443
JK
14560 while (1)
14561 {
14562 CORE_ADDR range_beginning, range_end;
14563
14564 range_beginning = read_address (obfd, buffer, cu, &dummy);
14565 buffer += addr_size;
14566 range_end = read_address (obfd, buffer, cu, &dummy);
14567 buffer += addr_size;
14568 offset += 2 * addr_size;
14569
14570 /* An end of list marker is a pair of zero addresses. */
14571 if (range_beginning == 0 && range_end == 0)
14572 /* Found the end of list entry. */
14573 break;
14574
14575 /* Each base address selection entry is a pair of 2 values.
14576 The first is the largest possible address, the second is
14577 the base address. Check for a base address here. */
14578 if ((range_beginning & mask) == mask)
14579 {
28d2bfb9
AB
14580 /* If we found the largest possible address, then we already
14581 have the base address in range_end. */
14582 base = range_end;
43039443
JK
14583 found_base = 1;
14584 continue;
14585 }
14586
14587 if (!found_base)
14588 {
14589 /* We have no valid base address for the ranges
14590 data. */
b98664d3 14591 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14592 return 0;
14593 }
14594
9277c30c
UW
14595 if (range_beginning > range_end)
14596 {
14597 /* Inverted range entries are invalid. */
b98664d3 14598 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14599 return 0;
14600 }
14601
14602 /* Empty range entries have no effect. */
14603 if (range_beginning == range_end)
14604 continue;
14605
43039443
JK
14606 range_beginning += base;
14607 range_end += base;
14608
01093045
DE
14609 /* A not-uncommon case of bad debug info.
14610 Don't pollute the addrmap with bad data. */
14611 if (range_beginning + baseaddr == 0
14612 && !dwarf2_per_objfile->has_section_at_zero)
14613 {
b98664d3 14614 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14615 " [in module %s]"), objfile_name (objfile));
01093045
DE
14616 continue;
14617 }
14618
5f46c5a5
JK
14619 callback (range_beginning, range_end);
14620 }
14621
14622 return 1;
14623}
14624
14625/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14626 Return 1 if the attributes are present and valid, otherwise, return 0.
14627 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14628
14629static int
14630dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14631 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14632 struct partial_symtab *ranges_pst)
14633{
518817b3 14634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14636 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14637 SECT_OFF_TEXT (objfile));
14638 int low_set = 0;
14639 CORE_ADDR low = 0;
14640 CORE_ADDR high = 0;
14641 int retval;
14642
14643 retval = dwarf2_ranges_process (offset, cu,
14644 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14645 {
9277c30c 14646 if (ranges_pst != NULL)
3e29f34a
MR
14647 {
14648 CORE_ADDR lowpc;
14649 CORE_ADDR highpc;
14650
79748972
TT
14651 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14652 range_beginning + baseaddr)
14653 - baseaddr);
14654 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14655 range_end + baseaddr)
14656 - baseaddr);
d320c2b5
TT
14657 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14658 lowpc, highpc - 1, ranges_pst);
3e29f34a 14659 }
ff013f42 14660
43039443
JK
14661 /* FIXME: This is recording everything as a low-high
14662 segment of consecutive addresses. We should have a
14663 data structure for discontiguous block ranges
14664 instead. */
14665 if (! low_set)
14666 {
14667 low = range_beginning;
14668 high = range_end;
14669 low_set = 1;
14670 }
14671 else
14672 {
14673 if (range_beginning < low)
14674 low = range_beginning;
14675 if (range_end > high)
14676 high = range_end;
14677 }
5f46c5a5
JK
14678 });
14679 if (!retval)
14680 return 0;
43039443
JK
14681
14682 if (! low_set)
14683 /* If the first entry is an end-of-list marker, the range
14684 describes an empty scope, i.e. no instructions. */
14685 return 0;
14686
14687 if (low_return)
14688 *low_return = low;
14689 if (high_return)
14690 *high_return = high;
14691 return 1;
14692}
14693
3a2b436a
JK
14694/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14695 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14696 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14697
3a2b436a 14698static enum pc_bounds_kind
af34e669 14699dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14700 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14701 struct partial_symtab *pst)
c906108c 14702{
518817b3
SM
14703 struct dwarf2_per_objfile *dwarf2_per_objfile
14704 = cu->per_cu->dwarf2_per_objfile;
c906108c 14705 struct attribute *attr;
91da1414 14706 struct attribute *attr_high;
af34e669
DJ
14707 CORE_ADDR low = 0;
14708 CORE_ADDR high = 0;
e385593e 14709 enum pc_bounds_kind ret;
c906108c 14710
91da1414
MW
14711 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14712 if (attr_high)
af34e669 14713 {
e142c38c 14714 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14715 if (attr != nullptr)
91da1414 14716 {
31aa7e4e
JB
14717 low = attr_value_as_address (attr);
14718 high = attr_value_as_address (attr_high);
14719 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14720 high += low;
91da1414 14721 }
af34e669
DJ
14722 else
14723 /* Found high w/o low attribute. */
e385593e 14724 return PC_BOUNDS_INVALID;
af34e669
DJ
14725
14726 /* Found consecutive range of addresses. */
3a2b436a 14727 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14728 }
c906108c 14729 else
af34e669 14730 {
e142c38c 14731 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14732 if (attr != NULL)
14733 {
ab435259
DE
14734 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14735 We take advantage of the fact that DW_AT_ranges does not appear
14736 in DW_TAG_compile_unit of DWO files. */
14737 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14738 unsigned int ranges_offset = (DW_UNSND (attr)
14739 + (need_ranges_base
14740 ? cu->ranges_base
14741 : 0));
2e3cf129 14742
af34e669 14743 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14744 .debug_ranges section. */
2e3cf129 14745 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14746 return PC_BOUNDS_INVALID;
43039443 14747 /* Found discontinuous range of addresses. */
3a2b436a 14748 ret = PC_BOUNDS_RANGES;
af34e669 14749 }
e385593e
JK
14750 else
14751 return PC_BOUNDS_NOT_PRESENT;
af34e669 14752 }
c906108c 14753
48fbe735 14754 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14755 if (high <= low)
e385593e 14756 return PC_BOUNDS_INVALID;
c906108c
SS
14757
14758 /* When using the GNU linker, .gnu.linkonce. sections are used to
14759 eliminate duplicate copies of functions and vtables and such.
14760 The linker will arbitrarily choose one and discard the others.
14761 The AT_*_pc values for such functions refer to local labels in
14762 these sections. If the section from that file was discarded, the
14763 labels are not in the output, so the relocs get a value of 0.
14764 If this is a discarded function, mark the pc bounds as invalid,
14765 so that GDB will ignore it. */
72dca2f5 14766 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14767 return PC_BOUNDS_INVALID;
c906108c
SS
14768
14769 *lowpc = low;
96408a79
SA
14770 if (highpc)
14771 *highpc = high;
af34e669 14772 return ret;
c906108c
SS
14773}
14774
b084d499
JB
14775/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14776 its low and high PC addresses. Do nothing if these addresses could not
14777 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14778 and HIGHPC to the high address if greater than HIGHPC. */
14779
14780static void
14781dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14782 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14783 struct dwarf2_cu *cu)
14784{
14785 CORE_ADDR low, high;
14786 struct die_info *child = die->child;
14787
e385593e 14788 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14789 {
325fac50
PA
14790 *lowpc = std::min (*lowpc, low);
14791 *highpc = std::max (*highpc, high);
b084d499
JB
14792 }
14793
14794 /* If the language does not allow nested subprograms (either inside
14795 subprograms or lexical blocks), we're done. */
14796 if (cu->language != language_ada)
14797 return;
6e70227d 14798
b084d499
JB
14799 /* Check all the children of the given DIE. If it contains nested
14800 subprograms, then check their pc bounds. Likewise, we need to
14801 check lexical blocks as well, as they may also contain subprogram
14802 definitions. */
14803 while (child && child->tag)
14804 {
14805 if (child->tag == DW_TAG_subprogram
14806 || child->tag == DW_TAG_lexical_block)
14807 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14808 child = sibling_die (child);
14809 }
14810}
14811
fae299cd
DC
14812/* Get the low and high pc's represented by the scope DIE, and store
14813 them in *LOWPC and *HIGHPC. If the correct values can't be
14814 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14815
14816static void
14817get_scope_pc_bounds (struct die_info *die,
14818 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14819 struct dwarf2_cu *cu)
14820{
14821 CORE_ADDR best_low = (CORE_ADDR) -1;
14822 CORE_ADDR best_high = (CORE_ADDR) 0;
14823 CORE_ADDR current_low, current_high;
14824
3a2b436a 14825 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14826 >= PC_BOUNDS_RANGES)
fae299cd
DC
14827 {
14828 best_low = current_low;
14829 best_high = current_high;
14830 }
14831 else
14832 {
14833 struct die_info *child = die->child;
14834
14835 while (child && child->tag)
14836 {
14837 switch (child->tag) {
14838 case DW_TAG_subprogram:
b084d499 14839 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14840 break;
14841 case DW_TAG_namespace:
f55ee35c 14842 case DW_TAG_module:
fae299cd
DC
14843 /* FIXME: carlton/2004-01-16: Should we do this for
14844 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14845 that current GCC's always emit the DIEs corresponding
14846 to definitions of methods of classes as children of a
14847 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14848 the DIEs giving the declarations, which could be
14849 anywhere). But I don't see any reason why the
14850 standards says that they have to be there. */
14851 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14852
14853 if (current_low != ((CORE_ADDR) -1))
14854 {
325fac50
PA
14855 best_low = std::min (best_low, current_low);
14856 best_high = std::max (best_high, current_high);
fae299cd
DC
14857 }
14858 break;
14859 default:
0963b4bd 14860 /* Ignore. */
fae299cd
DC
14861 break;
14862 }
14863
14864 child = sibling_die (child);
14865 }
14866 }
14867
14868 *lowpc = best_low;
14869 *highpc = best_high;
14870}
14871
801e3a5b
JB
14872/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14873 in DIE. */
380bca97 14874
801e3a5b
JB
14875static void
14876dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14877 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14878{
518817b3 14879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14880 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14881 struct attribute *attr;
91da1414 14882 struct attribute *attr_high;
801e3a5b 14883
91da1414
MW
14884 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14885 if (attr_high)
801e3a5b 14886 {
801e3a5b 14887 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14888 if (attr != nullptr)
801e3a5b 14889 {
31aa7e4e
JB
14890 CORE_ADDR low = attr_value_as_address (attr);
14891 CORE_ADDR high = attr_value_as_address (attr_high);
14892
14893 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14894 high += low;
9a619af0 14895
3e29f34a
MR
14896 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14897 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14898 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14899 }
14900 }
14901
14902 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14903 if (attr != nullptr)
801e3a5b 14904 {
ab435259
DE
14905 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14906 We take advantage of the fact that DW_AT_ranges does not appear
14907 in DW_TAG_compile_unit of DWO files. */
14908 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14909
14910 /* The value of the DW_AT_ranges attribute is the offset of the
14911 address range list in the .debug_ranges section. */
ab435259
DE
14912 unsigned long offset = (DW_UNSND (attr)
14913 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14914
2d5f09ec 14915 std::vector<blockrange> blockvec;
5f46c5a5
JK
14916 dwarf2_ranges_process (offset, cu,
14917 [&] (CORE_ADDR start, CORE_ADDR end)
14918 {
58fdfd2c
JK
14919 start += baseaddr;
14920 end += baseaddr;
5f46c5a5
JK
14921 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14922 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14923 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14924 blockvec.emplace_back (start, end);
5f46c5a5 14925 });
2d5f09ec
KB
14926
14927 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14928 }
14929}
14930
685b1105
JK
14931/* Check whether the producer field indicates either of GCC < 4.6, or the
14932 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14933
685b1105
JK
14934static void
14935check_producer (struct dwarf2_cu *cu)
60d5a603 14936{
38360086 14937 int major, minor;
60d5a603
JK
14938
14939 if (cu->producer == NULL)
14940 {
14941 /* For unknown compilers expect their behavior is DWARF version
14942 compliant.
14943
14944 GCC started to support .debug_types sections by -gdwarf-4 since
14945 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14946 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14947 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14948 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14949 }
b1ffba5a 14950 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14951 {
38360086
MW
14952 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14953 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14954 }
5230b05a 14955 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14956 {
14957 cu->producer_is_icc = true;
14958 cu->producer_is_icc_lt_14 = major < 14;
14959 }
c258c396
JD
14960 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14961 cu->producer_is_codewarrior = true;
685b1105
JK
14962 else
14963 {
14964 /* For other non-GCC compilers, expect their behavior is DWARF version
14965 compliant. */
60d5a603
JK
14966 }
14967
9068261f 14968 cu->checked_producer = true;
685b1105 14969}
ba919b58 14970
685b1105
JK
14971/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14972 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14973 during 4.6.0 experimental. */
14974
9068261f 14975static bool
685b1105
JK
14976producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14977{
14978 if (!cu->checked_producer)
14979 check_producer (cu);
14980
14981 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14982}
14983
c258c396
JD
14984
14985/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14986 with incorrect is_stmt attributes. */
14987
14988static bool
14989producer_is_codewarrior (struct dwarf2_cu *cu)
14990{
14991 if (!cu->checked_producer)
14992 check_producer (cu);
14993
14994 return cu->producer_is_codewarrior;
14995}
14996
405feb71 14997/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14998 DW_AT_accessibility. */
14999
15000static enum dwarf_access_attribute
15001dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
15002{
15003 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15004 {
15005 /* The default DWARF 2 accessibility for members is public, the default
15006 accessibility for inheritance is private. */
15007
15008 if (die->tag != DW_TAG_inheritance)
15009 return DW_ACCESS_public;
15010 else
15011 return DW_ACCESS_private;
15012 }
15013 else
15014 {
15015 /* DWARF 3+ defines the default accessibility a different way. The same
15016 rules apply now for DW_TAG_inheritance as for the members and it only
15017 depends on the container kind. */
15018
15019 if (die->parent->tag == DW_TAG_class_type)
15020 return DW_ACCESS_private;
15021 else
15022 return DW_ACCESS_public;
15023 }
15024}
15025
74ac6d43
TT
15026/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15027 offset. If the attribute was not found return 0, otherwise return
15028 1. If it was found but could not properly be handled, set *OFFSET
15029 to 0. */
15030
15031static int
15032handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15033 LONGEST *offset)
15034{
15035 struct attribute *attr;
15036
15037 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15038 if (attr != NULL)
15039 {
15040 *offset = 0;
15041
15042 /* Note that we do not check for a section offset first here.
15043 This is because DW_AT_data_member_location is new in DWARF 4,
15044 so if we see it, we can assume that a constant form is really
15045 a constant and not a section offset. */
15046 if (attr_form_is_constant (attr))
15047 *offset = dwarf2_get_attr_constant_value (attr, 0);
15048 else if (attr_form_is_section_offset (attr))
15049 dwarf2_complex_location_expr_complaint ();
15050 else if (attr_form_is_block (attr))
15051 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15052 else
15053 dwarf2_complex_location_expr_complaint ();
15054
15055 return 1;
15056 }
15057
15058 return 0;
15059}
15060
c906108c
SS
15061/* Add an aggregate field to the field list. */
15062
15063static void
107d2387 15064dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15065 struct dwarf2_cu *cu)
6e70227d 15066{
518817b3 15067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15068 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15069 struct nextfield *new_field;
15070 struct attribute *attr;
15071 struct field *fp;
15d034d0 15072 const char *fieldname = "";
c906108c 15073
7d0ccb61
DJ
15074 if (die->tag == DW_TAG_inheritance)
15075 {
be2daae6
TT
15076 fip->baseclasses.emplace_back ();
15077 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15078 }
15079 else
15080 {
be2daae6
TT
15081 fip->fields.emplace_back ();
15082 new_field = &fip->fields.back ();
7d0ccb61 15083 }
be2daae6 15084
c906108c
SS
15085 fip->nfields++;
15086
e142c38c 15087 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15088 if (attr != nullptr)
c906108c 15089 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15090 else
15091 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15092 if (new_field->accessibility != DW_ACCESS_public)
15093 fip->non_public_fields = 1;
60d5a603 15094
e142c38c 15095 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15096 if (attr != nullptr)
c906108c 15097 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15098 else
15099 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15100
15101 fp = &new_field->field;
a9a9bd0f 15102
e142c38c 15103 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15104 {
74ac6d43
TT
15105 LONGEST offset;
15106
a9a9bd0f 15107 /* Data member other than a C++ static data member. */
6e70227d 15108
c906108c 15109 /* Get type of field. */
e7c27a73 15110 fp->type = die_type (die, cu);
c906108c 15111
d6a843b5 15112 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15113
c906108c 15114 /* Get bit size of field (zero if none). */
e142c38c 15115 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15116 if (attr != nullptr)
c906108c
SS
15117 {
15118 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15119 }
15120 else
15121 {
15122 FIELD_BITSIZE (*fp) = 0;
15123 }
15124
15125 /* Get bit offset of field. */
74ac6d43
TT
15126 if (handle_data_member_location (die, cu, &offset))
15127 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15128 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15129 if (attr != nullptr)
c906108c 15130 {
d5a22e77 15131 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15132 {
15133 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15134 additional bit offset from the MSB of the containing
15135 anonymous object to the MSB of the field. We don't
15136 have to do anything special since we don't need to
15137 know the size of the anonymous object. */
f41f5e61 15138 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15139 }
15140 else
15141 {
15142 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15143 MSB of the anonymous object, subtract off the number of
15144 bits from the MSB of the field to the MSB of the
15145 object, and then subtract off the number of bits of
15146 the field itself. The result is the bit offset of
15147 the LSB of the field. */
c906108c
SS
15148 int anonymous_size;
15149 int bit_offset = DW_UNSND (attr);
15150
e142c38c 15151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15152 if (attr != nullptr)
c906108c
SS
15153 {
15154 /* The size of the anonymous object containing
15155 the bit field is explicit, so use the
15156 indicated size (in bytes). */
15157 anonymous_size = DW_UNSND (attr);
15158 }
15159 else
15160 {
15161 /* The size of the anonymous object containing
15162 the bit field must be inferred from the type
15163 attribute of the data member containing the
15164 bit field. */
15165 anonymous_size = TYPE_LENGTH (fp->type);
15166 }
f41f5e61
PA
15167 SET_FIELD_BITPOS (*fp,
15168 (FIELD_BITPOS (*fp)
15169 + anonymous_size * bits_per_byte
15170 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15171 }
15172 }
da5b30da
AA
15173 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15174 if (attr != NULL)
15175 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15176 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15177
15178 /* Get name of field. */
39cbfefa
DJ
15179 fieldname = dwarf2_name (die, cu);
15180 if (fieldname == NULL)
15181 fieldname = "";
d8151005
DJ
15182
15183 /* The name is already allocated along with this objfile, so we don't
15184 need to duplicate it for the type. */
15185 fp->name = fieldname;
c906108c
SS
15186
15187 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15188 pointer or virtual base class pointer) to private. */
e142c38c 15189 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15190 {
d48cc9dd 15191 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15192 new_field->accessibility = DW_ACCESS_private;
15193 fip->non_public_fields = 1;
15194 }
15195 }
a9a9bd0f 15196 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15197 {
a9a9bd0f
DC
15198 /* C++ static member. */
15199
15200 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15201 is a declaration, but all versions of G++ as of this writing
15202 (so through at least 3.2.1) incorrectly generate
15203 DW_TAG_variable tags. */
6e70227d 15204
ff355380 15205 const char *physname;
c906108c 15206
a9a9bd0f 15207 /* Get name of field. */
39cbfefa
DJ
15208 fieldname = dwarf2_name (die, cu);
15209 if (fieldname == NULL)
c906108c
SS
15210 return;
15211
254e6b9e 15212 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15213 if (attr
15214 /* Only create a symbol if this is an external value.
15215 new_symbol checks this and puts the value in the global symbol
15216 table, which we want. If it is not external, new_symbol
15217 will try to put the value in cu->list_in_scope which is wrong. */
15218 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15219 {
15220 /* A static const member, not much different than an enum as far as
15221 we're concerned, except that we can support more types. */
15222 new_symbol (die, NULL, cu);
15223 }
15224
2df3850c 15225 /* Get physical name. */
ff355380 15226 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15227
d8151005
DJ
15228 /* The name is already allocated along with this objfile, so we don't
15229 need to duplicate it for the type. */
15230 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15231 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15232 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15233 }
15234 else if (die->tag == DW_TAG_inheritance)
15235 {
74ac6d43 15236 LONGEST offset;
d4b96c9a 15237
74ac6d43
TT
15238 /* C++ base class field. */
15239 if (handle_data_member_location (die, cu, &offset))
15240 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15241 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15242 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15243 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15244 }
2ddeaf8a
TT
15245 else if (die->tag == DW_TAG_variant_part)
15246 {
15247 /* process_structure_scope will treat this DIE as a union. */
15248 process_structure_scope (die, cu);
15249
15250 /* The variant part is relative to the start of the enclosing
15251 structure. */
15252 SET_FIELD_BITPOS (*fp, 0);
15253 fp->type = get_die_type (die, cu);
15254 fp->artificial = 1;
15255 fp->name = "<<variant>>";
c8c81635
TT
15256
15257 /* Normally a DW_TAG_variant_part won't have a size, but our
15258 representation requires one, so set it to the maximum of the
489dbda6
TT
15259 child sizes, being sure to account for the offset at which
15260 each child is seen. */
c8c81635
TT
15261 if (TYPE_LENGTH (fp->type) == 0)
15262 {
15263 unsigned max = 0;
15264 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15265 {
15266 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15267 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15268 if (len > max)
15269 max = len;
15270 }
c8c81635
TT
15271 TYPE_LENGTH (fp->type) = max;
15272 }
2ddeaf8a
TT
15273 }
15274 else
15275 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15276}
15277
883fd55a
KS
15278/* Can the type given by DIE define another type? */
15279
15280static bool
15281type_can_define_types (const struct die_info *die)
15282{
15283 switch (die->tag)
15284 {
15285 case DW_TAG_typedef:
15286 case DW_TAG_class_type:
15287 case DW_TAG_structure_type:
15288 case DW_TAG_union_type:
15289 case DW_TAG_enumeration_type:
15290 return true;
15291
15292 default:
15293 return false;
15294 }
15295}
15296
15297/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15298
15299static void
883fd55a
KS
15300dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15301 struct dwarf2_cu *cu)
6e70227d 15302{
be2daae6
TT
15303 struct decl_field fp;
15304 memset (&fp, 0, sizeof (fp));
98751a41 15305
883fd55a 15306 gdb_assert (type_can_define_types (die));
98751a41 15307
883fd55a 15308 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15309 fp.name = dwarf2_name (die, cu);
15310 fp.type = read_type_die (die, cu);
98751a41 15311
c191a687
KS
15312 /* Save accessibility. */
15313 enum dwarf_access_attribute accessibility;
15314 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15315 if (attr != NULL)
15316 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15317 else
15318 accessibility = dwarf2_default_access_attribute (die, cu);
15319 switch (accessibility)
15320 {
15321 case DW_ACCESS_public:
15322 /* The assumed value if neither private nor protected. */
15323 break;
15324 case DW_ACCESS_private:
be2daae6 15325 fp.is_private = 1;
c191a687
KS
15326 break;
15327 case DW_ACCESS_protected:
be2daae6 15328 fp.is_protected = 1;
c191a687
KS
15329 break;
15330 default:
b98664d3 15331 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15332 }
15333
883fd55a 15334 if (die->tag == DW_TAG_typedef)
be2daae6 15335 fip->typedef_field_list.push_back (fp);
883fd55a 15336 else
be2daae6 15337 fip->nested_types_list.push_back (fp);
98751a41
JK
15338}
15339
c906108c
SS
15340/* Create the vector of fields, and attach it to the type. */
15341
15342static void
fba45db2 15343dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15344 struct dwarf2_cu *cu)
c906108c
SS
15345{
15346 int nfields = fip->nfields;
15347
15348 /* Record the field count, allocate space for the array of fields,
15349 and create blank accessibility bitfields if necessary. */
15350 TYPE_NFIELDS (type) = nfields;
15351 TYPE_FIELDS (type) = (struct field *)
be2daae6 15352 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15353
b4ba55a1 15354 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15355 {
15356 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15357
15358 TYPE_FIELD_PRIVATE_BITS (type) =
15359 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15360 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15361
15362 TYPE_FIELD_PROTECTED_BITS (type) =
15363 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15364 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15365
774b6a14
TT
15366 TYPE_FIELD_IGNORE_BITS (type) =
15367 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15368 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15369 }
15370
15371 /* If the type has baseclasses, allocate and clear a bit vector for
15372 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15373 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15374 {
be2daae6 15375 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15376 unsigned char *pointer;
c906108c
SS
15377
15378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15379 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15380 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15381 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15382 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15383 }
15384
2ddeaf8a
TT
15385 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15386 {
15387 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15388
be2daae6 15389 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15390 {
be2daae6
TT
15391 struct nextfield &field = fip->fields[index];
15392
15393 if (field.variant.is_discriminant)
2ddeaf8a 15394 di->discriminant_index = index;
be2daae6 15395 else if (field.variant.default_branch)
2ddeaf8a
TT
15396 di->default_index = index;
15397 else
be2daae6 15398 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15399 }
15400 }
15401
be2daae6
TT
15402 /* Copy the saved-up fields into the field vector. */
15403 for (int i = 0; i < nfields; ++i)
c906108c 15404 {
be2daae6
TT
15405 struct nextfield &field
15406 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15407 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15408
be2daae6
TT
15409 TYPE_FIELD (type, i) = field.field;
15410 switch (field.accessibility)
c906108c 15411 {
c5aa993b 15412 case DW_ACCESS_private:
b4ba55a1 15413 if (cu->language != language_ada)
be2daae6 15414 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15415 break;
c906108c 15416
c5aa993b 15417 case DW_ACCESS_protected:
b4ba55a1 15418 if (cu->language != language_ada)
be2daae6 15419 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15420 break;
c906108c 15421
c5aa993b
JM
15422 case DW_ACCESS_public:
15423 break;
c906108c 15424
c5aa993b
JM
15425 default:
15426 /* Unknown accessibility. Complain and treat it as public. */
15427 {
b98664d3 15428 complaint (_("unsupported accessibility %d"),
be2daae6 15429 field.accessibility);
c5aa993b
JM
15430 }
15431 break;
c906108c 15432 }
be2daae6 15433 if (i < fip->baseclasses.size ())
c906108c 15434 {
be2daae6 15435 switch (field.virtuality)
c906108c 15436 {
c5aa993b
JM
15437 case DW_VIRTUALITY_virtual:
15438 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15439 if (cu->language == language_ada)
a73c6dcd 15440 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15441 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15442 break;
c906108c
SS
15443 }
15444 }
c906108c
SS
15445 }
15446}
15447
7d27a96d
TT
15448/* Return true if this member function is a constructor, false
15449 otherwise. */
15450
15451static int
15452dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15453{
15454 const char *fieldname;
fe978cb0 15455 const char *type_name;
7d27a96d
TT
15456 int len;
15457
15458 if (die->parent == NULL)
15459 return 0;
15460
15461 if (die->parent->tag != DW_TAG_structure_type
15462 && die->parent->tag != DW_TAG_union_type
15463 && die->parent->tag != DW_TAG_class_type)
15464 return 0;
15465
15466 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15467 type_name = dwarf2_name (die->parent, cu);
15468 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15469 return 0;
15470
15471 len = strlen (fieldname);
fe978cb0
PA
15472 return (strncmp (fieldname, type_name, len) == 0
15473 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15474}
15475
c906108c
SS
15476/* Add a member function to the proper fieldlist. */
15477
15478static void
107d2387 15479dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15480 struct type *type, struct dwarf2_cu *cu)
c906108c 15481{
518817b3 15482 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15483 struct attribute *attr;
c906108c 15484 int i;
be2daae6 15485 struct fnfieldlist *flp = nullptr;
c906108c 15486 struct fn_field *fnp;
15d034d0 15487 const char *fieldname;
f792889a 15488 struct type *this_type;
60d5a603 15489 enum dwarf_access_attribute accessibility;
c906108c 15490
b4ba55a1 15491 if (cu->language == language_ada)
a73c6dcd 15492 error (_("unexpected member function in Ada type"));
b4ba55a1 15493
2df3850c 15494 /* Get name of member function. */
39cbfefa
DJ
15495 fieldname = dwarf2_name (die, cu);
15496 if (fieldname == NULL)
2df3850c 15497 return;
c906108c 15498
c906108c 15499 /* Look up member function name in fieldlist. */
be2daae6 15500 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15501 {
27bfe10e 15502 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15503 {
15504 flp = &fip->fnfieldlists[i];
15505 break;
15506 }
c906108c
SS
15507 }
15508
be2daae6
TT
15509 /* Create a new fnfieldlist if necessary. */
15510 if (flp == nullptr)
c906108c 15511 {
be2daae6
TT
15512 fip->fnfieldlists.emplace_back ();
15513 flp = &fip->fnfieldlists.back ();
c906108c 15514 flp->name = fieldname;
be2daae6 15515 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15516 }
15517
be2daae6
TT
15518 /* Create a new member function field and add it to the vector of
15519 fnfieldlists. */
15520 flp->fnfields.emplace_back ();
15521 fnp = &flp->fnfields.back ();
3da10d80
KS
15522
15523 /* Delay processing of the physname until later. */
9c37b5ae 15524 if (cu->language == language_cplus)
be2daae6
TT
15525 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15526 die, cu);
3da10d80
KS
15527 else
15528 {
1d06ead6 15529 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15530 fnp->physname = physname ? physname : "";
15531 }
15532
c906108c 15533 fnp->type = alloc_type (objfile);
f792889a
DJ
15534 this_type = read_type_die (die, cu);
15535 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15536 {
f792889a 15537 int nparams = TYPE_NFIELDS (this_type);
c906108c 15538
f792889a 15539 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15540 of the method itself (TYPE_CODE_METHOD). */
15541 smash_to_method_type (fnp->type, type,
f792889a
DJ
15542 TYPE_TARGET_TYPE (this_type),
15543 TYPE_FIELDS (this_type),
15544 TYPE_NFIELDS (this_type),
15545 TYPE_VARARGS (this_type));
c906108c
SS
15546
15547 /* Handle static member functions.
c5aa993b 15548 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15549 member functions. G++ helps GDB by marking the first
15550 parameter for non-static member functions (which is the this
15551 pointer) as artificial. We obtain this information from
15552 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15553 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15554 fnp->voffset = VOFFSET_STATIC;
15555 }
15556 else
b98664d3 15557 complaint (_("member function type missing for '%s'"),
3da10d80 15558 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15559
15560 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15561 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15562 fnp->fcontext = die_containing_type (die, cu);
c906108c 15563
3e43a32a
MS
15564 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15565 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15566
15567 /* Get accessibility. */
e142c38c 15568 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15569 if (attr != nullptr)
aead7601 15570 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15571 else
15572 accessibility = dwarf2_default_access_attribute (die, cu);
15573 switch (accessibility)
c906108c 15574 {
60d5a603
JK
15575 case DW_ACCESS_private:
15576 fnp->is_private = 1;
15577 break;
15578 case DW_ACCESS_protected:
15579 fnp->is_protected = 1;
15580 break;
c906108c
SS
15581 }
15582
b02dede2 15583 /* Check for artificial methods. */
e142c38c 15584 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15585 if (attr && DW_UNSND (attr) != 0)
15586 fnp->is_artificial = 1;
15587
7d27a96d
TT
15588 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15589
0d564a31 15590 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15591 function. For older versions of GCC, this is an offset in the
15592 appropriate virtual table, as specified by DW_AT_containing_type.
15593 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15594 to the object address. */
15595
e142c38c 15596 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15597 if (attr != nullptr)
8e19ed76 15598 {
aec5aa8b 15599 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15600 {
aec5aa8b
TT
15601 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15602 {
15603 /* Old-style GCC. */
15604 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15605 }
15606 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15607 || (DW_BLOCK (attr)->size > 1
15608 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15609 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15610 {
aec5aa8b
TT
15611 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15612 if ((fnp->voffset % cu->header.addr_size) != 0)
15613 dwarf2_complex_location_expr_complaint ();
15614 else
15615 fnp->voffset /= cu->header.addr_size;
15616 fnp->voffset += 2;
15617 }
15618 else
15619 dwarf2_complex_location_expr_complaint ();
15620
15621 if (!fnp->fcontext)
7e993ebf
KS
15622 {
15623 /* If there is no `this' field and no DW_AT_containing_type,
15624 we cannot actually find a base class context for the
15625 vtable! */
15626 if (TYPE_NFIELDS (this_type) == 0
15627 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15628 {
b98664d3 15629 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15630 "function \"%s\" (offset %s)"),
15631 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15632 }
15633 else
15634 {
15635 fnp->fcontext
15636 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15637 }
15638 }
aec5aa8b 15639 }
3690dd37 15640 else if (attr_form_is_section_offset (attr))
8e19ed76 15641 {
4d3c2250 15642 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15643 }
15644 else
15645 {
4d3c2250
KB
15646 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15647 fieldname);
8e19ed76 15648 }
0d564a31 15649 }
d48cc9dd
DJ
15650 else
15651 {
15652 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15653 if (attr && DW_UNSND (attr))
15654 {
15655 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15656 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15657 "but the vtable offset is not specified"),
9d8780f0 15658 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15659 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15660 TYPE_CPLUS_DYNAMIC (type) = 1;
15661 }
15662 }
c906108c
SS
15663}
15664
15665/* Create the vector of member function fields, and attach it to the type. */
15666
15667static void
fba45db2 15668dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15669 struct dwarf2_cu *cu)
c906108c 15670{
b4ba55a1 15671 if (cu->language == language_ada)
a73c6dcd 15672 error (_("unexpected member functions in Ada type"));
b4ba55a1 15673
c906108c
SS
15674 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15675 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15676 TYPE_ALLOC (type,
15677 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15678
be2daae6 15679 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15680 {
be2daae6 15681 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15682 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15683
be2daae6
TT
15684 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15685 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15686 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15687 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15688
15689 for (int k = 0; k < nf.fnfields.size (); ++k)
15690 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15691 }
15692
be2daae6 15693 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15694}
15695
1168df01
JB
15696/* Returns non-zero if NAME is the name of a vtable member in CU's
15697 language, zero otherwise. */
15698static int
15699is_vtable_name (const char *name, struct dwarf2_cu *cu)
15700{
15701 static const char vptr[] = "_vptr";
15702
9c37b5ae
TT
15703 /* Look for the C++ form of the vtable. */
15704 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15705 return 1;
15706
15707 return 0;
15708}
15709
c0dd20ea 15710/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15711 functions, with the ABI-specified layout. If TYPE describes
15712 such a structure, smash it into a member function type.
61049d3b
DJ
15713
15714 GCC shouldn't do this; it should just output pointer to member DIEs.
15715 This is GCC PR debug/28767. */
c0dd20ea 15716
0b92b5bb
TT
15717static void
15718quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15719{
09e2d7c7 15720 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15721
15722 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15723 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15724 return;
c0dd20ea
DJ
15725
15726 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15727 if (TYPE_FIELD_NAME (type, 0) == NULL
15728 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15729 || TYPE_FIELD_NAME (type, 1) == NULL
15730 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15731 return;
c0dd20ea
DJ
15732
15733 /* Find the type of the method. */
0b92b5bb 15734 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15735 if (pfn_type == NULL
15736 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15737 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15738 return;
c0dd20ea
DJ
15739
15740 /* Look for the "this" argument. */
15741 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15742 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15743 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15744 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15745 return;
c0dd20ea 15746
09e2d7c7 15747 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15748 new_type = alloc_type (objfile);
09e2d7c7 15749 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15750 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15751 TYPE_VARARGS (pfn_type));
0b92b5bb 15752 smash_to_methodptr_type (type, new_type);
c0dd20ea 15753}
1168df01 15754
2b4424c3
TT
15755/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15756 appropriate error checking and issuing complaints if there is a
15757 problem. */
15758
15759static ULONGEST
15760get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15761{
15762 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15763
15764 if (attr == nullptr)
15765 return 0;
15766
15767 if (!attr_form_is_constant (attr))
15768 {
b98664d3 15769 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15770 " - DIE at %s [in module %s]"),
15771 sect_offset_str (die->sect_off),
15772 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15773 return 0;
15774 }
15775
15776 ULONGEST align;
15777 if (attr->form == DW_FORM_sdata)
15778 {
15779 LONGEST val = DW_SND (attr);
15780 if (val < 0)
15781 {
b98664d3 15782 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15783 " - DIE at %s [in module %s]"),
15784 sect_offset_str (die->sect_off),
15785 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15786 return 0;
15787 }
15788 align = val;
15789 }
15790 else
15791 align = DW_UNSND (attr);
15792
15793 if (align == 0)
15794 {
b98664d3 15795 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15796 " - DIE at %s [in module %s]"),
15797 sect_offset_str (die->sect_off),
15798 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15799 return 0;
15800 }
15801 if ((align & (align - 1)) != 0)
15802 {
b98664d3 15803 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15804 " - DIE at %s [in module %s]"),
15805 sect_offset_str (die->sect_off),
15806 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15807 return 0;
15808 }
15809
15810 return align;
15811}
15812
15813/* If the DIE has a DW_AT_alignment attribute, use its value to set
15814 the alignment for TYPE. */
15815
15816static void
15817maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15818 struct type *type)
15819{
15820 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15821 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15822 " - DIE at %s [in module %s]"),
15823 sect_offset_str (die->sect_off),
15824 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15825}
685b1105 15826
c906108c 15827/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15828 (definition) to create a type for the structure or union. Fill in
15829 the type's name and general properties; the members will not be
83655187
DE
15830 processed until process_structure_scope. A symbol table entry for
15831 the type will also not be done until process_structure_scope (assuming
15832 the type has a name).
c906108c 15833
c767944b
DJ
15834 NOTE: we need to call these functions regardless of whether or not the
15835 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15836 structure or union. This gets the type entered into our set of
83655187 15837 user defined types. */
c906108c 15838
f792889a 15839static struct type *
134d01f1 15840read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15841{
518817b3 15842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15843 struct type *type;
15844 struct attribute *attr;
15d034d0 15845 const char *name;
c906108c 15846
348e048f
DE
15847 /* If the definition of this type lives in .debug_types, read that type.
15848 Don't follow DW_AT_specification though, that will take us back up
15849 the chain and we want to go down. */
45e58e77 15850 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15851 if (attr != nullptr)
348e048f 15852 {
ac9ec31b 15853 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15854
ac9ec31b 15855 /* The type's CU may not be the same as CU.
02142a6c 15856 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15857 return set_die_type (die, type, cu);
15858 }
15859
c0dd20ea 15860 type = alloc_type (objfile);
c906108c 15861 INIT_CPLUS_SPECIFIC (type);
93311388 15862
39cbfefa
DJ
15863 name = dwarf2_name (die, cu);
15864 if (name != NULL)
c906108c 15865 {
987504bb 15866 if (cu->language == language_cplus
c44af4eb
TT
15867 || cu->language == language_d
15868 || cu->language == language_rust)
63d06c5c 15869 {
15d034d0 15870 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15871
15872 /* dwarf2_full_name might have already finished building the DIE's
15873 type. If so, there is no need to continue. */
15874 if (get_die_type (die, cu) != NULL)
15875 return get_die_type (die, cu);
15876
e86ca25f 15877 TYPE_NAME (type) = full_name;
63d06c5c
DC
15878 }
15879 else
15880 {
d8151005
DJ
15881 /* The name is already allocated along with this objfile, so
15882 we don't need to duplicate it for the type. */
e86ca25f 15883 TYPE_NAME (type) = name;
63d06c5c 15884 }
c906108c
SS
15885 }
15886
15887 if (die->tag == DW_TAG_structure_type)
15888 {
15889 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15890 }
15891 else if (die->tag == DW_TAG_union_type)
15892 {
15893 TYPE_CODE (type) = TYPE_CODE_UNION;
15894 }
2ddeaf8a
TT
15895 else if (die->tag == DW_TAG_variant_part)
15896 {
15897 TYPE_CODE (type) = TYPE_CODE_UNION;
15898 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15899 }
c906108c
SS
15900 else
15901 {
4753d33b 15902 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15903 }
15904
0cc2414c
TT
15905 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15906 TYPE_DECLARED_CLASS (type) = 1;
15907
e142c38c 15908 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15909 if (attr != nullptr)
c906108c 15910 {
155bfbd3
JB
15911 if (attr_form_is_constant (attr))
15912 TYPE_LENGTH (type) = DW_UNSND (attr);
15913 else
15914 {
15915 /* For the moment, dynamic type sizes are not supported
15916 by GDB's struct type. The actual size is determined
15917 on-demand when resolving the type of a given object,
15918 so set the type's length to zero for now. Otherwise,
15919 we record an expression as the length, and that expression
15920 could lead to a very large value, which could eventually
15921 lead to us trying to allocate that much memory when creating
15922 a value of that type. */
15923 TYPE_LENGTH (type) = 0;
15924 }
c906108c
SS
15925 }
15926 else
15927 {
15928 TYPE_LENGTH (type) = 0;
15929 }
15930
2b4424c3
TT
15931 maybe_set_alignment (cu, die, type);
15932
5230b05a 15933 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15934 {
5230b05a
WT
15935 /* ICC<14 does not output the required DW_AT_declaration on
15936 incomplete types, but gives them a size of zero. */
422b1cb0 15937 TYPE_STUB (type) = 1;
685b1105
JK
15938 }
15939 else
15940 TYPE_STUB_SUPPORTED (type) = 1;
15941
dc718098 15942 if (die_is_declaration (die, cu))
876cecd0 15943 TYPE_STUB (type) = 1;
a6c727b2
DJ
15944 else if (attr == NULL && die->child == NULL
15945 && producer_is_realview (cu->producer))
15946 /* RealView does not output the required DW_AT_declaration
15947 on incomplete types. */
15948 TYPE_STUB (type) = 1;
dc718098 15949
c906108c
SS
15950 /* We need to add the type field to the die immediately so we don't
15951 infinitely recurse when dealing with pointers to the structure
0963b4bd 15952 type within the structure itself. */
1c379e20 15953 set_die_type (die, type, cu);
c906108c 15954
7e314c57
JK
15955 /* set_die_type should be already done. */
15956 set_descriptive_type (type, die, cu);
15957
c767944b
DJ
15958 return type;
15959}
15960
2ddeaf8a
TT
15961/* A helper for process_structure_scope that handles a single member
15962 DIE. */
15963
15964static void
15965handle_struct_member_die (struct die_info *child_die, struct type *type,
15966 struct field_info *fi,
15967 std::vector<struct symbol *> *template_args,
15968 struct dwarf2_cu *cu)
15969{
15970 if (child_die->tag == DW_TAG_member
15971 || child_die->tag == DW_TAG_variable
15972 || child_die->tag == DW_TAG_variant_part)
15973 {
15974 /* NOTE: carlton/2002-11-05: A C++ static data member
15975 should be a DW_TAG_member that is a declaration, but
15976 all versions of G++ as of this writing (so through at
15977 least 3.2.1) incorrectly generate DW_TAG_variable
15978 tags for them instead. */
15979 dwarf2_add_field (fi, child_die, cu);
15980 }
15981 else if (child_die->tag == DW_TAG_subprogram)
15982 {
15983 /* Rust doesn't have member functions in the C++ sense.
15984 However, it does emit ordinary functions as children
15985 of a struct DIE. */
15986 if (cu->language == language_rust)
15987 read_func_scope (child_die, cu);
15988 else
15989 {
15990 /* C++ member function. */
15991 dwarf2_add_member_fn (fi, child_die, type, cu);
15992 }
15993 }
15994 else if (child_die->tag == DW_TAG_inheritance)
15995 {
15996 /* C++ base class field. */
15997 dwarf2_add_field (fi, child_die, cu);
15998 }
15999 else if (type_can_define_types (child_die))
16000 dwarf2_add_type_defn (fi, child_die, cu);
16001 else if (child_die->tag == DW_TAG_template_type_param
16002 || child_die->tag == DW_TAG_template_value_param)
16003 {
16004 struct symbol *arg = new_symbol (child_die, NULL, cu);
16005
16006 if (arg != NULL)
16007 template_args->push_back (arg);
16008 }
16009 else if (child_die->tag == DW_TAG_variant)
16010 {
16011 /* In a variant we want to get the discriminant and also add a
16012 field for our sole member child. */
16013 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16014
bde09ab7 16015 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16016 variant_child != NULL;
16017 variant_child = sibling_die (variant_child))
16018 {
16019 if (variant_child->tag == DW_TAG_member)
16020 {
16021 handle_struct_member_die (variant_child, type, fi,
16022 template_args, cu);
16023 /* Only handle the one. */
16024 break;
16025 }
16026 }
16027
16028 /* We don't handle this but we might as well report it if we see
16029 it. */
16030 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16031 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16032 " - DIE at %s [in module %s]"),
16033 sect_offset_str (child_die->sect_off),
16034 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16035
16036 /* The first field was just added, so we can stash the
16037 discriminant there. */
be2daae6 16038 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16039 if (discr == NULL)
be2daae6 16040 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16041 else
be2daae6 16042 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16043 }
16044}
16045
c767944b
DJ
16046/* Finish creating a structure or union type, including filling in
16047 its members and creating a symbol for it. */
16048
16049static void
16050process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16051{
518817b3 16052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16053 struct die_info *child_die;
c767944b
DJ
16054 struct type *type;
16055
16056 type = get_die_type (die, cu);
16057 if (type == NULL)
16058 type = read_structure_type (die, cu);
16059
2ddeaf8a
TT
16060 /* When reading a DW_TAG_variant_part, we need to notice when we
16061 read the discriminant member, so we can record it later in the
16062 discriminant_info. */
16063 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16064 sect_offset discr_offset {};
3e1d3d8c 16065 bool has_template_parameters = false;
2ddeaf8a
TT
16066
16067 if (is_variant_part)
16068 {
16069 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16070 if (discr == NULL)
16071 {
16072 /* Maybe it's a univariant form, an extension we support.
16073 In this case arrange not to check the offset. */
16074 is_variant_part = false;
16075 }
16076 else if (attr_form_is_ref (discr))
16077 {
16078 struct dwarf2_cu *target_cu = cu;
16079 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16080
16081 discr_offset = target_die->sect_off;
16082 }
16083 else
16084 {
b98664d3 16085 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16086 " - DIE at %s [in module %s]"),
16087 sect_offset_str (die->sect_off),
16088 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16089 is_variant_part = false;
16090 }
16091 }
16092
e142c38c 16093 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16094 {
16095 struct field_info fi;
2f4732b0 16096 std::vector<struct symbol *> template_args;
c906108c 16097
639d11d3 16098 child_die = die->child;
c906108c
SS
16099
16100 while (child_die && child_die->tag)
16101 {
2ddeaf8a 16102 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16103
2ddeaf8a 16104 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16105 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16106
c906108c
SS
16107 child_die = sibling_die (child_die);
16108 }
16109
34eaf542 16110 /* Attach template arguments to type. */
2f4732b0 16111 if (!template_args.empty ())
34eaf542 16112 {
3e1d3d8c 16113 has_template_parameters = true;
34eaf542 16114 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16115 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16116 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16117 = XOBNEWVEC (&objfile->objfile_obstack,
16118 struct symbol *,
16119 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16120 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16121 template_args.data (),
34eaf542
TT
16122 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16123 * sizeof (struct symbol *)));
34eaf542
TT
16124 }
16125
c906108c
SS
16126 /* Attach fields and member functions to the type. */
16127 if (fi.nfields)
e7c27a73 16128 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16129 if (!fi.fnfieldlists.empty ())
c906108c 16130 {
e7c27a73 16131 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16132
c5aa993b 16133 /* Get the type which refers to the base class (possibly this
c906108c 16134 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16135 class from the DW_AT_containing_type attribute. This use of
16136 DW_AT_containing_type is a GNU extension. */
c906108c 16137
e142c38c 16138 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16139 {
e7c27a73 16140 struct type *t = die_containing_type (die, cu);
c906108c 16141
ae6ae975 16142 set_type_vptr_basetype (type, t);
c906108c
SS
16143 if (type == t)
16144 {
c906108c
SS
16145 int i;
16146
16147 /* Our own class provides vtbl ptr. */
16148 for (i = TYPE_NFIELDS (t) - 1;
16149 i >= TYPE_N_BASECLASSES (t);
16150 --i)
16151 {
0d5cff50 16152 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16153
1168df01 16154 if (is_vtable_name (fieldname, cu))
c906108c 16155 {
ae6ae975 16156 set_type_vptr_fieldno (type, i);
c906108c
SS
16157 break;
16158 }
16159 }
16160
16161 /* Complain if virtual function table field not found. */
16162 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16163 complaint (_("virtual function table pointer "
3e43a32a 16164 "not found when defining class '%s'"),
e86ca25f 16165 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16166 }
16167 else
16168 {
ae6ae975 16169 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16170 }
16171 }
f6235d4c 16172 else if (cu->producer
61012eef 16173 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16174 {
16175 /* The IBM XLC compiler does not provide direct indication
16176 of the containing type, but the vtable pointer is
16177 always named __vfp. */
16178
16179 int i;
16180
16181 for (i = TYPE_NFIELDS (type) - 1;
16182 i >= TYPE_N_BASECLASSES (type);
16183 --i)
16184 {
16185 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16186 {
ae6ae975
DE
16187 set_type_vptr_fieldno (type, i);
16188 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16189 break;
16190 }
16191 }
16192 }
c906108c 16193 }
98751a41
JK
16194
16195 /* Copy fi.typedef_field_list linked list elements content into the
16196 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16197 if (!fi.typedef_field_list.empty ())
98751a41 16198 {
be2daae6 16199 int count = fi.typedef_field_list.size ();
98751a41 16200
a0d7a4ff 16201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16202 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16203 = ((struct decl_field *)
be2daae6
TT
16204 TYPE_ALLOC (type,
16205 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16206 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16207
be2daae6
TT
16208 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16209 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16210 }
c767944b 16211
883fd55a
KS
16212 /* Copy fi.nested_types_list linked list elements content into the
16213 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16214 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16215 {
be2daae6 16216 int count = fi.nested_types_list.size ();
883fd55a
KS
16217
16218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16219 TYPE_NESTED_TYPES_ARRAY (type)
16220 = ((struct decl_field *)
be2daae6
TT
16221 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16222 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16223
be2daae6
TT
16224 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16225 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16226 }
c906108c 16227 }
63d06c5c 16228
bb5ed363 16229 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16230 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16231 cu->rust_unions.push_back (type);
0b92b5bb 16232
90aeadfc
DC
16233 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16234 snapshots) has been known to create a die giving a declaration
16235 for a class that has, as a child, a die giving a definition for a
16236 nested class. So we have to process our children even if the
16237 current die is a declaration. Normally, of course, a declaration
16238 won't have any children at all. */
134d01f1 16239
ca040673
DE
16240 child_die = die->child;
16241
90aeadfc
DC
16242 while (child_die != NULL && child_die->tag)
16243 {
16244 if (child_die->tag == DW_TAG_member
16245 || child_die->tag == DW_TAG_variable
34eaf542
TT
16246 || child_die->tag == DW_TAG_inheritance
16247 || child_die->tag == DW_TAG_template_value_param
16248 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16249 {
90aeadfc 16250 /* Do nothing. */
134d01f1 16251 }
90aeadfc
DC
16252 else
16253 process_die (child_die, cu);
134d01f1 16254
90aeadfc 16255 child_die = sibling_die (child_die);
134d01f1
DJ
16256 }
16257
fa4028e9
JB
16258 /* Do not consider external references. According to the DWARF standard,
16259 these DIEs are identified by the fact that they have no byte_size
16260 attribute, and a declaration attribute. */
16261 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16262 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16263 {
16264 struct symbol *sym = new_symbol (die, type, cu);
16265
16266 if (has_template_parameters)
16267 {
a776957c
TT
16268 struct symtab *symtab;
16269 if (sym != nullptr)
16270 symtab = symbol_symtab (sym);
16271 else if (cu->line_header != nullptr)
16272 {
16273 /* Any related symtab will do. */
16274 symtab
7ba99d21 16275 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16276 }
16277 else
16278 {
16279 symtab = nullptr;
16280 complaint (_("could not find suitable "
16281 "symtab for template parameter"
16282 " - DIE at %s [in module %s]"),
16283 sect_offset_str (die->sect_off),
16284 objfile_name (objfile));
16285 }
16286
16287 if (symtab != nullptr)
16288 {
16289 /* Make sure that the symtab is set on the new symbols.
16290 Even though they don't appear in this symtab directly,
16291 other parts of gdb assume that symbols do, and this is
16292 reasonably true. */
16293 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16294 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16295 }
3e1d3d8c
TT
16296 }
16297 }
134d01f1
DJ
16298}
16299
55426c9d
JB
16300/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16301 update TYPE using some information only available in DIE's children. */
16302
16303static void
16304update_enumeration_type_from_children (struct die_info *die,
16305 struct type *type,
16306 struct dwarf2_cu *cu)
16307{
60f7655a 16308 struct die_info *child_die;
55426c9d
JB
16309 int unsigned_enum = 1;
16310 int flag_enum = 1;
16311 ULONGEST mask = 0;
55426c9d 16312
8268c778 16313 auto_obstack obstack;
55426c9d 16314
60f7655a
DE
16315 for (child_die = die->child;
16316 child_die != NULL && child_die->tag;
16317 child_die = sibling_die (child_die))
55426c9d
JB
16318 {
16319 struct attribute *attr;
16320 LONGEST value;
16321 const gdb_byte *bytes;
16322 struct dwarf2_locexpr_baton *baton;
16323 const char *name;
60f7655a 16324
55426c9d
JB
16325 if (child_die->tag != DW_TAG_enumerator)
16326 continue;
16327
16328 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16329 if (attr == NULL)
16330 continue;
16331
16332 name = dwarf2_name (child_die, cu);
16333 if (name == NULL)
16334 name = "<anonymous enumerator>";
16335
16336 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16337 &value, &bytes, &baton);
16338 if (value < 0)
16339 {
16340 unsigned_enum = 0;
16341 flag_enum = 0;
16342 }
16343 else if ((mask & value) != 0)
16344 flag_enum = 0;
16345 else
16346 mask |= value;
16347
16348 /* If we already know that the enum type is neither unsigned, nor
16349 a flag type, no need to look at the rest of the enumerates. */
16350 if (!unsigned_enum && !flag_enum)
16351 break;
55426c9d
JB
16352 }
16353
16354 if (unsigned_enum)
16355 TYPE_UNSIGNED (type) = 1;
16356 if (flag_enum)
16357 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16358}
16359
134d01f1
DJ
16360/* Given a DW_AT_enumeration_type die, set its type. We do not
16361 complete the type's fields yet, or create any symbols. */
c906108c 16362
f792889a 16363static struct type *
134d01f1 16364read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16365{
518817b3 16366 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16367 struct type *type;
c906108c 16368 struct attribute *attr;
0114d602 16369 const char *name;
134d01f1 16370
348e048f
DE
16371 /* If the definition of this type lives in .debug_types, read that type.
16372 Don't follow DW_AT_specification though, that will take us back up
16373 the chain and we want to go down. */
45e58e77 16374 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16375 if (attr != nullptr)
348e048f 16376 {
ac9ec31b 16377 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16378
ac9ec31b 16379 /* The type's CU may not be the same as CU.
02142a6c 16380 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16381 return set_die_type (die, type, cu);
16382 }
16383
c906108c
SS
16384 type = alloc_type (objfile);
16385
16386 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16387 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16388 if (name != NULL)
e86ca25f 16389 TYPE_NAME (type) = name;
c906108c 16390
0626fc76
TT
16391 attr = dwarf2_attr (die, DW_AT_type, cu);
16392 if (attr != NULL)
16393 {
16394 struct type *underlying_type = die_type (die, cu);
16395
16396 TYPE_TARGET_TYPE (type) = underlying_type;
16397 }
16398
e142c38c 16399 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16400 if (attr != nullptr)
c906108c
SS
16401 {
16402 TYPE_LENGTH (type) = DW_UNSND (attr);
16403 }
16404 else
16405 {
16406 TYPE_LENGTH (type) = 0;
16407 }
16408
2b4424c3
TT
16409 maybe_set_alignment (cu, die, type);
16410
137033e9
JB
16411 /* The enumeration DIE can be incomplete. In Ada, any type can be
16412 declared as private in the package spec, and then defined only
16413 inside the package body. Such types are known as Taft Amendment
16414 Types. When another package uses such a type, an incomplete DIE
16415 may be generated by the compiler. */
02eb380e 16416 if (die_is_declaration (die, cu))
876cecd0 16417 TYPE_STUB (type) = 1;
02eb380e 16418
0626fc76
TT
16419 /* Finish the creation of this type by using the enum's children.
16420 We must call this even when the underlying type has been provided
16421 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16422 update_enumeration_type_from_children (die, type, cu);
16423
0626fc76
TT
16424 /* If this type has an underlying type that is not a stub, then we
16425 may use its attributes. We always use the "unsigned" attribute
16426 in this situation, because ordinarily we guess whether the type
16427 is unsigned -- but the guess can be wrong and the underlying type
16428 can tell us the reality. However, we defer to a local size
16429 attribute if one exists, because this lets the compiler override
16430 the underlying type if needed. */
16431 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16432 {
16433 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16434 if (TYPE_LENGTH (type) == 0)
16435 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16436 if (TYPE_RAW_ALIGN (type) == 0
16437 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16438 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16439 }
16440
3d567982
TT
16441 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16442
f792889a 16443 return set_die_type (die, type, cu);
134d01f1
DJ
16444}
16445
16446/* Given a pointer to a die which begins an enumeration, process all
16447 the dies that define the members of the enumeration, and create the
16448 symbol for the enumeration type.
16449
16450 NOTE: We reverse the order of the element list. */
16451
16452static void
16453process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16454{
f792889a 16455 struct type *this_type;
134d01f1 16456
f792889a
DJ
16457 this_type = get_die_type (die, cu);
16458 if (this_type == NULL)
16459 this_type = read_enumeration_type (die, cu);
9dc481d3 16460
639d11d3 16461 if (die->child != NULL)
c906108c 16462 {
9dc481d3
DE
16463 struct die_info *child_die;
16464 struct symbol *sym;
16465 struct field *fields = NULL;
16466 int num_fields = 0;
15d034d0 16467 const char *name;
9dc481d3 16468
639d11d3 16469 child_die = die->child;
c906108c
SS
16470 while (child_die && child_die->tag)
16471 {
16472 if (child_die->tag != DW_TAG_enumerator)
16473 {
e7c27a73 16474 process_die (child_die, cu);
c906108c
SS
16475 }
16476 else
16477 {
39cbfefa
DJ
16478 name = dwarf2_name (child_die, cu);
16479 if (name)
c906108c 16480 {
f792889a 16481 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16482
16483 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16484 {
16485 fields = (struct field *)
16486 xrealloc (fields,
16487 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16488 * sizeof (struct field));
c906108c
SS
16489 }
16490
987012b8 16491 FIELD_NAME (fields[num_fields]) = sym->linkage_name ();
c906108c 16492 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16493 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16494 FIELD_BITSIZE (fields[num_fields]) = 0;
16495
16496 num_fields++;
16497 }
16498 }
16499
16500 child_die = sibling_die (child_die);
16501 }
16502
16503 if (num_fields)
16504 {
f792889a
DJ
16505 TYPE_NFIELDS (this_type) = num_fields;
16506 TYPE_FIELDS (this_type) = (struct field *)
16507 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16508 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16509 sizeof (struct field) * num_fields);
b8c9b27d 16510 xfree (fields);
c906108c 16511 }
c906108c 16512 }
134d01f1 16513
6c83ed52
TT
16514 /* If we are reading an enum from a .debug_types unit, and the enum
16515 is a declaration, and the enum is not the signatured type in the
16516 unit, then we do not want to add a symbol for it. Adding a
16517 symbol would in some cases obscure the true definition of the
16518 enum, giving users an incomplete type when the definition is
16519 actually available. Note that we do not want to do this for all
16520 enums which are just declarations, because C++0x allows forward
16521 enum declarations. */
3019eac3 16522 if (cu->per_cu->is_debug_types
6c83ed52
TT
16523 && die_is_declaration (die, cu))
16524 {
52dc124a 16525 struct signatured_type *sig_type;
6c83ed52 16526
c0f78cd4 16527 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16528 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16529 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16530 return;
16531 }
16532
f792889a 16533 new_symbol (die, this_type, cu);
c906108c
SS
16534}
16535
16536/* Extract all information from a DW_TAG_array_type DIE and put it in
16537 the DIE's type field. For now, this only handles one dimensional
16538 arrays. */
16539
f792889a 16540static struct type *
e7c27a73 16541read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16542{
518817b3 16543 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16544 struct die_info *child_die;
7e314c57 16545 struct type *type;
c906108c 16546 struct type *element_type, *range_type, *index_type;
c906108c 16547 struct attribute *attr;
15d034d0 16548 const char *name;
a405673c 16549 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16550 unsigned int bit_stride = 0;
c906108c 16551
e7c27a73 16552 element_type = die_type (die, cu);
c906108c 16553
7e314c57
JK
16554 /* The die_type call above may have already set the type for this DIE. */
16555 type = get_die_type (die, cu);
16556 if (type)
16557 return type;
16558
dc53a7ad
JB
16559 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16560 if (attr != NULL)
a405673c
JB
16561 {
16562 int stride_ok;
9a49df9d
AB
16563 struct type *prop_type
16564 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16565
16566 byte_stride_prop
16567 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16568 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16569 prop_type);
a405673c
JB
16570 if (!stride_ok)
16571 {
b98664d3 16572 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16573 " - DIE at %s [in module %s]"),
16574 sect_offset_str (die->sect_off),
518817b3 16575 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16576 /* Ignore this attribute. We will likely not be able to print
16577 arrays of this type correctly, but there is little we can do
16578 to help if we cannot read the attribute's value. */
16579 byte_stride_prop = NULL;
16580 }
16581 }
dc53a7ad
JB
16582
16583 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16584 if (attr != NULL)
16585 bit_stride = DW_UNSND (attr);
16586
c906108c
SS
16587 /* Irix 6.2 native cc creates array types without children for
16588 arrays with unspecified length. */
639d11d3 16589 if (die->child == NULL)
c906108c 16590 {
46bf5051 16591 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16592 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16593 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16594 byte_stride_prop, bit_stride);
f792889a 16595 return set_die_type (die, type, cu);
c906108c
SS
16596 }
16597
791afaa2 16598 std::vector<struct type *> range_types;
639d11d3 16599 child_die = die->child;
c906108c
SS
16600 while (child_die && child_die->tag)
16601 {
16602 if (child_die->tag == DW_TAG_subrange_type)
16603 {
f792889a 16604 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16605
f792889a 16606 if (child_type != NULL)
a02abb62 16607 {
0963b4bd
MS
16608 /* The range type was succesfully read. Save it for the
16609 array type creation. */
791afaa2 16610 range_types.push_back (child_type);
a02abb62 16611 }
c906108c
SS
16612 }
16613 child_die = sibling_die (child_die);
16614 }
16615
16616 /* Dwarf2 dimensions are output from left to right, create the
16617 necessary array types in backwards order. */
7ca2d3a3 16618
c906108c 16619 type = element_type;
7ca2d3a3
DL
16620
16621 if (read_array_order (die, cu) == DW_ORD_col_major)
16622 {
16623 int i = 0;
9a619af0 16624
791afaa2 16625 while (i < range_types.size ())
dc53a7ad 16626 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16627 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16628 }
16629 else
16630 {
791afaa2 16631 size_t ndim = range_types.size ();
7ca2d3a3 16632 while (ndim-- > 0)
dc53a7ad 16633 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16634 byte_stride_prop, bit_stride);
7ca2d3a3 16635 }
c906108c 16636
f5f8a009
EZ
16637 /* Understand Dwarf2 support for vector types (like they occur on
16638 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16639 array type. This is not part of the Dwarf2/3 standard yet, but a
16640 custom vendor extension. The main difference between a regular
16641 array and the vector variant is that vectors are passed by value
16642 to functions. */
e142c38c 16643 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16644 if (attr != nullptr)
ea37ba09 16645 make_vector_type (type);
f5f8a009 16646
dbc98a8b
KW
16647 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16648 implementation may choose to implement triple vectors using this
16649 attribute. */
16650 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16651 if (attr != nullptr)
dbc98a8b
KW
16652 {
16653 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16654 TYPE_LENGTH (type) = DW_UNSND (attr);
16655 else
b98664d3 16656 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16657 "than the total size of elements"));
dbc98a8b
KW
16658 }
16659
39cbfefa
DJ
16660 name = dwarf2_name (die, cu);
16661 if (name)
16662 TYPE_NAME (type) = name;
6e70227d 16663
2b4424c3
TT
16664 maybe_set_alignment (cu, die, type);
16665
0963b4bd 16666 /* Install the type in the die. */
7e314c57
JK
16667 set_die_type (die, type, cu);
16668
16669 /* set_die_type should be already done. */
b4ba55a1
JB
16670 set_descriptive_type (type, die, cu);
16671
7e314c57 16672 return type;
c906108c
SS
16673}
16674
7ca2d3a3 16675static enum dwarf_array_dim_ordering
6e70227d 16676read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16677{
16678 struct attribute *attr;
16679
16680 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16681
435d3d88 16682 if (attr != nullptr)
aead7601 16683 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16684
0963b4bd
MS
16685 /* GNU F77 is a special case, as at 08/2004 array type info is the
16686 opposite order to the dwarf2 specification, but data is still
16687 laid out as per normal fortran.
7ca2d3a3 16688
0963b4bd
MS
16689 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16690 version checking. */
7ca2d3a3 16691
905e0470
PM
16692 if (cu->language == language_fortran
16693 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16694 {
16695 return DW_ORD_row_major;
16696 }
16697
6e70227d 16698 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16699 {
16700 case array_column_major:
16701 return DW_ORD_col_major;
16702 case array_row_major:
16703 default:
16704 return DW_ORD_row_major;
16705 };
16706}
16707
72019c9c 16708/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16709 the DIE's type field. */
72019c9c 16710
f792889a 16711static struct type *
72019c9c
GM
16712read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16713{
7e314c57
JK
16714 struct type *domain_type, *set_type;
16715 struct attribute *attr;
f792889a 16716
7e314c57
JK
16717 domain_type = die_type (die, cu);
16718
16719 /* The die_type call above may have already set the type for this DIE. */
16720 set_type = get_die_type (die, cu);
16721 if (set_type)
16722 return set_type;
16723
16724 set_type = create_set_type (NULL, domain_type);
16725
16726 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16727 if (attr != nullptr)
d09039dd 16728 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16729
2b4424c3
TT
16730 maybe_set_alignment (cu, die, set_type);
16731
f792889a 16732 return set_die_type (die, set_type, cu);
72019c9c 16733}
7ca2d3a3 16734
0971de02
TT
16735/* A helper for read_common_block that creates a locexpr baton.
16736 SYM is the symbol which we are marking as computed.
16737 COMMON_DIE is the DIE for the common block.
16738 COMMON_LOC is the location expression attribute for the common
16739 block itself.
16740 MEMBER_LOC is the location expression attribute for the particular
16741 member of the common block that we are processing.
16742 CU is the CU from which the above come. */
16743
16744static void
16745mark_common_block_symbol_computed (struct symbol *sym,
16746 struct die_info *common_die,
16747 struct attribute *common_loc,
16748 struct attribute *member_loc,
16749 struct dwarf2_cu *cu)
16750{
518817b3
SM
16751 struct dwarf2_per_objfile *dwarf2_per_objfile
16752 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16753 struct objfile *objfile = dwarf2_per_objfile->objfile;
16754 struct dwarf2_locexpr_baton *baton;
16755 gdb_byte *ptr;
16756 unsigned int cu_off;
16757 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16758 LONGEST offset = 0;
16759
16760 gdb_assert (common_loc && member_loc);
16761 gdb_assert (attr_form_is_block (common_loc));
16762 gdb_assert (attr_form_is_block (member_loc)
16763 || attr_form_is_constant (member_loc));
16764
8d749320 16765 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16766 baton->per_cu = cu->per_cu;
16767 gdb_assert (baton->per_cu);
16768
16769 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16770
16771 if (attr_form_is_constant (member_loc))
16772 {
16773 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16774 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16775 }
16776 else
16777 baton->size += DW_BLOCK (member_loc)->size;
16778
224c3ddb 16779 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16780 baton->data = ptr;
16781
16782 *ptr++ = DW_OP_call4;
9c541725 16783 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16784 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16785 ptr += 4;
16786
16787 if (attr_form_is_constant (member_loc))
16788 {
16789 *ptr++ = DW_OP_addr;
16790 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16791 ptr += cu->header.addr_size;
16792 }
16793 else
16794 {
16795 /* We have to copy the data here, because DW_OP_call4 will only
16796 use a DW_AT_location attribute. */
16797 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16798 ptr += DW_BLOCK (member_loc)->size;
16799 }
16800
16801 *ptr++ = DW_OP_plus;
16802 gdb_assert (ptr - baton->data == baton->size);
16803
0971de02 16804 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16805 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16806}
16807
4357ac6c
TT
16808/* Create appropriate locally-scoped variables for all the
16809 DW_TAG_common_block entries. Also create a struct common_block
16810 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16811 is used to separate the common blocks name namespace from regular
4357ac6c 16812 variable names. */
c906108c
SS
16813
16814static void
e7c27a73 16815read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16816{
0971de02
TT
16817 struct attribute *attr;
16818
16819 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16820 if (attr != nullptr)
0971de02
TT
16821 {
16822 /* Support the .debug_loc offsets. */
16823 if (attr_form_is_block (attr))
16824 {
16825 /* Ok. */
16826 }
16827 else if (attr_form_is_section_offset (attr))
16828 {
16829 dwarf2_complex_location_expr_complaint ();
16830 attr = NULL;
16831 }
16832 else
16833 {
16834 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16835 "common block member");
16836 attr = NULL;
16837 }
16838 }
16839
639d11d3 16840 if (die->child != NULL)
c906108c 16841 {
518817b3 16842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16843 struct die_info *child_die;
16844 size_t n_entries = 0, size;
16845 struct common_block *common_block;
16846 struct symbol *sym;
74ac6d43 16847
4357ac6c
TT
16848 for (child_die = die->child;
16849 child_die && child_die->tag;
16850 child_die = sibling_die (child_die))
16851 ++n_entries;
16852
16853 size = (sizeof (struct common_block)
16854 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16855 common_block
16856 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16857 size);
4357ac6c
TT
16858 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16859 common_block->n_entries = 0;
16860
16861 for (child_die = die->child;
16862 child_die && child_die->tag;
16863 child_die = sibling_die (child_die))
16864 {
16865 /* Create the symbol in the DW_TAG_common_block block in the current
16866 symbol scope. */
e7c27a73 16867 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16868 if (sym != NULL)
16869 {
16870 struct attribute *member_loc;
16871
16872 common_block->contents[common_block->n_entries++] = sym;
16873
16874 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16875 cu);
16876 if (member_loc)
16877 {
16878 /* GDB has handled this for a long time, but it is
16879 not specified by DWARF. It seems to have been
16880 emitted by gfortran at least as recently as:
16881 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16882 complaint (_("Variable in common block has "
0971de02 16883 "DW_AT_data_member_location "
9d8780f0
SM
16884 "- DIE at %s [in module %s]"),
16885 sect_offset_str (child_die->sect_off),
518817b3 16886 objfile_name (objfile));
0971de02
TT
16887
16888 if (attr_form_is_section_offset (member_loc))
16889 dwarf2_complex_location_expr_complaint ();
16890 else if (attr_form_is_constant (member_loc)
16891 || attr_form_is_block (member_loc))
16892 {
435d3d88 16893 if (attr != nullptr)
0971de02
TT
16894 mark_common_block_symbol_computed (sym, die, attr,
16895 member_loc, cu);
16896 }
16897 else
16898 dwarf2_complex_location_expr_complaint ();
16899 }
16900 }
c906108c 16901 }
4357ac6c
TT
16902
16903 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16904 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16905 }
16906}
16907
0114d602 16908/* Create a type for a C++ namespace. */
d9fa45fe 16909
0114d602
DJ
16910static struct type *
16911read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16912{
518817b3 16913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16914 const char *previous_prefix, *name;
9219021c 16915 int is_anonymous;
0114d602
DJ
16916 struct type *type;
16917
16918 /* For extensions, reuse the type of the original namespace. */
16919 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16920 {
16921 struct die_info *ext_die;
16922 struct dwarf2_cu *ext_cu = cu;
9a619af0 16923
0114d602
DJ
16924 ext_die = dwarf2_extension (die, &ext_cu);
16925 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16926
16927 /* EXT_CU may not be the same as CU.
02142a6c 16928 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16929 return set_die_type (die, type, cu);
16930 }
9219021c 16931
e142c38c 16932 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16933
16934 /* Now build the name of the current namespace. */
16935
0114d602
DJ
16936 previous_prefix = determine_prefix (die, cu);
16937 if (previous_prefix[0] != '\0')
16938 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16939 previous_prefix, name, 0, cu);
0114d602
DJ
16940
16941 /* Create the type. */
19f392bc 16942 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16943
60531b24 16944 return set_die_type (die, type, cu);
0114d602
DJ
16945}
16946
22cee43f 16947/* Read a namespace scope. */
0114d602
DJ
16948
16949static void
16950read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16951{
518817b3 16952 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16953 int is_anonymous;
9219021c 16954
5c4e30ca
DC
16955 /* Add a symbol associated to this if we haven't seen the namespace
16956 before. Also, add a using directive if it's an anonymous
16957 namespace. */
9219021c 16958
f2f0e013 16959 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16960 {
16961 struct type *type;
16962
0114d602 16963 type = read_type_die (die, cu);
e7c27a73 16964 new_symbol (die, type, cu);
5c4e30ca 16965
e8e80198 16966 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16967 if (is_anonymous)
0114d602
DJ
16968 {
16969 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16970
eb1e02fd 16971 std::vector<const char *> excludes;
804d2729 16972 add_using_directive (using_directives (cu),
22cee43f 16973 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16974 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16975 }
5c4e30ca 16976 }
9219021c 16977
639d11d3 16978 if (die->child != NULL)
d9fa45fe 16979 {
639d11d3 16980 struct die_info *child_die = die->child;
6e70227d 16981
d9fa45fe
DC
16982 while (child_die && child_die->tag)
16983 {
e7c27a73 16984 process_die (child_die, cu);
d9fa45fe
DC
16985 child_die = sibling_die (child_die);
16986 }
16987 }
38d518c9
EZ
16988}
16989
f55ee35c
JK
16990/* Read a Fortran module as type. This DIE can be only a declaration used for
16991 imported module. Still we need that type as local Fortran "use ... only"
16992 declaration imports depend on the created type in determine_prefix. */
16993
16994static struct type *
16995read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16996{
518817b3 16997 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16998 const char *module_name;
f55ee35c
JK
16999 struct type *type;
17000
17001 module_name = dwarf2_name (die, cu);
19f392bc 17002 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17003
f55ee35c
JK
17004 return set_die_type (die, type, cu);
17005}
17006
5d7cb8df
JK
17007/* Read a Fortran module. */
17008
17009static void
17010read_module (struct die_info *die, struct dwarf2_cu *cu)
17011{
17012 struct die_info *child_die = die->child;
530e8392
KB
17013 struct type *type;
17014
17015 type = read_type_die (die, cu);
17016 new_symbol (die, type, cu);
5d7cb8df 17017
5d7cb8df
JK
17018 while (child_die && child_die->tag)
17019 {
17020 process_die (child_die, cu);
17021 child_die = sibling_die (child_die);
17022 }
17023}
17024
38d518c9
EZ
17025/* Return the name of the namespace represented by DIE. Set
17026 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17027 namespace. */
17028
17029static const char *
e142c38c 17030namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17031{
17032 struct die_info *current_die;
17033 const char *name = NULL;
17034
17035 /* Loop through the extensions until we find a name. */
17036
17037 for (current_die = die;
17038 current_die != NULL;
f2f0e013 17039 current_die = dwarf2_extension (die, &cu))
38d518c9 17040 {
96553a0c
DE
17041 /* We don't use dwarf2_name here so that we can detect the absence
17042 of a name -> anonymous namespace. */
7d45c7c3 17043 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17044
38d518c9
EZ
17045 if (name != NULL)
17046 break;
17047 }
17048
17049 /* Is it an anonymous namespace? */
17050
17051 *is_anonymous = (name == NULL);
17052 if (*is_anonymous)
2b1dbab0 17053 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17054
17055 return name;
d9fa45fe
DC
17056}
17057
c906108c
SS
17058/* Extract all information from a DW_TAG_pointer_type DIE and add to
17059 the user defined type vector. */
17060
f792889a 17061static struct type *
e7c27a73 17062read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17063{
518817b3
SM
17064 struct gdbarch *gdbarch
17065 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17066 struct comp_unit_head *cu_header = &cu->header;
c906108c 17067 struct type *type;
8b2dbe47
KB
17068 struct attribute *attr_byte_size;
17069 struct attribute *attr_address_class;
17070 int byte_size, addr_class;
7e314c57
JK
17071 struct type *target_type;
17072
17073 target_type = die_type (die, cu);
c906108c 17074
7e314c57
JK
17075 /* The die_type call above may have already set the type for this DIE. */
17076 type = get_die_type (die, cu);
17077 if (type)
17078 return type;
17079
17080 type = lookup_pointer_type (target_type);
8b2dbe47 17081
e142c38c 17082 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17083 if (attr_byte_size)
17084 byte_size = DW_UNSND (attr_byte_size);
c906108c 17085 else
8b2dbe47
KB
17086 byte_size = cu_header->addr_size;
17087
e142c38c 17088 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17089 if (attr_address_class)
17090 addr_class = DW_UNSND (attr_address_class);
17091 else
17092 addr_class = DW_ADDR_none;
17093
2b4424c3
TT
17094 ULONGEST alignment = get_alignment (cu, die);
17095
17096 /* If the pointer size, alignment, or address class is different
17097 than the default, create a type variant marked as such and set
17098 the length accordingly. */
17099 if (TYPE_LENGTH (type) != byte_size
17100 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17101 && alignment != TYPE_RAW_ALIGN (type))
17102 || addr_class != DW_ADDR_none)
c906108c 17103 {
5e2b427d 17104 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17105 {
17106 int type_flags;
17107
849957d9 17108 type_flags = gdbarch_address_class_type_flags
5e2b427d 17109 (gdbarch, byte_size, addr_class);
876cecd0
TT
17110 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17111 == 0);
8b2dbe47
KB
17112 type = make_type_with_address_space (type, type_flags);
17113 }
17114 else if (TYPE_LENGTH (type) != byte_size)
17115 {
b98664d3 17116 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17117 }
2b4424c3
TT
17118 else if (TYPE_RAW_ALIGN (type) != alignment)
17119 {
b98664d3 17120 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17121 " - DIE at %s [in module %s]"),
17122 sect_offset_str (die->sect_off),
17123 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17124 }
6e70227d 17125 else
9a619af0
MS
17126 {
17127 /* Should we also complain about unhandled address classes? */
17128 }
c906108c 17129 }
8b2dbe47
KB
17130
17131 TYPE_LENGTH (type) = byte_size;
2b4424c3 17132 set_type_align (type, alignment);
f792889a 17133 return set_die_type (die, type, cu);
c906108c
SS
17134}
17135
17136/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17137 the user defined type vector. */
17138
f792889a 17139static struct type *
e7c27a73 17140read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17141{
17142 struct type *type;
17143 struct type *to_type;
17144 struct type *domain;
17145
e7c27a73
DJ
17146 to_type = die_type (die, cu);
17147 domain = die_containing_type (die, cu);
0d5de010 17148
7e314c57
JK
17149 /* The calls above may have already set the type for this DIE. */
17150 type = get_die_type (die, cu);
17151 if (type)
17152 return type;
17153
0d5de010
DJ
17154 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17155 type = lookup_methodptr_type (to_type);
7078baeb
TT
17156 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17157 {
518817b3
SM
17158 struct type *new_type
17159 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17160
17161 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17162 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17163 TYPE_VARARGS (to_type));
17164 type = lookup_methodptr_type (new_type);
17165 }
0d5de010
DJ
17166 else
17167 type = lookup_memberptr_type (to_type, domain);
c906108c 17168
f792889a 17169 return set_die_type (die, type, cu);
c906108c
SS
17170}
17171
4297a3f0 17172/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17173 the user defined type vector. */
17174
f792889a 17175static struct type *
4297a3f0
AV
17176read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17177 enum type_code refcode)
c906108c 17178{
e7c27a73 17179 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17180 struct type *type, *target_type;
c906108c
SS
17181 struct attribute *attr;
17182
4297a3f0
AV
17183 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17184
7e314c57
JK
17185 target_type = die_type (die, cu);
17186
17187 /* The die_type call above may have already set the type for this DIE. */
17188 type = get_die_type (die, cu);
17189 if (type)
17190 return type;
17191
4297a3f0 17192 type = lookup_reference_type (target_type, refcode);
e142c38c 17193 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17194 if (attr != nullptr)
c906108c
SS
17195 {
17196 TYPE_LENGTH (type) = DW_UNSND (attr);
17197 }
17198 else
17199 {
107d2387 17200 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17201 }
2b4424c3 17202 maybe_set_alignment (cu, die, type);
f792889a 17203 return set_die_type (die, type, cu);
c906108c
SS
17204}
17205
cf363f18
MW
17206/* Add the given cv-qualifiers to the element type of the array. GCC
17207 outputs DWARF type qualifiers that apply to an array, not the
17208 element type. But GDB relies on the array element type to carry
17209 the cv-qualifiers. This mimics section 6.7.3 of the C99
17210 specification. */
17211
17212static struct type *
17213add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17214 struct type *base_type, int cnst, int voltl)
17215{
17216 struct type *el_type, *inner_array;
17217
17218 base_type = copy_type (base_type);
17219 inner_array = base_type;
17220
17221 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17222 {
17223 TYPE_TARGET_TYPE (inner_array) =
17224 copy_type (TYPE_TARGET_TYPE (inner_array));
17225 inner_array = TYPE_TARGET_TYPE (inner_array);
17226 }
17227
17228 el_type = TYPE_TARGET_TYPE (inner_array);
17229 cnst |= TYPE_CONST (el_type);
17230 voltl |= TYPE_VOLATILE (el_type);
17231 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17232
17233 return set_die_type (die, base_type, cu);
17234}
17235
f792889a 17236static struct type *
e7c27a73 17237read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17238{
f792889a 17239 struct type *base_type, *cv_type;
c906108c 17240
e7c27a73 17241 base_type = die_type (die, cu);
7e314c57
JK
17242
17243 /* The die_type call above may have already set the type for this DIE. */
17244 cv_type = get_die_type (die, cu);
17245 if (cv_type)
17246 return cv_type;
17247
2f608a3a
KW
17248 /* In case the const qualifier is applied to an array type, the element type
17249 is so qualified, not the array type (section 6.7.3 of C99). */
17250 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17251 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17252
f792889a
DJ
17253 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17254 return set_die_type (die, cv_type, cu);
c906108c
SS
17255}
17256
f792889a 17257static struct type *
e7c27a73 17258read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17259{
f792889a 17260 struct type *base_type, *cv_type;
c906108c 17261
e7c27a73 17262 base_type = die_type (die, cu);
7e314c57
JK
17263
17264 /* The die_type call above may have already set the type for this DIE. */
17265 cv_type = get_die_type (die, cu);
17266 if (cv_type)
17267 return cv_type;
17268
cf363f18
MW
17269 /* In case the volatile qualifier is applied to an array type, the
17270 element type is so qualified, not the array type (section 6.7.3
17271 of C99). */
17272 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17273 return add_array_cv_type (die, cu, base_type, 0, 1);
17274
f792889a
DJ
17275 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17276 return set_die_type (die, cv_type, cu);
c906108c
SS
17277}
17278
06d66ee9
TT
17279/* Handle DW_TAG_restrict_type. */
17280
17281static struct type *
17282read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17283{
17284 struct type *base_type, *cv_type;
17285
17286 base_type = die_type (die, cu);
17287
17288 /* The die_type call above may have already set the type for this DIE. */
17289 cv_type = get_die_type (die, cu);
17290 if (cv_type)
17291 return cv_type;
17292
17293 cv_type = make_restrict_type (base_type);
17294 return set_die_type (die, cv_type, cu);
17295}
17296
a2c2acaf
MW
17297/* Handle DW_TAG_atomic_type. */
17298
17299static struct type *
17300read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17301{
17302 struct type *base_type, *cv_type;
17303
17304 base_type = die_type (die, cu);
17305
17306 /* The die_type call above may have already set the type for this DIE. */
17307 cv_type = get_die_type (die, cu);
17308 if (cv_type)
17309 return cv_type;
17310
17311 cv_type = make_atomic_type (base_type);
17312 return set_die_type (die, cv_type, cu);
17313}
17314
c906108c
SS
17315/* Extract all information from a DW_TAG_string_type DIE and add to
17316 the user defined type vector. It isn't really a user defined type,
17317 but it behaves like one, with other DIE's using an AT_user_def_type
17318 attribute to reference it. */
17319
f792889a 17320static struct type *
e7c27a73 17321read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17322{
518817b3 17323 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17324 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17325 struct type *type, *range_type, *index_type, *char_type;
17326 struct attribute *attr;
216a7e6b
AB
17327 struct dynamic_prop prop;
17328 bool length_is_constant = true;
17329 LONGEST length;
17330
17331 /* There are a couple of places where bit sizes might be made use of
17332 when parsing a DW_TAG_string_type, however, no producer that we know
17333 of make use of these. Handling bit sizes that are a multiple of the
17334 byte size is easy enough, but what about other bit sizes? Lets deal
17335 with that problem when we have to. Warn about these attributes being
17336 unsupported, then parse the type and ignore them like we always
17337 have. */
17338 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17339 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17340 {
17341 static bool warning_printed = false;
17342 if (!warning_printed)
17343 {
17344 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17345 "currently supported on DW_TAG_string_type."));
17346 warning_printed = true;
17347 }
17348 }
c906108c 17349
e142c38c 17350 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17351 if (attr != nullptr && !attr_form_is_constant (attr))
17352 {
17353 /* The string length describes the location at which the length of
17354 the string can be found. The size of the length field can be
17355 specified with one of the attributes below. */
17356 struct type *prop_type;
17357 struct attribute *len
17358 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17359 if (len == nullptr)
17360 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17361 if (len != nullptr && attr_form_is_constant (len))
17362 {
17363 /* Pass 0 as the default as we know this attribute is constant
17364 and the default value will not be returned. */
17365 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17366 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17367 }
17368 else
17369 {
17370 /* If the size is not specified then we assume it is the size of
17371 an address on this target. */
17372 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17373 }
17374
17375 /* Convert the attribute into a dynamic property. */
17376 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17377 length = 1;
17378 else
17379 length_is_constant = false;
17380 }
17381 else if (attr != nullptr)
17382 {
17383 /* This DW_AT_string_length just contains the length with no
17384 indirection. There's no need to create a dynamic property in this
17385 case. Pass 0 for the default value as we know it will not be
17386 returned in this case. */
17387 length = dwarf2_get_attr_constant_value (attr, 0);
17388 }
17389 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17390 {
216a7e6b
AB
17391 /* We don't currently support non-constant byte sizes for strings. */
17392 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17393 }
17394 else
17395 {
216a7e6b
AB
17396 /* Use 1 as a fallback length if we have nothing else. */
17397 length = 1;
c906108c 17398 }
6ccb9162 17399
46bf5051 17400 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17401 if (length_is_constant)
17402 range_type = create_static_range_type (NULL, index_type, 1, length);
17403 else
17404 {
17405 struct dynamic_prop low_bound;
17406
17407 low_bound.kind = PROP_CONST;
17408 low_bound.data.const_val = 1;
17409 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17410 }
3b7538c0
UW
17411 char_type = language_string_char_type (cu->language_defn, gdbarch);
17412 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17413
f792889a 17414 return set_die_type (die, type, cu);
c906108c
SS
17415}
17416
4d804846
JB
17417/* Assuming that DIE corresponds to a function, returns nonzero
17418 if the function is prototyped. */
17419
17420static int
17421prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17422{
17423 struct attribute *attr;
17424
17425 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17426 if (attr && (DW_UNSND (attr) != 0))
17427 return 1;
17428
17429 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17430 is only meaningful for C, but the concept also extends to other
4d804846
JB
17431 languages that allow unprototyped functions (Eg: Objective C).
17432 For all other languages, assume that functions are always
17433 prototyped. */
17434 if (cu->language != language_c
17435 && cu->language != language_objc
17436 && cu->language != language_opencl)
17437 return 1;
17438
17439 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17440 prototyped and unprototyped functions; default to prototyped,
17441 since that is more common in modern code (and RealView warns
17442 about unprototyped functions). */
17443 if (producer_is_realview (cu->producer))
17444 return 1;
17445
17446 return 0;
17447}
17448
c906108c
SS
17449/* Handle DIES due to C code like:
17450
17451 struct foo
c5aa993b
JM
17452 {
17453 int (*funcp)(int a, long l);
17454 int b;
17455 };
c906108c 17456
0963b4bd 17457 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17458
f792889a 17459static struct type *
e7c27a73 17460read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17461{
518817b3 17462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17463 struct type *type; /* Type that this function returns. */
17464 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17465 struct attribute *attr;
17466
e7c27a73 17467 type = die_type (die, cu);
7e314c57
JK
17468
17469 /* The die_type call above may have already set the type for this DIE. */
17470 ftype = get_die_type (die, cu);
17471 if (ftype)
17472 return ftype;
17473
0c8b41f1 17474 ftype = lookup_function_type (type);
c906108c 17475
4d804846 17476 if (prototyped_function_p (die, cu))
a6c727b2 17477 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17478
c055b101
CV
17479 /* Store the calling convention in the type if it's available in
17480 the subroutine die. Otherwise set the calling convention to
17481 the default value DW_CC_normal. */
17482 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
435d3d88 17483 if (attr != nullptr)
54fcddd0
UW
17484 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17485 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17486 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17487 else
17488 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17489
743649fd
MW
17490 /* Record whether the function returns normally to its caller or not
17491 if the DWARF producer set that information. */
17492 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17493 if (attr && (DW_UNSND (attr) != 0))
17494 TYPE_NO_RETURN (ftype) = 1;
17495
76c10ea2
GM
17496 /* We need to add the subroutine type to the die immediately so
17497 we don't infinitely recurse when dealing with parameters
0963b4bd 17498 declared as the same subroutine type. */
76c10ea2 17499 set_die_type (die, ftype, cu);
6e70227d 17500
639d11d3 17501 if (die->child != NULL)
c906108c 17502 {
bb5ed363 17503 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17504 struct die_info *child_die;
8072405b 17505 int nparams, iparams;
c906108c
SS
17506
17507 /* Count the number of parameters.
17508 FIXME: GDB currently ignores vararg functions, but knows about
17509 vararg member functions. */
8072405b 17510 nparams = 0;
639d11d3 17511 child_die = die->child;
c906108c
SS
17512 while (child_die && child_die->tag)
17513 {
17514 if (child_die->tag == DW_TAG_formal_parameter)
17515 nparams++;
17516 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17517 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17518 child_die = sibling_die (child_die);
17519 }
17520
17521 /* Allocate storage for parameters and fill them in. */
17522 TYPE_NFIELDS (ftype) = nparams;
17523 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17524 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17525
8072405b
JK
17526 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17527 even if we error out during the parameters reading below. */
17528 for (iparams = 0; iparams < nparams; iparams++)
17529 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17530
17531 iparams = 0;
639d11d3 17532 child_die = die->child;
c906108c
SS
17533 while (child_die && child_die->tag)
17534 {
17535 if (child_die->tag == DW_TAG_formal_parameter)
17536 {
3ce3b1ba
PA
17537 struct type *arg_type;
17538
17539 /* DWARF version 2 has no clean way to discern C++
17540 static and non-static member functions. G++ helps
17541 GDB by marking the first parameter for non-static
17542 member functions (which is the this pointer) as
17543 artificial. We pass this information to
17544 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17545
17546 DWARF version 3 added DW_AT_object_pointer, which GCC
17547 4.5 does not yet generate. */
e142c38c 17548 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17549 if (attr != nullptr)
c906108c
SS
17550 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17551 else
9c37b5ae 17552 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17553 arg_type = die_type (child_die, cu);
17554
17555 /* RealView does not mark THIS as const, which the testsuite
17556 expects. GCC marks THIS as const in method definitions,
17557 but not in the class specifications (GCC PR 43053). */
17558 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17559 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17560 {
17561 int is_this = 0;
17562 struct dwarf2_cu *arg_cu = cu;
17563 const char *name = dwarf2_name (child_die, cu);
17564
17565 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17566 if (attr != nullptr)
3ce3b1ba
PA
17567 {
17568 /* If the compiler emits this, use it. */
17569 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17570 is_this = 1;
17571 }
17572 else if (name && strcmp (name, "this") == 0)
17573 /* Function definitions will have the argument names. */
17574 is_this = 1;
17575 else if (name == NULL && iparams == 0)
17576 /* Declarations may not have the names, so like
17577 elsewhere in GDB, assume an artificial first
17578 argument is "this". */
17579 is_this = 1;
17580
17581 if (is_this)
17582 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17583 arg_type, 0);
17584 }
17585
17586 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17587 iparams++;
17588 }
17589 child_die = sibling_die (child_die);
17590 }
17591 }
17592
76c10ea2 17593 return ftype;
c906108c
SS
17594}
17595
f792889a 17596static struct type *
e7c27a73 17597read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17598{
518817b3 17599 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17600 const char *name = NULL;
3c8e0968 17601 struct type *this_type, *target_type;
c906108c 17602
94af9270 17603 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17604 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17605 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17606 set_die_type (die, this_type, cu);
3c8e0968
DE
17607 target_type = die_type (die, cu);
17608 if (target_type != this_type)
17609 TYPE_TARGET_TYPE (this_type) = target_type;
17610 else
17611 {
17612 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17613 spec and cause infinite loops in GDB. */
b98664d3 17614 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17615 "- DIE at %s [in module %s]"),
17616 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17617 TYPE_TARGET_TYPE (this_type) = NULL;
17618 }
f792889a 17619 return this_type;
c906108c
SS
17620}
17621
9b790ce7
UW
17622/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17623 (which may be different from NAME) to the architecture back-end to allow
17624 it to guess the correct format if necessary. */
17625
17626static struct type *
17627dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17628 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17629{
17630 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17631 const struct floatformat **format;
17632 struct type *type;
17633
17634 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17635 if (format)
103a685e 17636 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17637 else
77b7c781 17638 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17639
17640 return type;
17641}
17642
eb77c9df
AB
17643/* Allocate an integer type of size BITS and name NAME. */
17644
17645static struct type *
17646dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17647 int bits, int unsigned_p, const char *name)
17648{
17649 struct type *type;
17650
17651 /* Versions of Intel's C Compiler generate an integer type called "void"
17652 instead of using DW_TAG_unspecified_type. This has been seen on
17653 at least versions 14, 17, and 18. */
35ee2dc2
AB
17654 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17655 && strcmp (name, "void") == 0)
eb77c9df
AB
17656 type = objfile_type (objfile)->builtin_void;
17657 else
17658 type = init_integer_type (objfile, bits, unsigned_p, name);
17659
17660 return type;
17661}
17662
8bdc1658
AB
17663/* Initialise and return a floating point type of size BITS suitable for
17664 use as a component of a complex number. The NAME_HINT is passed through
17665 when initialising the floating point type and is the name of the complex
17666 type.
17667
17668 As DWARF doesn't currently provide an explicit name for the components
17669 of a complex number, but it can be helpful to have these components
17670 named, we try to select a suitable name based on the size of the
17671 component. */
17672static struct type *
17673dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17674 struct objfile *objfile,
103a685e
TT
17675 int bits, const char *name_hint,
17676 enum bfd_endian byte_order)
8bdc1658
AB
17677{
17678 gdbarch *gdbarch = get_objfile_arch (objfile);
17679 struct type *tt = nullptr;
17680
35add35e
AB
17681 /* Try to find a suitable floating point builtin type of size BITS.
17682 We're going to use the name of this type as the name for the complex
17683 target type that we are about to create. */
1db455a7 17684 switch (cu->language)
8bdc1658 17685 {
1db455a7
AB
17686 case language_fortran:
17687 switch (bits)
17688 {
17689 case 32:
17690 tt = builtin_f_type (gdbarch)->builtin_real;
17691 break;
17692 case 64:
17693 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17694 break;
17695 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17696 case 128:
17697 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17698 break;
17699 }
8bdc1658 17700 break;
1db455a7
AB
17701 default:
17702 switch (bits)
17703 {
17704 case 32:
17705 tt = builtin_type (gdbarch)->builtin_float;
17706 break;
17707 case 64:
17708 tt = builtin_type (gdbarch)->builtin_double;
17709 break;
17710 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17711 case 128:
17712 tt = builtin_type (gdbarch)->builtin_long_double;
17713 break;
17714 }
8bdc1658
AB
17715 break;
17716 }
17717
35add35e
AB
17718 /* If the type we found doesn't match the size we were looking for, then
17719 pretend we didn't find a type at all, the complex target type we
17720 create will then be nameless. */
a12e5744 17721 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17722 tt = nullptr;
17723
8bdc1658 17724 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17725 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17726}
17727
c906108c
SS
17728/* Find a representation of a given base type and install
17729 it in the TYPE field of the die. */
17730
f792889a 17731static struct type *
e7c27a73 17732read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17733{
518817b3 17734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17735 struct type *type;
17736 struct attribute *attr;
19f392bc 17737 int encoding = 0, bits = 0;
15d034d0 17738 const char *name;
34877895 17739 gdbarch *arch;
c906108c 17740
e142c38c 17741 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17742 if (attr != nullptr)
34877895 17743 encoding = DW_UNSND (attr);
e142c38c 17744 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17745 if (attr != nullptr)
34877895 17746 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17747 name = dwarf2_name (die, cu);
6ccb9162 17748 if (!name)
34877895 17749 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17750
17751 arch = get_objfile_arch (objfile);
17752 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17753
34877895
PJ
17754 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17755 if (attr)
103a685e
TT
17756 {
17757 int endianity = DW_UNSND (attr);
17758
17759 switch (endianity)
17760 {
17761 case DW_END_big:
17762 byte_order = BFD_ENDIAN_BIG;
17763 break;
17764 case DW_END_little:
17765 byte_order = BFD_ENDIAN_LITTLE;
17766 break;
17767 default:
17768 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17769 break;
17770 }
17771 }
6ccb9162
UW
17772
17773 switch (encoding)
c906108c 17774 {
6ccb9162
UW
17775 case DW_ATE_address:
17776 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17777 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17778 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17779 break;
17780 case DW_ATE_boolean:
19f392bc 17781 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17782 break;
17783 case DW_ATE_complex_float:
103a685e
TT
17784 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17785 byte_order);
19f392bc 17786 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17787 break;
17788 case DW_ATE_decimal_float:
19f392bc 17789 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17790 break;
17791 case DW_ATE_float:
103a685e 17792 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17793 break;
17794 case DW_ATE_signed:
eb77c9df 17795 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17796 break;
17797 case DW_ATE_unsigned:
3b2b8fea
TT
17798 if (cu->language == language_fortran
17799 && name
61012eef 17800 && startswith (name, "character("))
19f392bc
UW
17801 type = init_character_type (objfile, bits, 1, name);
17802 else
eb77c9df 17803 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17804 break;
17805 case DW_ATE_signed_char:
6e70227d 17806 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17807 || cu->language == language_pascal
17808 || cu->language == language_fortran)
19f392bc
UW
17809 type = init_character_type (objfile, bits, 0, name);
17810 else
eb77c9df 17811 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17812 break;
17813 case DW_ATE_unsigned_char:
868a0084 17814 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17815 || cu->language == language_pascal
c44af4eb
TT
17816 || cu->language == language_fortran
17817 || cu->language == language_rust)
19f392bc
UW
17818 type = init_character_type (objfile, bits, 1, name);
17819 else
eb77c9df 17820 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17821 break;
75079b2b 17822 case DW_ATE_UTF:
53e710ac 17823 {
53e710ac
PA
17824 if (bits == 16)
17825 type = builtin_type (arch)->builtin_char16;
17826 else if (bits == 32)
17827 type = builtin_type (arch)->builtin_char32;
17828 else
17829 {
b98664d3 17830 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17831 bits);
eb77c9df 17832 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17833 }
17834 return set_die_type (die, type, cu);
17835 }
75079b2b
TT
17836 break;
17837
6ccb9162 17838 default:
b98664d3 17839 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17840 dwarf_type_encoding_name (encoding));
77b7c781 17841 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17842 break;
c906108c 17843 }
6ccb9162 17844
0114d602 17845 if (name && strcmp (name, "char") == 0)
876cecd0 17846 TYPE_NOSIGN (type) = 1;
0114d602 17847
2b4424c3
TT
17848 maybe_set_alignment (cu, die, type);
17849
103a685e 17850 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17851
f792889a 17852 return set_die_type (die, type, cu);
c906108c
SS
17853}
17854
80180f79
SA
17855/* Parse dwarf attribute if it's a block, reference or constant and put the
17856 resulting value of the attribute into struct bound_prop.
17857 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17858
17859static int
17860attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17861 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17862 struct type *default_type)
80180f79
SA
17863{
17864 struct dwarf2_property_baton *baton;
518817b3
SM
17865 struct obstack *obstack
17866 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17867
9a49df9d
AB
17868 gdb_assert (default_type != NULL);
17869
80180f79
SA
17870 if (attr == NULL || prop == NULL)
17871 return 0;
17872
17873 if (attr_form_is_block (attr))
17874 {
8d749320 17875 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17876 baton->property_type = default_type;
80180f79
SA
17877 baton->locexpr.per_cu = cu->per_cu;
17878 baton->locexpr.size = DW_BLOCK (attr)->size;
17879 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17880 switch (attr->name)
17881 {
17882 case DW_AT_string_length:
17883 baton->locexpr.is_reference = true;
17884 break;
17885 default:
17886 baton->locexpr.is_reference = false;
17887 break;
17888 }
80180f79
SA
17889 prop->data.baton = baton;
17890 prop->kind = PROP_LOCEXPR;
17891 gdb_assert (prop->data.baton != NULL);
17892 }
17893 else if (attr_form_is_ref (attr))
17894 {
17895 struct dwarf2_cu *target_cu = cu;
17896 struct die_info *target_die;
17897 struct attribute *target_attr;
17898
17899 target_die = follow_die_ref (die, attr, &target_cu);
17900 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17901 if (target_attr == NULL)
17902 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17903 target_cu);
80180f79
SA
17904 if (target_attr == NULL)
17905 return 0;
17906
df25ebbd 17907 switch (target_attr->name)
80180f79 17908 {
df25ebbd
JB
17909 case DW_AT_location:
17910 if (attr_form_is_section_offset (target_attr))
17911 {
8d749320 17912 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17913 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17914 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17915 prop->data.baton = baton;
17916 prop->kind = PROP_LOCLIST;
17917 gdb_assert (prop->data.baton != NULL);
17918 }
17919 else if (attr_form_is_block (target_attr))
17920 {
8d749320 17921 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17922 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17923 baton->locexpr.per_cu = cu->per_cu;
17924 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17925 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17926 baton->locexpr.is_reference = true;
df25ebbd
JB
17927 prop->data.baton = baton;
17928 prop->kind = PROP_LOCEXPR;
17929 gdb_assert (prop->data.baton != NULL);
17930 }
17931 else
17932 {
17933 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17934 "dynamic property");
17935 return 0;
17936 }
17937 break;
17938 case DW_AT_data_member_location:
17939 {
17940 LONGEST offset;
17941
17942 if (!handle_data_member_location (target_die, target_cu,
17943 &offset))
17944 return 0;
17945
8d749320 17946 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17947 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17948 target_cu);
df25ebbd
JB
17949 baton->offset_info.offset = offset;
17950 baton->offset_info.type = die_type (target_die, target_cu);
17951 prop->data.baton = baton;
17952 prop->kind = PROP_ADDR_OFFSET;
17953 break;
17954 }
80180f79
SA
17955 }
17956 }
17957 else if (attr_form_is_constant (attr))
17958 {
17959 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17960 prop->kind = PROP_CONST;
17961 }
17962 else
17963 {
17964 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17965 dwarf2_name (die, cu));
17966 return 0;
17967 }
17968
17969 return 1;
17970}
17971
11a8b164
AB
17972/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17973 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17974
17975static struct type *
11a8b164
AB
17976dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17977 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17978{
17979 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17980 struct type *int_type;
17981
17982 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17983#define TRY_TYPE(F) \
17984 int_type = (unsigned_p \
17985 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17986 : objfile_type (objfile)->builtin_ ## F); \
17987 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17988 return int_type
17989
17990 TRY_TYPE (char);
17991 TRY_TYPE (short);
17992 TRY_TYPE (int);
17993 TRY_TYPE (long);
17994 TRY_TYPE (long_long);
17995
17996#undef TRY_TYPE
17997
17998 gdb_assert_not_reached ("unable to find suitable integer type");
17999}
18000
11a8b164
AB
18001/* Find an integer type the same size as the address size given in the
18002 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18003 is unsigned or not. */
18004
18005static struct type *
18006dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18007 bool unsigned_p)
18008{
18009 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18010 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18011}
18012
b86352cf
AB
18013/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18014 present (which is valid) then compute the default type based on the
18015 compilation units address size. */
18016
18017static struct type *
18018read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18019{
18020 struct type *index_type = die_type (die, cu);
18021
18022 /* Dwarf-2 specifications explicitly allows to create subrange types
18023 without specifying a base type.
18024 In that case, the base type must be set to the type of
18025 the lower bound, upper bound or count, in that order, if any of these
18026 three attributes references an object that has a type.
18027 If no base type is found, the Dwarf-2 specifications say that
18028 a signed integer type of size equal to the size of an address should
18029 be used.
18030 For the following C code: `extern char gdb_int [];'
18031 GCC produces an empty range DIE.
18032 FIXME: muller/2010-05-28: Possible references to object for low bound,
18033 high bound or count are not yet handled by this code. */
18034 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18035 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18036
18037 return index_type;
18038}
18039
a02abb62
JB
18040/* Read the given DW_AT_subrange DIE. */
18041
f792889a 18042static struct type *
a02abb62
JB
18043read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18044{
4c9ad8c2 18045 struct type *base_type, *orig_base_type;
a02abb62
JB
18046 struct type *range_type;
18047 struct attribute *attr;
729efb13 18048 struct dynamic_prop low, high;
4fae6e18 18049 int low_default_is_valid;
c451ebe5 18050 int high_bound_is_count = 0;
15d034d0 18051 const char *name;
d359392f 18052 ULONGEST negative_mask;
e77813c8 18053
b86352cf
AB
18054 orig_base_type = read_subrange_index_type (die, cu);
18055
4c9ad8c2
TT
18056 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18057 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18058 creating the range type, but we use the result of check_typedef
18059 when examining properties of the type. */
18060 base_type = check_typedef (orig_base_type);
a02abb62 18061
7e314c57
JK
18062 /* The die_type call above may have already set the type for this DIE. */
18063 range_type = get_die_type (die, cu);
18064 if (range_type)
18065 return range_type;
18066
729efb13
SA
18067 low.kind = PROP_CONST;
18068 high.kind = PROP_CONST;
18069 high.data.const_val = 0;
18070
4fae6e18
JK
18071 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18072 omitting DW_AT_lower_bound. */
18073 switch (cu->language)
6e70227d 18074 {
4fae6e18
JK
18075 case language_c:
18076 case language_cplus:
729efb13 18077 low.data.const_val = 0;
4fae6e18
JK
18078 low_default_is_valid = 1;
18079 break;
18080 case language_fortran:
729efb13 18081 low.data.const_val = 1;
4fae6e18
JK
18082 low_default_is_valid = 1;
18083 break;
18084 case language_d:
4fae6e18 18085 case language_objc:
c44af4eb 18086 case language_rust:
729efb13 18087 low.data.const_val = 0;
4fae6e18
JK
18088 low_default_is_valid = (cu->header.version >= 4);
18089 break;
18090 case language_ada:
18091 case language_m2:
18092 case language_pascal:
729efb13 18093 low.data.const_val = 1;
4fae6e18
JK
18094 low_default_is_valid = (cu->header.version >= 4);
18095 break;
18096 default:
729efb13 18097 low.data.const_val = 0;
4fae6e18
JK
18098 low_default_is_valid = 0;
18099 break;
a02abb62
JB
18100 }
18101
e142c38c 18102 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18103 if (attr != nullptr)
9a49df9d 18104 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18105 else if (!low_default_is_valid)
b98664d3 18106 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18107 "- DIE at %s [in module %s]"),
18108 sect_offset_str (die->sect_off),
518817b3 18109 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18110
506f5c41
TV
18111 struct attribute *attr_ub, *attr_count;
18112 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18113 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18114 {
506f5c41 18115 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18116 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18117 {
c451ebe5
SA
18118 /* If bounds are constant do the final calculation here. */
18119 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18120 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18121 else
18122 high_bound_is_count = 1;
c2ff108b 18123 }
506f5c41
TV
18124 else
18125 {
18126 if (attr_ub != NULL)
18127 complaint (_("Unresolved DW_AT_upper_bound "
18128 "- DIE at %s [in module %s]"),
18129 sect_offset_str (die->sect_off),
18130 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18131 if (attr_count != NULL)
18132 complaint (_("Unresolved DW_AT_count "
18133 "- DIE at %s [in module %s]"),
18134 sect_offset_str (die->sect_off),
18135 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18136 }
e77813c8 18137 }
a02abb62 18138
4e962e74
TT
18139 LONGEST bias = 0;
18140 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18141 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18142 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18143
dbb9c2b1
JB
18144 /* Normally, the DWARF producers are expected to use a signed
18145 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18146 But this is unfortunately not always the case, as witnessed
18147 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18148 is used instead. To work around that ambiguity, we treat
18149 the bounds as signed, and thus sign-extend their values, when
18150 the base type is signed. */
6e70227d 18151 negative_mask =
d359392f 18152 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18153 if (low.kind == PROP_CONST
18154 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18155 low.data.const_val |= negative_mask;
18156 if (high.kind == PROP_CONST
18157 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18158 high.data.const_val |= negative_mask;
43bbcdc2 18159
5bbd8269
AB
18160 /* Check for bit and byte strides. */
18161 struct dynamic_prop byte_stride_prop;
18162 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18163 if (attr_byte_stride != nullptr)
18164 {
18165 struct type *prop_type
18166 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18167 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18168 prop_type);
18169 }
18170
18171 struct dynamic_prop bit_stride_prop;
18172 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18173 if (attr_bit_stride != nullptr)
18174 {
18175 /* It only makes sense to have either a bit or byte stride. */
18176 if (attr_byte_stride != nullptr)
18177 {
18178 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18179 "- DIE at %s [in module %s]"),
18180 sect_offset_str (die->sect_off),
18181 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18182 attr_bit_stride = nullptr;
18183 }
18184 else
18185 {
18186 struct type *prop_type
18187 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18188 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18189 prop_type);
18190 }
18191 }
18192
18193 if (attr_byte_stride != nullptr
18194 || attr_bit_stride != nullptr)
18195 {
18196 bool byte_stride_p = (attr_byte_stride != nullptr);
18197 struct dynamic_prop *stride
18198 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18199
18200 range_type
18201 = create_range_type_with_stride (NULL, orig_base_type, &low,
18202 &high, bias, stride, byte_stride_p);
18203 }
18204 else
18205 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18206
c451ebe5
SA
18207 if (high_bound_is_count)
18208 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18209
c2ff108b
JK
18210 /* Ada expects an empty array on no boundary attributes. */
18211 if (attr == NULL && cu->language != language_ada)
729efb13 18212 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18213
39cbfefa
DJ
18214 name = dwarf2_name (die, cu);
18215 if (name)
18216 TYPE_NAME (range_type) = name;
6e70227d 18217
e142c38c 18218 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18219 if (attr != nullptr)
a02abb62
JB
18220 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18221
2b4424c3
TT
18222 maybe_set_alignment (cu, die, range_type);
18223
7e314c57
JK
18224 set_die_type (die, range_type, cu);
18225
18226 /* set_die_type should be already done. */
b4ba55a1
JB
18227 set_descriptive_type (range_type, die, cu);
18228
7e314c57 18229 return range_type;
a02abb62 18230}
6e70227d 18231
f792889a 18232static struct type *
81a17f79
JB
18233read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18234{
18235 struct type *type;
81a17f79 18236
518817b3
SM
18237 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18238 NULL);
0114d602 18239 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18240
74a2f8ff 18241 /* In Ada, an unspecified type is typically used when the description
85102364 18242 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18243 such a type, we treat it as a stub, and try to resolve it later on,
18244 when needed. */
18245 if (cu->language == language_ada)
18246 TYPE_STUB (type) = 1;
18247
f792889a 18248 return set_die_type (die, type, cu);
81a17f79 18249}
a02abb62 18250
639d11d3
DC
18251/* Read a single die and all its descendents. Set the die's sibling
18252 field to NULL; set other fields in the die correctly, and set all
18253 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18254 location of the info_ptr after reading all of those dies. PARENT
18255 is the parent of the die in question. */
18256
18257static struct die_info *
dee91e82 18258read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18259 const gdb_byte *info_ptr,
18260 const gdb_byte **new_info_ptr,
dee91e82 18261 struct die_info *parent)
639d11d3
DC
18262{
18263 struct die_info *die;
d521ce57 18264 const gdb_byte *cur_ptr;
639d11d3
DC
18265 int has_children;
18266
bf6af496 18267 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18268 if (die == NULL)
18269 {
18270 *new_info_ptr = cur_ptr;
18271 return NULL;
18272 }
93311388 18273 store_in_ref_table (die, reader->cu);
639d11d3
DC
18274
18275 if (has_children)
bf6af496 18276 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18277 else
18278 {
18279 die->child = NULL;
18280 *new_info_ptr = cur_ptr;
18281 }
18282
18283 die->sibling = NULL;
18284 die->parent = parent;
18285 return die;
18286}
18287
18288/* Read a die, all of its descendents, and all of its siblings; set
18289 all of the fields of all of the dies correctly. Arguments are as
18290 in read_die_and_children. */
18291
18292static struct die_info *
bf6af496 18293read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18294 const gdb_byte *info_ptr,
18295 const gdb_byte **new_info_ptr,
bf6af496 18296 struct die_info *parent)
639d11d3
DC
18297{
18298 struct die_info *first_die, *last_sibling;
d521ce57 18299 const gdb_byte *cur_ptr;
639d11d3 18300
c906108c 18301 cur_ptr = info_ptr;
639d11d3
DC
18302 first_die = last_sibling = NULL;
18303
18304 while (1)
c906108c 18305 {
639d11d3 18306 struct die_info *die
dee91e82 18307 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18308
1d325ec1 18309 if (die == NULL)
c906108c 18310 {
639d11d3
DC
18311 *new_info_ptr = cur_ptr;
18312 return first_die;
c906108c 18313 }
1d325ec1
DJ
18314
18315 if (!first_die)
18316 first_die = die;
c906108c 18317 else
1d325ec1
DJ
18318 last_sibling->sibling = die;
18319
18320 last_sibling = die;
c906108c 18321 }
c906108c
SS
18322}
18323
bf6af496
DE
18324/* Read a die, all of its descendents, and all of its siblings; set
18325 all of the fields of all of the dies correctly. Arguments are as
18326 in read_die_and_children.
18327 This the main entry point for reading a DIE and all its children. */
18328
18329static struct die_info *
18330read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18331 const gdb_byte *info_ptr,
18332 const gdb_byte **new_info_ptr,
bf6af496
DE
18333 struct die_info *parent)
18334{
18335 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18336 new_info_ptr, parent);
18337
b4f54984 18338 if (dwarf_die_debug)
bf6af496
DE
18339 {
18340 fprintf_unfiltered (gdb_stdlog,
18341 "Read die from %s@0x%x of %s:\n",
a32a8923 18342 get_section_name (reader->die_section),
bf6af496
DE
18343 (unsigned) (info_ptr - reader->die_section->buffer),
18344 bfd_get_filename (reader->abfd));
b4f54984 18345 dump_die (die, dwarf_die_debug);
bf6af496
DE
18346 }
18347
18348 return die;
18349}
18350
3019eac3
DE
18351/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18352 attributes.
18353 The caller is responsible for filling in the extra attributes
18354 and updating (*DIEP)->num_attrs.
18355 Set DIEP to point to a newly allocated die with its information,
18356 except for its child, sibling, and parent fields.
18357 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18358
d521ce57 18359static const gdb_byte *
3019eac3 18360read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18361 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18362 int *has_children, int num_extra_attrs)
93311388 18363{
b64f50a1 18364 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18365 struct abbrev_info *abbrev;
18366 struct die_info *die;
18367 struct dwarf2_cu *cu = reader->cu;
18368 bfd *abfd = reader->abfd;
18369
9c541725 18370 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18371 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18372 info_ptr += bytes_read;
18373 if (!abbrev_number)
18374 {
18375 *diep = NULL;
18376 *has_children = 0;
18377 return info_ptr;
18378 }
18379
685af9cd 18380 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18381 if (!abbrev)
348e048f
DE
18382 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18383 abbrev_number,
18384 bfd_get_filename (abfd));
18385
3019eac3 18386 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18387 die->sect_off = sect_off;
93311388
DE
18388 die->tag = abbrev->tag;
18389 die->abbrev = abbrev_number;
18390
3019eac3
DE
18391 /* Make the result usable.
18392 The caller needs to update num_attrs after adding the extra
18393 attributes. */
93311388
DE
18394 die->num_attrs = abbrev->num_attrs;
18395
18396 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18397 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18398 info_ptr);
93311388
DE
18399
18400 *diep = die;
18401 *has_children = abbrev->has_children;
18402 return info_ptr;
18403}
18404
3019eac3
DE
18405/* Read a die and all its attributes.
18406 Set DIEP to point to a newly allocated die with its information,
18407 except for its child, sibling, and parent fields.
18408 Set HAS_CHILDREN to tell whether the die has children or not. */
18409
d521ce57 18410static const gdb_byte *
3019eac3 18411read_full_die (const struct die_reader_specs *reader,
d521ce57 18412 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18413 int *has_children)
18414{
d521ce57 18415 const gdb_byte *result;
bf6af496
DE
18416
18417 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18418
b4f54984 18419 if (dwarf_die_debug)
bf6af496
DE
18420 {
18421 fprintf_unfiltered (gdb_stdlog,
18422 "Read die from %s@0x%x of %s:\n",
a32a8923 18423 get_section_name (reader->die_section),
bf6af496
DE
18424 (unsigned) (info_ptr - reader->die_section->buffer),
18425 bfd_get_filename (reader->abfd));
b4f54984 18426 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18427 }
18428
18429 return result;
3019eac3 18430}
433df2d4
DE
18431\f
18432/* Abbreviation tables.
3019eac3 18433
433df2d4 18434 In DWARF version 2, the description of the debugging information is
c906108c
SS
18435 stored in a separate .debug_abbrev section. Before we read any
18436 dies from a section we read in all abbreviations and install them
433df2d4
DE
18437 in a hash table. */
18438
18439/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18440
685af9cd
TT
18441struct abbrev_info *
18442abbrev_table::alloc_abbrev ()
433df2d4
DE
18443{
18444 struct abbrev_info *abbrev;
18445
685af9cd 18446 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18447 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18448
433df2d4
DE
18449 return abbrev;
18450}
18451
18452/* Add an abbreviation to the table. */
c906108c 18453
685af9cd
TT
18454void
18455abbrev_table::add_abbrev (unsigned int abbrev_number,
18456 struct abbrev_info *abbrev)
433df2d4
DE
18457{
18458 unsigned int hash_number;
18459
18460 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18461 abbrev->next = m_abbrevs[hash_number];
18462 m_abbrevs[hash_number] = abbrev;
433df2d4 18463}
dee91e82 18464
433df2d4
DE
18465/* Look up an abbrev in the table.
18466 Returns NULL if the abbrev is not found. */
18467
685af9cd
TT
18468struct abbrev_info *
18469abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18470{
433df2d4
DE
18471 unsigned int hash_number;
18472 struct abbrev_info *abbrev;
18473
18474 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18475 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18476
18477 while (abbrev)
18478 {
18479 if (abbrev->number == abbrev_number)
18480 return abbrev;
18481 abbrev = abbrev->next;
18482 }
18483 return NULL;
18484}
18485
18486/* Read in an abbrev table. */
18487
685af9cd 18488static abbrev_table_up
ed2dc618
SM
18489abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18490 struct dwarf2_section_info *section,
9c541725 18491 sect_offset sect_off)
433df2d4
DE
18492{
18493 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18494 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18495 const gdb_byte *abbrev_ptr;
c906108c
SS
18496 struct abbrev_info *cur_abbrev;
18497 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18498 unsigned int abbrev_form;
f3dd6933
DJ
18499 struct attr_abbrev *cur_attrs;
18500 unsigned int allocated_attrs;
c906108c 18501
685af9cd 18502 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18503
433df2d4 18504 dwarf2_read_section (objfile, section);
9c541725 18505 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18506 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18507 abbrev_ptr += bytes_read;
18508
f3dd6933 18509 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18510 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18511
0963b4bd 18512 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18513 while (abbrev_number)
18514 {
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
f3dd6933 18549 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18550 {
f3dd6933
DJ
18551 allocated_attrs += ATTR_ALLOC_CHUNK;
18552 cur_attrs
224c3ddb 18553 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18554 }
ae038cb0 18555
aead7601
SM
18556 cur_attrs[cur_abbrev->num_attrs].name
18557 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18558 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18559 = (enum dwarf_form) abbrev_form;
43988095 18560 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18561 ++cur_abbrev->num_attrs;
c906108c
SS
18562 }
18563
8d749320
SM
18564 cur_abbrev->attrs =
18565 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18566 cur_abbrev->num_attrs);
f3dd6933
DJ
18567 memcpy (cur_abbrev->attrs, cur_attrs,
18568 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18569
685af9cd 18570 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18571
18572 /* Get next abbreviation.
18573 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18574 always properly terminated with an abbrev number of 0.
18575 Exit loop if we encounter an abbreviation which we have
18576 already read (which means we are about to read the abbreviations
18577 for the next compile unit) or if the end of the abbreviation
18578 table is reached. */
433df2d4 18579 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18580 break;
18581 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18582 abbrev_ptr += bytes_read;
685af9cd 18583 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18584 break;
18585 }
f3dd6933
DJ
18586
18587 xfree (cur_attrs);
433df2d4 18588 return abbrev_table;
c906108c
SS
18589}
18590
72bf9492
DJ
18591/* Returns nonzero if TAG represents a type that we might generate a partial
18592 symbol for. */
18593
18594static int
18595is_type_tag_for_partial (int tag)
18596{
18597 switch (tag)
18598 {
18599#if 0
18600 /* Some types that would be reasonable to generate partial symbols for,
18601 that we don't at present. */
18602 case DW_TAG_array_type:
18603 case DW_TAG_file_type:
18604 case DW_TAG_ptr_to_member_type:
18605 case DW_TAG_set_type:
18606 case DW_TAG_string_type:
18607 case DW_TAG_subroutine_type:
18608#endif
18609 case DW_TAG_base_type:
18610 case DW_TAG_class_type:
680b30c7 18611 case DW_TAG_interface_type:
72bf9492
DJ
18612 case DW_TAG_enumeration_type:
18613 case DW_TAG_structure_type:
18614 case DW_TAG_subrange_type:
18615 case DW_TAG_typedef:
18616 case DW_TAG_union_type:
18617 return 1;
18618 default:
18619 return 0;
18620 }
18621}
18622
18623/* Load all DIEs that are interesting for partial symbols into memory. */
18624
18625static struct partial_die_info *
dee91e82 18626load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18627 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18628{
dee91e82 18629 struct dwarf2_cu *cu = reader->cu;
518817b3 18630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18631 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18632 unsigned int bytes_read;
5afb4e99 18633 unsigned int load_all = 0;
72bf9492
DJ
18634 int nesting_level = 1;
18635
18636 parent_die = NULL;
18637 last_die = NULL;
18638
7adf1e79
DE
18639 gdb_assert (cu->per_cu != NULL);
18640 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18641 load_all = 1;
18642
72bf9492
DJ
18643 cu->partial_dies
18644 = htab_create_alloc_ex (cu->header.length / 12,
18645 partial_die_hash,
18646 partial_die_eq,
18647 NULL,
18648 &cu->comp_unit_obstack,
18649 hashtab_obstack_allocate,
18650 dummy_obstack_deallocate);
18651
72bf9492
DJ
18652 while (1)
18653 {
685af9cd 18654 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18655
18656 /* A NULL abbrev means the end of a series of children. */
18657 if (abbrev == NULL)
18658 {
18659 if (--nesting_level == 0)
cd9983dd
YQ
18660 return first_die;
18661
72bf9492
DJ
18662 info_ptr += bytes_read;
18663 last_die = parent_die;
18664 parent_die = parent_die->die_parent;
18665 continue;
18666 }
18667
98bfdba5
PA
18668 /* Check for template arguments. We never save these; if
18669 they're seen, we just mark the parent, and go on our way. */
18670 if (parent_die != NULL
18671 && cu->language == language_cplus
18672 && (abbrev->tag == DW_TAG_template_type_param
18673 || abbrev->tag == DW_TAG_template_value_param))
18674 {
18675 parent_die->has_template_arguments = 1;
18676
18677 if (!load_all)
18678 {
18679 /* We don't need a partial DIE for the template argument. */
dee91e82 18680 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18681 continue;
18682 }
18683 }
18684
0d99eb77 18685 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18686 Skip their other children. */
18687 if (!load_all
18688 && cu->language == language_cplus
18689 && parent_die != NULL
18690 && parent_die->tag == DW_TAG_subprogram)
18691 {
dee91e82 18692 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18693 continue;
18694 }
18695
5afb4e99
DJ
18696 /* Check whether this DIE is interesting enough to save. Normally
18697 we would not be interested in members here, but there may be
18698 later variables referencing them via DW_AT_specification (for
18699 static members). */
18700 if (!load_all
18701 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18702 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18703 && abbrev->tag != DW_TAG_enumerator
18704 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18705 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18706 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18707 && abbrev->tag != DW_TAG_variable
5afb4e99 18708 && abbrev->tag != DW_TAG_namespace
f55ee35c 18709 && abbrev->tag != DW_TAG_module
95554aad 18710 && abbrev->tag != DW_TAG_member
74921315
KS
18711 && abbrev->tag != DW_TAG_imported_unit
18712 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18713 {
18714 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18715 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18716 continue;
18717 }
18718
6f06d47b
YQ
18719 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18720 abbrev);
cd9983dd 18721
48fbe735 18722 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18723
18724 /* This two-pass algorithm for processing partial symbols has a
18725 high cost in cache pressure. Thus, handle some simple cases
18726 here which cover the majority of C partial symbols. DIEs
18727 which neither have specification tags in them, nor could have
18728 specification tags elsewhere pointing at them, can simply be
18729 processed and discarded.
18730
18731 This segment is also optional; scan_partial_symbols and
18732 add_partial_symbol will handle these DIEs if we chain
18733 them in normally. When compilers which do not emit large
18734 quantities of duplicate debug information are more common,
18735 this code can probably be removed. */
18736
18737 /* Any complete simple types at the top level (pretty much all
18738 of them, for a language without namespaces), can be processed
18739 directly. */
18740 if (parent_die == NULL
cd9983dd
YQ
18741 && pdi.has_specification == 0
18742 && pdi.is_declaration == 0
18743 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18744 || pdi.tag == DW_TAG_base_type
18745 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18746 {
cd9983dd 18747 if (building_psymtab && pdi.name != NULL)
31edb802 18748 add_psymbol_to_list (pdi.name, false,
79748972 18749 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18750 psymbol_placement::STATIC,
1762568f 18751 0, cu->language, objfile);
cd9983dd 18752 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18753 continue;
18754 }
18755
d8228535
JK
18756 /* The exception for DW_TAG_typedef with has_children above is
18757 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18758 type_name_or_error will error on such types later.
d8228535
JK
18759
18760 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18761 it could not find the child DIEs referenced later, this is checked
18762 above. In correct DWARF DW_TAG_typedef should have no children. */
18763
cd9983dd 18764 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18765 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18766 "- DIE at %s [in module %s]"),
cd9983dd 18767 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18768
72bf9492
DJ
18769 /* If we're at the second level, and we're an enumerator, and
18770 our parent has no specification (meaning possibly lives in a
18771 namespace elsewhere), then we can add the partial symbol now
18772 instead of queueing it. */
cd9983dd 18773 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18774 && parent_die != NULL
18775 && parent_die->die_parent == NULL
18776 && parent_die->tag == DW_TAG_enumeration_type
18777 && parent_die->has_specification == 0)
18778 {
cd9983dd 18779 if (pdi.name == NULL)
b98664d3 18780 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18781 else if (building_psymtab)
31edb802 18782 add_psymbol_to_list (pdi.name, false,
79748972 18783 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18784 cu->language == language_cplus
75aedd27
TT
18785 ? psymbol_placement::GLOBAL
18786 : psymbol_placement::STATIC,
1762568f 18787 0, cu->language, objfile);
72bf9492 18788
cd9983dd 18789 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18790 continue;
18791 }
18792
cd9983dd 18793 struct partial_die_info *part_die
6f06d47b 18794 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18795
72bf9492
DJ
18796 /* We'll save this DIE so link it in. */
18797 part_die->die_parent = parent_die;
18798 part_die->die_sibling = NULL;
18799 part_die->die_child = NULL;
18800
18801 if (last_die && last_die == parent_die)
18802 last_die->die_child = part_die;
18803 else if (last_die)
18804 last_die->die_sibling = part_die;
18805
18806 last_die = part_die;
18807
18808 if (first_die == NULL)
18809 first_die = part_die;
18810
18811 /* Maybe add the DIE to the hash table. Not all DIEs that we
18812 find interesting need to be in the hash table, because we
18813 also have the parent/sibling/child chains; only those that we
18814 might refer to by offset later during partial symbol reading.
18815
18816 For now this means things that might have be the target of a
18817 DW_AT_specification, DW_AT_abstract_origin, or
18818 DW_AT_extension. DW_AT_extension will refer only to
18819 namespaces; DW_AT_abstract_origin refers to functions (and
18820 many things under the function DIE, but we do not recurse
18821 into function DIEs during partial symbol reading) and
18822 possibly variables as well; DW_AT_specification refers to
18823 declarations. Declarations ought to have the DW_AT_declaration
18824 flag. It happens that GCC forgets to put it in sometimes, but
18825 only for functions, not for types.
18826
18827 Adding more things than necessary to the hash table is harmless
18828 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18829 wasted time in find_partial_die, when we reread the compilation
18830 unit with load_all_dies set. */
72bf9492 18831
5afb4e99 18832 if (load_all
72929c62 18833 || abbrev->tag == DW_TAG_constant
5afb4e99 18834 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18835 || abbrev->tag == DW_TAG_variable
18836 || abbrev->tag == DW_TAG_namespace
18837 || part_die->is_declaration)
18838 {
18839 void **slot;
18840
18841 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18842 to_underlying (part_die->sect_off),
18843 INSERT);
72bf9492
DJ
18844 *slot = part_die;
18845 }
18846
72bf9492 18847 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18848 we have no reason to follow the children of structures; for other
98bfdba5
PA
18849 languages we have to, so that we can get at method physnames
18850 to infer fully qualified class names, for DW_AT_specification,
18851 and for C++ template arguments. For C++, we also look one level
18852 inside functions to find template arguments (if the name of the
18853 function does not already contain the template arguments).
bc30ff58 18854
0a4b0913
AB
18855 For Ada and Fortran, we need to scan the children of subprograms
18856 and lexical blocks as well because these languages allow the
18857 definition of nested entities that could be interesting for the
18858 debugger, such as nested subprograms for instance. */
72bf9492 18859 if (last_die->has_children
5afb4e99
DJ
18860 && (load_all
18861 || last_die->tag == DW_TAG_namespace
f55ee35c 18862 || last_die->tag == DW_TAG_module
72bf9492 18863 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18864 || (cu->language == language_cplus
18865 && last_die->tag == DW_TAG_subprogram
18866 && (last_die->name == NULL
18867 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18868 || (cu->language != language_c
18869 && (last_die->tag == DW_TAG_class_type
680b30c7 18870 || last_die->tag == DW_TAG_interface_type
72bf9492 18871 || last_die->tag == DW_TAG_structure_type
bc30ff58 18872 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18873 || ((cu->language == language_ada
18874 || cu->language == language_fortran)
bc30ff58
JB
18875 && (last_die->tag == DW_TAG_subprogram
18876 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18877 {
18878 nesting_level++;
18879 parent_die = last_die;
18880 continue;
18881 }
18882
18883 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18884 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18885
18886 /* Back to the top, do it again. */
18887 }
18888}
18889
6f06d47b
YQ
18890partial_die_info::partial_die_info (sect_offset sect_off_,
18891 struct abbrev_info *abbrev)
18892 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18893{
18894}
18895
35cc7ed7
YQ
18896/* Read a minimal amount of information into the minimal die structure.
18897 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18898
48fbe735
YQ
18899const gdb_byte *
18900partial_die_info::read (const struct die_reader_specs *reader,
18901 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18902{
dee91e82 18903 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18904 struct dwarf2_per_objfile *dwarf2_per_objfile
18905 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18906 unsigned int i;
c5aa993b 18907 int has_low_pc_attr = 0;
c906108c 18908 int has_high_pc_attr = 0;
91da1414 18909 int high_pc_relative = 0;
c906108c 18910
fd0a254f 18911 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18912 {
48fbe735
YQ
18913 struct attribute attr;
18914
fd0a254f 18915 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18916
18917 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18918 partial symbol table. */
c906108c
SS
18919 switch (attr.name)
18920 {
18921 case DW_AT_name:
48fbe735 18922 switch (tag)
71c25dea
TT
18923 {
18924 case DW_TAG_compile_unit:
95554aad 18925 case DW_TAG_partial_unit:
348e048f 18926 case DW_TAG_type_unit:
71c25dea
TT
18927 /* Compilation units have a DW_AT_name that is a filename, not
18928 a source language identifier. */
18929 case DW_TAG_enumeration_type:
18930 case DW_TAG_enumerator:
18931 /* These tags always have simple identifiers already; no need
18932 to canonicalize them. */
48fbe735 18933 name = DW_STRING (&attr);
71c25dea
TT
18934 break;
18935 default:
48fbe735
YQ
18936 {
18937 struct objfile *objfile = dwarf2_per_objfile->objfile;
18938
18939 name
18940 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18941 &objfile->per_bfd->storage_obstack);
18942 }
71c25dea
TT
18943 break;
18944 }
c906108c 18945 break;
31ef98ae 18946 case DW_AT_linkage_name:
c906108c 18947 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18948 /* Note that both forms of linkage name might appear. We
18949 assume they will be the same, and we only store the last
18950 one we see. */
48fbe735 18951 linkage_name = DW_STRING (&attr);
c906108c
SS
18952 break;
18953 case DW_AT_low_pc:
18954 has_low_pc_attr = 1;
48fbe735 18955 lowpc = attr_value_as_address (&attr);
c906108c
SS
18956 break;
18957 case DW_AT_high_pc:
18958 has_high_pc_attr = 1;
48fbe735 18959 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18960 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18961 high_pc_relative = 1;
c906108c
SS
18962 break;
18963 case DW_AT_location:
0963b4bd 18964 /* Support the .debug_loc offsets. */
8e19ed76
PS
18965 if (attr_form_is_block (&attr))
18966 {
48fbe735 18967 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18968 }
3690dd37 18969 else if (attr_form_is_section_offset (&attr))
8e19ed76 18970 {
4d3c2250 18971 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18972 }
18973 else
18974 {
4d3c2250
KB
18975 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18976 "partial symbol information");
8e19ed76 18977 }
c906108c 18978 break;
c906108c 18979 case DW_AT_external:
48fbe735 18980 is_external = DW_UNSND (&attr);
c906108c
SS
18981 break;
18982 case DW_AT_declaration:
48fbe735 18983 is_declaration = DW_UNSND (&attr);
c906108c
SS
18984 break;
18985 case DW_AT_type:
48fbe735 18986 has_type = 1;
c906108c
SS
18987 break;
18988 case DW_AT_abstract_origin:
18989 case DW_AT_specification:
72bf9492 18990 case DW_AT_extension:
48fbe735
YQ
18991 has_specification = 1;
18992 spec_offset = dwarf2_get_ref_die_offset (&attr);
18993 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18994 || cu->per_cu->is_dwz);
c906108c
SS
18995 break;
18996 case DW_AT_sibling:
18997 /* Ignore absolute siblings, they might point outside of
18998 the current compile unit. */
18999 if (attr.form == DW_FORM_ref_addr)
b98664d3 19000 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19001 else
b9502d3f 19002 {
48fbe735 19003 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19004 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19005 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19006
19007 if (sibling_ptr < info_ptr)
b98664d3 19008 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19009 else if (sibling_ptr > reader->buffer_end)
19010 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19011 else
48fbe735 19012 sibling = sibling_ptr;
b9502d3f 19013 }
c906108c 19014 break;
fa4028e9 19015 case DW_AT_byte_size:
48fbe735 19016 has_byte_size = 1;
fa4028e9 19017 break;
ff908ebf 19018 case DW_AT_const_value:
48fbe735 19019 has_const_value = 1;
ff908ebf 19020 break;
68511cec
CES
19021 case DW_AT_calling_convention:
19022 /* DWARF doesn't provide a way to identify a program's source-level
19023 entry point. DW_AT_calling_convention attributes are only meant
19024 to describe functions' calling conventions.
19025
19026 However, because it's a necessary piece of information in
0c1b455e
TT
19027 Fortran, and before DWARF 4 DW_CC_program was the only
19028 piece of debugging information whose definition refers to
19029 a 'main program' at all, several compilers marked Fortran
19030 main programs with DW_CC_program --- even when those
19031 functions use the standard calling conventions.
19032
19033 Although DWARF now specifies a way to provide this
19034 information, we support this practice for backward
19035 compatibility. */
68511cec 19036 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19037 && cu->language == language_fortran)
48fbe735 19038 main_subprogram = 1;
68511cec 19039 break;
481860b3
GB
19040 case DW_AT_inline:
19041 if (DW_UNSND (&attr) == DW_INL_inlined
19042 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19043 may_be_inlined = 1;
481860b3 19044 break;
95554aad
TT
19045
19046 case DW_AT_import:
48fbe735 19047 if (tag == DW_TAG_imported_unit)
36586728 19048 {
48fbe735
YQ
19049 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19050 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19051 || cu->per_cu->is_dwz);
19052 }
95554aad
TT
19053 break;
19054
0c1b455e 19055 case DW_AT_main_subprogram:
48fbe735 19056 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19057 break;
19058
05caa1d2
TT
19059 case DW_AT_ranges:
19060 {
19061 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19062 but that requires a full DIE, so instead we just
19063 reimplement it. */
19064 int need_ranges_base = tag != DW_TAG_compile_unit;
19065 unsigned int ranges_offset = (DW_UNSND (&attr)
19066 + (need_ranges_base
19067 ? cu->ranges_base
19068 : 0));
19069
19070 /* Value of the DW_AT_ranges attribute is the offset in the
19071 .debug_ranges section. */
19072 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19073 nullptr))
19074 has_pc_info = 1;
19075 }
19076 break;
19077
c906108c
SS
19078 default:
19079 break;
19080 }
19081 }
19082
10d06d82
TT
19083 /* For Ada, if both the name and the linkage name appear, we prefer
19084 the latter. This lets "catch exception" work better, regardless
19085 of the order in which the name and linkage name were emitted.
19086 Really, though, this is just a workaround for the fact that gdb
19087 doesn't store both the name and the linkage name. */
19088 if (cu->language == language_ada && linkage_name != nullptr)
19089 name = linkage_name;
19090
91da1414 19091 if (high_pc_relative)
48fbe735 19092 highpc += lowpc;
91da1414 19093
9373cf26
JK
19094 if (has_low_pc_attr && has_high_pc_attr)
19095 {
19096 /* When using the GNU linker, .gnu.linkonce. sections are used to
19097 eliminate duplicate copies of functions and vtables and such.
19098 The linker will arbitrarily choose one and discard the others.
19099 The AT_*_pc values for such functions refer to local labels in
19100 these sections. If the section from that file was discarded, the
19101 labels are not in the output, so the relocs get a value of 0.
19102 If this is a discarded function, mark the pc bounds as invalid,
19103 so that GDB will ignore it. */
48fbe735 19104 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19105 {
48fbe735 19106 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19108
b98664d3 19109 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19110 "for DIE at %s [in module %s]"),
48fbe735
YQ
19111 paddress (gdbarch, lowpc),
19112 sect_offset_str (sect_off),
9d8780f0 19113 objfile_name (objfile));
9373cf26
JK
19114 }
19115 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19116 else if (lowpc >= highpc)
9373cf26 19117 {
48fbe735 19118 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19120
b98664d3 19121 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19122 "for DIE at %s [in module %s]"),
48fbe735
YQ
19123 paddress (gdbarch, lowpc),
19124 paddress (gdbarch, highpc),
19125 sect_offset_str (sect_off),
9c541725 19126 objfile_name (objfile));
9373cf26
JK
19127 }
19128 else
48fbe735 19129 has_pc_info = 1;
9373cf26 19130 }
85cbf3d3 19131
c906108c
SS
19132 return info_ptr;
19133}
19134
72bf9492
DJ
19135/* Find a cached partial DIE at OFFSET in CU. */
19136
d590ff25
YQ
19137struct partial_die_info *
19138dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19139{
19140 struct partial_die_info *lookup_die = NULL;
6f06d47b 19141 struct partial_die_info part_die (sect_off);
72bf9492 19142
9a3c8263 19143 lookup_die = ((struct partial_die_info *)
d590ff25 19144 htab_find_with_hash (partial_dies, &part_die,
9c541725 19145 to_underlying (sect_off)));
72bf9492 19146
72bf9492
DJ
19147 return lookup_die;
19148}
19149
348e048f
DE
19150/* Find a partial DIE at OFFSET, which may or may not be in CU,
19151 except in the case of .debug_types DIEs which do not reference
19152 outside their CU (they do however referencing other types via
55f1336d 19153 DW_FORM_ref_sig8). */
72bf9492 19154
122cf0f2 19155static const struct cu_partial_die_info
9c541725 19156find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19157{
518817b3
SM
19158 struct dwarf2_per_objfile *dwarf2_per_objfile
19159 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19160 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19161 struct dwarf2_per_cu_data *per_cu = NULL;
19162 struct partial_die_info *pd = NULL;
72bf9492 19163
36586728 19164 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19165 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19166 {
d590ff25 19167 pd = cu->find_partial_die (sect_off);
5afb4e99 19168 if (pd != NULL)
fb816e8b 19169 return { cu, pd };
0d99eb77
DE
19170 /* We missed recording what we needed.
19171 Load all dies and try again. */
19172 per_cu = cu->per_cu;
5afb4e99 19173 }
0d99eb77
DE
19174 else
19175 {
19176 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19177 if (cu->per_cu->is_debug_types)
0d99eb77 19178 {
9d8780f0
SM
19179 error (_("Dwarf Error: Type Unit at offset %s contains"
19180 " external reference to offset %s [in module %s].\n"),
19181 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19182 bfd_get_filename (objfile->obfd));
19183 }
9c541725 19184 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19185 dwarf2_per_objfile);
72bf9492 19186
0d99eb77
DE
19187 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19188 load_partial_comp_unit (per_cu);
ae038cb0 19189
0d99eb77 19190 per_cu->cu->last_used = 0;
d590ff25 19191 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19192 }
5afb4e99 19193
dee91e82
DE
19194 /* If we didn't find it, and not all dies have been loaded,
19195 load them all and try again. */
19196
5afb4e99
DJ
19197 if (pd == NULL && per_cu->load_all_dies == 0)
19198 {
5afb4e99 19199 per_cu->load_all_dies = 1;
fd820528
DE
19200
19201 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19202 THIS_CU->cu may already be in use. So we can't just free it and
19203 replace its DIEs with the ones we read in. Instead, we leave those
19204 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19205 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19206 set. */
dee91e82 19207 load_partial_comp_unit (per_cu);
5afb4e99 19208
d590ff25 19209 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19210 }
19211
19212 if (pd == NULL)
19213 internal_error (__FILE__, __LINE__,
9d8780f0 19214 _("could not find partial DIE %s "
3e43a32a 19215 "in cache [from module %s]\n"),
9d8780f0 19216 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19217 return { per_cu->cu, pd };
72bf9492
DJ
19218}
19219
abc72ce4
DE
19220/* See if we can figure out if the class lives in a namespace. We do
19221 this by looking for a member function; its demangled name will
19222 contain namespace info, if there is any. */
19223
19224static void
19225guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19226 struct dwarf2_cu *cu)
19227{
19228 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19229 what template types look like, because the demangler
19230 frequently doesn't give the same name as the debug info. We
19231 could fix this by only using the demangled name to get the
19232 prefix (but see comment in read_structure_type). */
19233
19234 struct partial_die_info *real_pdi;
19235 struct partial_die_info *child_pdi;
19236
19237 /* If this DIE (this DIE's specification, if any) has a parent, then
19238 we should not do this. We'll prepend the parent's fully qualified
19239 name when we create the partial symbol. */
19240
19241 real_pdi = struct_pdi;
19242 while (real_pdi->has_specification)
fb816e8b 19243 {
122cf0f2
AB
19244 auto res = find_partial_die (real_pdi->spec_offset,
19245 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19246 real_pdi = res.pdi;
19247 cu = res.cu;
19248 }
abc72ce4
DE
19249
19250 if (real_pdi->die_parent != NULL)
19251 return;
19252
19253 for (child_pdi = struct_pdi->die_child;
19254 child_pdi != NULL;
19255 child_pdi = child_pdi->die_sibling)
19256 {
19257 if (child_pdi->tag == DW_TAG_subprogram
19258 && child_pdi->linkage_name != NULL)
19259 {
19260 char *actual_class_name
19261 = language_class_name_from_physname (cu->language_defn,
19262 child_pdi->linkage_name);
19263 if (actual_class_name != NULL)
19264 {
518817b3 19265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19266 struct_pdi->name
021887d8
TT
19267 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19268 actual_class_name);
abc72ce4
DE
19269 xfree (actual_class_name);
19270 }
19271 break;
19272 }
19273 }
19274}
19275
52356b79
YQ
19276void
19277partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19278{
abc72ce4
DE
19279 /* Once we've fixed up a die, there's no point in doing so again.
19280 This also avoids a memory leak if we were to call
19281 guess_partial_die_structure_name multiple times. */
52356b79 19282 if (fixup_called)
abc72ce4
DE
19283 return;
19284
72bf9492
DJ
19285 /* If we found a reference attribute and the DIE has no name, try
19286 to find a name in the referred to DIE. */
19287
52356b79 19288 if (name == NULL && has_specification)
72bf9492
DJ
19289 {
19290 struct partial_die_info *spec_die;
72bf9492 19291
122cf0f2 19292 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19293 spec_die = res.pdi;
19294 cu = res.cu;
72bf9492 19295
52356b79 19296 spec_die->fixup (cu);
72bf9492
DJ
19297
19298 if (spec_die->name)
19299 {
52356b79 19300 name = spec_die->name;
72bf9492
DJ
19301
19302 /* Copy DW_AT_external attribute if it is set. */
19303 if (spec_die->is_external)
52356b79 19304 is_external = spec_die->is_external;
72bf9492
DJ
19305 }
19306 }
19307
19308 /* Set default names for some unnamed DIEs. */
72bf9492 19309
52356b79
YQ
19310 if (name == NULL && tag == DW_TAG_namespace)
19311 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19312
abc72ce4
DE
19313 /* If there is no parent die to provide a namespace, and there are
19314 children, see if we can determine the namespace from their linkage
122d1940 19315 name. */
abc72ce4 19316 if (cu->language == language_cplus
fd5866f6 19317 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19318 && die_parent == NULL
19319 && has_children
19320 && (tag == DW_TAG_class_type
19321 || tag == DW_TAG_structure_type
19322 || tag == DW_TAG_union_type))
19323 guess_partial_die_structure_name (this, cu);
abc72ce4 19324
53832f31
TT
19325 /* GCC might emit a nameless struct or union that has a linkage
19326 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19327 if (name == NULL
19328 && (tag == DW_TAG_class_type
19329 || tag == DW_TAG_interface_type
19330 || tag == DW_TAG_structure_type
19331 || tag == DW_TAG_union_type)
19332 && linkage_name != NULL)
53832f31
TT
19333 {
19334 char *demangled;
19335
52356b79 19336 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19337 if (demangled)
19338 {
96408a79
SA
19339 const char *base;
19340
19341 /* Strip any leading namespaces/classes, keep only the base name.
19342 DW_AT_name for named DIEs does not contain the prefixes. */
19343 base = strrchr (demangled, ':');
19344 if (base && base > demangled && base[-1] == ':')
19345 base++;
19346 else
19347 base = demangled;
19348
518817b3 19349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19350 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19351 xfree (demangled);
19352 }
19353 }
19354
52356b79 19355 fixup_called = 1;
72bf9492
DJ
19356}
19357
a8329558 19358/* Read an attribute value described by an attribute form. */
c906108c 19359
d521ce57 19360static const gdb_byte *
dee91e82
DE
19361read_attribute_value (const struct die_reader_specs *reader,
19362 struct attribute *attr, unsigned form,
43988095 19363 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19364{
dee91e82 19365 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19366 struct dwarf2_per_objfile *dwarf2_per_objfile
19367 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19368 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19369 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19370 bfd *abfd = reader->abfd;
e7c27a73 19371 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19372 unsigned int bytes_read;
19373 struct dwarf_block *blk;
19374
aead7601 19375 attr->form = (enum dwarf_form) form;
a8329558 19376 switch (form)
c906108c 19377 {
c906108c 19378 case DW_FORM_ref_addr:
ae411497 19379 if (cu->header.version == 2)
4568ecf9 19380 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19381 else
4568ecf9
DE
19382 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19383 &cu->header, &bytes_read);
ae411497
TT
19384 info_ptr += bytes_read;
19385 break;
36586728
TT
19386 case DW_FORM_GNU_ref_alt:
19387 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19388 info_ptr += bytes_read;
19389 break;
ae411497 19390 case DW_FORM_addr:
e7c27a73 19391 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19392 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19393 info_ptr += bytes_read;
c906108c
SS
19394 break;
19395 case DW_FORM_block2:
7b5a2f43 19396 blk = dwarf_alloc_block (cu);
c906108c
SS
19397 blk->size = read_2_bytes (abfd, info_ptr);
19398 info_ptr += 2;
19399 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19400 info_ptr += blk->size;
19401 DW_BLOCK (attr) = blk;
19402 break;
19403 case DW_FORM_block4:
7b5a2f43 19404 blk = dwarf_alloc_block (cu);
c906108c
SS
19405 blk->size = read_4_bytes (abfd, info_ptr);
19406 info_ptr += 4;
19407 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19408 info_ptr += blk->size;
19409 DW_BLOCK (attr) = blk;
19410 break;
19411 case DW_FORM_data2:
19412 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19413 info_ptr += 2;
19414 break;
19415 case DW_FORM_data4:
19416 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19417 info_ptr += 4;
19418 break;
19419 case DW_FORM_data8:
19420 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19421 info_ptr += 8;
19422 break;
0224619f
JK
19423 case DW_FORM_data16:
19424 blk = dwarf_alloc_block (cu);
19425 blk->size = 16;
19426 blk->data = read_n_bytes (abfd, info_ptr, 16);
19427 info_ptr += 16;
19428 DW_BLOCK (attr) = blk;
19429 break;
2dc7f7b3
TT
19430 case DW_FORM_sec_offset:
19431 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19432 info_ptr += bytes_read;
19433 break;
c906108c 19434 case DW_FORM_string:
9b1c24c8 19435 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19436 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19437 info_ptr += bytes_read;
19438 break;
4bdf3d34 19439 case DW_FORM_strp:
36586728
TT
19440 if (!cu->per_cu->is_dwz)
19441 {
ed2dc618
SM
19442 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19443 abfd, info_ptr, cu_header,
36586728
TT
19444 &bytes_read);
19445 DW_STRING_IS_CANONICAL (attr) = 0;
19446 info_ptr += bytes_read;
19447 break;
19448 }
19449 /* FALLTHROUGH */
43988095
JK
19450 case DW_FORM_line_strp:
19451 if (!cu->per_cu->is_dwz)
19452 {
ed2dc618
SM
19453 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19454 abfd, info_ptr,
43988095
JK
19455 cu_header, &bytes_read);
19456 DW_STRING_IS_CANONICAL (attr) = 0;
19457 info_ptr += bytes_read;
19458 break;
19459 }
19460 /* FALLTHROUGH */
36586728
TT
19461 case DW_FORM_GNU_strp_alt:
19462 {
ed2dc618 19463 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19464 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19465 &bytes_read);
19466
ed2dc618
SM
19467 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19468 dwz, str_offset);
36586728
TT
19469 DW_STRING_IS_CANONICAL (attr) = 0;
19470 info_ptr += bytes_read;
19471 }
4bdf3d34 19472 break;
2dc7f7b3 19473 case DW_FORM_exprloc:
c906108c 19474 case DW_FORM_block:
7b5a2f43 19475 blk = dwarf_alloc_block (cu);
c906108c
SS
19476 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19477 info_ptr += bytes_read;
19478 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19479 info_ptr += blk->size;
19480 DW_BLOCK (attr) = blk;
19481 break;
19482 case DW_FORM_block1:
7b5a2f43 19483 blk = dwarf_alloc_block (cu);
c906108c
SS
19484 blk->size = read_1_byte (abfd, info_ptr);
19485 info_ptr += 1;
19486 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19487 info_ptr += blk->size;
19488 DW_BLOCK (attr) = blk;
19489 break;
19490 case DW_FORM_data1:
19491 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19492 info_ptr += 1;
19493 break;
19494 case DW_FORM_flag:
19495 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19496 info_ptr += 1;
19497 break;
2dc7f7b3
TT
19498 case DW_FORM_flag_present:
19499 DW_UNSND (attr) = 1;
19500 break;
c906108c
SS
19501 case DW_FORM_sdata:
19502 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19503 info_ptr += bytes_read;
19504 break;
19505 case DW_FORM_udata:
19506 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19507 info_ptr += bytes_read;
19508 break;
19509 case DW_FORM_ref1:
9c541725 19510 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19511 + read_1_byte (abfd, info_ptr));
c906108c
SS
19512 info_ptr += 1;
19513 break;
19514 case DW_FORM_ref2:
9c541725 19515 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19516 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19517 info_ptr += 2;
19518 break;
19519 case DW_FORM_ref4:
9c541725 19520 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19521 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19522 info_ptr += 4;
19523 break;
613e1657 19524 case DW_FORM_ref8:
9c541725 19525 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19526 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19527 info_ptr += 8;
19528 break;
55f1336d 19529 case DW_FORM_ref_sig8:
ac9ec31b 19530 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19531 info_ptr += 8;
19532 break;
c906108c 19533 case DW_FORM_ref_udata:
9c541725 19534 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19535 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19536 info_ptr += bytes_read;
19537 break;
c906108c 19538 case DW_FORM_indirect:
a8329558
KW
19539 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19540 info_ptr += bytes_read;
43988095
JK
19541 if (form == DW_FORM_implicit_const)
19542 {
19543 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19544 info_ptr += bytes_read;
19545 }
19546 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19547 info_ptr);
19548 break;
19549 case DW_FORM_implicit_const:
19550 DW_SND (attr) = implicit_const;
a8329558 19551 break;
336d760d 19552 case DW_FORM_addrx:
3019eac3
DE
19553 case DW_FORM_GNU_addr_index:
19554 if (reader->dwo_file == NULL)
19555 {
19556 /* For now flag a hard error.
19557 Later we can turn this into a complaint. */
19558 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19559 dwarf_form_name (form),
19560 bfd_get_filename (abfd));
19561 }
19562 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19563 info_ptr += bytes_read;
19564 break;
cf532bd1 19565 case DW_FORM_strx:
15f18d14
AT
19566 case DW_FORM_strx1:
19567 case DW_FORM_strx2:
19568 case DW_FORM_strx3:
19569 case DW_FORM_strx4:
3019eac3
DE
19570 case DW_FORM_GNU_str_index:
19571 if (reader->dwo_file == NULL)
19572 {
19573 /* For now flag a hard error.
19574 Later we can turn this into a complaint if warranted. */
19575 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19576 dwarf_form_name (form),
19577 bfd_get_filename (abfd));
19578 }
19579 {
15f18d14
AT
19580 ULONGEST str_index;
19581 if (form == DW_FORM_strx1)
19582 {
19583 str_index = read_1_byte (abfd, info_ptr);
19584 info_ptr += 1;
19585 }
19586 else if (form == DW_FORM_strx2)
19587 {
19588 str_index = read_2_bytes (abfd, info_ptr);
19589 info_ptr += 2;
19590 }
19591 else if (form == DW_FORM_strx3)
19592 {
19593 str_index = read_3_bytes (abfd, info_ptr);
19594 info_ptr += 3;
19595 }
19596 else if (form == DW_FORM_strx4)
19597 {
19598 str_index = read_4_bytes (abfd, info_ptr);
19599 info_ptr += 4;
19600 }
19601 else
19602 {
19603 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19604 info_ptr += bytes_read;
19605 }
342587c4 19606 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19607 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19608 }
19609 break;
c906108c 19610 default:
8a3fe4f8 19611 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19612 dwarf_form_name (form),
19613 bfd_get_filename (abfd));
c906108c 19614 }
28e94949 19615
36586728 19616 /* Super hack. */
7771576e 19617 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19618 attr->form = DW_FORM_GNU_ref_alt;
19619
28e94949
JB
19620 /* We have seen instances where the compiler tried to emit a byte
19621 size attribute of -1 which ended up being encoded as an unsigned
19622 0xffffffff. Although 0xffffffff is technically a valid size value,
19623 an object of this size seems pretty unlikely so we can relatively
19624 safely treat these cases as if the size attribute was invalid and
19625 treat them as zero by default. */
19626 if (attr->name == DW_AT_byte_size
19627 && form == DW_FORM_data4
19628 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19629 {
19630 complaint
b98664d3 19631 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19632 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19633 DW_UNSND (attr) = 0;
19634 }
28e94949 19635
c906108c
SS
19636 return info_ptr;
19637}
19638
a8329558
KW
19639/* Read an attribute described by an abbreviated attribute. */
19640
d521ce57 19641static const gdb_byte *
dee91e82
DE
19642read_attribute (const struct die_reader_specs *reader,
19643 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19644 const gdb_byte *info_ptr)
a8329558
KW
19645{
19646 attr->name = abbrev->name;
43988095
JK
19647 return read_attribute_value (reader, attr, abbrev->form,
19648 abbrev->implicit_const, info_ptr);
a8329558
KW
19649}
19650
0963b4bd 19651/* Read dwarf information from a buffer. */
c906108c
SS
19652
19653static unsigned int
a1855c1d 19654read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19655{
fe1b8b76 19656 return bfd_get_8 (abfd, buf);
c906108c
SS
19657}
19658
19659static int
a1855c1d 19660read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19661{
fe1b8b76 19662 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19663}
19664
19665static unsigned int
a1855c1d 19666read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19667{
fe1b8b76 19668 return bfd_get_16 (abfd, buf);
c906108c
SS
19669}
19670
21ae7a4d 19671static int
a1855c1d 19672read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19673{
19674 return bfd_get_signed_16 (abfd, buf);
19675}
19676
15f18d14
AT
19677static unsigned int
19678read_3_bytes (bfd *abfd, const gdb_byte *buf)
19679{
19680 unsigned int result = 0;
19681 for (int i = 0; i < 3; ++i)
19682 {
19683 unsigned char byte = bfd_get_8 (abfd, buf);
19684 buf++;
19685 result |= ((unsigned int) byte << (i * 8));
19686 }
19687 return result;
19688}
19689
c906108c 19690static unsigned int
a1855c1d 19691read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19692{
fe1b8b76 19693 return bfd_get_32 (abfd, buf);
c906108c
SS
19694}
19695
21ae7a4d 19696static int
a1855c1d 19697read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19698{
19699 return bfd_get_signed_32 (abfd, buf);
19700}
19701
93311388 19702static ULONGEST
a1855c1d 19703read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19704{
fe1b8b76 19705 return bfd_get_64 (abfd, buf);
c906108c
SS
19706}
19707
19708static CORE_ADDR
d521ce57 19709read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19710 unsigned int *bytes_read)
c906108c 19711{
e7c27a73 19712 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19713 CORE_ADDR retval = 0;
19714
107d2387 19715 if (cu_header->signed_addr_p)
c906108c 19716 {
107d2387
AC
19717 switch (cu_header->addr_size)
19718 {
19719 case 2:
fe1b8b76 19720 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19721 break;
19722 case 4:
fe1b8b76 19723 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19724 break;
19725 case 8:
fe1b8b76 19726 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19727 break;
19728 default:
8e65ff28 19729 internal_error (__FILE__, __LINE__,
e2e0b3e5 19730 _("read_address: bad switch, signed [in module %s]"),
659b0389 19731 bfd_get_filename (abfd));
107d2387
AC
19732 }
19733 }
19734 else
19735 {
19736 switch (cu_header->addr_size)
19737 {
19738 case 2:
fe1b8b76 19739 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19740 break;
19741 case 4:
fe1b8b76 19742 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19743 break;
19744 case 8:
fe1b8b76 19745 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19746 break;
19747 default:
8e65ff28 19748 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19749 _("read_address: bad switch, "
19750 "unsigned [in module %s]"),
659b0389 19751 bfd_get_filename (abfd));
107d2387 19752 }
c906108c 19753 }
64367e0a 19754
107d2387
AC
19755 *bytes_read = cu_header->addr_size;
19756 return retval;
c906108c
SS
19757}
19758
f7ef9339 19759/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19760 specification allows the initial length to take up either 4 bytes
19761 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19762 bytes describe the length and all offsets will be 8 bytes in length
19763 instead of 4.
19764
f7ef9339
KB
19765 An older, non-standard 64-bit format is also handled by this
19766 function. The older format in question stores the initial length
19767 as an 8-byte quantity without an escape value. Lengths greater
19768 than 2^32 aren't very common which means that the initial 4 bytes
19769 is almost always zero. Since a length value of zero doesn't make
19770 sense for the 32-bit format, this initial zero can be considered to
19771 be an escape value which indicates the presence of the older 64-bit
19772 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19773 greater than 4GB. If it becomes necessary to handle lengths
19774 somewhat larger than 4GB, we could allow other small values (such
19775 as the non-sensical values of 1, 2, and 3) to also be used as
19776 escape values indicating the presence of the old format.
f7ef9339 19777
917c78fc
MK
19778 The value returned via bytes_read should be used to increment the
19779 relevant pointer after calling read_initial_length().
c764a876 19780
613e1657
KB
19781 [ Note: read_initial_length() and read_offset() are based on the
19782 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19783 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19784 from:
19785
f7ef9339 19786 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19787
613e1657
KB
19788 This document is only a draft and is subject to change. (So beware.)
19789
f7ef9339 19790 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19791 determined empirically by examining 64-bit ELF files produced by
19792 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19793
19794 - Kevin, July 16, 2002
613e1657
KB
19795 ] */
19796
19797static LONGEST
d521ce57 19798read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19799{
fe1b8b76 19800 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19801
dd373385 19802 if (length == 0xffffffff)
613e1657 19803 {
fe1b8b76 19804 length = bfd_get_64 (abfd, buf + 4);
613e1657 19805 *bytes_read = 12;
613e1657 19806 }
dd373385 19807 else if (length == 0)
f7ef9339 19808 {
dd373385 19809 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19810 length = bfd_get_64 (abfd, buf);
f7ef9339 19811 *bytes_read = 8;
f7ef9339 19812 }
613e1657
KB
19813 else
19814 {
19815 *bytes_read = 4;
613e1657
KB
19816 }
19817
c764a876
DE
19818 return length;
19819}
dd373385 19820
c764a876
DE
19821/* Cover function for read_initial_length.
19822 Returns the length of the object at BUF, and stores the size of the
19823 initial length in *BYTES_READ and stores the size that offsets will be in
19824 *OFFSET_SIZE.
19825 If the initial length size is not equivalent to that specified in
19826 CU_HEADER then issue a complaint.
19827 This is useful when reading non-comp-unit headers. */
dd373385 19828
c764a876 19829static LONGEST
d521ce57 19830read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19831 const struct comp_unit_head *cu_header,
19832 unsigned int *bytes_read,
19833 unsigned int *offset_size)
19834{
19835 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19836
19837 gdb_assert (cu_header->initial_length_size == 4
19838 || cu_header->initial_length_size == 8
19839 || cu_header->initial_length_size == 12);
19840
19841 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19842 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19843
c764a876 19844 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19845 return length;
613e1657
KB
19846}
19847
19848/* Read an offset from the data stream. The size of the offset is
917c78fc 19849 given by cu_header->offset_size. */
613e1657
KB
19850
19851static LONGEST
d521ce57
TT
19852read_offset (bfd *abfd, const gdb_byte *buf,
19853 const struct comp_unit_head *cu_header,
891d2f0b 19854 unsigned int *bytes_read)
c764a876
DE
19855{
19856 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19857
c764a876
DE
19858 *bytes_read = cu_header->offset_size;
19859 return offset;
19860}
19861
19862/* Read an offset from the data stream. */
19863
19864static LONGEST
d521ce57 19865read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19866{
19867 LONGEST retval = 0;
19868
c764a876 19869 switch (offset_size)
613e1657
KB
19870 {
19871 case 4:
fe1b8b76 19872 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19873 break;
19874 case 8:
fe1b8b76 19875 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19876 break;
19877 default:
8e65ff28 19878 internal_error (__FILE__, __LINE__,
c764a876 19879 _("read_offset_1: bad switch [in module %s]"),
659b0389 19880 bfd_get_filename (abfd));
613e1657
KB
19881 }
19882
917c78fc 19883 return retval;
613e1657
KB
19884}
19885
d521ce57
TT
19886static const gdb_byte *
19887read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19888{
19889 /* If the size of a host char is 8 bits, we can return a pointer
19890 to the buffer, otherwise we have to copy the data to a buffer
19891 allocated on the temporary obstack. */
4bdf3d34 19892 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19893 return buf;
c906108c
SS
19894}
19895
d521ce57
TT
19896static const char *
19897read_direct_string (bfd *abfd, const gdb_byte *buf,
19898 unsigned int *bytes_read_ptr)
c906108c
SS
19899{
19900 /* If the size of a host char is 8 bits, we can return a pointer
19901 to the string, otherwise we have to copy the string to a buffer
19902 allocated on the temporary obstack. */
4bdf3d34 19903 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19904 if (*buf == '\0')
19905 {
19906 *bytes_read_ptr = 1;
19907 return NULL;
19908 }
d521ce57
TT
19909 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19910 return (const char *) buf;
4bdf3d34
JJ
19911}
19912
43988095
JK
19913/* Return pointer to string at section SECT offset STR_OFFSET with error
19914 reporting strings FORM_NAME and SECT_NAME. */
19915
d521ce57 19916static const char *
ed2dc618
SM
19917read_indirect_string_at_offset_from (struct objfile *objfile,
19918 bfd *abfd, LONGEST str_offset,
43988095
JK
19919 struct dwarf2_section_info *sect,
19920 const char *form_name,
19921 const char *sect_name)
19922{
ed2dc618 19923 dwarf2_read_section (objfile, sect);
43988095
JK
19924 if (sect->buffer == NULL)
19925 error (_("%s used without %s section [in module %s]"),
19926 form_name, sect_name, bfd_get_filename (abfd));
19927 if (str_offset >= sect->size)
19928 error (_("%s pointing outside of %s section [in module %s]"),
19929 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19930 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19931 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19932 return NULL;
43988095
JK
19933 return (const char *) (sect->buffer + str_offset);
19934}
19935
19936/* Return pointer to string at .debug_str offset STR_OFFSET. */
19937
19938static const char *
ed2dc618
SM
19939read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19940 bfd *abfd, LONGEST str_offset)
43988095 19941{
ed2dc618
SM
19942 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19943 abfd, str_offset,
43988095
JK
19944 &dwarf2_per_objfile->str,
19945 "DW_FORM_strp", ".debug_str");
19946}
19947
19948/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19949
19950static const char *
ed2dc618
SM
19951read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19952 bfd *abfd, LONGEST str_offset)
43988095 19953{
ed2dc618
SM
19954 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19955 abfd, str_offset,
43988095
JK
19956 &dwarf2_per_objfile->line_str,
19957 "DW_FORM_line_strp",
19958 ".debug_line_str");
c906108c
SS
19959}
19960
36586728
TT
19961/* Read a string at offset STR_OFFSET in the .debug_str section from
19962 the .dwz file DWZ. Throw an error if the offset is too large. If
19963 the string consists of a single NUL byte, return NULL; otherwise
19964 return a pointer to the string. */
19965
d521ce57 19966static const char *
ed2dc618
SM
19967read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19968 LONGEST str_offset)
36586728 19969{
ed2dc618 19970 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19971
19972 if (dwz->str.buffer == NULL)
19973 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19974 "section [in module %s]"),
00f93c44 19975 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19976 if (str_offset >= dwz->str.size)
19977 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19978 ".debug_str section [in module %s]"),
00f93c44 19979 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19980 gdb_assert (HOST_CHAR_BIT == 8);
19981 if (dwz->str.buffer[str_offset] == '\0')
19982 return NULL;
d521ce57 19983 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19984}
19985
43988095
JK
19986/* Return pointer to string at .debug_str offset as read from BUF.
19987 BUF is assumed to be in a compilation unit described by CU_HEADER.
19988 Return *BYTES_READ_PTR count of bytes read from BUF. */
19989
d521ce57 19990static const char *
ed2dc618
SM
19991read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19992 const gdb_byte *buf,
cf2c3c16
TT
19993 const struct comp_unit_head *cu_header,
19994 unsigned int *bytes_read_ptr)
19995{
19996 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19997
ed2dc618 19998 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19999}
20000
43988095
JK
20001/* Return pointer to string at .debug_line_str offset as read from BUF.
20002 BUF is assumed to be in a compilation unit described by CU_HEADER.
20003 Return *BYTES_READ_PTR count of bytes read from BUF. */
20004
20005static const char *
ed2dc618
SM
20006read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20007 bfd *abfd, const gdb_byte *buf,
43988095
JK
20008 const struct comp_unit_head *cu_header,
20009 unsigned int *bytes_read_ptr)
20010{
20011 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20012
ed2dc618
SM
20013 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20014 str_offset);
43988095
JK
20015}
20016
20017ULONGEST
d521ce57 20018read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20019 unsigned int *bytes_read_ptr)
c906108c 20020{
12df843f 20021 ULONGEST result;
ce5d95e1 20022 unsigned int num_read;
870f88f7 20023 int shift;
c906108c
SS
20024 unsigned char byte;
20025
20026 result = 0;
20027 shift = 0;
20028 num_read = 0;
c906108c
SS
20029 while (1)
20030 {
fe1b8b76 20031 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20032 buf++;
20033 num_read++;
12df843f 20034 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20035 if ((byte & 128) == 0)
20036 {
20037 break;
20038 }
20039 shift += 7;
20040 }
20041 *bytes_read_ptr = num_read;
20042 return result;
20043}
20044
12df843f 20045static LONGEST
d521ce57
TT
20046read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20047 unsigned int *bytes_read_ptr)
c906108c 20048{
4dd1b460 20049 ULONGEST result;
870f88f7 20050 int shift, num_read;
c906108c
SS
20051 unsigned char byte;
20052
20053 result = 0;
20054 shift = 0;
c906108c 20055 num_read = 0;
c906108c
SS
20056 while (1)
20057 {
fe1b8b76 20058 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20059 buf++;
20060 num_read++;
4dd1b460 20061 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20062 shift += 7;
20063 if ((byte & 128) == 0)
20064 {
20065 break;
20066 }
20067 }
77e0b926 20068 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20069 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20070 *bytes_read_ptr = num_read;
20071 return result;
20072}
20073
3019eac3
DE
20074/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20075 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20076 ADDR_SIZE is the size of addresses from the CU header. */
20077
20078static CORE_ADDR
ed2dc618
SM
20079read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20080 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20081{
20082 struct objfile *objfile = dwarf2_per_objfile->objfile;
20083 bfd *abfd = objfile->obfd;
20084 const gdb_byte *info_ptr;
20085
20086 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20087 if (dwarf2_per_objfile->addr.buffer == NULL)
20088 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20089 objfile_name (objfile));
3019eac3
DE
20090 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20091 error (_("DW_FORM_addr_index pointing outside of "
20092 ".debug_addr section [in module %s]"),
4262abfb 20093 objfile_name (objfile));
3019eac3
DE
20094 info_ptr = (dwarf2_per_objfile->addr.buffer
20095 + addr_base + addr_index * addr_size);
20096 if (addr_size == 4)
20097 return bfd_get_32 (abfd, info_ptr);
20098 else
20099 return bfd_get_64 (abfd, info_ptr);
20100}
20101
20102/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20103
20104static CORE_ADDR
20105read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20106{
518817b3
SM
20107 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20108 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20109}
20110
20111/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20112
20113static CORE_ADDR
d521ce57 20114read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20115 unsigned int *bytes_read)
20116{
518817b3 20117 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20118 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20119
20120 return read_addr_index (cu, addr_index);
20121}
20122
20123/* Data structure to pass results from dwarf2_read_addr_index_reader
20124 back to dwarf2_read_addr_index. */
20125
20126struct dwarf2_read_addr_index_data
20127{
20128 ULONGEST addr_base;
20129 int addr_size;
20130};
20131
20132/* die_reader_func for dwarf2_read_addr_index. */
20133
20134static void
20135dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20136 const gdb_byte *info_ptr,
3019eac3
DE
20137 struct die_info *comp_unit_die,
20138 int has_children,
20139 void *data)
20140{
20141 struct dwarf2_cu *cu = reader->cu;
20142 struct dwarf2_read_addr_index_data *aidata =
20143 (struct dwarf2_read_addr_index_data *) data;
20144
20145 aidata->addr_base = cu->addr_base;
20146 aidata->addr_size = cu->header.addr_size;
20147}
20148
20149/* Given an index in .debug_addr, fetch the value.
20150 NOTE: This can be called during dwarf expression evaluation,
20151 long after the debug information has been read, and thus per_cu->cu
20152 may no longer exist. */
20153
20154CORE_ADDR
20155dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20156 unsigned int addr_index)
20157{
ed2dc618 20158 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20159 struct dwarf2_cu *cu = per_cu->cu;
20160 ULONGEST addr_base;
20161 int addr_size;
20162
3019eac3
DE
20163 /* We need addr_base and addr_size.
20164 If we don't have PER_CU->cu, we have to get it.
20165 Nasty, but the alternative is storing the needed info in PER_CU,
20166 which at this point doesn't seem justified: it's not clear how frequently
20167 it would get used and it would increase the size of every PER_CU.
20168 Entry points like dwarf2_per_cu_addr_size do a similar thing
20169 so we're not in uncharted territory here.
20170 Alas we need to be a bit more complicated as addr_base is contained
20171 in the DIE.
20172
20173 We don't need to read the entire CU(/TU).
20174 We just need the header and top level die.
a1b64ce1 20175
3019eac3 20176 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20177 For now we skip this optimization. */
3019eac3
DE
20178
20179 if (cu != NULL)
20180 {
20181 addr_base = cu->addr_base;
20182 addr_size = cu->header.addr_size;
20183 }
20184 else
20185 {
20186 struct dwarf2_read_addr_index_data aidata;
20187
a1b64ce1
DE
20188 /* Note: We can't use init_cutu_and_read_dies_simple here,
20189 we need addr_base. */
58f0c718 20190 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20191 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20192 addr_base = aidata.addr_base;
20193 addr_size = aidata.addr_size;
20194 }
20195
ed2dc618
SM
20196 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20197 addr_size);
3019eac3
DE
20198}
20199
cf532bd1 20200/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20201 This is only used by the Fission support. */
3019eac3 20202
d521ce57 20203static const char *
342587c4 20204read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20205{
ed2dc618 20206 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20207 struct dwarf2_per_objfile *dwarf2_per_objfile
20208 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20209 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20210 const char *objf_name = objfile_name (objfile);
3019eac3 20211 bfd *abfd = objfile->obfd;
73869dc2
DE
20212 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20213 struct dwarf2_section_info *str_offsets_section =
20214 &reader->dwo_file->sections.str_offsets;
d521ce57 20215 const gdb_byte *info_ptr;
3019eac3 20216 ULONGEST str_offset;
cf532bd1 20217 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20218
73869dc2
DE
20219 dwarf2_read_section (objfile, str_section);
20220 dwarf2_read_section (objfile, str_offsets_section);
20221 if (str_section->buffer == NULL)
57d63ce2 20222 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20223 " in CU at offset %s [in module %s]"),
20224 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20225 if (str_offsets_section->buffer == NULL)
57d63ce2 20226 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20227 " in CU at offset %s [in module %s]"),
20228 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20229 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20230 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20231 " section in CU at offset %s [in module %s]"),
20232 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20233 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20234 + str_index * cu->header.offset_size);
20235 if (cu->header.offset_size == 4)
20236 str_offset = bfd_get_32 (abfd, info_ptr);
20237 else
20238 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20239 if (str_offset >= str_section->size)
57d63ce2 20240 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20241 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20242 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20243 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20244}
20245
3019eac3
DE
20246/* Return the length of an LEB128 number in BUF. */
20247
20248static int
20249leb128_size (const gdb_byte *buf)
20250{
20251 const gdb_byte *begin = buf;
20252 gdb_byte byte;
20253
20254 while (1)
20255 {
20256 byte = *buf++;
20257 if ((byte & 128) == 0)
20258 return buf - begin;
20259 }
20260}
20261
c906108c 20262static void
e142c38c 20263set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20264{
20265 switch (lang)
20266 {
20267 case DW_LANG_C89:
76bee0cc 20268 case DW_LANG_C99:
0cfd832f 20269 case DW_LANG_C11:
c906108c 20270 case DW_LANG_C:
d1be3247 20271 case DW_LANG_UPC:
e142c38c 20272 cu->language = language_c;
c906108c 20273 break;
9c37b5ae 20274 case DW_LANG_Java:
c906108c 20275 case DW_LANG_C_plus_plus:
0cfd832f
MW
20276 case DW_LANG_C_plus_plus_11:
20277 case DW_LANG_C_plus_plus_14:
e142c38c 20278 cu->language = language_cplus;
c906108c 20279 break;
6aecb9c2
JB
20280 case DW_LANG_D:
20281 cu->language = language_d;
20282 break;
c906108c
SS
20283 case DW_LANG_Fortran77:
20284 case DW_LANG_Fortran90:
b21b22e0 20285 case DW_LANG_Fortran95:
f7de9aab
MW
20286 case DW_LANG_Fortran03:
20287 case DW_LANG_Fortran08:
e142c38c 20288 cu->language = language_fortran;
c906108c 20289 break;
a766d390
DE
20290 case DW_LANG_Go:
20291 cu->language = language_go;
20292 break;
c906108c 20293 case DW_LANG_Mips_Assembler:
e142c38c 20294 cu->language = language_asm;
c906108c
SS
20295 break;
20296 case DW_LANG_Ada83:
8aaf0b47 20297 case DW_LANG_Ada95:
bc5f45f8
JB
20298 cu->language = language_ada;
20299 break;
72019c9c
GM
20300 case DW_LANG_Modula2:
20301 cu->language = language_m2;
20302 break;
fe8e67fd
PM
20303 case DW_LANG_Pascal83:
20304 cu->language = language_pascal;
20305 break;
22566fbd
DJ
20306 case DW_LANG_ObjC:
20307 cu->language = language_objc;
20308 break;
c44af4eb
TT
20309 case DW_LANG_Rust:
20310 case DW_LANG_Rust_old:
20311 cu->language = language_rust;
20312 break;
c906108c
SS
20313 case DW_LANG_Cobol74:
20314 case DW_LANG_Cobol85:
c906108c 20315 default:
e142c38c 20316 cu->language = language_minimal;
c906108c
SS
20317 break;
20318 }
e142c38c 20319 cu->language_defn = language_def (cu->language);
c906108c
SS
20320}
20321
20322/* Return the named attribute or NULL if not there. */
20323
20324static struct attribute *
e142c38c 20325dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20326{
a48e046c 20327 for (;;)
c906108c 20328 {
a48e046c
TT
20329 unsigned int i;
20330 struct attribute *spec = NULL;
20331
20332 for (i = 0; i < die->num_attrs; ++i)
20333 {
20334 if (die->attrs[i].name == name)
20335 return &die->attrs[i];
20336 if (die->attrs[i].name == DW_AT_specification
20337 || die->attrs[i].name == DW_AT_abstract_origin)
20338 spec = &die->attrs[i];
20339 }
20340
20341 if (!spec)
20342 break;
c906108c 20343
f2f0e013 20344 die = follow_die_ref (die, spec, &cu);
f2f0e013 20345 }
c5aa993b 20346
c906108c
SS
20347 return NULL;
20348}
20349
348e048f
DE
20350/* Return the named attribute or NULL if not there,
20351 but do not follow DW_AT_specification, etc.
20352 This is for use in contexts where we're reading .debug_types dies.
20353 Following DW_AT_specification, DW_AT_abstract_origin will take us
20354 back up the chain, and we want to go down. */
20355
20356static struct attribute *
45e58e77 20357dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20358{
20359 unsigned int i;
20360
20361 for (i = 0; i < die->num_attrs; ++i)
20362 if (die->attrs[i].name == name)
20363 return &die->attrs[i];
20364
20365 return NULL;
20366}
20367
7d45c7c3
KB
20368/* Return the string associated with a string-typed attribute, or NULL if it
20369 is either not found or is of an incorrect type. */
20370
20371static const char *
20372dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20373{
20374 struct attribute *attr;
20375 const char *str = NULL;
20376
20377 attr = dwarf2_attr (die, name, cu);
20378
20379 if (attr != NULL)
20380 {
43988095 20381 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20382 || attr->form == DW_FORM_string
cf532bd1 20383 || attr->form == DW_FORM_strx
8fe0f950
AT
20384 || attr->form == DW_FORM_strx1
20385 || attr->form == DW_FORM_strx2
20386 || attr->form == DW_FORM_strx3
20387 || attr->form == DW_FORM_strx4
b3340438 20388 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20389 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20390 str = DW_STRING (attr);
20391 else
b98664d3 20392 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20393 "DIE at %s in module %s"),
20394 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20395 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20396 }
20397
20398 return str;
20399}
20400
a084a2a6 20401/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20402 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20403static const char *
20404dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20405{
20406 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20407 if (dwo_name == nullptr)
20408 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20409 return dwo_name;
20410}
20411
05cf31d1
JB
20412/* Return non-zero iff the attribute NAME is defined for the given DIE,
20413 and holds a non-zero value. This function should only be used for
2dc7f7b3 20414 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20415
20416static int
20417dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20418{
20419 struct attribute *attr = dwarf2_attr (die, name, cu);
20420
20421 return (attr && DW_UNSND (attr));
20422}
20423
3ca72b44 20424static int
e142c38c 20425die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20426{
05cf31d1
JB
20427 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20428 which value is non-zero. However, we have to be careful with
20429 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20430 (via dwarf2_flag_true_p) follows this attribute. So we may
20431 end up accidently finding a declaration attribute that belongs
20432 to a different DIE referenced by the specification attribute,
20433 even though the given DIE does not have a declaration attribute. */
20434 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20435 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20436}
20437
63d06c5c 20438/* Return the die giving the specification for DIE, if there is
f2f0e013 20439 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20440 containing the return value on output. If there is no
20441 specification, but there is an abstract origin, that is
20442 returned. */
63d06c5c
DC
20443
20444static struct die_info *
f2f0e013 20445die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20446{
f2f0e013
DJ
20447 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20448 *spec_cu);
63d06c5c 20449
edb3359d
DJ
20450 if (spec_attr == NULL)
20451 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20452
63d06c5c
DC
20453 if (spec_attr == NULL)
20454 return NULL;
20455 else
f2f0e013 20456 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20457}
c906108c 20458
527f3840
JK
20459/* Stub for free_line_header to match void * callback types. */
20460
20461static void
20462free_line_header_voidp (void *arg)
20463{
9a3c8263 20464 struct line_header *lh = (struct line_header *) arg;
527f3840 20465
fff8551c 20466 delete lh;
527f3840
JK
20467}
20468
fff8551c
PA
20469void
20470line_header::add_include_dir (const char *include_dir)
c906108c 20471{
27e0867f 20472 if (dwarf_line_debug >= 2)
7ba99d21
AT
20473 {
20474 size_t new_size;
20475 if (version >= 5)
20476 new_size = m_include_dirs.size ();
20477 else
20478 new_size = m_include_dirs.size () + 1;
20479 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20480 new_size, include_dir);
20481 }
20482 m_include_dirs.push_back (include_dir);
debd256d 20483}
6e70227d 20484
fff8551c
PA
20485void
20486line_header::add_file_name (const char *name,
ecfb656c 20487 dir_index d_index,
fff8551c
PA
20488 unsigned int mod_time,
20489 unsigned int length)
debd256d 20490{
27e0867f 20491 if (dwarf_line_debug >= 2)
7ba99d21
AT
20492 {
20493 size_t new_size;
20494 if (version >= 5)
20495 new_size = file_names_size ();
20496 else
20497 new_size = file_names_size () + 1;
20498 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20499 new_size, name);
20500 }
20501 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20502}
6e70227d 20503
83769d0b 20504/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20505
20506static struct dwarf2_section_info *
20507get_debug_line_section (struct dwarf2_cu *cu)
20508{
20509 struct dwarf2_section_info *section;
518817b3
SM
20510 struct dwarf2_per_objfile *dwarf2_per_objfile
20511 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20512
20513 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20514 DWO file. */
20515 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20516 section = &cu->dwo_unit->dwo_file->sections.line;
20517 else if (cu->per_cu->is_dwz)
20518 {
ed2dc618 20519 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20520
20521 section = &dwz->line;
20522 }
20523 else
20524 section = &dwarf2_per_objfile->line;
20525
20526 return section;
20527}
20528
43988095
JK
20529/* Read directory or file name entry format, starting with byte of
20530 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20531 entries count and the entries themselves in the described entry
20532 format. */
20533
20534static void
ed2dc618
SM
20535read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20536 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20537 struct line_header *lh,
20538 const struct comp_unit_head *cu_header,
20539 void (*callback) (struct line_header *lh,
20540 const char *name,
ecfb656c 20541 dir_index d_index,
43988095
JK
20542 unsigned int mod_time,
20543 unsigned int length))
20544{
20545 gdb_byte format_count, formati;
20546 ULONGEST data_count, datai;
20547 const gdb_byte *buf = *bufp;
20548 const gdb_byte *format_header_data;
43988095
JK
20549 unsigned int bytes_read;
20550
20551 format_count = read_1_byte (abfd, buf);
20552 buf += 1;
20553 format_header_data = buf;
20554 for (formati = 0; formati < format_count; formati++)
20555 {
20556 read_unsigned_leb128 (abfd, buf, &bytes_read);
20557 buf += bytes_read;
20558 read_unsigned_leb128 (abfd, buf, &bytes_read);
20559 buf += bytes_read;
20560 }
20561
20562 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20563 buf += bytes_read;
20564 for (datai = 0; datai < data_count; datai++)
20565 {
20566 const gdb_byte *format = format_header_data;
20567 struct file_entry fe;
20568
43988095
JK
20569 for (formati = 0; formati < format_count; formati++)
20570 {
ecfb656c 20571 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20572 format += bytes_read;
43988095 20573
ecfb656c 20574 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20575 format += bytes_read;
ecfb656c
PA
20576
20577 gdb::optional<const char *> string;
20578 gdb::optional<unsigned int> uint;
20579
43988095
JK
20580 switch (form)
20581 {
20582 case DW_FORM_string:
ecfb656c 20583 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20584 buf += bytes_read;
20585 break;
20586
20587 case DW_FORM_line_strp:
ed2dc618
SM
20588 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20589 abfd, buf,
ecfb656c
PA
20590 cu_header,
20591 &bytes_read));
43988095
JK
20592 buf += bytes_read;
20593 break;
20594
20595 case DW_FORM_data1:
ecfb656c 20596 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20597 buf += 1;
20598 break;
20599
20600 case DW_FORM_data2:
ecfb656c 20601 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20602 buf += 2;
20603 break;
20604
20605 case DW_FORM_data4:
ecfb656c 20606 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20607 buf += 4;
20608 break;
20609
20610 case DW_FORM_data8:
ecfb656c 20611 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20612 buf += 8;
20613 break;
20614
7ba99d21
AT
20615 case DW_FORM_data16:
20616 /* This is used for MD5, but file_entry does not record MD5s. */
20617 buf += 16;
20618 break;
20619
43988095 20620 case DW_FORM_udata:
ecfb656c 20621 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20622 buf += bytes_read;
20623 break;
20624
20625 case DW_FORM_block:
20626 /* It is valid only for DW_LNCT_timestamp which is ignored by
20627 current GDB. */
20628 break;
20629 }
ecfb656c
PA
20630
20631 switch (content_type)
20632 {
20633 case DW_LNCT_path:
20634 if (string.has_value ())
20635 fe.name = *string;
20636 break;
20637 case DW_LNCT_directory_index:
20638 if (uint.has_value ())
20639 fe.d_index = (dir_index) *uint;
20640 break;
20641 case DW_LNCT_timestamp:
20642 if (uint.has_value ())
20643 fe.mod_time = *uint;
20644 break;
20645 case DW_LNCT_size:
20646 if (uint.has_value ())
20647 fe.length = *uint;
20648 break;
20649 case DW_LNCT_MD5:
20650 break;
20651 default:
b98664d3 20652 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20653 pulongest (content_type));
20654 }
43988095
JK
20655 }
20656
ecfb656c 20657 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20658 }
20659
20660 *bufp = buf;
20661}
20662
debd256d 20663/* Read the statement program header starting at OFFSET in
3019eac3 20664 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20665 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20666 Returns NULL if there is a problem reading the header, e.g., if it
20667 has a version we don't understand.
debd256d
JB
20668
20669 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20670 the returned object point into the dwarf line section buffer,
20671 and must not be freed. */
ae2de4f8 20672
fff8551c 20673static line_header_up
9c541725 20674dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20675{
d521ce57 20676 const gdb_byte *line_ptr;
c764a876 20677 unsigned int bytes_read, offset_size;
debd256d 20678 int i;
d521ce57 20679 const char *cur_dir, *cur_file;
3019eac3
DE
20680 struct dwarf2_section_info *section;
20681 bfd *abfd;
518817b3
SM
20682 struct dwarf2_per_objfile *dwarf2_per_objfile
20683 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20684
36586728 20685 section = get_debug_line_section (cu);
3019eac3
DE
20686 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20687 if (section->buffer == NULL)
debd256d 20688 {
3019eac3 20689 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20690 complaint (_("missing .debug_line.dwo section"));
3019eac3 20691 else
b98664d3 20692 complaint (_("missing .debug_line section"));
debd256d
JB
20693 return 0;
20694 }
20695
fceca515
DE
20696 /* We can't do this until we know the section is non-empty.
20697 Only then do we know we have such a section. */
a32a8923 20698 abfd = get_section_bfd_owner (section);
fceca515 20699
a738430d
MK
20700 /* Make sure that at least there's room for the total_length field.
20701 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20702 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20703 {
4d3c2250 20704 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20705 return 0;
20706 }
20707
fff8551c 20708 line_header_up lh (new line_header ());
debd256d 20709
9c541725 20710 lh->sect_off = sect_off;
527f3840
JK
20711 lh->offset_in_dwz = cu->per_cu->is_dwz;
20712
9c541725 20713 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20714
a738430d 20715 /* Read in the header. */
6e70227d 20716 lh->total_length =
c764a876
DE
20717 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20718 &bytes_read, &offset_size);
debd256d 20719 line_ptr += bytes_read;
7ba99d21
AT
20720
20721 const gdb_byte *start_here = line_ptr;
20722
3019eac3 20723 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20724 {
4d3c2250 20725 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20726 return 0;
20727 }
7ba99d21 20728 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20729 lh->version = read_2_bytes (abfd, line_ptr);
20730 line_ptr += 2;
43988095 20731 if (lh->version > 5)
cd366ee8
DE
20732 {
20733 /* This is a version we don't understand. The format could have
20734 changed in ways we don't handle properly so just punt. */
b98664d3 20735 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20736 return NULL;
20737 }
43988095
JK
20738 if (lh->version >= 5)
20739 {
20740 gdb_byte segment_selector_size;
20741
20742 /* Skip address size. */
20743 read_1_byte (abfd, line_ptr);
20744 line_ptr += 1;
20745
20746 segment_selector_size = read_1_byte (abfd, line_ptr);
20747 line_ptr += 1;
20748 if (segment_selector_size != 0)
20749 {
b98664d3 20750 complaint (_("unsupported segment selector size %u "
43988095
JK
20751 "in .debug_line section"),
20752 segment_selector_size);
20753 return NULL;
20754 }
20755 }
c764a876
DE
20756 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20757 line_ptr += offset_size;
7ba99d21 20758 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20759 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20760 line_ptr += 1;
2dc7f7b3
TT
20761 if (lh->version >= 4)
20762 {
20763 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20764 line_ptr += 1;
20765 }
20766 else
20767 lh->maximum_ops_per_instruction = 1;
20768
20769 if (lh->maximum_ops_per_instruction == 0)
20770 {
20771 lh->maximum_ops_per_instruction = 1;
b98664d3 20772 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20773 "in `.debug_line' section"));
2dc7f7b3
TT
20774 }
20775
debd256d
JB
20776 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20777 line_ptr += 1;
20778 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20779 line_ptr += 1;
20780 lh->line_range = read_1_byte (abfd, line_ptr);
20781 line_ptr += 1;
20782 lh->opcode_base = read_1_byte (abfd, line_ptr);
20783 line_ptr += 1;
fff8551c 20784 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20785
20786 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20787 for (i = 1; i < lh->opcode_base; ++i)
20788 {
20789 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20790 line_ptr += 1;
20791 }
20792
43988095 20793 if (lh->version >= 5)
debd256d 20794 {
43988095 20795 /* Read directory table. */
ed2dc618
SM
20796 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20797 &cu->header,
b926417a 20798 [] (struct line_header *header, const char *name,
ecfb656c 20799 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20800 unsigned int length)
20801 {
b926417a 20802 header->add_include_dir (name);
fff8551c 20803 });
debd256d 20804
43988095 20805 /* Read file name table. */
ed2dc618
SM
20806 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20807 &cu->header,
b926417a 20808 [] (struct line_header *header, const char *name,
ecfb656c 20809 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20810 unsigned int length)
20811 {
b926417a 20812 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20813 });
43988095
JK
20814 }
20815 else
debd256d 20816 {
43988095
JK
20817 /* Read directory table. */
20818 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20819 {
20820 line_ptr += bytes_read;
fff8551c 20821 lh->add_include_dir (cur_dir);
43988095 20822 }
debd256d
JB
20823 line_ptr += bytes_read;
20824
43988095
JK
20825 /* Read file name table. */
20826 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20827 {
ecfb656c
PA
20828 unsigned int mod_time, length;
20829 dir_index d_index;
43988095
JK
20830
20831 line_ptr += bytes_read;
ecfb656c 20832 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20833 line_ptr += bytes_read;
20834 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20835 line_ptr += bytes_read;
20836 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20837 line_ptr += bytes_read;
20838
ecfb656c 20839 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20840 }
20841 line_ptr += bytes_read;
debd256d 20842 }
debd256d 20843
3019eac3 20844 if (line_ptr > (section->buffer + section->size))
b98664d3 20845 complaint (_("line number info header doesn't "
3e43a32a 20846 "fit in `.debug_line' section"));
debd256d 20847
debd256d
JB
20848 return lh;
20849}
c906108c 20850
c6da4cef 20851/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20852 Return the file name of the psymtab for the given file_entry.
c6da4cef 20853 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20854 If space for the result is malloc'd, *NAME_HOLDER will be set.
20855 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20856
d521ce57 20857static const char *
7ba99d21 20858psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20859 const struct partial_symtab *pst,
c89b44cd
TT
20860 const char *comp_dir,
20861 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20862{
d521ce57
TT
20863 const char *include_name = fe.name;
20864 const char *include_name_to_compare = include_name;
72b9f47f 20865 const char *pst_filename;
c6da4cef
DE
20866 int file_is_pst;
20867
8c43009f 20868 const char *dir_name = fe.include_dir (lh);
c6da4cef 20869
c89b44cd 20870 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20871 if (!IS_ABSOLUTE_PATH (include_name)
20872 && (dir_name != NULL || comp_dir != NULL))
20873 {
20874 /* Avoid creating a duplicate psymtab for PST.
20875 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20876 Before we do the comparison, however, we need to account
20877 for DIR_NAME and COMP_DIR.
20878 First prepend dir_name (if non-NULL). If we still don't
20879 have an absolute path prepend comp_dir (if non-NULL).
20880 However, the directory we record in the include-file's
20881 psymtab does not contain COMP_DIR (to match the
20882 corresponding symtab(s)).
20883
20884 Example:
20885
20886 bash$ cd /tmp
20887 bash$ gcc -g ./hello.c
20888 include_name = "hello.c"
20889 dir_name = "."
20890 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20891 DW_AT_name = "./hello.c"
20892
20893 */
c6da4cef
DE
20894
20895 if (dir_name != NULL)
20896 {
c89b44cd
TT
20897 name_holder->reset (concat (dir_name, SLASH_STRING,
20898 include_name, (char *) NULL));
20899 include_name = name_holder->get ();
c6da4cef 20900 include_name_to_compare = include_name;
c6da4cef
DE
20901 }
20902 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20903 {
c89b44cd
TT
20904 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20905 include_name, (char *) NULL));
20906 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20907 }
20908 }
20909
20910 pst_filename = pst->filename;
c89b44cd 20911 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20912 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20913 {
c89b44cd
TT
20914 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20915 pst_filename, (char *) NULL));
20916 pst_filename = copied_name.get ();
c6da4cef
DE
20917 }
20918
1e3fad37 20919 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20920
c6da4cef
DE
20921 if (file_is_pst)
20922 return NULL;
20923 return include_name;
20924}
20925
d9b3de22
DE
20926/* State machine to track the state of the line number program. */
20927
6f77053d 20928class lnp_state_machine
d9b3de22 20929{
6f77053d
PA
20930public:
20931 /* Initialize a machine state for the start of a line number
20932 program. */
804d2729
TT
20933 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20934 bool record_lines_p);
6f77053d 20935
8c43009f
PA
20936 file_entry *current_file ()
20937 {
20938 /* lh->file_names is 0-based, but the file name numbers in the
20939 statement program are 1-based. */
6f77053d
PA
20940 return m_line_header->file_name_at (m_file);
20941 }
20942
20943 /* Record the line in the state machine. END_SEQUENCE is true if
20944 we're processing the end of a sequence. */
20945 void record_line (bool end_sequence);
20946
7ab6656f
OJ
20947 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20948 nop-out rest of the lines in this sequence. */
6f77053d
PA
20949 void check_line_address (struct dwarf2_cu *cu,
20950 const gdb_byte *line_ptr,
7ab6656f 20951 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20952
20953 void handle_set_discriminator (unsigned int discriminator)
20954 {
20955 m_discriminator = discriminator;
20956 m_line_has_non_zero_discriminator |= discriminator != 0;
20957 }
20958
20959 /* Handle DW_LNE_set_address. */
20960 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20961 {
20962 m_op_index = 0;
20963 address += baseaddr;
20964 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20965 }
20966
20967 /* Handle DW_LNS_advance_pc. */
20968 void handle_advance_pc (CORE_ADDR adjust);
20969
20970 /* Handle a special opcode. */
20971 void handle_special_opcode (unsigned char op_code);
20972
20973 /* Handle DW_LNS_advance_line. */
20974 void handle_advance_line (int line_delta)
20975 {
20976 advance_line (line_delta);
20977 }
20978
20979 /* Handle DW_LNS_set_file. */
20980 void handle_set_file (file_name_index file);
20981
20982 /* Handle DW_LNS_negate_stmt. */
20983 void handle_negate_stmt ()
20984 {
20985 m_is_stmt = !m_is_stmt;
20986 }
20987
20988 /* Handle DW_LNS_const_add_pc. */
20989 void handle_const_add_pc ();
20990
20991 /* Handle DW_LNS_fixed_advance_pc. */
20992 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20993 {
20994 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20995 m_op_index = 0;
20996 }
20997
20998 /* Handle DW_LNS_copy. */
20999 void handle_copy ()
21000 {
21001 record_line (false);
21002 m_discriminator = 0;
21003 }
21004
21005 /* Handle DW_LNE_end_sequence. */
21006 void handle_end_sequence ()
21007 {
804d2729 21008 m_currently_recording_lines = true;
6f77053d
PA
21009 }
21010
21011private:
21012 /* Advance the line by LINE_DELTA. */
21013 void advance_line (int line_delta)
21014 {
21015 m_line += line_delta;
21016
21017 if (line_delta != 0)
21018 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21019 }
21020
804d2729
TT
21021 struct dwarf2_cu *m_cu;
21022
6f77053d
PA
21023 gdbarch *m_gdbarch;
21024
21025 /* True if we're recording lines.
21026 Otherwise we're building partial symtabs and are just interested in
21027 finding include files mentioned by the line number program. */
21028 bool m_record_lines_p;
21029
8c43009f 21030 /* The line number header. */
6f77053d 21031 line_header *m_line_header;
8c43009f 21032
6f77053d
PA
21033 /* These are part of the standard DWARF line number state machine,
21034 and initialized according to the DWARF spec. */
d9b3de22 21035
6f77053d 21036 unsigned char m_op_index = 0;
7ba99d21
AT
21037 /* The line table index of the current file. */
21038 file_name_index m_file = 1;
6f77053d
PA
21039 unsigned int m_line = 1;
21040
21041 /* These are initialized in the constructor. */
21042
21043 CORE_ADDR m_address;
21044 bool m_is_stmt;
21045 unsigned int m_discriminator;
d9b3de22
DE
21046
21047 /* Additional bits of state we need to track. */
21048
21049 /* The last file that we called dwarf2_start_subfile for.
21050 This is only used for TLLs. */
6f77053d 21051 unsigned int m_last_file = 0;
d9b3de22 21052 /* The last file a line number was recorded for. */
6f77053d 21053 struct subfile *m_last_subfile = NULL;
d9b3de22 21054
804d2729
TT
21055 /* When true, record the lines we decode. */
21056 bool m_currently_recording_lines = false;
d9b3de22
DE
21057
21058 /* The last line number that was recorded, used to coalesce
21059 consecutive entries for the same line. This can happen, for
21060 example, when discriminators are present. PR 17276. */
6f77053d
PA
21061 unsigned int m_last_line = 0;
21062 bool m_line_has_non_zero_discriminator = false;
8c43009f 21063};
d9b3de22 21064
6f77053d
PA
21065void
21066lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21067{
21068 CORE_ADDR addr_adj = (((m_op_index + adjust)
21069 / m_line_header->maximum_ops_per_instruction)
21070 * m_line_header->minimum_instruction_length);
21071 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21072 m_op_index = ((m_op_index + adjust)
21073 % m_line_header->maximum_ops_per_instruction);
21074}
d9b3de22 21075
6f77053d
PA
21076void
21077lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21078{
6f77053d
PA
21079 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21080 CORE_ADDR addr_adj = (((m_op_index
21081 + (adj_opcode / m_line_header->line_range))
21082 / m_line_header->maximum_ops_per_instruction)
21083 * m_line_header->minimum_instruction_length);
21084 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21085 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21086 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21087
6f77053d
PA
21088 int line_delta = (m_line_header->line_base
21089 + (adj_opcode % m_line_header->line_range));
21090 advance_line (line_delta);
21091 record_line (false);
21092 m_discriminator = 0;
21093}
d9b3de22 21094
6f77053d
PA
21095void
21096lnp_state_machine::handle_set_file (file_name_index file)
21097{
21098 m_file = file;
21099
21100 const file_entry *fe = current_file ();
21101 if (fe == NULL)
21102 dwarf2_debug_line_missing_file_complaint ();
21103 else if (m_record_lines_p)
21104 {
21105 const char *dir = fe->include_dir (m_line_header);
21106
c24bdb02 21107 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21108 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21109 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21110 }
21111}
21112
21113void
21114lnp_state_machine::handle_const_add_pc ()
21115{
21116 CORE_ADDR adjust
21117 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21118
21119 CORE_ADDR addr_adj
21120 = (((m_op_index + adjust)
21121 / m_line_header->maximum_ops_per_instruction)
21122 * m_line_header->minimum_instruction_length);
21123
21124 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21125 m_op_index = ((m_op_index + adjust)
21126 % m_line_header->maximum_ops_per_instruction);
21127}
d9b3de22 21128
a05a36a5
DE
21129/* Return non-zero if we should add LINE to the line number table.
21130 LINE is the line to add, LAST_LINE is the last line that was added,
21131 LAST_SUBFILE is the subfile for LAST_LINE.
21132 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21133 had a non-zero discriminator.
21134
21135 We have to be careful in the presence of discriminators.
21136 E.g., for this line:
21137
21138 for (i = 0; i < 100000; i++);
21139
21140 clang can emit four line number entries for that one line,
21141 each with a different discriminator.
21142 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21143
21144 However, we want gdb to coalesce all four entries into one.
21145 Otherwise the user could stepi into the middle of the line and
21146 gdb would get confused about whether the pc really was in the
21147 middle of the line.
21148
21149 Things are further complicated by the fact that two consecutive
21150 line number entries for the same line is a heuristic used by gcc
21151 to denote the end of the prologue. So we can't just discard duplicate
21152 entries, we have to be selective about it. The heuristic we use is
21153 that we only collapse consecutive entries for the same line if at least
21154 one of those entries has a non-zero discriminator. PR 17276.
21155
21156 Note: Addresses in the line number state machine can never go backwards
21157 within one sequence, thus this coalescing is ok. */
21158
21159static int
804d2729
TT
21160dwarf_record_line_p (struct dwarf2_cu *cu,
21161 unsigned int line, unsigned int last_line,
a05a36a5
DE
21162 int line_has_non_zero_discriminator,
21163 struct subfile *last_subfile)
21164{
c24bdb02 21165 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21166 return 1;
21167 if (line != last_line)
21168 return 1;
21169 /* Same line for the same file that we've seen already.
21170 As a last check, for pr 17276, only record the line if the line
21171 has never had a non-zero discriminator. */
21172 if (!line_has_non_zero_discriminator)
21173 return 1;
21174 return 0;
21175}
21176
804d2729
TT
21177/* Use the CU's builder to record line number LINE beginning at
21178 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21179
21180static void
d9b3de22
DE
21181dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21182 unsigned int line, CORE_ADDR address,
804d2729 21183 struct dwarf2_cu *cu)
252a6764
DE
21184{
21185 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21186
27e0867f
DE
21187 if (dwarf_line_debug)
21188 {
21189 fprintf_unfiltered (gdb_stdlog,
21190 "Recording line %u, file %s, address %s\n",
21191 line, lbasename (subfile->name),
21192 paddress (gdbarch, address));
21193 }
21194
804d2729 21195 if (cu != nullptr)
c24bdb02 21196 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21197}
21198
21199/* Subroutine of dwarf_decode_lines_1 to simplify it.
21200 Mark the end of a set of line number records.
d9b3de22 21201 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21202 If SUBFILE is NULL the request is ignored. */
21203
21204static void
21205dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21206 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21207{
27e0867f
DE
21208 if (subfile == NULL)
21209 return;
21210
21211 if (dwarf_line_debug)
21212 {
21213 fprintf_unfiltered (gdb_stdlog,
21214 "Finishing current line, file %s, address %s\n",
21215 lbasename (subfile->name),
21216 paddress (gdbarch, address));
21217 }
21218
804d2729 21219 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21220}
21221
6f77053d
PA
21222void
21223lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21224{
d9b3de22
DE
21225 if (dwarf_line_debug)
21226 {
21227 fprintf_unfiltered (gdb_stdlog,
21228 "Processing actual line %u: file %u,"
21229 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21230 m_line, m_file,
6f77053d
PA
21231 paddress (m_gdbarch, m_address),
21232 m_is_stmt, m_discriminator);
d9b3de22
DE
21233 }
21234
6f77053d 21235 file_entry *fe = current_file ();
8c43009f
PA
21236
21237 if (fe == NULL)
d9b3de22
DE
21238 dwarf2_debug_line_missing_file_complaint ();
21239 /* For now we ignore lines not starting on an instruction boundary.
21240 But not when processing end_sequence for compatibility with the
21241 previous version of the code. */
6f77053d 21242 else if (m_op_index == 0 || end_sequence)
d9b3de22 21243 {
8c43009f 21244 fe->included_p = 1;
c258c396 21245 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21246 {
c24bdb02 21247 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21248 || end_sequence)
d9b3de22 21249 {
804d2729
TT
21250 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21251 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21252 }
21253
21254 if (!end_sequence)
21255 {
804d2729 21256 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21257 m_line_has_non_zero_discriminator,
21258 m_last_subfile))
d9b3de22 21259 {
c24bdb02 21260 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21261 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21262 builder->get_current_subfile (),
6f77053d 21263 m_line, m_address,
804d2729 21264 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21265 }
c24bdb02 21266 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21267 m_last_line = m_line;
d9b3de22
DE
21268 }
21269 }
21270 }
21271}
21272
804d2729
TT
21273lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21274 line_header *lh, bool record_lines_p)
d9b3de22 21275{
804d2729 21276 m_cu = cu;
6f77053d
PA
21277 m_gdbarch = arch;
21278 m_record_lines_p = record_lines_p;
21279 m_line_header = lh;
d9b3de22 21280
804d2729 21281 m_currently_recording_lines = true;
d9b3de22 21282
d9b3de22
DE
21283 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21284 was a line entry for it so that the backend has a chance to adjust it
21285 and also record it in case it needs it. This is currently used by MIPS
21286 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21287 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21288 m_is_stmt = lh->default_is_stmt;
21289 m_discriminator = 0;
252a6764
DE
21290}
21291
6f77053d
PA
21292void
21293lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21294 const gdb_byte *line_ptr,
7ab6656f 21295 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21296{
7ab6656f
OJ
21297 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21298 the pc range of the CU. However, we restrict the test to only ADDRESS
21299 values of zero to preserve GDB's previous behaviour which is to handle
21300 the specific case of a function being GC'd by the linker. */
924c2928 21301
7ab6656f 21302 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21303 {
21304 /* This line table is for a function which has been
21305 GCd by the linker. Ignore it. PR gdb/12528 */
21306
518817b3 21307 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21308 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21309
b98664d3 21310 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21311 line_offset, objfile_name (objfile));
804d2729
TT
21312 m_currently_recording_lines = false;
21313 /* Note: m_currently_recording_lines is left as false until we see
21314 DW_LNE_end_sequence. */
924c2928
DE
21315 }
21316}
21317
f3f5162e 21318/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21319 Process the line number information in LH.
21320 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21321 program in order to set included_p for every referenced header. */
debd256d 21322
c906108c 21323static void
43f3e411
DE
21324dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21325 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21326{
d521ce57
TT
21327 const gdb_byte *line_ptr, *extended_end;
21328 const gdb_byte *line_end;
a8c50c1f 21329 unsigned int bytes_read, extended_len;
699ca60a 21330 unsigned char op_code, extended_op;
e142c38c 21331 CORE_ADDR baseaddr;
518817b3 21332 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21333 bfd *abfd = objfile->obfd;
fbf65064 21334 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21335 /* True if we're recording line info (as opposed to building partial
21336 symtabs and just interested in finding include files mentioned by
21337 the line number program). */
21338 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21339
21340 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21341
debd256d
JB
21342 line_ptr = lh->statement_program_start;
21343 line_end = lh->statement_program_end;
c906108c
SS
21344
21345 /* Read the statement sequences until there's nothing left. */
21346 while (line_ptr < line_end)
21347 {
6f77053d
PA
21348 /* The DWARF line number program state machine. Reset the state
21349 machine at the start of each sequence. */
804d2729 21350 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21351 bool end_sequence = false;
d9b3de22 21352
8c43009f 21353 if (record_lines_p)
c906108c 21354 {
8c43009f
PA
21355 /* Start a subfile for the current file of the state
21356 machine. */
21357 const file_entry *fe = state_machine.current_file ();
21358
21359 if (fe != NULL)
804d2729 21360 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21361 }
21362
a738430d 21363 /* Decode the table. */
d9b3de22 21364 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21365 {
21366 op_code = read_1_byte (abfd, line_ptr);
21367 line_ptr += 1;
9aa1fe7e 21368
debd256d 21369 if (op_code >= lh->opcode_base)
6e70227d 21370 {
8e07a239 21371 /* Special opcode. */
6f77053d 21372 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21373 }
21374 else switch (op_code)
c906108c
SS
21375 {
21376 case DW_LNS_extended_op:
3e43a32a
MS
21377 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21378 &bytes_read);
473b7be6 21379 line_ptr += bytes_read;
a8c50c1f 21380 extended_end = line_ptr + extended_len;
c906108c
SS
21381 extended_op = read_1_byte (abfd, line_ptr);
21382 line_ptr += 1;
21383 switch (extended_op)
21384 {
21385 case DW_LNE_end_sequence:
6f77053d
PA
21386 state_machine.handle_end_sequence ();
21387 end_sequence = true;
c906108c
SS
21388 break;
21389 case DW_LNE_set_address:
d9b3de22
DE
21390 {
21391 CORE_ADDR address
21392 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21393 line_ptr += bytes_read;
6f77053d
PA
21394
21395 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21396 lowpc - baseaddr, address);
6f77053d 21397 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21398 }
c906108c
SS
21399 break;
21400 case DW_LNE_define_file:
debd256d 21401 {
d521ce57 21402 const char *cur_file;
ecfb656c
PA
21403 unsigned int mod_time, length;
21404 dir_index dindex;
6e70227d 21405
3e43a32a
MS
21406 cur_file = read_direct_string (abfd, line_ptr,
21407 &bytes_read);
debd256d 21408 line_ptr += bytes_read;
ecfb656c 21409 dindex = (dir_index)
debd256d
JB
21410 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21411 line_ptr += bytes_read;
21412 mod_time =
21413 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21414 line_ptr += bytes_read;
21415 length =
21416 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21417 line_ptr += bytes_read;
ecfb656c 21418 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21419 }
c906108c 21420 break;
d0c6ba3d 21421 case DW_LNE_set_discriminator:
6f77053d
PA
21422 {
21423 /* The discriminator is not interesting to the
21424 debugger; just ignore it. We still need to
21425 check its value though:
21426 if there are consecutive entries for the same
21427 (non-prologue) line we want to coalesce them.
21428 PR 17276. */
21429 unsigned int discr
21430 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21431 line_ptr += bytes_read;
21432
21433 state_machine.handle_set_discriminator (discr);
21434 }
d0c6ba3d 21435 break;
c906108c 21436 default:
b98664d3 21437 complaint (_("mangled .debug_line section"));
debd256d 21438 return;
c906108c 21439 }
a8c50c1f
DJ
21440 /* Make sure that we parsed the extended op correctly. If e.g.
21441 we expected a different address size than the producer used,
21442 we may have read the wrong number of bytes. */
21443 if (line_ptr != extended_end)
21444 {
b98664d3 21445 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21446 return;
21447 }
c906108c
SS
21448 break;
21449 case DW_LNS_copy:
6f77053d 21450 state_machine.handle_copy ();
c906108c
SS
21451 break;
21452 case DW_LNS_advance_pc:
2dc7f7b3
TT
21453 {
21454 CORE_ADDR adjust
21455 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21456 line_ptr += bytes_read;
6f77053d
PA
21457
21458 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21459 }
c906108c
SS
21460 break;
21461 case DW_LNS_advance_line:
a05a36a5
DE
21462 {
21463 int line_delta
21464 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21465 line_ptr += bytes_read;
6f77053d
PA
21466
21467 state_machine.handle_advance_line (line_delta);
a05a36a5 21468 }
c906108c
SS
21469 break;
21470 case DW_LNS_set_file:
d9b3de22 21471 {
6f77053d 21472 file_name_index file
ecfb656c
PA
21473 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21474 &bytes_read);
d9b3de22 21475 line_ptr += bytes_read;
8c43009f 21476
6f77053d 21477 state_machine.handle_set_file (file);
d9b3de22 21478 }
c906108c
SS
21479 break;
21480 case DW_LNS_set_column:
0ad93d4f 21481 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21482 line_ptr += bytes_read;
21483 break;
21484 case DW_LNS_negate_stmt:
6f77053d 21485 state_machine.handle_negate_stmt ();
c906108c
SS
21486 break;
21487 case DW_LNS_set_basic_block:
c906108c 21488 break;
c2c6d25f
JM
21489 /* Add to the address register of the state machine the
21490 address increment value corresponding to special opcode
a738430d
MK
21491 255. I.e., this value is scaled by the minimum
21492 instruction length since special opcode 255 would have
b021a221 21493 scaled the increment. */
c906108c 21494 case DW_LNS_const_add_pc:
6f77053d 21495 state_machine.handle_const_add_pc ();
c906108c
SS
21496 break;
21497 case DW_LNS_fixed_advance_pc:
3e29f34a 21498 {
6f77053d 21499 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21500 line_ptr += 2;
6f77053d
PA
21501
21502 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21503 }
c906108c 21504 break;
9aa1fe7e 21505 default:
a738430d
MK
21506 {
21507 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21508 int i;
a738430d 21509
debd256d 21510 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21511 {
21512 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21513 line_ptr += bytes_read;
21514 }
21515 }
c906108c
SS
21516 }
21517 }
d9b3de22
DE
21518
21519 if (!end_sequence)
21520 dwarf2_debug_line_missing_end_sequence_complaint ();
21521
21522 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21523 in which case we still finish recording the last line). */
6f77053d 21524 state_machine.record_line (true);
c906108c 21525 }
f3f5162e
DE
21526}
21527
21528/* Decode the Line Number Program (LNP) for the given line_header
21529 structure and CU. The actual information extracted and the type
21530 of structures created from the LNP depends on the value of PST.
21531
21532 1. If PST is NULL, then this procedure uses the data from the program
21533 to create all necessary symbol tables, and their linetables.
21534
21535 2. If PST is not NULL, this procedure reads the program to determine
21536 the list of files included by the unit represented by PST, and
21537 builds all the associated partial symbol tables.
21538
21539 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21540 It is used for relative paths in the line table.
21541 NOTE: When processing partial symtabs (pst != NULL),
21542 comp_dir == pst->dirname.
21543
21544 NOTE: It is important that psymtabs have the same file name (via strcmp)
21545 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21546 symtab we don't use it in the name of the psymtabs we create.
21547 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21548 A good testcase for this is mb-inline.exp.
21549
527f3840
JK
21550 LOWPC is the lowest address in CU (or 0 if not known).
21551
21552 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21553 for its PC<->lines mapping information. Otherwise only the filename
21554 table is read in. */
f3f5162e
DE
21555
21556static void
21557dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21558 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21559 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21560{
518817b3 21561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21562 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21563
527f3840
JK
21564 if (decode_mapping)
21565 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21566
21567 if (decode_for_pst_p)
21568 {
aaa75496
JB
21569 /* Now that we're done scanning the Line Header Program, we can
21570 create the psymtab of each included file. */
7ba99d21
AT
21571 for (auto &file_entry : lh->file_names ())
21572 if (file_entry.included_p == 1)
aaa75496 21573 {
c89b44cd 21574 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21575 const char *include_name =
7ba99d21
AT
21576 psymtab_include_file_name (lh, file_entry, pst,
21577 comp_dir, &name_holder);
c6da4cef 21578 if (include_name != NULL)
aaa75496
JB
21579 dwarf2_create_include_psymtab (include_name, pst, objfile);
21580 }
21581 }
cb1df416
DJ
21582 else
21583 {
21584 /* Make sure a symtab is created for every file, even files
21585 which contain only variables (i.e. no code with associated
21586 line numbers). */
c24bdb02
KS
21587 buildsym_compunit *builder = cu->get_builder ();
21588 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21589
7ba99d21 21590 for (auto &fe : lh->file_names ())
cb1df416 21591 {
804d2729 21592 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21593 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21594 {
c24bdb02 21595 builder->get_current_subfile ()->symtab
804d2729 21596 = allocate_symtab (cust,
c24bdb02 21597 builder->get_current_subfile ()->name);
43f3e411 21598 }
c24bdb02 21599 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21600 }
21601 }
c906108c
SS
21602}
21603
21604/* Start a subfile for DWARF. FILENAME is the name of the file and
21605 DIRNAME the name of the source directory which contains FILENAME
4d663531 21606 or NULL if not known.
c906108c
SS
21607 This routine tries to keep line numbers from identical absolute and
21608 relative file names in a common subfile.
21609
21610 Using the `list' example from the GDB testsuite, which resides in
21611 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21612 of /srcdir/list0.c yields the following debugging information for list0.c:
21613
c5aa993b 21614 DW_AT_name: /srcdir/list0.c
4d663531 21615 DW_AT_comp_dir: /compdir
357e46e7 21616 files.files[0].name: list0.h
c5aa993b 21617 files.files[0].dir: /srcdir
357e46e7 21618 files.files[1].name: list0.c
c5aa993b 21619 files.files[1].dir: /srcdir
c906108c
SS
21620
21621 The line number information for list0.c has to end up in a single
4f1520fb
FR
21622 subfile, so that `break /srcdir/list0.c:1' works as expected.
21623 start_subfile will ensure that this happens provided that we pass the
21624 concatenation of files.files[1].dir and files.files[1].name as the
21625 subfile's name. */
c906108c
SS
21626
21627static void
804d2729
TT
21628dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21629 const char *dirname)
c906108c 21630{
d521ce57 21631 char *copy = NULL;
4f1520fb 21632
4d663531 21633 /* In order not to lose the line information directory,
4f1520fb
FR
21634 we concatenate it to the filename when it makes sense.
21635 Note that the Dwarf3 standard says (speaking of filenames in line
21636 information): ``The directory index is ignored for file names
21637 that represent full path names''. Thus ignoring dirname in the
21638 `else' branch below isn't an issue. */
c906108c 21639
d5166ae1 21640 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21641 {
21642 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21643 filename = copy;
21644 }
c906108c 21645
c24bdb02 21646 cu->get_builder ()->start_subfile (filename);
4f1520fb 21647
d521ce57
TT
21648 if (copy != NULL)
21649 xfree (copy);
c906108c
SS
21650}
21651
804d2729
TT
21652/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21653 buildsym_compunit constructor. */
f4dc4d17 21654
c24bdb02
KS
21655struct compunit_symtab *
21656dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21657 CORE_ADDR low_pc)
f4dc4d17 21658{
c24bdb02 21659 gdb_assert (m_builder == nullptr);
43f3e411 21660
c24bdb02
KS
21661 m_builder.reset (new struct buildsym_compunit
21662 (per_cu->dwarf2_per_objfile->objfile,
21663 name, comp_dir, language, low_pc));
93b8bea4 21664
c24bdb02 21665 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21666
abeeff98 21667 get_builder ()->record_debugformat (xstrprintf ("DWARF %d", this->header.version));
c24bdb02 21668 get_builder ()->record_producer (producer);
f4dc4d17 21669
c24bdb02 21670 processing_has_namespace_info = false;
43f3e411 21671
c24bdb02 21672 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21673}
21674
4c2df51b
DJ
21675static void
21676var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21677 struct dwarf2_cu *cu)
4c2df51b 21678{
518817b3 21679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21680 struct comp_unit_head *cu_header = &cu->header;
21681
4c2df51b
DJ
21682 /* NOTE drow/2003-01-30: There used to be a comment and some special
21683 code here to turn a symbol with DW_AT_external and a
21684 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21685 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21686 with some versions of binutils) where shared libraries could have
21687 relocations against symbols in their debug information - the
21688 minimal symbol would have the right address, but the debug info
21689 would not. It's no longer necessary, because we will explicitly
21690 apply relocations when we read in the debug information now. */
21691
21692 /* A DW_AT_location attribute with no contents indicates that a
21693 variable has been optimized away. */
21694 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21695 {
f1e6e072 21696 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21697 return;
21698 }
21699
21700 /* Handle one degenerate form of location expression specially, to
21701 preserve GDB's previous behavior when section offsets are
336d760d
AT
21702 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21703 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21704
21705 if (attr_form_is_block (attr)
3019eac3
DE
21706 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21707 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21708 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21709 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21710 && (DW_BLOCK (attr)->size
21711 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21712 {
891d2f0b 21713 unsigned int dummy;
4c2df51b 21714
3019eac3 21715 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21716 SET_SYMBOL_VALUE_ADDRESS (sym,
21717 read_address (objfile->obfd,
21718 DW_BLOCK (attr)->data + 1,
21719 cu, &dummy));
3019eac3 21720 else
38583298
TT
21721 SET_SYMBOL_VALUE_ADDRESS
21722 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21723 &dummy));
f1e6e072 21724 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21725 fixup_symbol_section (sym, objfile);
38583298
TT
21726 SET_SYMBOL_VALUE_ADDRESS (sym,
21727 SYMBOL_VALUE_ADDRESS (sym)
21728 + ANOFFSET (objfile->section_offsets,
21729 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21730 return;
21731 }
21732
21733 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21734 expression evaluator, and use LOC_COMPUTED only when necessary
21735 (i.e. when the value of a register or memory location is
21736 referenced, or a thread-local block, etc.). Then again, it might
21737 not be worthwhile. I'm assuming that it isn't unless performance
21738 or memory numbers show me otherwise. */
21739
f1e6e072 21740 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21741
f1e6e072 21742 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21743 cu->has_loclist = true;
4c2df51b
DJ
21744}
21745
c906108c
SS
21746/* Given a pointer to a DWARF information entry, figure out if we need
21747 to make a symbol table entry for it, and if so, create a new entry
21748 and return a pointer to it.
21749 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21750 used the passed type.
21751 If SPACE is not NULL, use it to hold the new symbol. If it is
21752 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21753
21754static struct symbol *
5e2db402
TT
21755new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21756 struct symbol *space)
c906108c 21757{
518817b3
SM
21758 struct dwarf2_per_objfile *dwarf2_per_objfile
21759 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21760 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21762 struct symbol *sym = NULL;
15d034d0 21763 const char *name;
c906108c
SS
21764 struct attribute *attr = NULL;
21765 struct attribute *attr2 = NULL;
e142c38c 21766 CORE_ADDR baseaddr;
e37fd15a
SW
21767 struct pending **list_to_add = NULL;
21768
edb3359d 21769 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21770
21771 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21772
94af9270 21773 name = dwarf2_name (die, cu);
c906108c
SS
21774 if (name)
21775 {
94af9270 21776 const char *linkagename;
34eaf542 21777 int suppress_add = 0;
94af9270 21778
34eaf542
TT
21779 if (space)
21780 sym = space;
21781 else
e623cf5d 21782 sym = allocate_symbol (objfile);
c906108c 21783 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21784
21785 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21786 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270 21787 linkagename = dwarf2_physname (name, die, cu);
31edb802 21788 SYMBOL_SET_NAMES (sym, linkagename, false, objfile);
c906108c 21789
f55ee35c
JK
21790 /* Fortran does not have mangling standard and the mangling does differ
21791 between gfortran, iFort etc. */
21792 if (cu->language == language_fortran
468c0cbb
CB
21793 && symbol_get_demangled_name (sym) == NULL)
21794 symbol_set_demangled_name (sym,
cfc594ee 21795 dwarf2_full_name (name, die, cu),
29df156d 21796 NULL);
f55ee35c 21797
c906108c 21798 /* Default assumptions.
c5aa993b 21799 Use the passed type or decode it from the die. */
176620f1 21800 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21801 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21802 if (type != NULL)
21803 SYMBOL_TYPE (sym) = type;
21804 else
e7c27a73 21805 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21806 attr = dwarf2_attr (die,
21807 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21808 cu);
435d3d88 21809 if (attr != nullptr)
c906108c
SS
21810 {
21811 SYMBOL_LINE (sym) = DW_UNSND (attr);
21812 }
cb1df416 21813
edb3359d
DJ
21814 attr = dwarf2_attr (die,
21815 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21816 cu);
435d3d88 21817 if (attr != nullptr)
cb1df416 21818 {
ecfb656c 21819 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21820 struct file_entry *fe;
9a619af0 21821
ecfb656c
PA
21822 if (cu->line_header != NULL)
21823 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21824 else
21825 fe = NULL;
21826
21827 if (fe == NULL)
b98664d3 21828 complaint (_("file index out of range"));
8c43009f
PA
21829 else
21830 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21831 }
21832
c906108c
SS
21833 switch (die->tag)
21834 {
21835 case DW_TAG_label:
e142c38c 21836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21837 if (attr != nullptr)
3e29f34a
MR
21838 {
21839 CORE_ADDR addr;
21840
21841 addr = attr_value_as_address (attr);
21842 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21843 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21844 }
0f5238ed
TT
21845 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21846 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21847 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21848 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21849 break;
21850 case DW_TAG_subprogram:
21851 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21852 finish_block. */
f1e6e072 21853 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21854 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21855 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21856 || cu->language == language_ada
21857 || cu->language == language_fortran)
c906108c 21858 {
2cfa0c8d 21859 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21860 Ada and Fortran subprograms, whether marked external or
21861 not, are always stored as a global symbol, because we want
21862 to be able to access them globally. For instance, we want
21863 to be able to break on a nested subprogram without having
21864 to specify the context. */
c24bdb02 21865 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21866 }
21867 else
21868 {
e37fd15a 21869 list_to_add = cu->list_in_scope;
c906108c
SS
21870 }
21871 break;
edb3359d
DJ
21872 case DW_TAG_inlined_subroutine:
21873 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21874 finish_block. */
f1e6e072 21875 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21876 SYMBOL_INLINED (sym) = 1;
481860b3 21877 list_to_add = cu->list_in_scope;
edb3359d 21878 break;
34eaf542
TT
21879 case DW_TAG_template_value_param:
21880 suppress_add = 1;
21881 /* Fall through. */
72929c62 21882 case DW_TAG_constant:
c906108c 21883 case DW_TAG_variable:
254e6b9e 21884 case DW_TAG_member:
0963b4bd
MS
21885 /* Compilation with minimal debug info may result in
21886 variables with missing type entries. Change the
21887 misleading `void' type to something sensible. */
c906108c 21888 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21889 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21890
e142c38c 21891 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21892 /* In the case of DW_TAG_member, we should only be called for
21893 static const members. */
21894 if (die->tag == DW_TAG_member)
21895 {
3863f96c
DE
21896 /* dwarf2_add_field uses die_is_declaration,
21897 so we do the same. */
254e6b9e
DE
21898 gdb_assert (die_is_declaration (die, cu));
21899 gdb_assert (attr);
21900 }
435d3d88 21901 if (attr != nullptr)
c906108c 21902 {
e7c27a73 21903 dwarf2_const_value (attr, sym, cu);
e142c38c 21904 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21905 if (!suppress_add)
34eaf542
TT
21906 {
21907 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21908 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21909 else
e37fd15a 21910 list_to_add = cu->list_in_scope;
34eaf542 21911 }
c906108c
SS
21912 break;
21913 }
e142c38c 21914 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21915 if (attr != nullptr)
c906108c 21916 {
e7c27a73 21917 var_decode_location (attr, sym, cu);
e142c38c 21918 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21919
21920 /* Fortran explicitly imports any global symbols to the local
21921 scope by DW_TAG_common_block. */
21922 if (cu->language == language_fortran && die->parent
21923 && die->parent->tag == DW_TAG_common_block)
21924 attr2 = NULL;
21925
caac4577
JG
21926 if (SYMBOL_CLASS (sym) == LOC_STATIC
21927 && SYMBOL_VALUE_ADDRESS (sym) == 0
21928 && !dwarf2_per_objfile->has_section_at_zero)
21929 {
21930 /* When a static variable is eliminated by the linker,
21931 the corresponding debug information is not stripped
21932 out, but the variable address is set to null;
21933 do not add such variables into symbol table. */
21934 }
21935 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21936 {
4b610737
TT
21937 if (SYMBOL_CLASS (sym) == LOC_STATIC
21938 && (objfile->flags & OBJF_MAINLINE) == 0
21939 && dwarf2_per_objfile->can_copy)
21940 {
21941 /* A global static variable might be subject to
21942 copy relocation. We first check for a local
21943 minsym, though, because maybe the symbol was
21944 marked hidden, in which case this would not
21945 apply. */
21946 bound_minimal_symbol found
21947 = (lookup_minimal_symbol_linkage
987012b8 21948 (sym->linkage_name (), objfile));
4b610737
TT
21949 if (found.minsym != nullptr)
21950 sym->maybe_copied = 1;
21951 }
f55ee35c 21952
1c809c68
TT
21953 /* A variable with DW_AT_external is never static,
21954 but it may be block-scoped. */
804d2729 21955 list_to_add
c24bdb02
KS
21956 = ((cu->list_in_scope
21957 == cu->get_builder ()->get_file_symbols ())
21958 ? cu->get_builder ()->get_global_symbols ()
804d2729 21959 : cu->list_in_scope);
1c809c68 21960 }
c906108c 21961 else
e37fd15a 21962 list_to_add = cu->list_in_scope;
c906108c
SS
21963 }
21964 else
21965 {
21966 /* We do not know the address of this symbol.
c5aa993b
JM
21967 If it is an external symbol and we have type information
21968 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21969 The address of the variable will then be determined from
21970 the minimal symbol table whenever the variable is
21971 referenced. */
e142c38c 21972 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21973
21974 /* Fortran explicitly imports any global symbols to the local
21975 scope by DW_TAG_common_block. */
21976 if (cu->language == language_fortran && die->parent
21977 && die->parent->tag == DW_TAG_common_block)
21978 {
21979 /* SYMBOL_CLASS doesn't matter here because
21980 read_common_block is going to reset it. */
21981 if (!suppress_add)
21982 list_to_add = cu->list_in_scope;
21983 }
21984 else if (attr2 && (DW_UNSND (attr2) != 0)
21985 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21986 {
0fe7935b
DJ
21987 /* A variable with DW_AT_external is never static, but it
21988 may be block-scoped. */
804d2729 21989 list_to_add
c24bdb02
KS
21990 = ((cu->list_in_scope
21991 == cu->get_builder ()->get_file_symbols ())
21992 ? cu->get_builder ()->get_global_symbols ()
804d2729 21993 : cu->list_in_scope);
0fe7935b 21994
f1e6e072 21995 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21996 }
442ddf59
JK
21997 else if (!die_is_declaration (die, cu))
21998 {
21999 /* Use the default LOC_OPTIMIZED_OUT class. */
22000 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22001 if (!suppress_add)
22002 list_to_add = cu->list_in_scope;
442ddf59 22003 }
c906108c
SS
22004 }
22005 break;
22006 case DW_TAG_formal_parameter:
a60f3166
TT
22007 {
22008 /* If we are inside a function, mark this as an argument. If
22009 not, we might be looking at an argument to an inlined function
22010 when we do not have enough information to show inlined frames;
22011 pretend it's a local variable in that case so that the user can
22012 still see it. */
804d2729 22013 struct context_stack *curr
c24bdb02 22014 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22015 if (curr != nullptr && curr->name != nullptr)
22016 SYMBOL_IS_ARGUMENT (sym) = 1;
22017 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22018 if (attr != nullptr)
a60f3166
TT
22019 {
22020 var_decode_location (attr, sym, cu);
22021 }
22022 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22023 if (attr != nullptr)
a60f3166
TT
22024 {
22025 dwarf2_const_value (attr, sym, cu);
22026 }
f346a30d 22027
a60f3166
TT
22028 list_to_add = cu->list_in_scope;
22029 }
c906108c
SS
22030 break;
22031 case DW_TAG_unspecified_parameters:
22032 /* From varargs functions; gdb doesn't seem to have any
22033 interest in this information, so just ignore it for now.
22034 (FIXME?) */
22035 break;
34eaf542
TT
22036 case DW_TAG_template_type_param:
22037 suppress_add = 1;
22038 /* Fall through. */
c906108c 22039 case DW_TAG_class_type:
680b30c7 22040 case DW_TAG_interface_type:
c906108c
SS
22041 case DW_TAG_structure_type:
22042 case DW_TAG_union_type:
72019c9c 22043 case DW_TAG_set_type:
c906108c 22044 case DW_TAG_enumeration_type:
f1e6e072 22045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22046 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22047
63d06c5c 22048 {
9c37b5ae 22049 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22050 really ever be static objects: otherwise, if you try
22051 to, say, break of a class's method and you're in a file
22052 which doesn't mention that class, it won't work unless
22053 the check for all static symbols in lookup_symbol_aux
22054 saves you. See the OtherFileClass tests in
22055 gdb.c++/namespace.exp. */
22056
e37fd15a 22057 if (!suppress_add)
34eaf542 22058 {
c24bdb02 22059 buildsym_compunit *builder = cu->get_builder ();
804d2729 22060 list_to_add
c24bdb02 22061 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22062 && cu->language == language_cplus
c24bdb02 22063 ? builder->get_global_symbols ()
804d2729 22064 : cu->list_in_scope);
63d06c5c 22065
64382290 22066 /* The semantics of C++ state that "struct foo {
9c37b5ae 22067 ... }" also defines a typedef for "foo". */
64382290 22068 if (cu->language == language_cplus
45280282 22069 || cu->language == language_ada
c44af4eb
TT
22070 || cu->language == language_d
22071 || cu->language == language_rust)
64382290
TT
22072 {
22073 /* The symbol's name is already allocated along
22074 with this objfile, so we don't need to
22075 duplicate it for the type. */
22076 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22077 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22078 }
63d06c5c
DC
22079 }
22080 }
c906108c
SS
22081 break;
22082 case DW_TAG_typedef:
f1e6e072 22083 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22084 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22085 list_to_add = cu->list_in_scope;
63d06c5c 22086 break;
c906108c 22087 case DW_TAG_base_type:
a02abb62 22088 case DW_TAG_subrange_type:
f1e6e072 22089 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22090 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22091 list_to_add = cu->list_in_scope;
c906108c
SS
22092 break;
22093 case DW_TAG_enumerator:
e142c38c 22094 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22095 if (attr != nullptr)
c906108c 22096 {
e7c27a73 22097 dwarf2_const_value (attr, sym, cu);
c906108c 22098 }
63d06c5c
DC
22099 {
22100 /* NOTE: carlton/2003-11-10: See comment above in the
22101 DW_TAG_class_type, etc. block. */
22102
804d2729 22103 list_to_add
c24bdb02 22104 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22105 && cu->language == language_cplus
c24bdb02 22106 ? cu->get_builder ()->get_global_symbols ()
804d2729 22107 : cu->list_in_scope);
63d06c5c 22108 }
c906108c 22109 break;
74921315 22110 case DW_TAG_imported_declaration:
5c4e30ca 22111 case DW_TAG_namespace:
f1e6e072 22112 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22113 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22114 break;
530e8392
KB
22115 case DW_TAG_module:
22116 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22117 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22118 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22119 break;
4357ac6c 22120 case DW_TAG_common_block:
f1e6e072 22121 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22122 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22123 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22124 break;
c906108c
SS
22125 default:
22126 /* Not a tag we recognize. Hopefully we aren't processing
22127 trash data, but since we must specifically ignore things
22128 we don't recognize, there is nothing else we should do at
0963b4bd 22129 this point. */
b98664d3 22130 complaint (_("unsupported tag: '%s'"),
4d3c2250 22131 dwarf_tag_name (die->tag));
c906108c
SS
22132 break;
22133 }
df8a16a1 22134
e37fd15a
SW
22135 if (suppress_add)
22136 {
22137 sym->hash_next = objfile->template_symbols;
22138 objfile->template_symbols = sym;
22139 list_to_add = NULL;
22140 }
22141
22142 if (list_to_add != NULL)
d3cb6808 22143 add_symbol_to_list (sym, list_to_add);
e37fd15a 22144
df8a16a1
DJ
22145 /* For the benefit of old versions of GCC, check for anonymous
22146 namespaces based on the demangled name. */
4d4ec4e5 22147 if (!cu->processing_has_namespace_info
94af9270 22148 && cu->language == language_cplus)
c24bdb02 22149 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22150 }
22151 return (sym);
22152}
22153
98bfdba5
PA
22154/* Given an attr with a DW_FORM_dataN value in host byte order,
22155 zero-extend it as appropriate for the symbol's type. The DWARF
22156 standard (v4) is not entirely clear about the meaning of using
22157 DW_FORM_dataN for a constant with a signed type, where the type is
22158 wider than the data. The conclusion of a discussion on the DWARF
22159 list was that this is unspecified. We choose to always zero-extend
22160 because that is the interpretation long in use by GCC. */
c906108c 22161
98bfdba5 22162static gdb_byte *
ff39bb5e 22163dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22164 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22165{
518817b3 22166 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22167 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22168 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22169 LONGEST l = DW_UNSND (attr);
22170
22171 if (bits < sizeof (*value) * 8)
22172 {
22173 l &= ((LONGEST) 1 << bits) - 1;
22174 *value = l;
22175 }
22176 else if (bits == sizeof (*value) * 8)
22177 *value = l;
22178 else
22179 {
224c3ddb 22180 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22181 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22182 return bytes;
22183 }
22184
22185 return NULL;
22186}
22187
22188/* Read a constant value from an attribute. Either set *VALUE, or if
22189 the value does not fit in *VALUE, set *BYTES - either already
22190 allocated on the objfile obstack, or newly allocated on OBSTACK,
22191 or, set *BATON, if we translated the constant to a location
22192 expression. */
22193
22194static void
ff39bb5e 22195dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22196 const char *name, struct obstack *obstack,
22197 struct dwarf2_cu *cu,
d521ce57 22198 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22199 struct dwarf2_locexpr_baton **baton)
22200{
518817b3 22201 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22202 struct comp_unit_head *cu_header = &cu->header;
c906108c 22203 struct dwarf_block *blk;
98bfdba5
PA
22204 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22205 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22206
22207 *value = 0;
22208 *bytes = NULL;
22209 *baton = NULL;
c906108c
SS
22210
22211 switch (attr->form)
22212 {
22213 case DW_FORM_addr:
336d760d 22214 case DW_FORM_addrx:
3019eac3 22215 case DW_FORM_GNU_addr_index:
ac56253d 22216 {
ac56253d
TT
22217 gdb_byte *data;
22218
98bfdba5
PA
22219 if (TYPE_LENGTH (type) != cu_header->addr_size)
22220 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22221 cu_header->addr_size,
98bfdba5 22222 TYPE_LENGTH (type));
ac56253d
TT
22223 /* Symbols of this form are reasonably rare, so we just
22224 piggyback on the existing location code rather than writing
22225 a new implementation of symbol_computed_ops. */
8d749320 22226 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22227 (*baton)->per_cu = cu->per_cu;
22228 gdb_assert ((*baton)->per_cu);
ac56253d 22229
98bfdba5 22230 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22231 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22232 (*baton)->data = data;
ac56253d
TT
22233
22234 data[0] = DW_OP_addr;
22235 store_unsigned_integer (&data[1], cu_header->addr_size,
22236 byte_order, DW_ADDR (attr));
22237 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22238 }
c906108c 22239 break;
4ac36638 22240 case DW_FORM_string:
93b5768b 22241 case DW_FORM_strp:
cf532bd1 22242 case DW_FORM_strx:
3019eac3 22243 case DW_FORM_GNU_str_index:
36586728 22244 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22245 /* DW_STRING is already allocated on the objfile obstack, point
22246 directly to it. */
d521ce57 22247 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22248 break;
c906108c
SS
22249 case DW_FORM_block1:
22250 case DW_FORM_block2:
22251 case DW_FORM_block4:
22252 case DW_FORM_block:
2dc7f7b3 22253 case DW_FORM_exprloc:
0224619f 22254 case DW_FORM_data16:
c906108c 22255 blk = DW_BLOCK (attr);
98bfdba5
PA
22256 if (TYPE_LENGTH (type) != blk->size)
22257 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22258 TYPE_LENGTH (type));
22259 *bytes = blk->data;
c906108c 22260 break;
2df3850c
JM
22261
22262 /* The DW_AT_const_value attributes are supposed to carry the
22263 symbol's value "represented as it would be on the target
22264 architecture." By the time we get here, it's already been
22265 converted to host endianness, so we just need to sign- or
22266 zero-extend it as appropriate. */
22267 case DW_FORM_data1:
3aef2284 22268 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22269 break;
c906108c 22270 case DW_FORM_data2:
3aef2284 22271 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22272 break;
c906108c 22273 case DW_FORM_data4:
3aef2284 22274 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22275 break;
c906108c 22276 case DW_FORM_data8:
3aef2284 22277 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22278 break;
22279
c906108c 22280 case DW_FORM_sdata:
663c44ac 22281 case DW_FORM_implicit_const:
98bfdba5 22282 *value = DW_SND (attr);
2df3850c
JM
22283 break;
22284
c906108c 22285 case DW_FORM_udata:
98bfdba5 22286 *value = DW_UNSND (attr);
c906108c 22287 break;
2df3850c 22288
c906108c 22289 default:
b98664d3 22290 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22291 dwarf_form_name (attr->form));
98bfdba5 22292 *value = 0;
c906108c
SS
22293 break;
22294 }
22295}
22296
2df3850c 22297
98bfdba5
PA
22298/* Copy constant value from an attribute to a symbol. */
22299
2df3850c 22300static void
ff39bb5e 22301dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22302 struct dwarf2_cu *cu)
2df3850c 22303{
518817b3 22304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22305 LONGEST value;
d521ce57 22306 const gdb_byte *bytes;
98bfdba5 22307 struct dwarf2_locexpr_baton *baton;
2df3850c 22308
98bfdba5 22309 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22310 sym->print_name (),
98bfdba5
PA
22311 &objfile->objfile_obstack, cu,
22312 &value, &bytes, &baton);
2df3850c 22313
98bfdba5
PA
22314 if (baton != NULL)
22315 {
98bfdba5 22316 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22317 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22318 }
22319 else if (bytes != NULL)
22320 {
22321 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22322 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22323 }
22324 else
22325 {
22326 SYMBOL_VALUE (sym) = value;
f1e6e072 22327 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22328 }
2df3850c
JM
22329}
22330
c906108c
SS
22331/* Return the type of the die in question using its DW_AT_type attribute. */
22332
22333static struct type *
e7c27a73 22334die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22335{
c906108c 22336 struct attribute *type_attr;
c906108c 22337
e142c38c 22338 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22339 if (!type_attr)
22340 {
518817b3 22341 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22342 /* A missing DW_AT_type represents a void type. */
518817b3 22343 return objfile_type (objfile)->builtin_void;
c906108c 22344 }
348e048f 22345
673bfd45 22346 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22347}
22348
b4ba55a1
JB
22349/* True iff CU's producer generates GNAT Ada auxiliary information
22350 that allows to find parallel types through that information instead
22351 of having to do expensive parallel lookups by type name. */
22352
22353static int
22354need_gnat_info (struct dwarf2_cu *cu)
22355{
de4cb04a
JB
22356 /* Assume that the Ada compiler was GNAT, which always produces
22357 the auxiliary information. */
22358 return (cu->language == language_ada);
b4ba55a1
JB
22359}
22360
b4ba55a1
JB
22361/* Return the auxiliary type of the die in question using its
22362 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22363 attribute is not present. */
22364
22365static struct type *
22366die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22367{
b4ba55a1 22368 struct attribute *type_attr;
b4ba55a1
JB
22369
22370 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22371 if (!type_attr)
22372 return NULL;
22373
673bfd45 22374 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22375}
22376
22377/* If DIE has a descriptive_type attribute, then set the TYPE's
22378 descriptive type accordingly. */
22379
22380static void
22381set_descriptive_type (struct type *type, struct die_info *die,
22382 struct dwarf2_cu *cu)
22383{
22384 struct type *descriptive_type = die_descriptive_type (die, cu);
22385
22386 if (descriptive_type)
22387 {
22388 ALLOCATE_GNAT_AUX_TYPE (type);
22389 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22390 }
22391}
22392
c906108c
SS
22393/* Return the containing type of the die in question using its
22394 DW_AT_containing_type attribute. */
22395
22396static struct type *
e7c27a73 22397die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22398{
c906108c 22399 struct attribute *type_attr;
518817b3 22400 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22401
e142c38c 22402 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22403 if (!type_attr)
22404 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22405 "[in module %s]"), objfile_name (objfile));
33ac96f0 22406
673bfd45 22407 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22408}
22409
ac9ec31b
DE
22410/* Return an error marker type to use for the ill formed type in DIE/CU. */
22411
22412static struct type *
22413build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22414{
518817b3
SM
22415 struct dwarf2_per_objfile *dwarf2_per_objfile
22416 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22417 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22418 char *saved;
ac9ec31b 22419
528e1572
SM
22420 std::string message
22421 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22422 objfile_name (objfile),
22423 sect_offset_str (cu->header.sect_off),
22424 sect_offset_str (die->sect_off));
efba19b0 22425 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22426
19f392bc 22427 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22428}
22429
673bfd45 22430/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22431 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22432 DW_AT_containing_type.
673bfd45
DE
22433 If there is no type substitute an error marker. */
22434
c906108c 22435static struct type *
ff39bb5e 22436lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22437 struct dwarf2_cu *cu)
c906108c 22438{
518817b3
SM
22439 struct dwarf2_per_objfile *dwarf2_per_objfile
22440 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22441 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22442 struct type *this_type;
22443
ac9ec31b
DE
22444 gdb_assert (attr->name == DW_AT_type
22445 || attr->name == DW_AT_GNAT_descriptive_type
22446 || attr->name == DW_AT_containing_type);
22447
673bfd45
DE
22448 /* First see if we have it cached. */
22449
36586728
TT
22450 if (attr->form == DW_FORM_GNU_ref_alt)
22451 {
22452 struct dwarf2_per_cu_data *per_cu;
9c541725 22453 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22454
ed2dc618
SM
22455 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22456 dwarf2_per_objfile);
9c541725 22457 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22458 }
7771576e 22459 else if (attr_form_is_ref (attr))
673bfd45 22460 {
9c541725 22461 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22462
9c541725 22463 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22464 }
55f1336d 22465 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22466 {
ac9ec31b 22467 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22468
ac9ec31b 22469 return get_signatured_type (die, signature, cu);
673bfd45
DE
22470 }
22471 else
22472 {
b98664d3 22473 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22474 " at %s [in module %s]"),
22475 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22476 objfile_name (objfile));
ac9ec31b 22477 return build_error_marker_type (cu, die);
673bfd45
DE
22478 }
22479
22480 /* If not cached we need to read it in. */
22481
22482 if (this_type == NULL)
22483 {
ac9ec31b 22484 struct die_info *type_die = NULL;
673bfd45
DE
22485 struct dwarf2_cu *type_cu = cu;
22486
7771576e 22487 if (attr_form_is_ref (attr))
ac9ec31b
DE
22488 type_die = follow_die_ref (die, attr, &type_cu);
22489 if (type_die == NULL)
22490 return build_error_marker_type (cu, die);
22491 /* If we find the type now, it's probably because the type came
3019eac3
DE
22492 from an inter-CU reference and the type's CU got expanded before
22493 ours. */
ac9ec31b 22494 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22495 }
22496
22497 /* If we still don't have a type use an error marker. */
22498
22499 if (this_type == NULL)
ac9ec31b 22500 return build_error_marker_type (cu, die);
673bfd45 22501
f792889a 22502 return this_type;
c906108c
SS
22503}
22504
673bfd45
DE
22505/* Return the type in DIE, CU.
22506 Returns NULL for invalid types.
22507
02142a6c 22508 This first does a lookup in die_type_hash,
673bfd45
DE
22509 and only reads the die in if necessary.
22510
22511 NOTE: This can be called when reading in partial or full symbols. */
22512
f792889a 22513static struct type *
e7c27a73 22514read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22515{
f792889a
DJ
22516 struct type *this_type;
22517
22518 this_type = get_die_type (die, cu);
22519 if (this_type)
22520 return this_type;
22521
673bfd45
DE
22522 return read_type_die_1 (die, cu);
22523}
22524
22525/* Read the type in DIE, CU.
22526 Returns NULL for invalid types. */
22527
22528static struct type *
22529read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22530{
22531 struct type *this_type = NULL;
22532
c906108c
SS
22533 switch (die->tag)
22534 {
22535 case DW_TAG_class_type:
680b30c7 22536 case DW_TAG_interface_type:
c906108c
SS
22537 case DW_TAG_structure_type:
22538 case DW_TAG_union_type:
f792889a 22539 this_type = read_structure_type (die, cu);
c906108c
SS
22540 break;
22541 case DW_TAG_enumeration_type:
f792889a 22542 this_type = read_enumeration_type (die, cu);
c906108c
SS
22543 break;
22544 case DW_TAG_subprogram:
22545 case DW_TAG_subroutine_type:
edb3359d 22546 case DW_TAG_inlined_subroutine:
f792889a 22547 this_type = read_subroutine_type (die, cu);
c906108c
SS
22548 break;
22549 case DW_TAG_array_type:
f792889a 22550 this_type = read_array_type (die, cu);
c906108c 22551 break;
72019c9c 22552 case DW_TAG_set_type:
f792889a 22553 this_type = read_set_type (die, cu);
72019c9c 22554 break;
c906108c 22555 case DW_TAG_pointer_type:
f792889a 22556 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22557 break;
22558 case DW_TAG_ptr_to_member_type:
f792889a 22559 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22560 break;
22561 case DW_TAG_reference_type:
4297a3f0
AV
22562 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22563 break;
22564 case DW_TAG_rvalue_reference_type:
22565 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22566 break;
22567 case DW_TAG_const_type:
f792889a 22568 this_type = read_tag_const_type (die, cu);
c906108c
SS
22569 break;
22570 case DW_TAG_volatile_type:
f792889a 22571 this_type = read_tag_volatile_type (die, cu);
c906108c 22572 break;
06d66ee9
TT
22573 case DW_TAG_restrict_type:
22574 this_type = read_tag_restrict_type (die, cu);
22575 break;
c906108c 22576 case DW_TAG_string_type:
f792889a 22577 this_type = read_tag_string_type (die, cu);
c906108c
SS
22578 break;
22579 case DW_TAG_typedef:
f792889a 22580 this_type = read_typedef (die, cu);
c906108c 22581 break;
a02abb62 22582 case DW_TAG_subrange_type:
f792889a 22583 this_type = read_subrange_type (die, cu);
a02abb62 22584 break;
c906108c 22585 case DW_TAG_base_type:
f792889a 22586 this_type = read_base_type (die, cu);
c906108c 22587 break;
81a17f79 22588 case DW_TAG_unspecified_type:
f792889a 22589 this_type = read_unspecified_type (die, cu);
81a17f79 22590 break;
0114d602
DJ
22591 case DW_TAG_namespace:
22592 this_type = read_namespace_type (die, cu);
22593 break;
f55ee35c
JK
22594 case DW_TAG_module:
22595 this_type = read_module_type (die, cu);
22596 break;
a2c2acaf
MW
22597 case DW_TAG_atomic_type:
22598 this_type = read_tag_atomic_type (die, cu);
22599 break;
c906108c 22600 default:
b98664d3 22601 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22602 dwarf_tag_name (die->tag));
c906108c
SS
22603 break;
22604 }
63d06c5c 22605
f792889a 22606 return this_type;
63d06c5c
DC
22607}
22608
abc72ce4
DE
22609/* See if we can figure out if the class lives in a namespace. We do
22610 this by looking for a member function; its demangled name will
22611 contain namespace info, if there is any.
22612 Return the computed name or NULL.
22613 Space for the result is allocated on the objfile's obstack.
22614 This is the full-die version of guess_partial_die_structure_name.
22615 In this case we know DIE has no useful parent. */
22616
22617static char *
22618guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22619{
22620 struct die_info *spec_die;
22621 struct dwarf2_cu *spec_cu;
22622 struct die_info *child;
518817b3 22623 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22624
22625 spec_cu = cu;
22626 spec_die = die_specification (die, &spec_cu);
22627 if (spec_die != NULL)
22628 {
22629 die = spec_die;
22630 cu = spec_cu;
22631 }
22632
22633 for (child = die->child;
22634 child != NULL;
22635 child = child->sibling)
22636 {
22637 if (child->tag == DW_TAG_subprogram)
22638 {
73b9be8b 22639 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22640
7d45c7c3 22641 if (linkage_name != NULL)
abc72ce4
DE
22642 {
22643 char *actual_name
22644 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22645 linkage_name);
abc72ce4
DE
22646 char *name = NULL;
22647
22648 if (actual_name != NULL)
22649 {
15d034d0 22650 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22651
22652 if (die_name != NULL
22653 && strcmp (die_name, actual_name) != 0)
22654 {
22655 /* Strip off the class name from the full name.
22656 We want the prefix. */
22657 int die_name_len = strlen (die_name);
22658 int actual_name_len = strlen (actual_name);
22659
22660 /* Test for '::' as a sanity check. */
22661 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22662 && actual_name[actual_name_len
22663 - die_name_len - 1] == ':')
0cf9feb9 22664 name = obstack_strndup (
e3b94546 22665 &objfile->per_bfd->storage_obstack,
224c3ddb 22666 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22667 }
22668 }
22669 xfree (actual_name);
22670 return name;
22671 }
22672 }
22673 }
22674
22675 return NULL;
22676}
22677
96408a79
SA
22678/* GCC might emit a nameless typedef that has a linkage name. Determine the
22679 prefix part in such case. See
22680 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22681
a121b7c1 22682static const char *
96408a79
SA
22683anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22684{
22685 struct attribute *attr;
e6a959d6 22686 const char *base;
96408a79
SA
22687
22688 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22689 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22690 return NULL;
22691
7d45c7c3 22692 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22693 return NULL;
22694
73b9be8b 22695 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22696 if (attr == NULL || DW_STRING (attr) == NULL)
22697 return NULL;
22698
22699 /* dwarf2_name had to be already called. */
22700 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22701
22702 /* Strip the base name, keep any leading namespaces/classes. */
22703 base = strrchr (DW_STRING (attr), ':');
22704 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22705 return "";
22706
518817b3 22707 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22708 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22709 DW_STRING (attr),
22710 &base[-1] - DW_STRING (attr));
96408a79
SA
22711}
22712
fdde2d81 22713/* Return the name of the namespace/class that DIE is defined within,
0114d602 22714 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22715
0114d602
DJ
22716 For example, if we're within the method foo() in the following
22717 code:
22718
22719 namespace N {
22720 class C {
22721 void foo () {
22722 }
22723 };
22724 }
22725
22726 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22727
0d5cff50 22728static const char *
e142c38c 22729determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22730{
518817b3
SM
22731 struct dwarf2_per_objfile *dwarf2_per_objfile
22732 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22733 struct die_info *parent, *spec_die;
22734 struct dwarf2_cu *spec_cu;
22735 struct type *parent_type;
a121b7c1 22736 const char *retval;
63d06c5c 22737
9c37b5ae 22738 if (cu->language != language_cplus
c44af4eb
TT
22739 && cu->language != language_fortran && cu->language != language_d
22740 && cu->language != language_rust)
0114d602
DJ
22741 return "";
22742
96408a79
SA
22743 retval = anonymous_struct_prefix (die, cu);
22744 if (retval)
22745 return retval;
22746
0114d602
DJ
22747 /* We have to be careful in the presence of DW_AT_specification.
22748 For example, with GCC 3.4, given the code
22749
22750 namespace N {
22751 void foo() {
22752 // Definition of N::foo.
22753 }
22754 }
22755
22756 then we'll have a tree of DIEs like this:
22757
22758 1: DW_TAG_compile_unit
22759 2: DW_TAG_namespace // N
22760 3: DW_TAG_subprogram // declaration of N::foo
22761 4: DW_TAG_subprogram // definition of N::foo
22762 DW_AT_specification // refers to die #3
22763
22764 Thus, when processing die #4, we have to pretend that we're in
22765 the context of its DW_AT_specification, namely the contex of die
22766 #3. */
22767 spec_cu = cu;
22768 spec_die = die_specification (die, &spec_cu);
22769 if (spec_die == NULL)
22770 parent = die->parent;
22771 else
63d06c5c 22772 {
0114d602
DJ
22773 parent = spec_die->parent;
22774 cu = spec_cu;
63d06c5c 22775 }
0114d602
DJ
22776
22777 if (parent == NULL)
22778 return "";
98bfdba5
PA
22779 else if (parent->building_fullname)
22780 {
22781 const char *name;
22782 const char *parent_name;
22783
22784 /* It has been seen on RealView 2.2 built binaries,
22785 DW_TAG_template_type_param types actually _defined_ as
22786 children of the parent class:
22787
22788 enum E {};
22789 template class <class Enum> Class{};
22790 Class<enum E> class_e;
22791
22792 1: DW_TAG_class_type (Class)
22793 2: DW_TAG_enumeration_type (E)
22794 3: DW_TAG_enumerator (enum1:0)
22795 3: DW_TAG_enumerator (enum2:1)
22796 ...
22797 2: DW_TAG_template_type_param
22798 DW_AT_type DW_FORM_ref_udata (E)
22799
22800 Besides being broken debug info, it can put GDB into an
22801 infinite loop. Consider:
22802
22803 When we're building the full name for Class<E>, we'll start
22804 at Class, and go look over its template type parameters,
22805 finding E. We'll then try to build the full name of E, and
22806 reach here. We're now trying to build the full name of E,
22807 and look over the parent DIE for containing scope. In the
22808 broken case, if we followed the parent DIE of E, we'd again
22809 find Class, and once again go look at its template type
22810 arguments, etc., etc. Simply don't consider such parent die
22811 as source-level parent of this die (it can't be, the language
22812 doesn't allow it), and break the loop here. */
22813 name = dwarf2_name (die, cu);
22814 parent_name = dwarf2_name (parent, cu);
b98664d3 22815 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22816 name ? name : "<unknown>",
22817 parent_name ? parent_name : "<unknown>");
22818 return "";
22819 }
63d06c5c 22820 else
0114d602
DJ
22821 switch (parent->tag)
22822 {
63d06c5c 22823 case DW_TAG_namespace:
0114d602 22824 parent_type = read_type_die (parent, cu);
acebe513
UW
22825 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22826 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22827 Work around this problem here. */
22828 if (cu->language == language_cplus
e86ca25f 22829 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22830 return "";
0114d602 22831 /* We give a name to even anonymous namespaces. */
e86ca25f 22832 return TYPE_NAME (parent_type);
63d06c5c 22833 case DW_TAG_class_type:
680b30c7 22834 case DW_TAG_interface_type:
63d06c5c 22835 case DW_TAG_structure_type:
0114d602 22836 case DW_TAG_union_type:
f55ee35c 22837 case DW_TAG_module:
0114d602 22838 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22839 if (TYPE_NAME (parent_type) != NULL)
22840 return TYPE_NAME (parent_type);
0114d602
DJ
22841 else
22842 /* An anonymous structure is only allowed non-static data
22843 members; no typedefs, no member functions, et cetera.
22844 So it does not need a prefix. */
22845 return "";
abc72ce4 22846 case DW_TAG_compile_unit:
95554aad 22847 case DW_TAG_partial_unit:
abc72ce4
DE
22848 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22849 if (cu->language == language_cplus
fd5866f6 22850 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22851 && die->child != NULL
22852 && (die->tag == DW_TAG_class_type
22853 || die->tag == DW_TAG_structure_type
22854 || die->tag == DW_TAG_union_type))
22855 {
22856 char *name = guess_full_die_structure_name (die, cu);
22857 if (name != NULL)
22858 return name;
22859 }
22860 return "";
0a4b0913
AB
22861 case DW_TAG_subprogram:
22862 /* Nested subroutines in Fortran get a prefix with the name
22863 of the parent's subroutine. */
22864 if (cu->language == language_fortran)
22865 {
22866 if ((die->tag == DW_TAG_subprogram)
22867 && (dwarf2_name (parent, cu) != NULL))
22868 return dwarf2_name (parent, cu);
22869 }
22870 return determine_prefix (parent, cu);
3d567982
TT
22871 case DW_TAG_enumeration_type:
22872 parent_type = read_type_die (parent, cu);
22873 if (TYPE_DECLARED_CLASS (parent_type))
22874 {
e86ca25f
TT
22875 if (TYPE_NAME (parent_type) != NULL)
22876 return TYPE_NAME (parent_type);
3d567982
TT
22877 return "";
22878 }
22879 /* Fall through. */
63d06c5c 22880 default:
8176b9b8 22881 return determine_prefix (parent, cu);
63d06c5c 22882 }
63d06c5c
DC
22883}
22884
3e43a32a
MS
22885/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22886 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22887 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22888 an obconcat, otherwise allocate storage for the result. The CU argument is
22889 used to determine the language and hence, the appropriate separator. */
987504bb 22890
f55ee35c 22891#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22892
22893static char *
f55ee35c
JK
22894typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22895 int physname, struct dwarf2_cu *cu)
63d06c5c 22896{
f55ee35c 22897 const char *lead = "";
5c315b68 22898 const char *sep;
63d06c5c 22899
3e43a32a
MS
22900 if (suffix == NULL || suffix[0] == '\0'
22901 || prefix == NULL || prefix[0] == '\0')
987504bb 22902 sep = "";
45280282
IB
22903 else if (cu->language == language_d)
22904 {
22905 /* For D, the 'main' function could be defined in any module, but it
22906 should never be prefixed. */
22907 if (strcmp (suffix, "D main") == 0)
22908 {
22909 prefix = "";
22910 sep = "";
22911 }
22912 else
22913 sep = ".";
22914 }
f55ee35c
JK
22915 else if (cu->language == language_fortran && physname)
22916 {
22917 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22918 DW_AT_MIPS_linkage_name is preferred and used instead. */
22919
22920 lead = "__";
22921 sep = "_MOD_";
22922 }
987504bb
JJ
22923 else
22924 sep = "::";
63d06c5c 22925
6dd47d34
DE
22926 if (prefix == NULL)
22927 prefix = "";
22928 if (suffix == NULL)
22929 suffix = "";
22930
987504bb
JJ
22931 if (obs == NULL)
22932 {
3e43a32a 22933 char *retval
224c3ddb
SM
22934 = ((char *)
22935 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22936
f55ee35c
JK
22937 strcpy (retval, lead);
22938 strcat (retval, prefix);
6dd47d34
DE
22939 strcat (retval, sep);
22940 strcat (retval, suffix);
63d06c5c
DC
22941 return retval;
22942 }
987504bb
JJ
22943 else
22944 {
22945 /* We have an obstack. */
f55ee35c 22946 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22947 }
63d06c5c
DC
22948}
22949
c906108c
SS
22950/* Return sibling of die, NULL if no sibling. */
22951
f9aca02d 22952static struct die_info *
fba45db2 22953sibling_die (struct die_info *die)
c906108c 22954{
639d11d3 22955 return die->sibling;
c906108c
SS
22956}
22957
71c25dea
TT
22958/* Get name of a die, return NULL if not found. */
22959
15d034d0
TT
22960static const char *
22961dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22962 struct obstack *obstack)
22963{
22964 if (name && cu->language == language_cplus)
22965 {
2f408ecb 22966 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22967
2f408ecb 22968 if (!canon_name.empty ())
71c25dea 22969 {
2f408ecb 22970 if (canon_name != name)
efba19b0 22971 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22972 }
22973 }
22974
22975 return name;
c906108c
SS
22976}
22977
96553a0c
DE
22978/* Get name of a die, return NULL if not found.
22979 Anonymous namespaces are converted to their magic string. */
9219021c 22980
15d034d0 22981static const char *
e142c38c 22982dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22983{
22984 struct attribute *attr;
518817b3 22985 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22986
e142c38c 22987 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22988 if ((!attr || !DW_STRING (attr))
96553a0c 22989 && die->tag != DW_TAG_namespace
53832f31
TT
22990 && die->tag != DW_TAG_class_type
22991 && die->tag != DW_TAG_interface_type
22992 && die->tag != DW_TAG_structure_type
22993 && die->tag != DW_TAG_union_type)
71c25dea
TT
22994 return NULL;
22995
22996 switch (die->tag)
22997 {
22998 case DW_TAG_compile_unit:
95554aad 22999 case DW_TAG_partial_unit:
71c25dea
TT
23000 /* Compilation units have a DW_AT_name that is a filename, not
23001 a source language identifier. */
23002 case DW_TAG_enumeration_type:
23003 case DW_TAG_enumerator:
23004 /* These tags always have simple identifiers already; no need
23005 to canonicalize them. */
23006 return DW_STRING (attr);
907af001 23007
96553a0c
DE
23008 case DW_TAG_namespace:
23009 if (attr != NULL && DW_STRING (attr) != NULL)
23010 return DW_STRING (attr);
23011 return CP_ANONYMOUS_NAMESPACE_STR;
23012
907af001
UW
23013 case DW_TAG_class_type:
23014 case DW_TAG_interface_type:
23015 case DW_TAG_structure_type:
23016 case DW_TAG_union_type:
23017 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23018 structures or unions. These were of the form "._%d" in GCC 4.1,
23019 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23020 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23021 if (attr && DW_STRING (attr)
61012eef
GB
23022 && (startswith (DW_STRING (attr), "._")
23023 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23024 return NULL;
53832f31
TT
23025
23026 /* GCC might emit a nameless typedef that has a linkage name. See
23027 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23028 if (!attr || DW_STRING (attr) == NULL)
23029 {
df5c6c50 23030 char *demangled = NULL;
53832f31 23031
73b9be8b 23032 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23033 if (attr == NULL || DW_STRING (attr) == NULL)
23034 return NULL;
23035
df5c6c50
JK
23036 /* Avoid demangling DW_STRING (attr) the second time on a second
23037 call for the same DIE. */
23038 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 23039 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
23040
23041 if (demangled)
23042 {
e6a959d6 23043 const char *base;
96408a79 23044
53832f31 23045 /* FIXME: we already did this for the partial symbol... */
34a68019 23046 DW_STRING (attr)
021887d8
TT
23047 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23048 demangled);
53832f31
TT
23049 DW_STRING_IS_CANONICAL (attr) = 1;
23050 xfree (demangled);
96408a79
SA
23051
23052 /* Strip any leading namespaces/classes, keep only the base name.
23053 DW_AT_name for named DIEs does not contain the prefixes. */
23054 base = strrchr (DW_STRING (attr), ':');
23055 if (base && base > DW_STRING (attr) && base[-1] == ':')
23056 return &base[1];
23057 else
23058 return DW_STRING (attr);
53832f31
TT
23059 }
23060 }
907af001
UW
23061 break;
23062
71c25dea 23063 default:
907af001
UW
23064 break;
23065 }
23066
23067 if (!DW_STRING_IS_CANONICAL (attr))
23068 {
23069 DW_STRING (attr)
23070 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23071 &objfile->per_bfd->storage_obstack);
907af001 23072 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23073 }
907af001 23074 return DW_STRING (attr);
9219021c
DC
23075}
23076
23077/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23078 is none. *EXT_CU is the CU containing DIE on input, and the CU
23079 containing the return value on output. */
9219021c
DC
23080
23081static struct die_info *
f2f0e013 23082dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23083{
23084 struct attribute *attr;
9219021c 23085
f2f0e013 23086 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23087 if (attr == NULL)
23088 return NULL;
23089
f2f0e013 23090 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23091}
23092
fa9c3fa0
TT
23093/* A convenience function that returns an "unknown" DWARF name,
23094 including the value of V. STR is the name of the entity being
23095 printed, e.g., "TAG". */
23096
23097static const char *
23098dwarf_unknown (const char *str, unsigned v)
23099{
23100 char *cell = get_print_cell ();
23101 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23102 return cell;
23103}
23104
c906108c
SS
23105/* Convert a DIE tag into its string name. */
23106
f39c6ffd 23107static const char *
aa1ee363 23108dwarf_tag_name (unsigned tag)
c906108c 23109{
f39c6ffd
TT
23110 const char *name = get_DW_TAG_name (tag);
23111
23112 if (name == NULL)
fa9c3fa0 23113 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23114
23115 return name;
c906108c
SS
23116}
23117
23118/* Convert a DWARF attribute code into its string name. */
23119
f39c6ffd 23120static const char *
aa1ee363 23121dwarf_attr_name (unsigned attr)
c906108c 23122{
f39c6ffd
TT
23123 const char *name;
23124
c764a876 23125#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23126 if (attr == DW_AT_MIPS_fde)
23127 return "DW_AT_MIPS_fde";
23128#else
23129 if (attr == DW_AT_HP_block_index)
23130 return "DW_AT_HP_block_index";
c764a876 23131#endif
f39c6ffd
TT
23132
23133 name = get_DW_AT_name (attr);
23134
23135 if (name == NULL)
fa9c3fa0 23136 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23137
23138 return name;
c906108c
SS
23139}
23140
a084a2a6
AT
23141/* Convert a unit type to corresponding DW_UT name. */
23142
23143static const char *
23144dwarf_unit_type_name (int unit_type) {
23145 switch (unit_type)
23146 {
23147 case 0x01:
23148 return "DW_UT_compile (0x01)";
23149 case 0x02:
23150 return "DW_UT_type (0x02)";
23151 case 0x03:
23152 return "DW_UT_partial (0x03)";
23153 case 0x04:
23154 return "DW_UT_skeleton (0x04)";
23155 case 0x05:
23156 return "DW_UT_split_compile (0x05)";
23157 case 0x06:
23158 return "DW_UT_split_type (0x06)";
23159 case 0x80:
23160 return "DW_UT_lo_user (0x80)";
23161 case 0xff:
23162 return "DW_UT_hi_user (0xff)";
23163 default:
23164 return nullptr;
23165 }
23166}
23167
c906108c
SS
23168/* Convert a DWARF value form code into its string name. */
23169
f39c6ffd 23170static const char *
aa1ee363 23171dwarf_form_name (unsigned form)
c906108c 23172{
f39c6ffd
TT
23173 const char *name = get_DW_FORM_name (form);
23174
23175 if (name == NULL)
fa9c3fa0 23176 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23177
23178 return name;
c906108c
SS
23179}
23180
a121b7c1 23181static const char *
fba45db2 23182dwarf_bool_name (unsigned mybool)
c906108c
SS
23183{
23184 if (mybool)
23185 return "TRUE";
23186 else
23187 return "FALSE";
23188}
23189
23190/* Convert a DWARF type code into its string name. */
23191
f39c6ffd 23192static const char *
aa1ee363 23193dwarf_type_encoding_name (unsigned enc)
c906108c 23194{
f39c6ffd 23195 const char *name = get_DW_ATE_name (enc);
c906108c 23196
f39c6ffd 23197 if (name == NULL)
fa9c3fa0 23198 return dwarf_unknown ("ATE", enc);
c906108c 23199
f39c6ffd 23200 return name;
c906108c 23201}
c906108c 23202
f9aca02d 23203static void
d97bc12b 23204dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23205{
23206 unsigned int i;
23207
d97bc12b 23208 print_spaces (indent, f);
9d8780f0 23209 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23210 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23211 sect_offset_str (die->sect_off));
d97bc12b
DE
23212
23213 if (die->parent != NULL)
23214 {
23215 print_spaces (indent, f);
9d8780f0
SM
23216 fprintf_unfiltered (f, " parent at offset: %s\n",
23217 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23218 }
23219
23220 print_spaces (indent, f);
23221 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23222 dwarf_bool_name (die->child != NULL));
c906108c 23223
d97bc12b
DE
23224 print_spaces (indent, f);
23225 fprintf_unfiltered (f, " attributes:\n");
23226
c906108c
SS
23227 for (i = 0; i < die->num_attrs; ++i)
23228 {
d97bc12b
DE
23229 print_spaces (indent, f);
23230 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23231 dwarf_attr_name (die->attrs[i].name),
23232 dwarf_form_name (die->attrs[i].form));
d97bc12b 23233
c906108c
SS
23234 switch (die->attrs[i].form)
23235 {
c906108c 23236 case DW_FORM_addr:
336d760d 23237 case DW_FORM_addrx:
3019eac3 23238 case DW_FORM_GNU_addr_index:
d97bc12b 23239 fprintf_unfiltered (f, "address: ");
5af949e3 23240 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23241 break;
23242 case DW_FORM_block2:
23243 case DW_FORM_block4:
23244 case DW_FORM_block:
23245 case DW_FORM_block1:
56eb65bd
SP
23246 fprintf_unfiltered (f, "block: size %s",
23247 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23248 break;
2dc7f7b3 23249 case DW_FORM_exprloc:
56eb65bd
SP
23250 fprintf_unfiltered (f, "expression: size %s",
23251 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23252 break;
0224619f
JK
23253 case DW_FORM_data16:
23254 fprintf_unfiltered (f, "constant of 16 bytes");
23255 break;
4568ecf9
DE
23256 case DW_FORM_ref_addr:
23257 fprintf_unfiltered (f, "ref address: ");
23258 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23259 break;
36586728
TT
23260 case DW_FORM_GNU_ref_alt:
23261 fprintf_unfiltered (f, "alt ref address: ");
23262 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23263 break;
10b3939b
DJ
23264 case DW_FORM_ref1:
23265 case DW_FORM_ref2:
23266 case DW_FORM_ref4:
4568ecf9
DE
23267 case DW_FORM_ref8:
23268 case DW_FORM_ref_udata:
d97bc12b 23269 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23270 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23271 break;
c906108c
SS
23272 case DW_FORM_data1:
23273 case DW_FORM_data2:
23274 case DW_FORM_data4:
ce5d95e1 23275 case DW_FORM_data8:
c906108c
SS
23276 case DW_FORM_udata:
23277 case DW_FORM_sdata:
43bbcdc2
PH
23278 fprintf_unfiltered (f, "constant: %s",
23279 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23280 break;
2dc7f7b3
TT
23281 case DW_FORM_sec_offset:
23282 fprintf_unfiltered (f, "section offset: %s",
23283 pulongest (DW_UNSND (&die->attrs[i])));
23284 break;
55f1336d 23285 case DW_FORM_ref_sig8:
ac9ec31b
DE
23286 fprintf_unfiltered (f, "signature: %s",
23287 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23288 break;
c906108c 23289 case DW_FORM_string:
4bdf3d34 23290 case DW_FORM_strp:
43988095 23291 case DW_FORM_line_strp:
cf532bd1 23292 case DW_FORM_strx:
3019eac3 23293 case DW_FORM_GNU_str_index:
36586728 23294 case DW_FORM_GNU_strp_alt:
8285870a 23295 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23296 DW_STRING (&die->attrs[i])
8285870a
JK
23297 ? DW_STRING (&die->attrs[i]) : "",
23298 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23299 break;
23300 case DW_FORM_flag:
23301 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23302 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23303 else
d97bc12b 23304 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23305 break;
2dc7f7b3
TT
23306 case DW_FORM_flag_present:
23307 fprintf_unfiltered (f, "flag: TRUE");
23308 break;
a8329558 23309 case DW_FORM_indirect:
0963b4bd
MS
23310 /* The reader will have reduced the indirect form to
23311 the "base form" so this form should not occur. */
5f48f8f3 23312 fprintf_unfiltered (f,
3e43a32a 23313 "unexpected attribute form: DW_FORM_indirect");
a8329558 23314 break;
663c44ac
JK
23315 case DW_FORM_implicit_const:
23316 fprintf_unfiltered (f, "constant: %s",
23317 plongest (DW_SND (&die->attrs[i])));
23318 break;
c906108c 23319 default:
d97bc12b 23320 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23321 die->attrs[i].form);
d97bc12b 23322 break;
c906108c 23323 }
d97bc12b 23324 fprintf_unfiltered (f, "\n");
c906108c
SS
23325 }
23326}
23327
f9aca02d 23328static void
d97bc12b 23329dump_die_for_error (struct die_info *die)
c906108c 23330{
d97bc12b
DE
23331 dump_die_shallow (gdb_stderr, 0, die);
23332}
23333
23334static void
23335dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23336{
23337 int indent = level * 4;
23338
23339 gdb_assert (die != NULL);
23340
23341 if (level >= max_level)
23342 return;
23343
23344 dump_die_shallow (f, indent, die);
23345
23346 if (die->child != NULL)
c906108c 23347 {
d97bc12b
DE
23348 print_spaces (indent, f);
23349 fprintf_unfiltered (f, " Children:");
23350 if (level + 1 < max_level)
23351 {
23352 fprintf_unfiltered (f, "\n");
23353 dump_die_1 (f, level + 1, max_level, die->child);
23354 }
23355 else
23356 {
3e43a32a
MS
23357 fprintf_unfiltered (f,
23358 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23359 }
23360 }
23361
23362 if (die->sibling != NULL && level > 0)
23363 {
23364 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23365 }
23366}
23367
d97bc12b
DE
23368/* This is called from the pdie macro in gdbinit.in.
23369 It's not static so gcc will keep a copy callable from gdb. */
23370
23371void
23372dump_die (struct die_info *die, int max_level)
23373{
23374 dump_die_1 (gdb_stdlog, 0, max_level, die);
23375}
23376
f9aca02d 23377static void
51545339 23378store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23379{
51545339 23380 void **slot;
c906108c 23381
9c541725
PA
23382 slot = htab_find_slot_with_hash (cu->die_hash, die,
23383 to_underlying (die->sect_off),
b64f50a1 23384 INSERT);
51545339
DJ
23385
23386 *slot = die;
c906108c
SS
23387}
23388
b64f50a1
JK
23389/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23390 required kind. */
23391
23392static sect_offset
ff39bb5e 23393dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23394{
7771576e 23395 if (attr_form_is_ref (attr))
9c541725 23396 return (sect_offset) DW_UNSND (attr);
93311388 23397
b98664d3 23398 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23399 dwarf_form_name (attr->form));
9c541725 23400 return {};
c906108c
SS
23401}
23402
43bbcdc2
PH
23403/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23404 * the value held by the attribute is not constant. */
a02abb62 23405
43bbcdc2 23406static LONGEST
ff39bb5e 23407dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23408{
663c44ac 23409 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23410 return DW_SND (attr);
23411 else if (attr->form == DW_FORM_udata
23412 || attr->form == DW_FORM_data1
23413 || attr->form == DW_FORM_data2
23414 || attr->form == DW_FORM_data4
23415 || attr->form == DW_FORM_data8)
23416 return DW_UNSND (attr);
23417 else
23418 {
0224619f 23419 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23420 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23421 dwarf_form_name (attr->form));
23422 return default_value;
23423 }
23424}
23425
348e048f
DE
23426/* Follow reference or signature attribute ATTR of SRC_DIE.
23427 On entry *REF_CU is the CU of SRC_DIE.
23428 On exit *REF_CU is the CU of the result. */
23429
23430static struct die_info *
ff39bb5e 23431follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23432 struct dwarf2_cu **ref_cu)
23433{
23434 struct die_info *die;
23435
7771576e 23436 if (attr_form_is_ref (attr))
348e048f 23437 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23438 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23439 die = follow_die_sig (src_die, attr, ref_cu);
23440 else
23441 {
23442 dump_die_for_error (src_die);
23443 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23444 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23445 }
23446
23447 return die;
03dd20cc
DJ
23448}
23449
5c631832 23450/* Follow reference OFFSET.
673bfd45
DE
23451 On entry *REF_CU is the CU of the source die referencing OFFSET.
23452 On exit *REF_CU is the CU of the result.
23453 Returns NULL if OFFSET is invalid. */
f504f079 23454
f9aca02d 23455static struct die_info *
9c541725 23456follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23457 struct dwarf2_cu **ref_cu)
c906108c 23458{
10b3939b 23459 struct die_info temp_die;
f2f0e013 23460 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23461 struct dwarf2_per_objfile *dwarf2_per_objfile
23462 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23463
348e048f
DE
23464 gdb_assert (cu->per_cu != NULL);
23465
98bfdba5
PA
23466 target_cu = cu;
23467
3019eac3 23468 if (cu->per_cu->is_debug_types)
348e048f
DE
23469 {
23470 /* .debug_types CUs cannot reference anything outside their CU.
23471 If they need to, they have to reference a signatured type via
55f1336d 23472 DW_FORM_ref_sig8. */
9c541725 23473 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23474 return NULL;
348e048f 23475 }
36586728 23476 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23477 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23478 {
23479 struct dwarf2_per_cu_data *per_cu;
9a619af0 23480
9c541725 23481 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23482 dwarf2_per_objfile);
03dd20cc
DJ
23483
23484 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23485 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23486 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23487
10b3939b
DJ
23488 target_cu = per_cu->cu;
23489 }
98bfdba5
PA
23490 else if (cu->dies == NULL)
23491 {
23492 /* We're loading full DIEs during partial symbol reading. */
23493 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23494 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23495 }
c906108c 23496
f2f0e013 23497 *ref_cu = target_cu;
9c541725 23498 temp_die.sect_off = sect_off;
c24bdb02
KS
23499
23500 if (target_cu != cu)
23501 target_cu->ancestor = cu;
23502
9a3c8263 23503 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23504 &temp_die,
23505 to_underlying (sect_off));
5c631832 23506}
10b3939b 23507
5c631832
JK
23508/* Follow reference attribute ATTR of SRC_DIE.
23509 On entry *REF_CU is the CU of SRC_DIE.
23510 On exit *REF_CU is the CU of the result. */
23511
23512static struct die_info *
ff39bb5e 23513follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23514 struct dwarf2_cu **ref_cu)
23515{
9c541725 23516 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23517 struct dwarf2_cu *cu = *ref_cu;
23518 struct die_info *die;
23519
9c541725 23520 die = follow_die_offset (sect_off,
36586728
TT
23521 (attr->form == DW_FORM_GNU_ref_alt
23522 || cu->per_cu->is_dwz),
23523 ref_cu);
5c631832 23524 if (!die)
9d8780f0
SM
23525 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23526 "at %s [in module %s]"),
23527 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23528 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23529
5c631832
JK
23530 return die;
23531}
23532
9c541725 23533/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23534 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23535 dwarf2_locexpr_baton->data has lifetime of
23536 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23537
23538struct dwarf2_locexpr_baton
9c541725 23539dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23540 struct dwarf2_per_cu_data *per_cu,
23541 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23542 void *baton, bool resolve_abstract_p)
5c631832 23543{
918dd910 23544 struct dwarf2_cu *cu;
5c631832
JK
23545 struct die_info *die;
23546 struct attribute *attr;
23547 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23548 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23549 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23550
918dd910 23551 if (per_cu->cu == NULL)
58f0c718 23552 load_cu (per_cu, false);
918dd910 23553 cu = per_cu->cu;
cc12ce38
DE
23554 if (cu == NULL)
23555 {
23556 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23557 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23558 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23559 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23560 }
918dd910 23561
9c541725 23562 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23563 if (!die)
9d8780f0
SM
23564 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23565 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23566
23567 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23568 if (!attr && resolve_abstract_p
3360b6e7 23569 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23570 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23571 {
23572 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23573 CORE_ADDR baseaddr
23574 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23576
3360b6e7
TV
23577 for (const auto &cand_off
23578 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23579 {
3360b6e7
TV
23580 struct dwarf2_cu *cand_cu = cu;
23581 struct die_info *cand
23582 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23583 if (!cand
23584 || !cand->parent
e4a62c65
TV
23585 || cand->parent->tag != DW_TAG_subprogram)
23586 continue;
23587
23588 CORE_ADDR pc_low, pc_high;
23589 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23590 if (pc_low == ((CORE_ADDR) -1))
23591 continue;
23592 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23593 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23594 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23595 continue;
23596
23597 die = cand;
23598 attr = dwarf2_attr (die, DW_AT_location, cu);
23599 break;
23600 }
23601 }
23602
5c631832
JK
23603 if (!attr)
23604 {
e103e986
JK
23605 /* DWARF: "If there is no such attribute, then there is no effect.".
23606 DATA is ignored if SIZE is 0. */
5c631832 23607
e103e986 23608 retval.data = NULL;
5c631832
JK
23609 retval.size = 0;
23610 }
8cf6f0b1
TT
23611 else if (attr_form_is_section_offset (attr))
23612 {
23613 struct dwarf2_loclist_baton loclist_baton;
23614 CORE_ADDR pc = (*get_frame_pc) (baton);
23615 size_t size;
23616
23617 fill_in_loclist_baton (cu, &loclist_baton, attr);
23618
23619 retval.data = dwarf2_find_location_expression (&loclist_baton,
23620 &size, pc);
23621 retval.size = size;
23622 }
5c631832
JK
23623 else
23624 {
23625 if (!attr_form_is_block (attr))
9d8780f0 23626 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23627 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23628 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23629
23630 retval.data = DW_BLOCK (attr)->data;
23631 retval.size = DW_BLOCK (attr)->size;
23632 }
23633 retval.per_cu = cu->per_cu;
918dd910 23634
ed2dc618 23635 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23636
5c631832 23637 return retval;
348e048f
DE
23638}
23639
8b9737bf
TT
23640/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23641 offset. */
23642
23643struct dwarf2_locexpr_baton
23644dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23645 struct dwarf2_per_cu_data *per_cu,
23646 CORE_ADDR (*get_frame_pc) (void *baton),
23647 void *baton)
23648{
9c541725 23649 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23650
9c541725 23651 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23652}
23653
b6807d98
TT
23654/* Write a constant of a given type as target-ordered bytes into
23655 OBSTACK. */
23656
23657static const gdb_byte *
23658write_constant_as_bytes (struct obstack *obstack,
23659 enum bfd_endian byte_order,
23660 struct type *type,
23661 ULONGEST value,
23662 LONGEST *len)
23663{
23664 gdb_byte *result;
23665
23666 *len = TYPE_LENGTH (type);
224c3ddb 23667 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23668 store_unsigned_integer (result, *len, byte_order, value);
23669
23670 return result;
23671}
23672
23673/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23674 pointer to the constant bytes and set LEN to the length of the
23675 data. If memory is needed, allocate it on OBSTACK. If the DIE
23676 does not have a DW_AT_const_value, return NULL. */
23677
23678const gdb_byte *
9c541725 23679dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23680 struct dwarf2_per_cu_data *per_cu,
23681 struct obstack *obstack,
23682 LONGEST *len)
23683{
23684 struct dwarf2_cu *cu;
23685 struct die_info *die;
23686 struct attribute *attr;
23687 const gdb_byte *result = NULL;
23688 struct type *type;
23689 LONGEST value;
23690 enum bfd_endian byte_order;
e3b94546 23691 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23692
b6807d98 23693 if (per_cu->cu == NULL)
58f0c718 23694 load_cu (per_cu, false);
b6807d98 23695 cu = per_cu->cu;
cc12ce38
DE
23696 if (cu == NULL)
23697 {
23698 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23699 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23700 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23701 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23702 }
b6807d98 23703
9c541725 23704 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23705 if (!die)
9d8780f0
SM
23706 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23707 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23708
23709 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23710 if (attr == NULL)
23711 return NULL;
23712
e3b94546 23713 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23714 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23715
23716 switch (attr->form)
23717 {
23718 case DW_FORM_addr:
336d760d 23719 case DW_FORM_addrx:
b6807d98
TT
23720 case DW_FORM_GNU_addr_index:
23721 {
23722 gdb_byte *tem;
23723
23724 *len = cu->header.addr_size;
224c3ddb 23725 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23726 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23727 result = tem;
23728 }
23729 break;
23730 case DW_FORM_string:
23731 case DW_FORM_strp:
cf532bd1 23732 case DW_FORM_strx:
b6807d98
TT
23733 case DW_FORM_GNU_str_index:
23734 case DW_FORM_GNU_strp_alt:
23735 /* DW_STRING is already allocated on the objfile obstack, point
23736 directly to it. */
23737 result = (const gdb_byte *) DW_STRING (attr);
23738 *len = strlen (DW_STRING (attr));
23739 break;
23740 case DW_FORM_block1:
23741 case DW_FORM_block2:
23742 case DW_FORM_block4:
23743 case DW_FORM_block:
23744 case DW_FORM_exprloc:
0224619f 23745 case DW_FORM_data16:
b6807d98
TT
23746 result = DW_BLOCK (attr)->data;
23747 *len = DW_BLOCK (attr)->size;
23748 break;
23749
23750 /* The DW_AT_const_value attributes are supposed to carry the
23751 symbol's value "represented as it would be on the target
23752 architecture." By the time we get here, it's already been
23753 converted to host endianness, so we just need to sign- or
23754 zero-extend it as appropriate. */
23755 case DW_FORM_data1:
23756 type = die_type (die, cu);
23757 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23758 if (result == NULL)
23759 result = write_constant_as_bytes (obstack, byte_order,
23760 type, value, len);
23761 break;
23762 case DW_FORM_data2:
23763 type = die_type (die, cu);
23764 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23765 if (result == NULL)
23766 result = write_constant_as_bytes (obstack, byte_order,
23767 type, value, len);
23768 break;
23769 case DW_FORM_data4:
23770 type = die_type (die, cu);
23771 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23772 if (result == NULL)
23773 result = write_constant_as_bytes (obstack, byte_order,
23774 type, value, len);
23775 break;
23776 case DW_FORM_data8:
23777 type = die_type (die, cu);
23778 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23779 if (result == NULL)
23780 result = write_constant_as_bytes (obstack, byte_order,
23781 type, value, len);
23782 break;
23783
23784 case DW_FORM_sdata:
663c44ac 23785 case DW_FORM_implicit_const:
b6807d98
TT
23786 type = die_type (die, cu);
23787 result = write_constant_as_bytes (obstack, byte_order,
23788 type, DW_SND (attr), len);
23789 break;
23790
23791 case DW_FORM_udata:
23792 type = die_type (die, cu);
23793 result = write_constant_as_bytes (obstack, byte_order,
23794 type, DW_UNSND (attr), len);
23795 break;
23796
23797 default:
b98664d3 23798 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23799 dwarf_form_name (attr->form));
23800 break;
23801 }
23802
23803 return result;
23804}
23805
7942e96e
AA
23806/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23807 valid type for this die is found. */
23808
23809struct type *
9c541725 23810dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23811 struct dwarf2_per_cu_data *per_cu)
23812{
23813 struct dwarf2_cu *cu;
23814 struct die_info *die;
23815
7942e96e 23816 if (per_cu->cu == NULL)
58f0c718 23817 load_cu (per_cu, false);
7942e96e
AA
23818 cu = per_cu->cu;
23819 if (!cu)
23820 return NULL;
23821
9c541725 23822 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23823 if (!die)
23824 return NULL;
23825
23826 return die_type (die, cu);
23827}
23828
8a9b8146
TT
23829/* Return the type of the DIE at DIE_OFFSET in the CU named by
23830 PER_CU. */
23831
23832struct type *
b64f50a1 23833dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23834 struct dwarf2_per_cu_data *per_cu)
23835{
9c541725 23836 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23837 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23838}
23839
ac9ec31b 23840/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23841 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23842 On exit *REF_CU is the CU of the result.
23843 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23844
23845static struct die_info *
ac9ec31b
DE
23846follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23847 struct dwarf2_cu **ref_cu)
348e048f 23848{
348e048f 23849 struct die_info temp_die;
c24bdb02 23850 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23851 struct die_info *die;
23852
ac9ec31b
DE
23853 /* While it might be nice to assert sig_type->type == NULL here,
23854 we can get here for DW_AT_imported_declaration where we need
23855 the DIE not the type. */
348e048f
DE
23856
23857 /* If necessary, add it to the queue and load its DIEs. */
23858
95554aad 23859 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23860 read_signatured_type (sig_type);
348e048f 23861
348e048f 23862 sig_cu = sig_type->per_cu.cu;
69d751e3 23863 gdb_assert (sig_cu != NULL);
9c541725
PA
23864 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23865 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23866 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23867 to_underlying (temp_die.sect_off));
348e048f
DE
23868 if (die)
23869 {
ed2dc618 23870 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23871 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23872
796a7ff8
DE
23873 /* For .gdb_index version 7 keep track of included TUs.
23874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23875 if (dwarf2_per_objfile->index_table != NULL
23876 && dwarf2_per_objfile->index_table->version <= 7)
23877 {
ae640021 23878 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23879 }
23880
348e048f 23881 *ref_cu = sig_cu;
c24bdb02
KS
23882 if (sig_cu != cu)
23883 sig_cu->ancestor = cu;
23884
348e048f
DE
23885 return die;
23886 }
23887
ac9ec31b
DE
23888 return NULL;
23889}
23890
23891/* Follow signatured type referenced by ATTR in SRC_DIE.
23892 On entry *REF_CU is the CU of SRC_DIE.
23893 On exit *REF_CU is the CU of the result.
23894 The result is the DIE of the type.
23895 If the referenced type cannot be found an error is thrown. */
23896
23897static struct die_info *
ff39bb5e 23898follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23899 struct dwarf2_cu **ref_cu)
23900{
23901 ULONGEST signature = DW_SIGNATURE (attr);
23902 struct signatured_type *sig_type;
23903 struct die_info *die;
23904
23905 gdb_assert (attr->form == DW_FORM_ref_sig8);
23906
a2ce51a0 23907 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23908 /* sig_type will be NULL if the signatured type is missing from
23909 the debug info. */
23910 if (sig_type == NULL)
23911 {
23912 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23913 " from DIE at %s [in module %s]"),
23914 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23915 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23916 }
23917
23918 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23919 if (die == NULL)
23920 {
23921 dump_die_for_error (src_die);
23922 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23923 " from DIE at %s [in module %s]"),
23924 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23925 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23926 }
23927
23928 return die;
23929}
23930
23931/* Get the type specified by SIGNATURE referenced in DIE/CU,
23932 reading in and processing the type unit if necessary. */
23933
23934static struct type *
23935get_signatured_type (struct die_info *die, ULONGEST signature,
23936 struct dwarf2_cu *cu)
23937{
518817b3
SM
23938 struct dwarf2_per_objfile *dwarf2_per_objfile
23939 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23940 struct signatured_type *sig_type;
23941 struct dwarf2_cu *type_cu;
23942 struct die_info *type_die;
23943 struct type *type;
23944
a2ce51a0 23945 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23946 /* sig_type will be NULL if the signatured type is missing from
23947 the debug info. */
23948 if (sig_type == NULL)
23949 {
b98664d3 23950 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23951 " from DIE at %s [in module %s]"),
23952 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23953 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23954 return build_error_marker_type (cu, die);
23955 }
23956
23957 /* If we already know the type we're done. */
23958 if (sig_type->type != NULL)
23959 return sig_type->type;
23960
23961 type_cu = cu;
23962 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23963 if (type_die != NULL)
23964 {
23965 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23966 is created. This is important, for example, because for c++ classes
23967 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23968 type = read_type_die (type_die, type_cu);
23969 if (type == NULL)
23970 {
b98664d3 23971 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23972 " referenced from DIE at %s [in module %s]"),
23973 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23974 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23975 type = build_error_marker_type (cu, die);
23976 }
23977 }
23978 else
23979 {
b98664d3 23980 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23981 " from DIE at %s [in module %s]"),
23982 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23983 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23984 type = build_error_marker_type (cu, die);
23985 }
23986 sig_type->type = type;
23987
23988 return type;
23989}
23990
23991/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23992 reading in and processing the type unit if necessary. */
23993
23994static struct type *
ff39bb5e 23995get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23996 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23997{
23998 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23999 if (attr_form_is_ref (attr))
ac9ec31b
DE
24000 {
24001 struct dwarf2_cu *type_cu = cu;
24002 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24003
24004 return read_type_die (type_die, type_cu);
24005 }
24006 else if (attr->form == DW_FORM_ref_sig8)
24007 {
24008 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24009 }
24010 else
24011 {
518817b3
SM
24012 struct dwarf2_per_objfile *dwarf2_per_objfile
24013 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24014
b98664d3 24015 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24016 " at %s [in module %s]"),
24017 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24018 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24019 return build_error_marker_type (cu, die);
24020 }
348e048f
DE
24021}
24022
e5fe5e75 24023/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24024
24025static void
e5fe5e75 24026load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24027{
52dc124a 24028 struct signatured_type *sig_type;
348e048f 24029
f4dc4d17
DE
24030 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24031 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24032
6721b2ec
DE
24033 /* We have the per_cu, but we need the signatured_type.
24034 Fortunately this is an easy translation. */
24035 gdb_assert (per_cu->is_debug_types);
24036 sig_type = (struct signatured_type *) per_cu;
348e048f 24037
6721b2ec 24038 gdb_assert (per_cu->cu == NULL);
348e048f 24039
52dc124a 24040 read_signatured_type (sig_type);
348e048f 24041
6721b2ec 24042 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24043}
24044
dee91e82
DE
24045/* die_reader_func for read_signatured_type.
24046 This is identical to load_full_comp_unit_reader,
24047 but is kept separate for now. */
348e048f
DE
24048
24049static void
dee91e82 24050read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 24051 const gdb_byte *info_ptr,
dee91e82
DE
24052 struct die_info *comp_unit_die,
24053 int has_children,
24054 void *data)
348e048f 24055{
dee91e82 24056 struct dwarf2_cu *cu = reader->cu;
348e048f 24057
dee91e82
DE
24058 gdb_assert (cu->die_hash == NULL);
24059 cu->die_hash =
24060 htab_create_alloc_ex (cu->header.length / 12,
24061 die_hash,
24062 die_eq,
24063 NULL,
24064 &cu->comp_unit_obstack,
24065 hashtab_obstack_allocate,
24066 dummy_obstack_deallocate);
348e048f 24067
dee91e82
DE
24068 if (has_children)
24069 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24070 &info_ptr, comp_unit_die);
24071 cu->dies = comp_unit_die;
24072 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
24073
24074 /* We try not to read any attributes in this function, because not
9cdd5dbd 24075 all CUs needed for references have been loaded yet, and symbol
348e048f 24076 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
24077 or we won't be able to build types correctly.
24078 Similarly, if we do not read the producer, we can not apply
24079 producer-specific interpretation. */
95554aad 24080 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 24081}
348e048f 24082
3019eac3
DE
24083/* Read in a signatured type and build its CU and DIEs.
24084 If the type is a stub for the real type in a DWO file,
24085 read in the real type from the DWO file as well. */
dee91e82
DE
24086
24087static void
24088read_signatured_type (struct signatured_type *sig_type)
24089{
24090 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24091
3019eac3 24092 gdb_assert (per_cu->is_debug_types);
dee91e82 24093 gdb_assert (per_cu->cu == NULL);
348e048f 24094
58f0c718 24095 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 24096 read_signatured_type_reader, NULL);
7ee85ab1 24097 sig_type->per_cu.tu_read = 1;
c906108c
SS
24098}
24099
c906108c
SS
24100/* Decode simple location descriptions.
24101 Given a pointer to a dwarf block that defines a location, compute
24102 the location and return the value.
24103
4cecd739
DJ
24104 NOTE drow/2003-11-18: This function is called in two situations
24105 now: for the address of static or global variables (partial symbols
24106 only) and for offsets into structures which are expected to be
24107 (more or less) constant. The partial symbol case should go away,
24108 and only the constant case should remain. That will let this
24109 function complain more accurately. A few special modes are allowed
24110 without complaint for global variables (for instance, global
24111 register values and thread-local values).
c906108c
SS
24112
24113 A location description containing no operations indicates that the
4cecd739 24114 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24115 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24116 callers will only want a very basic result and this can become a
21ae7a4d
JK
24117 complaint.
24118
24119 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24120
24121static CORE_ADDR
e7c27a73 24122decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24123{
518817b3 24124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24125 size_t i;
24126 size_t size = blk->size;
d521ce57 24127 const gdb_byte *data = blk->data;
21ae7a4d
JK
24128 CORE_ADDR stack[64];
24129 int stacki;
24130 unsigned int bytes_read, unsnd;
24131 gdb_byte op;
c906108c 24132
21ae7a4d
JK
24133 i = 0;
24134 stacki = 0;
24135 stack[stacki] = 0;
24136 stack[++stacki] = 0;
24137
24138 while (i < size)
24139 {
24140 op = data[i++];
24141 switch (op)
24142 {
24143 case DW_OP_lit0:
24144 case DW_OP_lit1:
24145 case DW_OP_lit2:
24146 case DW_OP_lit3:
24147 case DW_OP_lit4:
24148 case DW_OP_lit5:
24149 case DW_OP_lit6:
24150 case DW_OP_lit7:
24151 case DW_OP_lit8:
24152 case DW_OP_lit9:
24153 case DW_OP_lit10:
24154 case DW_OP_lit11:
24155 case DW_OP_lit12:
24156 case DW_OP_lit13:
24157 case DW_OP_lit14:
24158 case DW_OP_lit15:
24159 case DW_OP_lit16:
24160 case DW_OP_lit17:
24161 case DW_OP_lit18:
24162 case DW_OP_lit19:
24163 case DW_OP_lit20:
24164 case DW_OP_lit21:
24165 case DW_OP_lit22:
24166 case DW_OP_lit23:
24167 case DW_OP_lit24:
24168 case DW_OP_lit25:
24169 case DW_OP_lit26:
24170 case DW_OP_lit27:
24171 case DW_OP_lit28:
24172 case DW_OP_lit29:
24173 case DW_OP_lit30:
24174 case DW_OP_lit31:
24175 stack[++stacki] = op - DW_OP_lit0;
24176 break;
f1bea926 24177
21ae7a4d
JK
24178 case DW_OP_reg0:
24179 case DW_OP_reg1:
24180 case DW_OP_reg2:
24181 case DW_OP_reg3:
24182 case DW_OP_reg4:
24183 case DW_OP_reg5:
24184 case DW_OP_reg6:
24185 case DW_OP_reg7:
24186 case DW_OP_reg8:
24187 case DW_OP_reg9:
24188 case DW_OP_reg10:
24189 case DW_OP_reg11:
24190 case DW_OP_reg12:
24191 case DW_OP_reg13:
24192 case DW_OP_reg14:
24193 case DW_OP_reg15:
24194 case DW_OP_reg16:
24195 case DW_OP_reg17:
24196 case DW_OP_reg18:
24197 case DW_OP_reg19:
24198 case DW_OP_reg20:
24199 case DW_OP_reg21:
24200 case DW_OP_reg22:
24201 case DW_OP_reg23:
24202 case DW_OP_reg24:
24203 case DW_OP_reg25:
24204 case DW_OP_reg26:
24205 case DW_OP_reg27:
24206 case DW_OP_reg28:
24207 case DW_OP_reg29:
24208 case DW_OP_reg30:
24209 case DW_OP_reg31:
24210 stack[++stacki] = op - DW_OP_reg0;
24211 if (i < size)
24212 dwarf2_complex_location_expr_complaint ();
24213 break;
c906108c 24214
21ae7a4d
JK
24215 case DW_OP_regx:
24216 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24217 i += bytes_read;
24218 stack[++stacki] = unsnd;
24219 if (i < size)
24220 dwarf2_complex_location_expr_complaint ();
24221 break;
c906108c 24222
21ae7a4d
JK
24223 case DW_OP_addr:
24224 stack[++stacki] = read_address (objfile->obfd, &data[i],
24225 cu, &bytes_read);
24226 i += bytes_read;
24227 break;
d53d4ac5 24228
21ae7a4d
JK
24229 case DW_OP_const1u:
24230 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24231 i += 1;
24232 break;
24233
24234 case DW_OP_const1s:
24235 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24236 i += 1;
24237 break;
24238
24239 case DW_OP_const2u:
24240 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24241 i += 2;
24242 break;
24243
24244 case DW_OP_const2s:
24245 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24246 i += 2;
24247 break;
d53d4ac5 24248
21ae7a4d
JK
24249 case DW_OP_const4u:
24250 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24251 i += 4;
24252 break;
24253
24254 case DW_OP_const4s:
24255 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24256 i += 4;
24257 break;
24258
585861ea
JK
24259 case DW_OP_const8u:
24260 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24261 i += 8;
24262 break;
24263
21ae7a4d
JK
24264 case DW_OP_constu:
24265 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24266 &bytes_read);
24267 i += bytes_read;
24268 break;
24269
24270 case DW_OP_consts:
24271 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24272 i += bytes_read;
24273 break;
24274
24275 case DW_OP_dup:
24276 stack[stacki + 1] = stack[stacki];
24277 stacki++;
24278 break;
24279
24280 case DW_OP_plus:
24281 stack[stacki - 1] += stack[stacki];
24282 stacki--;
24283 break;
24284
24285 case DW_OP_plus_uconst:
24286 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24287 &bytes_read);
24288 i += bytes_read;
24289 break;
24290
24291 case DW_OP_minus:
24292 stack[stacki - 1] -= stack[stacki];
24293 stacki--;
24294 break;
24295
24296 case DW_OP_deref:
24297 /* If we're not the last op, then we definitely can't encode
24298 this using GDB's address_class enum. This is valid for partial
24299 global symbols, although the variable's address will be bogus
24300 in the psymtab. */
24301 if (i < size)
24302 dwarf2_complex_location_expr_complaint ();
24303 break;
24304
24305 case DW_OP_GNU_push_tls_address:
4aa4e28b 24306 case DW_OP_form_tls_address:
21ae7a4d
JK
24307 /* The top of the stack has the offset from the beginning
24308 of the thread control block at which the variable is located. */
24309 /* Nothing should follow this operator, so the top of stack would
24310 be returned. */
24311 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24312 address will be bogus in the psymtab. Make it always at least
24313 non-zero to not look as a variable garbage collected by linker
24314 which have DW_OP_addr 0. */
21ae7a4d
JK
24315 if (i < size)
24316 dwarf2_complex_location_expr_complaint ();
585861ea 24317 stack[stacki]++;
21ae7a4d
JK
24318 break;
24319
24320 case DW_OP_GNU_uninit:
24321 break;
24322
336d760d 24323 case DW_OP_addrx:
3019eac3 24324 case DW_OP_GNU_addr_index:
49f6c839 24325 case DW_OP_GNU_const_index:
3019eac3
DE
24326 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24327 &bytes_read);
24328 i += bytes_read;
24329 break;
24330
21ae7a4d
JK
24331 default:
24332 {
f39c6ffd 24333 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24334
24335 if (name)
b98664d3 24336 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24337 name);
24338 else
b98664d3 24339 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24340 op);
24341 }
24342
24343 return (stack[stacki]);
d53d4ac5 24344 }
3c6e0cb3 24345
21ae7a4d
JK
24346 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24347 outside of the allocated space. Also enforce minimum>0. */
24348 if (stacki >= ARRAY_SIZE (stack) - 1)
24349 {
b98664d3 24350 complaint (_("location description stack overflow"));
21ae7a4d
JK
24351 return 0;
24352 }
24353
24354 if (stacki <= 0)
24355 {
b98664d3 24356 complaint (_("location description stack underflow"));
21ae7a4d
JK
24357 return 0;
24358 }
24359 }
24360 return (stack[stacki]);
c906108c
SS
24361}
24362
24363/* memory allocation interface */
24364
c906108c 24365static struct dwarf_block *
7b5a2f43 24366dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24367{
8d749320 24368 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24369}
24370
c906108c 24371static struct die_info *
b60c80d6 24372dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24373{
24374 struct die_info *die;
b60c80d6
DJ
24375 size_t size = sizeof (struct die_info);
24376
24377 if (num_attrs > 1)
24378 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24379
b60c80d6 24380 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24381 memset (die, 0, sizeof (struct die_info));
24382 return (die);
24383}
2e276125
JB
24384
24385\f
24386/* Macro support. */
24387
233d95b5
JK
24388/* Return file name relative to the compilation directory of file number I in
24389 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24390 responsible for freeing it. */
233d95b5 24391
2e276125 24392static char *
233d95b5 24393file_file_name (int file, struct line_header *lh)
2e276125 24394{
6a83a1e6
EZ
24395 /* Is the file number a valid index into the line header's file name
24396 table? Remember that file numbers start with one, not zero. */
7ba99d21 24397 if (lh->is_valid_file_index (file))
6a83a1e6 24398 {
7ba99d21 24399 const file_entry *fe = lh->file_name_at (file);
6e70227d 24400
7ba99d21 24401 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24402 {
7ba99d21 24403 const char *dir = fe->include_dir (lh);
8c43009f 24404 if (dir != NULL)
7ba99d21 24405 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24406 }
7ba99d21 24407 return xstrdup (fe->name);
6a83a1e6 24408 }
2e276125
JB
24409 else
24410 {
6a83a1e6
EZ
24411 /* The compiler produced a bogus file number. We can at least
24412 record the macro definitions made in the file, even if we
24413 won't be able to find the file by name. */
24414 char fake_name[80];
9a619af0 24415
8c042590
PM
24416 xsnprintf (fake_name, sizeof (fake_name),
24417 "<bad macro file number %d>", file);
2e276125 24418
b98664d3 24419 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24420 file);
2e276125 24421
6a83a1e6 24422 return xstrdup (fake_name);
2e276125
JB
24423 }
24424}
24425
233d95b5
JK
24426/* Return the full name of file number I in *LH's file name table.
24427 Use COMP_DIR as the name of the current directory of the
24428 compilation. The result is allocated using xmalloc; the caller is
24429 responsible for freeing it. */
24430static char *
24431file_full_name (int file, struct line_header *lh, const char *comp_dir)
24432{
24433 /* Is the file number a valid index into the line header's file name
24434 table? Remember that file numbers start with one, not zero. */
7ba99d21 24435 if (lh->is_valid_file_index (file))
233d95b5
JK
24436 {
24437 char *relative = file_file_name (file, lh);
24438
24439 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24440 return relative;
b36cec19
PA
24441 return reconcat (relative, comp_dir, SLASH_STRING,
24442 relative, (char *) NULL);
233d95b5
JK
24443 }
24444 else
24445 return file_file_name (file, lh);
24446}
24447
2e276125
JB
24448
24449static struct macro_source_file *
804d2729
TT
24450macro_start_file (struct dwarf2_cu *cu,
24451 int file, int line,
2e276125 24452 struct macro_source_file *current_file,
43f3e411 24453 struct line_header *lh)
2e276125 24454{
233d95b5
JK
24455 /* File name relative to the compilation directory of this source file. */
24456 char *file_name = file_file_name (file, lh);
2e276125 24457
2e276125 24458 if (! current_file)
abc9d0dc 24459 {
fc474241
DE
24460 /* Note: We don't create a macro table for this compilation unit
24461 at all until we actually get a filename. */
c24bdb02 24462 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24463
abc9d0dc
TT
24464 /* If we have no current file, then this must be the start_file
24465 directive for the compilation unit's main source file. */
fc474241
DE
24466 current_file = macro_set_main (macro_table, file_name);
24467 macro_define_special (macro_table);
abc9d0dc 24468 }
2e276125 24469 else
233d95b5 24470 current_file = macro_include (current_file, line, file_name);
2e276125 24471
233d95b5 24472 xfree (file_name);
6e70227d 24473
2e276125
JB
24474 return current_file;
24475}
24476
2e276125
JB
24477static const char *
24478consume_improper_spaces (const char *p, const char *body)
24479{
24480 if (*p == ' ')
24481 {
b98664d3 24482 complaint (_("macro definition contains spaces "
3e43a32a 24483 "in formal argument list:\n`%s'"),
4d3c2250 24484 body);
2e276125
JB
24485
24486 while (*p == ' ')
24487 p++;
24488 }
24489
24490 return p;
24491}
24492
24493
24494static void
24495parse_macro_definition (struct macro_source_file *file, int line,
24496 const char *body)
24497{
24498 const char *p;
24499
24500 /* The body string takes one of two forms. For object-like macro
24501 definitions, it should be:
24502
24503 <macro name> " " <definition>
24504
24505 For function-like macro definitions, it should be:
24506
24507 <macro name> "() " <definition>
24508 or
24509 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24510
24511 Spaces may appear only where explicitly indicated, and in the
24512 <definition>.
24513
24514 The Dwarf 2 spec says that an object-like macro's name is always
24515 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24516 the space when the macro's definition is the empty string.
2e276125
JB
24517
24518 The Dwarf 2 spec says that there should be no spaces between the
24519 formal arguments in a function-like macro's formal argument list,
24520 but versions of GCC around March 2002 include spaces after the
24521 commas. */
24522
24523
24524 /* Find the extent of the macro name. The macro name is terminated
24525 by either a space or null character (for an object-like macro) or
24526 an opening paren (for a function-like macro). */
24527 for (p = body; *p; p++)
24528 if (*p == ' ' || *p == '(')
24529 break;
24530
24531 if (*p == ' ' || *p == '\0')
24532 {
24533 /* It's an object-like macro. */
24534 int name_len = p - body;
3f8a7804 24535 char *name = savestring (body, name_len);
2e276125
JB
24536 const char *replacement;
24537
24538 if (*p == ' ')
24539 replacement = body + name_len + 1;
24540 else
24541 {
4d3c2250 24542 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24543 replacement = body + name_len;
24544 }
6e70227d 24545
2e276125
JB
24546 macro_define_object (file, line, name, replacement);
24547
24548 xfree (name);
24549 }
24550 else if (*p == '(')
24551 {
24552 /* It's a function-like macro. */
3f8a7804 24553 char *name = savestring (body, p - body);
2e276125
JB
24554 int argc = 0;
24555 int argv_size = 1;
8d749320 24556 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24557
24558 p++;
24559
24560 p = consume_improper_spaces (p, body);
24561
24562 /* Parse the formal argument list. */
24563 while (*p && *p != ')')
24564 {
24565 /* Find the extent of the current argument name. */
24566 const char *arg_start = p;
24567
24568 while (*p && *p != ',' && *p != ')' && *p != ' ')
24569 p++;
24570
24571 if (! *p || p == arg_start)
4d3c2250 24572 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24573 else
24574 {
24575 /* Make sure argv has room for the new argument. */
24576 if (argc >= argv_size)
24577 {
24578 argv_size *= 2;
224c3ddb 24579 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24580 }
24581
3f8a7804 24582 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24583 }
24584
24585 p = consume_improper_spaces (p, body);
24586
24587 /* Consume the comma, if present. */
24588 if (*p == ',')
24589 {
24590 p++;
24591
24592 p = consume_improper_spaces (p, body);
24593 }
24594 }
24595
24596 if (*p == ')')
24597 {
24598 p++;
24599
24600 if (*p == ' ')
24601 /* Perfectly formed definition, no complaints. */
24602 macro_define_function (file, line, name,
6e70227d 24603 argc, (const char **) argv,
2e276125
JB
24604 p + 1);
24605 else if (*p == '\0')
24606 {
24607 /* Complain, but do define it. */
4d3c2250 24608 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24609 macro_define_function (file, line, name,
6e70227d 24610 argc, (const char **) argv,
2e276125
JB
24611 p);
24612 }
24613 else
24614 /* Just complain. */
4d3c2250 24615 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24616 }
24617 else
24618 /* Just complain. */
4d3c2250 24619 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24620
24621 xfree (name);
24622 {
24623 int i;
24624
24625 for (i = 0; i < argc; i++)
24626 xfree (argv[i]);
24627 }
24628 xfree (argv);
24629 }
24630 else
4d3c2250 24631 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24632}
24633
cf2c3c16
TT
24634/* Skip some bytes from BYTES according to the form given in FORM.
24635 Returns the new pointer. */
2e276125 24636
d521ce57
TT
24637static const gdb_byte *
24638skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24639 enum dwarf_form form,
24640 unsigned int offset_size,
24641 struct dwarf2_section_info *section)
2e276125 24642{
cf2c3c16 24643 unsigned int bytes_read;
2e276125 24644
cf2c3c16 24645 switch (form)
2e276125 24646 {
cf2c3c16
TT
24647 case DW_FORM_data1:
24648 case DW_FORM_flag:
24649 ++bytes;
24650 break;
24651
24652 case DW_FORM_data2:
24653 bytes += 2;
24654 break;
24655
24656 case DW_FORM_data4:
24657 bytes += 4;
24658 break;
24659
24660 case DW_FORM_data8:
24661 bytes += 8;
24662 break;
24663
0224619f
JK
24664 case DW_FORM_data16:
24665 bytes += 16;
24666 break;
24667
cf2c3c16
TT
24668 case DW_FORM_string:
24669 read_direct_string (abfd, bytes, &bytes_read);
24670 bytes += bytes_read;
24671 break;
24672
24673 case DW_FORM_sec_offset:
24674 case DW_FORM_strp:
36586728 24675 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24676 bytes += offset_size;
24677 break;
24678
24679 case DW_FORM_block:
24680 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24681 bytes += bytes_read;
24682 break;
24683
24684 case DW_FORM_block1:
24685 bytes += 1 + read_1_byte (abfd, bytes);
24686 break;
24687 case DW_FORM_block2:
24688 bytes += 2 + read_2_bytes (abfd, bytes);
24689 break;
24690 case DW_FORM_block4:
24691 bytes += 4 + read_4_bytes (abfd, bytes);
24692 break;
24693
336d760d 24694 case DW_FORM_addrx:
cf2c3c16 24695 case DW_FORM_sdata:
cf532bd1 24696 case DW_FORM_strx:
cf2c3c16 24697 case DW_FORM_udata:
3019eac3
DE
24698 case DW_FORM_GNU_addr_index:
24699 case DW_FORM_GNU_str_index:
d521ce57 24700 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24701 if (bytes == NULL)
24702 {
24703 dwarf2_section_buffer_overflow_complaint (section);
24704 return NULL;
24705 }
cf2c3c16
TT
24706 break;
24707
663c44ac
JK
24708 case DW_FORM_implicit_const:
24709 break;
24710
cf2c3c16
TT
24711 default:
24712 {
b98664d3 24713 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24714 form, get_section_name (section));
cf2c3c16
TT
24715 return NULL;
24716 }
2e276125
JB
24717 }
24718
cf2c3c16
TT
24719 return bytes;
24720}
757a13d0 24721
cf2c3c16
TT
24722/* A helper for dwarf_decode_macros that handles skipping an unknown
24723 opcode. Returns an updated pointer to the macro data buffer; or,
24724 on error, issues a complaint and returns NULL. */
757a13d0 24725
d521ce57 24726static const gdb_byte *
cf2c3c16 24727skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24728 const gdb_byte **opcode_definitions,
24729 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24730 bfd *abfd,
24731 unsigned int offset_size,
24732 struct dwarf2_section_info *section)
24733{
24734 unsigned int bytes_read, i;
24735 unsigned long arg;
d521ce57 24736 const gdb_byte *defn;
2e276125 24737
cf2c3c16 24738 if (opcode_definitions[opcode] == NULL)
2e276125 24739 {
b98664d3 24740 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24741 opcode);
24742 return NULL;
24743 }
2e276125 24744
cf2c3c16
TT
24745 defn = opcode_definitions[opcode];
24746 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24747 defn += bytes_read;
2e276125 24748
cf2c3c16
TT
24749 for (i = 0; i < arg; ++i)
24750 {
aead7601
SM
24751 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24752 (enum dwarf_form) defn[i], offset_size,
f664829e 24753 section);
cf2c3c16
TT
24754 if (mac_ptr == NULL)
24755 {
24756 /* skip_form_bytes already issued the complaint. */
24757 return NULL;
24758 }
24759 }
757a13d0 24760
cf2c3c16
TT
24761 return mac_ptr;
24762}
757a13d0 24763
cf2c3c16
TT
24764/* A helper function which parses the header of a macro section.
24765 If the macro section is the extended (for now called "GNU") type,
24766 then this updates *OFFSET_SIZE. Returns a pointer to just after
24767 the header, or issues a complaint and returns NULL on error. */
757a13d0 24768
d521ce57
TT
24769static const gdb_byte *
24770dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24771 bfd *abfd,
d521ce57 24772 const gdb_byte *mac_ptr,
cf2c3c16
TT
24773 unsigned int *offset_size,
24774 int section_is_gnu)
24775{
24776 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24777
cf2c3c16
TT
24778 if (section_is_gnu)
24779 {
24780 unsigned int version, flags;
757a13d0 24781
cf2c3c16 24782 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24783 if (version != 4 && version != 5)
cf2c3c16 24784 {
b98664d3 24785 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24786 version);
24787 return NULL;
24788 }
24789 mac_ptr += 2;
757a13d0 24790
cf2c3c16
TT
24791 flags = read_1_byte (abfd, mac_ptr);
24792 ++mac_ptr;
24793 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24794
cf2c3c16
TT
24795 if ((flags & 2) != 0)
24796 /* We don't need the line table offset. */
24797 mac_ptr += *offset_size;
757a13d0 24798
cf2c3c16
TT
24799 /* Vendor opcode descriptions. */
24800 if ((flags & 4) != 0)
24801 {
24802 unsigned int i, count;
757a13d0 24803
cf2c3c16
TT
24804 count = read_1_byte (abfd, mac_ptr);
24805 ++mac_ptr;
24806 for (i = 0; i < count; ++i)
24807 {
24808 unsigned int opcode, bytes_read;
24809 unsigned long arg;
24810
24811 opcode = read_1_byte (abfd, mac_ptr);
24812 ++mac_ptr;
24813 opcode_definitions[opcode] = mac_ptr;
24814 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24815 mac_ptr += bytes_read;
24816 mac_ptr += arg;
24817 }
757a13d0 24818 }
cf2c3c16 24819 }
757a13d0 24820
cf2c3c16
TT
24821 return mac_ptr;
24822}
757a13d0 24823
cf2c3c16 24824/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24825 including DW_MACRO_import. */
cf2c3c16
TT
24826
24827static void
804d2729 24828dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24829 bfd *abfd,
d521ce57 24830 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24831 struct macro_source_file *current_file,
43f3e411 24832 struct line_header *lh,
cf2c3c16 24833 struct dwarf2_section_info *section,
36586728 24834 int section_is_gnu, int section_is_dwz,
cf2c3c16 24835 unsigned int offset_size,
8fc3fc34 24836 htab_t include_hash)
cf2c3c16 24837{
804d2729
TT
24838 struct dwarf2_per_objfile *dwarf2_per_objfile
24839 = cu->per_cu->dwarf2_per_objfile;
4d663531 24840 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24841 enum dwarf_macro_record_type macinfo_type;
24842 int at_commandline;
d521ce57 24843 const gdb_byte *opcode_definitions[256];
757a13d0 24844
cf2c3c16
TT
24845 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24846 &offset_size, section_is_gnu);
24847 if (mac_ptr == NULL)
24848 {
24849 /* We already issued a complaint. */
24850 return;
24851 }
757a13d0
JK
24852
24853 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24854 GDB is still reading the definitions from command line. First
24855 DW_MACINFO_start_file will need to be ignored as it was already executed
24856 to create CURRENT_FILE for the main source holding also the command line
24857 definitions. On first met DW_MACINFO_start_file this flag is reset to
24858 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24859
24860 at_commandline = 1;
24861
24862 do
24863 {
24864 /* Do we at least have room for a macinfo type byte? */
24865 if (mac_ptr >= mac_end)
24866 {
f664829e 24867 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24868 break;
24869 }
24870
aead7601 24871 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24872 mac_ptr++;
24873
cf2c3c16
TT
24874 /* Note that we rely on the fact that the corresponding GNU and
24875 DWARF constants are the same. */
132448f8
SM
24876 DIAGNOSTIC_PUSH
24877 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24878 switch (macinfo_type)
24879 {
24880 /* A zero macinfo type indicates the end of the macro
24881 information. */
24882 case 0:
24883 break;
2e276125 24884
0af92d60
JK
24885 case DW_MACRO_define:
24886 case DW_MACRO_undef:
24887 case DW_MACRO_define_strp:
24888 case DW_MACRO_undef_strp:
24889 case DW_MACRO_define_sup:
24890 case DW_MACRO_undef_sup:
2e276125 24891 {
891d2f0b 24892 unsigned int bytes_read;
2e276125 24893 int line;
d521ce57 24894 const char *body;
cf2c3c16 24895 int is_define;
2e276125 24896
cf2c3c16
TT
24897 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24898 mac_ptr += bytes_read;
24899
0af92d60
JK
24900 if (macinfo_type == DW_MACRO_define
24901 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24902 {
24903 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24904 mac_ptr += bytes_read;
24905 }
24906 else
24907 {
24908 LONGEST str_offset;
24909
24910 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24911 mac_ptr += offset_size;
2e276125 24912
0af92d60
JK
24913 if (macinfo_type == DW_MACRO_define_sup
24914 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24915 || section_is_dwz)
36586728 24916 {
ed2dc618
SM
24917 struct dwz_file *dwz
24918 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24919
ed2dc618
SM
24920 body = read_indirect_string_from_dwz (objfile,
24921 dwz, str_offset);
36586728
TT
24922 }
24923 else
ed2dc618
SM
24924 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24925 abfd, str_offset);
cf2c3c16
TT
24926 }
24927
0af92d60
JK
24928 is_define = (macinfo_type == DW_MACRO_define
24929 || macinfo_type == DW_MACRO_define_strp
24930 || macinfo_type == DW_MACRO_define_sup);
2e276125 24931 if (! current_file)
757a13d0
JK
24932 {
24933 /* DWARF violation as no main source is present. */
b98664d3 24934 complaint (_("debug info with no main source gives macro %s "
757a13d0 24935 "on line %d: %s"),
cf2c3c16
TT
24936 is_define ? _("definition") : _("undefinition"),
24937 line, body);
757a13d0
JK
24938 break;
24939 }
3e43a32a
MS
24940 if ((line == 0 && !at_commandline)
24941 || (line != 0 && at_commandline))
b98664d3 24942 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24943 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24944 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24945 line == 0 ? _("zero") : _("non-zero"), line, body);
24946
955b06fa 24947 if (body == NULL)
7bede828 24948 {
955b06fa
SDJ
24949 /* Fedora's rpm-build's "debugedit" binary
24950 corrupted .debug_macro sections.
24951
24952 For more info, see
24953 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24954 complaint (_("debug info gives %s invalid macro %s "
24955 "without body (corrupted?) at line %d "
24956 "on file %s"),
24957 at_commandline ? _("command-line") : _("in-file"),
24958 is_define ? _("definition") : _("undefinition"),
24959 line, current_file->filename);
7bede828 24960 }
955b06fa
SDJ
24961 else if (is_define)
24962 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24963 else
24964 {
0af92d60
JK
24965 gdb_assert (macinfo_type == DW_MACRO_undef
24966 || macinfo_type == DW_MACRO_undef_strp
24967 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24968 macro_undef (current_file, line, body);
24969 }
2e276125
JB
24970 }
24971 break;
24972
0af92d60 24973 case DW_MACRO_start_file:
2e276125 24974 {
891d2f0b 24975 unsigned int bytes_read;
2e276125
JB
24976 int line, file;
24977
24978 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24979 mac_ptr += bytes_read;
24980 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24981 mac_ptr += bytes_read;
24982
3e43a32a
MS
24983 if ((line == 0 && !at_commandline)
24984 || (line != 0 && at_commandline))
b98664d3 24985 complaint (_("debug info gives source %d included "
757a13d0
JK
24986 "from %s at %s line %d"),
24987 file, at_commandline ? _("command-line") : _("file"),
24988 line == 0 ? _("zero") : _("non-zero"), line);
24989
24990 if (at_commandline)
24991 {
0af92d60 24992 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24993 pass one. */
757a13d0
JK
24994 at_commandline = 0;
24995 }
24996 else
804d2729
TT
24997 current_file = macro_start_file (cu, file, line, current_file,
24998 lh);
2e276125
JB
24999 }
25000 break;
25001
0af92d60 25002 case DW_MACRO_end_file:
2e276125 25003 if (! current_file)
b98664d3 25004 complaint (_("macro debug info has an unmatched "
3e43a32a 25005 "`close_file' directive"));
2e276125
JB
25006 else
25007 {
25008 current_file = current_file->included_by;
25009 if (! current_file)
25010 {
cf2c3c16 25011 enum dwarf_macro_record_type next_type;
2e276125
JB
25012
25013 /* GCC circa March 2002 doesn't produce the zero
25014 type byte marking the end of the compilation
25015 unit. Complain if it's not there, but exit no
25016 matter what. */
25017
25018 /* Do we at least have room for a macinfo type byte? */
25019 if (mac_ptr >= mac_end)
25020 {
f664829e 25021 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25022 return;
25023 }
25024
25025 /* We don't increment mac_ptr here, so this is just
25026 a look-ahead. */
aead7601
SM
25027 next_type
25028 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25029 mac_ptr);
2e276125 25030 if (next_type != 0)
b98664d3 25031 complaint (_("no terminating 0-type entry for "
3e43a32a 25032 "macros in `.debug_macinfo' section"));
2e276125
JB
25033
25034 return;
25035 }
25036 }
25037 break;
25038
0af92d60
JK
25039 case DW_MACRO_import:
25040 case DW_MACRO_import_sup:
cf2c3c16
TT
25041 {
25042 LONGEST offset;
8fc3fc34 25043 void **slot;
a036ba48
TT
25044 bfd *include_bfd = abfd;
25045 struct dwarf2_section_info *include_section = section;
d521ce57 25046 const gdb_byte *include_mac_end = mac_end;
a036ba48 25047 int is_dwz = section_is_dwz;
d521ce57 25048 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25049
25050 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25051 mac_ptr += offset_size;
25052
0af92d60 25053 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25054 {
ed2dc618 25055 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25056
4d663531 25057 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25058
a036ba48 25059 include_section = &dwz->macro;
a32a8923 25060 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25061 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25062 is_dwz = 1;
25063 }
25064
25065 new_mac_ptr = include_section->buffer + offset;
25066 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25067
8fc3fc34
TT
25068 if (*slot != NULL)
25069 {
25070 /* This has actually happened; see
25071 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25072 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25073 ".debug_macro section"));
25074 }
25075 else
25076 {
d521ce57 25077 *slot = (void *) new_mac_ptr;
36586728 25078
804d2729 25079 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25080 include_mac_end, current_file, lh,
36586728 25081 section, section_is_gnu, is_dwz,
4d663531 25082 offset_size, include_hash);
8fc3fc34 25083
d521ce57 25084 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25085 }
cf2c3c16
TT
25086 }
25087 break;
25088
2e276125 25089 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25090 if (!section_is_gnu)
25091 {
25092 unsigned int bytes_read;
2e276125 25093
ac298888
TT
25094 /* This reads the constant, but since we don't recognize
25095 any vendor extensions, we ignore it. */
25096 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25097 mac_ptr += bytes_read;
25098 read_direct_string (abfd, mac_ptr, &bytes_read);
25099 mac_ptr += bytes_read;
2e276125 25100
cf2c3c16
TT
25101 /* We don't recognize any vendor extensions. */
25102 break;
25103 }
25104 /* FALLTHROUGH */
25105
25106 default:
25107 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25108 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25109 section);
25110 if (mac_ptr == NULL)
25111 return;
25112 break;
2e276125 25113 }
132448f8 25114 DIAGNOSTIC_POP
757a13d0 25115 } while (macinfo_type != 0);
2e276125 25116}
8e19ed76 25117
cf2c3c16 25118static void
09262596 25119dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25120 int section_is_gnu)
cf2c3c16 25121{
518817b3
SM
25122 struct dwarf2_per_objfile *dwarf2_per_objfile
25123 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25124 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25125 struct line_header *lh = cu->line_header;
25126 bfd *abfd;
d521ce57 25127 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25128 struct macro_source_file *current_file = 0;
25129 enum dwarf_macro_record_type macinfo_type;
25130 unsigned int offset_size = cu->header.offset_size;
d521ce57 25131 const gdb_byte *opcode_definitions[256];
8fc3fc34 25132 void **slot;
09262596
DE
25133 struct dwarf2_section_info *section;
25134 const char *section_name;
25135
25136 if (cu->dwo_unit != NULL)
25137 {
25138 if (section_is_gnu)
25139 {
25140 section = &cu->dwo_unit->dwo_file->sections.macro;
25141 section_name = ".debug_macro.dwo";
25142 }
25143 else
25144 {
25145 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25146 section_name = ".debug_macinfo.dwo";
25147 }
25148 }
25149 else
25150 {
25151 if (section_is_gnu)
25152 {
25153 section = &dwarf2_per_objfile->macro;
25154 section_name = ".debug_macro";
25155 }
25156 else
25157 {
25158 section = &dwarf2_per_objfile->macinfo;
25159 section_name = ".debug_macinfo";
25160 }
25161 }
cf2c3c16 25162
bb5ed363 25163 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25164 if (section->buffer == NULL)
25165 {
b98664d3 25166 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25167 return;
25168 }
a32a8923 25169 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25170
25171 /* First pass: Find the name of the base filename.
25172 This filename is needed in order to process all macros whose definition
25173 (or undefinition) comes from the command line. These macros are defined
25174 before the first DW_MACINFO_start_file entry, and yet still need to be
25175 associated to the base file.
25176
25177 To determine the base file name, we scan the macro definitions until we
25178 reach the first DW_MACINFO_start_file entry. We then initialize
25179 CURRENT_FILE accordingly so that any macro definition found before the
25180 first DW_MACINFO_start_file can still be associated to the base file. */
25181
25182 mac_ptr = section->buffer + offset;
25183 mac_end = section->buffer + section->size;
25184
25185 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25186 &offset_size, section_is_gnu);
25187 if (mac_ptr == NULL)
25188 {
25189 /* We already issued a complaint. */
25190 return;
25191 }
25192
25193 do
25194 {
25195 /* Do we at least have room for a macinfo type byte? */
25196 if (mac_ptr >= mac_end)
25197 {
25198 /* Complaint is printed during the second pass as GDB will probably
25199 stop the first pass earlier upon finding
25200 DW_MACINFO_start_file. */
25201 break;
25202 }
25203
aead7601 25204 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25205 mac_ptr++;
25206
25207 /* Note that we rely on the fact that the corresponding GNU and
25208 DWARF constants are the same. */
132448f8
SM
25209 DIAGNOSTIC_PUSH
25210 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25211 switch (macinfo_type)
25212 {
25213 /* A zero macinfo type indicates the end of the macro
25214 information. */
25215 case 0:
25216 break;
25217
0af92d60
JK
25218 case DW_MACRO_define:
25219 case DW_MACRO_undef:
cf2c3c16
TT
25220 /* Only skip the data by MAC_PTR. */
25221 {
25222 unsigned int bytes_read;
25223
25224 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25225 mac_ptr += bytes_read;
25226 read_direct_string (abfd, mac_ptr, &bytes_read);
25227 mac_ptr += bytes_read;
25228 }
25229 break;
25230
0af92d60 25231 case DW_MACRO_start_file:
cf2c3c16
TT
25232 {
25233 unsigned int bytes_read;
25234 int line, file;
25235
25236 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25237 mac_ptr += bytes_read;
25238 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25239 mac_ptr += bytes_read;
25240
804d2729 25241 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25242 }
25243 break;
25244
0af92d60 25245 case DW_MACRO_end_file:
cf2c3c16
TT
25246 /* No data to skip by MAC_PTR. */
25247 break;
25248
0af92d60
JK
25249 case DW_MACRO_define_strp:
25250 case DW_MACRO_undef_strp:
25251 case DW_MACRO_define_sup:
25252 case DW_MACRO_undef_sup:
cf2c3c16
TT
25253 {
25254 unsigned int bytes_read;
25255
25256 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25257 mac_ptr += bytes_read;
25258 mac_ptr += offset_size;
25259 }
25260 break;
25261
0af92d60
JK
25262 case DW_MACRO_import:
25263 case DW_MACRO_import_sup:
cf2c3c16 25264 /* Note that, according to the spec, a transparent include
0af92d60 25265 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25266 skip this opcode. */
25267 mac_ptr += offset_size;
25268 break;
25269
25270 case DW_MACINFO_vendor_ext:
25271 /* Only skip the data by MAC_PTR. */
25272 if (!section_is_gnu)
25273 {
25274 unsigned int bytes_read;
25275
25276 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25277 mac_ptr += bytes_read;
25278 read_direct_string (abfd, mac_ptr, &bytes_read);
25279 mac_ptr += bytes_read;
25280 }
25281 /* FALLTHROUGH */
25282
25283 default:
25284 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25285 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25286 section);
25287 if (mac_ptr == NULL)
25288 return;
25289 break;
25290 }
132448f8 25291 DIAGNOSTIC_POP
cf2c3c16
TT
25292 } while (macinfo_type != 0 && current_file == NULL);
25293
25294 /* Second pass: Process all entries.
25295
25296 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25297 command-line macro definitions/undefinitions. This flag is unset when we
25298 reach the first DW_MACINFO_start_file entry. */
25299
fc4007c9
TT
25300 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25301 htab_eq_pointer,
25302 NULL, xcalloc, xfree));
8fc3fc34 25303 mac_ptr = section->buffer + offset;
fc4007c9 25304 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25305 *slot = (void *) mac_ptr;
804d2729 25306 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25307 current_file, lh, section,
fc4007c9
TT
25308 section_is_gnu, 0, offset_size,
25309 include_hash.get ());
cf2c3c16
TT
25310}
25311
8e19ed76 25312/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25313 if so return true else false. */
380bca97 25314
8e19ed76 25315static int
6e5a29e1 25316attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25317{
25318 return (attr == NULL ? 0 :
25319 attr->form == DW_FORM_block1
25320 || attr->form == DW_FORM_block2
25321 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25322 || attr->form == DW_FORM_block
25323 || attr->form == DW_FORM_exprloc);
8e19ed76 25324}
4c2df51b 25325
c6a0999f
JB
25326/* Return non-zero if ATTR's value is a section offset --- classes
25327 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25328 You may use DW_UNSND (attr) to retrieve such offsets.
25329
25330 Section 7.5.4, "Attribute Encodings", explains that no attribute
25331 may have a value that belongs to more than one of these classes; it
25332 would be ambiguous if we did, because we use the same forms for all
25333 of them. */
380bca97 25334
3690dd37 25335static int
6e5a29e1 25336attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25337{
25338 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25339 || attr->form == DW_FORM_data8
25340 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25341}
25342
3690dd37
JB
25343/* Return non-zero if ATTR's value falls in the 'constant' class, or
25344 zero otherwise. When this function returns true, you can apply
25345 dwarf2_get_attr_constant_value to it.
25346
25347 However, note that for some attributes you must check
25348 attr_form_is_section_offset before using this test. DW_FORM_data4
25349 and DW_FORM_data8 are members of both the constant class, and of
25350 the classes that contain offsets into other debug sections
25351 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25352 that, if an attribute's can be either a constant or one of the
25353 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25354 taken as section offsets, not constants.
25355
25356 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25357 cannot handle that. */
380bca97 25358
3690dd37 25359static int
6e5a29e1 25360attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25361{
25362 switch (attr->form)
25363 {
25364 case DW_FORM_sdata:
25365 case DW_FORM_udata:
25366 case DW_FORM_data1:
25367 case DW_FORM_data2:
25368 case DW_FORM_data4:
25369 case DW_FORM_data8:
663c44ac 25370 case DW_FORM_implicit_const:
3690dd37
JB
25371 return 1;
25372 default:
25373 return 0;
25374 }
25375}
25376
7771576e
SA
25377
25378/* DW_ADDR is always stored already as sect_offset; despite for the forms
25379 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25380
25381static int
6e5a29e1 25382attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25383{
25384 switch (attr->form)
25385 {
25386 case DW_FORM_ref_addr:
25387 case DW_FORM_ref1:
25388 case DW_FORM_ref2:
25389 case DW_FORM_ref4:
25390 case DW_FORM_ref8:
25391 case DW_FORM_ref_udata:
25392 case DW_FORM_GNU_ref_alt:
25393 return 1;
25394 default:
25395 return 0;
25396 }
25397}
25398
3019eac3
DE
25399/* Return the .debug_loc section to use for CU.
25400 For DWO files use .debug_loc.dwo. */
25401
25402static struct dwarf2_section_info *
25403cu_debug_loc_section (struct dwarf2_cu *cu)
25404{
518817b3
SM
25405 struct dwarf2_per_objfile *dwarf2_per_objfile
25406 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25407
3019eac3 25408 if (cu->dwo_unit)
43988095
JK
25409 {
25410 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25411
43988095
JK
25412 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25413 }
25414 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25415 : &dwarf2_per_objfile->loc);
3019eac3
DE
25416}
25417
8cf6f0b1
TT
25418/* A helper function that fills in a dwarf2_loclist_baton. */
25419
25420static void
25421fill_in_loclist_baton (struct dwarf2_cu *cu,
25422 struct dwarf2_loclist_baton *baton,
ff39bb5e 25423 const struct attribute *attr)
8cf6f0b1 25424{
518817b3
SM
25425 struct dwarf2_per_objfile *dwarf2_per_objfile
25426 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25427 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25428
25429 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25430
25431 baton->per_cu = cu->per_cu;
25432 gdb_assert (baton->per_cu);
25433 /* We don't know how long the location list is, but make sure we
25434 don't run off the edge of the section. */
3019eac3
DE
25435 baton->size = section->size - DW_UNSND (attr);
25436 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25437 baton->base_address = cu->base_address;
f664829e 25438 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25439}
25440
4c2df51b 25441static void
ff39bb5e 25442dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25443 struct dwarf2_cu *cu, int is_block)
4c2df51b 25444{
518817b3
SM
25445 struct dwarf2_per_objfile *dwarf2_per_objfile
25446 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25447 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25448 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25449
3690dd37 25450 if (attr_form_is_section_offset (attr)
3019eac3 25451 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25452 the section. If so, fall through to the complaint in the
25453 other branch. */
3019eac3 25454 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25455 {
0d53c4c4 25456 struct dwarf2_loclist_baton *baton;
4c2df51b 25457
8d749320 25458 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25459
8cf6f0b1 25460 fill_in_loclist_baton (cu, baton, attr);
be391dca 25461
d00adf39 25462 if (cu->base_known == 0)
b98664d3 25463 complaint (_("Location list used without "
3e43a32a 25464 "specifying the CU base address."));
4c2df51b 25465
f1e6e072
TT
25466 SYMBOL_ACLASS_INDEX (sym) = (is_block
25467 ? dwarf2_loclist_block_index
25468 : dwarf2_loclist_index);
0d53c4c4
DJ
25469 SYMBOL_LOCATION_BATON (sym) = baton;
25470 }
25471 else
25472 {
25473 struct dwarf2_locexpr_baton *baton;
25474
8d749320 25475 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25476 baton->per_cu = cu->per_cu;
25477 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25478
25479 if (attr_form_is_block (attr))
25480 {
25481 /* Note that we're just copying the block's data pointer
25482 here, not the actual data. We're still pointing into the
6502dd73
DJ
25483 info_buffer for SYM's objfile; right now we never release
25484 that buffer, but when we do clean up properly this may
25485 need to change. */
0d53c4c4
DJ
25486 baton->size = DW_BLOCK (attr)->size;
25487 baton->data = DW_BLOCK (attr)->data;
25488 }
25489 else
25490 {
25491 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25492 sym->natural_name ());
0d53c4c4 25493 baton->size = 0;
0d53c4c4 25494 }
6e70227d 25495
f1e6e072
TT
25496 SYMBOL_ACLASS_INDEX (sym) = (is_block
25497 ? dwarf2_locexpr_block_index
25498 : dwarf2_locexpr_index);
0d53c4c4
DJ
25499 SYMBOL_LOCATION_BATON (sym) = baton;
25500 }
4c2df51b 25501}
6502dd73 25502
9aa1f1e3
TT
25503/* Return the OBJFILE associated with the compilation unit CU. If CU
25504 came from a separate debuginfo file, then the master objfile is
25505 returned. */
ae0d2f24
UW
25506
25507struct objfile *
25508dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25509{
e3b94546 25510 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25511
25512 /* Return the master objfile, so that we can report and look up the
25513 correct file containing this variable. */
25514 if (objfile->separate_debug_objfile_backlink)
25515 objfile = objfile->separate_debug_objfile_backlink;
25516
25517 return objfile;
25518}
25519
96408a79
SA
25520/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25521 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25522 CU_HEADERP first. */
25523
25524static const struct comp_unit_head *
25525per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25526 struct dwarf2_per_cu_data *per_cu)
25527{
d521ce57 25528 const gdb_byte *info_ptr;
96408a79
SA
25529
25530 if (per_cu->cu)
25531 return &per_cu->cu->header;
25532
9c541725 25533 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25534
25535 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25536 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25537 rcuh_kind::COMPILE);
96408a79
SA
25538
25539 return cu_headerp;
25540}
25541
ae0d2f24
UW
25542/* Return the address size given in the compilation unit header for CU. */
25543
98714339 25544int
ae0d2f24
UW
25545dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25546{
96408a79
SA
25547 struct comp_unit_head cu_header_local;
25548 const struct comp_unit_head *cu_headerp;
c471e790 25549
96408a79
SA
25550 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25551
25552 return cu_headerp->addr_size;
ae0d2f24
UW
25553}
25554
9eae7c52
TT
25555/* Return the offset size given in the compilation unit header for CU. */
25556
25557int
25558dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25559{
96408a79
SA
25560 struct comp_unit_head cu_header_local;
25561 const struct comp_unit_head *cu_headerp;
9c6c53f7 25562
96408a79
SA
25563 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25564
25565 return cu_headerp->offset_size;
25566}
25567
25568/* See its dwarf2loc.h declaration. */
25569
25570int
25571dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25572{
25573 struct comp_unit_head cu_header_local;
25574 const struct comp_unit_head *cu_headerp;
25575
25576 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25577
25578 if (cu_headerp->version == 2)
25579 return cu_headerp->addr_size;
25580 else
25581 return cu_headerp->offset_size;
181cebd4
JK
25582}
25583
9aa1f1e3
TT
25584/* Return the text offset of the CU. The returned offset comes from
25585 this CU's objfile. If this objfile came from a separate debuginfo
25586 file, then the offset may be different from the corresponding
25587 offset in the parent objfile. */
25588
25589CORE_ADDR
25590dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25591{
e3b94546 25592 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25593
25594 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25595}
25596
9a49df9d
AB
25597/* Return a type that is a generic pointer type, the size of which matches
25598 the address size given in the compilation unit header for PER_CU. */
25599static struct type *
25600dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25601{
25602 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25603 struct type *void_type = objfile_type (objfile)->builtin_void;
25604 struct type *addr_type = lookup_pointer_type (void_type);
25605 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25606
25607 if (TYPE_LENGTH (addr_type) == addr_size)
25608 return addr_type;
25609
25610 addr_type
25611 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25612 return addr_type;
25613}
25614
43988095
JK
25615/* Return DWARF version number of PER_CU. */
25616
25617short
25618dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25619{
25620 return per_cu->dwarf_version;
25621}
25622
348e048f
DE
25623/* Locate the .debug_info compilation unit from CU's objfile which contains
25624 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25625
25626static struct dwarf2_per_cu_data *
9c541725 25627dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25628 unsigned int offset_in_dwz,
ed2dc618 25629 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25630{
25631 struct dwarf2_per_cu_data *this_cu;
25632 int low, high;
25633
ae038cb0 25634 low = 0;
b76e467d 25635 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25636 while (high > low)
25637 {
36586728 25638 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25639 int mid = low + (high - low) / 2;
9a619af0 25640
36586728 25641 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25642 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25643 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25644 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25645 high = mid;
25646 else
25647 low = mid + 1;
25648 }
25649 gdb_assert (low == high);
36586728 25650 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25651 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25652 {
36586728 25653 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25654 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25655 "offset %s [in module %s]"),
25656 sect_offset_str (sect_off),
ed2dc618 25657 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25658
9c541725
PA
25659 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25660 <= sect_off);
ae038cb0
DJ
25661 return dwarf2_per_objfile->all_comp_units[low-1];
25662 }
25663 else
25664 {
b76e467d 25665 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25666 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25667 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25668 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25669 return this_cu;
25670 }
25671}
25672
23745b47 25673/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25674
fcd3b13d
SM
25675dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25676 : per_cu (per_cu_),
9068261f
AB
25677 mark (false),
25678 has_loclist (false),
25679 checked_producer (false),
25680 producer_is_gxx_lt_4_6 (false),
25681 producer_is_gcc_lt_4_3 (false),
eb77c9df 25682 producer_is_icc (false),
9068261f 25683 producer_is_icc_lt_14 (false),
c258c396 25684 producer_is_codewarrior (false),
9068261f 25685 processing_has_namespace_info (false)
93311388 25686{
fcd3b13d
SM
25687 per_cu->cu = this;
25688}
25689
25690/* Destroy a dwarf2_cu. */
25691
25692dwarf2_cu::~dwarf2_cu ()
25693{
25694 per_cu->cu = NULL;
9816fde3
JK
25695}
25696
25697/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25698
25699static void
95554aad
TT
25700prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25701 enum language pretend_language)
9816fde3
JK
25702{
25703 struct attribute *attr;
25704
25705 /* Set the language we're debugging. */
25706 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25707 if (attr != nullptr)
9816fde3
JK
25708 set_cu_language (DW_UNSND (attr), cu);
25709 else
9cded63f 25710 {
95554aad 25711 cu->language = pretend_language;
9cded63f
TT
25712 cu->language_defn = language_def (cu->language);
25713 }
dee91e82 25714
7d45c7c3 25715 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25716}
25717
ae038cb0
DJ
25718/* Increase the age counter on each cached compilation unit, and free
25719 any that are too old. */
25720
25721static void
ed2dc618 25722age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25723{
25724 struct dwarf2_per_cu_data *per_cu, **last_chain;
25725
25726 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25727 per_cu = dwarf2_per_objfile->read_in_chain;
25728 while (per_cu != NULL)
25729 {
25730 per_cu->cu->last_used ++;
b4f54984 25731 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25732 dwarf2_mark (per_cu->cu);
25733 per_cu = per_cu->cu->read_in_chain;
25734 }
25735
25736 per_cu = dwarf2_per_objfile->read_in_chain;
25737 last_chain = &dwarf2_per_objfile->read_in_chain;
25738 while (per_cu != NULL)
25739 {
25740 struct dwarf2_per_cu_data *next_cu;
25741
25742 next_cu = per_cu->cu->read_in_chain;
25743
25744 if (!per_cu->cu->mark)
25745 {
fcd3b13d 25746 delete per_cu->cu;
ae038cb0
DJ
25747 *last_chain = next_cu;
25748 }
25749 else
25750 last_chain = &per_cu->cu->read_in_chain;
25751
25752 per_cu = next_cu;
25753 }
25754}
25755
25756/* Remove a single compilation unit from the cache. */
25757
25758static void
dee91e82 25759free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25760{
25761 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25762 struct dwarf2_per_objfile *dwarf2_per_objfile
25763 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25764
25765 per_cu = dwarf2_per_objfile->read_in_chain;
25766 last_chain = &dwarf2_per_objfile->read_in_chain;
25767 while (per_cu != NULL)
25768 {
25769 struct dwarf2_per_cu_data *next_cu;
25770
25771 next_cu = per_cu->cu->read_in_chain;
25772
dee91e82 25773 if (per_cu == target_per_cu)
ae038cb0 25774 {
fcd3b13d 25775 delete per_cu->cu;
dee91e82 25776 per_cu->cu = NULL;
ae038cb0
DJ
25777 *last_chain = next_cu;
25778 break;
25779 }
25780 else
25781 last_chain = &per_cu->cu->read_in_chain;
25782
25783 per_cu = next_cu;
25784 }
25785}
25786
dee91e82
DE
25787/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25788 We store these in a hash table separate from the DIEs, and preserve them
25789 when the DIEs are flushed out of cache.
25790
25791 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25792 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25793 or the type may come from a DWO file. Furthermore, while it's more logical
25794 to use per_cu->section+offset, with Fission the section with the data is in
25795 the DWO file but we don't know that section at the point we need it.
25796 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25797 because we can enter the lookup routine, get_die_type_at_offset, from
25798 outside this file, and thus won't necessarily have PER_CU->cu.
25799 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25800
dee91e82 25801struct dwarf2_per_cu_offset_and_type
1c379e20 25802{
dee91e82 25803 const struct dwarf2_per_cu_data *per_cu;
9c541725 25804 sect_offset sect_off;
1c379e20
DJ
25805 struct type *type;
25806};
25807
dee91e82 25808/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25809
25810static hashval_t
dee91e82 25811per_cu_offset_and_type_hash (const void *item)
1c379e20 25812{
9a3c8263
SM
25813 const struct dwarf2_per_cu_offset_and_type *ofs
25814 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25815
9c541725 25816 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25817}
25818
dee91e82 25819/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25820
25821static int
dee91e82 25822per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25823{
9a3c8263
SM
25824 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25825 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25826 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25827 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25828
dee91e82 25829 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25830 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25831}
25832
25833/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25834 table if necessary. For convenience, return TYPE.
25835
25836 The DIEs reading must have careful ordering to:
85102364 25837 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25838 reading current DIE.
25839 * Not trying to dereference contents of still incompletely read in types
25840 while reading in other DIEs.
25841 * Enable referencing still incompletely read in types just by a pointer to
25842 the type without accessing its fields.
25843
25844 Therefore caller should follow these rules:
25845 * Try to fetch any prerequisite types we may need to build this DIE type
25846 before building the type and calling set_die_type.
e71ec853 25847 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25848 possible before fetching more types to complete the current type.
25849 * Make the type as complete as possible before fetching more types. */
1c379e20 25850
f792889a 25851static struct type *
1c379e20
DJ
25852set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25853{
518817b3
SM
25854 struct dwarf2_per_objfile *dwarf2_per_objfile
25855 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25856 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25857 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25858 struct attribute *attr;
25859 struct dynamic_prop prop;
1c379e20 25860
b4ba55a1
JB
25861 /* For Ada types, make sure that the gnat-specific data is always
25862 initialized (if not already set). There are a few types where
25863 we should not be doing so, because the type-specific area is
25864 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25865 where the type-specific area is used to store the floatformat).
25866 But this is not a problem, because the gnat-specific information
25867 is actually not needed for these types. */
25868 if (need_gnat_info (cu)
25869 && TYPE_CODE (type) != TYPE_CODE_FUNC
25870 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25871 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25872 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25873 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25874 && !HAVE_GNAT_AUX_INFO (type))
25875 INIT_GNAT_SPECIFIC (type);
25876
3f2f83dd
KB
25877 /* Read DW_AT_allocated and set in type. */
25878 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25879 if (attr_form_is_block (attr))
25880 {
9a49df9d
AB
25881 struct type *prop_type
25882 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25883 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25884 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25885 }
25886 else if (attr != NULL)
25887 {
b98664d3 25888 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25889 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25890 sect_offset_str (die->sect_off));
3f2f83dd
KB
25891 }
25892
25893 /* Read DW_AT_associated and set in type. */
25894 attr = dwarf2_attr (die, DW_AT_associated, cu);
25895 if (attr_form_is_block (attr))
25896 {
9a49df9d
AB
25897 struct type *prop_type
25898 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25899 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25900 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25901 }
25902 else if (attr != NULL)
25903 {
b98664d3 25904 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25905 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25906 sect_offset_str (die->sect_off));
3f2f83dd
KB
25907 }
25908
3cdcd0ce
JB
25909 /* Read DW_AT_data_location and set in type. */
25910 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25911 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25912 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25913 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25914
dee91e82 25915 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25916 {
dee91e82
DE
25917 dwarf2_per_objfile->die_type_hash =
25918 htab_create_alloc_ex (127,
25919 per_cu_offset_and_type_hash,
25920 per_cu_offset_and_type_eq,
25921 NULL,
25922 &objfile->objfile_obstack,
25923 hashtab_obstack_allocate,
25924 dummy_obstack_deallocate);
f792889a 25925 }
1c379e20 25926
dee91e82 25927 ofs.per_cu = cu->per_cu;
9c541725 25928 ofs.sect_off = die->sect_off;
1c379e20 25929 ofs.type = type;
dee91e82
DE
25930 slot = (struct dwarf2_per_cu_offset_and_type **)
25931 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25932 if (*slot)
b98664d3 25933 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25934 sect_offset_str (die->sect_off));
8d749320
SM
25935 *slot = XOBNEW (&objfile->objfile_obstack,
25936 struct dwarf2_per_cu_offset_and_type);
1c379e20 25937 **slot = ofs;
f792889a 25938 return type;
1c379e20
DJ
25939}
25940
9c541725 25941/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25942 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25943
25944static struct type *
9c541725 25945get_die_type_at_offset (sect_offset sect_off,
673bfd45 25946 struct dwarf2_per_cu_data *per_cu)
1c379e20 25947{
dee91e82 25948 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25949 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25950
dee91e82 25951 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25952 return NULL;
1c379e20 25953
dee91e82 25954 ofs.per_cu = per_cu;
9c541725 25955 ofs.sect_off = sect_off;
9a3c8263
SM
25956 slot = ((struct dwarf2_per_cu_offset_and_type *)
25957 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25958 if (slot)
25959 return slot->type;
25960 else
25961 return NULL;
25962}
25963
02142a6c 25964/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25965 or return NULL if DIE does not have a saved type. */
25966
25967static struct type *
25968get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25969{
9c541725 25970 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25971}
25972
10b3939b
DJ
25973/* Add a dependence relationship from CU to REF_PER_CU. */
25974
25975static void
25976dwarf2_add_dependence (struct dwarf2_cu *cu,
25977 struct dwarf2_per_cu_data *ref_per_cu)
25978{
25979 void **slot;
25980
25981 if (cu->dependencies == NULL)
25982 cu->dependencies
25983 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25984 NULL, &cu->comp_unit_obstack,
25985 hashtab_obstack_allocate,
25986 dummy_obstack_deallocate);
25987
25988 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25989 if (*slot == NULL)
25990 *slot = ref_per_cu;
25991}
1c379e20 25992
f504f079
DE
25993/* Subroutine of dwarf2_mark to pass to htab_traverse.
25994 Set the mark field in every compilation unit in the
ae038cb0
DJ
25995 cache that we must keep because we are keeping CU. */
25996
10b3939b
DJ
25997static int
25998dwarf2_mark_helper (void **slot, void *data)
25999{
26000 struct dwarf2_per_cu_data *per_cu;
26001
26002 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26003
26004 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26005 reading of the chain. As such dependencies remain valid it is not much
26006 useful to track and undo them during QUIT cleanups. */
26007 if (per_cu->cu == NULL)
26008 return 1;
26009
10b3939b
DJ
26010 if (per_cu->cu->mark)
26011 return 1;
9068261f 26012 per_cu->cu->mark = true;
10b3939b
DJ
26013
26014 if (per_cu->cu->dependencies != NULL)
26015 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26016
26017 return 1;
26018}
26019
f504f079
DE
26020/* Set the mark field in CU and in every other compilation unit in the
26021 cache that we must keep because we are keeping CU. */
26022
ae038cb0
DJ
26023static void
26024dwarf2_mark (struct dwarf2_cu *cu)
26025{
26026 if (cu->mark)
26027 return;
9068261f 26028 cu->mark = true;
10b3939b
DJ
26029 if (cu->dependencies != NULL)
26030 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26031}
26032
26033static void
26034dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26035{
26036 while (per_cu)
26037 {
9068261f 26038 per_cu->cu->mark = false;
ae038cb0
DJ
26039 per_cu = per_cu->cu->read_in_chain;
26040 }
72bf9492
DJ
26041}
26042
72bf9492
DJ
26043/* Trivial hash function for partial_die_info: the hash value of a DIE
26044 is its offset in .debug_info for this objfile. */
26045
26046static hashval_t
26047partial_die_hash (const void *item)
26048{
9a3c8263
SM
26049 const struct partial_die_info *part_die
26050 = (const struct partial_die_info *) item;
9a619af0 26051
9c541725 26052 return to_underlying (part_die->sect_off);
72bf9492
DJ
26053}
26054
26055/* Trivial comparison function for partial_die_info structures: two DIEs
26056 are equal if they have the same offset. */
26057
26058static int
26059partial_die_eq (const void *item_lhs, const void *item_rhs)
26060{
9a3c8263
SM
26061 const struct partial_die_info *part_die_lhs
26062 = (const struct partial_die_info *) item_lhs;
26063 const struct partial_die_info *part_die_rhs
26064 = (const struct partial_die_info *) item_rhs;
9a619af0 26065
9c541725 26066 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26067}
26068
3c3bb058
AB
26069struct cmd_list_element *set_dwarf_cmdlist;
26070struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26071
26072static void
981a3fb3 26073set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26074{
b4f54984 26075 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26076 gdb_stdout);
ae038cb0
DJ
26077}
26078
26079static void
981a3fb3 26080show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26081{
b4f54984 26082 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26083}
26084
491144b5 26085bool dwarf_always_disassemble;
437afbb8 26086
437afbb8 26087static void
cd4fb1b2
SM
26088show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26089 struct cmd_list_element *c, const char *value)
9291a0cd 26090{
cd4fb1b2
SM
26091 fprintf_filtered (file,
26092 _("Whether to always disassemble "
26093 "DWARF expressions is %s.\n"),
26094 value);
9291a0cd
TT
26095}
26096
9291a0cd 26097static void
cd4fb1b2
SM
26098show_check_physname (struct ui_file *file, int from_tty,
26099 struct cmd_list_element *c, const char *value)
9291a0cd 26100{
cd4fb1b2
SM
26101 fprintf_filtered (file,
26102 _("Whether to check \"physname\" is %s.\n"),
26103 value);
9291a0cd
TT
26104}
26105
cd4fb1b2
SM
26106void
26107_initialize_dwarf2_read (void)
9291a0cd 26108{
cd4fb1b2
SM
26109 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26110Set DWARF specific variables.\n\
590042fc 26111Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26112 &set_dwarf_cmdlist, "maintenance set dwarf ",
26113 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26114
cd4fb1b2 26115 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26116Show DWARF specific variables.\n\
26117Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26118 &show_dwarf_cmdlist, "maintenance show dwarf ",
26119 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26120
cd4fb1b2
SM
26121 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26122 &dwarf_max_cache_age, _("\
26123Set the upper bound on the age of cached DWARF compilation units."), _("\
26124Show the upper bound on the age of cached DWARF compilation units."), _("\
26125A higher limit means that cached compilation units will be stored\n\
26126in memory longer, and more total memory will be used. Zero disables\n\
26127caching, which can slow down startup."),
26128 NULL,
26129 show_dwarf_max_cache_age,
26130 &set_dwarf_cmdlist,
26131 &show_dwarf_cmdlist);
156942c7 26132
cd4fb1b2
SM
26133 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26134 &dwarf_always_disassemble, _("\
26135Set whether `info address' always disassembles DWARF expressions."), _("\
26136Show whether `info address' always disassembles DWARF expressions."), _("\
26137When enabled, DWARF expressions are always printed in an assembly-like\n\
26138syntax. When disabled, expressions will be printed in a more\n\
26139conversational style, when possible."),
26140 NULL,
26141 show_dwarf_always_disassemble,
26142 &set_dwarf_cmdlist,
26143 &show_dwarf_cmdlist);
9291a0cd 26144
cd4fb1b2
SM
26145 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26146Set debugging of the DWARF reader."), _("\
26147Show debugging of the DWARF reader."), _("\
26148When enabled (non-zero), debugging messages are printed during DWARF\n\
26149reading and symtab expansion. A value of 1 (one) provides basic\n\
26150information. A value greater than 1 provides more verbose information."),
26151 NULL,
26152 NULL,
26153 &setdebuglist, &showdebuglist);
9291a0cd 26154
cd4fb1b2
SM
26155 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26156Set debugging of the DWARF DIE reader."), _("\
26157Show debugging of the DWARF DIE reader."), _("\
26158When enabled (non-zero), DIEs are dumped after they are read in.\n\
26159The value is the maximum depth to print."),
26160 NULL,
26161 NULL,
26162 &setdebuglist, &showdebuglist);
9291a0cd 26163
cd4fb1b2
SM
26164 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26165Set debugging of the dwarf line reader."), _("\
26166Show debugging of the dwarf line reader."), _("\
26167When enabled (non-zero), line number entries are dumped as they are read in.\n\
26168A value of 1 (one) provides basic information.\n\
26169A value greater than 1 provides more verbose information."),
26170 NULL,
26171 NULL,
26172 &setdebuglist, &showdebuglist);
437afbb8 26173
cd4fb1b2
SM
26174 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26175Set cross-checking of \"physname\" code against demangler."), _("\
26176Show cross-checking of \"physname\" code against demangler."), _("\
26177When enabled, GDB's internal \"physname\" code is checked against\n\
26178the demangler."),
26179 NULL, show_check_physname,
26180 &setdebuglist, &showdebuglist);
900e11f9 26181
e615022a
DE
26182 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26183 no_class, &use_deprecated_index_sections, _("\
26184Set whether to use deprecated gdb_index sections."), _("\
26185Show whether to use deprecated gdb_index sections."), _("\
26186When enabled, deprecated .gdb_index sections are used anyway.\n\
26187Normally they are ignored either because of a missing feature or\n\
26188performance issue.\n\
26189Warning: This option must be enabled before gdb reads the file."),
26190 NULL,
26191 NULL,
26192 &setlist, &showlist);
26193
f1e6e072
TT
26194 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26195 &dwarf2_locexpr_funcs);
26196 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26197 &dwarf2_loclist_funcs);
26198
26199 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26200 &dwarf2_block_frame_base_locexpr_funcs);
26201 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26202 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26203
26204#if GDB_SELF_TEST
26205 selftests::register_test ("dw2_expand_symtabs_matching",
26206 selftests::dw2_expand_symtabs_matching::run_test);
26207#endif
6502dd73 26208}
This page took 4.986153 seconds and 4 git commands to generate.