Merge branch 'master' into merge-job
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4e5106e6 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/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1344 but this would require a corresponding change in unpack_field_as_long
1345 and friends. */
1346static int bits_per_byte = 8;
1347
2ddeaf8a
TT
1348/* When reading a variant or variant part, we track a bit more
1349 information about the field, and store it in an object of this
1350 type. */
1351
1352struct variant_field
1353{
1354 /* If we see a DW_TAG_variant, then this will be the discriminant
1355 value. */
1356 ULONGEST discriminant_value;
1357 /* If we see a DW_TAG_variant, then this will be set if this is the
1358 default branch. */
1359 bool default_branch;
1360 /* While reading a DW_TAG_variant_part, this will be set if this
1361 field is the discriminant. */
1362 bool is_discriminant;
1363};
1364
52059ffd
TT
1365struct nextfield
1366{
be2daae6
TT
1367 int accessibility = 0;
1368 int virtuality = 0;
2ddeaf8a 1369 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1370 struct variant_field variant {};
1371 struct field field {};
52059ffd
TT
1372};
1373
1374struct fnfieldlist
1375{
be2daae6
TT
1376 const char *name = nullptr;
1377 std::vector<struct fn_field> fnfields;
52059ffd
TT
1378};
1379
c906108c
SS
1380/* The routines that read and process dies for a C struct or C++ class
1381 pass lists of data member fields and lists of member function fields
1382 in an instance of a field_info structure, as defined below. */
1383struct field_info
c5aa993b 1384 {
0963b4bd 1385 /* List of data member and baseclasses fields. */
be2daae6
TT
1386 std::vector<struct nextfield> fields;
1387 std::vector<struct nextfield> baseclasses;
c906108c 1388
7d0ccb61 1389 /* Number of fields (including baseclasses). */
be2daae6 1390 int nfields = 0;
c906108c 1391
85102364 1392 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1393 int non_public_fields = 0;
c906108c 1394
c5aa993b
JM
1395 /* Member function fieldlist array, contains name of possibly overloaded
1396 member function, number of overloaded member functions and a pointer
1397 to the head of the member function field chain. */
be2daae6 1398 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1399
1400 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1401 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1402 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1403
1404 /* Nested types defined by this class and the number of elements in this
1405 list. */
be2daae6 1406 std::vector<struct decl_field> nested_types_list;
c5aa993b 1407 };
c906108c 1408
10b3939b
DJ
1409/* One item on the queue of compilation units to read in full symbols
1410 for. */
1411struct dwarf2_queue_item
1412{
1413 struct dwarf2_per_cu_data *per_cu;
95554aad 1414 enum language pretend_language;
10b3939b
DJ
1415 struct dwarf2_queue_item *next;
1416};
1417
1418/* The current queue. */
1419static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1420
ae038cb0
DJ
1421/* Loaded secondary compilation units are kept in memory until they
1422 have not been referenced for the processing of this many
1423 compilation units. Set this to zero to disable caching. Cache
1424 sizes of up to at least twenty will improve startup time for
1425 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1426static int dwarf_max_cache_age = 5;
920d2a44 1427static void
b4f54984
DE
1428show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1429 struct cmd_list_element *c, const char *value)
920d2a44 1430{
3e43a32a 1431 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1432 "DWARF compilation units is %s.\n"),
920d2a44
AC
1433 value);
1434}
4390d890 1435\f
c906108c
SS
1436/* local function prototypes */
1437
a32a8923
DE
1438static const char *get_section_name (const struct dwarf2_section_info *);
1439
1440static const char *get_section_file_name (const struct dwarf2_section_info *);
1441
918dd910
JK
1442static void dwarf2_find_base_address (struct die_info *die,
1443 struct dwarf2_cu *cu);
1444
0018ea6f
DE
1445static struct partial_symtab *create_partial_symtab
1446 (struct dwarf2_per_cu_data *per_cu, const char *name);
1447
f1902523
JK
1448static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1449 const gdb_byte *info_ptr,
1450 struct die_info *type_unit_die,
1451 int has_children, void *data);
1452
ed2dc618
SM
1453static void dwarf2_build_psymtabs_hard
1454 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1455
72bf9492
DJ
1456static void scan_partial_symbols (struct partial_die_info *,
1457 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1458 int, struct dwarf2_cu *);
c906108c 1459
72bf9492
DJ
1460static void add_partial_symbol (struct partial_die_info *,
1461 struct dwarf2_cu *);
63d06c5c 1462
72bf9492
DJ
1463static void add_partial_namespace (struct partial_die_info *pdi,
1464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1465 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1466
5d7cb8df 1467static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1468 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1469 struct dwarf2_cu *cu);
1470
72bf9492
DJ
1471static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1472 struct dwarf2_cu *cu);
91c24f0a 1473
bc30ff58
JB
1474static void add_partial_subprogram (struct partial_die_info *pdi,
1475 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1476 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1477
257e7a09
YQ
1478static void dwarf2_read_symtab (struct partial_symtab *,
1479 struct objfile *);
c906108c 1480
a14ed312 1481static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1482
685af9cd 1483static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1484 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1485 sect_offset);
433df2d4 1486
d521ce57 1487static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1488
dee91e82 1489static struct partial_die_info *load_partial_dies
d521ce57 1490 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1491
fb816e8b
TV
1492/* A pair of partial_die_info and compilation unit. */
1493struct cu_partial_die_info
1494{
1495 /* The compilation unit of the partial_die_info. */
1496 struct dwarf2_cu *cu;
1497 /* A partial_die_info. */
1498 struct partial_die_info *pdi;
122cf0f2
AB
1499
1500 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1501 : cu (cu),
1502 pdi (pdi)
405feb71 1503 { /* Nothing. */ }
122cf0f2
AB
1504
1505private:
1506 cu_partial_die_info () = delete;
fb816e8b
TV
1507};
1508
122cf0f2
AB
1509static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1510 struct dwarf2_cu *);
72bf9492 1511
d521ce57
TT
1512static const gdb_byte *read_attribute (const struct die_reader_specs *,
1513 struct attribute *, struct attr_abbrev *,
1514 const gdb_byte *);
a8329558 1515
a1855c1d 1516static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1517
a1855c1d 1518static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1519
a1855c1d 1520static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1521
15f18d14
AT
1522/* Read the next three bytes (little-endian order) as an unsigned integer. */
1523static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1524
a1855c1d 1525static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1526
a1855c1d 1527static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1528
d521ce57 1529static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1530 unsigned int *);
c906108c 1531
d521ce57 1532static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1533
1534static LONGEST read_checked_initial_length_and_offset
d521ce57 1535 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1536 unsigned int *, unsigned int *);
613e1657 1537
d521ce57
TT
1538static LONGEST read_offset (bfd *, const gdb_byte *,
1539 const struct comp_unit_head *,
c764a876
DE
1540 unsigned int *);
1541
d521ce57 1542static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1543
ed2dc618
SM
1544static sect_offset read_abbrev_offset
1545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1546 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1547
d521ce57 1548static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1549
d521ce57 1550static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1551
ed2dc618
SM
1552static const char *read_indirect_string
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1554 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1555
ed2dc618
SM
1556static const char *read_indirect_line_string
1557 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1558 const struct comp_unit_head *, unsigned int *);
36586728 1559
ed2dc618
SM
1560static const char *read_indirect_string_at_offset
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1562 LONGEST str_offset);
927aa2e7 1563
ed2dc618
SM
1564static const char *read_indirect_string_from_dwz
1565 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1566
d521ce57 1567static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1568
d521ce57
TT
1569static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1570 const gdb_byte *,
3019eac3
DE
1571 unsigned int *);
1572
d521ce57 1573static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1574 ULONGEST str_index);
3019eac3 1575
e142c38c 1576static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1577
e142c38c
DJ
1578static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1579 struct dwarf2_cu *);
c906108c 1580
348e048f 1581static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1582 unsigned int);
348e048f 1583
7d45c7c3
KB
1584static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1585 struct dwarf2_cu *cu);
1586
a084a2a6
AT
1587static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1588
05cf31d1
JB
1589static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1590 struct dwarf2_cu *cu);
1591
e142c38c 1592static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1593
e142c38c 1594static struct die_info *die_specification (struct die_info *die,
f2f0e013 1595 struct dwarf2_cu **);
63d06c5c 1596
9c541725 1597static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1598 struct dwarf2_cu *cu);
debd256d 1599
f3f5162e 1600static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1601 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1602 CORE_ADDR, int decode_mapping);
c906108c 1603
804d2729
TT
1604static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1605 const char *);
c906108c 1606
a14ed312 1607static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1608 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1609
ff39bb5e 1610static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1611 struct dwarf2_cu *);
c906108c 1612
ff39bb5e 1613static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1614 struct type *type,
1615 const char *name,
1616 struct obstack *obstack,
12df843f 1617 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1618 const gdb_byte **bytes,
98bfdba5 1619 struct dwarf2_locexpr_baton **baton);
2df3850c 1620
e7c27a73 1621static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1622
b4ba55a1
JB
1623static int need_gnat_info (struct dwarf2_cu *);
1624
3e43a32a
MS
1625static struct type *die_descriptive_type (struct die_info *,
1626 struct dwarf2_cu *);
b4ba55a1
JB
1627
1628static void set_descriptive_type (struct type *, struct die_info *,
1629 struct dwarf2_cu *);
1630
e7c27a73
DJ
1631static struct type *die_containing_type (struct die_info *,
1632 struct dwarf2_cu *);
c906108c 1633
ff39bb5e 1634static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1635 struct dwarf2_cu *);
c906108c 1636
f792889a 1637static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1638
673bfd45
DE
1639static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1640
0d5cff50 1641static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1642
6e70227d 1643static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1644 const char *suffix, int physname,
1645 struct dwarf2_cu *cu);
63d06c5c 1646
e7c27a73 1647static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1648
348e048f
DE
1649static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1650
e7c27a73 1651static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1652
e7c27a73 1653static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1654
96408a79
SA
1655static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1656
71a3c369
TT
1657static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1658
ff013f42
JK
1659static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1660 struct dwarf2_cu *, struct partial_symtab *);
1661
3a2b436a 1662/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1663 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1664enum pc_bounds_kind
1665{
e385593e 1666 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1667 PC_BOUNDS_NOT_PRESENT,
1668
e385593e
JK
1669 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1670 were present but they do not form a valid range of PC addresses. */
1671 PC_BOUNDS_INVALID,
1672
3a2b436a
JK
1673 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1674 PC_BOUNDS_RANGES,
1675
1676 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1677 PC_BOUNDS_HIGH_LOW,
1678};
1679
1680static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1681 CORE_ADDR *, CORE_ADDR *,
1682 struct dwarf2_cu *,
1683 struct partial_symtab *);
c906108c 1684
fae299cd
DC
1685static void get_scope_pc_bounds (struct die_info *,
1686 CORE_ADDR *, CORE_ADDR *,
1687 struct dwarf2_cu *);
1688
801e3a5b
JB
1689static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1690 CORE_ADDR, struct dwarf2_cu *);
1691
a14ed312 1692static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1693 struct dwarf2_cu *);
c906108c 1694
a14ed312 1695static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1696 struct type *, struct dwarf2_cu *);
c906108c 1697
a14ed312 1698static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1699 struct die_info *, struct type *,
e7c27a73 1700 struct dwarf2_cu *);
c906108c 1701
a14ed312 1702static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1703 struct type *,
1704 struct dwarf2_cu *);
c906108c 1705
134d01f1 1706static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1707
e7c27a73 1708static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1709
e7c27a73 1710static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1711
5d7cb8df
JK
1712static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1713
804d2729 1714static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1715
27aa8d6a
SW
1716static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1717
74921315
KS
1718static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1719
f55ee35c
JK
1720static struct type *read_module_type (struct die_info *die,
1721 struct dwarf2_cu *cu);
1722
38d518c9 1723static const char *namespace_name (struct die_info *die,
e142c38c 1724 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1725
134d01f1 1726static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1727
e7c27a73 1728static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1729
6e70227d 1730static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1731 struct dwarf2_cu *);
1732
bf6af496 1733static struct die_info *read_die_and_siblings_1
d521ce57 1734 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1735 struct die_info *);
639d11d3 1736
dee91e82 1737static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1738 const gdb_byte *info_ptr,
1739 const gdb_byte **new_info_ptr,
639d11d3
DC
1740 struct die_info *parent);
1741
d521ce57
TT
1742static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1743 struct die_info **, const gdb_byte *,
1744 int *, int);
3019eac3 1745
d521ce57
TT
1746static const gdb_byte *read_full_die (const struct die_reader_specs *,
1747 struct die_info **, const gdb_byte *,
1748 int *);
93311388 1749
e7c27a73 1750static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1751
15d034d0
TT
1752static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1753 struct obstack *);
71c25dea 1754
15d034d0 1755static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1756
15d034d0 1757static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1758 struct die_info *die,
1759 struct dwarf2_cu *cu);
1760
ca69b9e6
DE
1761static const char *dwarf2_physname (const char *name, struct die_info *die,
1762 struct dwarf2_cu *cu);
1763
e142c38c 1764static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1765 struct dwarf2_cu **);
9219021c 1766
f39c6ffd 1767static const char *dwarf_tag_name (unsigned int);
c906108c 1768
f39c6ffd 1769static const char *dwarf_attr_name (unsigned int);
c906108c 1770
a084a2a6
AT
1771static const char *dwarf_unit_type_name (int unit_type);
1772
f39c6ffd 1773static const char *dwarf_form_name (unsigned int);
c906108c 1774
a121b7c1 1775static const char *dwarf_bool_name (unsigned int);
c906108c 1776
f39c6ffd 1777static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1778
f9aca02d 1779static struct die_info *sibling_die (struct die_info *);
c906108c 1780
d97bc12b
DE
1781static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1782
1783static void dump_die_for_error (struct die_info *);
1784
1785static void dump_die_1 (struct ui_file *, int level, int max_level,
1786 struct die_info *);
c906108c 1787
d97bc12b 1788/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1789
51545339 1790static void store_in_ref_table (struct die_info *,
10b3939b 1791 struct dwarf2_cu *);
c906108c 1792
ff39bb5e 1793static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1794
ff39bb5e 1795static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1796
348e048f 1797static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1798 const struct attribute *,
348e048f
DE
1799 struct dwarf2_cu **);
1800
10b3939b 1801static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1802 const struct attribute *,
f2f0e013 1803 struct dwarf2_cu **);
c906108c 1804
348e048f 1805static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1806 const struct attribute *,
348e048f
DE
1807 struct dwarf2_cu **);
1808
ac9ec31b
DE
1809static struct type *get_signatured_type (struct die_info *, ULONGEST,
1810 struct dwarf2_cu *);
1811
1812static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1813 const struct attribute *,
ac9ec31b
DE
1814 struct dwarf2_cu *);
1815
e5fe5e75 1816static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1817
52dc124a 1818static void read_signatured_type (struct signatured_type *);
348e048f 1819
63e43d3a
PMR
1820static int attr_to_dynamic_prop (const struct attribute *attr,
1821 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1822 struct dynamic_prop *prop, struct type *type);
63e43d3a 1823
c906108c
SS
1824/* memory allocation interface */
1825
7b5a2f43 1826static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1827
b60c80d6 1828static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1829
43f3e411 1830static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1831
6e5a29e1 1832static int attr_form_is_block (const struct attribute *);
8e19ed76 1833
6e5a29e1 1834static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1835
6e5a29e1 1836static int attr_form_is_constant (const struct attribute *);
3690dd37 1837
6e5a29e1 1838static int attr_form_is_ref (const struct attribute *);
7771576e 1839
8cf6f0b1
TT
1840static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1841 struct dwarf2_loclist_baton *baton,
ff39bb5e 1842 const struct attribute *attr);
8cf6f0b1 1843
ff39bb5e 1844static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1845 struct symbol *sym,
f1e6e072
TT
1846 struct dwarf2_cu *cu,
1847 int is_block);
4c2df51b 1848
d521ce57
TT
1849static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1850 const gdb_byte *info_ptr,
1851 struct abbrev_info *abbrev);
4bb7a0a7 1852
72bf9492
DJ
1853static hashval_t partial_die_hash (const void *item);
1854
1855static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1856
ae038cb0 1857static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1858 (sect_offset sect_off, unsigned int offset_in_dwz,
1859 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1860
9816fde3 1861static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1862 struct die_info *comp_unit_die,
1863 enum language pretend_language);
93311388 1864
ed2dc618 1865static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1866
dee91e82 1867static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1868
f792889a
DJ
1869static struct type *set_die_type (struct die_info *, struct type *,
1870 struct dwarf2_cu *);
1c379e20 1871
ed2dc618 1872static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1873
ed2dc618 1874static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1875
58f0c718 1876static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1877 enum language);
10b3939b 1878
95554aad
TT
1879static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1880 enum language);
10b3939b 1881
f4dc4d17
DE
1882static void process_full_type_unit (struct dwarf2_per_cu_data *,
1883 enum language);
1884
10b3939b
DJ
1885static void dwarf2_add_dependence (struct dwarf2_cu *,
1886 struct dwarf2_per_cu_data *);
1887
ae038cb0
DJ
1888static void dwarf2_mark (struct dwarf2_cu *);
1889
1890static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1891
b64f50a1 1892static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1893 struct dwarf2_per_cu_data *);
673bfd45 1894
f792889a 1895static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1896
95554aad
TT
1897static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1898 enum language pretend_language);
1899
ed2dc618 1900static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1901
9a49df9d
AB
1902static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1903static struct type *dwarf2_per_cu_addr_sized_int_type
1904 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1905static struct type *dwarf2_per_cu_int_type
1906 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1907 bool unsigned_p);
9a49df9d 1908
b303c6f6
AB
1909/* Class, the destructor of which frees all allocated queue entries. This
1910 will only have work to do if an error was thrown while processing the
1911 dwarf. If no error was thrown then the queue entries should have all
1912 been processed, and freed, as we went along. */
1913
1914class dwarf2_queue_guard
1915{
1916public:
1917 dwarf2_queue_guard () = default;
1918
1919 /* Free any entries remaining on the queue. There should only be
1920 entries left if we hit an error while processing the dwarf. */
1921 ~dwarf2_queue_guard ()
1922 {
1923 struct dwarf2_queue_item *item, *last;
1924
1925 item = dwarf2_queue;
1926 while (item)
1927 {
1928 /* Anything still marked queued is likely to be in an
1929 inconsistent state, so discard it. */
1930 if (item->per_cu->queued)
1931 {
1932 if (item->per_cu->cu != NULL)
1933 free_one_cached_comp_unit (item->per_cu);
1934 item->per_cu->queued = 0;
1935 }
1936
1937 last = item;
1938 item = item->next;
1939 xfree (last);
1940 }
1941
1942 dwarf2_queue = dwarf2_queue_tail = NULL;
1943 }
1944};
1945
d721ba37
PA
1946/* The return type of find_file_and_directory. Note, the enclosed
1947 string pointers are only valid while this object is valid. */
1948
1949struct file_and_directory
1950{
1951 /* The filename. This is never NULL. */
1952 const char *name;
1953
1954 /* The compilation directory. NULL if not known. If we needed to
1955 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1956 points directly to the DW_AT_comp_dir string attribute owned by
1957 the obstack that owns the DIE. */
1958 const char *comp_dir;
1959
1960 /* If we needed to build a new string for comp_dir, this is what
1961 owns the storage. */
1962 std::string comp_dir_storage;
1963};
1964
1965static file_and_directory find_file_and_directory (struct die_info *die,
1966 struct dwarf2_cu *cu);
9291a0cd
TT
1967
1968static char *file_full_name (int file, struct line_header *lh,
1969 const char *comp_dir);
1970
43988095
JK
1971/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1972enum class rcuh_kind { COMPILE, TYPE };
1973
d521ce57 1974static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1975 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1976 struct comp_unit_head *header,
36586728 1977 struct dwarf2_section_info *section,
d521ce57 1978 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1979 rcuh_kind section_kind);
36586728 1980
fd820528 1981static void init_cutu_and_read_dies
f4dc4d17 1982 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1983 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1984 die_reader_func_ftype *die_reader_func, void *data);
1985
dee91e82
DE
1986static void init_cutu_and_read_dies_simple
1987 (struct dwarf2_per_cu_data *this_cu,
1988 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1989
673bfd45 1990static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1991
3019eac3
DE
1992static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1993
57d63ce2 1994static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1995 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1996 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1997 ULONGEST signature, int is_debug_types);
a2ce51a0 1998
ed2dc618
SM
1999static struct dwp_file *get_dwp_file
2000 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2001
3019eac3 2002static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2003 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2004
2005static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2006 (struct signatured_type *, const char *, const char *);
3019eac3 2007
89e63ee4
DE
2008static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2009
263db9a1
TT
2010/* A unique pointer to a dwo_file. */
2011
51ac9db5 2012typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2013
ed2dc618 2014static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2015
1b80a9fa 2016static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2017
2018static void free_line_header_voidp (void *arg);
4390d890
DE
2019\f
2020/* Various complaints about symbol reading that don't abort the process. */
2021
2022static void
2023dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2024{
b98664d3 2025 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2026}
2027
2028static void
2029dwarf2_debug_line_missing_file_complaint (void)
2030{
b98664d3 2031 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2032}
2033
2034static void
2035dwarf2_debug_line_missing_end_sequence_complaint (void)
2036{
b98664d3 2037 complaint (_(".debug_line section has line "
4390d890
DE
2038 "program sequence without an end"));
2039}
2040
2041static void
2042dwarf2_complex_location_expr_complaint (void)
2043{
b98664d3 2044 complaint (_("location expression too complex"));
4390d890
DE
2045}
2046
2047static void
2048dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2049 int arg3)
2050{
b98664d3 2051 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2052 arg1, arg2, arg3);
2053}
2054
2055static void
2056dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2057{
b98664d3 2058 complaint (_("debug info runs off end of %s section"
4390d890 2059 " [in module %s]"),
a32a8923
DE
2060 get_section_name (section),
2061 get_section_file_name (section));
4390d890 2062}
1b80a9fa 2063
4390d890
DE
2064static void
2065dwarf2_macro_malformed_definition_complaint (const char *arg1)
2066{
b98664d3 2067 complaint (_("macro debug info contains a "
4390d890
DE
2068 "malformed macro definition:\n`%s'"),
2069 arg1);
2070}
2071
2072static void
2073dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2074{
b98664d3 2075 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2076 arg1, arg2);
2077}
527f3840
JK
2078
2079/* Hash function for line_header_hash. */
2080
2081static hashval_t
2082line_header_hash (const struct line_header *ofs)
2083{
9c541725 2084 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2085}
2086
2087/* Hash function for htab_create_alloc_ex for line_header_hash. */
2088
2089static hashval_t
2090line_header_hash_voidp (const void *item)
2091{
9a3c8263 2092 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2093
2094 return line_header_hash (ofs);
2095}
2096
2097/* Equality function for line_header_hash. */
2098
2099static int
2100line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2101{
9a3c8263
SM
2102 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2103 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2104
9c541725 2105 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2106 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2107}
2108
4390d890 2109\f
9291a0cd 2110
31aa7e4e
JB
2111/* Read the given attribute value as an address, taking the attribute's
2112 form into account. */
2113
2114static CORE_ADDR
2115attr_value_as_address (struct attribute *attr)
2116{
2117 CORE_ADDR addr;
2118
336d760d
AT
2119 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2120 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2121 {
2122 /* Aside from a few clearly defined exceptions, attributes that
2123 contain an address must always be in DW_FORM_addr form.
2124 Unfortunately, some compilers happen to be violating this
2125 requirement by encoding addresses using other forms, such
2126 as DW_FORM_data4 for example. For those broken compilers,
2127 we try to do our best, without any guarantee of success,
2128 to interpret the address correctly. It would also be nice
2129 to generate a complaint, but that would require us to maintain
2130 a list of legitimate cases where a non-address form is allowed,
2131 as well as update callers to pass in at least the CU's DWARF
2132 version. This is more overhead than what we're willing to
2133 expand for a pretty rare case. */
2134 addr = DW_UNSND (attr);
2135 }
2136 else
2137 addr = DW_ADDR (attr);
2138
2139 return addr;
2140}
2141
330cdd98
PA
2142/* See declaration. */
2143
2144dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2145 const dwarf2_debug_sections *names,
2146 bool can_copy_)
2147 : objfile (objfile_),
2148 can_copy (can_copy_)
330cdd98
PA
2149{
2150 if (names == NULL)
2151 names = &dwarf2_elf_names;
2152
2153 bfd *obfd = objfile->obfd;
2154
2155 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2156 locate_sections (obfd, sec, *names);
2157}
2158
2159dwarf2_per_objfile::~dwarf2_per_objfile ()
2160{
2161 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2162 free_cached_comp_units ();
2163
2164 if (quick_file_names_table)
2165 htab_delete (quick_file_names_table);
2166
2167 if (line_header_hash)
2168 htab_delete (line_header_hash);
2169
b76e467d 2170 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2171 per_cu->imported_symtabs_free ();
fc8e7e75 2172
b2bdb8cf 2173 for (signatured_type *sig_type : all_type_units)
ae640021 2174 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2175
330cdd98
PA
2176 /* Everything else should be on the objfile obstack. */
2177}
2178
2179/* See declaration. */
2180
2181void
2182dwarf2_per_objfile::free_cached_comp_units ()
2183{
2184 dwarf2_per_cu_data *per_cu = read_in_chain;
2185 dwarf2_per_cu_data **last_chain = &read_in_chain;
2186 while (per_cu != NULL)
2187 {
2188 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2189
fcd3b13d 2190 delete per_cu->cu;
330cdd98
PA
2191 *last_chain = next_cu;
2192 per_cu = next_cu;
2193 }
2194}
2195
11ed8cad
TT
2196/* A helper class that calls free_cached_comp_units on
2197 destruction. */
2198
2199class free_cached_comp_units
2200{
2201public:
2202
2203 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2204 : m_per_objfile (per_objfile)
2205 {
2206 }
2207
2208 ~free_cached_comp_units ()
2209 {
2210 m_per_objfile->free_cached_comp_units ();
2211 }
2212
2213 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2214
2215private:
2216
2217 dwarf2_per_objfile *m_per_objfile;
2218};
2219
c906108c 2220/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2221 information and return true if we have enough to do something.
2222 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2223 ELF names are used. CAN_COPY is true for formats where symbol
2224 interposition is possible and so symbol values must follow copy
2225 relocation rules. */
c906108c
SS
2226
2227int
251d32d9 2228dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2229 const struct dwarf2_debug_sections *names,
2230 bool can_copy)
c906108c 2231{
97cbe998
SDJ
2232 if (objfile->flags & OBJF_READNEVER)
2233 return 0;
2234
ed2dc618
SM
2235 struct dwarf2_per_objfile *dwarf2_per_objfile
2236 = get_dwarf2_per_objfile (objfile);
2237
2238 if (dwarf2_per_objfile == NULL)
5bfd760d 2239 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2240 names,
2241 can_copy);
5bfd760d 2242
73869dc2 2243 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2244 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2245 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2246 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2247}
2248
2249/* Return the containing section of virtual section SECTION. */
2250
2251static struct dwarf2_section_info *
2252get_containing_section (const struct dwarf2_section_info *section)
2253{
2254 gdb_assert (section->is_virtual);
2255 return section->s.containing_section;
c906108c
SS
2256}
2257
a32a8923
DE
2258/* Return the bfd owner of SECTION. */
2259
2260static struct bfd *
2261get_section_bfd_owner (const struct dwarf2_section_info *section)
2262{
73869dc2
DE
2263 if (section->is_virtual)
2264 {
2265 section = get_containing_section (section);
2266 gdb_assert (!section->is_virtual);
2267 }
049412e3 2268 return section->s.section->owner;
a32a8923
DE
2269}
2270
2271/* Return the bfd section of SECTION.
2272 Returns NULL if the section is not present. */
2273
2274static asection *
2275get_section_bfd_section (const struct dwarf2_section_info *section)
2276{
73869dc2
DE
2277 if (section->is_virtual)
2278 {
2279 section = get_containing_section (section);
2280 gdb_assert (!section->is_virtual);
2281 }
049412e3 2282 return section->s.section;
a32a8923
DE
2283}
2284
2285/* Return the name of SECTION. */
2286
2287static const char *
2288get_section_name (const struct dwarf2_section_info *section)
2289{
2290 asection *sectp = get_section_bfd_section (section);
2291
2292 gdb_assert (sectp != NULL);
fd361982 2293 return bfd_section_name (sectp);
a32a8923
DE
2294}
2295
2296/* Return the name of the file SECTION is in. */
2297
2298static const char *
2299get_section_file_name (const struct dwarf2_section_info *section)
2300{
2301 bfd *abfd = get_section_bfd_owner (section);
2302
2303 return bfd_get_filename (abfd);
2304}
2305
2306/* Return the id of SECTION.
2307 Returns 0 if SECTION doesn't exist. */
2308
2309static int
2310get_section_id (const struct dwarf2_section_info *section)
2311{
2312 asection *sectp = get_section_bfd_section (section);
2313
2314 if (sectp == NULL)
2315 return 0;
2316 return sectp->id;
2317}
2318
2319/* Return the flags of SECTION.
73869dc2 2320 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2321
2322static int
2323get_section_flags (const struct dwarf2_section_info *section)
2324{
2325 asection *sectp = get_section_bfd_section (section);
2326
2327 gdb_assert (sectp != NULL);
fd361982 2328 return bfd_section_flags (sectp);
a32a8923
DE
2329}
2330
251d32d9
TG
2331/* When loading sections, we look either for uncompressed section or for
2332 compressed section names. */
233a11ab
CS
2333
2334static int
251d32d9
TG
2335section_is_p (const char *section_name,
2336 const struct dwarf2_section_names *names)
233a11ab 2337{
251d32d9
TG
2338 if (names->normal != NULL
2339 && strcmp (section_name, names->normal) == 0)
2340 return 1;
2341 if (names->compressed != NULL
2342 && strcmp (section_name, names->compressed) == 0)
2343 return 1;
2344 return 0;
233a11ab
CS
2345}
2346
330cdd98 2347/* See declaration. */
c906108c 2348
330cdd98
PA
2349void
2350dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2351 const dwarf2_debug_sections &names)
c906108c 2352{
fd361982 2353 flagword aflag = bfd_section_flags (sectp);
251d32d9 2354
dc7650b8
JK
2355 if ((aflag & SEC_HAS_CONTENTS) == 0)
2356 {
2357 }
950b7495
KS
2358 else if (elf_section_data (sectp)->this_hdr.sh_size
2359 > bfd_get_file_size (abfd))
2360 {
2361 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2362 warning (_("Discarding section %s which has a section size (%s"
2363 ") larger than the file size [in module %s]"),
2364 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2365 bfd_get_filename (abfd));
2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.info))
c906108c 2368 {
330cdd98 2369 this->info.s.section = sectp;
fd361982 2370 this->info.size = bfd_section_size (sectp);
c906108c 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2373 {
330cdd98 2374 this->abbrev.s.section = sectp;
fd361982 2375 this->abbrev.size = bfd_section_size (sectp);
c906108c 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.line))
c906108c 2378 {
330cdd98 2379 this->line.s.section = sectp;
fd361982 2380 this->line.size = bfd_section_size (sectp);
c906108c 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.loc))
c906108c 2383 {
330cdd98 2384 this->loc.s.section = sectp;
fd361982 2385 this->loc.size = bfd_section_size (sectp);
c906108c 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.loclists))
43988095 2388 {
330cdd98 2389 this->loclists.s.section = sectp;
fd361982 2390 this->loclists.size = bfd_section_size (sectp);
43988095 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2393 {
330cdd98 2394 this->macinfo.s.section = sectp;
fd361982 2395 this->macinfo.size = bfd_section_size (sectp);
c906108c 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2398 {
330cdd98 2399 this->macro.s.section = sectp;
fd361982 2400 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.str))
c906108c 2403 {
330cdd98 2404 this->str.s.section = sectp;
fd361982 2405 this->str.size = bfd_section_size (sectp);
c906108c 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.line_str))
43988095 2408 {
330cdd98 2409 this->line_str.s.section = sectp;
fd361982 2410 this->line_str.size = bfd_section_size (sectp);
43988095 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2413 {
330cdd98 2414 this->addr.s.section = sectp;
fd361982 2415 this->addr.size = bfd_section_size (sectp);
3019eac3 2416 }
330cdd98 2417 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2418 {
330cdd98 2419 this->frame.s.section = sectp;
fd361982 2420 this->frame.size = bfd_section_size (sectp);
b6af0555 2421 }
330cdd98 2422 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2423 {
330cdd98 2424 this->eh_frame.s.section = sectp;
fd361982 2425 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2426 }
330cdd98 2427 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2428 {
330cdd98 2429 this->ranges.s.section = sectp;
fd361982 2430 this->ranges.size = bfd_section_size (sectp);
af34e669 2431 }
330cdd98 2432 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2433 {
330cdd98 2434 this->rnglists.s.section = sectp;
fd361982 2435 this->rnglists.size = bfd_section_size (sectp);
43988095 2436 }
330cdd98 2437 else if (section_is_p (sectp->name, &names.types))
348e048f 2438 {
8b70b953
TT
2439 struct dwarf2_section_info type_section;
2440
2441 memset (&type_section, 0, sizeof (type_section));
049412e3 2442 type_section.s.section = sectp;
fd361982 2443 type_section.size = bfd_section_size (sectp);
8b70b953 2444
fd5866f6 2445 this->types.push_back (type_section);
348e048f 2446 }
330cdd98 2447 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2448 {
330cdd98 2449 this->gdb_index.s.section = sectp;
fd361982 2450 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2451 }
927aa2e7
JK
2452 else if (section_is_p (sectp->name, &names.debug_names))
2453 {
2454 this->debug_names.s.section = sectp;
fd361982 2455 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2456 }
2457 else if (section_is_p (sectp->name, &names.debug_aranges))
2458 {
2459 this->debug_aranges.s.section = sectp;
fd361982 2460 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2461 }
dce234bc 2462
fd361982
AM
2463 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2464 && bfd_section_vma (sectp) == 0)
330cdd98 2465 this->has_section_at_zero = true;
c906108c
SS
2466}
2467
fceca515
DE
2468/* A helper function that decides whether a section is empty,
2469 or not present. */
9e0ac564
TT
2470
2471static int
19ac8c2e 2472dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2473{
73869dc2
DE
2474 if (section->is_virtual)
2475 return section->size == 0;
049412e3 2476 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2477}
2478
cd4fb1b2 2479/* See dwarf2read.h. */
c906108c 2480
cd4fb1b2
SM
2481void
2482dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2483{
a32a8923 2484 asection *sectp;
3019eac3 2485 bfd *abfd;
dce234bc 2486 gdb_byte *buf, *retbuf;
c906108c 2487
be391dca
TT
2488 if (info->readin)
2489 return;
dce234bc 2490 info->buffer = NULL;
dc4ccb6f 2491 info->readin = true;
188dd5d6 2492
9e0ac564 2493 if (dwarf2_section_empty_p (info))
dce234bc 2494 return;
c906108c 2495
a32a8923 2496 sectp = get_section_bfd_section (info);
3019eac3 2497
73869dc2
DE
2498 /* If this is a virtual section we need to read in the real one first. */
2499 if (info->is_virtual)
2500 {
2501 struct dwarf2_section_info *containing_section =
2502 get_containing_section (info);
2503
2504 gdb_assert (sectp != NULL);
2505 if ((sectp->flags & SEC_RELOC) != 0)
2506 {
2507 error (_("Dwarf Error: DWP format V2 with relocations is not"
2508 " supported in section %s [in module %s]"),
2509 get_section_name (info), get_section_file_name (info));
2510 }
2511 dwarf2_read_section (objfile, containing_section);
2512 /* Other code should have already caught virtual sections that don't
2513 fit. */
2514 gdb_assert (info->virtual_offset + info->size
2515 <= containing_section->size);
2516 /* If the real section is empty or there was a problem reading the
2517 section we shouldn't get here. */
2518 gdb_assert (containing_section->buffer != NULL);
2519 info->buffer = containing_section->buffer + info->virtual_offset;
2520 return;
2521 }
2522
4bf44c1c
TT
2523 /* If the section has relocations, we must read it ourselves.
2524 Otherwise we attach it to the BFD. */
2525 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2526 {
d521ce57 2527 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2528 return;
dce234bc 2529 }
dce234bc 2530
224c3ddb 2531 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2532 info->buffer = buf;
dce234bc
PP
2533
2534 /* When debugging .o files, we may need to apply relocations; see
2535 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2536 We never compress sections in .o files, so we only need to
2537 try this when the section is not compressed. */
ac8035ab 2538 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2539 if (retbuf != NULL)
2540 {
2541 info->buffer = retbuf;
2542 return;
2543 }
2544
a32a8923
DE
2545 abfd = get_section_bfd_owner (info);
2546 gdb_assert (abfd != NULL);
2547
dce234bc
PP
2548 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2549 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2550 {
2551 error (_("Dwarf Error: Can't read DWARF data"
2552 " in section %s [in module %s]"),
fd361982 2553 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2554 }
dce234bc
PP
2555}
2556
9e0ac564
TT
2557/* A helper function that returns the size of a section in a safe way.
2558 If you are positive that the section has been read before using the
2559 size, then it is safe to refer to the dwarf2_section_info object's
2560 "size" field directly. In other cases, you must call this
2561 function, because for compressed sections the size field is not set
2562 correctly until the section has been read. */
2563
2564static bfd_size_type
2565dwarf2_section_size (struct objfile *objfile,
2566 struct dwarf2_section_info *info)
2567{
2568 if (!info->readin)
2569 dwarf2_read_section (objfile, info);
2570 return info->size;
2571}
2572
dce234bc 2573/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2574 SECTION_NAME. */
af34e669 2575
dce234bc 2576void
3017a003
TG
2577dwarf2_get_section_info (struct objfile *objfile,
2578 enum dwarf2_section_enum sect,
d521ce57 2579 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2580 bfd_size_type *sizep)
2581{
5bfd760d 2582 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2583 struct dwarf2_section_info *info;
a3b2a86b
TT
2584
2585 /* We may see an objfile without any DWARF, in which case we just
2586 return nothing. */
2587 if (data == NULL)
2588 {
2589 *sectp = NULL;
2590 *bufp = NULL;
2591 *sizep = 0;
2592 return;
2593 }
3017a003
TG
2594 switch (sect)
2595 {
2596 case DWARF2_DEBUG_FRAME:
2597 info = &data->frame;
2598 break;
2599 case DWARF2_EH_FRAME:
2600 info = &data->eh_frame;
2601 break;
2602 default:
2603 gdb_assert_not_reached ("unexpected section");
2604 }
dce234bc 2605
9e0ac564 2606 dwarf2_read_section (objfile, info);
dce234bc 2607
a32a8923 2608 *sectp = get_section_bfd_section (info);
dce234bc
PP
2609 *bufp = info->buffer;
2610 *sizep = info->size;
2611}
2612
36586728
TT
2613/* A helper function to find the sections for a .dwz file. */
2614
2615static void
2616locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2617{
9a3c8263 2618 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2619
2620 /* Note that we only support the standard ELF names, because .dwz
2621 is ELF-only (at the time of writing). */
2622 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2623 {
049412e3 2624 dwz_file->abbrev.s.section = sectp;
fd361982 2625 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2626 }
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2628 {
049412e3 2629 dwz_file->info.s.section = sectp;
fd361982 2630 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2633 {
049412e3 2634 dwz_file->str.s.section = sectp;
fd361982 2635 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2636 }
2637 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2638 {
049412e3 2639 dwz_file->line.s.section = sectp;
fd361982 2640 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2641 }
2642 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2643 {
049412e3 2644 dwz_file->macro.s.section = sectp;
fd361982 2645 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2646 }
2ec9a5e0
TT
2647 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2648 {
049412e3 2649 dwz_file->gdb_index.s.section = sectp;
fd361982 2650 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2651 }
927aa2e7
JK
2652 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2653 {
2654 dwz_file->debug_names.s.section = sectp;
fd361982 2655 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2656 }
36586728
TT
2657}
2658
c4973306 2659/* See dwarf2read.h. */
36586728 2660
c4973306 2661struct dwz_file *
ed2dc618 2662dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2663{
36586728 2664 const char *filename;
acd13123 2665 bfd_size_type buildid_len_arg;
dc294be5
TT
2666 size_t buildid_len;
2667 bfd_byte *buildid;
36586728
TT
2668
2669 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2670 return dwarf2_per_objfile->dwz_file.get ();
36586728 2671
4db1a1dc 2672 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2673 gdb::unique_xmalloc_ptr<char> data
2674 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2675 &buildid_len_arg, &buildid));
4db1a1dc
TT
2676 if (data == NULL)
2677 {
2678 if (bfd_get_error () == bfd_error_no_error)
2679 return NULL;
2680 error (_("could not read '.gnu_debugaltlink' section: %s"),
2681 bfd_errmsg (bfd_get_error ()));
2682 }
791afaa2
TT
2683
2684 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2685
acd13123
TT
2686 buildid_len = (size_t) buildid_len_arg;
2687
791afaa2 2688 filename = data.get ();
d721ba37
PA
2689
2690 std::string abs_storage;
36586728
TT
2691 if (!IS_ABSOLUTE_PATH (filename))
2692 {
14278e1f
TT
2693 gdb::unique_xmalloc_ptr<char> abs
2694 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2695
14278e1f 2696 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2697 filename = abs_storage.c_str ();
36586728
TT
2698 }
2699
dc294be5
TT
2700 /* First try the file name given in the section. If that doesn't
2701 work, try to use the build-id instead. */
192b62ce 2702 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2703 if (dwz_bfd != NULL)
36586728 2704 {
192b62ce 2705 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2706 dwz_bfd.reset (nullptr);
36586728
TT
2707 }
2708
dc294be5
TT
2709 if (dwz_bfd == NULL)
2710 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2711
2712 if (dwz_bfd == NULL)
2713 error (_("could not find '.gnu_debugaltlink' file for %s"),
2714 objfile_name (dwarf2_per_objfile->objfile));
2715
7ff8cb8c
TT
2716 std::unique_ptr<struct dwz_file> result
2717 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2718
7ff8cb8c
TT
2719 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2720 result.get ());
36586728 2721
7ff8cb8c
TT
2722 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2723 result->dwz_bfd.get ());
2724 dwarf2_per_objfile->dwz_file = std::move (result);
2725 return dwarf2_per_objfile->dwz_file.get ();
36586728 2726}
9291a0cd 2727\f
7b9f3c50
DE
2728/* DWARF quick_symbols_functions support. */
2729
2730/* TUs can share .debug_line entries, and there can be a lot more TUs than
2731 unique line tables, so we maintain a separate table of all .debug_line
2732 derived entries to support the sharing.
2733 All the quick functions need is the list of file names. We discard the
2734 line_header when we're done and don't need to record it here. */
2735struct quick_file_names
2736{
094b34ac
DE
2737 /* The data used to construct the hash key. */
2738 struct stmt_list_hash hash;
7b9f3c50
DE
2739
2740 /* The number of entries in file_names, real_names. */
2741 unsigned int num_file_names;
2742
2743 /* The file names from the line table, after being run through
2744 file_full_name. */
2745 const char **file_names;
2746
2747 /* The file names from the line table after being run through
2748 gdb_realpath. These are computed lazily. */
2749 const char **real_names;
2750};
2751
2752/* When using the index (and thus not using psymtabs), each CU has an
2753 object of this type. This is used to hold information needed by
2754 the various "quick" methods. */
2755struct dwarf2_per_cu_quick_data
2756{
2757 /* The file table. This can be NULL if there was no file table
2758 or it's currently not read in.
2759 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2760 struct quick_file_names *file_names;
2761
2762 /* The corresponding symbol table. This is NULL if symbols for this
2763 CU have not yet been read. */
43f3e411 2764 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2765
2766 /* A temporary mark bit used when iterating over all CUs in
2767 expand_symtabs_matching. */
2768 unsigned int mark : 1;
2769
2770 /* True if we've tried to read the file table and found there isn't one.
2771 There will be no point in trying to read it again next time. */
2772 unsigned int no_file_data : 1;
2773};
2774
094b34ac
DE
2775/* Utility hash function for a stmt_list_hash. */
2776
2777static hashval_t
2778hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2779{
2780 hashval_t v = 0;
2781
2782 if (stmt_list_hash->dwo_unit != NULL)
2783 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2784 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2785 return v;
2786}
2787
2788/* Utility equality function for a stmt_list_hash. */
2789
2790static int
2791eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2792 const struct stmt_list_hash *rhs)
2793{
2794 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2795 return 0;
2796 if (lhs->dwo_unit != NULL
2797 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2798 return 0;
2799
9c541725 2800 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2801}
2802
7b9f3c50
DE
2803/* Hash function for a quick_file_names. */
2804
2805static hashval_t
2806hash_file_name_entry (const void *e)
2807{
9a3c8263
SM
2808 const struct quick_file_names *file_data
2809 = (const struct quick_file_names *) e;
7b9f3c50 2810
094b34ac 2811 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2812}
2813
2814/* Equality function for a quick_file_names. */
2815
2816static int
2817eq_file_name_entry (const void *a, const void *b)
2818{
9a3c8263
SM
2819 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2820 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2821
094b34ac 2822 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2823}
2824
2825/* Delete function for a quick_file_names. */
2826
2827static void
2828delete_file_name_entry (void *e)
2829{
9a3c8263 2830 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2831 int i;
2832
2833 for (i = 0; i < file_data->num_file_names; ++i)
2834 {
2835 xfree ((void*) file_data->file_names[i]);
2836 if (file_data->real_names)
2837 xfree ((void*) file_data->real_names[i]);
2838 }
2839
2840 /* The space for the struct itself lives on objfile_obstack,
2841 so we don't free it here. */
2842}
2843
2844/* Create a quick_file_names hash table. */
2845
2846static htab_t
2847create_quick_file_names_table (unsigned int nr_initial_entries)
2848{
2849 return htab_create_alloc (nr_initial_entries,
2850 hash_file_name_entry, eq_file_name_entry,
2851 delete_file_name_entry, xcalloc, xfree);
2852}
9291a0cd 2853
918dd910
JK
2854/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2855 have to be created afterwards. You should call age_cached_comp_units after
2856 processing PER_CU->CU. dw2_setup must have been already called. */
2857
2858static void
58f0c718 2859load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2860{
3019eac3 2861 if (per_cu->is_debug_types)
e5fe5e75 2862 load_full_type_unit (per_cu);
918dd910 2863 else
58f0c718 2864 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2865
cc12ce38
DE
2866 if (per_cu->cu == NULL)
2867 return; /* Dummy CU. */
2dc860c0
DE
2868
2869 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2870}
2871
a0f42c21 2872/* Read in the symbols for PER_CU. */
2fdf6df6 2873
9291a0cd 2874static void
58f0c718 2875dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2876{
ed2dc618 2877 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2878
f4dc4d17
DE
2879 /* Skip type_unit_groups, reading the type units they contain
2880 is handled elsewhere. */
2881 if (IS_TYPE_UNIT_GROUP (per_cu))
2882 return;
2883
b303c6f6
AB
2884 /* The destructor of dwarf2_queue_guard frees any entries left on
2885 the queue. After this point we're guaranteed to leave this function
2886 with the dwarf queue empty. */
2887 dwarf2_queue_guard q_guard;
9291a0cd 2888
95554aad 2889 if (dwarf2_per_objfile->using_index
43f3e411 2890 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2891 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2892 {
2893 queue_comp_unit (per_cu, language_minimal);
58f0c718 2894 load_cu (per_cu, skip_partial);
89e63ee4
DE
2895
2896 /* If we just loaded a CU from a DWO, and we're working with an index
2897 that may badly handle TUs, load all the TUs in that DWO as well.
2898 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2899 if (!per_cu->is_debug_types
cc12ce38 2900 && per_cu->cu != NULL
89e63ee4
DE
2901 && per_cu->cu->dwo_unit != NULL
2902 && dwarf2_per_objfile->index_table != NULL
2903 && dwarf2_per_objfile->index_table->version <= 7
2904 /* DWP files aren't supported yet. */
ed2dc618 2905 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2906 queue_and_load_all_dwo_tus (per_cu);
95554aad 2907 }
9291a0cd 2908
ed2dc618 2909 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2910
2911 /* Age the cache, releasing compilation units that have not
2912 been used recently. */
ed2dc618 2913 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2914}
2915
2916/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2917 the objfile from which this CU came. Returns the resulting symbol
2918 table. */
2fdf6df6 2919
43f3e411 2920static struct compunit_symtab *
58f0c718 2921dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2922{
ed2dc618
SM
2923 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2924
95554aad 2925 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2926 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2927 {
11ed8cad 2928 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2929 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2930 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2931 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2932 }
f194fefb 2933
43f3e411 2934 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2935}
2936
ff4c9fec 2937/* See declaration. */
f4dc4d17 2938
ff4c9fec
SM
2939dwarf2_per_cu_data *
2940dwarf2_per_objfile::get_cutu (int index)
2941{
b76e467d 2942 if (index >= this->all_comp_units.size ())
ff4c9fec 2943 {
b76e467d 2944 index -= this->all_comp_units.size ();
b2bdb8cf 2945 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2946 return &this->all_type_units[index]->per_cu;
2947 }
f4dc4d17 2948
ff4c9fec
SM
2949 return this->all_comp_units[index];
2950}
f4dc4d17 2951
ff4c9fec 2952/* See declaration. */
2fdf6df6 2953
ff4c9fec
SM
2954dwarf2_per_cu_data *
2955dwarf2_per_objfile::get_cu (int index)
1fd400ff 2956{
b76e467d 2957 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2958
ff4c9fec 2959 return this->all_comp_units[index];
f4dc4d17
DE
2960}
2961
ff4c9fec 2962/* See declaration. */
f4dc4d17 2963
ff4c9fec
SM
2964signatured_type *
2965dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2966{
b2bdb8cf 2967 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2968
ff4c9fec 2969 return this->all_type_units[index];
1fd400ff
TT
2970}
2971
4b514bc8
JK
2972/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2973 objfile_obstack, and constructed with the specified field
2974 values. */
2975
2976static dwarf2_per_cu_data *
ed2dc618 2977create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2978 struct dwarf2_section_info *section,
2979 int is_dwz,
2980 sect_offset sect_off, ULONGEST length)
2981{
ed2dc618 2982 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2983 dwarf2_per_cu_data *the_cu
2984 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2985 struct dwarf2_per_cu_data);
2986 the_cu->sect_off = sect_off;
2987 the_cu->length = length;
e3b94546 2988 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2989 the_cu->section = section;
2990 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2991 struct dwarf2_per_cu_quick_data);
2992 the_cu->is_dwz = is_dwz;
2993 return the_cu;
2994}
2995
2ec9a5e0
TT
2996/* A helper for create_cus_from_index that handles a given list of
2997 CUs. */
2fdf6df6 2998
74a0d9f6 2999static void
12359b5e 3000create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3001 const gdb_byte *cu_list, offset_type n_elements,
3002 struct dwarf2_section_info *section,
b76e467d 3003 int is_dwz)
9291a0cd 3004{
12359b5e 3005 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3006 {
74a0d9f6 3007 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3008
3009 sect_offset sect_off
3010 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3011 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3012 cu_list += 2 * 8;
3013
b76e467d 3014 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3015 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3016 sect_off, length);
b76e467d 3017 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3018 }
9291a0cd
TT
3019}
3020
2ec9a5e0 3021/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3022 the CU objects for this objfile. */
2ec9a5e0 3023
74a0d9f6 3024static void
12359b5e 3025create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3026 const gdb_byte *cu_list, offset_type cu_list_elements,
3027 const gdb_byte *dwz_list, offset_type dwz_elements)
3028{
b76e467d
SM
3029 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3030 dwarf2_per_objfile->all_comp_units.reserve
3031 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3032
12359b5e 3033 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3034 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3035
3036 if (dwz_elements == 0)
74a0d9f6 3037 return;
2ec9a5e0 3038
12359b5e
SM
3039 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3040 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3041 &dwz->info, 1);
2ec9a5e0
TT
3042}
3043
1fd400ff 3044/* Create the signatured type hash table from the index. */
673bfd45 3045
74a0d9f6 3046static void
12359b5e
SM
3047create_signatured_type_table_from_index
3048 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3049 struct dwarf2_section_info *section,
3050 const gdb_byte *bytes,
3051 offset_type elements)
1fd400ff 3052{
12359b5e 3053 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3054
b2bdb8cf
SM
3055 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3056 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3057
12359b5e 3058 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3059
12359b5e 3060 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3061 {
52dc124a 3062 struct signatured_type *sig_type;
9c541725 3063 ULONGEST signature;
1fd400ff 3064 void **slot;
9c541725 3065 cu_offset type_offset_in_tu;
1fd400ff 3066
74a0d9f6 3067 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3068 sect_offset sect_off
3069 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3070 type_offset_in_tu
3071 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3072 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3073 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3074 bytes += 3 * 8;
3075
52dc124a 3076 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3077 struct signatured_type);
52dc124a 3078 sig_type->signature = signature;
9c541725 3079 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3080 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3081 sig_type->per_cu.section = section;
9c541725 3082 sig_type->per_cu.sect_off = sect_off;
e3b94546 3083 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3084 sig_type->per_cu.v.quick
1fd400ff
TT
3085 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3086 struct dwarf2_per_cu_quick_data);
3087
52dc124a
DE
3088 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3089 *slot = sig_type;
1fd400ff 3090
b2bdb8cf 3091 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3092 }
3093
673bfd45 3094 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3095}
3096
927aa2e7
JK
3097/* Create the signatured type hash table from .debug_names. */
3098
3099static void
3100create_signatured_type_table_from_debug_names
ed2dc618 3101 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3102 const mapped_debug_names &map,
3103 struct dwarf2_section_info *section,
3104 struct dwarf2_section_info *abbrev_section)
3105{
ed2dc618
SM
3106 struct objfile *objfile = dwarf2_per_objfile->objfile;
3107
927aa2e7
JK
3108 dwarf2_read_section (objfile, section);
3109 dwarf2_read_section (objfile, abbrev_section);
3110
b2bdb8cf
SM
3111 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3112 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3113
3114 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3115
3116 for (uint32_t i = 0; i < map.tu_count; ++i)
3117 {
3118 struct signatured_type *sig_type;
927aa2e7 3119 void **slot;
927aa2e7
JK
3120
3121 sect_offset sect_off
3122 = (sect_offset) (extract_unsigned_integer
3123 (map.tu_table_reordered + i * map.offset_size,
3124 map.offset_size,
3125 map.dwarf5_byte_order));
3126
3127 comp_unit_head cu_header;
ed2dc618
SM
3128 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3129 abbrev_section,
927aa2e7
JK
3130 section->buffer + to_underlying (sect_off),
3131 rcuh_kind::TYPE);
3132
3133 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3134 struct signatured_type);
3135 sig_type->signature = cu_header.signature;
3136 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3137 sig_type->per_cu.is_debug_types = 1;
3138 sig_type->per_cu.section = section;
3139 sig_type->per_cu.sect_off = sect_off;
e3b94546 3140 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3141 sig_type->per_cu.v.quick
3142 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3143 struct dwarf2_per_cu_quick_data);
3144
3145 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3146 *slot = sig_type;
3147
b2bdb8cf 3148 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3149 }
3150
3151 dwarf2_per_objfile->signatured_types = sig_types_hash;
3152}
3153
9291a0cd
TT
3154/* Read the address map data from the mapped index, and use it to
3155 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3156
9291a0cd 3157static void
ed2dc618
SM
3158create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3159 struct mapped_index *index)
9291a0cd 3160{
ed2dc618 3161 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3162 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3163 const gdb_byte *iter, *end;
9291a0cd 3164 struct addrmap *mutable_map;
9291a0cd
TT
3165 CORE_ADDR baseaddr;
3166
8268c778
PA
3167 auto_obstack temp_obstack;
3168
9291a0cd
TT
3169 mutable_map = addrmap_create_mutable (&temp_obstack);
3170
f00a2de2
PA
3171 iter = index->address_table.data ();
3172 end = iter + index->address_table.size ();
9291a0cd 3173
6a053cb1 3174 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9291a0cd
TT
3175
3176 while (iter < end)
3177 {
3178 ULONGEST hi, lo, cu_index;
3179 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3180 iter += 8;
3181 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3182 iter += 8;
3183 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3184 iter += 4;
f652bce2 3185
24a55014 3186 if (lo > hi)
f652bce2 3187 {
b98664d3 3188 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3189 hex_string (lo), hex_string (hi));
24a55014 3190 continue;
f652bce2 3191 }
24a55014 3192
b76e467d 3193 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3194 {
b98664d3 3195 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3196 (unsigned) cu_index);
24a55014 3197 continue;
f652bce2 3198 }
24a55014 3199
79748972
TT
3200 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3201 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3202 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3203 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3204 }
3205
d320c2b5 3206 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3207 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3208}
3209
927aa2e7
JK
3210/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3211 populate the objfile's psymtabs_addrmap. */
3212
3213static void
ed2dc618 3214create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3215 struct dwarf2_section_info *section)
3216{
ed2dc618 3217 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3218 bfd *abfd = objfile->obfd;
3219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6a053cb1 3220 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
927aa2e7
JK
3221
3222 auto_obstack temp_obstack;
3223 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3224
3225 std::unordered_map<sect_offset,
3226 dwarf2_per_cu_data *,
3227 gdb::hash_enum<sect_offset>>
3228 debug_info_offset_to_per_cu;
b76e467d 3229 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3230 {
927aa2e7
JK
3231 const auto insertpair
3232 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3233 if (!insertpair.second)
3234 {
3235 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3236 "debug_info_offset %s, ignoring .debug_aranges."),
3237 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3238 return;
3239 }
3240 }
3241
3242 dwarf2_read_section (objfile, section);
3243
3244 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3245
3246 const gdb_byte *addr = section->buffer;
3247
3248 while (addr < section->buffer + section->size)
3249 {
3250 const gdb_byte *const entry_addr = addr;
3251 unsigned int bytes_read;
3252
3253 const LONGEST entry_length = read_initial_length (abfd, addr,
3254 &bytes_read);
3255 addr += bytes_read;
3256
3257 const gdb_byte *const entry_end = addr + entry_length;
3258 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3259 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3260 if (addr + entry_length > section->buffer + section->size)
3261 {
47e3f474 3262 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3263 "length %s exceeds section length %s, "
3264 "ignoring .debug_aranges."),
47e3f474
TV
3265 objfile_name (objfile),
3266 plongest (entry_addr - section->buffer),
927aa2e7
JK
3267 plongest (bytes_read + entry_length),
3268 pulongest (section->size));
3269 return;
3270 }
3271
3272 /* The version number. */
3273 const uint16_t version = read_2_bytes (abfd, addr);
3274 addr += 2;
3275 if (version != 2)
3276 {
47e3f474 3277 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3278 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3279 objfile_name (objfile),
3280 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3281 return;
3282 }
3283
3284 const uint64_t debug_info_offset
3285 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3286 addr += offset_size;
3287 const auto per_cu_it
3288 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3289 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3290 {
47e3f474 3291 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3292 "debug_info_offset %s does not exists, "
3293 "ignoring .debug_aranges."),
47e3f474
TV
3294 objfile_name (objfile),
3295 plongest (entry_addr - section->buffer),
927aa2e7
JK
3296 pulongest (debug_info_offset));
3297 return;
3298 }
3299 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3300
3301 const uint8_t address_size = *addr++;
3302 if (address_size < 1 || address_size > 8)
3303 {
47e3f474 3304 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3305 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3306 objfile_name (objfile),
3307 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3308 return;
3309 }
3310
3311 const uint8_t segment_selector_size = *addr++;
3312 if (segment_selector_size != 0)
3313 {
47e3f474 3314 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3315 "segment_selector_size %u is not supported, "
3316 "ignoring .debug_aranges."),
47e3f474
TV
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer),
927aa2e7
JK
3319 segment_selector_size);
3320 return;
3321 }
3322
3323 /* Must pad to an alignment boundary that is twice the address
3324 size. It is undocumented by the DWARF standard but GCC does
3325 use it. */
3326 for (size_t padding = ((-(addr - section->buffer))
3327 & (2 * address_size - 1));
3328 padding > 0; padding--)
3329 if (*addr++ != 0)
3330 {
47e3f474 3331 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3332 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer));
927aa2e7
JK
3335 return;
3336 }
3337
3338 for (;;)
3339 {
3340 if (addr + 2 * address_size > entry_end)
3341 {
47e3f474 3342 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3343 "address list is not properly terminated, "
3344 "ignoring .debug_aranges."),
47e3f474
TV
3345 objfile_name (objfile),
3346 plongest (entry_addr - section->buffer));
927aa2e7
JK
3347 return;
3348 }
3349 ULONGEST start = extract_unsigned_integer (addr, address_size,
3350 dwarf5_byte_order);
3351 addr += address_size;
3352 ULONGEST length = extract_unsigned_integer (addr, address_size,
3353 dwarf5_byte_order);
3354 addr += address_size;
3355 if (start == 0 && length == 0)
3356 break;
3357 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3358 {
3359 /* Symbol was eliminated due to a COMDAT group. */
3360 continue;
3361 }
3362 ULONGEST end = start + length;
79748972
TT
3363 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3364 - baseaddr);
3365 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3366 - baseaddr);
927aa2e7
JK
3367 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3368 }
3369 }
3370
d320c2b5 3371 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3372 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3373}
3374
9291a0cd
TT
3375/* Find a slot in the mapped index INDEX for the object named NAME.
3376 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3377 constant pool and return true. If NAME cannot be found, return
3378 false. */
2fdf6df6 3379
109483d9 3380static bool
9291a0cd
TT
3381find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3382 offset_type **vec_out)
3383{
0cf03b49 3384 offset_type hash;
9291a0cd 3385 offset_type slot, step;
559a7a62 3386 int (*cmp) (const char *, const char *);
9291a0cd 3387
791afaa2 3388 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3389 if (current_language->la_language == language_cplus
45280282
IB
3390 || current_language->la_language == language_fortran
3391 || current_language->la_language == language_d)
0cf03b49
JK
3392 {
3393 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3394 not contain any. */
a8719064 3395
72998fb3 3396 if (strchr (name, '(') != NULL)
0cf03b49 3397 {
109483d9 3398 without_params = cp_remove_params (name);
0cf03b49 3399
72998fb3 3400 if (without_params != NULL)
791afaa2 3401 name = without_params.get ();
0cf03b49
JK
3402 }
3403 }
3404
559a7a62 3405 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3406 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3407 simulate our NAME being searched is also lowercased. */
3408 hash = mapped_index_string_hash ((index->version == 4
3409 && case_sensitivity == case_sensitive_off
3410 ? 5 : index->version),
3411 name);
3412
f00a2de2
PA
3413 slot = hash & (index->symbol_table.size () - 1);
3414 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3415 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3416
3417 for (;;)
3418 {
9291a0cd 3419 const char *str;
f00a2de2
PA
3420
3421 const auto &bucket = index->symbol_table[slot];
3422 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3423 return false;
9291a0cd 3424
f00a2de2 3425 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3426 if (!cmp (name, str))
9291a0cd
TT
3427 {
3428 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3429 + MAYBE_SWAP (bucket.vec));
109483d9 3430 return true;
9291a0cd
TT
3431 }
3432
f00a2de2 3433 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3434 }
3435}
3436
4485a1c1
SM
3437/* A helper function that reads the .gdb_index from BUFFER and fills
3438 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3439 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3440 ok to use deprecated sections.
3441
3442 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3443 out parameters that are filled in with information about the CU and
3444 TU lists in the section.
3445
4485a1c1 3446 Returns true if all went well, false otherwise. */
2fdf6df6 3447
d33bc52e 3448static bool
4485a1c1
SM
3449read_gdb_index_from_buffer (struct objfile *objfile,
3450 const char *filename,
3451 bool deprecated_ok,
3452 gdb::array_view<const gdb_byte> buffer,
3453 struct mapped_index *map,
3454 const gdb_byte **cu_list,
3455 offset_type *cu_list_elements,
3456 const gdb_byte **types_list,
3457 offset_type *types_list_elements)
3458{
3459 const gdb_byte *addr = &buffer[0];
82430852 3460
9291a0cd 3461 /* Version check. */
4485a1c1 3462 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3463 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3464 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3465 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3466 indices. */
831adc1f 3467 if (version < 4)
481860b3
GB
3468 {
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3471 {
3472 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3473 filename);
481860b3
GB
3474 warning_printed = 1;
3475 }
3476 return 0;
3477 }
3478 /* Index version 4 uses a different hash function than index version
3479 5 and later.
3480
3481 Versions earlier than 6 did not emit psymbols for inlined
3482 functions. Using these files will cause GDB not to be able to
3483 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3484 indices unless the user has done
3485 "set use-deprecated-index-sections on". */
2ec9a5e0 3486 if (version < 6 && !deprecated_ok)
481860b3
GB
3487 {
3488 static int warning_printed = 0;
3489 if (!warning_printed)
3490 {
e615022a
DE
3491 warning (_("\
3492Skipping deprecated .gdb_index section in %s.\n\
3493Do \"set use-deprecated-index-sections on\" before the file is read\n\
3494to use the section anyway."),
2ec9a5e0 3495 filename);
481860b3
GB
3496 warning_printed = 1;
3497 }
3498 return 0;
3499 }
796a7ff8 3500 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3501 of the TU (for symbols coming from TUs),
3502 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3503 Plus gold-generated indices can have duplicate entries for global symbols,
3504 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3505 These are just performance bugs, and we can't distinguish gdb-generated
3506 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3507
481860b3 3508 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3509 longer backward compatible. */
796a7ff8 3510 if (version > 8)
594e8718 3511 return 0;
9291a0cd 3512
559a7a62 3513 map->version = version;
9291a0cd 3514
4485a1c1 3515 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3516
4485a1c1 3517 int i = 0;
2ec9a5e0
TT
3518 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3519 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3520 / 8);
1fd400ff
TT
3521 ++i;
3522
2ec9a5e0
TT
3523 *types_list = addr + MAYBE_SWAP (metadata[i]);
3524 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3525 - MAYBE_SWAP (metadata[i]))
3526 / 8);
987d643c 3527 ++i;
1fd400ff 3528
f00a2de2
PA
3529 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3530 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3531 map->address_table
3532 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3533 ++i;
3534
f00a2de2
PA
3535 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->symbol_table
3538 = gdb::array_view<mapped_index::symbol_table_slot>
3539 ((mapped_index::symbol_table_slot *) symbol_table,
3540 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3541
f00a2de2 3542 ++i;
f9d83a0b 3543 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3544
2ec9a5e0
TT
3545 return 1;
3546}
3547
4485a1c1
SM
3548/* Callback types for dwarf2_read_gdb_index. */
3549
3550typedef gdb::function_view
3551 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3552 get_gdb_index_contents_ftype;
3553typedef gdb::function_view
3554 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3555 get_gdb_index_contents_dwz_ftype;
3556
927aa2e7 3557/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3558 elements of all the CUs and return 1. Otherwise, return 0. */
3559
3560static int
4485a1c1
SM
3561dwarf2_read_gdb_index
3562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3563 get_gdb_index_contents_ftype get_gdb_index_contents,
3564 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3565{
2ec9a5e0
TT
3566 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3567 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3568 struct dwz_file *dwz;
12359b5e 3569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3570
4485a1c1
SM
3571 gdb::array_view<const gdb_byte> main_index_contents
3572 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3573
3574 if (main_index_contents.empty ())
3575 return 0;
3576
3063847f 3577 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3578 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3579 use_deprecated_index_sections,
3580 main_index_contents, map.get (), &cu_list,
3581 &cu_list_elements, &types_list,
3582 &types_list_elements))
2ec9a5e0
TT
3583 return 0;
3584
0fefef59 3585 /* Don't use the index if it's empty. */
3063847f 3586 if (map->symbol_table.empty ())
0fefef59
DE
3587 return 0;
3588
2ec9a5e0
TT
3589 /* If there is a .dwz file, read it so we can get its CU list as
3590 well. */
ed2dc618 3591 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3592 if (dwz != NULL)
2ec9a5e0 3593 {
2ec9a5e0
TT
3594 struct mapped_index dwz_map;
3595 const gdb_byte *dwz_types_ignore;
3596 offset_type dwz_types_elements_ignore;
3597
4485a1c1
SM
3598 gdb::array_view<const gdb_byte> dwz_index_content
3599 = get_gdb_index_contents_dwz (objfile, dwz);
3600
3601 if (dwz_index_content.empty ())
3602 return 0;
3603
3604 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3605 bfd_get_filename (dwz->dwz_bfd.get ()),
3606 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3607 &dwz_list, &dwz_list_elements,
3608 &dwz_types_ignore,
3609 &dwz_types_elements_ignore))
2ec9a5e0
TT
3610 {
3611 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3612 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3613 return 0;
3614 }
3615 }
3616
12359b5e
SM
3617 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3618 dwz_list, dwz_list_elements);
1fd400ff 3619
8b70b953
TT
3620 if (types_list_elements)
3621 {
8b70b953
TT
3622 /* We can only handle a single .debug_types when we have an
3623 index. */
fd5866f6 3624 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3625 return 0;
3626
fd5866f6 3627 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3628
12359b5e
SM
3629 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3630 types_list, types_list_elements);
8b70b953 3631 }
9291a0cd 3632
3063847f 3633 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3634
3063847f 3635 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3636 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3637 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3638 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3639
3640 return 1;
3641}
3642
dee91e82 3643/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3644
dee91e82
DE
3645static void
3646dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3647 const gdb_byte *info_ptr,
dee91e82
DE
3648 struct die_info *comp_unit_die,
3649 int has_children,
3650 void *data)
9291a0cd 3651{
dee91e82 3652 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3653 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3654 struct dwarf2_per_objfile *dwarf2_per_objfile
3655 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3656 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3657 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3658 struct attribute *attr;
7b9f3c50
DE
3659 void **slot;
3660 struct quick_file_names *qfn;
9291a0cd 3661
0186c6a7
DE
3662 gdb_assert (! this_cu->is_debug_types);
3663
07261596
TT
3664 /* Our callers never want to match partial units -- instead they
3665 will match the enclosing full CU. */
3666 if (comp_unit_die->tag == DW_TAG_partial_unit)
3667 {
3668 this_cu->v.quick->no_file_data = 1;
3669 return;
3670 }
3671
0186c6a7 3672 lh_cu = this_cu;
7b9f3c50 3673 slot = NULL;
dee91e82 3674
fff8551c 3675 line_header_up lh;
9c541725 3676 sect_offset line_offset {};
fff8551c 3677
dee91e82 3678 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3679 if (attr != nullptr)
9291a0cd 3680 {
7b9f3c50
DE
3681 struct quick_file_names find_entry;
3682
9c541725 3683 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3684
3685 /* We may have already read in this line header (TU line header sharing).
3686 If we have we're done. */
094b34ac 3687 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3688 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3689 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3690 &find_entry, INSERT);
3691 if (*slot != NULL)
3692 {
9a3c8263 3693 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3694 return;
7b9f3c50
DE
3695 }
3696
3019eac3 3697 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3698 }
3699 if (lh == NULL)
3700 {
094b34ac 3701 lh_cu->v.quick->no_file_data = 1;
dee91e82 3702 return;
9291a0cd
TT
3703 }
3704
8d749320 3705 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3706 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3707 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3708 gdb_assert (slot != NULL);
3709 *slot = qfn;
9291a0cd 3710
d721ba37 3711 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3712
aa391654
TT
3713 int offset = 0;
3714 if (strcmp (fnd.name, "<unknown>") != 0)
3715 ++offset;
3716
7ba99d21 3717 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3718 qfn->file_names =
aa391654
TT
3719 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3720 if (offset != 0)
3721 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3722 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3723 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3724 qfn->real_names = NULL;
9291a0cd 3725
094b34ac 3726 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3727}
3728
3729/* A helper for the "quick" functions which attempts to read the line
3730 table for THIS_CU. */
3731
3732static struct quick_file_names *
e4a48d9d 3733dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3734{
0186c6a7
DE
3735 /* This should never be called for TUs. */
3736 gdb_assert (! this_cu->is_debug_types);
3737 /* Nor type unit groups. */
3738 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3739
dee91e82
DE
3740 if (this_cu->v.quick->file_names != NULL)
3741 return this_cu->v.quick->file_names;
3742 /* If we know there is no line data, no point in looking again. */
3743 if (this_cu->v.quick->no_file_data)
3744 return NULL;
3745
0186c6a7 3746 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3747
3748 if (this_cu->v.quick->no_file_data)
3749 return NULL;
3750 return this_cu->v.quick->file_names;
9291a0cd
TT
3751}
3752
3753/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3754 real path for a given file name from the line table. */
2fdf6df6 3755
9291a0cd 3756static const char *
7b9f3c50
DE
3757dw2_get_real_path (struct objfile *objfile,
3758 struct quick_file_names *qfn, int index)
9291a0cd 3759{
7b9f3c50
DE
3760 if (qfn->real_names == NULL)
3761 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3762 qfn->num_file_names, const char *);
9291a0cd 3763
7b9f3c50 3764 if (qfn->real_names[index] == NULL)
14278e1f 3765 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3766
7b9f3c50 3767 return qfn->real_names[index];
9291a0cd
TT
3768}
3769
3770static struct symtab *
3771dw2_find_last_source_symtab (struct objfile *objfile)
3772{
ed2dc618
SM
3773 struct dwarf2_per_objfile *dwarf2_per_objfile
3774 = get_dwarf2_per_objfile (objfile);
b76e467d 3775 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3776 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3777
43f3e411
DE
3778 if (cust == NULL)
3779 return NULL;
ed2dc618 3780
43f3e411 3781 return compunit_primary_filetab (cust);
9291a0cd
TT
3782}
3783
7b9f3c50
DE
3784/* Traversal function for dw2_forget_cached_source_info. */
3785
3786static int
3787dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3788{
7b9f3c50 3789 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3790
7b9f3c50 3791 if (file_data->real_names)
9291a0cd 3792 {
7b9f3c50 3793 int i;
9291a0cd 3794
7b9f3c50 3795 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3796 {
7b9f3c50
DE
3797 xfree ((void*) file_data->real_names[i]);
3798 file_data->real_names[i] = NULL;
9291a0cd
TT
3799 }
3800 }
7b9f3c50
DE
3801
3802 return 1;
3803}
3804
3805static void
3806dw2_forget_cached_source_info (struct objfile *objfile)
3807{
ed2dc618
SM
3808 struct dwarf2_per_objfile *dwarf2_per_objfile
3809 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3810
3811 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3812 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3813}
3814
f8eba3c6
TT
3815/* Helper function for dw2_map_symtabs_matching_filename that expands
3816 the symtabs and calls the iterator. */
3817
3818static int
3819dw2_map_expand_apply (struct objfile *objfile,
3820 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3821 const char *name, const char *real_path,
14bc53a8 3822 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3823{
43f3e411 3824 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3825
3826 /* Don't visit already-expanded CUs. */
43f3e411 3827 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3828 return 0;
3829
3830 /* This may expand more than one symtab, and we want to iterate over
3831 all of them. */
58f0c718 3832 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3833
14bc53a8
PA
3834 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3835 last_made, callback);
f8eba3c6
TT
3836}
3837
3838/* Implementation of the map_symtabs_matching_filename method. */
3839
14bc53a8
PA
3840static bool
3841dw2_map_symtabs_matching_filename
3842 (struct objfile *objfile, const char *name, const char *real_path,
3843 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3844{
c011a4f4 3845 const char *name_basename = lbasename (name);
ed2dc618
SM
3846 struct dwarf2_per_objfile *dwarf2_per_objfile
3847 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3848
848e3e78
DE
3849 /* The rule is CUs specify all the files, including those used by
3850 any TU, so there's no need to scan TUs here. */
f4dc4d17 3851
b76e467d 3852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3853 {
3d7bb9d9 3854 /* We only need to look at symtabs not already expanded. */
43f3e411 3855 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3856 continue;
3857
b76e467d 3858 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3859 if (file_data == NULL)
9291a0cd
TT
3860 continue;
3861
b76e467d 3862 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3863 {
7b9f3c50 3864 const char *this_name = file_data->file_names[j];
da235a7c 3865 const char *this_real_name;
9291a0cd 3866
af529f8f 3867 if (compare_filenames_for_search (this_name, name))
9291a0cd 3868 {
f5b95b50 3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3870 callback))
3871 return true;
288e77a7 3872 continue;
4aac40c8 3873 }
9291a0cd 3874
c011a4f4
DE
3875 /* Before we invoke realpath, which can get expensive when many
3876 files are involved, do a quick comparison of the basenames. */
3877 if (! basenames_may_differ
3878 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3879 continue;
3880
da235a7c
JK
3881 this_real_name = dw2_get_real_path (objfile, file_data, j);
3882 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3883 {
da235a7c 3884 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3885 callback))
3886 return true;
288e77a7 3887 continue;
da235a7c 3888 }
9291a0cd 3889
da235a7c
JK
3890 if (real_path != NULL)
3891 {
af529f8f
JK
3892 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3893 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3894 if (this_real_name != NULL
af529f8f 3895 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3896 {
f5b95b50 3897 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3898 callback))
3899 return true;
288e77a7 3900 continue;
9291a0cd
TT
3901 }
3902 }
3903 }
3904 }
3905
14bc53a8 3906 return false;
9291a0cd
TT
3907}
3908
da51c347
DE
3909/* Struct used to manage iterating over all CUs looking for a symbol. */
3910
3911struct dw2_symtab_iterator
9291a0cd 3912{
ed2dc618
SM
3913 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3914 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3915 /* If set, only look for symbols that match that block. Valid values are
3916 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3917 gdb::optional<block_enum> block_index;
da51c347
DE
3918 /* The kind of symbol we're looking for. */
3919 domain_enum domain;
3920 /* The list of CUs from the index entry of the symbol,
3921 or NULL if not found. */
3922 offset_type *vec;
3923 /* The next element in VEC to look at. */
3924 int next;
3925 /* The number of elements in VEC, or zero if there is no match. */
3926 int length;
8943b874
DE
3927 /* Have we seen a global version of the symbol?
3928 If so we can ignore all further global instances.
3929 This is to work around gold/15646, inefficient gold-generated
3930 indices. */
3931 int global_seen;
da51c347 3932};
9291a0cd 3933
2b79f376 3934/* Initialize the index symtab iterator ITER. */
2fdf6df6 3935
9291a0cd 3936static void
da51c347 3937dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3938 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3939 gdb::optional<block_enum> block_index,
da51c347
DE
3940 domain_enum domain,
3941 const char *name)
3942{
ed2dc618 3943 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3944 iter->block_index = block_index;
3945 iter->domain = domain;
3946 iter->next = 0;
8943b874 3947 iter->global_seen = 0;
da51c347 3948
3063847f 3949 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3950
3951 /* index is NULL if OBJF_READNOW. */
3952 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3953 iter->length = MAYBE_SWAP (*iter->vec);
3954 else
3955 {
3956 iter->vec = NULL;
3957 iter->length = 0;
3958 }
3959}
3960
3961/* Return the next matching CU or NULL if there are no more. */
3962
3963static struct dwarf2_per_cu_data *
3964dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3965{
ed2dc618
SM
3966 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3967
da51c347
DE
3968 for ( ; iter->next < iter->length; ++iter->next)
3969 {
3970 offset_type cu_index_and_attrs =
3971 MAYBE_SWAP (iter->vec[iter->next + 1]);
3972 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3973 gdb_index_symbol_kind symbol_kind =
3974 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3975 /* Only check the symbol attributes if they're present.
3976 Indices prior to version 7 don't record them,
3977 and indices >= 7 may elide them for certain symbols
3978 (gold does this). */
3979 int attrs_valid =
ed2dc618 3980 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3981 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3982
3190f0c6 3983 /* Don't crash on bad data. */
b76e467d 3984 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3985 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3986 {
b98664d3 3987 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3988 " [in module %s]"),
3989 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3990 continue;
3991 }
3992
ff4c9fec 3993 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3994
da51c347 3995 /* Skip if already read in. */
43f3e411 3996 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3997 continue;
3998
8943b874
DE
3999 /* Check static vs global. */
4000 if (attrs_valid)
4001 {
2b79f376
SM
4002 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4003
4004 if (iter->block_index.has_value ())
4005 {
4006 bool want_static = *iter->block_index == STATIC_BLOCK;
4007
4008 if (is_static != want_static)
4009 continue;
4010 }
4011
8943b874
DE
4012 /* Work around gold/15646. */
4013 if (!is_static && iter->global_seen)
4014 continue;
4015 if (!is_static)
4016 iter->global_seen = 1;
4017 }
da51c347
DE
4018
4019 /* Only check the symbol's kind if it has one. */
4020 if (attrs_valid)
4021 {
4022 switch (iter->domain)
4023 {
4024 case VAR_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4026 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4027 /* Some types are also in VAR_DOMAIN. */
4028 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4029 continue;
4030 break;
4031 case STRUCT_DOMAIN:
4032 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4033 continue;
4034 break;
4035 case LABEL_DOMAIN:
4036 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4037 continue;
4038 break;
59c35742
AB
4039 case MODULE_DOMAIN:
4040 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4041 continue;
4042 break;
da51c347
DE
4043 default:
4044 break;
4045 }
4046 }
4047
4048 ++iter->next;
4049 return per_cu;
4050 }
4051
4052 return NULL;
4053}
4054
43f3e411 4055static struct compunit_symtab *
c7f839cb 4056dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4057 const char *name, domain_enum domain)
9291a0cd 4058{
43f3e411 4059 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4060 struct dwarf2_per_objfile *dwarf2_per_objfile
4061 = get_dwarf2_per_objfile (objfile);
9291a0cd 4062
b5ec771e
PA
4063 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4064
ed2dc618
SM
4065 struct dw2_symtab_iterator iter;
4066 struct dwarf2_per_cu_data *per_cu;
da51c347 4067
2b79f376 4068 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4069
ed2dc618
SM
4070 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4071 {
4072 struct symbol *sym, *with_opaque = NULL;
58f0c718 4073 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4074 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4075 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4076
ed2dc618
SM
4077 sym = block_find_symbol (block, name, domain,
4078 block_find_non_opaque_type_preferred,
4079 &with_opaque);
b2e2f908 4080
ed2dc618
SM
4081 /* Some caution must be observed with overloaded functions
4082 and methods, since the index will not contain any overload
4083 information (but NAME might contain it). */
da51c347 4084
ed2dc618
SM
4085 if (sym != NULL
4086 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4087 return stab;
4088 if (with_opaque != NULL
4089 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4090 stab_best = stab;
da51c347 4091
ed2dc618 4092 /* Keep looking through other CUs. */
9291a0cd 4093 }
9291a0cd 4094
da51c347 4095 return stab_best;
9291a0cd
TT
4096}
4097
4098static void
4099dw2_print_stats (struct objfile *objfile)
4100{
ed2dc618
SM
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
b76e467d 4103 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4104 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4105 int count = 0;
9291a0cd 4106
ed2dc618 4107 for (int i = 0; i < total; ++i)
9291a0cd 4108 {
ff4c9fec 4109 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4110
43f3e411 4111 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4112 ++count;
4113 }
e4a48d9d 4114 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4115 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4116}
4117
779bd270
DE
4118/* This dumps minimal information about the index.
4119 It is called via "mt print objfiles".
4120 One use is to verify .gdb_index has been loaded by the
4121 gdb.dwarf2/gdb-index.exp testcase. */
4122
9291a0cd
TT
4123static void
4124dw2_dump (struct objfile *objfile)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128
779bd270
DE
4129 gdb_assert (dwarf2_per_objfile->using_index);
4130 printf_filtered (".gdb_index:");
4131 if (dwarf2_per_objfile->index_table != NULL)
4132 {
4133 printf_filtered (" version %d\n",
4134 dwarf2_per_objfile->index_table->version);
4135 }
4136 else
4137 printf_filtered (" faked for \"readnow\"\n");
4138 printf_filtered ("\n");
9291a0cd
TT
4139}
4140
9291a0cd
TT
4141static void
4142dw2_expand_symtabs_for_function (struct objfile *objfile,
4143 const char *func_name)
4144{
ed2dc618
SM
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
da51c347 4147
ed2dc618
SM
4148 struct dw2_symtab_iterator iter;
4149 struct dwarf2_per_cu_data *per_cu;
da51c347 4150
2b79f376 4151 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4152
ed2dc618 4153 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4154 dw2_instantiate_symtab (per_cu, false);
da51c347 4155
9291a0cd
TT
4156}
4157
4158static void
4159dw2_expand_all_symtabs (struct objfile *objfile)
4160{
ed2dc618
SM
4161 struct dwarf2_per_objfile *dwarf2_per_objfile
4162 = get_dwarf2_per_objfile (objfile);
b76e467d 4163 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4164 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4165
ed2dc618 4166 for (int i = 0; i < total_units; ++i)
9291a0cd 4167 {
ff4c9fec 4168 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4169
58f0c718
TT
4170 /* We don't want to directly expand a partial CU, because if we
4171 read it with the wrong language, then assertion failures can
4172 be triggered later on. See PR symtab/23010. So, tell
4173 dw2_instantiate_symtab to skip partial CUs -- any important
4174 partial CU will be read via DW_TAG_imported_unit anyway. */
4175 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4176 }
4177}
4178
4179static void
652a8996
JK
4180dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4181 const char *fullname)
9291a0cd 4182{
ed2dc618
SM
4183 struct dwarf2_per_objfile *dwarf2_per_objfile
4184 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4185
4186 /* We don't need to consider type units here.
4187 This is only called for examining code, e.g. expand_line_sal.
4188 There can be an order of magnitude (or more) more type units
4189 than comp units, and we avoid them if we can. */
4190
b76e467d 4191 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4192 {
3d7bb9d9 4193 /* We only need to look at symtabs not already expanded. */
43f3e411 4194 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4195 continue;
4196
b76e467d 4197 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4198 if (file_data == NULL)
9291a0cd
TT
4199 continue;
4200
b76e467d 4201 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4202 {
652a8996
JK
4203 const char *this_fullname = file_data->file_names[j];
4204
4205 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4206 {
58f0c718 4207 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4208 break;
4209 }
4210 }
4211 }
4212}
4213
9291a0cd 4214static void
199b4314
TT
4215dw2_map_matching_symbols
4216 (struct objfile *objfile,
b054970d 4217 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4218 int global,
4219 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4220 symbol_compare_ftype *ordered_compare)
9291a0cd 4221{
40658b94 4222 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4223 current language is Ada for a non-Ada objfile using GNU index. As Ada
4224 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4225}
4226
e1ef7d7a
PA
4227/* Starting from a search name, return the string that finds the upper
4228 bound of all strings that start with SEARCH_NAME in a sorted name
4229 list. Returns the empty string to indicate that the upper bound is
4230 the end of the list. */
4231
4232static std::string
4233make_sort_after_prefix_name (const char *search_name)
4234{
4235 /* When looking to complete "func", we find the upper bound of all
4236 symbols that start with "func" by looking for where we'd insert
4237 the closest string that would follow "func" in lexicographical
4238 order. Usually, that's "func"-with-last-character-incremented,
4239 i.e. "fund". Mind non-ASCII characters, though. Usually those
4240 will be UTF-8 multi-byte sequences, but we can't be certain.
4241 Especially mind the 0xff character, which is a valid character in
4242 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4243 rule out compilers allowing it in identifiers. Note that
4244 conveniently, strcmp/strcasecmp are specified to compare
4245 characters interpreted as unsigned char. So what we do is treat
4246 the whole string as a base 256 number composed of a sequence of
4247 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4248 to 0, and carries 1 to the following more-significant position.
4249 If the very first character in SEARCH_NAME ends up incremented
4250 and carries/overflows, then the upper bound is the end of the
4251 list. The string after the empty string is also the empty
4252 string.
4253
4254 Some examples of this operation:
4255
4256 SEARCH_NAME => "+1" RESULT
4257
4258 "abc" => "abd"
4259 "ab\xff" => "ac"
4260 "\xff" "a" "\xff" => "\xff" "b"
4261 "\xff" => ""
4262 "\xff\xff" => ""
4263 "" => ""
4264
4265 Then, with these symbols for example:
4266
4267 func
4268 func1
4269 fund
4270
4271 completing "func" looks for symbols between "func" and
4272 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4273 which finds "func" and "func1", but not "fund".
4274
4275 And with:
4276
4277 funcÿ (Latin1 'ÿ' [0xff])
4278 funcÿ1
4279 fund
4280
4281 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4282 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4283
4284 And with:
4285
4286 ÿÿ (Latin1 'ÿ' [0xff])
4287 ÿÿ1
4288
4289 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4290 the end of the list.
4291 */
4292 std::string after = search_name;
4293 while (!after.empty () && (unsigned char) after.back () == 0xff)
4294 after.pop_back ();
4295 if (!after.empty ())
4296 after.back () = (unsigned char) after.back () + 1;
4297 return after;
4298}
4299
5c58de74 4300/* See declaration. */
61d96d7e 4301
5c58de74
PA
4302std::pair<std::vector<name_component>::const_iterator,
4303 std::vector<name_component>::const_iterator>
44ed8f3e 4304mapped_index_base::find_name_components_bounds
3b00ef10 4305 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4306{
5c58de74
PA
4307 auto *name_cmp
4308 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4309
3b00ef10
TT
4310 const char *lang_name
4311 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4312
3f563c84
PA
4313 /* Comparison function object for lower_bound that matches against a
4314 given symbol name. */
4315 auto lookup_compare_lower = [&] (const name_component &elem,
4316 const char *name)
4317 {
5c58de74 4318 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4319 const char *elem_name = elem_qualified + elem.name_offset;
4320 return name_cmp (elem_name, name) < 0;
4321 };
4322
4323 /* Comparison function object for upper_bound that matches against a
4324 given symbol name. */
4325 auto lookup_compare_upper = [&] (const char *name,
4326 const name_component &elem)
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 (name, elem_name) < 0;
4331 };
4332
5c58de74
PA
4333 auto begin = this->name_components.begin ();
4334 auto end = this->name_components.end ();
3f563c84
PA
4335
4336 /* Find the lower bound. */
4337 auto lower = [&] ()
4338 {
3b00ef10 4339 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4340 return begin;
4341 else
3b00ef10 4342 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4343 } ();
4344
4345 /* Find the upper bound. */
4346 auto upper = [&] ()
4347 {
5c58de74 4348 if (lookup_name_without_params.completion_mode ())
3f563c84 4349 {
e1ef7d7a
PA
4350 /* In completion mode, we want UPPER to point past all
4351 symbols names that have the same prefix. I.e., with
4352 these symbols, and completing "func":
4353
4354 function << lower bound
4355 function1
4356 other_function << upper bound
4357
4358 We find the upper bound by looking for the insertion
4359 point of "func"-with-last-character-incremented,
4360 i.e. "fund". */
3b00ef10 4361 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4362 if (after.empty ())
3f563c84 4363 return end;
e6b2f5ef
PA
4364 return std::lower_bound (lower, end, after.c_str (),
4365 lookup_compare_lower);
3f563c84
PA
4366 }
4367 else
3b00ef10 4368 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4369 } ();
4370
5c58de74
PA
4371 return {lower, upper};
4372}
4373
4374/* See declaration. */
4375
4376void
44ed8f3e 4377mapped_index_base::build_name_components ()
5c58de74
PA
4378{
4379 if (!this->name_components.empty ())
4380 return;
4381
4382 this->name_components_casing = case_sensitivity;
4383 auto *name_cmp
4384 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4385
4386 /* The code below only knows how to break apart components of C++
4387 symbol names (and other languages that use '::' as
3b00ef10 4388 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4389 auto count = this->symbol_name_count ();
4390 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4391 {
44ed8f3e 4392 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4393 continue;
4394
4395 const char *name = this->symbol_name_at (idx);
4396
4397 /* Add each name component to the name component table. */
4398 unsigned int previous_len = 0;
3b00ef10
TT
4399
4400 if (strstr (name, "::") != nullptr)
4401 {
4402 for (unsigned int current_len = cp_find_first_component (name);
4403 name[current_len] != '\0';
4404 current_len += cp_find_first_component (name + current_len))
4405 {
4406 gdb_assert (name[current_len] == ':');
4407 this->name_components.push_back ({previous_len, idx});
4408 /* Skip the '::'. */
4409 current_len += 2;
4410 previous_len = current_len;
4411 }
4412 }
4413 else
5c58de74 4414 {
3b00ef10
TT
4415 /* Handle the Ada encoded (aka mangled) form here. */
4416 for (const char *iter = strstr (name, "__");
4417 iter != nullptr;
4418 iter = strstr (iter, "__"))
4419 {
4420 this->name_components.push_back ({previous_len, idx});
4421 iter += 2;
4422 previous_len = iter - name;
4423 }
5c58de74 4424 }
3b00ef10 4425
5c58de74
PA
4426 this->name_components.push_back ({previous_len, idx});
4427 }
4428
4429 /* Sort name_components elements by name. */
4430 auto name_comp_compare = [&] (const name_component &left,
4431 const name_component &right)
4432 {
4433 const char *left_qualified = this->symbol_name_at (left.idx);
4434 const char *right_qualified = this->symbol_name_at (right.idx);
4435
4436 const char *left_name = left_qualified + left.name_offset;
4437 const char *right_name = right_qualified + right.name_offset;
4438
4439 return name_cmp (left_name, right_name) < 0;
4440 };
4441
4442 std::sort (this->name_components.begin (),
4443 this->name_components.end (),
4444 name_comp_compare);
4445}
4446
4447/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4448 mapped_index_base instead of the containing objfile. This is split
4449 to a separate function in order to be able to unit test the
4450 name_components matching using a mock mapped_index_base. For each
5c58de74 4451 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4452 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4453
4454static void
4455dw2_expand_symtabs_matching_symbol
44ed8f3e 4456 (mapped_index_base &index,
5c58de74
PA
4457 const lookup_name_info &lookup_name_in,
4458 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4459 enum search_domain kind,
3b00ef10 4460 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4461{
4462 lookup_name_info lookup_name_without_params
4463 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4464
4465 /* Build the symbol name component sorted vector, if we haven't
4466 yet. */
4467 index.build_name_components ();
4468
3f563c84
PA
4469 /* The same symbol may appear more than once in the range though.
4470 E.g., if we're looking for symbols that complete "w", and we have
4471 a symbol named "w1::w2", we'll find the two name components for
4472 that same symbol in the range. To be sure we only call the
4473 callback once per symbol, we first collect the symbol name
4474 indexes that matched in a temporary vector and ignore
4475 duplicates. */
4476 std::vector<offset_type> matches;
3f563c84 4477
3b00ef10
TT
4478 struct name_and_matcher
4479 {
4480 symbol_name_matcher_ftype *matcher;
4481 const std::string &name;
4482
4483 bool operator== (const name_and_matcher &other) const
3f563c84 4484 {
3b00ef10
TT
4485 return matcher == other.matcher && name == other.name;
4486 }
4487 };
4488
4489 /* A vector holding all the different symbol name matchers, for all
4490 languages. */
4491 std::vector<name_and_matcher> matchers;
4492
4493 for (int i = 0; i < nr_languages; i++)
4494 {
4495 enum language lang_e = (enum language) i;
4496
4497 const language_defn *lang = language_def (lang_e);
4498 symbol_name_matcher_ftype *name_matcher
4499 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4500
3b00ef10
TT
4501 name_and_matcher key {
4502 name_matcher,
4503 lookup_name_without_params.language_lookup_name (lang_e)
4504 };
4505
4506 /* Don't insert the same comparison routine more than once.
4507 Note that we do this linear walk. This is not a problem in
4508 practice because the number of supported languages is
4509 low. */
4510 if (std::find (matchers.begin (), matchers.end (), key)
4511 != matchers.end ())
9291a0cd 4512 continue;
3b00ef10
TT
4513 matchers.push_back (std::move (key));
4514
4515 auto bounds
4516 = index.find_name_components_bounds (lookup_name_without_params,
4517 lang_e);
4518
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4521
4522 for (; bounds.first != bounds.second; ++bounds.first)
4523 {
4524 const char *qualified = index.symbol_name_at (bounds.first->idx);
4525
4526 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4527 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4528 continue;
9291a0cd 4529
3b00ef10
TT
4530 matches.push_back (bounds.first->idx);
4531 }
3f563c84
PA
4532 }
4533
4534 std::sort (matches.begin (), matches.end ());
4535
4536 /* Finally call the callback, once per match. */
4537 ULONGEST prev = -1;
4538 for (offset_type idx : matches)
4539 {
4540 if (prev != idx)
4541 {
3b00ef10
TT
4542 if (!match_callback (idx))
4543 break;
3f563c84
PA
4544 prev = idx;
4545 }
4546 }
4547
4548 /* Above we use a type wider than idx's for 'prev', since 0 and
4549 (offset_type)-1 are both possible values. */
4550 static_assert (sizeof (prev) > sizeof (offset_type), "");
4551}
4552
c62446b1
PA
4553#if GDB_SELF_TEST
4554
4555namespace selftests { namespace dw2_expand_symtabs_matching {
4556
a3c5fafd
PA
4557/* A mock .gdb_index/.debug_names-like name index table, enough to
4558 exercise dw2_expand_symtabs_matching_symbol, which works with the
4559 mapped_index_base interface. Builds an index from the symbol list
4560 passed as parameter to the constructor. */
4561class mock_mapped_index : public mapped_index_base
c62446b1
PA
4562{
4563public:
a3c5fafd
PA
4564 mock_mapped_index (gdb::array_view<const char *> symbols)
4565 : m_symbol_table (symbols)
c62446b1
PA
4566 {}
4567
a3c5fafd 4568 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4569
a3c5fafd 4570 /* Return the number of names in the symbol table. */
632e107b 4571 size_t symbol_name_count () const override
c62446b1 4572 {
a3c5fafd 4573 return m_symbol_table.size ();
c62446b1
PA
4574 }
4575
a3c5fafd 4576 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4577 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4578 {
4579 return m_symbol_table[idx];
4580 }
c62446b1 4581
a3c5fafd
PA
4582private:
4583 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4584};
4585
4586/* Convenience function that converts a NULL pointer to a "<null>"
4587 string, to pass to print routines. */
4588
4589static const char *
4590string_or_null (const char *str)
4591{
4592 return str != NULL ? str : "<null>";
4593}
4594
4595/* Check if a lookup_name_info built from
4596 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4597 index. EXPECTED_LIST is the list of expected matches, in expected
4598 matching order. If no match expected, then an empty list is
4599 specified. Returns true on success. On failure prints a warning
4600 indicating the file:line that failed, and returns false. */
4601
4602static bool
4603check_match (const char *file, int line,
4604 mock_mapped_index &mock_index,
4605 const char *name, symbol_name_match_type match_type,
4606 bool completion_mode,
4607 std::initializer_list<const char *> expected_list)
4608{
4609 lookup_name_info lookup_name (name, match_type, completion_mode);
4610
4611 bool matched = true;
4612
4613 auto mismatch = [&] (const char *expected_str,
4614 const char *got)
4615 {
4616 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4617 "expected=\"%s\", got=\"%s\"\n"),
4618 file, line,
4619 (match_type == symbol_name_match_type::FULL
4620 ? "FULL" : "WILD"),
4621 name, string_or_null (expected_str), string_or_null (got));
4622 matched = false;
4623 };
4624
4625 auto expected_it = expected_list.begin ();
4626 auto expected_end = expected_list.end ();
4627
a3c5fafd 4628 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4629 NULL, ALL_DOMAIN,
4630 [&] (offset_type idx)
4631 {
a3c5fafd 4632 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4633 const char *expected_str
4634 = expected_it == expected_end ? NULL : *expected_it++;
4635
4636 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4637 mismatch (expected_str, matched_name);
3b00ef10 4638 return true;
c62446b1
PA
4639 });
4640
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643 if (expected_str != NULL)
4644 mismatch (expected_str, NULL);
4645
4646 return matched;
4647}
4648
4649/* The symbols added to the mock mapped_index for testing (in
4650 canonical form). */
4651static const char *test_symbols[] = {
4652 "function",
4653 "std::bar",
4654 "std::zfunction",
4655 "std::zfunction2",
4656 "w1::w2",
4657 "ns::foo<char*>",
4658 "ns::foo<int>",
4659 "ns::foo<long>",
a20714ff
PA
4660 "ns2::tmpl<int>::foo2",
4661 "(anonymous namespace)::A::B::C",
c62446b1 4662
e1ef7d7a
PA
4663 /* These are used to check that the increment-last-char in the
4664 matching algorithm for completion doesn't match "t1_fund" when
4665 completing "t1_func". */
4666 "t1_func",
4667 "t1_func1",
4668 "t1_fund",
4669 "t1_fund1",
4670
4671 /* A UTF-8 name with multi-byte sequences to make sure that
4672 cp-name-parser understands this as a single identifier ("função"
4673 is "function" in PT). */
4674 u8"u8função",
4675
4676 /* \377 (0xff) is Latin1 'ÿ'. */
4677 "yfunc\377",
4678
4679 /* \377 (0xff) is Latin1 'ÿ'. */
4680 "\377",
4681 "\377\377123",
4682
c62446b1
PA
4683 /* A name with all sorts of complications. Starts with "z" to make
4684 it easier for the completion tests below. */
4685#define Z_SYM_NAME \
4686 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4687 "::tuple<(anonymous namespace)::ui*, " \
4688 "std::default_delete<(anonymous namespace)::ui>, void>"
4689
4690 Z_SYM_NAME
4691};
4692
a3c5fafd
PA
4693/* Returns true if the mapped_index_base::find_name_component_bounds
4694 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4695 in completion mode. */
5c58de74
PA
4696
4697static bool
a3c5fafd 4698check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4699 const char *search_name,
4700 gdb::array_view<const char *> expected_syms)
4701{
4702 lookup_name_info lookup_name (search_name,
4703 symbol_name_match_type::FULL, true);
4704
3b00ef10
TT
4705 auto bounds = index.find_name_components_bounds (lookup_name,
4706 language_cplus);
5c58de74
PA
4707
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4710 return false;
4711
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4713 {
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4717 return false;
4718 }
4719
4720 return true;
4721}
4722
4723/* Test the lower-level mapped_index::find_name_component_bounds
4724 method. */
4725
c62446b1 4726static void
5c58de74
PA
4727test_mapped_index_find_name_component_bounds ()
4728{
4729 mock_mapped_index mock_index (test_symbols);
4730
a3c5fafd 4731 mock_index.build_name_components ();
5c58de74
PA
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4735 {
4736 static const char *expected_syms[] = {
4737 "t1_func",
4738 "t1_func1",
5c58de74
PA
4739 };
4740
a3c5fafd 4741 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4742 "t1_func", expected_syms));
4743 }
4744
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4747 {
4748 static const char *expected_syms1[] = {
4749 "\377",
4750 "\377\377123",
4751 };
a3c5fafd 4752 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4753 "\377", expected_syms1));
4754
4755 static const char *expected_syms2[] = {
4756 "\377\377123",
4757 };
a3c5fafd 4758 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4759 "\377\377", expected_syms2));
4760 }
4761}
4762
4763/* Test dw2_expand_symtabs_matching_symbol. */
4764
4765static void
4766test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4767{
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 /* We let all tests run until the end even if some fails, for debug
4771 convenience. */
4772 bool any_mismatch = false;
4773
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777#define EXPECT(...) { __VA_ARGS__ }
4778
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4783 mock_index, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4785 EXPECTED_LIST)
4786
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4789 {
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4792 EXPECT (sym));
4793
4794 /* Should be able to match all existing symbols with
4795 parameters. */
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 {});
4811 }
4812
e1ef7d7a
PA
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4815 {
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4819 }
4820
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4823 {
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4827 }
4828
c62446b1
PA
4829 /* Check that completion mode works at each prefix of the expected
4830 symbol name. */
4831 {
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4834 std::string lookup;
4835
4836 for (size_t i = 1; i < len; i++)
4837 {
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4841 }
4842 }
4843
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4846 {
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4848 EXPECT ("w1::w2"));
a20714ff
PA
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4850 EXPECT ("w1::w2"));
c62446b1
PA
4851 }
4852
4853 /* Same, with a "complicated" symbol. */
4854 {
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4857 std::string lookup;
4858
4859 for (size_t i = 1; i < len; i++)
4860 {
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4864 }
4865 }
4866
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4868 {
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4870 {});
4871 }
4872
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4882 }
4883
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4886 {
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4889 EXPECT (expected));
a20714ff
PA
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4891 EXPECT (expected));
c62446b1
PA
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4896 {
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4901 {
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
a20714ff
PA
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
c62446b1
PA
4906
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
a20714ff
PA
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
c62446b1
PA
4911 }
4912 }
4913
4914 {
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4925 }
4926
4927 /* Test lookup names that don't match anything. */
4928 {
a20714ff
PA
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4930 {});
4931
c62446b1
PA
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4933 {});
4934 }
4935
a20714ff
PA
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4938 {
4939 static const char *syms[] = {
4940 "A::B::C",
4941 "B::C",
4942 "C",
4943 "A :: B :: C ( int )",
4944 "B :: C ( int )",
4945 "C ( int )",
4946 };
4947
4948 for (const char *s : syms)
4949 {
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4952 }
4953 }
4954
4955 {
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4958 EXPECT (expected));
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 }
4962
c62446b1
PA
4963 SELF_CHECK (!any_mismatch);
4964
4965#undef EXPECT
4966#undef CHECK_MATCH
4967}
4968
5c58de74
PA
4969static void
4970run_test ()
4971{
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4974}
4975
c62446b1
PA
4976}} // namespace selftests::dw2_expand_symtabs_matching
4977
4978#endif /* GDB_SELF_TEST */
4979
4b514bc8
JK
4980/* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4984
4985static void
4986dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4990{
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4992 {
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4995
58f0c718 4996 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4997
4998 if (expansion_notify != NULL
4999 && symtab_was_null
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5002 }
5003}
5004
3f563c84
PA
5005/* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5008
5009static void
5010dw2_expand_marked_cus
ed2dc618 5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5014 search_domain kind)
5015{
3f563c84
PA
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
ed2dc618 5018 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5019
61920122 5020 vec = (offset_type *) (index.constant_pool
f00a2de2 5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5024 {
61920122
PA
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5035 int attrs_valid =
5036 (index.version >= 7
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5038
5039 /* Work around gold/15646. */
5040 if (attrs_valid)
9291a0cd 5041 {
61920122
PA
5042 if (!is_static && global_seen)
5043 continue;
5044 if (!is_static)
5045 global_seen = true;
5046 }
3190f0c6 5047
61920122
PA
5048 /* Only check the symbol's kind if it has one. */
5049 if (attrs_valid)
5050 {
5051 switch (kind)
8943b874 5052 {
61920122
PA
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5055 continue;
5056 break;
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5059 continue;
61920122
PA
5060 break;
5061 case TYPES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5063 continue;
5064 break;
59c35742
AB
5065 case MODULES_DOMAIN:
5066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5067 continue;
5068 break;
61920122
PA
5069 default:
5070 break;
8943b874 5071 }
61920122 5072 }
8943b874 5073
61920122 5074 /* Don't crash on bad data. */
b76e467d 5075 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5076 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5077 {
b98664d3 5078 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5079 " [in module %s]"),
5080 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5081 continue;
5082 }
5083
ff4c9fec 5084 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5085 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5086 expansion_notify);
61920122
PA
5087 }
5088}
5089
4b514bc8
JK
5090/* If FILE_MATCHER is non-NULL, set all the
5091 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5092 that match FILE_MATCHER. */
5093
61920122 5094static void
4b514bc8 5095dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5096 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5097 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5098{
4b514bc8 5099 if (file_matcher == NULL)
61920122
PA
5100 return;
5101
4b514bc8
JK
5102 objfile *const objfile = dwarf2_per_objfile->objfile;
5103
5104 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5105 htab_eq_pointer,
5106 NULL, xcalloc, xfree));
5107 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5108 htab_eq_pointer,
5109 NULL, xcalloc, xfree));
61920122 5110
4b514bc8
JK
5111 /* The rule is CUs specify all the files, including those used by
5112 any TU, so there's no need to scan TUs here. */
61920122 5113
b76e467d 5114 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5115 {
927aa2e7
JK
5116 QUIT;
5117
5118 per_cu->v.quick->mark = 0;
5119
5120 /* We only need to look at symtabs not already expanded. */
5121 if (per_cu->v.quick->compunit_symtab)
5122 continue;
5123
b76e467d 5124 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5125 if (file_data == NULL)
5126 continue;
5127
5128 if (htab_find (visited_not_found.get (), file_data) != NULL)
5129 continue;
5130 else if (htab_find (visited_found.get (), file_data) != NULL)
5131 {
5132 per_cu->v.quick->mark = 1;
5133 continue;
5134 }
5135
b76e467d 5136 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5137 {
5138 const char *this_real_name;
5139
5140 if (file_matcher (file_data->file_names[j], false))
5141 {
5142 per_cu->v.quick->mark = 1;
5143 break;
5144 }
5145
5146 /* Before we invoke realpath, which can get expensive when many
5147 files are involved, do a quick comparison of the basenames. */
5148 if (!basenames_may_differ
5149 && !file_matcher (lbasename (file_data->file_names[j]),
5150 true))
5151 continue;
5152
5153 this_real_name = dw2_get_real_path (objfile, file_data, j);
5154 if (file_matcher (this_real_name, false))
5155 {
5156 per_cu->v.quick->mark = 1;
5157 break;
5158 }
5159 }
5160
b76e467d
SM
5161 void **slot = htab_find_slot (per_cu->v.quick->mark
5162 ? visited_found.get ()
5163 : visited_not_found.get (),
5164 file_data, INSERT);
927aa2e7
JK
5165 *slot = file_data;
5166 }
5167}
5168
5169static void
5170dw2_expand_symtabs_matching
5171 (struct objfile *objfile,
5172 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5173 const lookup_name_info &lookup_name,
5174 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5175 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5176 enum search_domain kind)
5177{
ed2dc618
SM
5178 struct dwarf2_per_objfile *dwarf2_per_objfile
5179 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5180
5181 /* index_table is NULL if OBJF_READNOW. */
5182 if (!dwarf2_per_objfile->index_table)
5183 return;
5184
ed2dc618 5185 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5186
5187 mapped_index &index = *dwarf2_per_objfile->index_table;
5188
5189 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5190 symbol_matcher,
5191 kind, [&] (offset_type idx)
5192 {
ed2dc618 5193 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5194 expansion_notify, kind);
3b00ef10 5195 return true;
927aa2e7
JK
5196 });
5197}
5198
5199/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5200 symtab. */
5201
5202static struct compunit_symtab *
5203recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5204 CORE_ADDR pc)
5205{
5206 int i;
5207
5208 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5209 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5210 return cust;
5211
5212 if (cust->includes == NULL)
5213 return NULL;
5214
5215 for (i = 0; cust->includes[i]; ++i)
5216 {
5217 struct compunit_symtab *s = cust->includes[i];
5218
5219 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5220 if (s != NULL)
5221 return s;
5222 }
5223
5224 return NULL;
5225}
5226
5227static struct compunit_symtab *
5228dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5229 struct bound_minimal_symbol msymbol,
5230 CORE_ADDR pc,
5231 struct obj_section *section,
5232 int warn_if_readin)
5233{
5234 struct dwarf2_per_cu_data *data;
5235 struct compunit_symtab *result;
5236
d320c2b5 5237 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5238 return NULL;
5239
6a053cb1 5240 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
d320c2b5
TT
5241 data = (struct dwarf2_per_cu_data *) addrmap_find
5242 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5243 if (!data)
5244 return NULL;
5245
5246 if (warn_if_readin && data->v.quick->compunit_symtab)
5247 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5248 paddress (get_objfile_arch (objfile), pc));
5249
5250 result
58f0c718
TT
5251 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5252 false),
927aa2e7
JK
5253 pc);
5254 gdb_assert (result != NULL);
5255 return result;
5256}
5257
5258static void
5259dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5260 void *data, int need_fullname)
5261{
ed2dc618
SM
5262 struct dwarf2_per_objfile *dwarf2_per_objfile
5263 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5264
5265 if (!dwarf2_per_objfile->filenames_cache)
5266 {
5267 dwarf2_per_objfile->filenames_cache.emplace ();
5268
5269 htab_up visited (htab_create_alloc (10,
5270 htab_hash_pointer, htab_eq_pointer,
5271 NULL, xcalloc, xfree));
5272
5273 /* The rule is CUs specify all the files, including those used
5274 by any TU, so there's no need to scan TUs here. We can
5275 ignore file names coming from already-expanded CUs. */
5276
b76e467d 5277 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5278 {
927aa2e7
JK
5279 if (per_cu->v.quick->compunit_symtab)
5280 {
5281 void **slot = htab_find_slot (visited.get (),
5282 per_cu->v.quick->file_names,
5283 INSERT);
5284
5285 *slot = per_cu->v.quick->file_names;
5286 }
5287 }
5288
b76e467d 5289 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5290 {
927aa2e7
JK
5291 /* We only need to look at symtabs not already expanded. */
5292 if (per_cu->v.quick->compunit_symtab)
5293 continue;
5294
b76e467d 5295 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5296 if (file_data == NULL)
5297 continue;
5298
b76e467d 5299 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5300 if (*slot)
5301 {
5302 /* Already visited. */
5303 continue;
5304 }
5305 *slot = file_data;
5306
5307 for (int j = 0; j < file_data->num_file_names; ++j)
5308 {
5309 const char *filename = file_data->file_names[j];
5310 dwarf2_per_objfile->filenames_cache->seen (filename);
5311 }
5312 }
5313 }
5314
5315 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5316 {
5317 gdb::unique_xmalloc_ptr<char> this_real_name;
5318
5319 if (need_fullname)
5320 this_real_name = gdb_realpath (filename);
5321 (*fun) (filename, this_real_name.get (), data);
5322 });
5323}
5324
5325static int
5326dw2_has_symbols (struct objfile *objfile)
5327{
5328 return 1;
5329}
5330
5331const struct quick_symbol_functions dwarf2_gdb_index_functions =
5332{
5333 dw2_has_symbols,
5334 dw2_find_last_source_symtab,
5335 dw2_forget_cached_source_info,
5336 dw2_map_symtabs_matching_filename,
5337 dw2_lookup_symbol,
5338 dw2_print_stats,
5339 dw2_dump,
927aa2e7
JK
5340 dw2_expand_symtabs_for_function,
5341 dw2_expand_all_symtabs,
5342 dw2_expand_symtabs_with_fullname,
5343 dw2_map_matching_symbols,
5344 dw2_expand_symtabs_matching,
5345 dw2_find_pc_sect_compunit_symtab,
5346 NULL,
5347 dw2_map_symbol_filenames
5348};
5349
5350/* DWARF-5 debug_names reader. */
5351
5352/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5353static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5354
5355/* A helper function that reads the .debug_names section in SECTION
5356 and fills in MAP. FILENAME is the name of the file containing the
5357 section; it is used for error reporting.
5358
5359 Returns true if all went well, false otherwise. */
5360
5361static bool
5362read_debug_names_from_section (struct objfile *objfile,
5363 const char *filename,
5364 struct dwarf2_section_info *section,
5365 mapped_debug_names &map)
5366{
5367 if (dwarf2_section_empty_p (section))
5368 return false;
5369
5370 /* Older elfutils strip versions could keep the section in the main
5371 executable while splitting it for the separate debug info file. */
5372 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5373 return false;
5374
5375 dwarf2_read_section (objfile, section);
5376
5377 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5378
5379 const gdb_byte *addr = section->buffer;
5380
5381 bfd *const abfd = get_section_bfd_owner (section);
5382
5383 unsigned int bytes_read;
5384 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5385 addr += bytes_read;
5386
5387 map.dwarf5_is_dwarf64 = bytes_read != 4;
5388 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5389 if (bytes_read + length != section->size)
5390 {
5391 /* There may be multiple per-CU indices. */
5392 warning (_("Section .debug_names in %s length %s does not match "
5393 "section length %s, ignoring .debug_names."),
5394 filename, plongest (bytes_read + length),
5395 pulongest (section->size));
5396 return false;
5397 }
5398
5399 /* The version number. */
5400 uint16_t version = read_2_bytes (abfd, addr);
5401 addr += 2;
5402 if (version != 5)
5403 {
5404 warning (_("Section .debug_names in %s has unsupported version %d, "
5405 "ignoring .debug_names."),
5406 filename, version);
5407 return false;
5408 }
5409
5410 /* Padding. */
5411 uint16_t padding = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (padding != 0)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported padding %d, "
5416 "ignoring .debug_names."),
5417 filename, padding);
5418 return false;
5419 }
5420
5421 /* comp_unit_count - The number of CUs in the CU list. */
5422 map.cu_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* local_type_unit_count - The number of TUs in the local TU
5426 list. */
5427 map.tu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429
5430 /* foreign_type_unit_count - The number of TUs in the foreign TU
5431 list. */
5432 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434 if (foreign_tu_count != 0)
5435 {
5436 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5437 "ignoring .debug_names."),
5438 filename, static_cast<unsigned long> (foreign_tu_count));
5439 return false;
5440 }
5441
5442 /* bucket_count - The number of hash buckets in the hash lookup
5443 table. */
5444 map.bucket_count = read_4_bytes (abfd, addr);
5445 addr += 4;
5446
5447 /* name_count - The number of unique names in the index. */
5448 map.name_count = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* abbrev_table_size - The size in bytes of the abbreviations
5452 table. */
5453 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5454 addr += 4;
5455
5456 /* augmentation_string_size - The size in bytes of the augmentation
5457 string. This value is rounded up to a multiple of 4. */
5458 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5459 addr += 4;
5460 map.augmentation_is_gdb = ((augmentation_string_size
5461 == sizeof (dwarf5_augmentation))
5462 && memcmp (addr, dwarf5_augmentation,
5463 sizeof (dwarf5_augmentation)) == 0);
5464 augmentation_string_size += (-augmentation_string_size) & 3;
5465 addr += augmentation_string_size;
5466
5467 /* List of CUs */
5468 map.cu_table_reordered = addr;
5469 addr += map.cu_count * map.offset_size;
5470
5471 /* List of Local TUs */
5472 map.tu_table_reordered = addr;
5473 addr += map.tu_count * map.offset_size;
5474
5475 /* Hash Lookup Table */
5476 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5477 addr += map.bucket_count * 4;
5478 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.name_count * 4;
5480
5481 /* Name Table */
5482 map.name_table_string_offs_reordered = addr;
5483 addr += map.name_count * map.offset_size;
5484 map.name_table_entry_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486
5487 const gdb_byte *abbrev_table_start = addr;
5488 for (;;)
5489 {
927aa2e7
JK
5490 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5491 addr += bytes_read;
5492 if (index_num == 0)
5493 break;
5494
5495 const auto insertpair
5496 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5497 if (!insertpair.second)
5498 {
5499 warning (_("Section .debug_names in %s has duplicate index %s, "
5500 "ignoring .debug_names."),
5501 filename, pulongest (index_num));
5502 return false;
5503 }
5504 mapped_debug_names::index_val &indexval = insertpair.first->second;
5505 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5506 addr += bytes_read;
5507
5508 for (;;)
5509 {
5510 mapped_debug_names::index_val::attr attr;
5511 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5512 addr += bytes_read;
5513 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5514 addr += bytes_read;
5515 if (attr.form == DW_FORM_implicit_const)
5516 {
5517 attr.implicit_const = read_signed_leb128 (abfd, addr,
5518 &bytes_read);
5519 addr += bytes_read;
5520 }
5521 if (attr.dw_idx == 0 && attr.form == 0)
5522 break;
5523 indexval.attr_vec.push_back (std::move (attr));
5524 }
5525 }
5526 if (addr != abbrev_table_start + abbrev_table_size)
5527 {
5528 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5529 "of size %s vs. written as %u, ignoring .debug_names."),
5530 filename, plongest (addr - abbrev_table_start),
5531 abbrev_table_size);
927aa2e7
JK
5532 return false;
5533 }
5534 map.entry_pool = addr;
5535
5536 return true;
5537}
5538
5539/* A helper for create_cus_from_debug_names that handles the MAP's CU
5540 list. */
5541
5542static void
ed2dc618 5543create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5544 const mapped_debug_names &map,
5545 dwarf2_section_info &section,
b76e467d 5546 bool is_dwz)
927aa2e7
JK
5547{
5548 sect_offset sect_off_prev;
5549 for (uint32_t i = 0; i <= map.cu_count; ++i)
5550 {
5551 sect_offset sect_off_next;
5552 if (i < map.cu_count)
5553 {
5554 sect_off_next
5555 = (sect_offset) (extract_unsigned_integer
5556 (map.cu_table_reordered + i * map.offset_size,
5557 map.offset_size,
5558 map.dwarf5_byte_order));
5559 }
5560 else
5561 sect_off_next = (sect_offset) section.size;
5562 if (i >= 1)
5563 {
5564 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5565 dwarf2_per_cu_data *per_cu
ed2dc618 5566 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5567 sect_off_prev, length);
b76e467d 5568 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5569 }
5570 sect_off_prev = sect_off_next;
5571 }
5572}
5573
5574/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5575 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5576
5577static void
ed2dc618 5578create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5579 const mapped_debug_names &map,
5580 const mapped_debug_names &dwz_map)
5581{
b76e467d
SM
5582 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5583 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5584
ed2dc618
SM
5585 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5586 dwarf2_per_objfile->info,
b76e467d 5587 false /* is_dwz */);
927aa2e7
JK
5588
5589 if (dwz_map.cu_count == 0)
5590 return;
5591
ed2dc618
SM
5592 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5593 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5594 true /* is_dwz */);
927aa2e7
JK
5595}
5596
5597/* Read .debug_names. If everything went ok, initialize the "quick"
5598 elements of all the CUs and return true. Otherwise, return false. */
5599
5600static bool
ed2dc618 5601dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5602{
22ca247e
TT
5603 std::unique_ptr<mapped_debug_names> map
5604 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5605 mapped_debug_names dwz_map (dwarf2_per_objfile);
5606 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5607
5608 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5609 &dwarf2_per_objfile->debug_names,
22ca247e 5610 *map))
927aa2e7
JK
5611 return false;
5612
5613 /* Don't use the index if it's empty. */
22ca247e 5614 if (map->name_count == 0)
927aa2e7
JK
5615 return false;
5616
5617 /* If there is a .dwz file, read it so we can get its CU list as
5618 well. */
ed2dc618 5619 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5620 if (dwz != NULL)
5621 {
5622 if (!read_debug_names_from_section (objfile,
00f93c44 5623 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5624 &dwz->debug_names, dwz_map))
5625 {
5626 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5627 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5628 return false;
5629 }
5630 }
5631
22ca247e 5632 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5633
22ca247e 5634 if (map->tu_count != 0)
927aa2e7
JK
5635 {
5636 /* We can only handle a single .debug_types when we have an
5637 index. */
fd5866f6 5638 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5639 return false;
5640
fd5866f6 5641 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5642
5643 create_signatured_type_table_from_debug_names
22ca247e 5644 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5645 }
5646
ed2dc618
SM
5647 create_addrmap_from_aranges (dwarf2_per_objfile,
5648 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5649
22ca247e 5650 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5651 dwarf2_per_objfile->using_index = 1;
5652 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5653 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5654
5655 return true;
5656}
5657
927aa2e7
JK
5658/* Type used to manage iterating over all CUs looking for a symbol for
5659 .debug_names. */
5660
5661class dw2_debug_names_iterator
5662{
5663public:
927aa2e7 5664 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5665 gdb::optional<block_enum> block_index,
5666 domain_enum domain,
927aa2e7 5667 const char *name)
2b79f376 5668 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5669 m_addr (find_vec_in_debug_names (map, name))
5670 {}
5671
5672 dw2_debug_names_iterator (const mapped_debug_names &map,
5673 search_domain search, uint32_t namei)
5674 : m_map (map),
5675 m_search (search),
5676 m_addr (find_vec_in_debug_names (map, namei))
5677 {}
5678
3b00ef10
TT
5679 dw2_debug_names_iterator (const mapped_debug_names &map,
5680 block_enum block_index, domain_enum domain,
5681 uint32_t namei)
5682 : m_map (map), m_block_index (block_index), m_domain (domain),
5683 m_addr (find_vec_in_debug_names (map, namei))
5684 {}
5685
927aa2e7
JK
5686 /* Return the next matching CU or NULL if there are no more. */
5687 dwarf2_per_cu_data *next ();
5688
5689private:
5690 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5691 const char *name);
5692 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5693 uint32_t namei);
5694
5695 /* The internalized form of .debug_names. */
5696 const mapped_debug_names &m_map;
5697
2b79f376
SM
5698 /* If set, only look for symbols that match that block. Valid values are
5699 GLOBAL_BLOCK and STATIC_BLOCK. */
5700 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5701
5702 /* The kind of symbol we're looking for. */
5703 const domain_enum m_domain = UNDEF_DOMAIN;
5704 const search_domain m_search = ALL_DOMAIN;
5705
5706 /* The list of CUs from the index entry of the symbol, or NULL if
5707 not found. */
5708 const gdb_byte *m_addr;
5709};
5710
5711const char *
5712mapped_debug_names::namei_to_name (uint32_t namei) const
5713{
5714 const ULONGEST namei_string_offs
5715 = extract_unsigned_integer ((name_table_string_offs_reordered
5716 + namei * offset_size),
5717 offset_size,
5718 dwarf5_byte_order);
5719 return read_indirect_string_at_offset
ed2dc618 5720 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5721}
5722
5723/* Find a slot in .debug_names for the object named NAME. If NAME is
5724 found, return pointer to its pool data. If NAME cannot be found,
5725 return NULL. */
5726
5727const gdb_byte *
5728dw2_debug_names_iterator::find_vec_in_debug_names
5729 (const mapped_debug_names &map, const char *name)
5730{
5731 int (*cmp) (const char *, const char *);
5732
54ee4252 5733 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5734 if (current_language->la_language == language_cplus
5735 || current_language->la_language == language_fortran
5736 || current_language->la_language == language_d)
5737 {
5738 /* NAME is already canonical. Drop any qualifiers as
5739 .debug_names does not contain any. */
5740
5741 if (strchr (name, '(') != NULL)
5742 {
54ee4252 5743 without_params = cp_remove_params (name);
927aa2e7 5744 if (without_params != NULL)
54ee4252 5745 name = without_params.get ();
927aa2e7
JK
5746 }
5747 }
5748
5749 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5750
5751 const uint32_t full_hash = dwarf5_djb_hash (name);
5752 uint32_t namei
5753 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5754 (map.bucket_table_reordered
5755 + (full_hash % map.bucket_count)), 4,
5756 map.dwarf5_byte_order);
5757 if (namei == 0)
5758 return NULL;
5759 --namei;
5760 if (namei >= map.name_count)
5761 {
b98664d3 5762 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5763 "[in module %s]"),
5764 namei, map.name_count,
ed2dc618 5765 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5766 return NULL;
5767 }
5768
5769 for (;;)
5770 {
5771 const uint32_t namei_full_hash
5772 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5773 (map.hash_table_reordered + namei), 4,
5774 map.dwarf5_byte_order);
5775 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5776 return NULL;
5777
5778 if (full_hash == namei_full_hash)
5779 {
5780 const char *const namei_string = map.namei_to_name (namei);
5781
5782#if 0 /* An expensive sanity check. */
5783 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5784 {
b98664d3 5785 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5786 "[in module %s]"),
5787 namei, objfile_name (dwarf2_per_objfile->objfile));
5788 return NULL;
5789 }
5790#endif
5791
5792 if (cmp (namei_string, name) == 0)
5793 {
5794 const ULONGEST namei_entry_offs
5795 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5796 + namei * map.offset_size),
5797 map.offset_size, map.dwarf5_byte_order);
5798 return map.entry_pool + namei_entry_offs;
5799 }
5800 }
5801
5802 ++namei;
5803 if (namei >= map.name_count)
5804 return NULL;
5805 }
5806}
5807
5808const gdb_byte *
5809dw2_debug_names_iterator::find_vec_in_debug_names
5810 (const mapped_debug_names &map, uint32_t namei)
5811{
5812 if (namei >= map.name_count)
5813 {
b98664d3 5814 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5815 "[in module %s]"),
5816 namei, map.name_count,
ed2dc618 5817 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5818 return NULL;
5819 }
5820
5821 const ULONGEST namei_entry_offs
5822 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5823 + namei * map.offset_size),
5824 map.offset_size, map.dwarf5_byte_order);
5825 return map.entry_pool + namei_entry_offs;
5826}
5827
5828/* See dw2_debug_names_iterator. */
5829
5830dwarf2_per_cu_data *
5831dw2_debug_names_iterator::next ()
5832{
5833 if (m_addr == NULL)
5834 return NULL;
5835
ed2dc618
SM
5836 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5837 struct objfile *objfile = dwarf2_per_objfile->objfile;
5838 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5839
5840 again:
5841
5842 unsigned int bytes_read;
5843 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5844 m_addr += bytes_read;
5845 if (abbrev == 0)
5846 return NULL;
5847
5848 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5849 if (indexval_it == m_map.abbrev_map.cend ())
5850 {
b98664d3 5851 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5852 "[in module %s]"),
ed2dc618 5853 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5854 return NULL;
5855 }
5856 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5857 enum class symbol_linkage {
5858 unknown,
5859 static_,
5860 extern_,
23c13d42 5861 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5862 dwarf2_per_cu_data *per_cu = NULL;
5863 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5864 {
5865 ULONGEST ull;
5866 switch (attr.form)
5867 {
5868 case DW_FORM_implicit_const:
5869 ull = attr.implicit_const;
5870 break;
5871 case DW_FORM_flag_present:
5872 ull = 1;
5873 break;
5874 case DW_FORM_udata:
5875 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5876 m_addr += bytes_read;
5877 break;
5878 default:
b98664d3 5879 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5880 dwarf_form_name (attr.form),
ed2dc618 5881 objfile_name (objfile));
927aa2e7
JK
5882 return NULL;
5883 }
5884 switch (attr.dw_idx)
5885 {
5886 case DW_IDX_compile_unit:
5887 /* Don't crash on bad data. */
b76e467d 5888 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5889 {
b98664d3 5890 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5891 " [in module %s]"),
5892 pulongest (ull),
5893 objfile_name (dwarf2_per_objfile->objfile));
5894 continue;
5895 }
ff4c9fec 5896 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5897 break;
8af5c486
JK
5898 case DW_IDX_type_unit:
5899 /* Don't crash on bad data. */
b2bdb8cf 5900 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5901 {
b98664d3 5902 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5903 " [in module %s]"),
5904 pulongest (ull),
5905 objfile_name (dwarf2_per_objfile->objfile));
5906 continue;
5907 }
ff4c9fec 5908 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5909 break;
927aa2e7
JK
5910 case DW_IDX_GNU_internal:
5911 if (!m_map.augmentation_is_gdb)
5912 break;
23c13d42 5913 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5914 break;
5915 case DW_IDX_GNU_external:
5916 if (!m_map.augmentation_is_gdb)
5917 break;
23c13d42 5918 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5919 break;
5920 }
5921 }
5922
5923 /* Skip if already read in. */
5924 if (per_cu->v.quick->compunit_symtab)
5925 goto again;
5926
5927 /* Check static vs global. */
23c13d42 5928 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5929 {
2b79f376 5930 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5931 const bool symbol_is_static =
5932 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5933 if (want_static != symbol_is_static)
2b79f376 5934 goto again;
927aa2e7
JK
5935 }
5936
5937 /* Match dw2_symtab_iter_next, symbol_kind
5938 and debug_names::psymbol_tag. */
5939 switch (m_domain)
5940 {
5941 case VAR_DOMAIN:
5942 switch (indexval.dwarf_tag)
5943 {
5944 case DW_TAG_variable:
5945 case DW_TAG_subprogram:
5946 /* Some types are also in VAR_DOMAIN. */
5947 case DW_TAG_typedef:
5948 case DW_TAG_structure_type:
5949 break;
5950 default:
5951 goto again;
5952 }
5953 break;
5954 case STRUCT_DOMAIN:
5955 switch (indexval.dwarf_tag)
5956 {
5957 case DW_TAG_typedef:
5958 case DW_TAG_structure_type:
5959 break;
5960 default:
5961 goto again;
5962 }
5963 break;
5964 case LABEL_DOMAIN:
5965 switch (indexval.dwarf_tag)
5966 {
5967 case 0:
5968 case DW_TAG_variable:
5969 break;
5970 default:
5971 goto again;
5972 }
5973 break;
59c35742
AB
5974 case MODULE_DOMAIN:
5975 switch (indexval.dwarf_tag)
5976 {
5977 case DW_TAG_module:
5978 break;
5979 default:
5980 goto again;
5981 }
5982 break;
927aa2e7
JK
5983 default:
5984 break;
5985 }
5986
5987 /* Match dw2_expand_symtabs_matching, symbol_kind and
5988 debug_names::psymbol_tag. */
5989 switch (m_search)
4b514bc8 5990 {
927aa2e7
JK
5991 case VARIABLES_DOMAIN:
5992 switch (indexval.dwarf_tag)
4b514bc8 5993 {
927aa2e7
JK
5994 case DW_TAG_variable:
5995 break;
5996 default:
5997 goto again;
4b514bc8 5998 }
927aa2e7
JK
5999 break;
6000 case FUNCTIONS_DOMAIN:
6001 switch (indexval.dwarf_tag)
4b514bc8 6002 {
927aa2e7
JK
6003 case DW_TAG_subprogram:
6004 break;
6005 default:
6006 goto again;
4b514bc8 6007 }
927aa2e7
JK
6008 break;
6009 case TYPES_DOMAIN:
6010 switch (indexval.dwarf_tag)
6011 {
6012 case DW_TAG_typedef:
6013 case DW_TAG_structure_type:
6014 break;
6015 default:
6016 goto again;
6017 }
6018 break;
59c35742
AB
6019 case MODULES_DOMAIN:
6020 switch (indexval.dwarf_tag)
6021 {
6022 case DW_TAG_module:
6023 break;
6024 default:
6025 goto again;
6026 }
927aa2e7
JK
6027 default:
6028 break;
4b514bc8 6029 }
927aa2e7
JK
6030
6031 return per_cu;
4b514bc8 6032}
61920122 6033
927aa2e7 6034static struct compunit_symtab *
c7f839cb 6035dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6036 const char *name, domain_enum domain)
4b514bc8 6037{
ed2dc618
SM
6038 struct dwarf2_per_objfile *dwarf2_per_objfile
6039 = get_dwarf2_per_objfile (objfile);
61920122 6040
927aa2e7
JK
6041 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6042 if (!mapp)
61920122 6043 {
927aa2e7
JK
6044 /* index is NULL if OBJF_READNOW. */
6045 return NULL;
6046 }
6047 const auto &map = *mapp;
9291a0cd 6048
2b79f376 6049 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6050
927aa2e7
JK
6051 struct compunit_symtab *stab_best = NULL;
6052 struct dwarf2_per_cu_data *per_cu;
6053 while ((per_cu = iter.next ()) != NULL)
6054 {
6055 struct symbol *sym, *with_opaque = NULL;
58f0c718 6056 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6057 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6058 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6059
927aa2e7
JK
6060 sym = block_find_symbol (block, name, domain,
6061 block_find_non_opaque_type_preferred,
6062 &with_opaque);
9703b513 6063
927aa2e7
JK
6064 /* Some caution must be observed with overloaded functions and
6065 methods, since the index will not contain any overload
6066 information (but NAME might contain it). */
a3ec0bb1 6067
927aa2e7 6068 if (sym != NULL
987012b8 6069 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6070 return stab;
6071 if (with_opaque != NULL
987012b8 6072 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6073 stab_best = stab;
9703b513 6074
927aa2e7 6075 /* Keep looking through other CUs. */
9703b513
TT
6076 }
6077
927aa2e7 6078 return stab_best;
9703b513
TT
6079}
6080
927aa2e7
JK
6081/* This dumps minimal information about .debug_names. It is called
6082 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6083 uses this to verify that .debug_names has been loaded. */
9291a0cd 6084
927aa2e7
JK
6085static void
6086dw2_debug_names_dump (struct objfile *objfile)
6087{
ed2dc618
SM
6088 struct dwarf2_per_objfile *dwarf2_per_objfile
6089 = get_dwarf2_per_objfile (objfile);
6090
927aa2e7
JK
6091 gdb_assert (dwarf2_per_objfile->using_index);
6092 printf_filtered (".debug_names:");
6093 if (dwarf2_per_objfile->debug_names_table)
6094 printf_filtered (" exists\n");
6095 else
6096 printf_filtered (" faked for \"readnow\"\n");
6097 printf_filtered ("\n");
9291a0cd
TT
6098}
6099
9291a0cd 6100static void
927aa2e7
JK
6101dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6102 const char *func_name)
9291a0cd 6103{
ed2dc618
SM
6104 struct dwarf2_per_objfile *dwarf2_per_objfile
6105 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6106
927aa2e7
JK
6107 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6108 if (dwarf2_per_objfile->debug_names_table)
24c79950 6109 {
927aa2e7 6110 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6111
2b79f376 6112 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6113
927aa2e7
JK
6114 struct dwarf2_per_cu_data *per_cu;
6115 while ((per_cu = iter.next ()) != NULL)
58f0c718 6116 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6117 }
6118}
24c79950 6119
3b00ef10
TT
6120static void
6121dw2_debug_names_map_matching_symbols
6122 (struct objfile *objfile,
6123 const lookup_name_info &name, domain_enum domain,
6124 int global,
6125 gdb::function_view<symbol_found_callback_ftype> callback,
6126 symbol_compare_ftype *ordered_compare)
6127{
6128 struct dwarf2_per_objfile *dwarf2_per_objfile
6129 = get_dwarf2_per_objfile (objfile);
6130
6131 /* debug_names_table is NULL if OBJF_READNOW. */
6132 if (!dwarf2_per_objfile->debug_names_table)
6133 return;
6134
6135 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6136 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6137
6138 const char *match_name = name.ada ().lookup_name ().c_str ();
6139 auto matcher = [&] (const char *symname)
6140 {
6141 if (ordered_compare == nullptr)
6142 return true;
6143 return ordered_compare (symname, match_name) == 0;
6144 };
6145
6146 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6147 [&] (offset_type namei)
6148 {
6149 /* The name was matched, now expand corresponding CUs that were
6150 marked. */
6151 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6152
6153 struct dwarf2_per_cu_data *per_cu;
6154 while ((per_cu = iter.next ()) != NULL)
6155 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6156 return true;
6157 });
6158
6159 /* It's a shame we couldn't do this inside the
6160 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6161 that have already been expanded. Instead, this loop matches what
6162 the psymtab code does. */
6163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6164 {
6165 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6166 if (cust != nullptr)
6167 {
6168 const struct block *block
6169 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6170 if (!iterate_over_symbols_terminated (block, name,
6171 domain, callback))
6172 break;
6173 }
6174 }
6175}
6176
927aa2e7
JK
6177static void
6178dw2_debug_names_expand_symtabs_matching
6179 (struct objfile *objfile,
6180 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6181 const lookup_name_info &lookup_name,
6182 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6183 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6184 enum search_domain kind)
6185{
ed2dc618
SM
6186 struct dwarf2_per_objfile *dwarf2_per_objfile
6187 = get_dwarf2_per_objfile (objfile);
9291a0cd 6188
927aa2e7
JK
6189 /* debug_names_table is NULL if OBJF_READNOW. */
6190 if (!dwarf2_per_objfile->debug_names_table)
6191 return;
9291a0cd 6192
ed2dc618 6193 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6194
44ed8f3e 6195 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6196
44ed8f3e
PA
6197 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6198 symbol_matcher,
6199 kind, [&] (offset_type namei)
927aa2e7 6200 {
927aa2e7
JK
6201 /* The name was matched, now expand corresponding CUs that were
6202 marked. */
6203 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6204
927aa2e7
JK
6205 struct dwarf2_per_cu_data *per_cu;
6206 while ((per_cu = iter.next ()) != NULL)
6207 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6208 expansion_notify);
3b00ef10 6209 return true;
44ed8f3e 6210 });
9291a0cd
TT
6211}
6212
927aa2e7 6213const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6214{
6215 dw2_has_symbols,
6216 dw2_find_last_source_symtab,
6217 dw2_forget_cached_source_info,
f8eba3c6 6218 dw2_map_symtabs_matching_filename,
927aa2e7 6219 dw2_debug_names_lookup_symbol,
9291a0cd 6220 dw2_print_stats,
927aa2e7 6221 dw2_debug_names_dump,
927aa2e7 6222 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6223 dw2_expand_all_symtabs,
652a8996 6224 dw2_expand_symtabs_with_fullname,
3b00ef10 6225 dw2_debug_names_map_matching_symbols,
927aa2e7 6226 dw2_debug_names_expand_symtabs_matching,
43f3e411 6227 dw2_find_pc_sect_compunit_symtab,
71a3c369 6228 NULL,
9291a0cd
TT
6229 dw2_map_symbol_filenames
6230};
6231
4485a1c1
SM
6232/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6233 to either a dwarf2_per_objfile or dwz_file object. */
6234
6235template <typename T>
6236static gdb::array_view<const gdb_byte>
6237get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6238{
6239 dwarf2_section_info *section = &section_owner->gdb_index;
6240
6241 if (dwarf2_section_empty_p (section))
6242 return {};
6243
6244 /* Older elfutils strip versions could keep the section in the main
6245 executable while splitting it for the separate debug info file. */
6246 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6247 return {};
6248
6249 dwarf2_read_section (obj, section);
6250
8bebfcda
PA
6251 /* dwarf2_section_info::size is a bfd_size_type, while
6252 gdb::array_view works with size_t. On 32-bit hosts, with
6253 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6254 is 32-bit. So we need an explicit narrowing conversion here.
6255 This is fine, because it's impossible to allocate or mmap an
6256 array/buffer larger than what size_t can represent. */
6257 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6258}
6259
87d6a7aa
SM
6260/* Lookup the index cache for the contents of the index associated to
6261 DWARF2_OBJ. */
6262
6263static gdb::array_view<const gdb_byte>
6264get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6265{
6266 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6267 if (build_id == nullptr)
6268 return {};
6269
6270 return global_index_cache.lookup_gdb_index (build_id,
6271 &dwarf2_obj->index_cache_res);
6272}
6273
6274/* Same as the above, but for DWZ. */
6275
6276static gdb::array_view<const gdb_byte>
6277get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6278{
6279 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6280 if (build_id == nullptr)
6281 return {};
6282
6283 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6284}
6285
3c0aa29a 6286/* See symfile.h. */
9291a0cd 6287
3c0aa29a
PA
6288bool
6289dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6290{
ed2dc618
SM
6291 struct dwarf2_per_objfile *dwarf2_per_objfile
6292 = get_dwarf2_per_objfile (objfile);
6293
9291a0cd
TT
6294 /* If we're about to read full symbols, don't bother with the
6295 indices. In this case we also don't care if some other debug
6296 format is making psymtabs, because they are all about to be
6297 expanded anyway. */
6298 if ((objfile->flags & OBJF_READNOW))
6299 {
9291a0cd 6300 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6301 create_all_comp_units (dwarf2_per_objfile);
6302 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6303 dwarf2_per_objfile->quick_file_names_table
6304 = create_quick_file_names_table
6305 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6306
b76e467d 6307 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6308 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6309 {
ff4c9fec 6310 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6311
e254ef6a
DE
6312 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6313 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6314 }
6315
6316 /* Return 1 so that gdb sees the "quick" functions. However,
6317 these functions will be no-ops because we will have expanded
6318 all symtabs. */
3c0aa29a
PA
6319 *index_kind = dw_index_kind::GDB_INDEX;
6320 return true;
9291a0cd
TT
6321 }
6322
ed2dc618 6323 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6324 {
6325 *index_kind = dw_index_kind::DEBUG_NAMES;
6326 return true;
6327 }
927aa2e7 6328
4485a1c1
SM
6329 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6330 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6331 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6332 {
6333 *index_kind = dw_index_kind::GDB_INDEX;
6334 return true;
6335 }
9291a0cd 6336
87d6a7aa
SM
6337 /* ... otherwise, try to find the index in the index cache. */
6338 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6339 get_gdb_index_contents_from_cache,
6340 get_gdb_index_contents_from_cache_dwz))
6341 {
6342 global_index_cache.hit ();
6343 *index_kind = dw_index_kind::GDB_INDEX;
6344 return true;
6345 }
6346
6347 global_index_cache.miss ();
3c0aa29a 6348 return false;
9291a0cd
TT
6349}
6350
6351\f
6352
dce234bc
PP
6353/* Build a partial symbol table. */
6354
6355void
f29dff0a 6356dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6357{
ed2dc618
SM
6358 struct dwarf2_per_objfile *dwarf2_per_objfile
6359 = get_dwarf2_per_objfile (objfile);
c9bf0622 6360
6eee24ce 6361 init_psymbol_list (objfile, 1024);
c906108c 6362
a70b8144 6363 try
c9bf0622
TT
6364 {
6365 /* This isn't really ideal: all the data we allocate on the
6366 objfile's obstack is still uselessly kept around. However,
6367 freeing it seems unsafe. */
906768f9 6368 psymtab_discarder psymtabs (objfile);
ed2dc618 6369 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6370 psymtabs.keep ();
87d6a7aa
SM
6371
6372 /* (maybe) store an index in the cache. */
6373 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6374 }
230d2906 6375 catch (const gdb_exception_error &except)
492d29ea
PA
6376 {
6377 exception_print (gdb_stderr, except);
6378 }
c906108c 6379}
c906108c 6380
1ce1cefd
DE
6381/* Return the total length of the CU described by HEADER. */
6382
6383static unsigned int
6384get_cu_length (const struct comp_unit_head *header)
6385{
6386 return header->initial_length_size + header->length;
6387}
6388
9c541725 6389/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6390
9c541725
PA
6391static inline bool
6392offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6393{
9c541725
PA
6394 sect_offset bottom = cu_header->sect_off;
6395 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6396
9c541725 6397 return sect_off >= bottom && sect_off < top;
45452591
DE
6398}
6399
3b80fe9b
DE
6400/* Find the base address of the compilation unit for range lists and
6401 location lists. It will normally be specified by DW_AT_low_pc.
6402 In DWARF-3 draft 4, the base address could be overridden by
6403 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6404 compilation units with discontinuous ranges. */
6405
6406static void
6407dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6408{
6409 struct attribute *attr;
6410
6411 cu->base_known = 0;
6412 cu->base_address = 0;
6413
6414 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6415 if (attr != nullptr)
3b80fe9b 6416 {
31aa7e4e 6417 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6418 cu->base_known = 1;
6419 }
6420 else
6421 {
6422 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6423 if (attr != nullptr)
3b80fe9b 6424 {
31aa7e4e 6425 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6426 cu->base_known = 1;
6427 }
6428 }
6429}
6430
93311388 6431/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6432 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6433 NOTE: This leaves members offset, first_die_offset to be filled in
6434 by the caller. */
107d2387 6435
d521ce57 6436static const gdb_byte *
107d2387 6437read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6438 const gdb_byte *info_ptr,
6439 struct dwarf2_section_info *section,
6440 rcuh_kind section_kind)
107d2387
AC
6441{
6442 int signed_addr;
891d2f0b 6443 unsigned int bytes_read;
43988095
JK
6444 const char *filename = get_section_file_name (section);
6445 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6446
6447 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6448 cu_header->initial_length_size = bytes_read;
6449 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6450 info_ptr += bytes_read;
107d2387 6451 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6452 if (cu_header->version < 2 || cu_header->version > 5)
6453 error (_("Dwarf Error: wrong version in compilation unit header "
6454 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6455 cu_header->version, filename);
107d2387 6456 info_ptr += 2;
43988095
JK
6457 if (cu_header->version < 5)
6458 switch (section_kind)
6459 {
6460 case rcuh_kind::COMPILE:
6461 cu_header->unit_type = DW_UT_compile;
6462 break;
6463 case rcuh_kind::TYPE:
6464 cu_header->unit_type = DW_UT_type;
6465 break;
6466 default:
6467 internal_error (__FILE__, __LINE__,
6468 _("read_comp_unit_head: invalid section_kind"));
6469 }
6470 else
6471 {
6472 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6473 (read_1_byte (abfd, info_ptr));
6474 info_ptr += 1;
6475 switch (cu_header->unit_type)
6476 {
6477 case DW_UT_compile:
a084a2a6
AT
6478 case DW_UT_partial:
6479 case DW_UT_skeleton:
6480 case DW_UT_split_compile:
43988095
JK
6481 if (section_kind != rcuh_kind::COMPILE)
6482 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6483 "(is %s, should be %s) [in module %s]"),
6484 dwarf_unit_type_name (cu_header->unit_type),
6485 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6486 break;
6487 case DW_UT_type:
a084a2a6 6488 case DW_UT_split_type:
43988095
JK
6489 section_kind = rcuh_kind::TYPE;
6490 break;
6491 default:
6492 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6493 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6494 "[in module %s]"), cu_header->unit_type,
6495 dwarf_unit_type_name (DW_UT_compile),
6496 dwarf_unit_type_name (DW_UT_skeleton),
6497 dwarf_unit_type_name (DW_UT_split_compile),
6498 dwarf_unit_type_name (DW_UT_type),
6499 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6500 }
6501
6502 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6503 info_ptr += 1;
6504 }
9c541725
PA
6505 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6506 cu_header,
6507 &bytes_read);
613e1657 6508 info_ptr += bytes_read;
43988095
JK
6509 if (cu_header->version < 5)
6510 {
6511 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6512 info_ptr += 1;
6513 }
107d2387
AC
6514 signed_addr = bfd_get_sign_extend_vma (abfd);
6515 if (signed_addr < 0)
8e65ff28 6516 internal_error (__FILE__, __LINE__,
e2e0b3e5 6517 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6518 cu_header->signed_addr_p = signed_addr;
c764a876 6519
a084a2a6
AT
6520 bool header_has_signature = section_kind == rcuh_kind::TYPE
6521 || cu_header->unit_type == DW_UT_skeleton
6522 || cu_header->unit_type == DW_UT_split_compile;
43988095 6523
a084a2a6
AT
6524 if (header_has_signature)
6525 {
43988095
JK
6526 cu_header->signature = read_8_bytes (abfd, info_ptr);
6527 info_ptr += 8;
a084a2a6 6528 }
43988095 6529
a084a2a6
AT
6530 if (section_kind == rcuh_kind::TYPE)
6531 {
6532 LONGEST type_offset;
43988095
JK
6533 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6534 info_ptr += bytes_read;
9c541725
PA
6535 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6536 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6537 error (_("Dwarf Error: Too big type_offset in compilation unit "
6538 "header (is %s) [in module %s]"), plongest (type_offset),
6539 filename);
6540 }
6541
107d2387
AC
6542 return info_ptr;
6543}
6544
36586728
TT
6545/* Helper function that returns the proper abbrev section for
6546 THIS_CU. */
6547
6548static struct dwarf2_section_info *
6549get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6550{
6551 struct dwarf2_section_info *abbrev;
ed2dc618 6552 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6553
6554 if (this_cu->is_dwz)
ed2dc618 6555 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6556 else
6557 abbrev = &dwarf2_per_objfile->abbrev;
6558
6559 return abbrev;
6560}
6561
9ff913ba
DE
6562/* Subroutine of read_and_check_comp_unit_head and
6563 read_and_check_type_unit_head to simplify them.
6564 Perform various error checking on the header. */
6565
6566static void
ed2dc618
SM
6567error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6568 struct comp_unit_head *header,
4bdcc0c1
DE
6569 struct dwarf2_section_info *section,
6570 struct dwarf2_section_info *abbrev_section)
9ff913ba 6571{
a32a8923 6572 const char *filename = get_section_file_name (section);
9ff913ba 6573
9c541725 6574 if (to_underlying (header->abbrev_sect_off)
36586728 6575 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6576 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6577 "(offset %s + 6) [in module %s]"),
6578 sect_offset_str (header->abbrev_sect_off),
6579 sect_offset_str (header->sect_off),
9ff913ba
DE
6580 filename);
6581
9c541725 6582 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6583 avoid potential 32-bit overflow. */
9c541725 6584 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6585 > section->size)
9c541725 6586 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6587 "(offset %s + 0) [in module %s]"),
6588 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6589 filename);
6590}
6591
6592/* Read in a CU/TU header and perform some basic error checking.
6593 The contents of the header are stored in HEADER.
6594 The result is a pointer to the start of the first DIE. */
adabb602 6595
d521ce57 6596static const gdb_byte *
ed2dc618
SM
6597read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6598 struct comp_unit_head *header,
9ff913ba 6599 struct dwarf2_section_info *section,
4bdcc0c1 6600 struct dwarf2_section_info *abbrev_section,
d521ce57 6601 const gdb_byte *info_ptr,
43988095 6602 rcuh_kind section_kind)
72bf9492 6603{
d521ce57 6604 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6605
9c541725 6606 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6607
43988095 6608 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6609
9c541725 6610 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6611
ed2dc618
SM
6612 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6613 abbrev_section);
9ff913ba
DE
6614
6615 return info_ptr;
348e048f
DE
6616}
6617
f4dc4d17
DE
6618/* Fetch the abbreviation table offset from a comp or type unit header. */
6619
6620static sect_offset
ed2dc618
SM
6621read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6622 struct dwarf2_section_info *section,
9c541725 6623 sect_offset sect_off)
f4dc4d17 6624{
a32a8923 6625 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6626 const gdb_byte *info_ptr;
ac298888 6627 unsigned int initial_length_size, offset_size;
43988095 6628 uint16_t version;
f4dc4d17
DE
6629
6630 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6631 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6632 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6633 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6634 info_ptr += initial_length_size;
6635
6636 version = read_2_bytes (abfd, info_ptr);
6637 info_ptr += 2;
6638 if (version >= 5)
6639 {
6640 /* Skip unit type and address size. */
6641 info_ptr += 2;
6642 }
6643
9c541725 6644 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6645}
6646
aaa75496
JB
6647/* Allocate a new partial symtab for file named NAME and mark this new
6648 partial symtab as being an include of PST. */
6649
6650static void
d521ce57 6651dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6652 struct objfile *objfile)
6653{
6654 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6655
fbd9ab74
JK
6656 if (!IS_ABSOLUTE_PATH (subpst->filename))
6657 {
6658 /* It shares objfile->objfile_obstack. */
6659 subpst->dirname = pst->dirname;
6660 }
6661
a9342b62 6662 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6663 subpst->dependencies[0] = pst;
6664 subpst->number_of_dependencies = 1;
6665
aaa75496 6666 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6667
6668 /* No private part is necessary for include psymtabs. This property
6669 can be used to differentiate between such include psymtabs and
10b3939b 6670 the regular ones. */
58a9656e 6671 subpst->read_symtab_private = NULL;
aaa75496
JB
6672}
6673
6674/* Read the Line Number Program data and extract the list of files
6675 included by the source file represented by PST. Build an include
d85a05f0 6676 partial symtab for each of these included files. */
aaa75496
JB
6677
6678static void
6679dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6680 struct die_info *die,
6681 struct partial_symtab *pst)
aaa75496 6682{
fff8551c 6683 line_header_up lh;
d85a05f0 6684 struct attribute *attr;
aaa75496 6685
d85a05f0 6686 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6687 if (attr != nullptr)
9c541725 6688 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6689 if (lh == NULL)
6690 return; /* No linetable, so no includes. */
6691
79748972
TT
6692 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6693 that we pass in the raw text_low here; that is ok because we're
6694 only decoding the line table to make include partial symtabs, and
6695 so the addresses aren't really used. */
4ae976d1 6696 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6697 pst->raw_text_low (), 1);
aaa75496
JB
6698}
6699
348e048f 6700static hashval_t
52dc124a 6701hash_signatured_type (const void *item)
348e048f 6702{
9a3c8263
SM
6703 const struct signatured_type *sig_type
6704 = (const struct signatured_type *) item;
9a619af0 6705
348e048f 6706 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6707 return sig_type->signature;
348e048f
DE
6708}
6709
6710static int
52dc124a 6711eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6712{
9a3c8263
SM
6713 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6714 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6715
348e048f
DE
6716 return lhs->signature == rhs->signature;
6717}
6718
1fd400ff
TT
6719/* Allocate a hash table for signatured types. */
6720
6721static htab_t
673bfd45 6722allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6723{
6724 return htab_create_alloc_ex (41,
52dc124a
DE
6725 hash_signatured_type,
6726 eq_signatured_type,
1fd400ff
TT
6727 NULL,
6728 &objfile->objfile_obstack,
6729 hashtab_obstack_allocate,
6730 dummy_obstack_deallocate);
6731}
6732
d467dd73 6733/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6734
6735static int
d467dd73 6736add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6737{
9a3c8263 6738 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6739 std::vector<signatured_type *> *all_type_units
6740 = (std::vector<signatured_type *> *) datum;
1fd400ff 6741
b2bdb8cf 6742 all_type_units->push_back (sigt);
1fd400ff
TT
6743
6744 return 1;
6745}
6746
78d4d2c5 6747/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6748 and fill them into TYPES_HTAB. It will process only type units,
6749 therefore DW_UT_type. */
c88ee1f0 6750
78d4d2c5 6751static void
ed2dc618
SM
6752create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6753 struct dwo_file *dwo_file,
43988095
JK
6754 dwarf2_section_info *section, htab_t &types_htab,
6755 rcuh_kind section_kind)
348e048f 6756{
3019eac3 6757 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6758 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6759 bfd *abfd;
6760 const gdb_byte *info_ptr, *end_ptr;
348e048f 6761
4bdcc0c1
DE
6762 abbrev_section = (dwo_file != NULL
6763 ? &dwo_file->sections.abbrev
6764 : &dwarf2_per_objfile->abbrev);
6765
b4f54984 6766 if (dwarf_read_debug)
43988095
JK
6767 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6768 get_section_name (section),
a32a8923 6769 get_section_file_name (abbrev_section));
09406207 6770
78d4d2c5
JK
6771 dwarf2_read_section (objfile, section);
6772 info_ptr = section->buffer;
348e048f 6773
78d4d2c5
JK
6774 if (info_ptr == NULL)
6775 return;
348e048f 6776
78d4d2c5
JK
6777 /* We can't set abfd until now because the section may be empty or
6778 not present, in which case the bfd is unknown. */
6779 abfd = get_section_bfd_owner (section);
348e048f 6780
78d4d2c5
JK
6781 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6782 because we don't need to read any dies: the signature is in the
6783 header. */
3019eac3 6784
78d4d2c5
JK
6785 end_ptr = info_ptr + section->size;
6786 while (info_ptr < end_ptr)
6787 {
78d4d2c5
JK
6788 struct signatured_type *sig_type;
6789 struct dwo_unit *dwo_tu;
6790 void **slot;
6791 const gdb_byte *ptr = info_ptr;
6792 struct comp_unit_head header;
6793 unsigned int length;
8b70b953 6794
9c541725 6795 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6796
a49dd8dd
JK
6797 /* Initialize it due to a false compiler warning. */
6798 header.signature = -1;
9c541725 6799 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6800
78d4d2c5
JK
6801 /* We need to read the type's signature in order to build the hash
6802 table, but we don't need anything else just yet. */
348e048f 6803
ed2dc618 6804 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6805 abbrev_section, ptr, section_kind);
348e048f 6806
78d4d2c5 6807 length = get_cu_length (&header);
6caca83c 6808
78d4d2c5
JK
6809 /* Skip dummy type units. */
6810 if (ptr >= info_ptr + length
43988095
JK
6811 || peek_abbrev_code (abfd, ptr) == 0
6812 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6813 {
6814 info_ptr += length;
6815 continue;
6816 }
dee91e82 6817
78d4d2c5
JK
6818 if (types_htab == NULL)
6819 {
6820 if (dwo_file)
6821 types_htab = allocate_dwo_unit_table (objfile);
6822 else
6823 types_htab = allocate_signatured_type_table (objfile);
6824 }
8b70b953 6825
78d4d2c5
JK
6826 if (dwo_file)
6827 {
6828 sig_type = NULL;
6829 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6830 struct dwo_unit);
6831 dwo_tu->dwo_file = dwo_file;
43988095 6832 dwo_tu->signature = header.signature;
9c541725 6833 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6834 dwo_tu->section = section;
9c541725 6835 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6836 dwo_tu->length = length;
6837 }
6838 else
6839 {
6840 /* N.B.: type_offset is not usable if this type uses a DWO file.
6841 The real type_offset is in the DWO file. */
6842 dwo_tu = NULL;
6843 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6844 struct signatured_type);
43988095 6845 sig_type->signature = header.signature;
9c541725 6846 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6847 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6848 sig_type->per_cu.is_debug_types = 1;
6849 sig_type->per_cu.section = section;
9c541725 6850 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6851 sig_type->per_cu.length = length;
6852 }
6853
6854 slot = htab_find_slot (types_htab,
6855 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6856 INSERT);
6857 gdb_assert (slot != NULL);
6858 if (*slot != NULL)
6859 {
9c541725 6860 sect_offset dup_sect_off;
0349ea22 6861
3019eac3
DE
6862 if (dwo_file)
6863 {
78d4d2c5
JK
6864 const struct dwo_unit *dup_tu
6865 = (const struct dwo_unit *) *slot;
6866
9c541725 6867 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6868 }
6869 else
6870 {
78d4d2c5
JK
6871 const struct signatured_type *dup_tu
6872 = (const struct signatured_type *) *slot;
6873
9c541725 6874 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6875 }
8b70b953 6876
b98664d3 6877 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6878 " the entry at offset %s, signature %s"),
6879 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6880 hex_string (header.signature));
78d4d2c5
JK
6881 }
6882 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6883
78d4d2c5 6884 if (dwarf_read_debug > 1)
9d8780f0
SM
6885 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6886 sect_offset_str (sect_off),
43988095 6887 hex_string (header.signature));
3019eac3 6888
78d4d2c5
JK
6889 info_ptr += length;
6890 }
6891}
3019eac3 6892
78d4d2c5
JK
6893/* Create the hash table of all entries in the .debug_types
6894 (or .debug_types.dwo) section(s).
6895 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6896 otherwise it is NULL.
b3c8eb43 6897
78d4d2c5 6898 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6899
78d4d2c5 6900 Note: This function processes DWO files only, not DWP files. */
348e048f 6901
78d4d2c5 6902static void
ed2dc618
SM
6903create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6904 struct dwo_file *dwo_file,
fd5866f6 6905 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6906 htab_t &types_htab)
6907{
fd5866f6
SM
6908 for (dwarf2_section_info &section : type_sections)
6909 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6910 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6911}
6912
6913/* Create the hash table of all entries in the .debug_types section,
6914 and initialize all_type_units.
6915 The result is zero if there is an error (e.g. missing .debug_types section),
6916 otherwise non-zero. */
6917
6918static int
ed2dc618 6919create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6920{
78d4d2c5 6921 htab_t types_htab = NULL;
3019eac3 6922
ed2dc618
SM
6923 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6924 &dwarf2_per_objfile->info, types_htab,
43988095 6925 rcuh_kind::COMPILE);
ed2dc618
SM
6926 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6927 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6928 if (types_htab == NULL)
6929 {
6930 dwarf2_per_objfile->signatured_types = NULL;
6931 return 0;
6932 }
6933
348e048f
DE
6934 dwarf2_per_objfile->signatured_types = types_htab;
6935
b2bdb8cf
SM
6936 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6937 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6938
6939 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6940 &dwarf2_per_objfile->all_type_units);
1fd400ff 6941
348e048f
DE
6942 return 1;
6943}
6944
6aa5f3a6
DE
6945/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6946 If SLOT is non-NULL, it is the entry to use in the hash table.
6947 Otherwise we find one. */
6948
6949static struct signatured_type *
ed2dc618
SM
6950add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6951 void **slot)
6aa5f3a6
DE
6952{
6953 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6954
b2bdb8cf
SM
6955 if (dwarf2_per_objfile->all_type_units.size ()
6956 == dwarf2_per_objfile->all_type_units.capacity ())
6957 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6958
b2bdb8cf
SM
6959 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6960 struct signatured_type);
6961
6962 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6963 sig_type->signature = sig;
6964 sig_type->per_cu.is_debug_types = 1;
6965 if (dwarf2_per_objfile->using_index)
6966 {
6967 sig_type->per_cu.v.quick =
6968 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6969 struct dwarf2_per_cu_quick_data);
6970 }
6971
6972 if (slot == NULL)
6973 {
6974 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6975 sig_type, INSERT);
6976 }
6977 gdb_assert (*slot == NULL);
6978 *slot = sig_type;
6979 /* The rest of sig_type must be filled in by the caller. */
6980 return sig_type;
6981}
6982
a2ce51a0
DE
6983/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6984 Fill in SIG_ENTRY with DWO_ENTRY. */
6985
6986static void
ed2dc618 6987fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6988 struct signatured_type *sig_entry,
6989 struct dwo_unit *dwo_entry)
6990{
7ee85ab1 6991 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6992 gdb_assert (! sig_entry->per_cu.queued);
6993 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6994 if (dwarf2_per_objfile->using_index)
6995 {
6996 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6997 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6998 }
6999 else
7000 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7001 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7002 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7003 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7004 gdb_assert (sig_entry->dwo_unit == NULL);
7005
7006 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7007 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7008 sig_entry->per_cu.length = dwo_entry->length;
7009 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7010 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7011 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7012 sig_entry->dwo_unit = dwo_entry;
7013}
7014
7015/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7016 If we haven't read the TU yet, create the signatured_type data structure
7017 for a TU to be read in directly from a DWO file, bypassing the stub.
7018 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7019 using .gdb_index, then when reading a CU we want to stay in the DWO file
7020 containing that CU. Otherwise we could end up reading several other DWO
7021 files (due to comdat folding) to process the transitive closure of all the
7022 mentioned TUs, and that can be slow. The current DWO file will have every
7023 type signature that it needs.
a2ce51a0
DE
7024 We only do this for .gdb_index because in the psymtab case we already have
7025 to read all the DWOs to build the type unit groups. */
7026
7027static struct signatured_type *
7028lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7029{
518817b3
SM
7030 struct dwarf2_per_objfile *dwarf2_per_objfile
7031 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7032 struct objfile *objfile = dwarf2_per_objfile->objfile;
7033 struct dwo_file *dwo_file;
7034 struct dwo_unit find_dwo_entry, *dwo_entry;
7035 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7036 void **slot;
a2ce51a0
DE
7037
7038 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7039
6aa5f3a6
DE
7040 /* If TU skeletons have been removed then we may not have read in any
7041 TUs yet. */
7042 if (dwarf2_per_objfile->signatured_types == NULL)
7043 {
7044 dwarf2_per_objfile->signatured_types
7045 = allocate_signatured_type_table (objfile);
7046 }
a2ce51a0
DE
7047
7048 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7049 Use the global signatured_types array to do our own comdat-folding
7050 of types. If this is the first time we're reading this TU, and
7051 the TU has an entry in .gdb_index, replace the recorded data from
7052 .gdb_index with this TU. */
a2ce51a0 7053
a2ce51a0 7054 find_sig_entry.signature = sig;
6aa5f3a6
DE
7055 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7056 &find_sig_entry, INSERT);
9a3c8263 7057 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7058
7059 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7060 read. Don't reassign the global entry to point to this DWO if that's
7061 the case. Also note that if the TU is already being read, it may not
7062 have come from a DWO, the program may be a mix of Fission-compiled
7063 code and non-Fission-compiled code. */
7064
7065 /* Have we already tried to read this TU?
7066 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7067 needn't exist in the global table yet). */
7068 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7069 return sig_entry;
7070
6aa5f3a6
DE
7071 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7072 dwo_unit of the TU itself. */
7073 dwo_file = cu->dwo_unit->dwo_file;
7074
a2ce51a0
DE
7075 /* Ok, this is the first time we're reading this TU. */
7076 if (dwo_file->tus == NULL)
7077 return NULL;
7078 find_dwo_entry.signature = sig;
9a3c8263 7079 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7080 if (dwo_entry == NULL)
7081 return NULL;
7082
6aa5f3a6
DE
7083 /* If the global table doesn't have an entry for this TU, add one. */
7084 if (sig_entry == NULL)
ed2dc618 7085 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7086
ed2dc618 7087 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7088 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7089 return sig_entry;
7090}
7091
a2ce51a0
DE
7092/* Subroutine of lookup_signatured_type.
7093 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7094 then try the DWP file. If the TU stub (skeleton) has been removed then
7095 it won't be in .gdb_index. */
a2ce51a0
DE
7096
7097static struct signatured_type *
7098lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7099{
518817b3
SM
7100 struct dwarf2_per_objfile *dwarf2_per_objfile
7101 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7102 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7103 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7104 struct dwo_unit *dwo_entry;
7105 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7106 void **slot;
a2ce51a0
DE
7107
7108 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7109 gdb_assert (dwp_file != NULL);
7110
6aa5f3a6
DE
7111 /* If TU skeletons have been removed then we may not have read in any
7112 TUs yet. */
7113 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7114 {
6aa5f3a6
DE
7115 dwarf2_per_objfile->signatured_types
7116 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7117 }
7118
6aa5f3a6
DE
7119 find_sig_entry.signature = sig;
7120 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7121 &find_sig_entry, INSERT);
9a3c8263 7122 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7123
7124 /* Have we already tried to read this TU?
7125 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7126 needn't exist in the global table yet). */
7127 if (sig_entry != NULL)
7128 return sig_entry;
7129
a2ce51a0
DE
7130 if (dwp_file->tus == NULL)
7131 return NULL;
ed2dc618 7132 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7133 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7134 if (dwo_entry == NULL)
7135 return NULL;
7136
ed2dc618
SM
7137 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7138 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7139
a2ce51a0
DE
7140 return sig_entry;
7141}
7142
380bca97 7143/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7144 Returns NULL if signature SIG is not present in the table.
7145 It is up to the caller to complain about this. */
348e048f
DE
7146
7147static struct signatured_type *
a2ce51a0 7148lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7149{
518817b3
SM
7150 struct dwarf2_per_objfile *dwarf2_per_objfile
7151 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7152
a2ce51a0
DE
7153 if (cu->dwo_unit
7154 && dwarf2_per_objfile->using_index)
7155 {
7156 /* We're in a DWO/DWP file, and we're using .gdb_index.
7157 These cases require special processing. */
ed2dc618 7158 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7159 return lookup_dwo_signatured_type (cu, sig);
7160 else
7161 return lookup_dwp_signatured_type (cu, sig);
7162 }
7163 else
7164 {
7165 struct signatured_type find_entry, *entry;
348e048f 7166
a2ce51a0
DE
7167 if (dwarf2_per_objfile->signatured_types == NULL)
7168 return NULL;
7169 find_entry.signature = sig;
9a3c8263
SM
7170 entry = ((struct signatured_type *)
7171 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7172 return entry;
7173 }
348e048f 7174}
42e7ad6c
DE
7175\f
7176/* Low level DIE reading support. */
348e048f 7177
d85a05f0
DJ
7178/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7179
7180static void
7181init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7182 struct dwarf2_cu *cu,
3019eac3 7183 struct dwarf2_section_info *section,
685af9cd
TT
7184 struct dwo_file *dwo_file,
7185 struct abbrev_table *abbrev_table)
d85a05f0 7186{
fceca515 7187 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7188 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7189 reader->cu = cu;
3019eac3 7190 reader->dwo_file = dwo_file;
dee91e82
DE
7191 reader->die_section = section;
7192 reader->buffer = section->buffer;
f664829e 7193 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7194 reader->comp_dir = NULL;
685af9cd 7195 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7196}
7197
b0c7bfa9
DE
7198/* Subroutine of init_cutu_and_read_dies to simplify it.
7199 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7200 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7201 already.
7202
7203 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7204 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7205 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7206 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7207 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7208 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7209 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7210 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7211 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7212 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7213 kept around for at least as long as *RESULT_READER.
7214
b0c7bfa9
DE
7215 The result is non-zero if a valid (non-dummy) DIE was found. */
7216
7217static int
7218read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7219 struct dwo_unit *dwo_unit,
b0c7bfa9 7220 struct die_info *stub_comp_unit_die,
a2ce51a0 7221 const char *stub_comp_dir,
b0c7bfa9 7222 struct die_reader_specs *result_reader,
d521ce57 7223 const gdb_byte **result_info_ptr,
b0c7bfa9 7224 struct die_info **result_comp_unit_die,
685af9cd
TT
7225 int *result_has_children,
7226 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7227{
ed2dc618 7228 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7229 struct objfile *objfile = dwarf2_per_objfile->objfile;
7230 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7231 bfd *abfd;
d521ce57 7232 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7233 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7234 int i,num_extra_attrs;
7235 struct dwarf2_section_info *dwo_abbrev_section;
7236 struct attribute *attr;
7237 struct die_info *comp_unit_die;
7238
b0aeadb3
DE
7239 /* At most one of these may be provided. */
7240 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7241
b0c7bfa9
DE
7242 /* These attributes aren't processed until later:
7243 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7244 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7245 referenced later. However, these attributes are found in the stub
7246 which we won't have later. In order to not impose this complication
7247 on the rest of the code, we read them here and copy them to the
7248 DWO CU/TU die. */
b0c7bfa9
DE
7249
7250 stmt_list = NULL;
7251 low_pc = NULL;
7252 high_pc = NULL;
7253 ranges = NULL;
7254 comp_dir = NULL;
7255
7256 if (stub_comp_unit_die != NULL)
7257 {
7258 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7259 DWO file. */
7260 if (! this_cu->is_debug_types)
7261 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7262 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7263 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7264 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7265 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7266
7267 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7268 We need the value before we can process DW_FORM_GNU_addr_index
7269 or DW_FORM_addrx. */
b0c7bfa9
DE
7270 cu->addr_base = 0;
7271 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7272 if (attr != nullptr)
b0c7bfa9
DE
7273 cu->addr_base = DW_UNSND (attr);
7274
7275 /* There should be a DW_AT_ranges_base attribute here (if needed).
7276 We need the value before we can process DW_AT_ranges. */
7277 cu->ranges_base = 0;
7278 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7279 if (attr != nullptr)
b0c7bfa9
DE
7280 cu->ranges_base = DW_UNSND (attr);
7281 }
a2ce51a0
DE
7282 else if (stub_comp_dir != NULL)
7283 {
7284 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7285 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7286 comp_dir->name = DW_AT_comp_dir;
7287 comp_dir->form = DW_FORM_string;
7288 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7289 DW_STRING (comp_dir) = stub_comp_dir;
7290 }
b0c7bfa9
DE
7291
7292 /* Set up for reading the DWO CU/TU. */
7293 cu->dwo_unit = dwo_unit;
685af9cd 7294 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7295 dwarf2_read_section (objfile, section);
a32a8923 7296 abfd = get_section_bfd_owner (section);
9c541725
PA
7297 begin_info_ptr = info_ptr = (section->buffer
7298 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7299 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7300
7301 if (this_cu->is_debug_types)
7302 {
b0c7bfa9
DE
7303 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7304
ed2dc618
SM
7305 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7306 &cu->header, section,
b0c7bfa9 7307 dwo_abbrev_section,
43988095 7308 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7309 /* This is not an assert because it can be caused by bad debug info. */
43988095 7310 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7311 {
7312 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7313 " TU at offset %s [in module %s]"),
a2ce51a0 7314 hex_string (sig_type->signature),
43988095 7315 hex_string (cu->header.signature),
9d8780f0 7316 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7317 bfd_get_filename (abfd));
7318 }
9c541725 7319 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7320 /* For DWOs coming from DWP files, we don't know the CU length
7321 nor the type's offset in the TU until now. */
7322 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7323 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7324
7325 /* Establish the type offset that can be used to lookup the type.
7326 For DWO files, we don't know it until now. */
9c541725
PA
7327 sig_type->type_offset_in_section
7328 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7329 }
7330 else
7331 {
ed2dc618
SM
7332 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7333 &cu->header, section,
b0c7bfa9 7334 dwo_abbrev_section,
43988095 7335 info_ptr, rcuh_kind::COMPILE);
9c541725 7336 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7337 /* For DWOs coming from DWP files, we don't know the CU length
7338 until now. */
7339 dwo_unit->length = get_cu_length (&cu->header);
7340 }
7341
685af9cd
TT
7342 *result_dwo_abbrev_table
7343 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7344 cu->header.abbrev_sect_off);
7345 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7346 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7347
7348 /* Read in the die, but leave space to copy over the attributes
7349 from the stub. This has the benefit of simplifying the rest of
7350 the code - all the work to maintain the illusion of a single
7351 DW_TAG_{compile,type}_unit DIE is done here. */
7352 num_extra_attrs = ((stmt_list != NULL)
7353 + (low_pc != NULL)
7354 + (high_pc != NULL)
7355 + (ranges != NULL)
7356 + (comp_dir != NULL));
7357 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7358 result_has_children, num_extra_attrs);
7359
7360 /* Copy over the attributes from the stub to the DIE we just read in. */
7361 comp_unit_die = *result_comp_unit_die;
7362 i = comp_unit_die->num_attrs;
7363 if (stmt_list != NULL)
7364 comp_unit_die->attrs[i++] = *stmt_list;
7365 if (low_pc != NULL)
7366 comp_unit_die->attrs[i++] = *low_pc;
7367 if (high_pc != NULL)
7368 comp_unit_die->attrs[i++] = *high_pc;
7369 if (ranges != NULL)
7370 comp_unit_die->attrs[i++] = *ranges;
7371 if (comp_dir != NULL)
7372 comp_unit_die->attrs[i++] = *comp_dir;
7373 comp_unit_die->num_attrs += num_extra_attrs;
7374
b4f54984 7375 if (dwarf_die_debug)
bf6af496
DE
7376 {
7377 fprintf_unfiltered (gdb_stdlog,
7378 "Read die from %s@0x%x of %s:\n",
a32a8923 7379 get_section_name (section),
bf6af496
DE
7380 (unsigned) (begin_info_ptr - section->buffer),
7381 bfd_get_filename (abfd));
b4f54984 7382 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7383 }
7384
a2ce51a0
DE
7385 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7386 TUs by skipping the stub and going directly to the entry in the DWO file.
7387 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7388 to get it via circuitous means. Blech. */
7389 if (comp_dir != NULL)
7390 result_reader->comp_dir = DW_STRING (comp_dir);
7391
b0c7bfa9
DE
7392 /* Skip dummy compilation units. */
7393 if (info_ptr >= begin_info_ptr + dwo_unit->length
7394 || peek_abbrev_code (abfd, info_ptr) == 0)
7395 return 0;
7396
7397 *result_info_ptr = info_ptr;
7398 return 1;
7399}
7400
a084a2a6
AT
7401/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7402 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7403 signature is part of the header. */
7404static gdb::optional<ULONGEST>
7405lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7406{
7407 if (cu->header.version >= 5)
7408 return cu->header.signature;
7409 struct attribute *attr;
7410 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7411 if (attr == nullptr)
7412 return gdb::optional<ULONGEST> ();
7413 return DW_UNSND (attr);
7414}
7415
b0c7bfa9
DE
7416/* Subroutine of init_cutu_and_read_dies to simplify it.
7417 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7418 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7419
7420static struct dwo_unit *
7421lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7422 struct die_info *comp_unit_die)
7423{
7424 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7425 struct dwo_unit *dwo_unit;
7426 const char *comp_dir, *dwo_name;
7427
a2ce51a0
DE
7428 gdb_assert (cu != NULL);
7429
b0c7bfa9 7430 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7431 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7432 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7433
7434 if (this_cu->is_debug_types)
7435 {
7436 struct signatured_type *sig_type;
7437
7438 /* Since this_cu is the first member of struct signatured_type,
7439 we can go from a pointer to one to a pointer to the other. */
7440 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7441 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7442 }
7443 else
7444 {
a084a2a6
AT
7445 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7446 if (!signature.has_value ())
b0c7bfa9
DE
7447 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7448 " [in module %s]"),
e3b94546 7449 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7450 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7451 *signature);
b0c7bfa9
DE
7452 }
7453
b0c7bfa9
DE
7454 return dwo_unit;
7455}
7456
a2ce51a0 7457/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7458 See it for a description of the parameters.
fcd3b13d 7459 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7460
7461static void
6aa5f3a6
DE
7462init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7463 int use_existing_cu, int keep,
a2ce51a0
DE
7464 die_reader_func_ftype *die_reader_func,
7465 void *data)
7466{
fcd3b13d 7467 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7468 struct signatured_type *sig_type;
a2ce51a0
DE
7469 struct die_reader_specs reader;
7470 const gdb_byte *info_ptr;
7471 struct die_info *comp_unit_die;
7472 int has_children;
ed2dc618 7473 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7474
7475 /* Verify we can do the following downcast, and that we have the
7476 data we need. */
7477 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7478 sig_type = (struct signatured_type *) this_cu;
7479 gdb_assert (sig_type->dwo_unit != NULL);
7480
6aa5f3a6
DE
7481 if (use_existing_cu && this_cu->cu != NULL)
7482 {
7483 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7484 /* There's no need to do the rereading_dwo_cu handling that
7485 init_cutu_and_read_dies does since we don't read the stub. */
7486 }
7487 else
7488 {
7489 /* If !use_existing_cu, this_cu->cu must be NULL. */
7490 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7491 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7492 }
7493
7494 /* A future optimization, if needed, would be to use an existing
7495 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7496 could share abbrev tables. */
a2ce51a0 7497
685af9cd
TT
7498 /* The abbreviation table used by READER, this must live at least as long as
7499 READER. */
7500 abbrev_table_up dwo_abbrev_table;
7501
a2ce51a0 7502 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7503 NULL /* stub_comp_unit_die */,
7504 sig_type->dwo_unit->dwo_file->comp_dir,
7505 &reader, &info_ptr,
685af9cd
TT
7506 &comp_unit_die, &has_children,
7507 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7508 {
7509 /* Dummy die. */
a2ce51a0
DE
7510 return;
7511 }
7512
7513 /* All the "real" work is done here. */
7514 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7515
6aa5f3a6 7516 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7517 but the alternative is making the latter more complex.
7518 This function is only for the special case of using DWO files directly:
7519 no point in overly complicating the general case just to handle this. */
fcd3b13d 7520 if (new_cu != NULL && keep)
a2ce51a0 7521 {
fcd3b13d
SM
7522 /* Link this CU into read_in_chain. */
7523 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7524 dwarf2_per_objfile->read_in_chain = this_cu;
7525 /* The chain owns it now. */
7526 new_cu.release ();
a2ce51a0 7527 }
a2ce51a0
DE
7528}
7529
fd820528 7530/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7531 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7532
f4dc4d17
DE
7533 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7534 Otherwise the table specified in the comp unit header is read in and used.
7535 This is an optimization for when we already have the abbrev table.
7536
dee91e82
DE
7537 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7538 Otherwise, a new CU is allocated with xmalloc.
7539
7540 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7541 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7542
7543 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7544 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7545
70221824 7546static void
fd820528 7547init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7548 struct abbrev_table *abbrev_table,
fd820528 7549 int use_existing_cu, int keep,
58f0c718 7550 bool skip_partial,
fd820528
DE
7551 die_reader_func_ftype *die_reader_func,
7552 void *data)
c906108c 7553{
ed2dc618 7554 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7555 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7556 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7557 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7558 struct dwarf2_cu *cu;
d521ce57 7559 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7560 struct die_reader_specs reader;
d85a05f0 7561 struct die_info *comp_unit_die;
dee91e82 7562 int has_children;
dee91e82 7563 struct signatured_type *sig_type = NULL;
4bdcc0c1 7564 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7565 /* Non-zero if CU currently points to a DWO file and we need to
7566 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7567 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7568 int rereading_dwo_cu = 0;
c906108c 7569
b4f54984 7570 if (dwarf_die_debug)
9d8780f0 7571 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7572 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7573 sect_offset_str (this_cu->sect_off));
09406207 7574
dee91e82
DE
7575 if (use_existing_cu)
7576 gdb_assert (keep);
23745b47 7577
a2ce51a0
DE
7578 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7579 file (instead of going through the stub), short-circuit all of this. */
7580 if (this_cu->reading_dwo_directly)
7581 {
7582 /* Narrow down the scope of possibilities to have to understand. */
7583 gdb_assert (this_cu->is_debug_types);
7584 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7585 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7586 die_reader_func, data);
a2ce51a0
DE
7587 return;
7588 }
7589
dee91e82
DE
7590 /* This is cheap if the section is already read in. */
7591 dwarf2_read_section (objfile, section);
7592
9c541725 7593 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7594
7595 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7596
fcd3b13d 7597 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7598 if (use_existing_cu && this_cu->cu != NULL)
7599 {
7600 cu = this_cu->cu;
42e7ad6c
DE
7601 /* If this CU is from a DWO file we need to start over, we need to
7602 refetch the attributes from the skeleton CU.
7603 This could be optimized by retrieving those attributes from when we
7604 were here the first time: the previous comp_unit_die was stored in
7605 comp_unit_obstack. But there's no data yet that we need this
7606 optimization. */
7607 if (cu->dwo_unit != NULL)
7608 rereading_dwo_cu = 1;
dee91e82
DE
7609 }
7610 else
7611 {
7612 /* If !use_existing_cu, this_cu->cu must be NULL. */
7613 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7614 new_cu.reset (new dwarf2_cu (this_cu));
7615 cu = new_cu.get ();
42e7ad6c 7616 }
dee91e82 7617
b0c7bfa9 7618 /* Get the header. */
9c541725 7619 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7620 {
7621 /* We already have the header, there's no need to read it in again. */
9c541725 7622 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7623 }
7624 else
7625 {
3019eac3 7626 if (this_cu->is_debug_types)
dee91e82 7627 {
ed2dc618
SM
7628 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7629 &cu->header, section,
4bdcc0c1 7630 abbrev_section, info_ptr,
43988095 7631 rcuh_kind::TYPE);
dee91e82 7632
42e7ad6c
DE
7633 /* Since per_cu is the first member of struct signatured_type,
7634 we can go from a pointer to one to a pointer to the other. */
7635 sig_type = (struct signatured_type *) this_cu;
43988095 7636 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7637 gdb_assert (sig_type->type_offset_in_tu
7638 == cu->header.type_cu_offset_in_tu);
7639 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7640
42e7ad6c
DE
7641 /* LENGTH has not been set yet for type units if we're
7642 using .gdb_index. */
1ce1cefd 7643 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7644
7645 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7646 sig_type->type_offset_in_section =
7647 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7648
7649 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7650 }
7651 else
7652 {
ed2dc618
SM
7653 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7654 &cu->header, section,
4bdcc0c1 7655 abbrev_section,
43988095
JK
7656 info_ptr,
7657 rcuh_kind::COMPILE);
dee91e82 7658
9c541725 7659 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7660 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7661 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7662 }
7663 }
10b3939b 7664
6caca83c 7665 /* Skip dummy compilation units. */
dee91e82 7666 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7667 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7668 return;
6caca83c 7669
433df2d4
DE
7670 /* If we don't have them yet, read the abbrevs for this compilation unit.
7671 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7672 done (own the table through ABBREV_TABLE_HOLDER). */
7673 abbrev_table_up abbrev_table_holder;
f4dc4d17 7674 if (abbrev_table != NULL)
685af9cd
TT
7675 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7676 else
f4dc4d17 7677 {
685af9cd
TT
7678 abbrev_table_holder
7679 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7680 cu->header.abbrev_sect_off);
7681 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7682 }
af703f96 7683
dee91e82 7684 /* Read the top level CU/TU die. */
685af9cd 7685 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7686 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7687
58f0c718
TT
7688 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7689 return;
7690
b0c7bfa9 7691 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7692 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7693 table from the DWO file and pass the ownership over to us. It will be
7694 referenced from READER, so we must make sure to free it after we're done
7695 with READER.
7696
b0c7bfa9
DE
7697 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7698 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7699 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7700 abbrev_table_up dwo_abbrev_table;
a084a2a6 7701 if (dwo_name != nullptr)
3019eac3 7702 {
3019eac3 7703 struct dwo_unit *dwo_unit;
b0c7bfa9 7704 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7705
7706 if (has_children)
6a506a2d 7707 {
b98664d3 7708 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7709 " has children (offset %s) [in module %s]"),
7710 sect_offset_str (this_cu->sect_off),
7711 bfd_get_filename (abfd));
6a506a2d 7712 }
b0c7bfa9 7713 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7714 if (dwo_unit != NULL)
3019eac3 7715 {
6a506a2d 7716 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7717 comp_unit_die, NULL,
6a506a2d 7718 &reader, &info_ptr,
685af9cd
TT
7719 &dwo_comp_unit_die, &has_children,
7720 &dwo_abbrev_table) == 0)
6a506a2d
DE
7721 {
7722 /* Dummy die. */
6a506a2d
DE
7723 return;
7724 }
7725 comp_unit_die = dwo_comp_unit_die;
7726 }
7727 else
7728 {
7729 /* Yikes, we couldn't find the rest of the DIE, we only have
7730 the stub. A complaint has already been logged. There's
7731 not much more we can do except pass on the stub DIE to
7732 die_reader_func. We don't want to throw an error on bad
7733 debug info. */
3019eac3
DE
7734 }
7735 }
7736
b0c7bfa9 7737 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7738 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7739
b0c7bfa9 7740 /* Done, clean up. */
fcd3b13d 7741 if (new_cu != NULL && keep)
348e048f 7742 {
fcd3b13d
SM
7743 /* Link this CU into read_in_chain. */
7744 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7745 dwarf2_per_objfile->read_in_chain = this_cu;
7746 /* The chain owns it now. */
7747 new_cu.release ();
348e048f 7748 }
dee91e82
DE
7749}
7750
33e80786
DE
7751/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7752 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7753 to have already done the lookup to find the DWO file).
dee91e82
DE
7754
7755 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7756 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7757
7758 We fill in THIS_CU->length.
7759
7760 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7761 linker) then DIE_READER_FUNC will not get called.
7762
7763 THIS_CU->cu is always freed when done.
3019eac3
DE
7764 This is done in order to not leave THIS_CU->cu in a state where we have
7765 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7766
7767static void
7768init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7769 struct dwo_file *dwo_file,
dee91e82
DE
7770 die_reader_func_ftype *die_reader_func,
7771 void *data)
7772{
ed2dc618 7773 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7774 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7775 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7776 bfd *abfd = get_section_bfd_owner (section);
33e80786 7777 struct dwarf2_section_info *abbrev_section;
d521ce57 7778 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7779 struct die_reader_specs reader;
dee91e82
DE
7780 struct die_info *comp_unit_die;
7781 int has_children;
7782
b4f54984 7783 if (dwarf_die_debug)
9d8780f0 7784 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7785 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7786 sect_offset_str (this_cu->sect_off));
09406207 7787
dee91e82
DE
7788 gdb_assert (this_cu->cu == NULL);
7789
33e80786
DE
7790 abbrev_section = (dwo_file != NULL
7791 ? &dwo_file->sections.abbrev
7792 : get_abbrev_section_for_cu (this_cu));
7793
dee91e82
DE
7794 /* This is cheap if the section is already read in. */
7795 dwarf2_read_section (objfile, section);
7796
fcd3b13d 7797 struct dwarf2_cu cu (this_cu);
dee91e82 7798
9c541725 7799 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7800 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7801 &cu.header, section,
4bdcc0c1 7802 abbrev_section, info_ptr,
43988095
JK
7803 (this_cu->is_debug_types
7804 ? rcuh_kind::TYPE
7805 : rcuh_kind::COMPILE));
dee91e82 7806
1ce1cefd 7807 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7808
7809 /* Skip dummy compilation units. */
7810 if (info_ptr >= begin_info_ptr + this_cu->length
7811 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7812 return;
72bf9492 7813
685af9cd
TT
7814 abbrev_table_up abbrev_table
7815 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7816 cu.header.abbrev_sect_off);
dee91e82 7817
685af9cd 7818 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7819 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7820
7821 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7822}
7823
3019eac3
DE
7824/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7825 does not lookup the specified DWO file.
7826 This cannot be used to read DWO files.
dee91e82
DE
7827
7828 THIS_CU->cu is always freed when done.
3019eac3
DE
7829 This is done in order to not leave THIS_CU->cu in a state where we have
7830 to care whether it refers to the "main" CU or the DWO CU.
7831 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7832
7833static void
7834init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7835 die_reader_func_ftype *die_reader_func,
7836 void *data)
7837{
33e80786 7838 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7839}
0018ea6f
DE
7840\f
7841/* Type Unit Groups.
dee91e82 7842
0018ea6f
DE
7843 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7844 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7845 so that all types coming from the same compilation (.o file) are grouped
7846 together. A future step could be to put the types in the same symtab as
7847 the CU the types ultimately came from. */
ff013f42 7848
f4dc4d17
DE
7849static hashval_t
7850hash_type_unit_group (const void *item)
7851{
9a3c8263
SM
7852 const struct type_unit_group *tu_group
7853 = (const struct type_unit_group *) item;
f4dc4d17 7854
094b34ac 7855 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7856}
348e048f
DE
7857
7858static int
f4dc4d17 7859eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7860{
9a3c8263
SM
7861 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7862 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7863
094b34ac 7864 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7865}
348e048f 7866
f4dc4d17
DE
7867/* Allocate a hash table for type unit groups. */
7868
7869static htab_t
ed2dc618 7870allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7871{
7872 return htab_create_alloc_ex (3,
7873 hash_type_unit_group,
7874 eq_type_unit_group,
7875 NULL,
ed2dc618 7876 &objfile->objfile_obstack,
f4dc4d17
DE
7877 hashtab_obstack_allocate,
7878 dummy_obstack_deallocate);
7879}
dee91e82 7880
f4dc4d17
DE
7881/* Type units that don't have DW_AT_stmt_list are grouped into their own
7882 partial symtabs. We combine several TUs per psymtab to not let the size
7883 of any one psymtab grow too big. */
7884#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7885#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7886
094b34ac 7887/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7888 Create the type_unit_group object used to hold one or more TUs. */
7889
7890static struct type_unit_group *
094b34ac 7891create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7892{
518817b3
SM
7893 struct dwarf2_per_objfile *dwarf2_per_objfile
7894 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7895 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7896 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7897 struct type_unit_group *tu_group;
f4dc4d17
DE
7898
7899 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7900 struct type_unit_group);
094b34ac 7901 per_cu = &tu_group->per_cu;
518817b3 7902 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7903
094b34ac
DE
7904 if (dwarf2_per_objfile->using_index)
7905 {
7906 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7907 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7908 }
7909 else
7910 {
9c541725 7911 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7912 struct partial_symtab *pst;
528e1572 7913 std::string name;
094b34ac
DE
7914
7915 /* Give the symtab a useful name for debug purposes. */
7916 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7917 name = string_printf ("<type_units_%d>",
7918 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7919 else
528e1572 7920 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7921
528e1572 7922 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7923 pst->anonymous = 1;
094b34ac 7924 }
f4dc4d17 7925
094b34ac 7926 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7927 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7928
7929 return tu_group;
7930}
7931
094b34ac
DE
7932/* Look up the type_unit_group for type unit CU, and create it if necessary.
7933 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7934
7935static struct type_unit_group *
ff39bb5e 7936get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7937{
518817b3
SM
7938 struct dwarf2_per_objfile *dwarf2_per_objfile
7939 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7940 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7941 struct type_unit_group *tu_group;
7942 void **slot;
7943 unsigned int line_offset;
7944 struct type_unit_group type_unit_group_for_lookup;
7945
7946 if (dwarf2_per_objfile->type_unit_groups == NULL)
7947 {
7948 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7949 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7950 }
7951
7952 /* Do we need to create a new group, or can we use an existing one? */
7953
7954 if (stmt_list)
7955 {
7956 line_offset = DW_UNSND (stmt_list);
7957 ++tu_stats->nr_symtab_sharers;
7958 }
7959 else
7960 {
7961 /* Ugh, no stmt_list. Rare, but we have to handle it.
7962 We can do various things here like create one group per TU or
7963 spread them over multiple groups to split up the expansion work.
7964 To avoid worst case scenarios (too many groups or too large groups)
7965 we, umm, group them in bunches. */
7966 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7967 | (tu_stats->nr_stmt_less_type_units
7968 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7969 ++tu_stats->nr_stmt_less_type_units;
7970 }
7971
094b34ac 7972 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7973 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7974 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7975 &type_unit_group_for_lookup, INSERT);
7976 if (*slot != NULL)
7977 {
9a3c8263 7978 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7979 gdb_assert (tu_group != NULL);
7980 }
7981 else
7982 {
9c541725 7983 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7984 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7985 *slot = tu_group;
7986 ++tu_stats->nr_symtabs;
7987 }
7988
7989 return tu_group;
7990}
0018ea6f
DE
7991\f
7992/* Partial symbol tables. */
7993
7994/* Create a psymtab named NAME and assign it to PER_CU.
7995
7996 The caller must fill in the following details:
7997 dirname, textlow, texthigh. */
7998
7999static struct partial_symtab *
8000create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8001{
e3b94546 8002 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8003 struct partial_symtab *pst;
8004
939652a5 8005 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8006
8007 pst->psymtabs_addrmap_supported = 1;
8008
8009 /* This is the glue that links PST into GDB's symbol API. */
8010 pst->read_symtab_private = per_cu;
8011 pst->read_symtab = dwarf2_read_symtab;
8012 per_cu->v.psymtab = pst;
8013
8014 return pst;
8015}
8016
b93601f3
TT
8017/* The DATA object passed to process_psymtab_comp_unit_reader has this
8018 type. */
8019
8020struct process_psymtab_comp_unit_data
8021{
8022 /* True if we are reading a DW_TAG_partial_unit. */
8023
8024 int want_partial_unit;
8025
8026 /* The "pretend" language that is used if the CU doesn't declare a
8027 language. */
8028
8029 enum language pretend_language;
8030};
8031
0018ea6f
DE
8032/* die_reader_func for process_psymtab_comp_unit. */
8033
8034static void
8035process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8036 const gdb_byte *info_ptr,
0018ea6f
DE
8037 struct die_info *comp_unit_die,
8038 int has_children,
8039 void *data)
8040{
8041 struct dwarf2_cu *cu = reader->cu;
518817b3 8042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8045 CORE_ADDR baseaddr;
8046 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8047 struct partial_symtab *pst;
3a2b436a 8048 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8049 const char *filename;
9a3c8263
SM
8050 struct process_psymtab_comp_unit_data *info
8051 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8052
b93601f3 8053 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8054 return;
8055
8056 gdb_assert (! per_cu->is_debug_types);
8057
b93601f3 8058 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8059
0018ea6f 8060 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8061 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8062 if (filename == NULL)
0018ea6f 8063 filename = "";
0018ea6f
DE
8064
8065 pst = create_partial_symtab (per_cu, filename);
8066
8067 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8068 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 8069
6a053cb1 8070 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
0018ea6f
DE
8071
8072 dwarf2_find_base_address (comp_unit_die, cu);
8073
8074 /* Possibly set the default values of LOWPC and HIGHPC from
8075 `DW_AT_ranges'. */
3a2b436a
JK
8076 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8077 &best_highpc, cu, pst);
8078 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8079 {
8080 CORE_ADDR low
8081 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8082 - baseaddr);
8083 CORE_ADDR high
8084 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8085 - baseaddr - 1);
8086 /* Store the contiguous range if it is not empty; it can be
8087 empty for CUs with no code. */
d320c2b5
TT
8088 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8089 low, high, pst);
79748972 8090 }
0018ea6f
DE
8091
8092 /* Check if comp unit has_children.
8093 If so, read the rest of the partial symbols from this comp unit.
8094 If not, there's no more debug_info for this comp unit. */
8095 if (has_children)
8096 {
8097 struct partial_die_info *first_die;
8098 CORE_ADDR lowpc, highpc;
8099
8100 lowpc = ((CORE_ADDR) -1);
8101 highpc = ((CORE_ADDR) 0);
8102
8103 first_die = load_partial_dies (reader, info_ptr, 1);
8104
8105 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8106 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8107
8108 /* If we didn't find a lowpc, set it to highpc to avoid
8109 complaints from `maint check'. */
8110 if (lowpc == ((CORE_ADDR) -1))
8111 lowpc = highpc;
8112
8113 /* If the compilation unit didn't have an explicit address range,
8114 then use the information extracted from its child dies. */
e385593e 8115 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8116 {
8117 best_lowpc = lowpc;
8118 best_highpc = highpc;
8119 }
8120 }
4ae976d1 8121 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8122 best_lowpc + baseaddr)
8123 - baseaddr);
4ae976d1 8124 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8125 best_highpc + baseaddr)
8126 - baseaddr);
0018ea6f 8127
8763cede 8128 end_psymtab_common (objfile, pst);
0018ea6f 8129
ae640021 8130 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8131 {
8132 int i;
ae640021 8133 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8134
8135 /* Fill in 'dependencies' here; we fill in 'users' in a
8136 post-pass. */
8137 pst->number_of_dependencies = len;
a9342b62
TT
8138 pst->dependencies
8139 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8140 for (i = 0; i < len; ++i)
8141 {
8142 pst->dependencies[i]
8143 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8144 }
0018ea6f 8145
ae640021 8146 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8147 }
8148
8149 /* Get the list of files included in the current compilation unit,
8150 and build a psymtab for each of them. */
8151 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8152
b4f54984 8153 if (dwarf_read_debug)
b926417a
TT
8154 fprintf_unfiltered (gdb_stdlog,
8155 "Psymtab for %s unit @%s: %s - %s"
8156 ", %d global, %d static syms\n",
8157 per_cu->is_debug_types ? "type" : "comp",
8158 sect_offset_str (per_cu->sect_off),
8159 paddress (gdbarch, pst->text_low (objfile)),
8160 paddress (gdbarch, pst->text_high (objfile)),
8161 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8162}
8163
8164/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8165 Process compilation unit THIS_CU for a psymtab. */
8166
8167static void
8168process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8169 int want_partial_unit,
8170 enum language pretend_language)
0018ea6f
DE
8171{
8172 /* If this compilation unit was already read in, free the
8173 cached copy in order to read it in again. This is
8174 necessary because we skipped some symbols when we first
8175 read in the compilation unit (see load_partial_dies).
8176 This problem could be avoided, but the benefit is unclear. */
8177 if (this_cu->cu != NULL)
8178 free_one_cached_comp_unit (this_cu);
8179
f1902523 8180 if (this_cu->is_debug_types)
58f0c718
TT
8181 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8182 build_type_psymtabs_reader, NULL);
f1902523
JK
8183 else
8184 {
8185 process_psymtab_comp_unit_data info;
8186 info.want_partial_unit = want_partial_unit;
8187 info.pretend_language = pretend_language;
58f0c718 8188 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8189 process_psymtab_comp_unit_reader, &info);
8190 }
0018ea6f
DE
8191
8192 /* Age out any secondary CUs. */
ed2dc618 8193 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8194}
f4dc4d17
DE
8195
8196/* Reader function for build_type_psymtabs. */
8197
8198static void
8199build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8200 const gdb_byte *info_ptr,
f4dc4d17
DE
8201 struct die_info *type_unit_die,
8202 int has_children,
8203 void *data)
8204{
ed2dc618 8205 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8206 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8207 struct objfile *objfile = dwarf2_per_objfile->objfile;
8208 struct dwarf2_cu *cu = reader->cu;
8209 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8210 struct signatured_type *sig_type;
f4dc4d17
DE
8211 struct type_unit_group *tu_group;
8212 struct attribute *attr;
8213 struct partial_die_info *first_die;
8214 CORE_ADDR lowpc, highpc;
8215 struct partial_symtab *pst;
8216
8217 gdb_assert (data == NULL);
0186c6a7
DE
8218 gdb_assert (per_cu->is_debug_types);
8219 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8220
8221 if (! has_children)
8222 return;
8223
8224 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8225 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8226
df07e2c7 8227 if (tu_group->tus == nullptr)
a8b3b8e9 8228 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8229 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8230
8231 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8232 pst = create_partial_symtab (per_cu, "");
8233 pst->anonymous = 1;
8234
8235 first_die = load_partial_dies (reader, info_ptr, 1);
8236
8237 lowpc = (CORE_ADDR) -1;
8238 highpc = (CORE_ADDR) 0;
8239 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8240
8763cede 8241 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8242}
8243
73051182
DE
8244/* Struct used to sort TUs by their abbreviation table offset. */
8245
8246struct tu_abbrev_offset
8247{
b2bdb8cf
SM
8248 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8249 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8250 {}
8251
8252 signatured_type *sig_type;
73051182
DE
8253 sect_offset abbrev_offset;
8254};
8255
484cf504 8256/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8257
484cf504
TT
8258static bool
8259sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8260 const struct tu_abbrev_offset &b)
73051182 8261{
484cf504 8262 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8263}
8264
8265/* Efficiently read all the type units.
8266 This does the bulk of the work for build_type_psymtabs.
8267
8268 The efficiency is because we sort TUs by the abbrev table they use and
8269 only read each abbrev table once. In one program there are 200K TUs
8270 sharing 8K abbrev tables.
8271
8272 The main purpose of this function is to support building the
8273 dwarf2_per_objfile->type_unit_groups table.
8274 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8275 can collapse the search space by grouping them by stmt_list.
8276 The savings can be significant, in the same program from above the 200K TUs
8277 share 8K stmt_list tables.
8278
8279 FUNC is expected to call get_type_unit_group, which will create the
8280 struct type_unit_group if necessary and add it to
8281 dwarf2_per_objfile->type_unit_groups. */
8282
8283static void
ed2dc618 8284build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8285{
73051182 8286 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8287 abbrev_table_up abbrev_table;
73051182 8288 sect_offset abbrev_offset;
73051182
DE
8289
8290 /* It's up to the caller to not call us multiple times. */
8291 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8292
b2bdb8cf 8293 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8294 return;
8295
8296 /* TUs typically share abbrev tables, and there can be way more TUs than
8297 abbrev tables. Sort by abbrev table to reduce the number of times we
8298 read each abbrev table in.
8299 Alternatives are to punt or to maintain a cache of abbrev tables.
8300 This is simpler and efficient enough for now.
8301
8302 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8303 symtab to use). Typically TUs with the same abbrev offset have the same
8304 stmt_list value too so in practice this should work well.
8305
8306 The basic algorithm here is:
8307
8308 sort TUs by abbrev table
8309 for each TU with same abbrev table:
8310 read abbrev table if first user
8311 read TU top level DIE
8312 [IWBN if DWO skeletons had DW_AT_stmt_list]
8313 call FUNC */
8314
b4f54984 8315 if (dwarf_read_debug)
73051182
DE
8316 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8317
8318 /* Sort in a separate table to maintain the order of all_type_units
8319 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8320 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8321 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8322
8323 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8324 sorted_by_abbrev.emplace_back
8325 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8326 sig_type->per_cu.section,
8327 sig_type->per_cu.sect_off));
73051182 8328
484cf504
TT
8329 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8330 sort_tu_by_abbrev_offset);
73051182 8331
9c541725 8332 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8333
b2bdb8cf 8334 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8335 {
73051182
DE
8336 /* Switch to the next abbrev table if necessary. */
8337 if (abbrev_table == NULL
b2bdb8cf 8338 || tu.abbrev_offset != abbrev_offset)
73051182 8339 {
b2bdb8cf 8340 abbrev_offset = tu.abbrev_offset;
73051182 8341 abbrev_table =
ed2dc618
SM
8342 abbrev_table_read_table (dwarf2_per_objfile,
8343 &dwarf2_per_objfile->abbrev,
73051182
DE
8344 abbrev_offset);
8345 ++tu_stats->nr_uniq_abbrev_tables;
8346 }
8347
b2bdb8cf 8348 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8349 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8350 }
6aa5f3a6 8351}
73051182 8352
6aa5f3a6
DE
8353/* Print collected type unit statistics. */
8354
8355static void
ed2dc618 8356print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8357{
8358 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8359
8360 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8361 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8362 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8363 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8364 tu_stats->nr_uniq_abbrev_tables);
8365 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8366 tu_stats->nr_symtabs);
8367 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8368 tu_stats->nr_symtab_sharers);
8369 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8370 tu_stats->nr_stmt_less_type_units);
8371 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8372 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8373}
8374
f4dc4d17
DE
8375/* Traversal function for build_type_psymtabs. */
8376
8377static int
8378build_type_psymtab_dependencies (void **slot, void *info)
8379{
ed2dc618
SM
8380 struct dwarf2_per_objfile *dwarf2_per_objfile
8381 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8382 struct objfile *objfile = dwarf2_per_objfile->objfile;
8383 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8384 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8385 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8386 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8387 int i;
8388
8389 gdb_assert (len > 0);
0186c6a7 8390 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8391
8392 pst->number_of_dependencies = len;
a9342b62 8393 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8394 for (i = 0; i < len; ++i)
f4dc4d17 8395 {
df07e2c7 8396 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8397 gdb_assert (iter->per_cu.is_debug_types);
8398 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8399 iter->type_unit_group = tu_group;
f4dc4d17
DE
8400 }
8401
df07e2c7
AB
8402 delete tu_group->tus;
8403 tu_group->tus = nullptr;
348e048f
DE
8404
8405 return 1;
8406}
8407
8408/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8409 Build partial symbol tables for the .debug_types comp-units. */
8410
8411static void
ed2dc618 8412build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8413{
ed2dc618 8414 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8415 return;
8416
ed2dc618 8417 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8418}
f4dc4d17 8419
6aa5f3a6
DE
8420/* Traversal function for process_skeletonless_type_unit.
8421 Read a TU in a DWO file and build partial symbols for it. */
8422
8423static int
8424process_skeletonless_type_unit (void **slot, void *info)
8425{
8426 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8427 struct dwarf2_per_objfile *dwarf2_per_objfile
8428 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8429 struct signatured_type find_entry, *entry;
8430
8431 /* If this TU doesn't exist in the global table, add it and read it in. */
8432
8433 if (dwarf2_per_objfile->signatured_types == NULL)
8434 {
8435 dwarf2_per_objfile->signatured_types
ed2dc618 8436 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8437 }
8438
8439 find_entry.signature = dwo_unit->signature;
8440 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8441 INSERT);
8442 /* If we've already seen this type there's nothing to do. What's happening
8443 is we're doing our own version of comdat-folding here. */
8444 if (*slot != NULL)
8445 return 1;
8446
8447 /* This does the job that create_all_type_units would have done for
8448 this TU. */
ed2dc618
SM
8449 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8450 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8451 *slot = entry;
8452
8453 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8454 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8455 build_type_psymtabs_reader, NULL);
8456
8457 return 1;
8458}
8459
8460/* Traversal function for process_skeletonless_type_units. */
8461
8462static int
8463process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8464{
8465 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8466
8467 if (dwo_file->tus != NULL)
8468 {
8469 htab_traverse_noresize (dwo_file->tus,
8470 process_skeletonless_type_unit, info);
8471 }
8472
8473 return 1;
8474}
8475
8476/* Scan all TUs of DWO files, verifying we've processed them.
8477 This is needed in case a TU was emitted without its skeleton.
8478 Note: This can't be done until we know what all the DWO files are. */
8479
8480static void
ed2dc618 8481process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8482{
8483 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8484 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8485 && dwarf2_per_objfile->dwo_files != NULL)
8486 {
51ac9db5 8487 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8488 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8489 dwarf2_per_objfile);
6aa5f3a6 8490 }
348e048f
DE
8491}
8492
ed2dc618 8493/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8494
8495static void
ed2dc618 8496set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8497{
b76e467d 8498 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8499 {
95554aad 8500 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8501
36586728
TT
8502 if (pst == NULL)
8503 continue;
8504
b76e467d 8505 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8506 {
8507 /* Set the 'user' field only if it is not already set. */
8508 if (pst->dependencies[j]->user == NULL)
8509 pst->dependencies[j]->user = pst;
8510 }
8511 }
8512}
8513
93311388
DE
8514/* Build the partial symbol table by doing a quick pass through the
8515 .debug_info and .debug_abbrev sections. */
72bf9492 8516
93311388 8517static void
ed2dc618 8518dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8519{
ed2dc618 8520 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8521
b4f54984 8522 if (dwarf_read_debug)
45cfd468
DE
8523 {
8524 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8525 objfile_name (objfile));
45cfd468
DE
8526 }
8527
98bfdba5
PA
8528 dwarf2_per_objfile->reading_partial_symbols = 1;
8529
be391dca 8530 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8531
93311388
DE
8532 /* Any cached compilation units will be linked by the per-objfile
8533 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8534 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8535
ed2dc618 8536 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8537
ed2dc618 8538 create_all_comp_units (dwarf2_per_objfile);
c906108c 8539
60606b2c
TT
8540 /* Create a temporary address map on a temporary obstack. We later
8541 copy this to the final obstack. */
8268c778 8542 auto_obstack temp_obstack;
791afaa2
TT
8543
8544 scoped_restore save_psymtabs_addrmap
d320c2b5 8545 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8546 addrmap_create_mutable (&temp_obstack));
72bf9492 8547
b76e467d
SM
8548 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8549 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8550
6aa5f3a6 8551 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8552 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8553
8554 /* Now that all TUs have been processed we can fill in the dependencies. */
8555 if (dwarf2_per_objfile->type_unit_groups != NULL)
8556 {
8557 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8558 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8559 }
8560
b4f54984 8561 if (dwarf_read_debug)
ed2dc618 8562 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8563
ed2dc618 8564 set_partial_user (dwarf2_per_objfile);
95554aad 8565
d320c2b5
TT
8566 objfile->partial_symtabs->psymtabs_addrmap
8567 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8568 objfile->partial_symtabs->obstack ());
791afaa2
TT
8569 /* At this point we want to keep the address map. */
8570 save_psymtabs_addrmap.release ();
ff013f42 8571
b4f54984 8572 if (dwarf_read_debug)
45cfd468 8573 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8574 objfile_name (objfile));
ae038cb0
DJ
8575}
8576
3019eac3 8577/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8578
8579static void
dee91e82 8580load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8581 const gdb_byte *info_ptr,
dee91e82
DE
8582 struct die_info *comp_unit_die,
8583 int has_children,
8584 void *data)
ae038cb0 8585{
dee91e82 8586 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8587
95554aad 8588 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8589
ae038cb0
DJ
8590 /* Check if comp unit has_children.
8591 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8592 If not, there's no more debug_info for this comp unit. */
d85a05f0 8593 if (has_children)
dee91e82
DE
8594 load_partial_dies (reader, info_ptr, 0);
8595}
98bfdba5 8596
dee91e82
DE
8597/* Load the partial DIEs for a secondary CU into memory.
8598 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8599
dee91e82
DE
8600static void
8601load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8602{
58f0c718 8603 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8604 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8605}
8606
ae038cb0 8607static void
ed2dc618 8608read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8609 struct dwarf2_section_info *section,
f1902523 8610 struct dwarf2_section_info *abbrev_section,
b76e467d 8611 unsigned int is_dwz)
ae038cb0 8612{
d521ce57 8613 const gdb_byte *info_ptr;
ed2dc618 8614 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8615
b4f54984 8616 if (dwarf_read_debug)
bf6af496 8617 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8618 get_section_name (section),
8619 get_section_file_name (section));
bf6af496 8620
36586728 8621 dwarf2_read_section (objfile, section);
ae038cb0 8622
36586728 8623 info_ptr = section->buffer;
6e70227d 8624
36586728 8625 while (info_ptr < section->buffer + section->size)
ae038cb0 8626 {
ae038cb0 8627 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8628
9c541725 8629 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8630
f1902523 8631 comp_unit_head cu_header;
ed2dc618
SM
8632 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8633 abbrev_section, info_ptr,
8634 rcuh_kind::COMPILE);
ae038cb0
DJ
8635
8636 /* Save the compilation unit for later lookup. */
f1902523
JK
8637 if (cu_header.unit_type != DW_UT_type)
8638 {
8639 this_cu = XOBNEW (&objfile->objfile_obstack,
8640 struct dwarf2_per_cu_data);
8641 memset (this_cu, 0, sizeof (*this_cu));
8642 }
8643 else
8644 {
8645 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8646 struct signatured_type);
8647 memset (sig_type, 0, sizeof (*sig_type));
8648 sig_type->signature = cu_header.signature;
8649 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8650 this_cu = &sig_type->per_cu;
8651 }
8652 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8653 this_cu->sect_off = sect_off;
f1902523 8654 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8655 this_cu->is_dwz = is_dwz;
e3b94546 8656 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8657 this_cu->section = section;
ae038cb0 8658
b76e467d 8659 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8660
8661 info_ptr = info_ptr + this_cu->length;
8662 }
36586728
TT
8663}
8664
8665/* Create a list of all compilation units in OBJFILE.
8666 This is only done for -readnow and building partial symtabs. */
8667
8668static void
ed2dc618 8669create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8670{
b76e467d 8671 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8672 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8673 &dwarf2_per_objfile->abbrev, 0);
36586728 8674
b76e467d 8675 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8676 if (dwz != NULL)
ed2dc618 8677 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8678 1);
c906108c
SS
8679}
8680
5734ee8b 8681/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8682 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8683 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8684 DW_AT_ranges). See the comments of add_partial_subprogram on how
8685 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8686
72bf9492
DJ
8687static void
8688scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8689 CORE_ADDR *highpc, int set_addrmap,
8690 struct dwarf2_cu *cu)
c906108c 8691{
72bf9492 8692 struct partial_die_info *pdi;
c906108c 8693
91c24f0a
DC
8694 /* Now, march along the PDI's, descending into ones which have
8695 interesting children but skipping the children of the other ones,
8696 until we reach the end of the compilation unit. */
c906108c 8697
72bf9492 8698 pdi = first_die;
91c24f0a 8699
72bf9492
DJ
8700 while (pdi != NULL)
8701 {
52356b79 8702 pdi->fixup (cu);
c906108c 8703
f55ee35c 8704 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8705 children, so we need to look at them. Ditto for anonymous
8706 enums. */
933c6fe4 8707
72bf9492 8708 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8709 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8710 || pdi->tag == DW_TAG_imported_unit
8711 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8712 {
72bf9492 8713 switch (pdi->tag)
c906108c
SS
8714 {
8715 case DW_TAG_subprogram:
b1dc1806 8716 case DW_TAG_inlined_subroutine:
cdc07690 8717 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8718 break;
72929c62 8719 case DW_TAG_constant:
c906108c
SS
8720 case DW_TAG_variable:
8721 case DW_TAG_typedef:
91c24f0a 8722 case DW_TAG_union_type:
72bf9492 8723 if (!pdi->is_declaration)
63d06c5c 8724 {
72bf9492 8725 add_partial_symbol (pdi, cu);
63d06c5c
DC
8726 }
8727 break;
c906108c 8728 case DW_TAG_class_type:
680b30c7 8729 case DW_TAG_interface_type:
c906108c 8730 case DW_TAG_structure_type:
72bf9492 8731 if (!pdi->is_declaration)
c906108c 8732 {
72bf9492 8733 add_partial_symbol (pdi, cu);
c906108c 8734 }
b7fee5a3
KS
8735 if ((cu->language == language_rust
8736 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8737 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8738 set_addrmap, cu);
c906108c 8739 break;
91c24f0a 8740 case DW_TAG_enumeration_type:
72bf9492
DJ
8741 if (!pdi->is_declaration)
8742 add_partial_enumeration (pdi, cu);
c906108c
SS
8743 break;
8744 case DW_TAG_base_type:
a02abb62 8745 case DW_TAG_subrange_type:
c906108c 8746 /* File scope base type definitions are added to the partial
c5aa993b 8747 symbol table. */
72bf9492 8748 add_partial_symbol (pdi, cu);
c906108c 8749 break;
d9fa45fe 8750 case DW_TAG_namespace:
cdc07690 8751 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8752 break;
5d7cb8df 8753 case DW_TAG_module:
59c35742
AB
8754 if (!pdi->is_declaration)
8755 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8756 break;
95554aad
TT
8757 case DW_TAG_imported_unit:
8758 {
8759 struct dwarf2_per_cu_data *per_cu;
8760
f4dc4d17
DE
8761 /* For now we don't handle imported units in type units. */
8762 if (cu->per_cu->is_debug_types)
8763 {
8764 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8765 " supported in type units [in module %s]"),
518817b3 8766 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8767 }
8768
e3b94546
SM
8769 per_cu = dwarf2_find_containing_comp_unit
8770 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8771 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8772
8773 /* Go read the partial unit, if needed. */
8774 if (per_cu->v.psymtab == NULL)
b93601f3 8775 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8776
ae640021 8777 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8778 }
8779 break;
74921315
KS
8780 case DW_TAG_imported_declaration:
8781 add_partial_symbol (pdi, cu);
8782 break;
c906108c
SS
8783 default:
8784 break;
8785 }
8786 }
8787
72bf9492
DJ
8788 /* If the die has a sibling, skip to the sibling. */
8789
8790 pdi = pdi->die_sibling;
8791 }
8792}
8793
8794/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8795
72bf9492 8796 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8797 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8798 Enumerators are an exception; they use the scope of their parent
8799 enumeration type, i.e. the name of the enumeration type is not
8800 prepended to the enumerator.
91c24f0a 8801
72bf9492
DJ
8802 There are two complexities. One is DW_AT_specification; in this
8803 case "parent" means the parent of the target of the specification,
8804 instead of the direct parent of the DIE. The other is compilers
8805 which do not emit DW_TAG_namespace; in this case we try to guess
8806 the fully qualified name of structure types from their members'
8807 linkage names. This must be done using the DIE's children rather
8808 than the children of any DW_AT_specification target. We only need
8809 to do this for structures at the top level, i.e. if the target of
8810 any DW_AT_specification (if any; otherwise the DIE itself) does not
8811 have a parent. */
8812
8813/* Compute the scope prefix associated with PDI's parent, in
8814 compilation unit CU. The result will be allocated on CU's
8815 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8816 field. NULL is returned if no prefix is necessary. */
15d034d0 8817static const char *
72bf9492
DJ
8818partial_die_parent_scope (struct partial_die_info *pdi,
8819 struct dwarf2_cu *cu)
8820{
15d034d0 8821 const char *grandparent_scope;
72bf9492 8822 struct partial_die_info *parent, *real_pdi;
91c24f0a 8823
72bf9492
DJ
8824 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8825 then this means the parent of the specification DIE. */
8826
8827 real_pdi = pdi;
72bf9492 8828 while (real_pdi->has_specification)
fb816e8b 8829 {
122cf0f2
AB
8830 auto res = find_partial_die (real_pdi->spec_offset,
8831 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8832 real_pdi = res.pdi;
8833 cu = res.cu;
8834 }
72bf9492
DJ
8835
8836 parent = real_pdi->die_parent;
8837 if (parent == NULL)
8838 return NULL;
8839
8840 if (parent->scope_set)
8841 return parent->scope;
8842
52356b79 8843 parent->fixup (cu);
72bf9492 8844
10b3939b 8845 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8846
acebe513
UW
8847 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8848 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8849 Work around this problem here. */
8850 if (cu->language == language_cplus
6e70227d 8851 && parent->tag == DW_TAG_namespace
acebe513
UW
8852 && strcmp (parent->name, "::") == 0
8853 && grandparent_scope == NULL)
8854 {
8855 parent->scope = NULL;
8856 parent->scope_set = 1;
8857 return NULL;
8858 }
8859
0a4b0913 8860 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8861 if (pdi->tag == DW_TAG_enumerator)
8862 /* Enumerators should not get the name of the enumeration as a prefix. */
8863 parent->scope = grandparent_scope;
8864 else if (parent->tag == DW_TAG_namespace
f55ee35c 8865 || parent->tag == DW_TAG_module
72bf9492
DJ
8866 || parent->tag == DW_TAG_structure_type
8867 || parent->tag == DW_TAG_class_type
680b30c7 8868 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8869 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8870 || parent->tag == DW_TAG_enumeration_type
8871 || (cu->language == language_fortran
8872 && parent->tag == DW_TAG_subprogram
8873 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8874 {
8875 if (grandparent_scope == NULL)
8876 parent->scope = parent->name;
8877 else
3e43a32a
MS
8878 parent->scope = typename_concat (&cu->comp_unit_obstack,
8879 grandparent_scope,
f55ee35c 8880 parent->name, 0, cu);
72bf9492 8881 }
72bf9492
DJ
8882 else
8883 {
8884 /* FIXME drow/2004-04-01: What should we be doing with
8885 function-local names? For partial symbols, we should probably be
8886 ignoring them. */
fa9c3fa0
TT
8887 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8888 dwarf_tag_name (parent->tag),
8889 sect_offset_str (pdi->sect_off));
72bf9492 8890 parent->scope = grandparent_scope;
c906108c
SS
8891 }
8892
72bf9492
DJ
8893 parent->scope_set = 1;
8894 return parent->scope;
8895}
8896
8897/* Return the fully scoped name associated with PDI, from compilation unit
8898 CU. The result will be allocated with malloc. */
4568ecf9 8899
43816ebc 8900static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8901partial_die_full_name (struct partial_die_info *pdi,
8902 struct dwarf2_cu *cu)
8903{
15d034d0 8904 const char *parent_scope;
72bf9492 8905
98bfdba5
PA
8906 /* If this is a template instantiation, we can not work out the
8907 template arguments from partial DIEs. So, unfortunately, we have
8908 to go through the full DIEs. At least any work we do building
8909 types here will be reused if full symbols are loaded later. */
8910 if (pdi->has_template_arguments)
8911 {
52356b79 8912 pdi->fixup (cu);
98bfdba5
PA
8913
8914 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8915 {
8916 struct die_info *die;
8917 struct attribute attr;
8918 struct dwarf2_cu *ref_cu = cu;
8919
b64f50a1 8920 /* DW_FORM_ref_addr is using section offset. */
b4069958 8921 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8922 attr.form = DW_FORM_ref_addr;
9c541725 8923 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8924 die = follow_die_ref (NULL, &attr, &ref_cu);
8925
43816ebc 8926 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8927 }
8928 }
8929
72bf9492
DJ
8930 parent_scope = partial_die_parent_scope (pdi, cu);
8931 if (parent_scope == NULL)
8932 return NULL;
8933 else
43816ebc
TT
8934 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8935 pdi->name, 0, cu));
c906108c
SS
8936}
8937
8938static void
72bf9492 8939add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8940{
518817b3
SM
8941 struct dwarf2_per_objfile *dwarf2_per_objfile
8942 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8943 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8944 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8945 CORE_ADDR addr = 0;
15d034d0 8946 const char *actual_name = NULL;
e142c38c
DJ
8947 CORE_ADDR baseaddr;
8948
6a053cb1 8949 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 8950
43816ebc
TT
8951 gdb::unique_xmalloc_ptr<char> built_actual_name
8952 = partial_die_full_name (pdi, cu);
15d034d0 8953 if (built_actual_name != NULL)
43816ebc 8954 actual_name = built_actual_name.get ();
63d06c5c 8955
72bf9492
DJ
8956 if (actual_name == NULL)
8957 actual_name = pdi->name;
8958
c906108c
SS
8959 switch (pdi->tag)
8960 {
b1dc1806 8961 case DW_TAG_inlined_subroutine:
c906108c 8962 case DW_TAG_subprogram:
79748972
TT
8963 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8964 - baseaddr);
0a4b0913
AB
8965 if (pdi->is_external
8966 || cu->language == language_ada
8967 || (cu->language == language_fortran
8968 && pdi->die_parent != NULL
8969 && pdi->die_parent->tag == DW_TAG_subprogram))
8970 {
8971 /* Normally, only "external" DIEs are part of the global scope.
8972 But in Ada and Fortran, we want to be able to access nested
8973 procedures globally. So all Ada and Fortran subprograms are
8974 stored in the global scope. */
31edb802 8975 add_psymbol_to_list (actual_name,
15d034d0 8976 built_actual_name != NULL,
f47fb265 8977 VAR_DOMAIN, LOC_BLOCK,
79748972 8978 SECT_OFF_TEXT (objfile),
75aedd27 8979 psymbol_placement::GLOBAL,
79748972
TT
8980 addr,
8981 cu->language, objfile);
c906108c
SS
8982 }
8983 else
8984 {
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::STATIC,
1762568f 8990 addr, cu->language, objfile);
c906108c 8991 }
0c1b455e
TT
8992
8993 if (pdi->main_subprogram && actual_name != NULL)
8994 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8995 break;
72929c62 8996 case DW_TAG_constant:
31edb802 8997 add_psymbol_to_list (actual_name,
75aedd27
TT
8998 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8999 -1, (pdi->is_external
9000 ? psymbol_placement::GLOBAL
9001 : psymbol_placement::STATIC),
9002 0, cu->language, objfile);
72929c62 9003 break;
c906108c 9004 case DW_TAG_variable:
95554aad
TT
9005 if (pdi->d.locdesc)
9006 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9007
95554aad 9008 if (pdi->d.locdesc
caac4577
JG
9009 && addr == 0
9010 && !dwarf2_per_objfile->has_section_at_zero)
9011 {
9012 /* A global or static variable may also have been stripped
9013 out by the linker if unused, in which case its address
9014 will be nullified; do not add such variables into partial
9015 symbol table then. */
9016 }
9017 else if (pdi->is_external)
c906108c
SS
9018 {
9019 /* Global Variable.
9020 Don't enter into the minimal symbol tables as there is
9021 a minimal symbol table entry from the ELF symbols already.
9022 Enter into partial symbol table if it has a location
9023 descriptor or a type.
9024 If the location descriptor is missing, new_symbol will create
9025 a LOC_UNRESOLVED symbol, the address of the variable will then
9026 be determined from the minimal symbol table whenever the variable
9027 is referenced.
9028 The address for the partial symbol table entry is not
9029 used by GDB, but it comes in handy for debugging partial symbol
9030 table building. */
9031
95554aad 9032 if (pdi->d.locdesc || pdi->has_type)
31edb802 9033 add_psymbol_to_list (actual_name,
15d034d0 9034 built_actual_name != NULL,
f47fb265 9035 VAR_DOMAIN, LOC_STATIC,
79748972 9036 SECT_OFF_TEXT (objfile),
75aedd27 9037 psymbol_placement::GLOBAL,
79748972 9038 addr, cu->language, objfile);
c906108c
SS
9039 }
9040 else
9041 {
ff908ebf
AW
9042 int has_loc = pdi->d.locdesc != NULL;
9043
9044 /* Static Variable. Skip symbols whose value we cannot know (those
9045 without location descriptors or constant values). */
9046 if (!has_loc && !pdi->has_const_value)
43816ebc 9047 return;
ff908ebf 9048
31edb802 9049 add_psymbol_to_list (actual_name,
15d034d0 9050 built_actual_name != NULL,
f47fb265 9051 VAR_DOMAIN, LOC_STATIC,
79748972 9052 SECT_OFF_TEXT (objfile),
75aedd27 9053 psymbol_placement::STATIC,
79748972 9054 has_loc ? addr : 0,
f47fb265 9055 cu->language, objfile);
c906108c
SS
9056 }
9057 break;
9058 case DW_TAG_typedef:
9059 case DW_TAG_base_type:
a02abb62 9060 case DW_TAG_subrange_type:
31edb802 9061 add_psymbol_to_list (actual_name,
15d034d0 9062 built_actual_name != NULL,
79748972 9063 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9064 psymbol_placement::STATIC,
1762568f 9065 0, cu->language, objfile);
c906108c 9066 break;
74921315 9067 case DW_TAG_imported_declaration:
72bf9492 9068 case DW_TAG_namespace:
31edb802 9069 add_psymbol_to_list (actual_name,
15d034d0 9070 built_actual_name != NULL,
79748972 9071 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9072 psymbol_placement::GLOBAL,
1762568f 9073 0, cu->language, objfile);
72bf9492 9074 break;
530e8392 9075 case DW_TAG_module:
a5fd13a9
BH
9076 /* With Fortran 77 there might be a "BLOCK DATA" module
9077 available without any name. If so, we skip the module as it
9078 doesn't bring any value. */
9079 if (actual_name != nullptr)
31edb802 9080 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9081 built_actual_name != NULL,
9082 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9083 psymbol_placement::GLOBAL,
9084 0, cu->language, objfile);
530e8392 9085 break;
c906108c 9086 case DW_TAG_class_type:
680b30c7 9087 case DW_TAG_interface_type:
c906108c
SS
9088 case DW_TAG_structure_type:
9089 case DW_TAG_union_type:
9090 case DW_TAG_enumeration_type:
fa4028e9
JB
9091 /* Skip external references. The DWARF standard says in the section
9092 about "Structure, Union, and Class Type Entries": "An incomplete
9093 structure, union or class type is represented by a structure,
9094 union or class entry that does not have a byte size attribute
9095 and that has a DW_AT_declaration attribute." */
9096 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9097 return;
fa4028e9 9098
63d06c5c
DC
9099 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9100 static vs. global. */
31edb802 9101 add_psymbol_to_list (actual_name,
15d034d0 9102 built_actual_name != NULL,
79748972 9103 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9104 cu->language == language_cplus
75aedd27
TT
9105 ? psymbol_placement::GLOBAL
9106 : psymbol_placement::STATIC,
1762568f 9107 0, cu->language, objfile);
c906108c 9108
c906108c
SS
9109 break;
9110 case DW_TAG_enumerator:
31edb802 9111 add_psymbol_to_list (actual_name,
15d034d0 9112 built_actual_name != NULL,
79748972 9113 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9114 cu->language == language_cplus
75aedd27
TT
9115 ? psymbol_placement::GLOBAL
9116 : psymbol_placement::STATIC,
1762568f 9117 0, cu->language, objfile);
c906108c
SS
9118 break;
9119 default:
9120 break;
9121 }
9122}
9123
5c4e30ca
DC
9124/* Read a partial die corresponding to a namespace; also, add a symbol
9125 corresponding to that namespace to the symbol table. NAMESPACE is
9126 the name of the enclosing namespace. */
91c24f0a 9127
72bf9492
DJ
9128static void
9129add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9130 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9131 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9132{
72bf9492 9133 /* Add a symbol for the namespace. */
e7c27a73 9134
72bf9492 9135 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9136
9137 /* Now scan partial symbols in that namespace. */
9138
91c24f0a 9139 if (pdi->has_children)
cdc07690 9140 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9141}
9142
5d7cb8df
JK
9143/* Read a partial die corresponding to a Fortran module. */
9144
9145static void
9146add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9147 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9148{
530e8392
KB
9149 /* Add a symbol for the namespace. */
9150
9151 add_partial_symbol (pdi, cu);
9152
f55ee35c 9153 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9154
9155 if (pdi->has_children)
cdc07690 9156 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9157}
9158
b1dc1806
XR
9159/* Read a partial die corresponding to a subprogram or an inlined
9160 subprogram and create a partial symbol for that subprogram.
9161 When the CU language allows it, this routine also defines a partial
9162 symbol for each nested subprogram that this subprogram contains.
9163 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9164 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9165
cdc07690
YQ
9166 PDI may also be a lexical block, in which case we simply search
9167 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9168 Again, this is only performed when the CU language allows this
9169 type of definitions. */
9170
9171static void
9172add_partial_subprogram (struct partial_die_info *pdi,
9173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9174 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9175{
b1dc1806 9176 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9177 {
9178 if (pdi->has_pc_info)
9179 {
9180 if (pdi->lowpc < *lowpc)
9181 *lowpc = pdi->lowpc;
9182 if (pdi->highpc > *highpc)
9183 *highpc = pdi->highpc;
cdc07690 9184 if (set_addrmap)
5734ee8b 9185 {
518817b3 9186 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9187 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9188 CORE_ADDR baseaddr;
b926417a
TT
9189 CORE_ADDR this_highpc;
9190 CORE_ADDR this_lowpc;
5734ee8b 9191
6a053cb1 9192 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
b926417a
TT
9193 this_lowpc
9194 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9195 pdi->lowpc + baseaddr)
9196 - baseaddr);
9197 this_highpc
9198 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9199 pdi->highpc + baseaddr)
9200 - baseaddr);
d320c2b5 9201 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9202 this_lowpc, this_highpc - 1,
9291a0cd 9203 cu->per_cu->v.psymtab);
5734ee8b 9204 }
481860b3
GB
9205 }
9206
9207 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9208 {
bc30ff58 9209 if (!pdi->is_declaration)
e8d05480
JB
9210 /* Ignore subprogram DIEs that do not have a name, they are
9211 illegal. Do not emit a complaint at this point, we will
9212 do so when we convert this psymtab into a symtab. */
9213 if (pdi->name)
9214 add_partial_symbol (pdi, cu);
bc30ff58
JB
9215 }
9216 }
6e70227d 9217
bc30ff58
JB
9218 if (! pdi->has_children)
9219 return;
9220
0a4b0913 9221 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9222 {
9223 pdi = pdi->die_child;
9224 while (pdi != NULL)
9225 {
52356b79 9226 pdi->fixup (cu);
bc30ff58 9227 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9228 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9229 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9230 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9231 pdi = pdi->die_sibling;
9232 }
9233 }
9234}
9235
91c24f0a
DC
9236/* Read a partial die corresponding to an enumeration type. */
9237
72bf9492
DJ
9238static void
9239add_partial_enumeration (struct partial_die_info *enum_pdi,
9240 struct dwarf2_cu *cu)
91c24f0a 9241{
72bf9492 9242 struct partial_die_info *pdi;
91c24f0a
DC
9243
9244 if (enum_pdi->name != NULL)
72bf9492
DJ
9245 add_partial_symbol (enum_pdi, cu);
9246
9247 pdi = enum_pdi->die_child;
9248 while (pdi)
91c24f0a 9249 {
72bf9492 9250 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9251 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9252 else
72bf9492
DJ
9253 add_partial_symbol (pdi, cu);
9254 pdi = pdi->die_sibling;
91c24f0a 9255 }
91c24f0a
DC
9256}
9257
6caca83c
CC
9258/* Return the initial uleb128 in the die at INFO_PTR. */
9259
9260static unsigned int
d521ce57 9261peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9262{
9263 unsigned int bytes_read;
9264
9265 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9266}
9267
685af9cd
TT
9268/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9269 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9270
4bb7a0a7
DJ
9271 Return the corresponding abbrev, or NULL if the number is zero (indicating
9272 an empty DIE). In either case *BYTES_READ will be set to the length of
9273 the initial number. */
9274
9275static struct abbrev_info *
685af9cd
TT
9276peek_die_abbrev (const die_reader_specs &reader,
9277 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9278{
685af9cd 9279 dwarf2_cu *cu = reader.cu;
518817b3 9280 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9281 unsigned int abbrev_number
9282 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9283
9284 if (abbrev_number == 0)
9285 return NULL;
9286
685af9cd 9287 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9288 if (!abbrev)
9289 {
422b9917 9290 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9291 " at offset %s [in module %s]"),
422b9917 9292 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9293 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9294 }
9295
9296 return abbrev;
9297}
9298
93311388
DE
9299/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9300 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9301 DIE. Any children of the skipped DIEs will also be skipped. */
9302
d521ce57
TT
9303static const gdb_byte *
9304skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9305{
4bb7a0a7
DJ
9306 while (1)
9307 {
685af9cd
TT
9308 unsigned int bytes_read;
9309 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9310
4bb7a0a7
DJ
9311 if (abbrev == NULL)
9312 return info_ptr + bytes_read;
9313 else
dee91e82 9314 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9315 }
9316}
9317
93311388
DE
9318/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9319 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9320 abbrev corresponding to that skipped uleb128 should be passed in
9321 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9322 children. */
9323
d521ce57
TT
9324static const gdb_byte *
9325skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9326 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9327{
9328 unsigned int bytes_read;
9329 struct attribute attr;
dee91e82
DE
9330 bfd *abfd = reader->abfd;
9331 struct dwarf2_cu *cu = reader->cu;
d521ce57 9332 const gdb_byte *buffer = reader->buffer;
f664829e 9333 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9334 unsigned int form, i;
9335
9336 for (i = 0; i < abbrev->num_attrs; i++)
9337 {
9338 /* The only abbrev we care about is DW_AT_sibling. */
9339 if (abbrev->attrs[i].name == DW_AT_sibling)
9340 {
dee91e82 9341 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9342 if (attr.form == DW_FORM_ref_addr)
b98664d3 9343 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9344 else
b9502d3f 9345 {
9c541725
PA
9346 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9347 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9348
9349 if (sibling_ptr < info_ptr)
b98664d3 9350 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9351 else if (sibling_ptr > reader->buffer_end)
9352 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9353 else
9354 return sibling_ptr;
9355 }
4bb7a0a7
DJ
9356 }
9357
9358 /* If it isn't DW_AT_sibling, skip this attribute. */
9359 form = abbrev->attrs[i].form;
9360 skip_attribute:
9361 switch (form)
9362 {
4bb7a0a7 9363 case DW_FORM_ref_addr:
ae411497
TT
9364 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9365 and later it is offset sized. */
9366 if (cu->header.version == 2)
9367 info_ptr += cu->header.addr_size;
9368 else
9369 info_ptr += cu->header.offset_size;
9370 break;
36586728
TT
9371 case DW_FORM_GNU_ref_alt:
9372 info_ptr += cu->header.offset_size;
9373 break;
ae411497 9374 case DW_FORM_addr:
4bb7a0a7
DJ
9375 info_ptr += cu->header.addr_size;
9376 break;
9377 case DW_FORM_data1:
9378 case DW_FORM_ref1:
9379 case DW_FORM_flag:
8fe0f950 9380 case DW_FORM_strx1:
4bb7a0a7
DJ
9381 info_ptr += 1;
9382 break;
2dc7f7b3 9383 case DW_FORM_flag_present:
43988095 9384 case DW_FORM_implicit_const:
2dc7f7b3 9385 break;
4bb7a0a7
DJ
9386 case DW_FORM_data2:
9387 case DW_FORM_ref2:
8fe0f950 9388 case DW_FORM_strx2:
4bb7a0a7
DJ
9389 info_ptr += 2;
9390 break;
8fe0f950
AT
9391 case DW_FORM_strx3:
9392 info_ptr += 3;
9393 break;
4bb7a0a7
DJ
9394 case DW_FORM_data4:
9395 case DW_FORM_ref4:
8fe0f950 9396 case DW_FORM_strx4:
4bb7a0a7
DJ
9397 info_ptr += 4;
9398 break;
9399 case DW_FORM_data8:
9400 case DW_FORM_ref8:
55f1336d 9401 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9402 info_ptr += 8;
9403 break;
0224619f
JK
9404 case DW_FORM_data16:
9405 info_ptr += 16;
9406 break;
4bb7a0a7 9407 case DW_FORM_string:
9b1c24c8 9408 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9409 info_ptr += bytes_read;
9410 break;
2dc7f7b3 9411 case DW_FORM_sec_offset:
4bb7a0a7 9412 case DW_FORM_strp:
36586728 9413 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9414 info_ptr += cu->header.offset_size;
9415 break;
2dc7f7b3 9416 case DW_FORM_exprloc:
4bb7a0a7
DJ
9417 case DW_FORM_block:
9418 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9419 info_ptr += bytes_read;
9420 break;
9421 case DW_FORM_block1:
9422 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9423 break;
9424 case DW_FORM_block2:
9425 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9426 break;
9427 case DW_FORM_block4:
9428 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9429 break;
336d760d 9430 case DW_FORM_addrx:
cf532bd1 9431 case DW_FORM_strx:
4bb7a0a7
DJ
9432 case DW_FORM_sdata:
9433 case DW_FORM_udata:
9434 case DW_FORM_ref_udata:
3019eac3
DE
9435 case DW_FORM_GNU_addr_index:
9436 case DW_FORM_GNU_str_index:
d521ce57 9437 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9438 break;
9439 case DW_FORM_indirect:
9440 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9441 info_ptr += bytes_read;
9442 /* We need to continue parsing from here, so just go back to
9443 the top. */
9444 goto skip_attribute;
9445
9446 default:
3e43a32a
MS
9447 error (_("Dwarf Error: Cannot handle %s "
9448 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9449 dwarf_form_name (form),
9450 bfd_get_filename (abfd));
9451 }
9452 }
9453
9454 if (abbrev->has_children)
dee91e82 9455 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9456 else
9457 return info_ptr;
9458}
9459
93311388 9460/* Locate ORIG_PDI's sibling.
dee91e82 9461 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9462
d521ce57 9463static const gdb_byte *
dee91e82
DE
9464locate_pdi_sibling (const struct die_reader_specs *reader,
9465 struct partial_die_info *orig_pdi,
d521ce57 9466 const gdb_byte *info_ptr)
91c24f0a
DC
9467{
9468 /* Do we know the sibling already? */
72bf9492 9469
91c24f0a
DC
9470 if (orig_pdi->sibling)
9471 return orig_pdi->sibling;
9472
9473 /* Are there any children to deal with? */
9474
9475 if (!orig_pdi->has_children)
9476 return info_ptr;
9477
4bb7a0a7 9478 /* Skip the children the long way. */
91c24f0a 9479
dee91e82 9480 return skip_children (reader, info_ptr);
91c24f0a
DC
9481}
9482
257e7a09 9483/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9484 not NULL. */
c906108c
SS
9485
9486static void
257e7a09
YQ
9487dwarf2_read_symtab (struct partial_symtab *self,
9488 struct objfile *objfile)
c906108c 9489{
ed2dc618
SM
9490 struct dwarf2_per_objfile *dwarf2_per_objfile
9491 = get_dwarf2_per_objfile (objfile);
9492
257e7a09 9493 if (self->readin)
c906108c 9494 {
442e4d9c 9495 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9496 self->filename);
442e4d9c
YQ
9497 }
9498 else
9499 {
9500 if (info_verbose)
c906108c 9501 {
442e4d9c 9502 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9503 self->filename);
442e4d9c 9504 gdb_flush (gdb_stdout);
c906108c 9505 }
c906108c 9506
442e4d9c
YQ
9507 /* If this psymtab is constructed from a debug-only objfile, the
9508 has_section_at_zero flag will not necessarily be correct. We
9509 can get the correct value for this flag by looking at the data
9510 associated with the (presumably stripped) associated objfile. */
9511 if (objfile->separate_debug_objfile_backlink)
9512 {
9513 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9514 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9515
442e4d9c
YQ
9516 dwarf2_per_objfile->has_section_at_zero
9517 = dpo_backlink->has_section_at_zero;
9518 }
b2ab525c 9519
442e4d9c 9520 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9521
257e7a09 9522 psymtab_to_symtab_1 (self);
c906108c 9523
442e4d9c
YQ
9524 /* Finish up the debug error message. */
9525 if (info_verbose)
9526 printf_filtered (_("done.\n"));
c906108c 9527 }
95554aad 9528
ed2dc618 9529 process_cu_includes (dwarf2_per_objfile);
c906108c 9530}
9cdd5dbd
DE
9531\f
9532/* Reading in full CUs. */
c906108c 9533
10b3939b
DJ
9534/* Add PER_CU to the queue. */
9535
9536static void
95554aad
TT
9537queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9538 enum language pretend_language)
10b3939b
DJ
9539{
9540 struct dwarf2_queue_item *item;
9541
9542 per_cu->queued = 1;
8d749320 9543 item = XNEW (struct dwarf2_queue_item);
10b3939b 9544 item->per_cu = per_cu;
95554aad 9545 item->pretend_language = pretend_language;
10b3939b
DJ
9546 item->next = NULL;
9547
9548 if (dwarf2_queue == NULL)
9549 dwarf2_queue = item;
9550 else
9551 dwarf2_queue_tail->next = item;
9552
9553 dwarf2_queue_tail = item;
9554}
9555
89e63ee4
DE
9556/* If PER_CU is not yet queued, add it to the queue.
9557 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9558 dependency.
0907af0c 9559 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9560 meaning either PER_CU is already queued or it is already loaded.
9561
9562 N.B. There is an invariant here that if a CU is queued then it is loaded.
9563 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9564
9565static int
89e63ee4 9566maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9567 struct dwarf2_per_cu_data *per_cu,
9568 enum language pretend_language)
9569{
9570 /* We may arrive here during partial symbol reading, if we need full
9571 DIEs to process an unusual case (e.g. template arguments). Do
9572 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9573 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9574 {
9575 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9576 return 1;
9577 return 0;
9578 }
9579
9580 /* Mark the dependence relation so that we don't flush PER_CU
9581 too early. */
89e63ee4
DE
9582 if (dependent_cu != NULL)
9583 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9584
9585 /* If it's already on the queue, we have nothing to do. */
9586 if (per_cu->queued)
9587 return 0;
9588
9589 /* If the compilation unit is already loaded, just mark it as
9590 used. */
9591 if (per_cu->cu != NULL)
9592 {
9593 per_cu->cu->last_used = 0;
9594 return 0;
9595 }
9596
9597 /* Add it to the queue. */
9598 queue_comp_unit (per_cu, pretend_language);
9599
9600 return 1;
9601}
9602
10b3939b
DJ
9603/* Process the queue. */
9604
9605static void
ed2dc618 9606process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9607{
9608 struct dwarf2_queue_item *item, *next_item;
9609
b4f54984 9610 if (dwarf_read_debug)
45cfd468
DE
9611 {
9612 fprintf_unfiltered (gdb_stdlog,
9613 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9614 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9615 }
9616
03dd20cc
DJ
9617 /* The queue starts out with one item, but following a DIE reference
9618 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9619 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9620 {
cc12ce38
DE
9621 if ((dwarf2_per_objfile->using_index
9622 ? !item->per_cu->v.quick->compunit_symtab
9623 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9624 /* Skip dummy CUs. */
9625 && item->per_cu->cu != NULL)
f4dc4d17
DE
9626 {
9627 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9628 unsigned int debug_print_threshold;
247f5c4f 9629 char buf[100];
f4dc4d17 9630
247f5c4f 9631 if (per_cu->is_debug_types)
f4dc4d17 9632 {
247f5c4f
DE
9633 struct signatured_type *sig_type =
9634 (struct signatured_type *) per_cu;
9635
9d8780f0 9636 sprintf (buf, "TU %s at offset %s",
73be47f5 9637 hex_string (sig_type->signature),
9d8780f0 9638 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9639 /* There can be 100s of TUs.
9640 Only print them in verbose mode. */
9641 debug_print_threshold = 2;
f4dc4d17 9642 }
247f5c4f 9643 else
73be47f5 9644 {
9d8780f0
SM
9645 sprintf (buf, "CU at offset %s",
9646 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9647 debug_print_threshold = 1;
9648 }
247f5c4f 9649
b4f54984 9650 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9651 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9652
9653 if (per_cu->is_debug_types)
9654 process_full_type_unit (per_cu, item->pretend_language);
9655 else
9656 process_full_comp_unit (per_cu, item->pretend_language);
9657
b4f54984 9658 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9659 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9660 }
10b3939b
DJ
9661
9662 item->per_cu->queued = 0;
9663 next_item = item->next;
9664 xfree (item);
9665 }
9666
9667 dwarf2_queue_tail = NULL;
45cfd468 9668
b4f54984 9669 if (dwarf_read_debug)
45cfd468
DE
9670 {
9671 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9672 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9673 }
10b3939b
DJ
9674}
9675
10b3939b
DJ
9676/* Read in full symbols for PST, and anything it depends on. */
9677
c906108c 9678static void
fba45db2 9679psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9680{
10b3939b 9681 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9682 int i;
9683
95554aad
TT
9684 if (pst->readin)
9685 return;
9686
aaa75496 9687 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9688 if (!pst->dependencies[i]->readin
9689 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9690 {
9691 /* Inform about additional files that need to be read in. */
9692 if (info_verbose)
9693 {
a3f17187 9694 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9695 fputs_filtered (" ", gdb_stdout);
9696 wrap_here ("");
9697 fputs_filtered ("and ", gdb_stdout);
9698 wrap_here ("");
9699 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9700 wrap_here (""); /* Flush output. */
aaa75496
JB
9701 gdb_flush (gdb_stdout);
9702 }
9703 psymtab_to_symtab_1 (pst->dependencies[i]);
9704 }
9705
9a3c8263 9706 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9707
9708 if (per_cu == NULL)
aaa75496
JB
9709 {
9710 /* It's an include file, no symbols to read for it.
9711 Everything is in the parent symtab. */
9712 pst->readin = 1;
9713 return;
9714 }
c906108c 9715
58f0c718 9716 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9717}
9718
dee91e82
DE
9719/* Trivial hash function for die_info: the hash value of a DIE
9720 is its offset in .debug_info for this objfile. */
10b3939b 9721
dee91e82
DE
9722static hashval_t
9723die_hash (const void *item)
10b3939b 9724{
9a3c8263 9725 const struct die_info *die = (const struct die_info *) item;
6502dd73 9726
9c541725 9727 return to_underlying (die->sect_off);
dee91e82 9728}
63d06c5c 9729
dee91e82
DE
9730/* Trivial comparison function for die_info structures: two DIEs
9731 are equal if they have the same offset. */
98bfdba5 9732
dee91e82
DE
9733static int
9734die_eq (const void *item_lhs, const void *item_rhs)
9735{
9a3c8263
SM
9736 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9737 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9738
9c541725 9739 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9740}
c906108c 9741
dee91e82
DE
9742/* die_reader_func for load_full_comp_unit.
9743 This is identical to read_signatured_type_reader,
9744 but is kept separate for now. */
c906108c 9745
dee91e82
DE
9746static void
9747load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9748 const gdb_byte *info_ptr,
dee91e82
DE
9749 struct die_info *comp_unit_die,
9750 int has_children,
9751 void *data)
9752{
9753 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9754 enum language *language_ptr = (enum language *) data;
6caca83c 9755
dee91e82
DE
9756 gdb_assert (cu->die_hash == NULL);
9757 cu->die_hash =
9758 htab_create_alloc_ex (cu->header.length / 12,
9759 die_hash,
9760 die_eq,
9761 NULL,
9762 &cu->comp_unit_obstack,
9763 hashtab_obstack_allocate,
9764 dummy_obstack_deallocate);
e142c38c 9765
dee91e82
DE
9766 if (has_children)
9767 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9768 &info_ptr, comp_unit_die);
9769 cu->dies = comp_unit_die;
9770 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9771
9772 /* We try not to read any attributes in this function, because not
9cdd5dbd 9773 all CUs needed for references have been loaded yet, and symbol
10b3939b 9774 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9775 or we won't be able to build types correctly.
9776 Similarly, if we do not read the producer, we can not apply
9777 producer-specific interpretation. */
95554aad 9778 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9779}
10b3939b 9780
dee91e82 9781/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9782
dee91e82 9783static void
95554aad 9784load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9785 bool skip_partial,
95554aad 9786 enum language pretend_language)
dee91e82 9787{
3019eac3 9788 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9789
58f0c718 9790 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9791 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9792}
9793
3da10d80
KS
9794/* Add a DIE to the delayed physname list. */
9795
9796static void
9797add_to_method_list (struct type *type, int fnfield_index, int index,
9798 const char *name, struct die_info *die,
9799 struct dwarf2_cu *cu)
9800{
9801 struct delayed_method_info mi;
9802 mi.type = type;
9803 mi.fnfield_index = fnfield_index;
9804 mi.index = index;
9805 mi.name = name;
9806 mi.die = die;
c89b44cd 9807 cu->method_list.push_back (mi);
3da10d80
KS
9808}
9809
3693fdb3
PA
9810/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9811 "const" / "volatile". If so, decrements LEN by the length of the
9812 modifier and return true. Otherwise return false. */
9813
9814template<size_t N>
9815static bool
9816check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9817{
9818 size_t mod_len = sizeof (mod) - 1;
9819 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9820 {
9821 len -= mod_len;
9822 return true;
9823 }
9824 return false;
9825}
9826
3da10d80
KS
9827/* Compute the physnames of any methods on the CU's method list.
9828
9829 The computation of method physnames is delayed in order to avoid the
9830 (bad) condition that one of the method's formal parameters is of an as yet
9831 incomplete type. */
9832
9833static void
9834compute_delayed_physnames (struct dwarf2_cu *cu)
9835{
3693fdb3 9836 /* Only C++ delays computing physnames. */
c89b44cd 9837 if (cu->method_list.empty ())
3693fdb3
PA
9838 return;
9839 gdb_assert (cu->language == language_cplus);
9840
52941706 9841 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9842 {
1d06ead6 9843 const char *physname;
3da10d80 9844 struct fn_fieldlist *fn_flp
c89b44cd
TT
9845 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9846 physname = dwarf2_physname (mi.name, mi.die, cu);
9847 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9848 = physname ? physname : "";
3693fdb3
PA
9849
9850 /* Since there's no tag to indicate whether a method is a
9851 const/volatile overload, extract that information out of the
9852 demangled name. */
9853 if (physname != NULL)
9854 {
9855 size_t len = strlen (physname);
9856
9857 while (1)
9858 {
9859 if (physname[len] == ')') /* shortcut */
9860 break;
9861 else if (check_modifier (physname, len, " const"))
c89b44cd 9862 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9863 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9864 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9865 else
9866 break;
9867 }
9868 }
3da10d80 9869 }
c89b44cd
TT
9870
9871 /* The list is no longer needed. */
9872 cu->method_list.clear ();
3da10d80
KS
9873}
9874
a766d390
DE
9875/* Go objects should be embedded in a DW_TAG_module DIE,
9876 and it's not clear if/how imported objects will appear.
9877 To keep Go support simple until that's worked out,
9878 go back through what we've read and create something usable.
9879 We could do this while processing each DIE, and feels kinda cleaner,
9880 but that way is more invasive.
9881 This is to, for example, allow the user to type "p var" or "b main"
9882 without having to specify the package name, and allow lookups
9883 of module.object to work in contexts that use the expression
9884 parser. */
9885
9886static void
9887fixup_go_packaging (struct dwarf2_cu *cu)
9888{
421d1616 9889 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9890 struct pending *list;
9891 int i;
9892
c24bdb02 9893 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9894 list != NULL;
9895 list = list->next)
a766d390
DE
9896 {
9897 for (i = 0; i < list->nsyms; ++i)
9898 {
9899 struct symbol *sym = list->symbol[i];
9900
c1b5c1eb 9901 if (sym->language () == language_go
a766d390
DE
9902 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9903 {
421d1616
TT
9904 gdb::unique_xmalloc_ptr<char> this_package_name
9905 (go_symbol_package_name (sym));
a766d390
DE
9906
9907 if (this_package_name == NULL)
9908 continue;
9909 if (package_name == NULL)
421d1616 9910 package_name = std::move (this_package_name);
a766d390
DE
9911 else
9912 {
518817b3
SM
9913 struct objfile *objfile
9914 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9915 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9916 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9917 (symbol_symtab (sym) != NULL
9918 ? symtab_to_filename_for_display
9919 (symbol_symtab (sym))
e3b94546 9920 : objfile_name (objfile)),
421d1616 9921 this_package_name.get (), package_name.get ());
a766d390
DE
9922 }
9923 }
9924 }
9925 }
9926
9927 if (package_name != NULL)
9928 {
518817b3 9929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9930 const char *saved_package_name
421d1616 9931 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9932 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9933 saved_package_name);
a766d390
DE
9934 struct symbol *sym;
9935
e623cf5d 9936 sym = allocate_symbol (objfile);
d3ecddab 9937 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9938 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9939 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9940 e.g., "main" finds the "main" module and not C's main(). */
9941 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9942 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9943 SYMBOL_TYPE (sym) = type;
9944
c24bdb02 9945 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9946 }
9947}
9948
c9317f21
TT
9949/* Allocate a fully-qualified name consisting of the two parts on the
9950 obstack. */
9951
9952static const char *
9953rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9954{
9955 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9956}
9957
9958/* A helper that allocates a struct discriminant_info to attach to a
9959 union type. */
9960
9961static struct discriminant_info *
9962alloc_discriminant_info (struct type *type, int discriminant_index,
9963 int default_index)
9964{
9965 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9966 gdb_assert (discriminant_index == -1
9967 || (discriminant_index >= 0
9968 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9969 gdb_assert (default_index == -1
c7b15a66 9970 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9971
9972 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9973
9974 struct discriminant_info *disc
9975 = ((struct discriminant_info *)
9976 TYPE_ZALLOC (type,
9977 offsetof (struct discriminant_info, discriminants)
9978 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9979 disc->default_index = default_index;
9980 disc->discriminant_index = discriminant_index;
9981
9982 struct dynamic_prop prop;
9983 prop.kind = PROP_UNDEFINED;
9984 prop.data.baton = disc;
9985
9986 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9987
9988 return disc;
9989}
9990
9991/* Some versions of rustc emitted enums in an unusual way.
9992
9993 Ordinary enums were emitted as unions. The first element of each
9994 structure in the union was named "RUST$ENUM$DISR". This element
9995 held the discriminant.
9996
9997 These versions of Rust also implemented the "non-zero"
9998 optimization. When the enum had two values, and one is empty and
9999 the other holds a pointer that cannot be zero, the pointer is used
10000 as the discriminant, with a zero value meaning the empty variant.
10001 Here, the union's first member is of the form
10002 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10003 where the fieldnos are the indices of the fields that should be
10004 traversed in order to find the field (which may be several fields deep)
10005 and the variantname is the name of the variant of the case when the
10006 field is zero.
10007
10008 This function recognizes whether TYPE is of one of these forms,
10009 and, if so, smashes it to be a variant type. */
10010
10011static void
10012quirk_rust_enum (struct type *type, struct objfile *objfile)
10013{
10014 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10015
10016 /* We don't need to deal with empty enums. */
10017 if (TYPE_NFIELDS (type) == 0)
10018 return;
10019
10020#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10021 if (TYPE_NFIELDS (type) == 1
10022 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10023 {
10024 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10025
10026 /* Decode the field name to find the offset of the
10027 discriminant. */
10028 ULONGEST bit_offset = 0;
10029 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10030 while (name[0] >= '0' && name[0] <= '9')
10031 {
10032 char *tail;
10033 unsigned long index = strtoul (name, &tail, 10);
10034 name = tail;
10035 if (*name != '$'
10036 || index >= TYPE_NFIELDS (field_type)
10037 || (TYPE_FIELD_LOC_KIND (field_type, index)
10038 != FIELD_LOC_KIND_BITPOS))
10039 {
b98664d3 10040 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10041 "[in module %s]"),
10042 TYPE_FIELD_NAME (type, 0),
10043 objfile_name (objfile));
10044 return;
10045 }
10046 ++name;
10047
10048 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10049 field_type = TYPE_FIELD_TYPE (field_type, index);
10050 }
10051
10052 /* Make a union to hold the variants. */
10053 struct type *union_type = alloc_type (objfile);
10054 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10055 TYPE_NFIELDS (union_type) = 3;
10056 TYPE_FIELDS (union_type)
10057 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10058 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10059 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10060
10061 /* Put the discriminant must at index 0. */
10062 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10063 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10064 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10065 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10066
10067 /* The order of fields doesn't really matter, so put the real
10068 field at index 1 and the data-less field at index 2. */
10069 struct discriminant_info *disc
10070 = alloc_discriminant_info (union_type, 0, 1);
10071 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10072 TYPE_FIELD_NAME (union_type, 1)
10073 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10074 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10075 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10076 TYPE_FIELD_NAME (union_type, 1));
10077
10078 const char *dataless_name
10079 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10080 name);
10081 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10082 dataless_name);
10083 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10084 /* NAME points into the original discriminant name, which
10085 already has the correct lifetime. */
10086 TYPE_FIELD_NAME (union_type, 2) = name;
10087 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10088 disc->discriminants[2] = 0;
10089
10090 /* Smash this type to be a structure type. We have to do this
10091 because the type has already been recorded. */
10092 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10093 TYPE_NFIELDS (type) = 1;
10094 TYPE_FIELDS (type)
10095 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10096
10097 /* Install the variant part. */
10098 TYPE_FIELD_TYPE (type, 0) = union_type;
10099 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10100 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10101 }
77c2dba3
TT
10102 /* A union with a single anonymous field is probably an old-style
10103 univariant enum. */
10104 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10105 {
c9317f21
TT
10106 /* Smash this type to be a structure type. We have to do this
10107 because the type has already been recorded. */
10108 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10109
10110 /* Make a union to hold the variants. */
10111 struct type *union_type = alloc_type (objfile);
10112 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10113 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10114 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10115 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10116 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10117
10118 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10119 const char *variant_name
10120 = rust_last_path_segment (TYPE_NAME (field_type));
10121 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10122 TYPE_NAME (field_type)
10123 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10124 TYPE_NAME (type), variant_name);
c9317f21
TT
10125
10126 /* Install the union in the outer struct type. */
10127 TYPE_NFIELDS (type) = 1;
10128 TYPE_FIELDS (type)
10129 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10130 TYPE_FIELD_TYPE (type, 0) = union_type;
10131 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10132 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10133
10134 alloc_discriminant_info (union_type, -1, 0);
10135 }
10136 else
10137 {
10138 struct type *disr_type = nullptr;
10139 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10140 {
10141 disr_type = TYPE_FIELD_TYPE (type, i);
10142
a037790e
TT
10143 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10144 {
10145 /* All fields of a true enum will be structs. */
10146 return;
10147 }
10148 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10149 {
10150 /* Could be data-less variant, so keep going. */
a037790e 10151 disr_type = nullptr;
c9317f21
TT
10152 }
10153 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10154 "RUST$ENUM$DISR") != 0)
10155 {
10156 /* Not a Rust enum. */
10157 return;
10158 }
10159 else
10160 {
10161 /* Found one. */
10162 break;
10163 }
10164 }
10165
10166 /* If we got here without a discriminant, then it's probably
10167 just a union. */
10168 if (disr_type == nullptr)
10169 return;
10170
10171 /* Smash this type to be a structure type. We have to do this
10172 because the type has already been recorded. */
10173 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10174
10175 /* Make a union to hold the variants. */
10176 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10177 struct type *union_type = alloc_type (objfile);
10178 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10179 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10180 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10181 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10182 TYPE_FIELDS (union_type)
10183 = (struct field *) TYPE_ZALLOC (union_type,
10184 (TYPE_NFIELDS (union_type)
10185 * sizeof (struct field)));
10186
10187 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10188 TYPE_NFIELDS (type) * sizeof (struct field));
10189
10190 /* Install the discriminant at index 0 in the union. */
10191 TYPE_FIELD (union_type, 0) = *disr_field;
10192 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10193 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10194
10195 /* Install the union in the outer struct type. */
10196 TYPE_FIELD_TYPE (type, 0) = union_type;
10197 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10198 TYPE_NFIELDS (type) = 1;
10199
10200 /* Set the size and offset of the union type. */
10201 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10202
10203 /* We need a way to find the correct discriminant given a
10204 variant name. For convenience we build a map here. */
10205 struct type *enum_type = FIELD_TYPE (*disr_field);
10206 std::unordered_map<std::string, ULONGEST> discriminant_map;
10207 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10208 {
10209 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10210 {
10211 const char *name
10212 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10213 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10214 }
10215 }
10216
10217 int n_fields = TYPE_NFIELDS (union_type);
10218 struct discriminant_info *disc
10219 = alloc_discriminant_info (union_type, 0, -1);
10220 /* Skip the discriminant here. */
10221 for (int i = 1; i < n_fields; ++i)
10222 {
10223 /* Find the final word in the name of this variant's type.
10224 That name can be used to look up the correct
10225 discriminant. */
10226 const char *variant_name
10227 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10228 i)));
10229
10230 auto iter = discriminant_map.find (variant_name);
10231 if (iter != discriminant_map.end ())
10232 disc->discriminants[i] = iter->second;
10233
bedda9ac 10234 /* Remove the discriminant field, if it exists. */
c9317f21 10235 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10236 if (TYPE_NFIELDS (sub_type) > 0)
10237 {
10238 --TYPE_NFIELDS (sub_type);
10239 ++TYPE_FIELDS (sub_type);
10240 }
c9317f21
TT
10241 TYPE_FIELD_NAME (union_type, i) = variant_name;
10242 TYPE_NAME (sub_type)
10243 = rust_fully_qualify (&objfile->objfile_obstack,
10244 TYPE_NAME (type), variant_name);
10245 }
10246 }
10247}
10248
10249/* Rewrite some Rust unions to be structures with variants parts. */
10250
10251static void
10252rust_union_quirks (struct dwarf2_cu *cu)
10253{
10254 gdb_assert (cu->language == language_rust);
52941706
SM
10255 for (type *type_ : cu->rust_unions)
10256 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10257 /* We don't need this any more. */
10258 cu->rust_unions.clear ();
c9317f21
TT
10259}
10260
95554aad
TT
10261/* Return the symtab for PER_CU. This works properly regardless of
10262 whether we're using the index or psymtabs. */
10263
43f3e411
DE
10264static struct compunit_symtab *
10265get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10266{
ed2dc618 10267 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10268 ? per_cu->v.quick->compunit_symtab
10269 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10270}
10271
10272/* A helper function for computing the list of all symbol tables
10273 included by PER_CU. */
10274
10275static void
4c39bc03 10276recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10277 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10278 struct dwarf2_per_cu_data *per_cu,
43f3e411 10279 struct compunit_symtab *immediate_parent)
95554aad
TT
10280{
10281 void **slot;
43f3e411 10282 struct compunit_symtab *cust;
95554aad
TT
10283
10284 slot = htab_find_slot (all_children, per_cu, INSERT);
10285 if (*slot != NULL)
10286 {
10287 /* This inclusion and its children have been processed. */
10288 return;
10289 }
10290
10291 *slot = per_cu;
10292 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10293 cust = get_compunit_symtab (per_cu);
10294 if (cust != NULL)
ec94af83
DE
10295 {
10296 /* If this is a type unit only add its symbol table if we haven't
10297 seen it yet (type unit per_cu's can share symtabs). */
10298 if (per_cu->is_debug_types)
10299 {
43f3e411 10300 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10301 if (*slot == NULL)
10302 {
43f3e411 10303 *slot = cust;
4c39bc03 10304 result->push_back (cust);
43f3e411
DE
10305 if (cust->user == NULL)
10306 cust->user = immediate_parent;
ec94af83
DE
10307 }
10308 }
10309 else
f9125b6c 10310 {
4c39bc03 10311 result->push_back (cust);
43f3e411
DE
10312 if (cust->user == NULL)
10313 cust->user = immediate_parent;
f9125b6c 10314 }
ec94af83 10315 }
95554aad 10316
ae640021
AB
10317 if (!per_cu->imported_symtabs_empty ())
10318 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10319 {
10320 recursively_compute_inclusions (result, all_children,
10321 all_type_symtabs, ptr, cust);
10322 }
95554aad
TT
10323}
10324
43f3e411 10325/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10326 PER_CU. */
10327
10328static void
43f3e411 10329compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10330{
f4dc4d17
DE
10331 gdb_assert (! per_cu->is_debug_types);
10332
ae640021 10333 if (!per_cu->imported_symtabs_empty ())
95554aad 10334 {
ae640021 10335 int len;
4c39bc03 10336 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10337 htab_t all_children, all_type_symtabs;
43f3e411 10338 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10339
10340 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10341 if (cust == NULL)
95554aad
TT
10342 return;
10343
10344 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10345 NULL, xcalloc, xfree);
ec94af83
DE
10346 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10347 NULL, xcalloc, xfree);
95554aad 10348
ae640021 10349 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10350 {
10351 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10352 all_type_symtabs, ptr, cust);
ec94af83 10353 }
95554aad 10354
ec94af83 10355 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10356 len = result_symtabs.size ();
43f3e411 10357 cust->includes
ed2dc618 10358 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10359 struct compunit_symtab *, len + 1);
4c39bc03
TT
10360 memcpy (cust->includes, result_symtabs.data (),
10361 len * sizeof (compunit_symtab *));
43f3e411 10362 cust->includes[len] = NULL;
95554aad 10363
95554aad 10364 htab_delete (all_children);
ec94af83 10365 htab_delete (all_type_symtabs);
95554aad
TT
10366 }
10367}
10368
10369/* Compute the 'includes' field for the symtabs of all the CUs we just
10370 read. */
10371
10372static void
ed2dc618 10373process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10374{
71b73764 10375 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10376 {
10377 if (! iter->is_debug_types)
43f3e411 10378 compute_compunit_symtab_includes (iter);
f4dc4d17 10379 }
95554aad 10380
c5d0225d 10381 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10382}
10383
9cdd5dbd 10384/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10385 already been loaded into memory. */
10386
10387static void
95554aad
TT
10388process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10389 enum language pretend_language)
10b3939b 10390{
10b3939b 10391 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10392 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10393 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10394 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10395 CORE_ADDR lowpc, highpc;
43f3e411 10396 struct compunit_symtab *cust;
10b3939b 10397 CORE_ADDR baseaddr;
4359dff1 10398 struct block *static_block;
3e29f34a 10399 CORE_ADDR addr;
10b3939b 10400
6a053cb1 10401 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
10b3939b 10402
c89b44cd
TT
10403 /* Clear the list here in case something was left over. */
10404 cu->method_list.clear ();
10b3939b 10405
95554aad
TT
10406 cu->language = pretend_language;
10407 cu->language_defn = language_def (cu->language);
10408
c906108c 10409 /* Do line number decoding in read_file_scope () */
10b3939b 10410 process_die (cu->dies, cu);
c906108c 10411
a766d390
DE
10412 /* For now fudge the Go package. */
10413 if (cu->language == language_go)
10414 fixup_go_packaging (cu);
10415
5f48f8f3 10416 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10417 should be complete, and it should now be safe to compute all of the
10418 physnames. */
10419 compute_delayed_physnames (cu);
3da10d80 10420
c9317f21
TT
10421 if (cu->language == language_rust)
10422 rust_union_quirks (cu);
10423
fae299cd
DC
10424 /* Some compilers don't define a DW_AT_high_pc attribute for the
10425 compilation unit. If the DW_AT_high_pc is missing, synthesize
10426 it, by scanning the DIE's below the compilation unit. */
10b3939b 10427 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10428
3e29f34a 10429 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10430 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10431
10432 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10433 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10434 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10435 addrmap to help ensure it has an accurate map of pc values belonging to
10436 this comp unit. */
10437 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10438
c24bdb02 10439 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10440 SECT_OFF_TEXT (objfile),
10441 0);
c906108c 10442
43f3e411 10443 if (cust != NULL)
c906108c 10444 {
df15bd07 10445 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10446
8be455d7
JK
10447 /* Set symtab language to language from DW_AT_language. If the
10448 compilation is from a C file generated by language preprocessors, do
10449 not set the language if it was already deduced by start_subfile. */
43f3e411 10450 if (!(cu->language == language_c
40e3ad0e 10451 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10452 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10453
10454 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10455 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10456 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10457 there were bugs in prologue debug info, fixed later in GCC-4.5
10458 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10459
10460 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10461 needed, it would be wrong due to missing DW_AT_producer there.
10462
10463 Still one can confuse GDB by using non-standard GCC compilation
10464 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10465 */
ab260dad 10466 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10467 cust->locations_valid = 1;
e0d00bc7
JK
10468
10469 if (gcc_4_minor >= 5)
43f3e411 10470 cust->epilogue_unwind_valid = 1;
96408a79 10471
43f3e411 10472 cust->call_site_htab = cu->call_site_htab;
c906108c 10473 }
9291a0cd
TT
10474
10475 if (dwarf2_per_objfile->using_index)
43f3e411 10476 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10477 else
10478 {
10479 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10480 pst->compunit_symtab = cust;
9291a0cd
TT
10481 pst->readin = 1;
10482 }
c906108c 10483
95554aad 10484 /* Push it for inclusion processing later. */
c5d0225d 10485 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10486
10487 /* Not needed any more. */
c24bdb02 10488 cu->reset_builder ();
f4dc4d17 10489}
45cfd468 10490
f4dc4d17
DE
10491/* Generate full symbol information for type unit PER_CU, whose DIEs have
10492 already been loaded into memory. */
10493
10494static void
10495process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10496 enum language pretend_language)
10497{
10498 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10499 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10500 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10501 struct compunit_symtab *cust;
0186c6a7
DE
10502 struct signatured_type *sig_type;
10503
10504 gdb_assert (per_cu->is_debug_types);
10505 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10506
c89b44cd
TT
10507 /* Clear the list here in case something was left over. */
10508 cu->method_list.clear ();
f4dc4d17 10509
f4dc4d17
DE
10510 cu->language = pretend_language;
10511 cu->language_defn = language_def (cu->language);
10512
10513 /* The symbol tables are set up in read_type_unit_scope. */
10514 process_die (cu->dies, cu);
10515
10516 /* For now fudge the Go package. */
10517 if (cu->language == language_go)
10518 fixup_go_packaging (cu);
10519
5f48f8f3 10520 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10521 should be complete, and it should now be safe to compute all of the
10522 physnames. */
10523 compute_delayed_physnames (cu);
f4dc4d17 10524
c9317f21
TT
10525 if (cu->language == language_rust)
10526 rust_union_quirks (cu);
10527
f4dc4d17
DE
10528 /* TUs share symbol tables.
10529 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10530 of it with end_expandable_symtab. Otherwise, complete the addition of
10531 this TU's symbols to the existing symtab. */
43f3e411 10532 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10533 {
c24bdb02
KS
10534 buildsym_compunit *builder = cu->get_builder ();
10535 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10536 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10537
43f3e411 10538 if (cust != NULL)
f4dc4d17
DE
10539 {
10540 /* Set symtab language to language from DW_AT_language. If the
10541 compilation is from a C file generated by language preprocessors,
10542 do not set the language if it was already deduced by
10543 start_subfile. */
43f3e411
DE
10544 if (!(cu->language == language_c
10545 && COMPUNIT_FILETABS (cust)->language != language_c))
10546 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10547 }
10548 }
10549 else
10550 {
c24bdb02 10551 cu->get_builder ()->augment_type_symtab ();
43f3e411 10552 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10553 }
10554
10555 if (dwarf2_per_objfile->using_index)
43f3e411 10556 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10557 else
10558 {
10559 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10560 pst->compunit_symtab = cust;
f4dc4d17 10561 pst->readin = 1;
45cfd468 10562 }
804d2729
TT
10563
10564 /* Not needed any more. */
c24bdb02 10565 cu->reset_builder ();
c906108c
SS
10566}
10567
95554aad
TT
10568/* Process an imported unit DIE. */
10569
10570static void
10571process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10572{
10573 struct attribute *attr;
10574
f4dc4d17
DE
10575 /* For now we don't handle imported units in type units. */
10576 if (cu->per_cu->is_debug_types)
10577 {
10578 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10579 " supported in type units [in module %s]"),
518817b3 10580 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10581 }
10582
95554aad
TT
10583 attr = dwarf2_attr (die, DW_AT_import, cu);
10584 if (attr != NULL)
10585 {
9c541725
PA
10586 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10587 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10588 dwarf2_per_cu_data *per_cu
e3b94546 10589 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10590 cu->per_cu->dwarf2_per_objfile);
95554aad 10591
69d751e3 10592 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10593 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10594 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10595
ae640021 10596 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10597 }
10598}
10599
4c8aa72d
PA
10600/* RAII object that represents a process_die scope: i.e.,
10601 starts/finishes processing a DIE. */
10602class process_die_scope
adde2bff 10603{
4c8aa72d
PA
10604public:
10605 process_die_scope (die_info *die, dwarf2_cu *cu)
10606 : m_die (die), m_cu (cu)
10607 {
10608 /* We should only be processing DIEs not already in process. */
10609 gdb_assert (!m_die->in_process);
10610 m_die->in_process = true;
10611 }
8c3cb9fa 10612
4c8aa72d
PA
10613 ~process_die_scope ()
10614 {
10615 m_die->in_process = false;
10616
10617 /* If we're done processing the DIE for the CU that owns the line
10618 header, we don't need the line header anymore. */
10619 if (m_cu->line_header_die_owner == m_die)
10620 {
10621 delete m_cu->line_header;
10622 m_cu->line_header = NULL;
10623 m_cu->line_header_die_owner = NULL;
10624 }
10625 }
10626
10627private:
10628 die_info *m_die;
10629 dwarf2_cu *m_cu;
10630};
adde2bff 10631
c906108c
SS
10632/* Process a die and its children. */
10633
10634static void
e7c27a73 10635process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10636{
4c8aa72d 10637 process_die_scope scope (die, cu);
adde2bff 10638
c906108c
SS
10639 switch (die->tag)
10640 {
10641 case DW_TAG_padding:
10642 break;
10643 case DW_TAG_compile_unit:
95554aad 10644 case DW_TAG_partial_unit:
e7c27a73 10645 read_file_scope (die, cu);
c906108c 10646 break;
348e048f
DE
10647 case DW_TAG_type_unit:
10648 read_type_unit_scope (die, cu);
10649 break;
c906108c 10650 case DW_TAG_subprogram:
0a4b0913
AB
10651 /* Nested subprograms in Fortran get a prefix. */
10652 if (cu->language == language_fortran
10653 && die->parent != NULL
10654 && die->parent->tag == DW_TAG_subprogram)
10655 cu->processing_has_namespace_info = true;
10656 /* Fall through. */
c906108c 10657 case DW_TAG_inlined_subroutine:
edb3359d 10658 read_func_scope (die, cu);
c906108c
SS
10659 break;
10660 case DW_TAG_lexical_block:
14898363
L
10661 case DW_TAG_try_block:
10662 case DW_TAG_catch_block:
e7c27a73 10663 read_lexical_block_scope (die, cu);
c906108c 10664 break;
216f72a1 10665 case DW_TAG_call_site:
96408a79
SA
10666 case DW_TAG_GNU_call_site:
10667 read_call_site_scope (die, cu);
10668 break;
c906108c 10669 case DW_TAG_class_type:
680b30c7 10670 case DW_TAG_interface_type:
c906108c
SS
10671 case DW_TAG_structure_type:
10672 case DW_TAG_union_type:
134d01f1 10673 process_structure_scope (die, cu);
c906108c
SS
10674 break;
10675 case DW_TAG_enumeration_type:
134d01f1 10676 process_enumeration_scope (die, cu);
c906108c 10677 break;
134d01f1 10678
f792889a
DJ
10679 /* These dies have a type, but processing them does not create
10680 a symbol or recurse to process the children. Therefore we can
10681 read them on-demand through read_type_die. */
c906108c 10682 case DW_TAG_subroutine_type:
72019c9c 10683 case DW_TAG_set_type:
c906108c 10684 case DW_TAG_array_type:
c906108c 10685 case DW_TAG_pointer_type:
c906108c 10686 case DW_TAG_ptr_to_member_type:
c906108c 10687 case DW_TAG_reference_type:
4297a3f0 10688 case DW_TAG_rvalue_reference_type:
c906108c 10689 case DW_TAG_string_type:
c906108c 10690 break;
134d01f1 10691
c906108c 10692 case DW_TAG_base_type:
a02abb62 10693 case DW_TAG_subrange_type:
cb249c71 10694 case DW_TAG_typedef:
134d01f1
DJ
10695 /* Add a typedef symbol for the type definition, if it has a
10696 DW_AT_name. */
f792889a 10697 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10698 break;
c906108c 10699 case DW_TAG_common_block:
e7c27a73 10700 read_common_block (die, cu);
c906108c
SS
10701 break;
10702 case DW_TAG_common_inclusion:
10703 break;
d9fa45fe 10704 case DW_TAG_namespace:
9068261f 10705 cu->processing_has_namespace_info = true;
e7c27a73 10706 read_namespace (die, cu);
d9fa45fe 10707 break;
5d7cb8df 10708 case DW_TAG_module:
9068261f 10709 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10710 read_module (die, cu);
10711 break;
d9fa45fe 10712 case DW_TAG_imported_declaration:
9068261f 10713 cu->processing_has_namespace_info = true;
74921315
KS
10714 if (read_namespace_alias (die, cu))
10715 break;
86a73007
TT
10716 /* The declaration is not a global namespace alias. */
10717 /* Fall through. */
d9fa45fe 10718 case DW_TAG_imported_module:
9068261f 10719 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10720 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10721 || cu->language != language_fortran))
b98664d3 10722 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10723 dwarf_tag_name (die->tag));
10724 read_import_statement (die, cu);
d9fa45fe 10725 break;
95554aad
TT
10726
10727 case DW_TAG_imported_unit:
10728 process_imported_unit_die (die, cu);
10729 break;
10730
71a3c369
TT
10731 case DW_TAG_variable:
10732 read_variable (die, cu);
10733 break;
10734
c906108c 10735 default:
e7c27a73 10736 new_symbol (die, NULL, cu);
c906108c
SS
10737 break;
10738 }
10739}
ca69b9e6
DE
10740\f
10741/* DWARF name computation. */
c906108c 10742
94af9270
KS
10743/* A helper function for dwarf2_compute_name which determines whether DIE
10744 needs to have the name of the scope prepended to the name listed in the
10745 die. */
10746
10747static int
10748die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10749{
1c809c68
TT
10750 struct attribute *attr;
10751
94af9270
KS
10752 switch (die->tag)
10753 {
10754 case DW_TAG_namespace:
10755 case DW_TAG_typedef:
10756 case DW_TAG_class_type:
10757 case DW_TAG_interface_type:
10758 case DW_TAG_structure_type:
10759 case DW_TAG_union_type:
10760 case DW_TAG_enumeration_type:
10761 case DW_TAG_enumerator:
10762 case DW_TAG_subprogram:
08a76f8a 10763 case DW_TAG_inlined_subroutine:
94af9270 10764 case DW_TAG_member:
74921315 10765 case DW_TAG_imported_declaration:
94af9270
KS
10766 return 1;
10767
10768 case DW_TAG_variable:
c2b0a229 10769 case DW_TAG_constant:
94af9270
KS
10770 /* We only need to prefix "globally" visible variables. These include
10771 any variable marked with DW_AT_external or any variable that
10772 lives in a namespace. [Variables in anonymous namespaces
10773 require prefixing, but they are not DW_AT_external.] */
10774
10775 if (dwarf2_attr (die, DW_AT_specification, cu))
10776 {
10777 struct dwarf2_cu *spec_cu = cu;
9a619af0 10778
94af9270
KS
10779 return die_needs_namespace (die_specification (die, &spec_cu),
10780 spec_cu);
10781 }
10782
1c809c68 10783 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10784 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10785 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10786 return 0;
10787 /* A variable in a lexical block of some kind does not need a
10788 namespace, even though in C++ such variables may be external
10789 and have a mangled name. */
10790 if (die->parent->tag == DW_TAG_lexical_block
10791 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10792 || die->parent->tag == DW_TAG_catch_block
10793 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10794 return 0;
10795 return 1;
94af9270
KS
10796
10797 default:
10798 return 0;
10799 }
10800}
10801
73b9be8b
KS
10802/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10803 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10804 defined for the given DIE. */
10805
10806static struct attribute *
10807dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10808{
10809 struct attribute *attr;
10810
10811 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10812 if (attr == NULL)
10813 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10814
10815 return attr;
10816}
10817
10818/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10819 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10820 defined for the given DIE. */
10821
10822static const char *
10823dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10824{
10825 const char *linkage_name;
10826
10827 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10828 if (linkage_name == NULL)
10829 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10830
10831 return linkage_name;
10832}
10833
94af9270 10834/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10835 compute the physname for the object, which include a method's:
9c37b5ae 10836 - formal parameters (C++),
a766d390 10837 - receiver type (Go),
a766d390
DE
10838
10839 The term "physname" is a bit confusing.
10840 For C++, for example, it is the demangled name.
10841 For Go, for example, it's the mangled name.
94af9270 10842
af6b7be1
JB
10843 For Ada, return the DIE's linkage name rather than the fully qualified
10844 name. PHYSNAME is ignored..
10845
94af9270
KS
10846 The result is allocated on the objfile_obstack and canonicalized. */
10847
10848static const char *
15d034d0
TT
10849dwarf2_compute_name (const char *name,
10850 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10851 int physname)
10852{
518817b3 10853 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10854
94af9270
KS
10855 if (name == NULL)
10856 name = dwarf2_name (die, cu);
10857
2ee7123e
DE
10858 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10859 but otherwise compute it by typename_concat inside GDB.
10860 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10861 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10862 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10863 will set the demangled name to the result of dwarf2_full_name, and it is
10864 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10865 if (cu->language == language_ada
10866 || (cu->language == language_fortran && physname))
10867 {
10868 /* For Ada unit, we prefer the linkage name over the name, as
10869 the former contains the exported name, which the user expects
10870 to be able to reference. Ideally, we want the user to be able
10871 to reference this entity using either natural or linkage name,
10872 but we haven't started looking at this enhancement yet. */
73b9be8b 10873 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10874
2ee7123e
DE
10875 if (linkage_name != NULL)
10876 return linkage_name;
f55ee35c
JK
10877 }
10878
94af9270
KS
10879 /* These are the only languages we know how to qualify names in. */
10880 if (name != NULL
9c37b5ae 10881 && (cu->language == language_cplus
c44af4eb
TT
10882 || cu->language == language_fortran || cu->language == language_d
10883 || cu->language == language_rust))
94af9270
KS
10884 {
10885 if (die_needs_namespace (die, cu))
10886 {
0d5cff50 10887 const char *prefix;
34a68019 10888 const char *canonical_name = NULL;
94af9270 10889
d7e74731
PA
10890 string_file buf;
10891
94af9270 10892 prefix = determine_prefix (die, cu);
94af9270
KS
10893 if (*prefix != '\0')
10894 {
43816ebc
TT
10895 gdb::unique_xmalloc_ptr<char> prefixed_name
10896 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10897
43816ebc 10898 buf.puts (prefixed_name.get ());
94af9270
KS
10899 }
10900 else
d7e74731 10901 buf.puts (name);
94af9270 10902
98bfdba5
PA
10903 /* Template parameters may be specified in the DIE's DW_AT_name, or
10904 as children with DW_TAG_template_type_param or
10905 DW_TAG_value_type_param. If the latter, add them to the name
10906 here. If the name already has template parameters, then
10907 skip this step; some versions of GCC emit both, and
10908 it is more efficient to use the pre-computed name.
10909
10910 Something to keep in mind about this process: it is very
10911 unlikely, or in some cases downright impossible, to produce
10912 something that will match the mangled name of a function.
10913 If the definition of the function has the same debug info,
10914 we should be able to match up with it anyway. But fallbacks
10915 using the minimal symbol, for instance to find a method
10916 implemented in a stripped copy of libstdc++, will not work.
10917 If we do not have debug info for the definition, we will have to
10918 match them up some other way.
10919
10920 When we do name matching there is a related problem with function
10921 templates; two instantiated function templates are allowed to
10922 differ only by their return types, which we do not add here. */
10923
10924 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10925 {
10926 struct attribute *attr;
10927 struct die_info *child;
10928 int first = 1;
10929
10930 die->building_fullname = 1;
10931
10932 for (child = die->child; child != NULL; child = child->sibling)
10933 {
10934 struct type *type;
12df843f 10935 LONGEST value;
d521ce57 10936 const gdb_byte *bytes;
98bfdba5
PA
10937 struct dwarf2_locexpr_baton *baton;
10938 struct value *v;
10939
10940 if (child->tag != DW_TAG_template_type_param
10941 && child->tag != DW_TAG_template_value_param)
10942 continue;
10943
10944 if (first)
10945 {
d7e74731 10946 buf.puts ("<");
98bfdba5
PA
10947 first = 0;
10948 }
10949 else
d7e74731 10950 buf.puts (", ");
98bfdba5
PA
10951
10952 attr = dwarf2_attr (child, DW_AT_type, cu);
10953 if (attr == NULL)
10954 {
b98664d3 10955 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10956 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10957 continue;
10958 }
10959 type = die_type (child, cu);
10960
10961 if (child->tag == DW_TAG_template_type_param)
10962 {
c1ec8cea
TT
10963 c_print_type (type, "", &buf, -1, 0, cu->language,
10964 &type_print_raw_options);
98bfdba5
PA
10965 continue;
10966 }
10967
10968 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10969 if (attr == NULL)
10970 {
b98664d3 10971 complaint (_("template parameter missing "
3e43a32a 10972 "DW_AT_const_value"));
d7e74731 10973 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10974 continue;
10975 }
10976
10977 dwarf2_const_value_attr (attr, type, name,
10978 &cu->comp_unit_obstack, cu,
10979 &value, &bytes, &baton);
10980
10981 if (TYPE_NOSIGN (type))
10982 /* GDB prints characters as NUMBER 'CHAR'. If that's
10983 changed, this can use value_print instead. */
d7e74731 10984 c_printchar (value, type, &buf);
98bfdba5
PA
10985 else
10986 {
10987 struct value_print_options opts;
10988
10989 if (baton != NULL)
10990 v = dwarf2_evaluate_loc_desc (type, NULL,
10991 baton->data,
10992 baton->size,
10993 baton->per_cu);
10994 else if (bytes != NULL)
10995 {
10996 v = allocate_value (type);
10997 memcpy (value_contents_writeable (v), bytes,
10998 TYPE_LENGTH (type));
10999 }
11000 else
11001 v = value_from_longest (type, value);
11002
3e43a32a
MS
11003 /* Specify decimal so that we do not depend on
11004 the radix. */
98bfdba5
PA
11005 get_formatted_print_options (&opts, 'd');
11006 opts.raw = 1;
d7e74731 11007 value_print (v, &buf, &opts);
98bfdba5 11008 release_value (v);
98bfdba5
PA
11009 }
11010 }
11011
11012 die->building_fullname = 0;
11013
11014 if (!first)
11015 {
11016 /* Close the argument list, with a space if necessary
11017 (nested templates). */
d7e74731
PA
11018 if (!buf.empty () && buf.string ().back () == '>')
11019 buf.puts (" >");
98bfdba5 11020 else
d7e74731 11021 buf.puts (">");
98bfdba5
PA
11022 }
11023 }
11024
9c37b5ae 11025 /* For C++ methods, append formal parameter type
94af9270 11026 information, if PHYSNAME. */
6e70227d 11027
94af9270 11028 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11029 && cu->language == language_cplus)
94af9270
KS
11030 {
11031 struct type *type = read_type_die (die, cu);
11032
d7e74731 11033 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11034 &type_print_raw_options);
94af9270 11035
9c37b5ae 11036 if (cu->language == language_cplus)
94af9270 11037 {
60430eff
DJ
11038 /* Assume that an artificial first parameter is
11039 "this", but do not crash if it is not. RealView
11040 marks unnamed (and thus unused) parameters as
11041 artificial; there is no way to differentiate
11042 the two cases. */
94af9270
KS
11043 if (TYPE_NFIELDS (type) > 0
11044 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11045 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11046 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11047 0))))
d7e74731 11048 buf.puts (" const");
94af9270
KS
11049 }
11050 }
11051
d7e74731 11052 const std::string &intermediate_name = buf.string ();
94af9270
KS
11053
11054 if (cu->language == language_cplus)
34a68019 11055 canonical_name
322a8516 11056 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11057 &objfile->per_bfd->storage_obstack);
11058
11059 /* If we only computed INTERMEDIATE_NAME, or if
11060 INTERMEDIATE_NAME is already canonical, then we need to
11061 copy it to the appropriate obstack. */
322a8516 11062 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11063 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11064 intermediate_name);
34a68019
TT
11065 else
11066 name = canonical_name;
94af9270
KS
11067 }
11068 }
11069
11070 return name;
11071}
11072
0114d602
DJ
11073/* Return the fully qualified name of DIE, based on its DW_AT_name.
11074 If scope qualifiers are appropriate they will be added. The result
34a68019 11075 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11076 not have a name. NAME may either be from a previous call to
11077 dwarf2_name or NULL.
11078
9c37b5ae 11079 The output string will be canonicalized (if C++). */
0114d602
DJ
11080
11081static const char *
15d034d0 11082dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11083{
94af9270
KS
11084 return dwarf2_compute_name (name, die, cu, 0);
11085}
0114d602 11086
94af9270
KS
11087/* Construct a physname for the given DIE in CU. NAME may either be
11088 from a previous call to dwarf2_name or NULL. The result will be
11089 allocated on the objfile_objstack or NULL if the DIE does not have a
11090 name.
0114d602 11091
9c37b5ae 11092 The output string will be canonicalized (if C++). */
0114d602 11093
94af9270 11094static const char *
15d034d0 11095dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11096{
518817b3 11097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11098 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11099 int need_copy = 1;
11100
11101 /* In this case dwarf2_compute_name is just a shortcut not building anything
11102 on its own. */
11103 if (!die_needs_namespace (die, cu))
11104 return dwarf2_compute_name (name, die, cu, 1);
11105
73b9be8b 11106 mangled = dw2_linkage_name (die, cu);
900e11f9 11107
e98c9e7c
TT
11108 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11109 See https://github.com/rust-lang/rust/issues/32925. */
11110 if (cu->language == language_rust && mangled != NULL
11111 && strchr (mangled, '{') != NULL)
11112 mangled = NULL;
11113
900e11f9
JK
11114 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11115 has computed. */
791afaa2 11116 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11117 if (mangled != NULL)
900e11f9 11118 {
900e11f9 11119
59cc4834
JB
11120 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11121 {
11122 /* Do nothing (do not demangle the symbol name). */
11123 }
11124 else if (cu->language == language_go)
a766d390 11125 {
5e2db402
TT
11126 /* This is a lie, but we already lie to the caller new_symbol.
11127 new_symbol assumes we return the mangled name.
a766d390 11128 This just undoes that lie until things are cleaned up. */
a766d390
DE
11129 }
11130 else
11131 {
0eb876f5
JB
11132 /* Use DMGL_RET_DROP for C++ template functions to suppress
11133 their return type. It is easier for GDB users to search
11134 for such functions as `name(params)' than `long name(params)'.
11135 In such case the minimal symbol names do not match the full
11136 symbol names but for template functions there is never a need
11137 to look up their definition from their declaration so
11138 the only disadvantage remains the minimal symbol variant
11139 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11140 demangled.reset (gdb_demangle (mangled,
11141 (DMGL_PARAMS | DMGL_ANSI
11142 | DMGL_RET_DROP)));
a766d390 11143 }
900e11f9 11144 if (demangled)
791afaa2 11145 canon = demangled.get ();
900e11f9
JK
11146 else
11147 {
11148 canon = mangled;
11149 need_copy = 0;
11150 }
11151 }
11152
11153 if (canon == NULL || check_physname)
11154 {
11155 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11156
11157 if (canon != NULL && strcmp (physname, canon) != 0)
11158 {
11159 /* It may not mean a bug in GDB. The compiler could also
11160 compute DW_AT_linkage_name incorrectly. But in such case
11161 GDB would need to be bug-to-bug compatible. */
11162
b98664d3 11163 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11164 "(from linkage <%s>) - DIE at %s [in module %s]"),
11165 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11166 objfile_name (objfile));
900e11f9
JK
11167
11168 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11169 is available here - over computed PHYSNAME. It is safer
11170 against both buggy GDB and buggy compilers. */
11171
11172 retval = canon;
11173 }
11174 else
11175 {
11176 retval = physname;
11177 need_copy = 0;
11178 }
11179 }
11180 else
11181 retval = canon;
11182
11183 if (need_copy)
021887d8 11184 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11185
900e11f9 11186 return retval;
0114d602
DJ
11187}
11188
74921315
KS
11189/* Inspect DIE in CU for a namespace alias. If one exists, record
11190 a new symbol for it.
11191
11192 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11193
11194static int
11195read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11196{
11197 struct attribute *attr;
11198
11199 /* If the die does not have a name, this is not a namespace
11200 alias. */
11201 attr = dwarf2_attr (die, DW_AT_name, cu);
11202 if (attr != NULL)
11203 {
11204 int num;
11205 struct die_info *d = die;
11206 struct dwarf2_cu *imported_cu = cu;
11207
11208 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11209 keep inspecting DIEs until we hit the underlying import. */
11210#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11211 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11212 {
11213 attr = dwarf2_attr (d, DW_AT_import, cu);
11214 if (attr == NULL)
11215 break;
11216
11217 d = follow_die_ref (d, attr, &imported_cu);
11218 if (d->tag != DW_TAG_imported_declaration)
11219 break;
11220 }
11221
11222 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11223 {
b98664d3 11224 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11225 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11226 return 0;
11227 }
11228
11229 if (attr != NULL)
11230 {
11231 struct type *type;
9c541725 11232 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11233
9c541725 11234 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11235 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11236 {
11237 /* This declaration is a global namespace alias. Add
11238 a symbol for it whose type is the aliased namespace. */
11239 new_symbol (die, type, cu);
11240 return 1;
11241 }
11242 }
11243 }
11244
11245 return 0;
11246}
11247
22cee43f 11248/* Return the using directives repository (global or local?) to use in the
804d2729 11249 current context for CU.
22cee43f
PMR
11250
11251 For Ada, imported declarations can materialize renamings, which *may* be
11252 global. However it is impossible (for now?) in DWARF to distinguish
11253 "external" imported declarations and "static" ones. As all imported
11254 declarations seem to be static in all other languages, make them all CU-wide
11255 global only in Ada. */
11256
11257static struct using_direct **
804d2729 11258using_directives (struct dwarf2_cu *cu)
22cee43f 11259{
c24bdb02
KS
11260 if (cu->language == language_ada
11261 && cu->get_builder ()->outermost_context_p ())
11262 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11263 else
c24bdb02 11264 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11265}
11266
27aa8d6a
SW
11267/* Read the import statement specified by the given die and record it. */
11268
11269static void
11270read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11271{
518817b3 11272 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11273 struct attribute *import_attr;
32019081 11274 struct die_info *imported_die, *child_die;
de4affc9 11275 struct dwarf2_cu *imported_cu;
27aa8d6a 11276 const char *imported_name;
794684b6 11277 const char *imported_name_prefix;
13387711
SW
11278 const char *canonical_name;
11279 const char *import_alias;
11280 const char *imported_declaration = NULL;
794684b6 11281 const char *import_prefix;
eb1e02fd 11282 std::vector<const char *> excludes;
13387711 11283
27aa8d6a
SW
11284 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11285 if (import_attr == NULL)
11286 {
b98664d3 11287 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11288 dwarf_tag_name (die->tag));
11289 return;
11290 }
11291
de4affc9
CC
11292 imported_cu = cu;
11293 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11294 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11295 if (imported_name == NULL)
11296 {
11297 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11298
11299 The import in the following code:
11300 namespace A
11301 {
11302 typedef int B;
11303 }
11304
11305 int main ()
11306 {
11307 using A::B;
11308 B b;
11309 return b;
11310 }
11311
11312 ...
11313 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11314 <52> DW_AT_decl_file : 1
11315 <53> DW_AT_decl_line : 6
11316 <54> DW_AT_import : <0x75>
11317 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11318 <59> DW_AT_name : B
11319 <5b> DW_AT_decl_file : 1
11320 <5c> DW_AT_decl_line : 2
11321 <5d> DW_AT_type : <0x6e>
11322 ...
11323 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11324 <76> DW_AT_byte_size : 4
11325 <77> DW_AT_encoding : 5 (signed)
11326
11327 imports the wrong die ( 0x75 instead of 0x58 ).
11328 This case will be ignored until the gcc bug is fixed. */
11329 return;
11330 }
11331
82856980
SW
11332 /* Figure out the local name after import. */
11333 import_alias = dwarf2_name (die, cu);
27aa8d6a 11334
794684b6
SW
11335 /* Figure out where the statement is being imported to. */
11336 import_prefix = determine_prefix (die, cu);
11337
11338 /* Figure out what the scope of the imported die is and prepend it
11339 to the name of the imported die. */
de4affc9 11340 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11341
f55ee35c
JK
11342 if (imported_die->tag != DW_TAG_namespace
11343 && imported_die->tag != DW_TAG_module)
794684b6 11344 {
13387711
SW
11345 imported_declaration = imported_name;
11346 canonical_name = imported_name_prefix;
794684b6 11347 }
13387711 11348 else if (strlen (imported_name_prefix) > 0)
12aaed36 11349 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11350 imported_name_prefix,
11351 (cu->language == language_d ? "." : "::"),
11352 imported_name, (char *) NULL);
13387711
SW
11353 else
11354 canonical_name = imported_name;
794684b6 11355
32019081
JK
11356 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11357 for (child_die = die->child; child_die && child_die->tag;
11358 child_die = sibling_die (child_die))
11359 {
11360 /* DWARF-4: A Fortran use statement with a “rename list” may be
11361 represented by an imported module entry with an import attribute
11362 referring to the module and owned entries corresponding to those
11363 entities that are renamed as part of being imported. */
11364
11365 if (child_die->tag != DW_TAG_imported_declaration)
11366 {
b98664d3 11367 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11368 "- DIE at %s [in module %s]"),
11369 sect_offset_str (child_die->sect_off),
11370 objfile_name (objfile));
32019081
JK
11371 continue;
11372 }
11373
11374 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11375 if (import_attr == NULL)
11376 {
b98664d3 11377 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11378 dwarf_tag_name (child_die->tag));
11379 continue;
11380 }
11381
11382 imported_cu = cu;
11383 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11384 &imported_cu);
11385 imported_name = dwarf2_name (imported_die, imported_cu);
11386 if (imported_name == NULL)
11387 {
b98664d3 11388 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11389 "imported name - DIE at %s [in module %s]"),
11390 sect_offset_str (child_die->sect_off),
11391 objfile_name (objfile));
32019081
JK
11392 continue;
11393 }
11394
eb1e02fd 11395 excludes.push_back (imported_name);
32019081
JK
11396
11397 process_die (child_die, cu);
11398 }
11399
804d2729 11400 add_using_directive (using_directives (cu),
22cee43f
PMR
11401 import_prefix,
11402 canonical_name,
11403 import_alias,
11404 imported_declaration,
11405 excludes,
11406 0,
11407 &objfile->objfile_obstack);
27aa8d6a
SW
11408}
11409
5230b05a
WT
11410/* ICC<14 does not output the required DW_AT_declaration on incomplete
11411 types, but gives them a size of zero. Starting with version 14,
11412 ICC is compatible with GCC. */
11413
9068261f 11414static bool
5230b05a
WT
11415producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11416{
11417 if (!cu->checked_producer)
11418 check_producer (cu);
11419
11420 return cu->producer_is_icc_lt_14;
11421}
11422
eb77c9df
AB
11423/* ICC generates a DW_AT_type for C void functions. This was observed on
11424 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11425 which says that void functions should not have a DW_AT_type. */
11426
11427static bool
11428producer_is_icc (struct dwarf2_cu *cu)
11429{
11430 if (!cu->checked_producer)
11431 check_producer (cu);
11432
11433 return cu->producer_is_icc;
11434}
11435
1b80a9fa
JK
11436/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11437 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11438 this, it was first present in GCC release 4.3.0. */
11439
9068261f 11440static bool
1b80a9fa
JK
11441producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11442{
11443 if (!cu->checked_producer)
11444 check_producer (cu);
11445
11446 return cu->producer_is_gcc_lt_4_3;
11447}
11448
d721ba37
PA
11449static file_and_directory
11450find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11451{
d721ba37
PA
11452 file_and_directory res;
11453
9291a0cd
TT
11454 /* Find the filename. Do not use dwarf2_name here, since the filename
11455 is not a source language identifier. */
d721ba37
PA
11456 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11457 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11458
d721ba37
PA
11459 if (res.comp_dir == NULL
11460 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11461 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11462 {
d721ba37
PA
11463 res.comp_dir_storage = ldirname (res.name);
11464 if (!res.comp_dir_storage.empty ())
11465 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11466 }
d721ba37 11467 if (res.comp_dir != NULL)
9291a0cd
TT
11468 {
11469 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11470 directory, get rid of it. */
d721ba37 11471 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11472
d721ba37
PA
11473 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11474 res.comp_dir = cp + 1;
9291a0cd
TT
11475 }
11476
d721ba37
PA
11477 if (res.name == NULL)
11478 res.name = "<unknown>";
11479
11480 return res;
9291a0cd
TT
11481}
11482
f4dc4d17
DE
11483/* Handle DW_AT_stmt_list for a compilation unit.
11484 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11485 COMP_DIR is the compilation directory. LOWPC is passed to
11486 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11487
11488static void
11489handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11490 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11491{
518817b3
SM
11492 struct dwarf2_per_objfile *dwarf2_per_objfile
11493 = cu->per_cu->dwarf2_per_objfile;
527f3840 11494 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11495 struct attribute *attr;
527f3840
JK
11496 struct line_header line_header_local;
11497 hashval_t line_header_local_hash;
527f3840
JK
11498 void **slot;
11499 int decode_mapping;
2ab95328 11500
f4dc4d17
DE
11501 gdb_assert (! cu->per_cu->is_debug_types);
11502
2ab95328 11503 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11504 if (attr == NULL)
11505 return;
11506
9c541725 11507 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11508
11509 /* The line header hash table is only created if needed (it exists to
11510 prevent redundant reading of the line table for partial_units).
11511 If we're given a partial_unit, we'll need it. If we're given a
11512 compile_unit, then use the line header hash table if it's already
11513 created, but don't create one just yet. */
11514
11515 if (dwarf2_per_objfile->line_header_hash == NULL
11516 && die->tag == DW_TAG_partial_unit)
2ab95328 11517 {
527f3840
JK
11518 dwarf2_per_objfile->line_header_hash
11519 = htab_create_alloc_ex (127, line_header_hash_voidp,
11520 line_header_eq_voidp,
11521 free_line_header_voidp,
11522 &objfile->objfile_obstack,
11523 hashtab_obstack_allocate,
11524 dummy_obstack_deallocate);
11525 }
2ab95328 11526
9c541725 11527 line_header_local.sect_off = line_offset;
527f3840
JK
11528 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11529 line_header_local_hash = line_header_hash (&line_header_local);
11530 if (dwarf2_per_objfile->line_header_hash != NULL)
11531 {
11532 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11533 &line_header_local,
11534 line_header_local_hash, NO_INSERT);
11535
11536 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11537 is not present in *SLOT (since if there is something in *SLOT then
11538 it will be for a partial_unit). */
11539 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11540 {
527f3840 11541 gdb_assert (*slot != NULL);
9a3c8263 11542 cu->line_header = (struct line_header *) *slot;
527f3840 11543 return;
dee91e82 11544 }
2ab95328 11545 }
527f3840
JK
11546
11547 /* dwarf_decode_line_header does not yet provide sufficient information.
11548 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11549 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11550 if (lh == NULL)
527f3840 11551 return;
4c8aa72d
PA
11552
11553 cu->line_header = lh.release ();
11554 cu->line_header_die_owner = die;
527f3840
JK
11555
11556 if (dwarf2_per_objfile->line_header_hash == NULL)
11557 slot = NULL;
11558 else
11559 {
11560 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11561 &line_header_local,
11562 line_header_local_hash, INSERT);
11563 gdb_assert (slot != NULL);
11564 }
11565 if (slot != NULL && *slot == NULL)
11566 {
11567 /* This newly decoded line number information unit will be owned
11568 by line_header_hash hash table. */
11569 *slot = cu->line_header;
4c8aa72d 11570 cu->line_header_die_owner = NULL;
527f3840
JK
11571 }
11572 else
11573 {
11574 /* We cannot free any current entry in (*slot) as that struct line_header
11575 may be already used by multiple CUs. Create only temporary decoded
11576 line_header for this CU - it may happen at most once for each line
11577 number information unit. And if we're not using line_header_hash
11578 then this is what we want as well. */
11579 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11580 }
11581 decode_mapping = (die->tag != DW_TAG_partial_unit);
11582 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11583 decode_mapping);
fff8551c 11584
2ab95328
TT
11585}
11586
95554aad 11587/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11588
c906108c 11589static void
e7c27a73 11590read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11591{
518817b3
SM
11592 struct dwarf2_per_objfile *dwarf2_per_objfile
11593 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11594 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11596 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11597 CORE_ADDR highpc = ((CORE_ADDR) 0);
11598 struct attribute *attr;
c906108c 11599 struct die_info *child_die;
e142c38c 11600 CORE_ADDR baseaddr;
6e70227d 11601
380618d6 11602 prepare_one_comp_unit (cu, die, cu->language);
6a053cb1 11603 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 11604
fae299cd 11605 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11606
11607 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11608 from finish_block. */
2acceee2 11609 if (lowpc == ((CORE_ADDR) -1))
c906108c 11610 lowpc = highpc;
3e29f34a 11611 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11612
d721ba37 11613 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11614
f4b8a18d
KW
11615 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11616 standardised yet. As a workaround for the language detection we fall
11617 back to the DW_AT_producer string. */
11618 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11619 cu->language = language_opencl;
11620
3019eac3
DE
11621 /* Similar hack for Go. */
11622 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11623 set_cu_language (DW_LANG_Go, cu);
11624
c24bdb02 11625 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11626
11627 /* Decode line number information if present. We do this before
11628 processing child DIEs, so that the line header table is available
11629 for DW_AT_decl_file. */
d721ba37 11630 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11631
11632 /* Process all dies in compilation unit. */
11633 if (die->child != NULL)
11634 {
11635 child_die = die->child;
11636 while (child_die && child_die->tag)
11637 {
11638 process_die (child_die, cu);
11639 child_die = sibling_die (child_die);
11640 }
11641 }
11642
11643 /* Decode macro information, if present. Dwarf 2 macro information
11644 refers to information in the line number info statement program
11645 header, so we can only read it if we've read the header
11646 successfully. */
0af92d60
JK
11647 attr = dwarf2_attr (die, DW_AT_macros, cu);
11648 if (attr == NULL)
11649 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11650 if (attr && cu->line_header)
11651 {
11652 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11653 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11654
43f3e411 11655 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11656 }
11657 else
11658 {
11659 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11660 if (attr && cu->line_header)
11661 {
11662 unsigned int macro_offset = DW_UNSND (attr);
11663
43f3e411 11664 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11665 }
11666 }
3019eac3
DE
11667}
11668
c24bdb02
KS
11669void
11670dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11671{
f4dc4d17
DE
11672 struct type_unit_group *tu_group;
11673 int first_time;
3019eac3 11674 struct attribute *attr;
9c541725 11675 unsigned int i;
0186c6a7 11676 struct signatured_type *sig_type;
3019eac3 11677
f4dc4d17 11678 gdb_assert (per_cu->is_debug_types);
0186c6a7 11679 sig_type = (struct signatured_type *) per_cu;
3019eac3 11680
c24bdb02 11681 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11682
f4dc4d17 11683 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11684 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11685 if (sig_type->type_unit_group == NULL)
c24bdb02 11686 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11687 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11688
11689 /* If we've already processed this stmt_list there's no real need to
11690 do it again, we could fake it and just recreate the part we need
11691 (file name,index -> symtab mapping). If data shows this optimization
11692 is useful we can do it then. */
43f3e411 11693 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11694
11695 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11696 debug info. */
fff8551c 11697 line_header_up lh;
f4dc4d17 11698 if (attr != NULL)
3019eac3 11699 {
9c541725 11700 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11701 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11702 }
11703 if (lh == NULL)
11704 {
11705 if (first_time)
c24bdb02 11706 start_symtab ("", NULL, 0);
f4dc4d17
DE
11707 else
11708 {
11709 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11710 gdb_assert (m_builder == nullptr);
804d2729 11711 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11712 m_builder.reset (new struct buildsym_compunit
11713 (COMPUNIT_OBJFILE (cust), "",
11714 COMPUNIT_DIRNAME (cust),
11715 compunit_language (cust),
11716 0, cust));
f4dc4d17 11717 }
f4dc4d17 11718 return;
3019eac3
DE
11719 }
11720
c24bdb02
KS
11721 line_header = lh.release ();
11722 line_header_die_owner = die;
3019eac3 11723
f4dc4d17
DE
11724 if (first_time)
11725 {
c24bdb02 11726 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11727
1fd60fc0
DE
11728 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11729 still initializing it, and our caller (a few levels up)
11730 process_full_type_unit still needs to know if this is the first
11731 time. */
11732
7ba99d21 11733 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11734 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11735 line_header->file_names_size ());
3019eac3 11736
7ba99d21
AT
11737 auto &file_names = line_header->file_names ();
11738 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11739 {
7ba99d21 11740 file_entry &fe = file_names[i];
c24bdb02
KS
11741 dwarf2_start_subfile (this, fe.name,
11742 fe.include_dir (line_header));
11743 buildsym_compunit *b = get_builder ();
11744 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11745 {
4c8aa72d
PA
11746 /* NOTE: start_subfile will recognize when it's been
11747 passed a file it has already seen. So we can't
11748 assume there's a simple mapping from
11749 cu->line_header->file_names to subfiles, plus
11750 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11751 b->get_current_subfile ()->symtab
11752 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11753 }
11754
c24bdb02 11755 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11756 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11757 }
11758 }
11759 else
3019eac3 11760 {
c24bdb02 11761 gdb_assert (m_builder == nullptr);
804d2729 11762 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11763 m_builder.reset (new struct buildsym_compunit
11764 (COMPUNIT_OBJFILE (cust), "",
11765 COMPUNIT_DIRNAME (cust),
11766 compunit_language (cust),
11767 0, cust));
f4dc4d17 11768
7ba99d21
AT
11769 auto &file_names = line_header->file_names ();
11770 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11771 {
7ba99d21 11772 file_entry &fe = file_names[i];
4c8aa72d 11773 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11774 }
3019eac3
DE
11775 }
11776
f4dc4d17
DE
11777 /* The main symtab is allocated last. Type units don't have DW_AT_name
11778 so they don't have a "real" (so to speak) symtab anyway.
11779 There is later code that will assign the main symtab to all symbols
11780 that don't have one. We need to handle the case of a symbol with a
11781 missing symtab (DW_AT_decl_file) anyway. */
11782}
3019eac3 11783
f4dc4d17
DE
11784/* Process DW_TAG_type_unit.
11785 For TUs we want to skip the first top level sibling if it's not the
11786 actual type being defined by this TU. In this case the first top
11787 level sibling is there to provide context only. */
3019eac3 11788
f4dc4d17
DE
11789static void
11790read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11791{
11792 struct die_info *child_die;
3019eac3 11793
f4dc4d17
DE
11794 prepare_one_comp_unit (cu, die, language_minimal);
11795
11796 /* Initialize (or reinitialize) the machinery for building symtabs.
11797 We do this before processing child DIEs, so that the line header table
11798 is available for DW_AT_decl_file. */
c24bdb02 11799 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11800
11801 if (die->child != NULL)
11802 {
11803 child_die = die->child;
11804 while (child_die && child_die->tag)
11805 {
11806 process_die (child_die, cu);
11807 child_die = sibling_die (child_die);
11808 }
11809 }
3019eac3
DE
11810}
11811\f
80626a55
DE
11812/* DWO/DWP files.
11813
11814 http://gcc.gnu.org/wiki/DebugFission
11815 http://gcc.gnu.org/wiki/DebugFissionDWP
11816
11817 To simplify handling of both DWO files ("object" files with the DWARF info)
11818 and DWP files (a file with the DWOs packaged up into one file), we treat
11819 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11820
11821static hashval_t
11822hash_dwo_file (const void *item)
11823{
9a3c8263 11824 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11825 hashval_t hash;
3019eac3 11826
a2ce51a0
DE
11827 hash = htab_hash_string (dwo_file->dwo_name);
11828 if (dwo_file->comp_dir != NULL)
11829 hash += htab_hash_string (dwo_file->comp_dir);
11830 return hash;
3019eac3
DE
11831}
11832
11833static int
11834eq_dwo_file (const void *item_lhs, const void *item_rhs)
11835{
9a3c8263
SM
11836 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11837 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11838
a2ce51a0
DE
11839 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11840 return 0;
11841 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11842 return lhs->comp_dir == rhs->comp_dir;
11843 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11844}
11845
11846/* Allocate a hash table for DWO files. */
11847
51ac9db5 11848static htab_up
ed2dc618 11849allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11850{
51ac9db5
SM
11851 auto delete_dwo_file = [] (void *item)
11852 {
11853 struct dwo_file *dwo_file = (struct dwo_file *) item;
11854
11855 delete dwo_file;
11856 };
11857
11858 return htab_up (htab_create_alloc_ex (41,
11859 hash_dwo_file,
11860 eq_dwo_file,
11861 delete_dwo_file,
11862 &objfile->objfile_obstack,
11863 hashtab_obstack_allocate,
11864 dummy_obstack_deallocate));
3019eac3
DE
11865}
11866
80626a55
DE
11867/* Lookup DWO file DWO_NAME. */
11868
11869static void **
ed2dc618
SM
11870lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11871 const char *dwo_name,
11872 const char *comp_dir)
80626a55
DE
11873{
11874 struct dwo_file find_entry;
11875 void **slot;
11876
11877 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11878 dwarf2_per_objfile->dwo_files
11879 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11880
0ac5b59e
DE
11881 find_entry.dwo_name = dwo_name;
11882 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11883 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11884 INSERT);
80626a55
DE
11885
11886 return slot;
11887}
11888
3019eac3
DE
11889static hashval_t
11890hash_dwo_unit (const void *item)
11891{
9a3c8263 11892 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11893
11894 /* This drops the top 32 bits of the id, but is ok for a hash. */
11895 return dwo_unit->signature;
11896}
11897
11898static int
11899eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11900{
9a3c8263
SM
11901 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11902 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11903
11904 /* The signature is assumed to be unique within the DWO file.
11905 So while object file CU dwo_id's always have the value zero,
11906 that's OK, assuming each object file DWO file has only one CU,
11907 and that's the rule for now. */
11908 return lhs->signature == rhs->signature;
11909}
11910
11911/* Allocate a hash table for DWO CUs,TUs.
11912 There is one of these tables for each of CUs,TUs for each DWO file. */
11913
11914static htab_t
11915allocate_dwo_unit_table (struct objfile *objfile)
11916{
11917 /* Start out with a pretty small number.
11918 Generally DWO files contain only one CU and maybe some TUs. */
11919 return htab_create_alloc_ex (3,
11920 hash_dwo_unit,
11921 eq_dwo_unit,
11922 NULL,
11923 &objfile->objfile_obstack,
11924 hashtab_obstack_allocate,
11925 dummy_obstack_deallocate);
11926}
11927
80626a55 11928/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11929
19c3d4c9 11930struct create_dwo_cu_data
3019eac3
DE
11931{
11932 struct dwo_file *dwo_file;
19c3d4c9 11933 struct dwo_unit dwo_unit;
3019eac3
DE
11934};
11935
19c3d4c9 11936/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11937
11938static void
19c3d4c9
DE
11939create_dwo_cu_reader (const struct die_reader_specs *reader,
11940 const gdb_byte *info_ptr,
11941 struct die_info *comp_unit_die,
11942 int has_children,
11943 void *datap)
3019eac3
DE
11944{
11945 struct dwarf2_cu *cu = reader->cu;
9c541725 11946 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11947 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11948 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11949 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11950 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11951
a084a2a6
AT
11952 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11953 if (!signature.has_value ())
3019eac3 11954 {
b98664d3 11955 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11956 " its dwo_id [in module %s]"),
9d8780f0 11957 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11958 return;
11959 }
11960
3019eac3 11961 dwo_unit->dwo_file = dwo_file;
a084a2a6 11962 dwo_unit->signature = *signature;
8a0459fd 11963 dwo_unit->section = section;
9c541725 11964 dwo_unit->sect_off = sect_off;
3019eac3
DE
11965 dwo_unit->length = cu->per_cu->length;
11966
b4f54984 11967 if (dwarf_read_debug)
9d8780f0
SM
11968 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11969 sect_offset_str (sect_off),
9c541725 11970 hex_string (dwo_unit->signature));
3019eac3
DE
11971}
11972
33c5cd75 11973/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11974 Note: This function processes DWO files only, not DWP files. */
3019eac3 11975
33c5cd75 11976static void
ed2dc618
SM
11977create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11978 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11979 htab_t &cus_htab)
3019eac3
DE
11980{
11981 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11982 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11983
33c5cd75
DB
11984 dwarf2_read_section (objfile, &section);
11985 info_ptr = section.buffer;
3019eac3
DE
11986
11987 if (info_ptr == NULL)
33c5cd75 11988 return;
3019eac3 11989
b4f54984 11990 if (dwarf_read_debug)
19c3d4c9
DE
11991 {
11992 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11993 get_section_name (&section),
11994 get_section_file_name (&section));
19c3d4c9 11995 }
3019eac3 11996
33c5cd75 11997 end_ptr = info_ptr + section.size;
3019eac3
DE
11998 while (info_ptr < end_ptr)
11999 {
12000 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12001 struct create_dwo_cu_data create_dwo_cu_data;
12002 struct dwo_unit *dwo_unit;
12003 void **slot;
12004 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12005
19c3d4c9
DE
12006 memset (&create_dwo_cu_data.dwo_unit, 0,
12007 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12008 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12009 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12010 per_cu.is_debug_types = 0;
33c5cd75
DB
12011 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12012 per_cu.section = &section;
c5ed0576 12013 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12014
12015 init_cutu_and_read_dies_no_follow (
12016 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12017 info_ptr += per_cu.length;
12018
12019 // If the unit could not be parsed, skip it.
12020 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12021 continue;
3019eac3 12022
33c5cd75
DB
12023 if (cus_htab == NULL)
12024 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12025
33c5cd75
DB
12026 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12027 *dwo_unit = create_dwo_cu_data.dwo_unit;
12028 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12029 gdb_assert (slot != NULL);
12030 if (*slot != NULL)
19c3d4c9 12031 {
33c5cd75
DB
12032 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12033 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12034
b98664d3 12035 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12036 " the entry at offset %s, signature %s"),
12037 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12038 hex_string (dwo_unit->signature));
19c3d4c9 12039 }
33c5cd75 12040 *slot = (void *)dwo_unit;
3019eac3 12041 }
3019eac3
DE
12042}
12043
80626a55
DE
12044/* DWP file .debug_{cu,tu}_index section format:
12045 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12046
d2415c6c
DE
12047 DWP Version 1:
12048
80626a55
DE
12049 Both index sections have the same format, and serve to map a 64-bit
12050 signature to a set of section numbers. Each section begins with a header,
12051 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12052 indexes, and a pool of 32-bit section numbers. The index sections will be
12053 aligned at 8-byte boundaries in the file.
12054
d2415c6c
DE
12055 The index section header consists of:
12056
12057 V, 32 bit version number
12058 -, 32 bits unused
12059 N, 32 bit number of compilation units or type units in the index
12060 M, 32 bit number of slots in the hash table
80626a55 12061
d2415c6c 12062 Numbers are recorded using the byte order of the application binary.
80626a55 12063
d2415c6c
DE
12064 The hash table begins at offset 16 in the section, and consists of an array
12065 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12066 order of the application binary). Unused slots in the hash table are 0.
12067 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12068
d2415c6c
DE
12069 The parallel table begins immediately after the hash table
12070 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12071 array of 32-bit indexes (using the byte order of the application binary),
12072 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12073 table contains a 32-bit index into the pool of section numbers. For unused
12074 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12075
73869dc2
DE
12076 The pool of section numbers begins immediately following the hash table
12077 (at offset 16 + 12 * M from the beginning of the section). The pool of
12078 section numbers consists of an array of 32-bit words (using the byte order
12079 of the application binary). Each item in the array is indexed starting
12080 from 0. The hash table entry provides the index of the first section
12081 number in the set. Additional section numbers in the set follow, and the
12082 set is terminated by a 0 entry (section number 0 is not used in ELF).
12083
12084 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12085 section must be the first entry in the set, and the .debug_abbrev.dwo must
12086 be the second entry. Other members of the set may follow in any order.
12087
12088 ---
12089
12090 DWP Version 2:
12091
12092 DWP Version 2 combines all the .debug_info, etc. sections into one,
12093 and the entries in the index tables are now offsets into these sections.
12094 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12095 section.
12096
12097 Index Section Contents:
12098 Header
12099 Hash Table of Signatures dwp_hash_table.hash_table
12100 Parallel Table of Indices dwp_hash_table.unit_table
12101 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12102 Table of Section Sizes dwp_hash_table.v2.sizes
12103
12104 The index section header consists of:
12105
12106 V, 32 bit version number
12107 L, 32 bit number of columns in the table of section offsets
12108 N, 32 bit number of compilation units or type units in the index
12109 M, 32 bit number of slots in the hash table
12110
12111 Numbers are recorded using the byte order of the application binary.
12112
12113 The hash table has the same format as version 1.
12114 The parallel table of indices has the same format as version 1,
12115 except that the entries are origin-1 indices into the table of sections
12116 offsets and the table of section sizes.
12117
12118 The table of offsets begins immediately following the parallel table
12119 (at offset 16 + 12 * M from the beginning of the section). The table is
12120 a two-dimensional array of 32-bit words (using the byte order of the
12121 application binary), with L columns and N+1 rows, in row-major order.
12122 Each row in the array is indexed starting from 0. The first row provides
12123 a key to the remaining rows: each column in this row provides an identifier
12124 for a debug section, and the offsets in the same column of subsequent rows
12125 refer to that section. The section identifiers are:
12126
12127 DW_SECT_INFO 1 .debug_info.dwo
12128 DW_SECT_TYPES 2 .debug_types.dwo
12129 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12130 DW_SECT_LINE 4 .debug_line.dwo
12131 DW_SECT_LOC 5 .debug_loc.dwo
12132 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12133 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12134 DW_SECT_MACRO 8 .debug_macro.dwo
12135
12136 The offsets provided by the CU and TU index sections are the base offsets
12137 for the contributions made by each CU or TU to the corresponding section
12138 in the package file. Each CU and TU header contains an abbrev_offset
12139 field, used to find the abbreviations table for that CU or TU within the
12140 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12141 be interpreted as relative to the base offset given in the index section.
12142 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12143 should be interpreted as relative to the base offset for .debug_line.dwo,
12144 and offsets into other debug sections obtained from DWARF attributes should
12145 also be interpreted as relative to the corresponding base offset.
12146
12147 The table of sizes begins immediately following the table of offsets.
12148 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12149 with L columns and N rows, in row-major order. Each row in the array is
12150 indexed starting from 1 (row 0 is shared by the two tables).
12151
12152 ---
12153
12154 Hash table lookup is handled the same in version 1 and 2:
12155
12156 We assume that N and M will not exceed 2^32 - 1.
12157 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12158
d2415c6c
DE
12159 Given a 64-bit compilation unit signature or a type signature S, an entry
12160 in the hash table is located as follows:
80626a55 12161
d2415c6c
DE
12162 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12163 the low-order k bits all set to 1.
80626a55 12164
d2415c6c 12165 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12166
d2415c6c
DE
12167 3) If the hash table entry at index H matches the signature, use that
12168 entry. If the hash table entry at index H is unused (all zeroes),
12169 terminate the search: the signature is not present in the table.
80626a55 12170
d2415c6c 12171 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12172
d2415c6c 12173 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12174 to stop at an unused slot or find the match. */
80626a55
DE
12175
12176/* Create a hash table to map DWO IDs to their CU/TU entry in
12177 .debug_{info,types}.dwo in DWP_FILE.
12178 Returns NULL if there isn't one.
12179 Note: This function processes DWP files only, not DWO files. */
12180
12181static struct dwp_hash_table *
ed2dc618
SM
12182create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12183 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12184{
12185 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12186 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12187 const gdb_byte *index_ptr, *index_end;
80626a55 12188 struct dwarf2_section_info *index;
73869dc2 12189 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12190 struct dwp_hash_table *htab;
12191
12192 if (is_debug_types)
12193 index = &dwp_file->sections.tu_index;
12194 else
12195 index = &dwp_file->sections.cu_index;
12196
12197 if (dwarf2_section_empty_p (index))
12198 return NULL;
12199 dwarf2_read_section (objfile, index);
12200
12201 index_ptr = index->buffer;
12202 index_end = index_ptr + index->size;
12203
12204 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12205 index_ptr += 4;
12206 if (version == 2)
12207 nr_columns = read_4_bytes (dbfd, index_ptr);
12208 else
12209 nr_columns = 0;
12210 index_ptr += 4;
80626a55
DE
12211 nr_units = read_4_bytes (dbfd, index_ptr);
12212 index_ptr += 4;
12213 nr_slots = read_4_bytes (dbfd, index_ptr);
12214 index_ptr += 4;
12215
73869dc2 12216 if (version != 1 && version != 2)
80626a55 12217 {
21aa081e 12218 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12219 " [in module %s]"),
21aa081e 12220 pulongest (version), dwp_file->name);
80626a55
DE
12221 }
12222 if (nr_slots != (nr_slots & -nr_slots))
12223 {
21aa081e 12224 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12225 " is not power of 2 [in module %s]"),
21aa081e 12226 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12227 }
12228
12229 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12230 htab->version = version;
12231 htab->nr_columns = nr_columns;
80626a55
DE
12232 htab->nr_units = nr_units;
12233 htab->nr_slots = nr_slots;
12234 htab->hash_table = index_ptr;
12235 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12236
12237 /* Exit early if the table is empty. */
12238 if (nr_slots == 0 || nr_units == 0
12239 || (version == 2 && nr_columns == 0))
12240 {
12241 /* All must be zero. */
12242 if (nr_slots != 0 || nr_units != 0
12243 || (version == 2 && nr_columns != 0))
12244 {
b98664d3 12245 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12246 " all zero [in modules %s]"),
12247 dwp_file->name);
12248 }
12249 return htab;
12250 }
12251
12252 if (version == 1)
12253 {
12254 htab->section_pool.v1.indices =
12255 htab->unit_table + sizeof (uint32_t) * nr_slots;
12256 /* It's harder to decide whether the section is too small in v1.
12257 V1 is deprecated anyway so we punt. */
12258 }
12259 else
12260 {
12261 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12262 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12263 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12264 /* Reverse map for error checking. */
12265 int ids_seen[DW_SECT_MAX + 1];
12266 int i;
12267
12268 if (nr_columns < 2)
12269 {
12270 error (_("Dwarf Error: bad DWP hash table, too few columns"
12271 " in section table [in module %s]"),
12272 dwp_file->name);
12273 }
12274 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12275 {
12276 error (_("Dwarf Error: bad DWP hash table, too many columns"
12277 " in section table [in module %s]"),
12278 dwp_file->name);
12279 }
04fd5eed
GB
12280 memset (ids, 255, sizeof_ids);
12281 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12282 for (i = 0; i < nr_columns; ++i)
12283 {
12284 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12285
12286 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12287 {
12288 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12289 " in section table [in module %s]"),
12290 id, dwp_file->name);
12291 }
12292 if (ids_seen[id] != -1)
12293 {
12294 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12295 " id %d in section table [in module %s]"),
12296 id, dwp_file->name);
12297 }
12298 ids_seen[id] = i;
12299 ids[i] = id;
12300 }
12301 /* Must have exactly one info or types section. */
12302 if (((ids_seen[DW_SECT_INFO] != -1)
12303 + (ids_seen[DW_SECT_TYPES] != -1))
12304 != 1)
12305 {
12306 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12307 " DWO info/types section [in module %s]"),
12308 dwp_file->name);
12309 }
12310 /* Must have an abbrev section. */
12311 if (ids_seen[DW_SECT_ABBREV] == -1)
12312 {
12313 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12314 " section [in module %s]"),
12315 dwp_file->name);
12316 }
12317 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12318 htab->section_pool.v2.sizes =
12319 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12320 * nr_units * nr_columns);
12321 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12322 * nr_units * nr_columns))
12323 > index_end)
12324 {
12325 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12326 " [in module %s]"),
12327 dwp_file->name);
12328 }
12329 }
80626a55
DE
12330
12331 return htab;
12332}
12333
12334/* Update SECTIONS with the data from SECTP.
12335
12336 This function is like the other "locate" section routines that are
12337 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12338 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12339
12340 The result is non-zero for success, or zero if an error was found. */
12341
12342static int
73869dc2
DE
12343locate_v1_virtual_dwo_sections (asection *sectp,
12344 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12345{
12346 const struct dwop_section_names *names = &dwop_section_names;
12347
12348 if (section_is_p (sectp->name, &names->abbrev_dwo))
12349 {
12350 /* There can be only one. */
049412e3 12351 if (sections->abbrev.s.section != NULL)
80626a55 12352 return 0;
049412e3 12353 sections->abbrev.s.section = sectp;
fd361982 12354 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12355 }
12356 else if (section_is_p (sectp->name, &names->info_dwo)
12357 || section_is_p (sectp->name, &names->types_dwo))
12358 {
12359 /* There can be only one. */
049412e3 12360 if (sections->info_or_types.s.section != NULL)
80626a55 12361 return 0;
049412e3 12362 sections->info_or_types.s.section = sectp;
fd361982 12363 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12364 }
12365 else if (section_is_p (sectp->name, &names->line_dwo))
12366 {
12367 /* There can be only one. */
049412e3 12368 if (sections->line.s.section != NULL)
80626a55 12369 return 0;
049412e3 12370 sections->line.s.section = sectp;
fd361982 12371 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12372 }
12373 else if (section_is_p (sectp->name, &names->loc_dwo))
12374 {
12375 /* There can be only one. */
049412e3 12376 if (sections->loc.s.section != NULL)
80626a55 12377 return 0;
049412e3 12378 sections->loc.s.section = sectp;
fd361982 12379 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12380 }
12381 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12382 {
12383 /* There can be only one. */
049412e3 12384 if (sections->macinfo.s.section != NULL)
80626a55 12385 return 0;
049412e3 12386 sections->macinfo.s.section = sectp;
fd361982 12387 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12388 }
12389 else if (section_is_p (sectp->name, &names->macro_dwo))
12390 {
12391 /* There can be only one. */
049412e3 12392 if (sections->macro.s.section != NULL)
80626a55 12393 return 0;
049412e3 12394 sections->macro.s.section = sectp;
fd361982 12395 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12396 }
12397 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12398 {
12399 /* There can be only one. */
049412e3 12400 if (sections->str_offsets.s.section != NULL)
80626a55 12401 return 0;
049412e3 12402 sections->str_offsets.s.section = sectp;
fd361982 12403 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12404 }
12405 else
12406 {
12407 /* No other kind of section is valid. */
12408 return 0;
12409 }
12410
12411 return 1;
12412}
12413
73869dc2
DE
12414/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12415 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12416 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12417 This is for DWP version 1 files. */
80626a55
DE
12418
12419static struct dwo_unit *
ed2dc618
SM
12420create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12421 struct dwp_file *dwp_file,
73869dc2
DE
12422 uint32_t unit_index,
12423 const char *comp_dir,
12424 ULONGEST signature, int is_debug_types)
80626a55
DE
12425{
12426 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12427 const struct dwp_hash_table *dwp_htab =
12428 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12429 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12430 const char *kind = is_debug_types ? "TU" : "CU";
12431 struct dwo_file *dwo_file;
12432 struct dwo_unit *dwo_unit;
73869dc2 12433 struct virtual_v1_dwo_sections sections;
80626a55 12434 void **dwo_file_slot;
80626a55
DE
12435 int i;
12436
73869dc2
DE
12437 gdb_assert (dwp_file->version == 1);
12438
b4f54984 12439 if (dwarf_read_debug)
80626a55 12440 {
73869dc2 12441 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12442 kind,
73869dc2 12443 pulongest (unit_index), hex_string (signature),
80626a55
DE
12444 dwp_file->name);
12445 }
12446
19ac8c2e 12447 /* Fetch the sections of this DWO unit.
80626a55
DE
12448 Put a limit on the number of sections we look for so that bad data
12449 doesn't cause us to loop forever. */
12450
73869dc2 12451#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12452 (1 /* .debug_info or .debug_types */ \
12453 + 1 /* .debug_abbrev */ \
12454 + 1 /* .debug_line */ \
12455 + 1 /* .debug_loc */ \
12456 + 1 /* .debug_str_offsets */ \
19ac8c2e 12457 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12458 + 1 /* trailing zero */)
12459
12460 memset (&sections, 0, sizeof (sections));
80626a55 12461
73869dc2 12462 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12463 {
12464 asection *sectp;
12465 uint32_t section_nr =
12466 read_4_bytes (dbfd,
73869dc2
DE
12467 dwp_htab->section_pool.v1.indices
12468 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12469
12470 if (section_nr == 0)
12471 break;
12472 if (section_nr >= dwp_file->num_sections)
12473 {
12474 error (_("Dwarf Error: bad DWP hash table, section number too large"
12475 " [in module %s]"),
12476 dwp_file->name);
12477 }
12478
12479 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12480 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12481 {
12482 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12483 " [in module %s]"),
12484 dwp_file->name);
12485 }
12486 }
12487
12488 if (i < 2
a32a8923
DE
12489 || dwarf2_section_empty_p (&sections.info_or_types)
12490 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12491 {
12492 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12493 " [in module %s]"),
12494 dwp_file->name);
12495 }
73869dc2 12496 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12497 {
12498 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12499 " [in module %s]"),
12500 dwp_file->name);
12501 }
12502
12503 /* It's easier for the rest of the code if we fake a struct dwo_file and
12504 have dwo_unit "live" in that. At least for now.
12505
12506 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12507 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12508 file, we can combine them back into a virtual DWO file to save space
12509 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12510 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12511
791afaa2
TT
12512 std::string virtual_dwo_name =
12513 string_printf ("virtual-dwo/%d-%d-%d-%d",
12514 get_section_id (&sections.abbrev),
12515 get_section_id (&sections.line),
12516 get_section_id (&sections.loc),
12517 get_section_id (&sections.str_offsets));
80626a55 12518 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12519 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12520 virtual_dwo_name.c_str (),
12521 comp_dir);
80626a55
DE
12522 /* Create one if necessary. */
12523 if (*dwo_file_slot == NULL)
12524 {
b4f54984 12525 if (dwarf_read_debug)
80626a55
DE
12526 {
12527 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12528 virtual_dwo_name.c_str ());
80626a55 12529 }
51ac9db5 12530 dwo_file = new struct dwo_file;
efba19b0
TT
12531 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12532 virtual_dwo_name);
0ac5b59e 12533 dwo_file->comp_dir = comp_dir;
80626a55
DE
12534 dwo_file->sections.abbrev = sections.abbrev;
12535 dwo_file->sections.line = sections.line;
12536 dwo_file->sections.loc = sections.loc;
12537 dwo_file->sections.macinfo = sections.macinfo;
12538 dwo_file->sections.macro = sections.macro;
12539 dwo_file->sections.str_offsets = sections.str_offsets;
12540 /* The "str" section is global to the entire DWP file. */
12541 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12542 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12543 there's no need to record it in dwo_file.
12544 Also, we can't simply record type sections in dwo_file because
12545 we record a pointer into the vector in dwo_unit. As we collect more
12546 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12547 for it, invalidating all copies of pointers into the previous
12548 contents. */
80626a55
DE
12549 *dwo_file_slot = dwo_file;
12550 }
12551 else
12552 {
b4f54984 12553 if (dwarf_read_debug)
80626a55
DE
12554 {
12555 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12556 virtual_dwo_name.c_str ());
80626a55 12557 }
9a3c8263 12558 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12559 }
80626a55
DE
12560
12561 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12562 dwo_unit->dwo_file = dwo_file;
12563 dwo_unit->signature = signature;
8d749320
SM
12564 dwo_unit->section =
12565 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12566 *dwo_unit->section = sections.info_or_types;
57d63ce2 12567 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12568
12569 return dwo_unit;
12570}
12571
73869dc2
DE
12572/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12573 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12574 piece within that section used by a TU/CU, return a virtual section
12575 of just that piece. */
12576
12577static struct dwarf2_section_info
ed2dc618
SM
12578create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12579 struct dwarf2_section_info *section,
73869dc2
DE
12580 bfd_size_type offset, bfd_size_type size)
12581{
12582 struct dwarf2_section_info result;
12583 asection *sectp;
12584
12585 gdb_assert (section != NULL);
12586 gdb_assert (!section->is_virtual);
12587
12588 memset (&result, 0, sizeof (result));
12589 result.s.containing_section = section;
dc4ccb6f 12590 result.is_virtual = true;
73869dc2
DE
12591
12592 if (size == 0)
12593 return result;
12594
12595 sectp = get_section_bfd_section (section);
12596
12597 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12598 bounds of the real section. This is a pretty-rare event, so just
12599 flag an error (easier) instead of a warning and trying to cope. */
12600 if (sectp == NULL
fd361982 12601 || offset + size > bfd_section_size (sectp))
73869dc2 12602 {
73869dc2
DE
12603 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12604 " in section %s [in module %s]"),
fd361982 12605 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12606 objfile_name (dwarf2_per_objfile->objfile));
12607 }
12608
12609 result.virtual_offset = offset;
12610 result.size = size;
12611 return result;
12612}
12613
12614/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12615 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12616 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12617 This is for DWP version 2 files. */
12618
12619static struct dwo_unit *
ed2dc618
SM
12620create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12621 struct dwp_file *dwp_file,
73869dc2
DE
12622 uint32_t unit_index,
12623 const char *comp_dir,
12624 ULONGEST signature, int is_debug_types)
12625{
12626 struct objfile *objfile = dwarf2_per_objfile->objfile;
12627 const struct dwp_hash_table *dwp_htab =
12628 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12629 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12630 const char *kind = is_debug_types ? "TU" : "CU";
12631 struct dwo_file *dwo_file;
12632 struct dwo_unit *dwo_unit;
12633 struct virtual_v2_dwo_sections sections;
12634 void **dwo_file_slot;
73869dc2
DE
12635 int i;
12636
12637 gdb_assert (dwp_file->version == 2);
12638
b4f54984 12639 if (dwarf_read_debug)
73869dc2
DE
12640 {
12641 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12642 kind,
12643 pulongest (unit_index), hex_string (signature),
12644 dwp_file->name);
12645 }
12646
12647 /* Fetch the section offsets of this DWO unit. */
12648
12649 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12650
12651 for (i = 0; i < dwp_htab->nr_columns; ++i)
12652 {
12653 uint32_t offset = read_4_bytes (dbfd,
12654 dwp_htab->section_pool.v2.offsets
12655 + (((unit_index - 1) * dwp_htab->nr_columns
12656 + i)
12657 * sizeof (uint32_t)));
12658 uint32_t size = read_4_bytes (dbfd,
12659 dwp_htab->section_pool.v2.sizes
12660 + (((unit_index - 1) * dwp_htab->nr_columns
12661 + i)
12662 * sizeof (uint32_t)));
12663
12664 switch (dwp_htab->section_pool.v2.section_ids[i])
12665 {
12666 case DW_SECT_INFO:
12667 case DW_SECT_TYPES:
12668 sections.info_or_types_offset = offset;
12669 sections.info_or_types_size = size;
12670 break;
12671 case DW_SECT_ABBREV:
12672 sections.abbrev_offset = offset;
12673 sections.abbrev_size = size;
12674 break;
12675 case DW_SECT_LINE:
12676 sections.line_offset = offset;
12677 sections.line_size = size;
12678 break;
12679 case DW_SECT_LOC:
12680 sections.loc_offset = offset;
12681 sections.loc_size = size;
12682 break;
12683 case DW_SECT_STR_OFFSETS:
12684 sections.str_offsets_offset = offset;
12685 sections.str_offsets_size = size;
12686 break;
12687 case DW_SECT_MACINFO:
12688 sections.macinfo_offset = offset;
12689 sections.macinfo_size = size;
12690 break;
12691 case DW_SECT_MACRO:
12692 sections.macro_offset = offset;
12693 sections.macro_size = size;
12694 break;
12695 }
12696 }
12697
12698 /* It's easier for the rest of the code if we fake a struct dwo_file and
12699 have dwo_unit "live" in that. At least for now.
12700
12701 The DWP file can be made up of a random collection of CUs and TUs.
12702 However, for each CU + set of TUs that came from the same original DWO
12703 file, we can combine them back into a virtual DWO file to save space
12704 (fewer struct dwo_file objects to allocate). Remember that for really
12705 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12706
791afaa2
TT
12707 std::string virtual_dwo_name =
12708 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12709 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12710 (long) (sections.line_size ? sections.line_offset : 0),
12711 (long) (sections.loc_size ? sections.loc_offset : 0),
12712 (long) (sections.str_offsets_size
12713 ? sections.str_offsets_offset : 0));
73869dc2 12714 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12715 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12716 virtual_dwo_name.c_str (),
12717 comp_dir);
73869dc2
DE
12718 /* Create one if necessary. */
12719 if (*dwo_file_slot == NULL)
12720 {
b4f54984 12721 if (dwarf_read_debug)
73869dc2
DE
12722 {
12723 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12724 virtual_dwo_name.c_str ());
73869dc2 12725 }
51ac9db5 12726 dwo_file = new struct dwo_file;
efba19b0
TT
12727 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12728 virtual_dwo_name);
73869dc2
DE
12729 dwo_file->comp_dir = comp_dir;
12730 dwo_file->sections.abbrev =
ed2dc618 12731 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12732 sections.abbrev_offset, sections.abbrev_size);
12733 dwo_file->sections.line =
ed2dc618 12734 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12735 sections.line_offset, sections.line_size);
12736 dwo_file->sections.loc =
ed2dc618 12737 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12738 sections.loc_offset, sections.loc_size);
12739 dwo_file->sections.macinfo =
ed2dc618 12740 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12741 sections.macinfo_offset, sections.macinfo_size);
12742 dwo_file->sections.macro =
ed2dc618 12743 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12744 sections.macro_offset, sections.macro_size);
12745 dwo_file->sections.str_offsets =
ed2dc618
SM
12746 create_dwp_v2_section (dwarf2_per_objfile,
12747 &dwp_file->sections.str_offsets,
73869dc2
DE
12748 sections.str_offsets_offset,
12749 sections.str_offsets_size);
12750 /* The "str" section is global to the entire DWP file. */
12751 dwo_file->sections.str = dwp_file->sections.str;
12752 /* The info or types section is assigned below to dwo_unit,
12753 there's no need to record it in dwo_file.
12754 Also, we can't simply record type sections in dwo_file because
12755 we record a pointer into the vector in dwo_unit. As we collect more
12756 types we'll grow the vector and eventually have to reallocate space
12757 for it, invalidating all copies of pointers into the previous
12758 contents. */
12759 *dwo_file_slot = dwo_file;
12760 }
12761 else
12762 {
b4f54984 12763 if (dwarf_read_debug)
73869dc2
DE
12764 {
12765 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12766 virtual_dwo_name.c_str ());
73869dc2 12767 }
9a3c8263 12768 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12769 }
73869dc2
DE
12770
12771 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12772 dwo_unit->dwo_file = dwo_file;
12773 dwo_unit->signature = signature;
8d749320
SM
12774 dwo_unit->section =
12775 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12776 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12777 is_debug_types
73869dc2
DE
12778 ? &dwp_file->sections.types
12779 : &dwp_file->sections.info,
12780 sections.info_or_types_offset,
12781 sections.info_or_types_size);
12782 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12783
12784 return dwo_unit;
12785}
12786
57d63ce2
DE
12787/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12788 Returns NULL if the signature isn't found. */
80626a55
DE
12789
12790static struct dwo_unit *
ed2dc618
SM
12791lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12792 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12793 ULONGEST signature, int is_debug_types)
80626a55 12794{
57d63ce2
DE
12795 const struct dwp_hash_table *dwp_htab =
12796 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12797 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12798 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12799 uint32_t hash = signature & mask;
12800 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12801 unsigned int i;
12802 void **slot;
870f88f7 12803 struct dwo_unit find_dwo_cu;
80626a55
DE
12804
12805 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12806 find_dwo_cu.signature = signature;
19ac8c2e
DE
12807 slot = htab_find_slot (is_debug_types
12808 ? dwp_file->loaded_tus
12809 : dwp_file->loaded_cus,
12810 &find_dwo_cu, INSERT);
80626a55
DE
12811
12812 if (*slot != NULL)
9a3c8263 12813 return (struct dwo_unit *) *slot;
80626a55
DE
12814
12815 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12816 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12817 {
12818 ULONGEST signature_in_table;
12819
12820 signature_in_table =
57d63ce2 12821 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12822 if (signature_in_table == signature)
12823 {
57d63ce2
DE
12824 uint32_t unit_index =
12825 read_4_bytes (dbfd,
12826 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12827
73869dc2
DE
12828 if (dwp_file->version == 1)
12829 {
ed2dc618
SM
12830 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12831 dwp_file, unit_index,
73869dc2
DE
12832 comp_dir, signature,
12833 is_debug_types);
12834 }
12835 else
12836 {
ed2dc618
SM
12837 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12838 dwp_file, unit_index,
73869dc2
DE
12839 comp_dir, signature,
12840 is_debug_types);
12841 }
9a3c8263 12842 return (struct dwo_unit *) *slot;
80626a55
DE
12843 }
12844 if (signature_in_table == 0)
12845 return NULL;
12846 hash = (hash + hash2) & mask;
12847 }
12848
12849 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12850 " [in module %s]"),
12851 dwp_file->name);
12852}
12853
ab5088bf 12854/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12855 Open the file specified by FILE_NAME and hand it off to BFD for
12856 preliminary analysis. Return a newly initialized bfd *, which
12857 includes a canonicalized copy of FILE_NAME.
80626a55 12858 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12859 SEARCH_CWD is true if the current directory is to be searched.
12860 It will be searched before debug-file-directory.
13aaf454
DE
12861 If successful, the file is added to the bfd include table of the
12862 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12863 If unable to find/open the file, return NULL.
3019eac3
DE
12864 NOTE: This function is derived from symfile_bfd_open. */
12865
192b62ce 12866static gdb_bfd_ref_ptr
ed2dc618
SM
12867try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12868 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12869{
24b9144d 12870 int desc;
9c02c129
DE
12871 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12872 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12873 to debug_file_directory. */
e0cc99a6 12874 const char *search_path;
9c02c129
DE
12875 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12876
e0cc99a6 12877 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12878 if (search_cwd)
12879 {
12880 if (*debug_file_directory != '\0')
e0cc99a6
TT
12881 {
12882 search_path_holder.reset (concat (".", dirname_separator_string,
12883 debug_file_directory,
12884 (char *) NULL));
12885 search_path = search_path_holder.get ();
12886 }
6ac97d4c 12887 else
e0cc99a6 12888 search_path = ".";
6ac97d4c 12889 }
9c02c129 12890 else
e0cc99a6 12891 search_path = debug_file_directory;
3019eac3 12892
24b9144d 12893 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12894 if (is_dwp)
12895 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12896
12897 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12898 desc = openp (search_path, flags, file_name,
3019eac3
DE
12899 O_RDONLY | O_BINARY, &absolute_name);
12900 if (desc < 0)
12901 return NULL;
12902
e0cc99a6
TT
12903 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12904 gnutarget, desc));
9c02c129
DE
12905 if (sym_bfd == NULL)
12906 return NULL;
192b62ce 12907 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12908
192b62ce
TT
12909 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12910 return NULL;
3019eac3 12911
13aaf454
DE
12912 /* Success. Record the bfd as having been included by the objfile's bfd.
12913 This is important because things like demangled_names_hash lives in the
12914 objfile's per_bfd space and may have references to things like symbol
12915 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12916 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12917
3019eac3
DE
12918 return sym_bfd;
12919}
12920
ab5088bf 12921/* Try to open DWO file FILE_NAME.
3019eac3
DE
12922 COMP_DIR is the DW_AT_comp_dir attribute.
12923 The result is the bfd handle of the file.
12924 If there is a problem finding or opening the file, return NULL.
12925 Upon success, the canonicalized path of the file is stored in the bfd,
12926 same as symfile_bfd_open. */
12927
192b62ce 12928static gdb_bfd_ref_ptr
ed2dc618
SM
12929open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12930 const char *file_name, const char *comp_dir)
3019eac3 12931{
80626a55 12932 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12933 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12934 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12935
12936 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12937
12938 if (comp_dir != NULL)
12939 {
43816ebc
TT
12940 gdb::unique_xmalloc_ptr<char> path_to_try
12941 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12942
12943 /* NOTE: If comp_dir is a relative path, this will also try the
12944 search path, which seems useful. */
ed2dc618 12945 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12946 path_to_try.get (),
ed2dc618 12947 0 /*is_dwp*/,
192b62ce 12948 1 /*search_cwd*/));
3019eac3
DE
12949 if (abfd != NULL)
12950 return abfd;
12951 }
12952
12953 /* That didn't work, try debug-file-directory, which, despite its name,
12954 is a list of paths. */
12955
12956 if (*debug_file_directory == '\0')
12957 return NULL;
12958
ed2dc618
SM
12959 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12960 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12961}
12962
80626a55
DE
12963/* This function is mapped across the sections and remembers the offset and
12964 size of each of the DWO debugging sections we are interested in. */
12965
12966static void
12967dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12968{
9a3c8263 12969 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12970 const struct dwop_section_names *names = &dwop_section_names;
12971
12972 if (section_is_p (sectp->name, &names->abbrev_dwo))
12973 {
049412e3 12974 dwo_sections->abbrev.s.section = sectp;
fd361982 12975 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12976 }
12977 else if (section_is_p (sectp->name, &names->info_dwo))
12978 {
049412e3 12979 dwo_sections->info.s.section = sectp;
fd361982 12980 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12981 }
12982 else if (section_is_p (sectp->name, &names->line_dwo))
12983 {
049412e3 12984 dwo_sections->line.s.section = sectp;
fd361982 12985 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12986 }
12987 else if (section_is_p (sectp->name, &names->loc_dwo))
12988 {
049412e3 12989 dwo_sections->loc.s.section = sectp;
fd361982 12990 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12991 }
12992 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12993 {
049412e3 12994 dwo_sections->macinfo.s.section = sectp;
fd361982 12995 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12996 }
12997 else if (section_is_p (sectp->name, &names->macro_dwo))
12998 {
049412e3 12999 dwo_sections->macro.s.section = sectp;
fd361982 13000 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13001 }
13002 else if (section_is_p (sectp->name, &names->str_dwo))
13003 {
049412e3 13004 dwo_sections->str.s.section = sectp;
fd361982 13005 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13006 }
13007 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13008 {
049412e3 13009 dwo_sections->str_offsets.s.section = sectp;
fd361982 13010 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13011 }
13012 else if (section_is_p (sectp->name, &names->types_dwo))
13013 {
13014 struct dwarf2_section_info type_section;
13015
13016 memset (&type_section, 0, sizeof (type_section));
049412e3 13017 type_section.s.section = sectp;
fd361982 13018 type_section.size = bfd_section_size (sectp);
fd5866f6 13019 dwo_sections->types.push_back (type_section);
80626a55
DE
13020 }
13021}
13022
ab5088bf 13023/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13024 by PER_CU. This is for the non-DWP case.
80626a55 13025 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13026
13027static struct dwo_file *
0ac5b59e
DE
13028open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13029 const char *dwo_name, const char *comp_dir)
3019eac3 13030{
ed2dc618 13031 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13032
fb1eb2f9 13033 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13034 if (dbfd == NULL)
13035 {
b4f54984 13036 if (dwarf_read_debug)
80626a55
DE
13037 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13038 return NULL;
13039 }
263db9a1 13040
51ac9db5 13041 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13042 dwo_file->dwo_name = dwo_name;
13043 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13044 dwo_file->dbfd = std::move (dbfd);
3019eac3 13045
fb1eb2f9 13046 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13047 &dwo_file->sections);
3019eac3 13048
ed2dc618
SM
13049 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13050 dwo_file->cus);
3019eac3 13051
263db9a1 13052 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13053 dwo_file->sections.types, dwo_file->tus);
3019eac3 13054
b4f54984 13055 if (dwarf_read_debug)
80626a55
DE
13056 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13057
263db9a1 13058 return dwo_file.release ();
3019eac3
DE
13059}
13060
80626a55 13061/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13062 size of each of the DWP debugging sections common to version 1 and 2 that
13063 we are interested in. */
3019eac3 13064
80626a55 13065static void
73869dc2
DE
13066dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13067 void *dwp_file_ptr)
3019eac3 13068{
9a3c8263 13069 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13070 const struct dwop_section_names *names = &dwop_section_names;
13071 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13072
80626a55 13073 /* Record the ELF section number for later lookup: this is what the
73869dc2 13074 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13075 gdb_assert (elf_section_nr < dwp_file->num_sections);
13076 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13077
80626a55
DE
13078 /* Look for specific sections that we need. */
13079 if (section_is_p (sectp->name, &names->str_dwo))
13080 {
049412e3 13081 dwp_file->sections.str.s.section = sectp;
fd361982 13082 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13083 }
13084 else if (section_is_p (sectp->name, &names->cu_index))
13085 {
049412e3 13086 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13087 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13088 }
13089 else if (section_is_p (sectp->name, &names->tu_index))
13090 {
049412e3 13091 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13092 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13093 }
13094}
3019eac3 13095
73869dc2
DE
13096/* This function is mapped across the sections and remembers the offset and
13097 size of each of the DWP version 2 debugging sections that we are interested
13098 in. This is split into a separate function because we don't know if we
13099 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13100
13101static void
13102dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13103{
9a3c8263 13104 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13105 const struct dwop_section_names *names = &dwop_section_names;
13106 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13107
13108 /* Record the ELF section number for later lookup: this is what the
13109 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13110 gdb_assert (elf_section_nr < dwp_file->num_sections);
13111 dwp_file->elf_sections[elf_section_nr] = sectp;
13112
13113 /* Look for specific sections that we need. */
13114 if (section_is_p (sectp->name, &names->abbrev_dwo))
13115 {
049412e3 13116 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13117 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13118 }
13119 else if (section_is_p (sectp->name, &names->info_dwo))
13120 {
049412e3 13121 dwp_file->sections.info.s.section = sectp;
fd361982 13122 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13123 }
13124 else if (section_is_p (sectp->name, &names->line_dwo))
13125 {
049412e3 13126 dwp_file->sections.line.s.section = sectp;
fd361982 13127 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13128 }
13129 else if (section_is_p (sectp->name, &names->loc_dwo))
13130 {
049412e3 13131 dwp_file->sections.loc.s.section = sectp;
fd361982 13132 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13133 }
13134 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13135 {
049412e3 13136 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13137 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13138 }
13139 else if (section_is_p (sectp->name, &names->macro_dwo))
13140 {
049412e3 13141 dwp_file->sections.macro.s.section = sectp;
fd361982 13142 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13143 }
13144 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13145 {
049412e3 13146 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13147 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13148 }
13149 else if (section_is_p (sectp->name, &names->types_dwo))
13150 {
049412e3 13151 dwp_file->sections.types.s.section = sectp;
fd361982 13152 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13153 }
13154}
13155
80626a55 13156/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13157
80626a55
DE
13158static hashval_t
13159hash_dwp_loaded_cutus (const void *item)
13160{
9a3c8263 13161 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13162
80626a55
DE
13163 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13164 return dwo_unit->signature;
3019eac3
DE
13165}
13166
80626a55 13167/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13168
80626a55
DE
13169static int
13170eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13171{
9a3c8263
SM
13172 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13173 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13174
80626a55
DE
13175 return dua->signature == dub->signature;
13176}
3019eac3 13177
80626a55 13178/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13179
80626a55
DE
13180static htab_t
13181allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13182{
13183 return htab_create_alloc_ex (3,
13184 hash_dwp_loaded_cutus,
13185 eq_dwp_loaded_cutus,
13186 NULL,
13187 &objfile->objfile_obstack,
13188 hashtab_obstack_allocate,
13189 dummy_obstack_deallocate);
13190}
3019eac3 13191
ab5088bf
DE
13192/* Try to open DWP file FILE_NAME.
13193 The result is the bfd handle of the file.
13194 If there is a problem finding or opening the file, return NULL.
13195 Upon success, the canonicalized path of the file is stored in the bfd,
13196 same as symfile_bfd_open. */
13197
192b62ce 13198static gdb_bfd_ref_ptr
ed2dc618
SM
13199open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13200 const char *file_name)
ab5088bf 13201{
ed2dc618
SM
13202 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13203 1 /*is_dwp*/,
192b62ce 13204 1 /*search_cwd*/));
6ac97d4c
DE
13205 if (abfd != NULL)
13206 return abfd;
13207
13208 /* Work around upstream bug 15652.
13209 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13210 [Whether that's a "bug" is debatable, but it is getting in our way.]
13211 We have no real idea where the dwp file is, because gdb's realpath-ing
13212 of the executable's path may have discarded the needed info.
13213 [IWBN if the dwp file name was recorded in the executable, akin to
13214 .gnu_debuglink, but that doesn't exist yet.]
13215 Strip the directory from FILE_NAME and search again. */
13216 if (*debug_file_directory != '\0')
13217 {
13218 /* Don't implicitly search the current directory here.
13219 If the user wants to search "." to handle this case,
13220 it must be added to debug-file-directory. */
ed2dc618
SM
13221 return try_open_dwop_file (dwarf2_per_objfile,
13222 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13223 0 /*search_cwd*/);
13224 }
13225
13226 return NULL;
ab5088bf
DE
13227}
13228
80626a55
DE
13229/* Initialize the use of the DWP file for the current objfile.
13230 By convention the name of the DWP file is ${objfile}.dwp.
13231 The result is NULL if it can't be found. */
a766d390 13232
400174b1 13233static std::unique_ptr<struct dwp_file>
ed2dc618 13234open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13235{
13236 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13237
82bf32bc
JK
13238 /* Try to find first .dwp for the binary file before any symbolic links
13239 resolving. */
6c447423
DE
13240
13241 /* If the objfile is a debug file, find the name of the real binary
13242 file and get the name of dwp file from there. */
d721ba37 13243 std::string dwp_name;
6c447423
DE
13244 if (objfile->separate_debug_objfile_backlink != NULL)
13245 {
13246 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13247 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13248
d721ba37 13249 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13250 }
13251 else
d721ba37
PA
13252 dwp_name = objfile->original_name;
13253
13254 dwp_name += ".dwp";
80626a55 13255
ed2dc618 13256 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13257 if (dbfd == NULL
13258 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13259 {
13260 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13261 dwp_name = objfile_name (objfile);
13262 dwp_name += ".dwp";
ed2dc618 13263 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13264 }
13265
80626a55
DE
13266 if (dbfd == NULL)
13267 {
b4f54984 13268 if (dwarf_read_debug)
d721ba37 13269 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13270 return std::unique_ptr<dwp_file> ();
3019eac3 13271 }
400174b1
TT
13272
13273 const char *name = bfd_get_filename (dbfd.get ());
13274 std::unique_ptr<struct dwp_file> dwp_file
13275 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13276
0a0f4c01 13277 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13278 dwp_file->elf_sections =
13279 OBSTACK_CALLOC (&objfile->objfile_obstack,
13280 dwp_file->num_sections, asection *);
13281
400174b1
TT
13282 bfd_map_over_sections (dwp_file->dbfd.get (),
13283 dwarf2_locate_common_dwp_sections,
13284 dwp_file.get ());
80626a55 13285
400174b1
TT
13286 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13287 0);
80626a55 13288
400174b1
TT
13289 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13290 1);
80626a55 13291
73869dc2 13292 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13293 if (dwp_file->cus && dwp_file->tus
13294 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13295 {
13296 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13297 pretty bizarre. We use pulongest here because that's the established
4d65956b 13298 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13299 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13300 " TU version %s [in DWP file %s]"),
13301 pulongest (dwp_file->cus->version),
d721ba37 13302 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13303 }
08302ed2
DE
13304
13305 if (dwp_file->cus)
13306 dwp_file->version = dwp_file->cus->version;
13307 else if (dwp_file->tus)
13308 dwp_file->version = dwp_file->tus->version;
13309 else
13310 dwp_file->version = 2;
73869dc2
DE
13311
13312 if (dwp_file->version == 2)
400174b1
TT
13313 bfd_map_over_sections (dwp_file->dbfd.get (),
13314 dwarf2_locate_v2_dwp_sections,
13315 dwp_file.get ());
73869dc2 13316
19ac8c2e
DE
13317 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13318 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13319
b4f54984 13320 if (dwarf_read_debug)
80626a55
DE
13321 {
13322 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13323 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13324 " %s CUs, %s TUs\n",
13325 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13326 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13327 }
13328
13329 return dwp_file;
3019eac3 13330}
c906108c 13331
ab5088bf
DE
13332/* Wrapper around open_and_init_dwp_file, only open it once. */
13333
13334static struct dwp_file *
ed2dc618 13335get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13336{
13337 if (! dwarf2_per_objfile->dwp_checked)
13338 {
ed2dc618
SM
13339 dwarf2_per_objfile->dwp_file
13340 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13341 dwarf2_per_objfile->dwp_checked = 1;
13342 }
400174b1 13343 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13344}
13345
80626a55
DE
13346/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13347 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13348 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13349 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13350 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13351
13352 This is called, for example, when wanting to read a variable with a
13353 complex location. Therefore we don't want to do file i/o for every call.
13354 Therefore we don't want to look for a DWO file on every call.
13355 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13356 then we check if we've already seen DWO_NAME, and only THEN do we check
13357 for a DWO file.
13358
1c658ad5 13359 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13360 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13361
3019eac3 13362static struct dwo_unit *
80626a55
DE
13363lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13364 const char *dwo_name, const char *comp_dir,
13365 ULONGEST signature, int is_debug_types)
3019eac3 13366{
ed2dc618 13367 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13368 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13369 const char *kind = is_debug_types ? "TU" : "CU";
13370 void **dwo_file_slot;
3019eac3 13371 struct dwo_file *dwo_file;
80626a55 13372 struct dwp_file *dwp_file;
cb1df416 13373
6a506a2d
DE
13374 /* First see if there's a DWP file.
13375 If we have a DWP file but didn't find the DWO inside it, don't
13376 look for the original DWO file. It makes gdb behave differently
13377 depending on whether one is debugging in the build tree. */
cf2c3c16 13378
ed2dc618 13379 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13380 if (dwp_file != NULL)
cf2c3c16 13381 {
80626a55
DE
13382 const struct dwp_hash_table *dwp_htab =
13383 is_debug_types ? dwp_file->tus : dwp_file->cus;
13384
13385 if (dwp_htab != NULL)
13386 {
13387 struct dwo_unit *dwo_cutu =
ed2dc618 13388 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13389 signature, is_debug_types);
80626a55
DE
13390
13391 if (dwo_cutu != NULL)
13392 {
b4f54984 13393 if (dwarf_read_debug)
80626a55
DE
13394 {
13395 fprintf_unfiltered (gdb_stdlog,
13396 "Virtual DWO %s %s found: @%s\n",
13397 kind, hex_string (signature),
13398 host_address_to_string (dwo_cutu));
13399 }
13400 return dwo_cutu;
13401 }
13402 }
13403 }
6a506a2d 13404 else
80626a55 13405 {
6a506a2d 13406 /* No DWP file, look for the DWO file. */
80626a55 13407
ed2dc618
SM
13408 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13409 dwo_name, comp_dir);
6a506a2d 13410 if (*dwo_file_slot == NULL)
80626a55 13411 {
6a506a2d
DE
13412 /* Read in the file and build a table of the CUs/TUs it contains. */
13413 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13414 }
6a506a2d 13415 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13416 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13417
6a506a2d 13418 if (dwo_file != NULL)
19c3d4c9 13419 {
6a506a2d
DE
13420 struct dwo_unit *dwo_cutu = NULL;
13421
13422 if (is_debug_types && dwo_file->tus)
13423 {
13424 struct dwo_unit find_dwo_cutu;
13425
13426 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13427 find_dwo_cutu.signature = signature;
9a3c8263
SM
13428 dwo_cutu
13429 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13430 }
33c5cd75 13431 else if (!is_debug_types && dwo_file->cus)
80626a55 13432 {
33c5cd75
DB
13433 struct dwo_unit find_dwo_cutu;
13434
13435 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13436 find_dwo_cutu.signature = signature;
13437 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13438 &find_dwo_cutu);
6a506a2d
DE
13439 }
13440
13441 if (dwo_cutu != NULL)
13442 {
b4f54984 13443 if (dwarf_read_debug)
6a506a2d
DE
13444 {
13445 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13446 kind, dwo_name, hex_string (signature),
13447 host_address_to_string (dwo_cutu));
13448 }
13449 return dwo_cutu;
80626a55
DE
13450 }
13451 }
2e276125 13452 }
9cdd5dbd 13453
80626a55
DE
13454 /* We didn't find it. This could mean a dwo_id mismatch, or
13455 someone deleted the DWO/DWP file, or the search path isn't set up
13456 correctly to find the file. */
13457
b4f54984 13458 if (dwarf_read_debug)
80626a55
DE
13459 {
13460 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13461 kind, dwo_name, hex_string (signature));
13462 }
3019eac3 13463
6656a72d
DE
13464 /* This is a warning and not a complaint because it can be caused by
13465 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13466 {
13467 /* Print the name of the DWP file if we looked there, helps the user
13468 better diagnose the problem. */
791afaa2 13469 std::string dwp_text;
43942612
DE
13470
13471 if (dwp_file != NULL)
791afaa2
TT
13472 dwp_text = string_printf (" [in DWP file %s]",
13473 lbasename (dwp_file->name));
43942612 13474
9d8780f0 13475 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13476 " [in module %s]"),
13477 kind, dwo_name, hex_string (signature),
791afaa2 13478 dwp_text.c_str (),
43942612 13479 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13480 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13481 }
3019eac3 13482 return NULL;
5fb290d7
DJ
13483}
13484
80626a55
DE
13485/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13486 See lookup_dwo_cutu_unit for details. */
13487
13488static struct dwo_unit *
13489lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13490 const char *dwo_name, const char *comp_dir,
13491 ULONGEST signature)
13492{
13493 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13494}
13495
13496/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13497 See lookup_dwo_cutu_unit for details. */
13498
13499static struct dwo_unit *
13500lookup_dwo_type_unit (struct signatured_type *this_tu,
13501 const char *dwo_name, const char *comp_dir)
13502{
13503 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13504}
13505
89e63ee4
DE
13506/* Traversal function for queue_and_load_all_dwo_tus. */
13507
13508static int
13509queue_and_load_dwo_tu (void **slot, void *info)
13510{
13511 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13512 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13513 ULONGEST signature = dwo_unit->signature;
13514 struct signatured_type *sig_type =
13515 lookup_dwo_signatured_type (per_cu->cu, signature);
13516
13517 if (sig_type != NULL)
13518 {
13519 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13520
13521 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13522 a real dependency of PER_CU on SIG_TYPE. That is detected later
13523 while processing PER_CU. */
13524 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13525 load_full_type_unit (sig_cu);
ae640021 13526 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13527 }
13528
13529 return 1;
13530}
13531
13532/* Queue all TUs contained in the DWO of PER_CU to be read in.
13533 The DWO may have the only definition of the type, though it may not be
13534 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13535 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13536
13537static void
13538queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13539{
13540 struct dwo_unit *dwo_unit;
13541 struct dwo_file *dwo_file;
13542
13543 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13544 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13545 gdb_assert (per_cu->cu != NULL);
13546
13547 dwo_unit = per_cu->cu->dwo_unit;
13548 gdb_assert (dwo_unit != NULL);
13549
13550 dwo_file = dwo_unit->dwo_file;
13551 if (dwo_file->tus != NULL)
13552 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13553}
13554
3019eac3 13555/* Read in various DIEs. */
348e048f 13556
d389af10 13557/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13558 Inherit only the children of the DW_AT_abstract_origin DIE not being
13559 already referenced by DW_AT_abstract_origin from the children of the
13560 current DIE. */
d389af10
JK
13561
13562static void
13563inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13564{
13565 struct die_info *child_die;
791afaa2 13566 sect_offset *offsetp;
d389af10
JK
13567 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13568 struct die_info *origin_die;
13569 /* Iterator of the ORIGIN_DIE children. */
13570 struct die_info *origin_child_die;
d389af10 13571 struct attribute *attr;
cd02d79d
PA
13572 struct dwarf2_cu *origin_cu;
13573 struct pending **origin_previous_list_in_scope;
d389af10
JK
13574
13575 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13576 if (!attr)
13577 return;
13578
cd02d79d
PA
13579 /* Note that following die references may follow to a die in a
13580 different cu. */
13581
13582 origin_cu = cu;
13583 origin_die = follow_die_ref (die, attr, &origin_cu);
13584
13585 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13586 symbols in. */
13587 origin_previous_list_in_scope = origin_cu->list_in_scope;
13588 origin_cu->list_in_scope = cu->list_in_scope;
13589
edb3359d
DJ
13590 if (die->tag != origin_die->tag
13591 && !(die->tag == DW_TAG_inlined_subroutine
13592 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13593 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13594 sect_offset_str (die->sect_off),
13595 sect_offset_str (origin_die->sect_off));
d389af10 13596
791afaa2 13597 std::vector<sect_offset> offsets;
d389af10 13598
3ea89b92
PMR
13599 for (child_die = die->child;
13600 child_die && child_die->tag;
13601 child_die = sibling_die (child_die))
13602 {
13603 struct die_info *child_origin_die;
13604 struct dwarf2_cu *child_origin_cu;
13605
13606 /* We are trying to process concrete instance entries:
216f72a1 13607 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13608 it's not relevant to our analysis here. i.e. detecting DIEs that are
13609 present in the abstract instance but not referenced in the concrete
13610 one. */
216f72a1
JK
13611 if (child_die->tag == DW_TAG_call_site
13612 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13613 continue;
13614
c38f313d
DJ
13615 /* For each CHILD_DIE, find the corresponding child of
13616 ORIGIN_DIE. If there is more than one layer of
13617 DW_AT_abstract_origin, follow them all; there shouldn't be,
13618 but GCC versions at least through 4.4 generate this (GCC PR
13619 40573). */
3ea89b92
PMR
13620 child_origin_die = child_die;
13621 child_origin_cu = cu;
c38f313d
DJ
13622 while (1)
13623 {
cd02d79d
PA
13624 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13625 child_origin_cu);
c38f313d
DJ
13626 if (attr == NULL)
13627 break;
cd02d79d
PA
13628 child_origin_die = follow_die_ref (child_origin_die, attr,
13629 &child_origin_cu);
c38f313d
DJ
13630 }
13631
d389af10
JK
13632 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13633 counterpart may exist. */
c38f313d 13634 if (child_origin_die != child_die)
d389af10 13635 {
edb3359d
DJ
13636 if (child_die->tag != child_origin_die->tag
13637 && !(child_die->tag == DW_TAG_inlined_subroutine
13638 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13639 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13640 "different tags"),
9d8780f0
SM
13641 sect_offset_str (child_die->sect_off),
13642 sect_offset_str (child_origin_die->sect_off));
c38f313d 13643 if (child_origin_die->parent != origin_die)
b98664d3 13644 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13645 "different parents"),
9d8780f0
SM
13646 sect_offset_str (child_die->sect_off),
13647 sect_offset_str (child_origin_die->sect_off));
c38f313d 13648 else
791afaa2 13649 offsets.push_back (child_origin_die->sect_off);
d389af10 13650 }
d389af10 13651 }
791afaa2
TT
13652 std::sort (offsets.begin (), offsets.end ());
13653 sect_offset *offsets_end = offsets.data () + offsets.size ();
13654 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13655 if (offsetp[-1] == *offsetp)
b98664d3 13656 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13657 "to DIE %s as their abstract origin"),
13658 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13659
791afaa2 13660 offsetp = offsets.data ();
d389af10
JK
13661 origin_child_die = origin_die->child;
13662 while (origin_child_die && origin_child_die->tag)
13663 {
13664 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13665 while (offsetp < offsets_end
9c541725 13666 && *offsetp < origin_child_die->sect_off)
d389af10 13667 offsetp++;
b64f50a1 13668 if (offsetp >= offsets_end
9c541725 13669 || *offsetp > origin_child_die->sect_off)
d389af10 13670 {
adde2bff
DE
13671 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13672 Check whether we're already processing ORIGIN_CHILD_DIE.
13673 This can happen with mutually referenced abstract_origins.
13674 PR 16581. */
13675 if (!origin_child_die->in_process)
13676 process_die (origin_child_die, origin_cu);
d389af10
JK
13677 }
13678 origin_child_die = sibling_die (origin_child_die);
13679 }
cd02d79d 13680 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13681
13682 if (cu != origin_cu)
13683 compute_delayed_physnames (origin_cu);
d389af10
JK
13684}
13685
c906108c 13686static void
e7c27a73 13687read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13688{
518817b3 13689 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13691 struct context_stack *newobj;
c906108c
SS
13692 CORE_ADDR lowpc;
13693 CORE_ADDR highpc;
13694 struct die_info *child_die;
edb3359d 13695 struct attribute *attr, *call_line, *call_file;
15d034d0 13696 const char *name;
e142c38c 13697 CORE_ADDR baseaddr;
801e3a5b 13698 struct block *block;
edb3359d 13699 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13700 std::vector<struct symbol *> template_args;
34eaf542 13701 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13702
13703 if (inlined_func)
13704 {
13705 /* If we do not have call site information, we can't show the
13706 caller of this inlined function. That's too confusing, so
13707 only use the scope for local variables. */
13708 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13709 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13710 if (call_line == NULL || call_file == NULL)
13711 {
13712 read_lexical_block_scope (die, cu);
13713 return;
13714 }
13715 }
c906108c 13716
6a053cb1 13717 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
e142c38c 13718
94af9270 13719 name = dwarf2_name (die, cu);
c906108c 13720
e8d05480
JB
13721 /* Ignore functions with missing or empty names. These are actually
13722 illegal according to the DWARF standard. */
13723 if (name == NULL)
13724 {
b98664d3 13725 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13726 sect_offset_str (die->sect_off));
e8d05480
JB
13727 return;
13728 }
13729
13730 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13731 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13732 <= PC_BOUNDS_INVALID)
e8d05480 13733 {
ae4d0c03
PM
13734 attr = dwarf2_attr (die, DW_AT_external, cu);
13735 if (!attr || !DW_UNSND (attr))
b98664d3 13736 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13737 "for subprogram DIE at %s"),
13738 sect_offset_str (die->sect_off));
e8d05480
JB
13739 return;
13740 }
c906108c 13741
3e29f34a
MR
13742 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13743 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13744
34eaf542
TT
13745 /* If we have any template arguments, then we must allocate a
13746 different sort of symbol. */
13747 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13748 {
13749 if (child_die->tag == DW_TAG_template_type_param
13750 || child_die->tag == DW_TAG_template_value_param)
13751 {
e623cf5d 13752 templ_func = allocate_template_symbol (objfile);
cf724bc9 13753 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13754 break;
13755 }
13756 }
13757
c24bdb02 13758 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13759 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13760 (struct symbol *) templ_func);
4c2df51b 13761
81873cc8 13762 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13763 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13764 cu->language);
13765
4cecd739
DJ
13766 /* If there is a location expression for DW_AT_frame_base, record
13767 it. */
e142c38c 13768 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13769 if (attr != nullptr)
fe978cb0 13770 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13771
63e43d3a
PMR
13772 /* If there is a location for the static link, record it. */
13773 newobj->static_link = NULL;
13774 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13775 if (attr != nullptr)
63e43d3a 13776 {
224c3ddb
SM
13777 newobj->static_link
13778 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13779 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13780 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13781 }
13782
c24bdb02 13783 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13784
639d11d3 13785 if (die->child != NULL)
c906108c 13786 {
639d11d3 13787 child_die = die->child;
c906108c
SS
13788 while (child_die && child_die->tag)
13789 {
34eaf542
TT
13790 if (child_die->tag == DW_TAG_template_type_param
13791 || child_die->tag == DW_TAG_template_value_param)
13792 {
13793 struct symbol *arg = new_symbol (child_die, NULL, cu);
13794
f1078f66 13795 if (arg != NULL)
2f4732b0 13796 template_args.push_back (arg);
34eaf542
TT
13797 }
13798 else
13799 process_die (child_die, cu);
c906108c
SS
13800 child_die = sibling_die (child_die);
13801 }
13802 }
13803
d389af10
JK
13804 inherit_abstract_dies (die, cu);
13805
4a811a97
UW
13806 /* If we have a DW_AT_specification, we might need to import using
13807 directives from the context of the specification DIE. See the
13808 comment in determine_prefix. */
13809 if (cu->language == language_cplus
13810 && dwarf2_attr (die, DW_AT_specification, cu))
13811 {
13812 struct dwarf2_cu *spec_cu = cu;
13813 struct die_info *spec_die = die_specification (die, &spec_cu);
13814
13815 while (spec_die)
13816 {
13817 child_die = spec_die->child;
13818 while (child_die && child_die->tag)
13819 {
13820 if (child_die->tag == DW_TAG_imported_module)
13821 process_die (child_die, spec_cu);
13822 child_die = sibling_die (child_die);
13823 }
13824
13825 /* In some cases, GCC generates specification DIEs that
13826 themselves contain DW_AT_specification attributes. */
13827 spec_die = die_specification (spec_die, &spec_cu);
13828 }
13829 }
13830
c24bdb02 13831 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13832 /* Make a block for the local symbols within. */
c24bdb02 13833 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13834 cstk.static_link, lowpc, highpc);
801e3a5b 13835
df8a16a1 13836 /* For C++, set the block's scope. */
45280282
IB
13837 if ((cu->language == language_cplus
13838 || cu->language == language_fortran
c44af4eb
TT
13839 || cu->language == language_d
13840 || cu->language == language_rust)
4d4ec4e5 13841 && cu->processing_has_namespace_info)
195a3f6c
TT
13842 block_set_scope (block, determine_prefix (die, cu),
13843 &objfile->objfile_obstack);
df8a16a1 13844
801e3a5b
JB
13845 /* If we have address ranges, record them. */
13846 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13847
a60f3166 13848 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13849
34eaf542 13850 /* Attach template arguments to function. */
2f4732b0 13851 if (!template_args.empty ())
34eaf542
TT
13852 {
13853 gdb_assert (templ_func != NULL);
13854
2f4732b0 13855 templ_func->n_template_arguments = template_args.size ();
34eaf542 13856 templ_func->template_arguments
8d749320
SM
13857 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13858 templ_func->n_template_arguments);
34eaf542 13859 memcpy (templ_func->template_arguments,
2f4732b0 13860 template_args.data (),
34eaf542 13861 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13862
13863 /* Make sure that the symtab is set on the new symbols. Even
13864 though they don't appear in this symtab directly, other parts
13865 of gdb assume that symbols do, and this is reasonably
13866 true. */
8634679f 13867 for (symbol *sym : template_args)
3e1d3d8c 13868 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13869 }
13870
208d8187
JB
13871 /* In C++, we can have functions nested inside functions (e.g., when
13872 a function declares a class that has methods). This means that
13873 when we finish processing a function scope, we may need to go
13874 back to building a containing block's symbol lists. */
c24bdb02
KS
13875 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13876 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13877
921e78cf
JB
13878 /* If we've finished processing a top-level function, subsequent
13879 symbols go in the file symbol list. */
c24bdb02
KS
13880 if (cu->get_builder ()->outermost_context_p ())
13881 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13882}
13883
13884/* Process all the DIES contained within a lexical block scope. Start
13885 a new scope, process the dies, and then close the scope. */
13886
13887static void
e7c27a73 13888read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13889{
518817b3 13890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13891 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13892 CORE_ADDR lowpc, highpc;
13893 struct die_info *child_die;
e142c38c
DJ
13894 CORE_ADDR baseaddr;
13895
6a053cb1 13896 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c
SS
13897
13898 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13899 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13900 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13901 be nasty. Might be easier to properly extend generic blocks to
af34e669 13902 describe ranges. */
e385593e
JK
13903 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13904 {
13905 case PC_BOUNDS_NOT_PRESENT:
13906 /* DW_TAG_lexical_block has no attributes, process its children as if
13907 there was no wrapping by that DW_TAG_lexical_block.
13908 GCC does no longer produces such DWARF since GCC r224161. */
13909 for (child_die = die->child;
13910 child_die != NULL && child_die->tag;
13911 child_die = sibling_die (child_die))
13912 process_die (child_die, cu);
13913 return;
13914 case PC_BOUNDS_INVALID:
13915 return;
13916 }
3e29f34a
MR
13917 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13918 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13919
c24bdb02 13920 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13921 if (die->child != NULL)
c906108c 13922 {
639d11d3 13923 child_die = die->child;
c906108c
SS
13924 while (child_die && child_die->tag)
13925 {
e7c27a73 13926 process_die (child_die, cu);
c906108c
SS
13927 child_die = sibling_die (child_die);
13928 }
13929 }
3ea89b92 13930 inherit_abstract_dies (die, cu);
c24bdb02 13931 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13932
c24bdb02
KS
13933 if (*cu->get_builder ()->get_local_symbols () != NULL
13934 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13935 {
801e3a5b 13936 struct block *block
c24bdb02 13937 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13938 cstk.start_addr, highpc);
801e3a5b
JB
13939
13940 /* Note that recording ranges after traversing children, as we
13941 do here, means that recording a parent's ranges entails
13942 walking across all its children's ranges as they appear in
13943 the address map, which is quadratic behavior.
13944
13945 It would be nicer to record the parent's ranges before
13946 traversing its children, simply overriding whatever you find
13947 there. But since we don't even decide whether to create a
13948 block until after we've traversed its children, that's hard
13949 to do. */
13950 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13951 }
c24bdb02
KS
13952 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13953 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13954}
13955
216f72a1 13956/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13957
13958static void
13959read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13960{
518817b3 13961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13963 CORE_ADDR pc, baseaddr;
13964 struct attribute *attr;
13965 struct call_site *call_site, call_site_local;
13966 void **slot;
13967 int nparams;
13968 struct die_info *child_die;
13969
6a053cb1 13970 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
96408a79 13971
216f72a1
JK
13972 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13973 if (attr == NULL)
13974 {
13975 /* This was a pre-DWARF-5 GNU extension alias
13976 for DW_AT_call_return_pc. */
13977 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13978 }
96408a79
SA
13979 if (!attr)
13980 {
b98664d3 13981 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13982 "DIE %s [in module %s]"),
13983 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13984 return;
13985 }
31aa7e4e 13986 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13987 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13988
13989 if (cu->call_site_htab == NULL)
13990 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13991 NULL, &objfile->objfile_obstack,
13992 hashtab_obstack_allocate, NULL);
13993 call_site_local.pc = pc;
13994 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13995 if (*slot != NULL)
13996 {
b98664d3 13997 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13998 "DIE %s [in module %s]"),
13999 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 14000 objfile_name (objfile));
96408a79
SA
14001 return;
14002 }
14003
14004 /* Count parameters at the caller. */
14005
14006 nparams = 0;
14007 for (child_die = die->child; child_die && child_die->tag;
14008 child_die = sibling_die (child_die))
14009 {
216f72a1
JK
14010 if (child_die->tag != DW_TAG_call_site_parameter
14011 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14012 {
b98664d3 14013 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14014 "DW_TAG_call_site child DIE %s [in module %s]"),
14015 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14016 objfile_name (objfile));
96408a79
SA
14017 continue;
14018 }
14019
14020 nparams++;
14021 }
14022
224c3ddb
SM
14023 call_site
14024 = ((struct call_site *)
14025 obstack_alloc (&objfile->objfile_obstack,
14026 sizeof (*call_site)
14027 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14028 *slot = call_site;
14029 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14030 call_site->pc = pc;
14031
216f72a1
JK
14032 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14033 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14034 {
14035 struct die_info *func_die;
14036
14037 /* Skip also over DW_TAG_inlined_subroutine. */
14038 for (func_die = die->parent;
14039 func_die && func_die->tag != DW_TAG_subprogram
14040 && func_die->tag != DW_TAG_subroutine_type;
14041 func_die = func_die->parent);
14042
216f72a1
JK
14043 /* DW_AT_call_all_calls is a superset
14044 of DW_AT_call_all_tail_calls. */
96408a79 14045 if (func_die
216f72a1 14046 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14047 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14048 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14049 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14050 {
14051 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14052 not complete. But keep CALL_SITE for look ups via call_site_htab,
14053 both the initial caller containing the real return address PC and
14054 the final callee containing the current PC of a chain of tail
14055 calls do not need to have the tail call list complete. But any
14056 function candidate for a virtual tail call frame searched via
14057 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14058 determined unambiguously. */
14059 }
14060 else
14061 {
14062 struct type *func_type = NULL;
14063
14064 if (func_die)
14065 func_type = get_die_type (func_die, cu);
14066 if (func_type != NULL)
14067 {
14068 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14069
14070 /* Enlist this call site to the function. */
14071 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14072 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14073 }
14074 else
b98664d3 14075 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14076 "DIE %s [in module %s]"),
14077 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14078 }
14079 }
14080
216f72a1
JK
14081 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14082 if (attr == NULL)
14083 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14084 if (attr == NULL)
14085 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14086 if (attr == NULL)
216f72a1
JK
14087 {
14088 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14089 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14090 }
96408a79
SA
14091 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14092 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14093 /* Keep NULL DWARF_BLOCK. */;
14094 else if (attr_form_is_block (attr))
14095 {
14096 struct dwarf2_locexpr_baton *dlbaton;
14097
8d749320 14098 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14099 dlbaton->data = DW_BLOCK (attr)->data;
14100 dlbaton->size = DW_BLOCK (attr)->size;
14101 dlbaton->per_cu = cu->per_cu;
14102
14103 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14104 }
7771576e 14105 else if (attr_form_is_ref (attr))
96408a79 14106 {
96408a79
SA
14107 struct dwarf2_cu *target_cu = cu;
14108 struct die_info *target_die;
14109
ac9ec31b 14110 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14111 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14112 if (die_is_declaration (target_die, target_cu))
14113 {
7d45c7c3 14114 const char *target_physname;
9112db09
JK
14115
14116 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14117 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14118 if (target_physname == NULL)
9112db09 14119 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14120 if (target_physname == NULL)
b98664d3 14121 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14122 "physname, for referencing DIE %s [in module %s]"),
14123 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14124 else
7d455152 14125 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14126 }
14127 else
14128 {
14129 CORE_ADDR lowpc;
14130
14131 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14132 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14133 <= PC_BOUNDS_INVALID)
b98664d3 14134 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14135 "low pc, for referencing DIE %s [in module %s]"),
14136 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14137 else
3e29f34a
MR
14138 {
14139 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14140 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14141 }
96408a79
SA
14142 }
14143 }
14144 else
b98664d3 14145 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14146 "block nor reference, for DIE %s [in module %s]"),
14147 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14148
14149 call_site->per_cu = cu->per_cu;
14150
14151 for (child_die = die->child;
14152 child_die && child_die->tag;
14153 child_die = sibling_die (child_die))
14154 {
96408a79 14155 struct call_site_parameter *parameter;
1788b2d3 14156 struct attribute *loc, *origin;
96408a79 14157
216f72a1
JK
14158 if (child_die->tag != DW_TAG_call_site_parameter
14159 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14160 {
14161 /* Already printed the complaint above. */
14162 continue;
14163 }
14164
14165 gdb_assert (call_site->parameter_count < nparams);
14166 parameter = &call_site->parameter[call_site->parameter_count];
14167
1788b2d3
JK
14168 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14169 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14170 register is contained in DW_AT_call_value. */
96408a79 14171
24c5c679 14172 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14173 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14174 if (origin == NULL)
14175 {
14176 /* This was a pre-DWARF-5 GNU extension alias
14177 for DW_AT_call_parameter. */
14178 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14179 }
7771576e 14180 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14181 {
1788b2d3 14182 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14183
14184 sect_offset sect_off
14185 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14186 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14187 {
14188 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14189 binding can be done only inside one CU. Such referenced DIE
14190 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14191 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14192 "DW_TAG_call_site child DIE %s [in module %s]"),
14193 sect_offset_str (child_die->sect_off),
9c541725 14194 objfile_name (objfile));
d76b7dbc
JK
14195 continue;
14196 }
9c541725
PA
14197 parameter->u.param_cu_off
14198 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14199 }
14200 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14201 {
b98664d3 14202 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14203 "DW_TAG_call_site child DIE %s [in module %s]"),
14204 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14205 continue;
14206 }
24c5c679 14207 else
96408a79 14208 {
24c5c679
JK
14209 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14210 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14211 if (parameter->u.dwarf_reg != -1)
14212 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14213 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14214 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14215 &parameter->u.fb_offset))
14216 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14217 else
14218 {
b98664d3 14219 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14220 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14221 "DW_TAG_call_site child DIE %s "
24c5c679 14222 "[in module %s]"),
9d8780f0 14223 sect_offset_str (child_die->sect_off),
9c541725 14224 objfile_name (objfile));
24c5c679
JK
14225 continue;
14226 }
96408a79
SA
14227 }
14228
216f72a1
JK
14229 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14230 if (attr == NULL)
14231 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14232 if (!attr_form_is_block (attr))
14233 {
b98664d3 14234 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14235 "DW_TAG_call_site child DIE %s [in module %s]"),
14236 sect_offset_str (child_die->sect_off),
9c541725 14237 objfile_name (objfile));
96408a79
SA
14238 continue;
14239 }
14240 parameter->value = DW_BLOCK (attr)->data;
14241 parameter->value_size = DW_BLOCK (attr)->size;
14242
14243 /* Parameters are not pre-cleared by memset above. */
14244 parameter->data_value = NULL;
14245 parameter->data_value_size = 0;
14246 call_site->parameter_count++;
14247
216f72a1
JK
14248 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14249 if (attr == NULL)
14250 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14251 if (attr != nullptr)
96408a79
SA
14252 {
14253 if (!attr_form_is_block (attr))
b98664d3 14254 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14255 "DW_TAG_call_site child DIE %s [in module %s]"),
14256 sect_offset_str (child_die->sect_off),
9c541725 14257 objfile_name (objfile));
96408a79
SA
14258 else
14259 {
14260 parameter->data_value = DW_BLOCK (attr)->data;
14261 parameter->data_value_size = DW_BLOCK (attr)->size;
14262 }
14263 }
14264 }
14265}
14266
71a3c369
TT
14267/* Helper function for read_variable. If DIE represents a virtual
14268 table, then return the type of the concrete object that is
14269 associated with the virtual table. Otherwise, return NULL. */
14270
14271static struct type *
14272rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14273{
14274 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14275 if (attr == NULL)
14276 return NULL;
14277
14278 /* Find the type DIE. */
14279 struct die_info *type_die = NULL;
14280 struct dwarf2_cu *type_cu = cu;
14281
14282 if (attr_form_is_ref (attr))
14283 type_die = follow_die_ref (die, attr, &type_cu);
14284 if (type_die == NULL)
14285 return NULL;
14286
14287 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14288 return NULL;
14289 return die_containing_type (type_die, type_cu);
14290}
14291
14292/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14293
14294static void
14295read_variable (struct die_info *die, struct dwarf2_cu *cu)
14296{
14297 struct rust_vtable_symbol *storage = NULL;
14298
14299 if (cu->language == language_rust)
14300 {
14301 struct type *containing_type = rust_containing_type (die, cu);
14302
14303 if (containing_type != NULL)
14304 {
518817b3 14305 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14306
468c0cbb 14307 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14308 initialize_objfile_symbol (storage);
14309 storage->concrete_type = containing_type;
cf724bc9 14310 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14311 }
14312 }
14313
e4a62c65
TV
14314 struct symbol *res = new_symbol (die, NULL, cu, storage);
14315 struct attribute *abstract_origin
14316 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14317 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14318 if (res == NULL && loc && abstract_origin)
14319 {
14320 /* We have a variable without a name, but with a location and an abstract
14321 origin. This may be a concrete instance of an abstract variable
14322 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14323 later. */
14324 struct dwarf2_cu *origin_cu = cu;
14325 struct die_info *origin_die
14326 = follow_die_ref (die, abstract_origin, &origin_cu);
14327 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14328 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14329 }
71a3c369
TT
14330}
14331
43988095
JK
14332/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14333 reading .debug_rnglists.
14334 Callback's type should be:
14335 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14336 Return true if the attributes are present and valid, otherwise,
14337 return false. */
14338
14339template <typename Callback>
14340static bool
14341dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14342 Callback &&callback)
14343{
ed2dc618 14344 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14345 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14346 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14347 bfd *obfd = objfile->obfd;
43988095
JK
14348 /* Base address selection entry. */
14349 CORE_ADDR base;
14350 int found_base;
43988095 14351 const gdb_byte *buffer;
43988095
JK
14352 CORE_ADDR baseaddr;
14353 bool overflow = false;
14354
14355 found_base = cu->base_known;
14356 base = cu->base_address;
14357
14358 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14359 if (offset >= dwarf2_per_objfile->rnglists.size)
14360 {
b98664d3 14361 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14362 offset);
14363 return false;
14364 }
14365 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14366
6a053cb1 14367 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
43988095
JK
14368
14369 while (1)
14370 {
7814882a
JK
14371 /* Initialize it due to a false compiler warning. */
14372 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14373 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14374 + dwarf2_per_objfile->rnglists.size);
14375 unsigned int bytes_read;
14376
14377 if (buffer == buf_end)
14378 {
14379 overflow = true;
14380 break;
14381 }
14382 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14383 switch (rlet)
14384 {
14385 case DW_RLE_end_of_list:
14386 break;
14387 case DW_RLE_base_address:
14388 if (buffer + cu->header.addr_size > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 base = read_address (obfd, buffer, cu, &bytes_read);
14394 found_base = 1;
14395 buffer += bytes_read;
14396 break;
14397 case DW_RLE_start_length:
14398 if (buffer + cu->header.addr_size > buf_end)
14399 {
14400 overflow = true;
14401 break;
14402 }
14403 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14404 buffer += bytes_read;
14405 range_end = (range_beginning
14406 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14407 buffer += bytes_read;
14408 if (buffer > buf_end)
14409 {
14410 overflow = true;
14411 break;
14412 }
14413 break;
14414 case DW_RLE_offset_pair:
14415 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14416 buffer += bytes_read;
14417 if (buffer > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14423 buffer += bytes_read;
14424 if (buffer > buf_end)
14425 {
14426 overflow = true;
14427 break;
14428 }
14429 break;
14430 case DW_RLE_start_end:
14431 if (buffer + 2 * cu->header.addr_size > buf_end)
14432 {
14433 overflow = true;
14434 break;
14435 }
14436 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14437 buffer += bytes_read;
14438 range_end = read_address (obfd, buffer, cu, &bytes_read);
14439 buffer += bytes_read;
14440 break;
14441 default:
b98664d3 14442 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14443 return false;
14444 }
14445 if (rlet == DW_RLE_end_of_list || overflow)
14446 break;
14447 if (rlet == DW_RLE_base_address)
14448 continue;
14449
14450 if (!found_base)
14451 {
14452 /* We have no valid base address for the ranges
14453 data. */
b98664d3 14454 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14455 return false;
14456 }
14457
14458 if (range_beginning > range_end)
14459 {
14460 /* Inverted range entries are invalid. */
b98664d3 14461 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14462 return false;
14463 }
14464
14465 /* Empty range entries have no effect. */
14466 if (range_beginning == range_end)
14467 continue;
14468
14469 range_beginning += base;
14470 range_end += base;
14471
14472 /* A not-uncommon case of bad debug info.
14473 Don't pollute the addrmap with bad data. */
14474 if (range_beginning + baseaddr == 0
14475 && !dwarf2_per_objfile->has_section_at_zero)
14476 {
b98664d3 14477 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14478 " [in module %s]"), objfile_name (objfile));
14479 continue;
14480 }
14481
14482 callback (range_beginning, range_end);
14483 }
14484
14485 if (overflow)
14486 {
b98664d3 14487 complaint (_("Offset %d is not terminated "
43988095
JK
14488 "for DW_AT_ranges attribute"),
14489 offset);
14490 return false;
14491 }
14492
14493 return true;
14494}
14495
14496/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14497 Callback's type should be:
14498 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14499 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14500
43988095 14501template <typename Callback>
43039443 14502static int
5f46c5a5 14503dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14504 Callback &&callback)
43039443 14505{
ed2dc618 14506 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14507 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14508 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14509 struct comp_unit_head *cu_header = &cu->header;
14510 bfd *obfd = objfile->obfd;
14511 unsigned int addr_size = cu_header->addr_size;
14512 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14513 /* Base address selection entry. */
14514 CORE_ADDR base;
14515 int found_base;
14516 unsigned int dummy;
d521ce57 14517 const gdb_byte *buffer;
ff013f42 14518 CORE_ADDR baseaddr;
43039443 14519
43988095
JK
14520 if (cu_header->version >= 5)
14521 return dwarf2_rnglists_process (offset, cu, callback);
14522
d00adf39
DE
14523 found_base = cu->base_known;
14524 base = cu->base_address;
43039443 14525
be391dca 14526 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14527 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14528 {
b98664d3 14529 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14530 offset);
14531 return 0;
14532 }
dce234bc 14533 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14534
6a053cb1 14535 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
ff013f42 14536
43039443
JK
14537 while (1)
14538 {
14539 CORE_ADDR range_beginning, range_end;
14540
14541 range_beginning = read_address (obfd, buffer, cu, &dummy);
14542 buffer += addr_size;
14543 range_end = read_address (obfd, buffer, cu, &dummy);
14544 buffer += addr_size;
14545 offset += 2 * addr_size;
14546
14547 /* An end of list marker is a pair of zero addresses. */
14548 if (range_beginning == 0 && range_end == 0)
14549 /* Found the end of list entry. */
14550 break;
14551
14552 /* Each base address selection entry is a pair of 2 values.
14553 The first is the largest possible address, the second is
14554 the base address. Check for a base address here. */
14555 if ((range_beginning & mask) == mask)
14556 {
28d2bfb9
AB
14557 /* If we found the largest possible address, then we already
14558 have the base address in range_end. */
14559 base = range_end;
43039443
JK
14560 found_base = 1;
14561 continue;
14562 }
14563
14564 if (!found_base)
14565 {
14566 /* We have no valid base address for the ranges
14567 data. */
b98664d3 14568 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14569 return 0;
14570 }
14571
9277c30c
UW
14572 if (range_beginning > range_end)
14573 {
14574 /* Inverted range entries are invalid. */
b98664d3 14575 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14576 return 0;
14577 }
14578
14579 /* Empty range entries have no effect. */
14580 if (range_beginning == range_end)
14581 continue;
14582
43039443
JK
14583 range_beginning += base;
14584 range_end += base;
14585
01093045
DE
14586 /* A not-uncommon case of bad debug info.
14587 Don't pollute the addrmap with bad data. */
14588 if (range_beginning + baseaddr == 0
14589 && !dwarf2_per_objfile->has_section_at_zero)
14590 {
b98664d3 14591 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14592 " [in module %s]"), objfile_name (objfile));
01093045
DE
14593 continue;
14594 }
14595
5f46c5a5
JK
14596 callback (range_beginning, range_end);
14597 }
14598
14599 return 1;
14600}
14601
14602/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14603 Return 1 if the attributes are present and valid, otherwise, return 0.
14604 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14605
14606static int
14607dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14608 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14609 struct partial_symtab *ranges_pst)
14610{
518817b3 14611 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6a053cb1 14613 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
5f46c5a5
JK
14614 int low_set = 0;
14615 CORE_ADDR low = 0;
14616 CORE_ADDR high = 0;
14617 int retval;
14618
14619 retval = dwarf2_ranges_process (offset, cu,
14620 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14621 {
9277c30c 14622 if (ranges_pst != NULL)
3e29f34a
MR
14623 {
14624 CORE_ADDR lowpc;
14625 CORE_ADDR highpc;
14626
79748972
TT
14627 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14628 range_beginning + baseaddr)
14629 - baseaddr);
14630 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14631 range_end + baseaddr)
14632 - baseaddr);
d320c2b5
TT
14633 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14634 lowpc, highpc - 1, ranges_pst);
3e29f34a 14635 }
ff013f42 14636
43039443
JK
14637 /* FIXME: This is recording everything as a low-high
14638 segment of consecutive addresses. We should have a
14639 data structure for discontiguous block ranges
14640 instead. */
14641 if (! low_set)
14642 {
14643 low = range_beginning;
14644 high = range_end;
14645 low_set = 1;
14646 }
14647 else
14648 {
14649 if (range_beginning < low)
14650 low = range_beginning;
14651 if (range_end > high)
14652 high = range_end;
14653 }
5f46c5a5
JK
14654 });
14655 if (!retval)
14656 return 0;
43039443
JK
14657
14658 if (! low_set)
14659 /* If the first entry is an end-of-list marker, the range
14660 describes an empty scope, i.e. no instructions. */
14661 return 0;
14662
14663 if (low_return)
14664 *low_return = low;
14665 if (high_return)
14666 *high_return = high;
14667 return 1;
14668}
14669
3a2b436a
JK
14670/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14671 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14672 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14673
3a2b436a 14674static enum pc_bounds_kind
af34e669 14675dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14676 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14677 struct partial_symtab *pst)
c906108c 14678{
518817b3
SM
14679 struct dwarf2_per_objfile *dwarf2_per_objfile
14680 = cu->per_cu->dwarf2_per_objfile;
c906108c 14681 struct attribute *attr;
91da1414 14682 struct attribute *attr_high;
af34e669
DJ
14683 CORE_ADDR low = 0;
14684 CORE_ADDR high = 0;
e385593e 14685 enum pc_bounds_kind ret;
c906108c 14686
91da1414
MW
14687 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14688 if (attr_high)
af34e669 14689 {
e142c38c 14690 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14691 if (attr != nullptr)
91da1414 14692 {
31aa7e4e
JB
14693 low = attr_value_as_address (attr);
14694 high = attr_value_as_address (attr_high);
14695 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14696 high += low;
91da1414 14697 }
af34e669
DJ
14698 else
14699 /* Found high w/o low attribute. */
e385593e 14700 return PC_BOUNDS_INVALID;
af34e669
DJ
14701
14702 /* Found consecutive range of addresses. */
3a2b436a 14703 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14704 }
c906108c 14705 else
af34e669 14706 {
e142c38c 14707 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14708 if (attr != NULL)
14709 {
ab435259
DE
14710 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14711 We take advantage of the fact that DW_AT_ranges does not appear
14712 in DW_TAG_compile_unit of DWO files. */
14713 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14714 unsigned int ranges_offset = (DW_UNSND (attr)
14715 + (need_ranges_base
14716 ? cu->ranges_base
14717 : 0));
2e3cf129 14718
af34e669 14719 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14720 .debug_ranges section. */
2e3cf129 14721 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14722 return PC_BOUNDS_INVALID;
43039443 14723 /* Found discontinuous range of addresses. */
3a2b436a 14724 ret = PC_BOUNDS_RANGES;
af34e669 14725 }
e385593e
JK
14726 else
14727 return PC_BOUNDS_NOT_PRESENT;
af34e669 14728 }
c906108c 14729
48fbe735 14730 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14731 if (high <= low)
e385593e 14732 return PC_BOUNDS_INVALID;
c906108c
SS
14733
14734 /* When using the GNU linker, .gnu.linkonce. sections are used to
14735 eliminate duplicate copies of functions and vtables and such.
14736 The linker will arbitrarily choose one and discard the others.
14737 The AT_*_pc values for such functions refer to local labels in
14738 these sections. If the section from that file was discarded, the
14739 labels are not in the output, so the relocs get a value of 0.
14740 If this is a discarded function, mark the pc bounds as invalid,
14741 so that GDB will ignore it. */
72dca2f5 14742 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14743 return PC_BOUNDS_INVALID;
c906108c
SS
14744
14745 *lowpc = low;
96408a79
SA
14746 if (highpc)
14747 *highpc = high;
af34e669 14748 return ret;
c906108c
SS
14749}
14750
b084d499
JB
14751/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14752 its low and high PC addresses. Do nothing if these addresses could not
14753 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14754 and HIGHPC to the high address if greater than HIGHPC. */
14755
14756static void
14757dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14759 struct dwarf2_cu *cu)
14760{
14761 CORE_ADDR low, high;
14762 struct die_info *child = die->child;
14763
e385593e 14764 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14765 {
325fac50
PA
14766 *lowpc = std::min (*lowpc, low);
14767 *highpc = std::max (*highpc, high);
b084d499
JB
14768 }
14769
14770 /* If the language does not allow nested subprograms (either inside
14771 subprograms or lexical blocks), we're done. */
14772 if (cu->language != language_ada)
14773 return;
6e70227d 14774
b084d499
JB
14775 /* Check all the children of the given DIE. If it contains nested
14776 subprograms, then check their pc bounds. Likewise, we need to
14777 check lexical blocks as well, as they may also contain subprogram
14778 definitions. */
14779 while (child && child->tag)
14780 {
14781 if (child->tag == DW_TAG_subprogram
14782 || child->tag == DW_TAG_lexical_block)
14783 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14784 child = sibling_die (child);
14785 }
14786}
14787
fae299cd
DC
14788/* Get the low and high pc's represented by the scope DIE, and store
14789 them in *LOWPC and *HIGHPC. If the correct values can't be
14790 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14791
14792static void
14793get_scope_pc_bounds (struct die_info *die,
14794 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14795 struct dwarf2_cu *cu)
14796{
14797 CORE_ADDR best_low = (CORE_ADDR) -1;
14798 CORE_ADDR best_high = (CORE_ADDR) 0;
14799 CORE_ADDR current_low, current_high;
14800
3a2b436a 14801 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14802 >= PC_BOUNDS_RANGES)
fae299cd
DC
14803 {
14804 best_low = current_low;
14805 best_high = current_high;
14806 }
14807 else
14808 {
14809 struct die_info *child = die->child;
14810
14811 while (child && child->tag)
14812 {
14813 switch (child->tag) {
14814 case DW_TAG_subprogram:
b084d499 14815 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14816 break;
14817 case DW_TAG_namespace:
f55ee35c 14818 case DW_TAG_module:
fae299cd
DC
14819 /* FIXME: carlton/2004-01-16: Should we do this for
14820 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14821 that current GCC's always emit the DIEs corresponding
14822 to definitions of methods of classes as children of a
14823 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14824 the DIEs giving the declarations, which could be
14825 anywhere). But I don't see any reason why the
14826 standards says that they have to be there. */
14827 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14828
14829 if (current_low != ((CORE_ADDR) -1))
14830 {
325fac50
PA
14831 best_low = std::min (best_low, current_low);
14832 best_high = std::max (best_high, current_high);
fae299cd
DC
14833 }
14834 break;
14835 default:
0963b4bd 14836 /* Ignore. */
fae299cd
DC
14837 break;
14838 }
14839
14840 child = sibling_die (child);
14841 }
14842 }
14843
14844 *lowpc = best_low;
14845 *highpc = best_high;
14846}
14847
801e3a5b
JB
14848/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14849 in DIE. */
380bca97 14850
801e3a5b
JB
14851static void
14852dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14853 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14854{
518817b3 14855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14857 struct attribute *attr;
91da1414 14858 struct attribute *attr_high;
801e3a5b 14859
91da1414
MW
14860 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14861 if (attr_high)
801e3a5b 14862 {
801e3a5b 14863 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14864 if (attr != nullptr)
801e3a5b 14865 {
31aa7e4e
JB
14866 CORE_ADDR low = attr_value_as_address (attr);
14867 CORE_ADDR high = attr_value_as_address (attr_high);
14868
14869 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14870 high += low;
9a619af0 14871
3e29f34a
MR
14872 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14873 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14874 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14875 }
14876 }
14877
14878 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14879 if (attr != nullptr)
801e3a5b 14880 {
ab435259
DE
14881 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14882 We take advantage of the fact that DW_AT_ranges does not appear
14883 in DW_TAG_compile_unit of DWO files. */
14884 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14885
14886 /* The value of the DW_AT_ranges attribute is the offset of the
14887 address range list in the .debug_ranges section. */
ab435259
DE
14888 unsigned long offset = (DW_UNSND (attr)
14889 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14890
2d5f09ec 14891 std::vector<blockrange> blockvec;
5f46c5a5
JK
14892 dwarf2_ranges_process (offset, cu,
14893 [&] (CORE_ADDR start, CORE_ADDR end)
14894 {
58fdfd2c
JK
14895 start += baseaddr;
14896 end += baseaddr;
5f46c5a5
JK
14897 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14898 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14899 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14900 blockvec.emplace_back (start, end);
5f46c5a5 14901 });
2d5f09ec
KB
14902
14903 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14904 }
14905}
14906
685b1105
JK
14907/* Check whether the producer field indicates either of GCC < 4.6, or the
14908 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14909
685b1105
JK
14910static void
14911check_producer (struct dwarf2_cu *cu)
60d5a603 14912{
38360086 14913 int major, minor;
60d5a603
JK
14914
14915 if (cu->producer == NULL)
14916 {
14917 /* For unknown compilers expect their behavior is DWARF version
14918 compliant.
14919
14920 GCC started to support .debug_types sections by -gdwarf-4 since
14921 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14922 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14923 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14924 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14925 }
b1ffba5a 14926 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14927 {
38360086
MW
14928 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14929 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14930 }
5230b05a 14931 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14932 {
14933 cu->producer_is_icc = true;
14934 cu->producer_is_icc_lt_14 = major < 14;
14935 }
c258c396
JD
14936 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14937 cu->producer_is_codewarrior = true;
685b1105
JK
14938 else
14939 {
14940 /* For other non-GCC compilers, expect their behavior is DWARF version
14941 compliant. */
60d5a603
JK
14942 }
14943
9068261f 14944 cu->checked_producer = true;
685b1105 14945}
ba919b58 14946
685b1105
JK
14947/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14948 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14949 during 4.6.0 experimental. */
14950
9068261f 14951static bool
685b1105
JK
14952producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14953{
14954 if (!cu->checked_producer)
14955 check_producer (cu);
14956
14957 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14958}
14959
c258c396
JD
14960
14961/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14962 with incorrect is_stmt attributes. */
14963
14964static bool
14965producer_is_codewarrior (struct dwarf2_cu *cu)
14966{
14967 if (!cu->checked_producer)
14968 check_producer (cu);
14969
14970 return cu->producer_is_codewarrior;
14971}
14972
405feb71 14973/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14974 DW_AT_accessibility. */
14975
14976static enum dwarf_access_attribute
14977dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14978{
14979 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14980 {
14981 /* The default DWARF 2 accessibility for members is public, the default
14982 accessibility for inheritance is private. */
14983
14984 if (die->tag != DW_TAG_inheritance)
14985 return DW_ACCESS_public;
14986 else
14987 return DW_ACCESS_private;
14988 }
14989 else
14990 {
14991 /* DWARF 3+ defines the default accessibility a different way. The same
14992 rules apply now for DW_TAG_inheritance as for the members and it only
14993 depends on the container kind. */
14994
14995 if (die->parent->tag == DW_TAG_class_type)
14996 return DW_ACCESS_private;
14997 else
14998 return DW_ACCESS_public;
14999 }
15000}
15001
74ac6d43
TT
15002/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15003 offset. If the attribute was not found return 0, otherwise return
15004 1. If it was found but could not properly be handled, set *OFFSET
15005 to 0. */
15006
15007static int
15008handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15009 LONGEST *offset)
15010{
15011 struct attribute *attr;
15012
15013 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15014 if (attr != NULL)
15015 {
15016 *offset = 0;
15017
15018 /* Note that we do not check for a section offset first here.
15019 This is because DW_AT_data_member_location is new in DWARF 4,
15020 so if we see it, we can assume that a constant form is really
15021 a constant and not a section offset. */
15022 if (attr_form_is_constant (attr))
15023 *offset = dwarf2_get_attr_constant_value (attr, 0);
15024 else if (attr_form_is_section_offset (attr))
15025 dwarf2_complex_location_expr_complaint ();
15026 else if (attr_form_is_block (attr))
15027 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15028 else
15029 dwarf2_complex_location_expr_complaint ();
15030
15031 return 1;
15032 }
15033
15034 return 0;
15035}
15036
c906108c
SS
15037/* Add an aggregate field to the field list. */
15038
15039static void
107d2387 15040dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15041 struct dwarf2_cu *cu)
6e70227d 15042{
518817b3 15043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15045 struct nextfield *new_field;
15046 struct attribute *attr;
15047 struct field *fp;
15d034d0 15048 const char *fieldname = "";
c906108c 15049
7d0ccb61
DJ
15050 if (die->tag == DW_TAG_inheritance)
15051 {
be2daae6
TT
15052 fip->baseclasses.emplace_back ();
15053 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15054 }
15055 else
15056 {
be2daae6
TT
15057 fip->fields.emplace_back ();
15058 new_field = &fip->fields.back ();
7d0ccb61 15059 }
be2daae6 15060
c906108c
SS
15061 fip->nfields++;
15062
e142c38c 15063 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15064 if (attr != nullptr)
c906108c 15065 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15066 else
15067 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15068 if (new_field->accessibility != DW_ACCESS_public)
15069 fip->non_public_fields = 1;
60d5a603 15070
e142c38c 15071 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15072 if (attr != nullptr)
c906108c 15073 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15074 else
15075 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15076
15077 fp = &new_field->field;
a9a9bd0f 15078
e142c38c 15079 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15080 {
74ac6d43
TT
15081 LONGEST offset;
15082
a9a9bd0f 15083 /* Data member other than a C++ static data member. */
6e70227d 15084
c906108c 15085 /* Get type of field. */
e7c27a73 15086 fp->type = die_type (die, cu);
c906108c 15087
d6a843b5 15088 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15089
c906108c 15090 /* Get bit size of field (zero if none). */
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15092 if (attr != nullptr)
c906108c
SS
15093 {
15094 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15095 }
15096 else
15097 {
15098 FIELD_BITSIZE (*fp) = 0;
15099 }
15100
15101 /* Get bit offset of field. */
74ac6d43
TT
15102 if (handle_data_member_location (die, cu, &offset))
15103 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15104 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15105 if (attr != nullptr)
c906108c 15106 {
d5a22e77 15107 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15108 {
15109 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15110 additional bit offset from the MSB of the containing
15111 anonymous object to the MSB of the field. We don't
15112 have to do anything special since we don't need to
15113 know the size of the anonymous object. */
f41f5e61 15114 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15115 }
15116 else
15117 {
15118 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15119 MSB of the anonymous object, subtract off the number of
15120 bits from the MSB of the field to the MSB of the
15121 object, and then subtract off the number of bits of
15122 the field itself. The result is the bit offset of
15123 the LSB of the field. */
c906108c
SS
15124 int anonymous_size;
15125 int bit_offset = DW_UNSND (attr);
15126
e142c38c 15127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15128 if (attr != nullptr)
c906108c
SS
15129 {
15130 /* The size of the anonymous object containing
15131 the bit field is explicit, so use the
15132 indicated size (in bytes). */
15133 anonymous_size = DW_UNSND (attr);
15134 }
15135 else
15136 {
15137 /* The size of the anonymous object containing
15138 the bit field must be inferred from the type
15139 attribute of the data member containing the
15140 bit field. */
15141 anonymous_size = TYPE_LENGTH (fp->type);
15142 }
f41f5e61
PA
15143 SET_FIELD_BITPOS (*fp,
15144 (FIELD_BITPOS (*fp)
15145 + anonymous_size * bits_per_byte
15146 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15147 }
15148 }
da5b30da
AA
15149 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15150 if (attr != NULL)
15151 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15152 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15153
15154 /* Get name of field. */
39cbfefa
DJ
15155 fieldname = dwarf2_name (die, cu);
15156 if (fieldname == NULL)
15157 fieldname = "";
d8151005
DJ
15158
15159 /* The name is already allocated along with this objfile, so we don't
15160 need to duplicate it for the type. */
15161 fp->name = fieldname;
c906108c
SS
15162
15163 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15164 pointer or virtual base class pointer) to private. */
e142c38c 15165 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15166 {
d48cc9dd 15167 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15168 new_field->accessibility = DW_ACCESS_private;
15169 fip->non_public_fields = 1;
15170 }
15171 }
a9a9bd0f 15172 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15173 {
a9a9bd0f
DC
15174 /* C++ static member. */
15175
15176 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15177 is a declaration, but all versions of G++ as of this writing
15178 (so through at least 3.2.1) incorrectly generate
15179 DW_TAG_variable tags. */
6e70227d 15180
ff355380 15181 const char *physname;
c906108c 15182
a9a9bd0f 15183 /* Get name of field. */
39cbfefa
DJ
15184 fieldname = dwarf2_name (die, cu);
15185 if (fieldname == NULL)
c906108c
SS
15186 return;
15187
254e6b9e 15188 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15189 if (attr
15190 /* Only create a symbol if this is an external value.
15191 new_symbol checks this and puts the value in the global symbol
15192 table, which we want. If it is not external, new_symbol
15193 will try to put the value in cu->list_in_scope which is wrong. */
15194 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15195 {
15196 /* A static const member, not much different than an enum as far as
15197 we're concerned, except that we can support more types. */
15198 new_symbol (die, NULL, cu);
15199 }
15200
2df3850c 15201 /* Get physical name. */
ff355380 15202 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15203
d8151005
DJ
15204 /* The name is already allocated along with this objfile, so we don't
15205 need to duplicate it for the type. */
15206 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15207 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15208 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15209 }
15210 else if (die->tag == DW_TAG_inheritance)
15211 {
74ac6d43 15212 LONGEST offset;
d4b96c9a 15213
74ac6d43
TT
15214 /* C++ base class field. */
15215 if (handle_data_member_location (die, cu, &offset))
15216 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15217 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15218 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15219 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15220 }
2ddeaf8a
TT
15221 else if (die->tag == DW_TAG_variant_part)
15222 {
15223 /* process_structure_scope will treat this DIE as a union. */
15224 process_structure_scope (die, cu);
15225
15226 /* The variant part is relative to the start of the enclosing
15227 structure. */
15228 SET_FIELD_BITPOS (*fp, 0);
15229 fp->type = get_die_type (die, cu);
15230 fp->artificial = 1;
15231 fp->name = "<<variant>>";
c8c81635
TT
15232
15233 /* Normally a DW_TAG_variant_part won't have a size, but our
15234 representation requires one, so set it to the maximum of the
489dbda6
TT
15235 child sizes, being sure to account for the offset at which
15236 each child is seen. */
c8c81635
TT
15237 if (TYPE_LENGTH (fp->type) == 0)
15238 {
15239 unsigned max = 0;
15240 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15241 {
15242 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15243 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15244 if (len > max)
15245 max = len;
15246 }
c8c81635
TT
15247 TYPE_LENGTH (fp->type) = max;
15248 }
2ddeaf8a
TT
15249 }
15250 else
15251 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15252}
15253
883fd55a
KS
15254/* Can the type given by DIE define another type? */
15255
15256static bool
15257type_can_define_types (const struct die_info *die)
15258{
15259 switch (die->tag)
15260 {
15261 case DW_TAG_typedef:
15262 case DW_TAG_class_type:
15263 case DW_TAG_structure_type:
15264 case DW_TAG_union_type:
15265 case DW_TAG_enumeration_type:
15266 return true;
15267
15268 default:
15269 return false;
15270 }
15271}
15272
15273/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15274
15275static void
883fd55a
KS
15276dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15277 struct dwarf2_cu *cu)
6e70227d 15278{
be2daae6
TT
15279 struct decl_field fp;
15280 memset (&fp, 0, sizeof (fp));
98751a41 15281
883fd55a 15282 gdb_assert (type_can_define_types (die));
98751a41 15283
883fd55a 15284 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15285 fp.name = dwarf2_name (die, cu);
15286 fp.type = read_type_die (die, cu);
98751a41 15287
c191a687
KS
15288 /* Save accessibility. */
15289 enum dwarf_access_attribute accessibility;
15290 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15291 if (attr != NULL)
15292 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15293 else
15294 accessibility = dwarf2_default_access_attribute (die, cu);
15295 switch (accessibility)
15296 {
15297 case DW_ACCESS_public:
15298 /* The assumed value if neither private nor protected. */
15299 break;
15300 case DW_ACCESS_private:
be2daae6 15301 fp.is_private = 1;
c191a687
KS
15302 break;
15303 case DW_ACCESS_protected:
be2daae6 15304 fp.is_protected = 1;
c191a687
KS
15305 break;
15306 default:
b98664d3 15307 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15308 }
15309
883fd55a 15310 if (die->tag == DW_TAG_typedef)
be2daae6 15311 fip->typedef_field_list.push_back (fp);
883fd55a 15312 else
be2daae6 15313 fip->nested_types_list.push_back (fp);
98751a41
JK
15314}
15315
c906108c
SS
15316/* Create the vector of fields, and attach it to the type. */
15317
15318static void
fba45db2 15319dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15320 struct dwarf2_cu *cu)
c906108c
SS
15321{
15322 int nfields = fip->nfields;
15323
15324 /* Record the field count, allocate space for the array of fields,
15325 and create blank accessibility bitfields if necessary. */
15326 TYPE_NFIELDS (type) = nfields;
15327 TYPE_FIELDS (type) = (struct field *)
be2daae6 15328 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15329
b4ba55a1 15330 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15331 {
15332 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15333
15334 TYPE_FIELD_PRIVATE_BITS (type) =
15335 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15336 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15337
15338 TYPE_FIELD_PROTECTED_BITS (type) =
15339 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15340 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15341
774b6a14
TT
15342 TYPE_FIELD_IGNORE_BITS (type) =
15343 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15344 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15345 }
15346
15347 /* If the type has baseclasses, allocate and clear a bit vector for
15348 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15349 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15350 {
be2daae6 15351 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15352 unsigned char *pointer;
c906108c
SS
15353
15354 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15355 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15356 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15357 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15358 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15359 }
15360
2ddeaf8a
TT
15361 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15362 {
15363 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15364
be2daae6 15365 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15366 {
be2daae6
TT
15367 struct nextfield &field = fip->fields[index];
15368
15369 if (field.variant.is_discriminant)
2ddeaf8a 15370 di->discriminant_index = index;
be2daae6 15371 else if (field.variant.default_branch)
2ddeaf8a
TT
15372 di->default_index = index;
15373 else
be2daae6 15374 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15375 }
15376 }
15377
be2daae6
TT
15378 /* Copy the saved-up fields into the field vector. */
15379 for (int i = 0; i < nfields; ++i)
c906108c 15380 {
be2daae6
TT
15381 struct nextfield &field
15382 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15383 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15384
be2daae6
TT
15385 TYPE_FIELD (type, i) = field.field;
15386 switch (field.accessibility)
c906108c 15387 {
c5aa993b 15388 case DW_ACCESS_private:
b4ba55a1 15389 if (cu->language != language_ada)
be2daae6 15390 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15391 break;
c906108c 15392
c5aa993b 15393 case DW_ACCESS_protected:
b4ba55a1 15394 if (cu->language != language_ada)
be2daae6 15395 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15396 break;
c906108c 15397
c5aa993b
JM
15398 case DW_ACCESS_public:
15399 break;
c906108c 15400
c5aa993b
JM
15401 default:
15402 /* Unknown accessibility. Complain and treat it as public. */
15403 {
b98664d3 15404 complaint (_("unsupported accessibility %d"),
be2daae6 15405 field.accessibility);
c5aa993b
JM
15406 }
15407 break;
c906108c 15408 }
be2daae6 15409 if (i < fip->baseclasses.size ())
c906108c 15410 {
be2daae6 15411 switch (field.virtuality)
c906108c 15412 {
c5aa993b
JM
15413 case DW_VIRTUALITY_virtual:
15414 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15415 if (cu->language == language_ada)
a73c6dcd 15416 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15417 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15418 break;
c906108c
SS
15419 }
15420 }
c906108c
SS
15421 }
15422}
15423
7d27a96d
TT
15424/* Return true if this member function is a constructor, false
15425 otherwise. */
15426
15427static int
15428dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15429{
15430 const char *fieldname;
fe978cb0 15431 const char *type_name;
7d27a96d
TT
15432 int len;
15433
15434 if (die->parent == NULL)
15435 return 0;
15436
15437 if (die->parent->tag != DW_TAG_structure_type
15438 && die->parent->tag != DW_TAG_union_type
15439 && die->parent->tag != DW_TAG_class_type)
15440 return 0;
15441
15442 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15443 type_name = dwarf2_name (die->parent, cu);
15444 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15445 return 0;
15446
15447 len = strlen (fieldname);
fe978cb0
PA
15448 return (strncmp (fieldname, type_name, len) == 0
15449 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15450}
15451
e35000a7
TBA
15452/* Check if the given VALUE is a recognized enum
15453 dwarf_defaulted_attribute constant according to DWARF5 spec,
15454 Table 7.24. */
15455
15456static bool
15457is_valid_DW_AT_defaulted (ULONGEST value)
15458{
15459 switch (value)
15460 {
15461 case DW_DEFAULTED_no:
15462 case DW_DEFAULTED_in_class:
15463 case DW_DEFAULTED_out_of_class:
15464 return true;
15465 }
15466
3142e908 15467 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15468 return false;
15469}
15470
c906108c
SS
15471/* Add a member function to the proper fieldlist. */
15472
15473static void
107d2387 15474dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15475 struct type *type, struct dwarf2_cu *cu)
c906108c 15476{
518817b3 15477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15478 struct attribute *attr;
c906108c 15479 int i;
be2daae6 15480 struct fnfieldlist *flp = nullptr;
c906108c 15481 struct fn_field *fnp;
15d034d0 15482 const char *fieldname;
f792889a 15483 struct type *this_type;
60d5a603 15484 enum dwarf_access_attribute accessibility;
c906108c 15485
b4ba55a1 15486 if (cu->language == language_ada)
a73c6dcd 15487 error (_("unexpected member function in Ada type"));
b4ba55a1 15488
2df3850c 15489 /* Get name of member function. */
39cbfefa
DJ
15490 fieldname = dwarf2_name (die, cu);
15491 if (fieldname == NULL)
2df3850c 15492 return;
c906108c 15493
c906108c 15494 /* Look up member function name in fieldlist. */
be2daae6 15495 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15496 {
27bfe10e 15497 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15498 {
15499 flp = &fip->fnfieldlists[i];
15500 break;
15501 }
c906108c
SS
15502 }
15503
be2daae6
TT
15504 /* Create a new fnfieldlist if necessary. */
15505 if (flp == nullptr)
c906108c 15506 {
be2daae6
TT
15507 fip->fnfieldlists.emplace_back ();
15508 flp = &fip->fnfieldlists.back ();
c906108c 15509 flp->name = fieldname;
be2daae6 15510 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15511 }
15512
be2daae6
TT
15513 /* Create a new member function field and add it to the vector of
15514 fnfieldlists. */
15515 flp->fnfields.emplace_back ();
15516 fnp = &flp->fnfields.back ();
3da10d80
KS
15517
15518 /* Delay processing of the physname until later. */
9c37b5ae 15519 if (cu->language == language_cplus)
be2daae6
TT
15520 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15521 die, cu);
3da10d80
KS
15522 else
15523 {
1d06ead6 15524 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15525 fnp->physname = physname ? physname : "";
15526 }
15527
c906108c 15528 fnp->type = alloc_type (objfile);
f792889a
DJ
15529 this_type = read_type_die (die, cu);
15530 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15531 {
f792889a 15532 int nparams = TYPE_NFIELDS (this_type);
c906108c 15533
f792889a 15534 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15535 of the method itself (TYPE_CODE_METHOD). */
15536 smash_to_method_type (fnp->type, type,
f792889a
DJ
15537 TYPE_TARGET_TYPE (this_type),
15538 TYPE_FIELDS (this_type),
15539 TYPE_NFIELDS (this_type),
15540 TYPE_VARARGS (this_type));
c906108c
SS
15541
15542 /* Handle static member functions.
c5aa993b 15543 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15544 member functions. G++ helps GDB by marking the first
15545 parameter for non-static member functions (which is the this
15546 pointer) as artificial. We obtain this information from
15547 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15548 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15549 fnp->voffset = VOFFSET_STATIC;
15550 }
15551 else
b98664d3 15552 complaint (_("member function type missing for '%s'"),
3da10d80 15553 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15554
15555 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15556 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15557 fnp->fcontext = die_containing_type (die, cu);
c906108c 15558
3e43a32a
MS
15559 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15560 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15561
15562 /* Get accessibility. */
e142c38c 15563 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15564 if (attr != nullptr)
aead7601 15565 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15566 else
15567 accessibility = dwarf2_default_access_attribute (die, cu);
15568 switch (accessibility)
c906108c 15569 {
60d5a603
JK
15570 case DW_ACCESS_private:
15571 fnp->is_private = 1;
15572 break;
15573 case DW_ACCESS_protected:
15574 fnp->is_protected = 1;
15575 break;
c906108c
SS
15576 }
15577
b02dede2 15578 /* Check for artificial methods. */
e142c38c 15579 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15580 if (attr && DW_UNSND (attr) != 0)
15581 fnp->is_artificial = 1;
15582
e35000a7
TBA
15583 /* Check for defaulted methods. */
15584 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15585 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15586 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15587
15588 /* Check for deleted methods. */
15589 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15590 if (attr != nullptr && DW_UNSND (attr) != 0)
15591 fnp->is_deleted = 1;
15592
7d27a96d
TT
15593 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15594
0d564a31 15595 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15596 function. For older versions of GCC, this is an offset in the
15597 appropriate virtual table, as specified by DW_AT_containing_type.
15598 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15599 to the object address. */
15600
e142c38c 15601 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15602 if (attr != nullptr)
8e19ed76 15603 {
aec5aa8b 15604 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15605 {
aec5aa8b
TT
15606 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15607 {
15608 /* Old-style GCC. */
15609 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15610 }
15611 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15612 || (DW_BLOCK (attr)->size > 1
15613 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15614 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15615 {
aec5aa8b
TT
15616 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15617 if ((fnp->voffset % cu->header.addr_size) != 0)
15618 dwarf2_complex_location_expr_complaint ();
15619 else
15620 fnp->voffset /= cu->header.addr_size;
15621 fnp->voffset += 2;
15622 }
15623 else
15624 dwarf2_complex_location_expr_complaint ();
15625
15626 if (!fnp->fcontext)
7e993ebf
KS
15627 {
15628 /* If there is no `this' field and no DW_AT_containing_type,
15629 we cannot actually find a base class context for the
15630 vtable! */
15631 if (TYPE_NFIELDS (this_type) == 0
15632 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15633 {
b98664d3 15634 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15635 "function \"%s\" (offset %s)"),
15636 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15637 }
15638 else
15639 {
15640 fnp->fcontext
15641 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15642 }
15643 }
aec5aa8b 15644 }
3690dd37 15645 else if (attr_form_is_section_offset (attr))
8e19ed76 15646 {
4d3c2250 15647 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15648 }
15649 else
15650 {
4d3c2250
KB
15651 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15652 fieldname);
8e19ed76 15653 }
0d564a31 15654 }
d48cc9dd
DJ
15655 else
15656 {
15657 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15658 if (attr && DW_UNSND (attr))
15659 {
15660 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15661 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15662 "but the vtable offset is not specified"),
9d8780f0 15663 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15664 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15665 TYPE_CPLUS_DYNAMIC (type) = 1;
15666 }
15667 }
c906108c
SS
15668}
15669
15670/* Create the vector of member function fields, and attach it to the type. */
15671
15672static void
fba45db2 15673dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15674 struct dwarf2_cu *cu)
c906108c 15675{
b4ba55a1 15676 if (cu->language == language_ada)
a73c6dcd 15677 error (_("unexpected member functions in Ada type"));
b4ba55a1 15678
c906108c
SS
15679 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15680 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15681 TYPE_ALLOC (type,
15682 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15683
be2daae6 15684 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15685 {
be2daae6 15686 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15687 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15688
be2daae6
TT
15689 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15690 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15691 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15692 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15693
15694 for (int k = 0; k < nf.fnfields.size (); ++k)
15695 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15696 }
15697
be2daae6 15698 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15699}
15700
1168df01
JB
15701/* Returns non-zero if NAME is the name of a vtable member in CU's
15702 language, zero otherwise. */
15703static int
15704is_vtable_name (const char *name, struct dwarf2_cu *cu)
15705{
15706 static const char vptr[] = "_vptr";
15707
9c37b5ae
TT
15708 /* Look for the C++ form of the vtable. */
15709 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15710 return 1;
15711
15712 return 0;
15713}
15714
c0dd20ea 15715/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15716 functions, with the ABI-specified layout. If TYPE describes
15717 such a structure, smash it into a member function type.
61049d3b
DJ
15718
15719 GCC shouldn't do this; it should just output pointer to member DIEs.
15720 This is GCC PR debug/28767. */
c0dd20ea 15721
0b92b5bb
TT
15722static void
15723quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15724{
09e2d7c7 15725 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15726
15727 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15728 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15729 return;
c0dd20ea
DJ
15730
15731 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15732 if (TYPE_FIELD_NAME (type, 0) == NULL
15733 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15734 || TYPE_FIELD_NAME (type, 1) == NULL
15735 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15736 return;
c0dd20ea
DJ
15737
15738 /* Find the type of the method. */
0b92b5bb 15739 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15740 if (pfn_type == NULL
15741 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15742 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15743 return;
c0dd20ea
DJ
15744
15745 /* Look for the "this" argument. */
15746 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15747 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15748 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15749 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15750 return;
c0dd20ea 15751
09e2d7c7 15752 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15753 new_type = alloc_type (objfile);
09e2d7c7 15754 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15755 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15756 TYPE_VARARGS (pfn_type));
0b92b5bb 15757 smash_to_methodptr_type (type, new_type);
c0dd20ea 15758}
1168df01 15759
2b4424c3
TT
15760/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15761 appropriate error checking and issuing complaints if there is a
15762 problem. */
15763
15764static ULONGEST
15765get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15766{
15767 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15768
15769 if (attr == nullptr)
15770 return 0;
15771
15772 if (!attr_form_is_constant (attr))
15773 {
b98664d3 15774 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15775 " - DIE at %s [in module %s]"),
15776 sect_offset_str (die->sect_off),
15777 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15778 return 0;
15779 }
15780
15781 ULONGEST align;
15782 if (attr->form == DW_FORM_sdata)
15783 {
15784 LONGEST val = DW_SND (attr);
15785 if (val < 0)
15786 {
b98664d3 15787 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15788 " - DIE at %s [in module %s]"),
15789 sect_offset_str (die->sect_off),
15790 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15791 return 0;
15792 }
15793 align = val;
15794 }
15795 else
15796 align = DW_UNSND (attr);
15797
15798 if (align == 0)
15799 {
b98664d3 15800 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15801 " - DIE at %s [in module %s]"),
15802 sect_offset_str (die->sect_off),
15803 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15804 return 0;
15805 }
15806 if ((align & (align - 1)) != 0)
15807 {
b98664d3 15808 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15809 " - DIE at %s [in module %s]"),
15810 sect_offset_str (die->sect_off),
15811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15812 return 0;
15813 }
15814
15815 return align;
15816}
15817
15818/* If the DIE has a DW_AT_alignment attribute, use its value to set
15819 the alignment for TYPE. */
15820
15821static void
15822maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15823 struct type *type)
15824{
15825 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15826 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15827 " - DIE at %s [in module %s]"),
15828 sect_offset_str (die->sect_off),
15829 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15830}
685b1105 15831
e35000a7
TBA
15832/* Check if the given VALUE is a valid enum dwarf_calling_convention
15833 constant for a type, according to DWARF5 spec, Table 5.5. */
15834
15835static bool
15836is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15837{
15838 switch (value)
15839 {
15840 case DW_CC_normal:
15841 case DW_CC_pass_by_reference:
15842 case DW_CC_pass_by_value:
15843 return true;
15844
15845 default:
15846 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15847 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15848 return false;
15849 }
15850}
15851
d0922fcf
TBA
15852/* Check if the given VALUE is a valid enum dwarf_calling_convention
15853 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15854 also according to GNU-specific values (see include/dwarf2.h). */
15855
15856static bool
15857is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15858{
15859 switch (value)
15860 {
15861 case DW_CC_normal:
15862 case DW_CC_program:
15863 case DW_CC_nocall:
15864 return true;
15865
15866 case DW_CC_GNU_renesas_sh:
15867 case DW_CC_GNU_borland_fastcall_i386:
15868 case DW_CC_GDB_IBM_OpenCL:
15869 return true;
15870
15871 default:
15872 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15873 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15874 return false;
15875 }
15876}
15877
c906108c 15878/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15879 (definition) to create a type for the structure or union. Fill in
15880 the type's name and general properties; the members will not be
83655187
DE
15881 processed until process_structure_scope. A symbol table entry for
15882 the type will also not be done until process_structure_scope (assuming
15883 the type has a name).
c906108c 15884
c767944b
DJ
15885 NOTE: we need to call these functions regardless of whether or not the
15886 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15887 structure or union. This gets the type entered into our set of
83655187 15888 user defined types. */
c906108c 15889
f792889a 15890static struct type *
134d01f1 15891read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15892{
518817b3 15893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15894 struct type *type;
15895 struct attribute *attr;
15d034d0 15896 const char *name;
c906108c 15897
348e048f
DE
15898 /* If the definition of this type lives in .debug_types, read that type.
15899 Don't follow DW_AT_specification though, that will take us back up
15900 the chain and we want to go down. */
45e58e77 15901 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15902 if (attr != nullptr)
348e048f 15903 {
ac9ec31b 15904 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15905
ac9ec31b 15906 /* The type's CU may not be the same as CU.
02142a6c 15907 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15908 return set_die_type (die, type, cu);
15909 }
15910
c0dd20ea 15911 type = alloc_type (objfile);
c906108c 15912 INIT_CPLUS_SPECIFIC (type);
93311388 15913
39cbfefa
DJ
15914 name = dwarf2_name (die, cu);
15915 if (name != NULL)
c906108c 15916 {
987504bb 15917 if (cu->language == language_cplus
c44af4eb
TT
15918 || cu->language == language_d
15919 || cu->language == language_rust)
63d06c5c 15920 {
15d034d0 15921 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15922
15923 /* dwarf2_full_name might have already finished building the DIE's
15924 type. If so, there is no need to continue. */
15925 if (get_die_type (die, cu) != NULL)
15926 return get_die_type (die, cu);
15927
e86ca25f 15928 TYPE_NAME (type) = full_name;
63d06c5c
DC
15929 }
15930 else
15931 {
d8151005
DJ
15932 /* The name is already allocated along with this objfile, so
15933 we don't need to duplicate it for the type. */
e86ca25f 15934 TYPE_NAME (type) = name;
63d06c5c 15935 }
c906108c
SS
15936 }
15937
15938 if (die->tag == DW_TAG_structure_type)
15939 {
15940 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15941 }
15942 else if (die->tag == DW_TAG_union_type)
15943 {
15944 TYPE_CODE (type) = TYPE_CODE_UNION;
15945 }
2ddeaf8a
TT
15946 else if (die->tag == DW_TAG_variant_part)
15947 {
15948 TYPE_CODE (type) = TYPE_CODE_UNION;
15949 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15950 }
c906108c
SS
15951 else
15952 {
4753d33b 15953 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15954 }
15955
0cc2414c
TT
15956 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15957 TYPE_DECLARED_CLASS (type) = 1;
15958
e35000a7
TBA
15959 /* Store the calling convention in the type if it's available in
15960 the die. Otherwise the calling convention remains set to
15961 the default value DW_CC_normal. */
15962 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15963 if (attr != nullptr
15964 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15965 {
15966 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15967 TYPE_CPLUS_CALLING_CONVENTION (type)
15968 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15969 }
15970
e142c38c 15971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15972 if (attr != nullptr)
c906108c 15973 {
155bfbd3
JB
15974 if (attr_form_is_constant (attr))
15975 TYPE_LENGTH (type) = DW_UNSND (attr);
15976 else
15977 {
15978 /* For the moment, dynamic type sizes are not supported
15979 by GDB's struct type. The actual size is determined
15980 on-demand when resolving the type of a given object,
15981 so set the type's length to zero for now. Otherwise,
15982 we record an expression as the length, and that expression
15983 could lead to a very large value, which could eventually
15984 lead to us trying to allocate that much memory when creating
15985 a value of that type. */
15986 TYPE_LENGTH (type) = 0;
15987 }
c906108c
SS
15988 }
15989 else
15990 {
15991 TYPE_LENGTH (type) = 0;
15992 }
15993
2b4424c3
TT
15994 maybe_set_alignment (cu, die, type);
15995
5230b05a 15996 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15997 {
5230b05a
WT
15998 /* ICC<14 does not output the required DW_AT_declaration on
15999 incomplete types, but gives them a size of zero. */
422b1cb0 16000 TYPE_STUB (type) = 1;
685b1105
JK
16001 }
16002 else
16003 TYPE_STUB_SUPPORTED (type) = 1;
16004
dc718098 16005 if (die_is_declaration (die, cu))
876cecd0 16006 TYPE_STUB (type) = 1;
a6c727b2
DJ
16007 else if (attr == NULL && die->child == NULL
16008 && producer_is_realview (cu->producer))
16009 /* RealView does not output the required DW_AT_declaration
16010 on incomplete types. */
16011 TYPE_STUB (type) = 1;
dc718098 16012
c906108c
SS
16013 /* We need to add the type field to the die immediately so we don't
16014 infinitely recurse when dealing with pointers to the structure
0963b4bd 16015 type within the structure itself. */
1c379e20 16016 set_die_type (die, type, cu);
c906108c 16017
7e314c57
JK
16018 /* set_die_type should be already done. */
16019 set_descriptive_type (type, die, cu);
16020
c767944b
DJ
16021 return type;
16022}
16023
2ddeaf8a
TT
16024/* A helper for process_structure_scope that handles a single member
16025 DIE. */
16026
16027static void
16028handle_struct_member_die (struct die_info *child_die, struct type *type,
16029 struct field_info *fi,
16030 std::vector<struct symbol *> *template_args,
16031 struct dwarf2_cu *cu)
16032{
16033 if (child_die->tag == DW_TAG_member
16034 || child_die->tag == DW_TAG_variable
16035 || child_die->tag == DW_TAG_variant_part)
16036 {
16037 /* NOTE: carlton/2002-11-05: A C++ static data member
16038 should be a DW_TAG_member that is a declaration, but
16039 all versions of G++ as of this writing (so through at
16040 least 3.2.1) incorrectly generate DW_TAG_variable
16041 tags for them instead. */
16042 dwarf2_add_field (fi, child_die, cu);
16043 }
16044 else if (child_die->tag == DW_TAG_subprogram)
16045 {
16046 /* Rust doesn't have member functions in the C++ sense.
16047 However, it does emit ordinary functions as children
16048 of a struct DIE. */
16049 if (cu->language == language_rust)
16050 read_func_scope (child_die, cu);
16051 else
16052 {
16053 /* C++ member function. */
16054 dwarf2_add_member_fn (fi, child_die, type, cu);
16055 }
16056 }
16057 else if (child_die->tag == DW_TAG_inheritance)
16058 {
16059 /* C++ base class field. */
16060 dwarf2_add_field (fi, child_die, cu);
16061 }
16062 else if (type_can_define_types (child_die))
16063 dwarf2_add_type_defn (fi, child_die, cu);
16064 else if (child_die->tag == DW_TAG_template_type_param
16065 || child_die->tag == DW_TAG_template_value_param)
16066 {
16067 struct symbol *arg = new_symbol (child_die, NULL, cu);
16068
16069 if (arg != NULL)
16070 template_args->push_back (arg);
16071 }
16072 else if (child_die->tag == DW_TAG_variant)
16073 {
16074 /* In a variant we want to get the discriminant and also add a
16075 field for our sole member child. */
16076 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16077
bde09ab7 16078 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16079 variant_child != NULL;
16080 variant_child = sibling_die (variant_child))
16081 {
16082 if (variant_child->tag == DW_TAG_member)
16083 {
16084 handle_struct_member_die (variant_child, type, fi,
16085 template_args, cu);
16086 /* Only handle the one. */
16087 break;
16088 }
16089 }
16090
16091 /* We don't handle this but we might as well report it if we see
16092 it. */
16093 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16094 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16095 " - DIE at %s [in module %s]"),
16096 sect_offset_str (child_die->sect_off),
16097 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16098
16099 /* The first field was just added, so we can stash the
16100 discriminant there. */
be2daae6 16101 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16102 if (discr == NULL)
be2daae6 16103 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16104 else
be2daae6 16105 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16106 }
16107}
16108
c767944b
DJ
16109/* Finish creating a structure or union type, including filling in
16110 its members and creating a symbol for it. */
16111
16112static void
16113process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16114{
518817b3 16115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16116 struct die_info *child_die;
c767944b
DJ
16117 struct type *type;
16118
16119 type = get_die_type (die, cu);
16120 if (type == NULL)
16121 type = read_structure_type (die, cu);
16122
2ddeaf8a
TT
16123 /* When reading a DW_TAG_variant_part, we need to notice when we
16124 read the discriminant member, so we can record it later in the
16125 discriminant_info. */
16126 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16127 sect_offset discr_offset {};
3e1d3d8c 16128 bool has_template_parameters = false;
2ddeaf8a
TT
16129
16130 if (is_variant_part)
16131 {
16132 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16133 if (discr == NULL)
16134 {
16135 /* Maybe it's a univariant form, an extension we support.
16136 In this case arrange not to check the offset. */
16137 is_variant_part = false;
16138 }
16139 else if (attr_form_is_ref (discr))
16140 {
16141 struct dwarf2_cu *target_cu = cu;
16142 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16143
16144 discr_offset = target_die->sect_off;
16145 }
16146 else
16147 {
b98664d3 16148 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16149 " - DIE at %s [in module %s]"),
16150 sect_offset_str (die->sect_off),
16151 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16152 is_variant_part = false;
16153 }
16154 }
16155
e142c38c 16156 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16157 {
16158 struct field_info fi;
2f4732b0 16159 std::vector<struct symbol *> template_args;
c906108c 16160
639d11d3 16161 child_die = die->child;
c906108c
SS
16162
16163 while (child_die && child_die->tag)
16164 {
2ddeaf8a 16165 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16166
2ddeaf8a 16167 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16168 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16169
c906108c
SS
16170 child_die = sibling_die (child_die);
16171 }
16172
34eaf542 16173 /* Attach template arguments to type. */
2f4732b0 16174 if (!template_args.empty ())
34eaf542 16175 {
3e1d3d8c 16176 has_template_parameters = true;
34eaf542 16177 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16178 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16179 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16180 = XOBNEWVEC (&objfile->objfile_obstack,
16181 struct symbol *,
16182 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16183 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16184 template_args.data (),
34eaf542
TT
16185 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16186 * sizeof (struct symbol *)));
34eaf542
TT
16187 }
16188
c906108c
SS
16189 /* Attach fields and member functions to the type. */
16190 if (fi.nfields)
e7c27a73 16191 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16192 if (!fi.fnfieldlists.empty ())
c906108c 16193 {
e7c27a73 16194 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16195
c5aa993b 16196 /* Get the type which refers to the base class (possibly this
c906108c 16197 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16198 class from the DW_AT_containing_type attribute. This use of
16199 DW_AT_containing_type is a GNU extension. */
c906108c 16200
e142c38c 16201 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16202 {
e7c27a73 16203 struct type *t = die_containing_type (die, cu);
c906108c 16204
ae6ae975 16205 set_type_vptr_basetype (type, t);
c906108c
SS
16206 if (type == t)
16207 {
c906108c
SS
16208 int i;
16209
16210 /* Our own class provides vtbl ptr. */
16211 for (i = TYPE_NFIELDS (t) - 1;
16212 i >= TYPE_N_BASECLASSES (t);
16213 --i)
16214 {
0d5cff50 16215 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16216
1168df01 16217 if (is_vtable_name (fieldname, cu))
c906108c 16218 {
ae6ae975 16219 set_type_vptr_fieldno (type, i);
c906108c
SS
16220 break;
16221 }
16222 }
16223
16224 /* Complain if virtual function table field not found. */
16225 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16226 complaint (_("virtual function table pointer "
3e43a32a 16227 "not found when defining class '%s'"),
e86ca25f 16228 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16229 }
16230 else
16231 {
ae6ae975 16232 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16233 }
16234 }
f6235d4c 16235 else if (cu->producer
61012eef 16236 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16237 {
16238 /* The IBM XLC compiler does not provide direct indication
16239 of the containing type, but the vtable pointer is
16240 always named __vfp. */
16241
16242 int i;
16243
16244 for (i = TYPE_NFIELDS (type) - 1;
16245 i >= TYPE_N_BASECLASSES (type);
16246 --i)
16247 {
16248 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16249 {
ae6ae975
DE
16250 set_type_vptr_fieldno (type, i);
16251 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16252 break;
16253 }
16254 }
16255 }
c906108c 16256 }
98751a41
JK
16257
16258 /* Copy fi.typedef_field_list linked list elements content into the
16259 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16260 if (!fi.typedef_field_list.empty ())
98751a41 16261 {
be2daae6 16262 int count = fi.typedef_field_list.size ();
98751a41 16263
a0d7a4ff 16264 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16265 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16266 = ((struct decl_field *)
be2daae6
TT
16267 TYPE_ALLOC (type,
16268 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16269 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16270
be2daae6
TT
16271 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16272 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16273 }
c767944b 16274
883fd55a
KS
16275 /* Copy fi.nested_types_list linked list elements content into the
16276 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16277 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16278 {
be2daae6 16279 int count = fi.nested_types_list.size ();
883fd55a
KS
16280
16281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16282 TYPE_NESTED_TYPES_ARRAY (type)
16283 = ((struct decl_field *)
be2daae6
TT
16284 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16285 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16286
be2daae6
TT
16287 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16288 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16289 }
c906108c 16290 }
63d06c5c 16291
bb5ed363 16292 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16293 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16294 cu->rust_unions.push_back (type);
0b92b5bb 16295
90aeadfc
DC
16296 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16297 snapshots) has been known to create a die giving a declaration
16298 for a class that has, as a child, a die giving a definition for a
16299 nested class. So we have to process our children even if the
16300 current die is a declaration. Normally, of course, a declaration
16301 won't have any children at all. */
134d01f1 16302
ca040673
DE
16303 child_die = die->child;
16304
90aeadfc
DC
16305 while (child_die != NULL && child_die->tag)
16306 {
16307 if (child_die->tag == DW_TAG_member
16308 || child_die->tag == DW_TAG_variable
34eaf542
TT
16309 || child_die->tag == DW_TAG_inheritance
16310 || child_die->tag == DW_TAG_template_value_param
16311 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16312 {
90aeadfc 16313 /* Do nothing. */
134d01f1 16314 }
90aeadfc
DC
16315 else
16316 process_die (child_die, cu);
134d01f1 16317
90aeadfc 16318 child_die = sibling_die (child_die);
134d01f1
DJ
16319 }
16320
fa4028e9
JB
16321 /* Do not consider external references. According to the DWARF standard,
16322 these DIEs are identified by the fact that they have no byte_size
16323 attribute, and a declaration attribute. */
16324 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16325 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16326 {
16327 struct symbol *sym = new_symbol (die, type, cu);
16328
16329 if (has_template_parameters)
16330 {
a776957c
TT
16331 struct symtab *symtab;
16332 if (sym != nullptr)
16333 symtab = symbol_symtab (sym);
16334 else if (cu->line_header != nullptr)
16335 {
16336 /* Any related symtab will do. */
16337 symtab
7ba99d21 16338 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16339 }
16340 else
16341 {
16342 symtab = nullptr;
16343 complaint (_("could not find suitable "
16344 "symtab for template parameter"
16345 " - DIE at %s [in module %s]"),
16346 sect_offset_str (die->sect_off),
16347 objfile_name (objfile));
16348 }
16349
16350 if (symtab != nullptr)
16351 {
16352 /* Make sure that the symtab is set on the new symbols.
16353 Even though they don't appear in this symtab directly,
16354 other parts of gdb assume that symbols do, and this is
16355 reasonably true. */
16356 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16357 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16358 }
3e1d3d8c
TT
16359 }
16360 }
134d01f1
DJ
16361}
16362
55426c9d
JB
16363/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16364 update TYPE using some information only available in DIE's children. */
16365
16366static void
16367update_enumeration_type_from_children (struct die_info *die,
16368 struct type *type,
16369 struct dwarf2_cu *cu)
16370{
60f7655a 16371 struct die_info *child_die;
55426c9d
JB
16372 int unsigned_enum = 1;
16373 int flag_enum = 1;
16374 ULONGEST mask = 0;
55426c9d 16375
8268c778 16376 auto_obstack obstack;
55426c9d 16377
60f7655a
DE
16378 for (child_die = die->child;
16379 child_die != NULL && child_die->tag;
16380 child_die = sibling_die (child_die))
55426c9d
JB
16381 {
16382 struct attribute *attr;
16383 LONGEST value;
16384 const gdb_byte *bytes;
16385 struct dwarf2_locexpr_baton *baton;
16386 const char *name;
60f7655a 16387
55426c9d
JB
16388 if (child_die->tag != DW_TAG_enumerator)
16389 continue;
16390
16391 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16392 if (attr == NULL)
16393 continue;
16394
16395 name = dwarf2_name (child_die, cu);
16396 if (name == NULL)
16397 name = "<anonymous enumerator>";
16398
16399 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16400 &value, &bytes, &baton);
16401 if (value < 0)
16402 {
16403 unsigned_enum = 0;
16404 flag_enum = 0;
16405 }
16406 else if ((mask & value) != 0)
16407 flag_enum = 0;
16408 else
16409 mask |= value;
16410
16411 /* If we already know that the enum type is neither unsigned, nor
16412 a flag type, no need to look at the rest of the enumerates. */
16413 if (!unsigned_enum && !flag_enum)
16414 break;
55426c9d
JB
16415 }
16416
16417 if (unsigned_enum)
16418 TYPE_UNSIGNED (type) = 1;
16419 if (flag_enum)
16420 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16421}
16422
134d01f1
DJ
16423/* Given a DW_AT_enumeration_type die, set its type. We do not
16424 complete the type's fields yet, or create any symbols. */
c906108c 16425
f792889a 16426static struct type *
134d01f1 16427read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16428{
518817b3 16429 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16430 struct type *type;
c906108c 16431 struct attribute *attr;
0114d602 16432 const char *name;
134d01f1 16433
348e048f
DE
16434 /* If the definition of this type lives in .debug_types, read that type.
16435 Don't follow DW_AT_specification though, that will take us back up
16436 the chain and we want to go down. */
45e58e77 16437 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16438 if (attr != nullptr)
348e048f 16439 {
ac9ec31b 16440 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16441
ac9ec31b 16442 /* The type's CU may not be the same as CU.
02142a6c 16443 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16444 return set_die_type (die, type, cu);
16445 }
16446
c906108c
SS
16447 type = alloc_type (objfile);
16448
16449 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16450 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16451 if (name != NULL)
e86ca25f 16452 TYPE_NAME (type) = name;
c906108c 16453
0626fc76
TT
16454 attr = dwarf2_attr (die, DW_AT_type, cu);
16455 if (attr != NULL)
16456 {
16457 struct type *underlying_type = die_type (die, cu);
16458
16459 TYPE_TARGET_TYPE (type) = underlying_type;
16460 }
16461
e142c38c 16462 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16463 if (attr != nullptr)
c906108c
SS
16464 {
16465 TYPE_LENGTH (type) = DW_UNSND (attr);
16466 }
16467 else
16468 {
16469 TYPE_LENGTH (type) = 0;
16470 }
16471
2b4424c3
TT
16472 maybe_set_alignment (cu, die, type);
16473
137033e9
JB
16474 /* The enumeration DIE can be incomplete. In Ada, any type can be
16475 declared as private in the package spec, and then defined only
16476 inside the package body. Such types are known as Taft Amendment
16477 Types. When another package uses such a type, an incomplete DIE
16478 may be generated by the compiler. */
02eb380e 16479 if (die_is_declaration (die, cu))
876cecd0 16480 TYPE_STUB (type) = 1;
02eb380e 16481
0626fc76
TT
16482 /* Finish the creation of this type by using the enum's children.
16483 We must call this even when the underlying type has been provided
16484 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16485 update_enumeration_type_from_children (die, type, cu);
16486
0626fc76
TT
16487 /* If this type has an underlying type that is not a stub, then we
16488 may use its attributes. We always use the "unsigned" attribute
16489 in this situation, because ordinarily we guess whether the type
16490 is unsigned -- but the guess can be wrong and the underlying type
16491 can tell us the reality. However, we defer to a local size
16492 attribute if one exists, because this lets the compiler override
16493 the underlying type if needed. */
16494 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16495 {
16496 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16497 if (TYPE_LENGTH (type) == 0)
16498 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16499 if (TYPE_RAW_ALIGN (type) == 0
16500 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16501 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16502 }
16503
3d567982
TT
16504 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16505
f792889a 16506 return set_die_type (die, type, cu);
134d01f1
DJ
16507}
16508
16509/* Given a pointer to a die which begins an enumeration, process all
16510 the dies that define the members of the enumeration, and create the
16511 symbol for the enumeration type.
16512
16513 NOTE: We reverse the order of the element list. */
16514
16515static void
16516process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16517{
f792889a 16518 struct type *this_type;
134d01f1 16519
f792889a
DJ
16520 this_type = get_die_type (die, cu);
16521 if (this_type == NULL)
16522 this_type = read_enumeration_type (die, cu);
9dc481d3 16523
639d11d3 16524 if (die->child != NULL)
c906108c 16525 {
9dc481d3
DE
16526 struct die_info *child_die;
16527 struct symbol *sym;
43816ebc 16528 std::vector<struct field> fields;
15d034d0 16529 const char *name;
9dc481d3 16530
639d11d3 16531 child_die = die->child;
c906108c
SS
16532 while (child_die && child_die->tag)
16533 {
16534 if (child_die->tag != DW_TAG_enumerator)
16535 {
e7c27a73 16536 process_die (child_die, cu);
c906108c
SS
16537 }
16538 else
16539 {
39cbfefa
DJ
16540 name = dwarf2_name (child_die, cu);
16541 if (name)
c906108c 16542 {
f792889a 16543 sym = new_symbol (child_die, this_type, cu);
c906108c 16544
43816ebc
TT
16545 fields.emplace_back ();
16546 struct field &field = fields.back ();
c906108c 16547
43816ebc
TT
16548 FIELD_NAME (field) = sym->linkage_name ();
16549 FIELD_TYPE (field) = NULL;
16550 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16551 FIELD_BITSIZE (field) = 0;
c906108c
SS
16552 }
16553 }
16554
16555 child_die = sibling_die (child_die);
16556 }
16557
43816ebc 16558 if (!fields.empty ())
c906108c 16559 {
43816ebc 16560 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16561 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16562 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16563 memcpy (TYPE_FIELDS (this_type), fields.data (),
16564 sizeof (struct field) * fields.size ());
c906108c 16565 }
c906108c 16566 }
134d01f1 16567
6c83ed52
TT
16568 /* If we are reading an enum from a .debug_types unit, and the enum
16569 is a declaration, and the enum is not the signatured type in the
16570 unit, then we do not want to add a symbol for it. Adding a
16571 symbol would in some cases obscure the true definition of the
16572 enum, giving users an incomplete type when the definition is
16573 actually available. Note that we do not want to do this for all
16574 enums which are just declarations, because C++0x allows forward
16575 enum declarations. */
3019eac3 16576 if (cu->per_cu->is_debug_types
6c83ed52
TT
16577 && die_is_declaration (die, cu))
16578 {
52dc124a 16579 struct signatured_type *sig_type;
6c83ed52 16580
c0f78cd4 16581 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16582 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16583 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16584 return;
16585 }
16586
f792889a 16587 new_symbol (die, this_type, cu);
c906108c
SS
16588}
16589
16590/* Extract all information from a DW_TAG_array_type DIE and put it in
16591 the DIE's type field. For now, this only handles one dimensional
16592 arrays. */
16593
f792889a 16594static struct type *
e7c27a73 16595read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16596{
518817b3 16597 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16598 struct die_info *child_die;
7e314c57 16599 struct type *type;
c906108c 16600 struct type *element_type, *range_type, *index_type;
c906108c 16601 struct attribute *attr;
15d034d0 16602 const char *name;
a405673c 16603 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16604 unsigned int bit_stride = 0;
c906108c 16605
e7c27a73 16606 element_type = die_type (die, cu);
c906108c 16607
7e314c57
JK
16608 /* The die_type call above may have already set the type for this DIE. */
16609 type = get_die_type (die, cu);
16610 if (type)
16611 return type;
16612
dc53a7ad
JB
16613 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16614 if (attr != NULL)
a405673c
JB
16615 {
16616 int stride_ok;
9a49df9d
AB
16617 struct type *prop_type
16618 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16619
16620 byte_stride_prop
16621 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16622 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16623 prop_type);
a405673c
JB
16624 if (!stride_ok)
16625 {
b98664d3 16626 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16627 " - DIE at %s [in module %s]"),
16628 sect_offset_str (die->sect_off),
518817b3 16629 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16630 /* Ignore this attribute. We will likely not be able to print
16631 arrays of this type correctly, but there is little we can do
16632 to help if we cannot read the attribute's value. */
16633 byte_stride_prop = NULL;
16634 }
16635 }
dc53a7ad
JB
16636
16637 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16638 if (attr != NULL)
16639 bit_stride = DW_UNSND (attr);
16640
c906108c
SS
16641 /* Irix 6.2 native cc creates array types without children for
16642 arrays with unspecified length. */
639d11d3 16643 if (die->child == NULL)
c906108c 16644 {
46bf5051 16645 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16646 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16647 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16648 byte_stride_prop, bit_stride);
f792889a 16649 return set_die_type (die, type, cu);
c906108c
SS
16650 }
16651
791afaa2 16652 std::vector<struct type *> range_types;
639d11d3 16653 child_die = die->child;
c906108c
SS
16654 while (child_die && child_die->tag)
16655 {
16656 if (child_die->tag == DW_TAG_subrange_type)
16657 {
f792889a 16658 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16659
f792889a 16660 if (child_type != NULL)
a02abb62 16661 {
0963b4bd
MS
16662 /* The range type was succesfully read. Save it for the
16663 array type creation. */
791afaa2 16664 range_types.push_back (child_type);
a02abb62 16665 }
c906108c
SS
16666 }
16667 child_die = sibling_die (child_die);
16668 }
16669
16670 /* Dwarf2 dimensions are output from left to right, create the
16671 necessary array types in backwards order. */
7ca2d3a3 16672
c906108c 16673 type = element_type;
7ca2d3a3
DL
16674
16675 if (read_array_order (die, cu) == DW_ORD_col_major)
16676 {
16677 int i = 0;
9a619af0 16678
791afaa2 16679 while (i < range_types.size ())
dc53a7ad 16680 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16681 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16682 }
16683 else
16684 {
791afaa2 16685 size_t ndim = range_types.size ();
7ca2d3a3 16686 while (ndim-- > 0)
dc53a7ad 16687 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16688 byte_stride_prop, bit_stride);
7ca2d3a3 16689 }
c906108c 16690
f5f8a009
EZ
16691 /* Understand Dwarf2 support for vector types (like they occur on
16692 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16693 array type. This is not part of the Dwarf2/3 standard yet, but a
16694 custom vendor extension. The main difference between a regular
16695 array and the vector variant is that vectors are passed by value
16696 to functions. */
e142c38c 16697 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16698 if (attr != nullptr)
ea37ba09 16699 make_vector_type (type);
f5f8a009 16700
dbc98a8b
KW
16701 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16702 implementation may choose to implement triple vectors using this
16703 attribute. */
16704 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16705 if (attr != nullptr)
dbc98a8b
KW
16706 {
16707 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16708 TYPE_LENGTH (type) = DW_UNSND (attr);
16709 else
b98664d3 16710 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16711 "than the total size of elements"));
dbc98a8b
KW
16712 }
16713
39cbfefa
DJ
16714 name = dwarf2_name (die, cu);
16715 if (name)
16716 TYPE_NAME (type) = name;
6e70227d 16717
2b4424c3
TT
16718 maybe_set_alignment (cu, die, type);
16719
0963b4bd 16720 /* Install the type in the die. */
7e314c57
JK
16721 set_die_type (die, type, cu);
16722
16723 /* set_die_type should be already done. */
b4ba55a1
JB
16724 set_descriptive_type (type, die, cu);
16725
7e314c57 16726 return type;
c906108c
SS
16727}
16728
7ca2d3a3 16729static enum dwarf_array_dim_ordering
6e70227d 16730read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16731{
16732 struct attribute *attr;
16733
16734 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16735
435d3d88 16736 if (attr != nullptr)
aead7601 16737 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16738
0963b4bd
MS
16739 /* GNU F77 is a special case, as at 08/2004 array type info is the
16740 opposite order to the dwarf2 specification, but data is still
16741 laid out as per normal fortran.
7ca2d3a3 16742
0963b4bd
MS
16743 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16744 version checking. */
7ca2d3a3 16745
905e0470
PM
16746 if (cu->language == language_fortran
16747 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16748 {
16749 return DW_ORD_row_major;
16750 }
16751
6e70227d 16752 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16753 {
16754 case array_column_major:
16755 return DW_ORD_col_major;
16756 case array_row_major:
16757 default:
16758 return DW_ORD_row_major;
16759 };
16760}
16761
72019c9c 16762/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16763 the DIE's type field. */
72019c9c 16764
f792889a 16765static struct type *
72019c9c
GM
16766read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16767{
7e314c57
JK
16768 struct type *domain_type, *set_type;
16769 struct attribute *attr;
f792889a 16770
7e314c57
JK
16771 domain_type = die_type (die, cu);
16772
16773 /* The die_type call above may have already set the type for this DIE. */
16774 set_type = get_die_type (die, cu);
16775 if (set_type)
16776 return set_type;
16777
16778 set_type = create_set_type (NULL, domain_type);
16779
16780 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16781 if (attr != nullptr)
d09039dd 16782 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16783
2b4424c3
TT
16784 maybe_set_alignment (cu, die, set_type);
16785
f792889a 16786 return set_die_type (die, set_type, cu);
72019c9c 16787}
7ca2d3a3 16788
0971de02
TT
16789/* A helper for read_common_block that creates a locexpr baton.
16790 SYM is the symbol which we are marking as computed.
16791 COMMON_DIE is the DIE for the common block.
16792 COMMON_LOC is the location expression attribute for the common
16793 block itself.
16794 MEMBER_LOC is the location expression attribute for the particular
16795 member of the common block that we are processing.
16796 CU is the CU from which the above come. */
16797
16798static void
16799mark_common_block_symbol_computed (struct symbol *sym,
16800 struct die_info *common_die,
16801 struct attribute *common_loc,
16802 struct attribute *member_loc,
16803 struct dwarf2_cu *cu)
16804{
518817b3
SM
16805 struct dwarf2_per_objfile *dwarf2_per_objfile
16806 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16807 struct objfile *objfile = dwarf2_per_objfile->objfile;
16808 struct dwarf2_locexpr_baton *baton;
16809 gdb_byte *ptr;
16810 unsigned int cu_off;
16811 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16812 LONGEST offset = 0;
16813
16814 gdb_assert (common_loc && member_loc);
16815 gdb_assert (attr_form_is_block (common_loc));
16816 gdb_assert (attr_form_is_block (member_loc)
16817 || attr_form_is_constant (member_loc));
16818
8d749320 16819 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16820 baton->per_cu = cu->per_cu;
16821 gdb_assert (baton->per_cu);
16822
16823 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16824
16825 if (attr_form_is_constant (member_loc))
16826 {
16827 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16828 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16829 }
16830 else
16831 baton->size += DW_BLOCK (member_loc)->size;
16832
224c3ddb 16833 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16834 baton->data = ptr;
16835
16836 *ptr++ = DW_OP_call4;
9c541725 16837 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16838 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16839 ptr += 4;
16840
16841 if (attr_form_is_constant (member_loc))
16842 {
16843 *ptr++ = DW_OP_addr;
16844 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16845 ptr += cu->header.addr_size;
16846 }
16847 else
16848 {
16849 /* We have to copy the data here, because DW_OP_call4 will only
16850 use a DW_AT_location attribute. */
16851 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16852 ptr += DW_BLOCK (member_loc)->size;
16853 }
16854
16855 *ptr++ = DW_OP_plus;
16856 gdb_assert (ptr - baton->data == baton->size);
16857
0971de02 16858 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16859 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16860}
16861
4357ac6c
TT
16862/* Create appropriate locally-scoped variables for all the
16863 DW_TAG_common_block entries. Also create a struct common_block
16864 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16865 is used to separate the common blocks name namespace from regular
4357ac6c 16866 variable names. */
c906108c
SS
16867
16868static void
e7c27a73 16869read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16870{
0971de02
TT
16871 struct attribute *attr;
16872
16873 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16874 if (attr != nullptr)
0971de02
TT
16875 {
16876 /* Support the .debug_loc offsets. */
16877 if (attr_form_is_block (attr))
16878 {
16879 /* Ok. */
16880 }
16881 else if (attr_form_is_section_offset (attr))
16882 {
16883 dwarf2_complex_location_expr_complaint ();
16884 attr = NULL;
16885 }
16886 else
16887 {
16888 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16889 "common block member");
16890 attr = NULL;
16891 }
16892 }
16893
639d11d3 16894 if (die->child != NULL)
c906108c 16895 {
518817b3 16896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16897 struct die_info *child_die;
16898 size_t n_entries = 0, size;
16899 struct common_block *common_block;
16900 struct symbol *sym;
74ac6d43 16901
4357ac6c
TT
16902 for (child_die = die->child;
16903 child_die && child_die->tag;
16904 child_die = sibling_die (child_die))
16905 ++n_entries;
16906
16907 size = (sizeof (struct common_block)
16908 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16909 common_block
16910 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16911 size);
4357ac6c
TT
16912 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16913 common_block->n_entries = 0;
16914
16915 for (child_die = die->child;
16916 child_die && child_die->tag;
16917 child_die = sibling_die (child_die))
16918 {
16919 /* Create the symbol in the DW_TAG_common_block block in the current
16920 symbol scope. */
e7c27a73 16921 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16922 if (sym != NULL)
16923 {
16924 struct attribute *member_loc;
16925
16926 common_block->contents[common_block->n_entries++] = sym;
16927
16928 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16929 cu);
16930 if (member_loc)
16931 {
16932 /* GDB has handled this for a long time, but it is
16933 not specified by DWARF. It seems to have been
16934 emitted by gfortran at least as recently as:
16935 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16936 complaint (_("Variable in common block has "
0971de02 16937 "DW_AT_data_member_location "
9d8780f0
SM
16938 "- DIE at %s [in module %s]"),
16939 sect_offset_str (child_die->sect_off),
518817b3 16940 objfile_name (objfile));
0971de02
TT
16941
16942 if (attr_form_is_section_offset (member_loc))
16943 dwarf2_complex_location_expr_complaint ();
16944 else if (attr_form_is_constant (member_loc)
16945 || attr_form_is_block (member_loc))
16946 {
435d3d88 16947 if (attr != nullptr)
0971de02
TT
16948 mark_common_block_symbol_computed (sym, die, attr,
16949 member_loc, cu);
16950 }
16951 else
16952 dwarf2_complex_location_expr_complaint ();
16953 }
16954 }
c906108c 16955 }
4357ac6c
TT
16956
16957 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16958 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16959 }
16960}
16961
0114d602 16962/* Create a type for a C++ namespace. */
d9fa45fe 16963
0114d602
DJ
16964static struct type *
16965read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16966{
518817b3 16967 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16968 const char *previous_prefix, *name;
9219021c 16969 int is_anonymous;
0114d602
DJ
16970 struct type *type;
16971
16972 /* For extensions, reuse the type of the original namespace. */
16973 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16974 {
16975 struct die_info *ext_die;
16976 struct dwarf2_cu *ext_cu = cu;
9a619af0 16977
0114d602
DJ
16978 ext_die = dwarf2_extension (die, &ext_cu);
16979 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16980
16981 /* EXT_CU may not be the same as CU.
02142a6c 16982 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16983 return set_die_type (die, type, cu);
16984 }
9219021c 16985
e142c38c 16986 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16987
16988 /* Now build the name of the current namespace. */
16989
0114d602
DJ
16990 previous_prefix = determine_prefix (die, cu);
16991 if (previous_prefix[0] != '\0')
16992 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16993 previous_prefix, name, 0, cu);
0114d602
DJ
16994
16995 /* Create the type. */
19f392bc 16996 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16997
60531b24 16998 return set_die_type (die, type, cu);
0114d602
DJ
16999}
17000
22cee43f 17001/* Read a namespace scope. */
0114d602
DJ
17002
17003static void
17004read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17005{
518817b3 17006 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17007 int is_anonymous;
9219021c 17008
5c4e30ca
DC
17009 /* Add a symbol associated to this if we haven't seen the namespace
17010 before. Also, add a using directive if it's an anonymous
17011 namespace. */
9219021c 17012
f2f0e013 17013 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17014 {
17015 struct type *type;
17016
0114d602 17017 type = read_type_die (die, cu);
e7c27a73 17018 new_symbol (die, type, cu);
5c4e30ca 17019
e8e80198 17020 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17021 if (is_anonymous)
0114d602
DJ
17022 {
17023 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17024
eb1e02fd 17025 std::vector<const char *> excludes;
804d2729 17026 add_using_directive (using_directives (cu),
22cee43f 17027 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17028 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17029 }
5c4e30ca 17030 }
9219021c 17031
639d11d3 17032 if (die->child != NULL)
d9fa45fe 17033 {
639d11d3 17034 struct die_info *child_die = die->child;
6e70227d 17035
d9fa45fe
DC
17036 while (child_die && child_die->tag)
17037 {
e7c27a73 17038 process_die (child_die, cu);
d9fa45fe
DC
17039 child_die = sibling_die (child_die);
17040 }
17041 }
38d518c9
EZ
17042}
17043
f55ee35c
JK
17044/* Read a Fortran module as type. This DIE can be only a declaration used for
17045 imported module. Still we need that type as local Fortran "use ... only"
17046 declaration imports depend on the created type in determine_prefix. */
17047
17048static struct type *
17049read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17050{
518817b3 17051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17052 const char *module_name;
f55ee35c
JK
17053 struct type *type;
17054
17055 module_name = dwarf2_name (die, cu);
19f392bc 17056 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17057
f55ee35c
JK
17058 return set_die_type (die, type, cu);
17059}
17060
5d7cb8df
JK
17061/* Read a Fortran module. */
17062
17063static void
17064read_module (struct die_info *die, struct dwarf2_cu *cu)
17065{
17066 struct die_info *child_die = die->child;
530e8392
KB
17067 struct type *type;
17068
17069 type = read_type_die (die, cu);
17070 new_symbol (die, type, cu);
5d7cb8df 17071
5d7cb8df
JK
17072 while (child_die && child_die->tag)
17073 {
17074 process_die (child_die, cu);
17075 child_die = sibling_die (child_die);
17076 }
17077}
17078
38d518c9
EZ
17079/* Return the name of the namespace represented by DIE. Set
17080 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17081 namespace. */
17082
17083static const char *
e142c38c 17084namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17085{
17086 struct die_info *current_die;
17087 const char *name = NULL;
17088
17089 /* Loop through the extensions until we find a name. */
17090
17091 for (current_die = die;
17092 current_die != NULL;
f2f0e013 17093 current_die = dwarf2_extension (die, &cu))
38d518c9 17094 {
96553a0c
DE
17095 /* We don't use dwarf2_name here so that we can detect the absence
17096 of a name -> anonymous namespace. */
7d45c7c3 17097 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17098
38d518c9
EZ
17099 if (name != NULL)
17100 break;
17101 }
17102
17103 /* Is it an anonymous namespace? */
17104
17105 *is_anonymous = (name == NULL);
17106 if (*is_anonymous)
2b1dbab0 17107 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17108
17109 return name;
d9fa45fe
DC
17110}
17111
c906108c
SS
17112/* Extract all information from a DW_TAG_pointer_type DIE and add to
17113 the user defined type vector. */
17114
f792889a 17115static struct type *
e7c27a73 17116read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17117{
518817b3
SM
17118 struct gdbarch *gdbarch
17119 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17120 struct comp_unit_head *cu_header = &cu->header;
c906108c 17121 struct type *type;
8b2dbe47
KB
17122 struct attribute *attr_byte_size;
17123 struct attribute *attr_address_class;
17124 int byte_size, addr_class;
7e314c57
JK
17125 struct type *target_type;
17126
17127 target_type = die_type (die, cu);
c906108c 17128
7e314c57
JK
17129 /* The die_type call above may have already set the type for this DIE. */
17130 type = get_die_type (die, cu);
17131 if (type)
17132 return type;
17133
17134 type = lookup_pointer_type (target_type);
8b2dbe47 17135
e142c38c 17136 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17137 if (attr_byte_size)
17138 byte_size = DW_UNSND (attr_byte_size);
c906108c 17139 else
8b2dbe47
KB
17140 byte_size = cu_header->addr_size;
17141
e142c38c 17142 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17143 if (attr_address_class)
17144 addr_class = DW_UNSND (attr_address_class);
17145 else
17146 addr_class = DW_ADDR_none;
17147
2b4424c3
TT
17148 ULONGEST alignment = get_alignment (cu, die);
17149
17150 /* If the pointer size, alignment, or address class is different
17151 than the default, create a type variant marked as such and set
17152 the length accordingly. */
17153 if (TYPE_LENGTH (type) != byte_size
17154 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17155 && alignment != TYPE_RAW_ALIGN (type))
17156 || addr_class != DW_ADDR_none)
c906108c 17157 {
5e2b427d 17158 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17159 {
17160 int type_flags;
17161
849957d9 17162 type_flags = gdbarch_address_class_type_flags
5e2b427d 17163 (gdbarch, byte_size, addr_class);
876cecd0
TT
17164 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17165 == 0);
8b2dbe47
KB
17166 type = make_type_with_address_space (type, type_flags);
17167 }
17168 else if (TYPE_LENGTH (type) != byte_size)
17169 {
b98664d3 17170 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17171 }
2b4424c3
TT
17172 else if (TYPE_RAW_ALIGN (type) != alignment)
17173 {
b98664d3 17174 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17175 " - DIE at %s [in module %s]"),
17176 sect_offset_str (die->sect_off),
17177 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17178 }
6e70227d 17179 else
9a619af0
MS
17180 {
17181 /* Should we also complain about unhandled address classes? */
17182 }
c906108c 17183 }
8b2dbe47
KB
17184
17185 TYPE_LENGTH (type) = byte_size;
2b4424c3 17186 set_type_align (type, alignment);
f792889a 17187 return set_die_type (die, type, cu);
c906108c
SS
17188}
17189
17190/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17191 the user defined type vector. */
17192
f792889a 17193static struct type *
e7c27a73 17194read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17195{
17196 struct type *type;
17197 struct type *to_type;
17198 struct type *domain;
17199
e7c27a73
DJ
17200 to_type = die_type (die, cu);
17201 domain = die_containing_type (die, cu);
0d5de010 17202
7e314c57
JK
17203 /* The calls above may have already set the type for this DIE. */
17204 type = get_die_type (die, cu);
17205 if (type)
17206 return type;
17207
0d5de010
DJ
17208 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17209 type = lookup_methodptr_type (to_type);
7078baeb
TT
17210 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17211 {
518817b3
SM
17212 struct type *new_type
17213 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17214
17215 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17216 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17217 TYPE_VARARGS (to_type));
17218 type = lookup_methodptr_type (new_type);
17219 }
0d5de010
DJ
17220 else
17221 type = lookup_memberptr_type (to_type, domain);
c906108c 17222
f792889a 17223 return set_die_type (die, type, cu);
c906108c
SS
17224}
17225
4297a3f0 17226/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17227 the user defined type vector. */
17228
f792889a 17229static struct type *
4297a3f0
AV
17230read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17231 enum type_code refcode)
c906108c 17232{
e7c27a73 17233 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17234 struct type *type, *target_type;
c906108c
SS
17235 struct attribute *attr;
17236
4297a3f0
AV
17237 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17238
7e314c57
JK
17239 target_type = die_type (die, cu);
17240
17241 /* The die_type call above may have already set the type for this DIE. */
17242 type = get_die_type (die, cu);
17243 if (type)
17244 return type;
17245
4297a3f0 17246 type = lookup_reference_type (target_type, refcode);
e142c38c 17247 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17248 if (attr != nullptr)
c906108c
SS
17249 {
17250 TYPE_LENGTH (type) = DW_UNSND (attr);
17251 }
17252 else
17253 {
107d2387 17254 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17255 }
2b4424c3 17256 maybe_set_alignment (cu, die, type);
f792889a 17257 return set_die_type (die, type, cu);
c906108c
SS
17258}
17259
cf363f18
MW
17260/* Add the given cv-qualifiers to the element type of the array. GCC
17261 outputs DWARF type qualifiers that apply to an array, not the
17262 element type. But GDB relies on the array element type to carry
17263 the cv-qualifiers. This mimics section 6.7.3 of the C99
17264 specification. */
17265
17266static struct type *
17267add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17268 struct type *base_type, int cnst, int voltl)
17269{
17270 struct type *el_type, *inner_array;
17271
17272 base_type = copy_type (base_type);
17273 inner_array = base_type;
17274
17275 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17276 {
17277 TYPE_TARGET_TYPE (inner_array) =
17278 copy_type (TYPE_TARGET_TYPE (inner_array));
17279 inner_array = TYPE_TARGET_TYPE (inner_array);
17280 }
17281
17282 el_type = TYPE_TARGET_TYPE (inner_array);
17283 cnst |= TYPE_CONST (el_type);
17284 voltl |= TYPE_VOLATILE (el_type);
17285 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17286
17287 return set_die_type (die, base_type, cu);
17288}
17289
f792889a 17290static struct type *
e7c27a73 17291read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17292{
f792889a 17293 struct type *base_type, *cv_type;
c906108c 17294
e7c27a73 17295 base_type = die_type (die, cu);
7e314c57
JK
17296
17297 /* The die_type call above may have already set the type for this DIE. */
17298 cv_type = get_die_type (die, cu);
17299 if (cv_type)
17300 return cv_type;
17301
2f608a3a
KW
17302 /* In case the const qualifier is applied to an array type, the element type
17303 is so qualified, not the array type (section 6.7.3 of C99). */
17304 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17305 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17306
f792889a
DJ
17307 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17308 return set_die_type (die, cv_type, cu);
c906108c
SS
17309}
17310
f792889a 17311static struct type *
e7c27a73 17312read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17313{
f792889a 17314 struct type *base_type, *cv_type;
c906108c 17315
e7c27a73 17316 base_type = die_type (die, cu);
7e314c57
JK
17317
17318 /* The die_type call above may have already set the type for this DIE. */
17319 cv_type = get_die_type (die, cu);
17320 if (cv_type)
17321 return cv_type;
17322
cf363f18
MW
17323 /* In case the volatile qualifier is applied to an array type, the
17324 element type is so qualified, not the array type (section 6.7.3
17325 of C99). */
17326 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17327 return add_array_cv_type (die, cu, base_type, 0, 1);
17328
f792889a
DJ
17329 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17330 return set_die_type (die, cv_type, cu);
c906108c
SS
17331}
17332
06d66ee9
TT
17333/* Handle DW_TAG_restrict_type. */
17334
17335static struct type *
17336read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17337{
17338 struct type *base_type, *cv_type;
17339
17340 base_type = die_type (die, cu);
17341
17342 /* The die_type call above may have already set the type for this DIE. */
17343 cv_type = get_die_type (die, cu);
17344 if (cv_type)
17345 return cv_type;
17346
17347 cv_type = make_restrict_type (base_type);
17348 return set_die_type (die, cv_type, cu);
17349}
17350
a2c2acaf
MW
17351/* Handle DW_TAG_atomic_type. */
17352
17353static struct type *
17354read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17355{
17356 struct type *base_type, *cv_type;
17357
17358 base_type = die_type (die, cu);
17359
17360 /* The die_type call above may have already set the type for this DIE. */
17361 cv_type = get_die_type (die, cu);
17362 if (cv_type)
17363 return cv_type;
17364
17365 cv_type = make_atomic_type (base_type);
17366 return set_die_type (die, cv_type, cu);
17367}
17368
c906108c
SS
17369/* Extract all information from a DW_TAG_string_type DIE and add to
17370 the user defined type vector. It isn't really a user defined type,
17371 but it behaves like one, with other DIE's using an AT_user_def_type
17372 attribute to reference it. */
17373
f792889a 17374static struct type *
e7c27a73 17375read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17376{
518817b3 17377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17378 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17379 struct type *type, *range_type, *index_type, *char_type;
17380 struct attribute *attr;
216a7e6b
AB
17381 struct dynamic_prop prop;
17382 bool length_is_constant = true;
17383 LONGEST length;
17384
17385 /* There are a couple of places where bit sizes might be made use of
17386 when parsing a DW_TAG_string_type, however, no producer that we know
17387 of make use of these. Handling bit sizes that are a multiple of the
17388 byte size is easy enough, but what about other bit sizes? Lets deal
17389 with that problem when we have to. Warn about these attributes being
17390 unsupported, then parse the type and ignore them like we always
17391 have. */
17392 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17393 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17394 {
17395 static bool warning_printed = false;
17396 if (!warning_printed)
17397 {
17398 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17399 "currently supported on DW_TAG_string_type."));
17400 warning_printed = true;
17401 }
17402 }
c906108c 17403
e142c38c 17404 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17405 if (attr != nullptr && !attr_form_is_constant (attr))
17406 {
17407 /* The string length describes the location at which the length of
17408 the string can be found. The size of the length field can be
17409 specified with one of the attributes below. */
17410 struct type *prop_type;
17411 struct attribute *len
17412 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17413 if (len == nullptr)
17414 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17415 if (len != nullptr && attr_form_is_constant (len))
17416 {
17417 /* Pass 0 as the default as we know this attribute is constant
17418 and the default value will not be returned. */
17419 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17420 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17421 }
17422 else
17423 {
17424 /* If the size is not specified then we assume it is the size of
17425 an address on this target. */
17426 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17427 }
17428
17429 /* Convert the attribute into a dynamic property. */
17430 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17431 length = 1;
17432 else
17433 length_is_constant = false;
17434 }
17435 else if (attr != nullptr)
17436 {
17437 /* This DW_AT_string_length just contains the length with no
17438 indirection. There's no need to create a dynamic property in this
17439 case. Pass 0 for the default value as we know it will not be
17440 returned in this case. */
17441 length = dwarf2_get_attr_constant_value (attr, 0);
17442 }
17443 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17444 {
216a7e6b
AB
17445 /* We don't currently support non-constant byte sizes for strings. */
17446 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17447 }
17448 else
17449 {
216a7e6b
AB
17450 /* Use 1 as a fallback length if we have nothing else. */
17451 length = 1;
c906108c 17452 }
6ccb9162 17453
46bf5051 17454 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17455 if (length_is_constant)
17456 range_type = create_static_range_type (NULL, index_type, 1, length);
17457 else
17458 {
17459 struct dynamic_prop low_bound;
17460
17461 low_bound.kind = PROP_CONST;
17462 low_bound.data.const_val = 1;
17463 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17464 }
3b7538c0
UW
17465 char_type = language_string_char_type (cu->language_defn, gdbarch);
17466 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17467
f792889a 17468 return set_die_type (die, type, cu);
c906108c
SS
17469}
17470
4d804846
JB
17471/* Assuming that DIE corresponds to a function, returns nonzero
17472 if the function is prototyped. */
17473
17474static int
17475prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17476{
17477 struct attribute *attr;
17478
17479 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17480 if (attr && (DW_UNSND (attr) != 0))
17481 return 1;
17482
17483 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17484 is only meaningful for C, but the concept also extends to other
4d804846
JB
17485 languages that allow unprototyped functions (Eg: Objective C).
17486 For all other languages, assume that functions are always
17487 prototyped. */
17488 if (cu->language != language_c
17489 && cu->language != language_objc
17490 && cu->language != language_opencl)
17491 return 1;
17492
17493 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17494 prototyped and unprototyped functions; default to prototyped,
17495 since that is more common in modern code (and RealView warns
17496 about unprototyped functions). */
17497 if (producer_is_realview (cu->producer))
17498 return 1;
17499
17500 return 0;
17501}
17502
c906108c
SS
17503/* Handle DIES due to C code like:
17504
17505 struct foo
c5aa993b
JM
17506 {
17507 int (*funcp)(int a, long l);
17508 int b;
17509 };
c906108c 17510
0963b4bd 17511 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17512
f792889a 17513static struct type *
e7c27a73 17514read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17515{
518817b3 17516 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17517 struct type *type; /* Type that this function returns. */
17518 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17519 struct attribute *attr;
17520
e7c27a73 17521 type = die_type (die, cu);
7e314c57
JK
17522
17523 /* The die_type call above may have already set the type for this DIE. */
17524 ftype = get_die_type (die, cu);
17525 if (ftype)
17526 return ftype;
17527
0c8b41f1 17528 ftype = lookup_function_type (type);
c906108c 17529
4d804846 17530 if (prototyped_function_p (die, cu))
a6c727b2 17531 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17532
c055b101
CV
17533 /* Store the calling convention in the type if it's available in
17534 the subroutine die. Otherwise set the calling convention to
17535 the default value DW_CC_normal. */
17536 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17537 if (attr != nullptr
17538 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17539 TYPE_CALLING_CONVENTION (ftype)
17540 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17541 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17542 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17543 else
17544 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17545
743649fd
MW
17546 /* Record whether the function returns normally to its caller or not
17547 if the DWARF producer set that information. */
17548 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17549 if (attr && (DW_UNSND (attr) != 0))
17550 TYPE_NO_RETURN (ftype) = 1;
17551
76c10ea2
GM
17552 /* We need to add the subroutine type to the die immediately so
17553 we don't infinitely recurse when dealing with parameters
0963b4bd 17554 declared as the same subroutine type. */
76c10ea2 17555 set_die_type (die, ftype, cu);
6e70227d 17556
639d11d3 17557 if (die->child != NULL)
c906108c 17558 {
bb5ed363 17559 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17560 struct die_info *child_die;
8072405b 17561 int nparams, iparams;
c906108c
SS
17562
17563 /* Count the number of parameters.
17564 FIXME: GDB currently ignores vararg functions, but knows about
17565 vararg member functions. */
8072405b 17566 nparams = 0;
639d11d3 17567 child_die = die->child;
c906108c
SS
17568 while (child_die && child_die->tag)
17569 {
17570 if (child_die->tag == DW_TAG_formal_parameter)
17571 nparams++;
17572 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17573 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17574 child_die = sibling_die (child_die);
17575 }
17576
17577 /* Allocate storage for parameters and fill them in. */
17578 TYPE_NFIELDS (ftype) = nparams;
17579 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17580 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17581
8072405b
JK
17582 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17583 even if we error out during the parameters reading below. */
17584 for (iparams = 0; iparams < nparams; iparams++)
17585 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17586
17587 iparams = 0;
639d11d3 17588 child_die = die->child;
c906108c
SS
17589 while (child_die && child_die->tag)
17590 {
17591 if (child_die->tag == DW_TAG_formal_parameter)
17592 {
3ce3b1ba
PA
17593 struct type *arg_type;
17594
17595 /* DWARF version 2 has no clean way to discern C++
17596 static and non-static member functions. G++ helps
17597 GDB by marking the first parameter for non-static
17598 member functions (which is the this pointer) as
17599 artificial. We pass this information to
17600 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17601
17602 DWARF version 3 added DW_AT_object_pointer, which GCC
17603 4.5 does not yet generate. */
e142c38c 17604 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17605 if (attr != nullptr)
c906108c
SS
17606 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17607 else
9c37b5ae 17608 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17609 arg_type = die_type (child_die, cu);
17610
17611 /* RealView does not mark THIS as const, which the testsuite
17612 expects. GCC marks THIS as const in method definitions,
17613 but not in the class specifications (GCC PR 43053). */
17614 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17615 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17616 {
17617 int is_this = 0;
17618 struct dwarf2_cu *arg_cu = cu;
17619 const char *name = dwarf2_name (child_die, cu);
17620
17621 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17622 if (attr != nullptr)
3ce3b1ba
PA
17623 {
17624 /* If the compiler emits this, use it. */
17625 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17626 is_this = 1;
17627 }
17628 else if (name && strcmp (name, "this") == 0)
17629 /* Function definitions will have the argument names. */
17630 is_this = 1;
17631 else if (name == NULL && iparams == 0)
17632 /* Declarations may not have the names, so like
17633 elsewhere in GDB, assume an artificial first
17634 argument is "this". */
17635 is_this = 1;
17636
17637 if (is_this)
17638 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17639 arg_type, 0);
17640 }
17641
17642 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17643 iparams++;
17644 }
17645 child_die = sibling_die (child_die);
17646 }
17647 }
17648
76c10ea2 17649 return ftype;
c906108c
SS
17650}
17651
f792889a 17652static struct type *
e7c27a73 17653read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17654{
518817b3 17655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17656 const char *name = NULL;
3c8e0968 17657 struct type *this_type, *target_type;
c906108c 17658
94af9270 17659 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17660 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17661 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17662 set_die_type (die, this_type, cu);
3c8e0968
DE
17663 target_type = die_type (die, cu);
17664 if (target_type != this_type)
17665 TYPE_TARGET_TYPE (this_type) = target_type;
17666 else
17667 {
17668 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17669 spec and cause infinite loops in GDB. */
b98664d3 17670 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17671 "- DIE at %s [in module %s]"),
17672 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17673 TYPE_TARGET_TYPE (this_type) = NULL;
17674 }
f792889a 17675 return this_type;
c906108c
SS
17676}
17677
9b790ce7
UW
17678/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17679 (which may be different from NAME) to the architecture back-end to allow
17680 it to guess the correct format if necessary. */
17681
17682static struct type *
17683dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17684 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17685{
17686 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17687 const struct floatformat **format;
17688 struct type *type;
17689
17690 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17691 if (format)
103a685e 17692 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17693 else
77b7c781 17694 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17695
17696 return type;
17697}
17698
eb77c9df
AB
17699/* Allocate an integer type of size BITS and name NAME. */
17700
17701static struct type *
17702dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17703 int bits, int unsigned_p, const char *name)
17704{
17705 struct type *type;
17706
17707 /* Versions of Intel's C Compiler generate an integer type called "void"
17708 instead of using DW_TAG_unspecified_type. This has been seen on
17709 at least versions 14, 17, and 18. */
35ee2dc2
AB
17710 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17711 && strcmp (name, "void") == 0)
eb77c9df
AB
17712 type = objfile_type (objfile)->builtin_void;
17713 else
17714 type = init_integer_type (objfile, bits, unsigned_p, name);
17715
17716 return type;
17717}
17718
8bdc1658
AB
17719/* Initialise and return a floating point type of size BITS suitable for
17720 use as a component of a complex number. The NAME_HINT is passed through
17721 when initialising the floating point type and is the name of the complex
17722 type.
17723
17724 As DWARF doesn't currently provide an explicit name for the components
17725 of a complex number, but it can be helpful to have these components
17726 named, we try to select a suitable name based on the size of the
17727 component. */
17728static struct type *
17729dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17730 struct objfile *objfile,
103a685e
TT
17731 int bits, const char *name_hint,
17732 enum bfd_endian byte_order)
8bdc1658
AB
17733{
17734 gdbarch *gdbarch = get_objfile_arch (objfile);
17735 struct type *tt = nullptr;
17736
35add35e
AB
17737 /* Try to find a suitable floating point builtin type of size BITS.
17738 We're going to use the name of this type as the name for the complex
17739 target type that we are about to create. */
1db455a7 17740 switch (cu->language)
8bdc1658 17741 {
1db455a7
AB
17742 case language_fortran:
17743 switch (bits)
17744 {
17745 case 32:
17746 tt = builtin_f_type (gdbarch)->builtin_real;
17747 break;
17748 case 64:
17749 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17750 break;
17751 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17752 case 128:
17753 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17754 break;
17755 }
8bdc1658 17756 break;
1db455a7
AB
17757 default:
17758 switch (bits)
17759 {
17760 case 32:
17761 tt = builtin_type (gdbarch)->builtin_float;
17762 break;
17763 case 64:
17764 tt = builtin_type (gdbarch)->builtin_double;
17765 break;
17766 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17767 case 128:
17768 tt = builtin_type (gdbarch)->builtin_long_double;
17769 break;
17770 }
8bdc1658
AB
17771 break;
17772 }
17773
35add35e
AB
17774 /* If the type we found doesn't match the size we were looking for, then
17775 pretend we didn't find a type at all, the complex target type we
17776 create will then be nameless. */
a12e5744 17777 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17778 tt = nullptr;
17779
8bdc1658 17780 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17781 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17782}
17783
c906108c
SS
17784/* Find a representation of a given base type and install
17785 it in the TYPE field of the die. */
17786
f792889a 17787static struct type *
e7c27a73 17788read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17789{
518817b3 17790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17791 struct type *type;
17792 struct attribute *attr;
19f392bc 17793 int encoding = 0, bits = 0;
15d034d0 17794 const char *name;
34877895 17795 gdbarch *arch;
c906108c 17796
e142c38c 17797 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17798 if (attr != nullptr)
34877895 17799 encoding = DW_UNSND (attr);
e142c38c 17800 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17801 if (attr != nullptr)
34877895 17802 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17803 name = dwarf2_name (die, cu);
6ccb9162 17804 if (!name)
34877895 17805 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17806
17807 arch = get_objfile_arch (objfile);
17808 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17809
34877895
PJ
17810 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17811 if (attr)
103a685e
TT
17812 {
17813 int endianity = DW_UNSND (attr);
17814
17815 switch (endianity)
17816 {
17817 case DW_END_big:
17818 byte_order = BFD_ENDIAN_BIG;
17819 break;
17820 case DW_END_little:
17821 byte_order = BFD_ENDIAN_LITTLE;
17822 break;
17823 default:
17824 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17825 break;
17826 }
17827 }
6ccb9162
UW
17828
17829 switch (encoding)
c906108c 17830 {
6ccb9162
UW
17831 case DW_ATE_address:
17832 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17833 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17834 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17835 break;
17836 case DW_ATE_boolean:
19f392bc 17837 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17838 break;
17839 case DW_ATE_complex_float:
103a685e
TT
17840 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17841 byte_order);
19f392bc 17842 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17843 break;
17844 case DW_ATE_decimal_float:
19f392bc 17845 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17846 break;
17847 case DW_ATE_float:
103a685e 17848 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17849 break;
17850 case DW_ATE_signed:
eb77c9df 17851 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17852 break;
17853 case DW_ATE_unsigned:
3b2b8fea
TT
17854 if (cu->language == language_fortran
17855 && name
61012eef 17856 && startswith (name, "character("))
19f392bc
UW
17857 type = init_character_type (objfile, bits, 1, name);
17858 else
eb77c9df 17859 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17860 break;
17861 case DW_ATE_signed_char:
6e70227d 17862 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17863 || cu->language == language_pascal
17864 || cu->language == language_fortran)
19f392bc
UW
17865 type = init_character_type (objfile, bits, 0, name);
17866 else
eb77c9df 17867 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17868 break;
17869 case DW_ATE_unsigned_char:
868a0084 17870 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17871 || cu->language == language_pascal
c44af4eb
TT
17872 || cu->language == language_fortran
17873 || cu->language == language_rust)
19f392bc
UW
17874 type = init_character_type (objfile, bits, 1, name);
17875 else
eb77c9df 17876 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17877 break;
75079b2b 17878 case DW_ATE_UTF:
53e710ac 17879 {
53e710ac
PA
17880 if (bits == 16)
17881 type = builtin_type (arch)->builtin_char16;
17882 else if (bits == 32)
17883 type = builtin_type (arch)->builtin_char32;
17884 else
17885 {
b98664d3 17886 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17887 bits);
eb77c9df 17888 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17889 }
17890 return set_die_type (die, type, cu);
17891 }
75079b2b
TT
17892 break;
17893
6ccb9162 17894 default:
b98664d3 17895 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17896 dwarf_type_encoding_name (encoding));
77b7c781 17897 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17898 break;
c906108c 17899 }
6ccb9162 17900
0114d602 17901 if (name && strcmp (name, "char") == 0)
876cecd0 17902 TYPE_NOSIGN (type) = 1;
0114d602 17903
2b4424c3
TT
17904 maybe_set_alignment (cu, die, type);
17905
103a685e 17906 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17907
f792889a 17908 return set_die_type (die, type, cu);
c906108c
SS
17909}
17910
80180f79
SA
17911/* Parse dwarf attribute if it's a block, reference or constant and put the
17912 resulting value of the attribute into struct bound_prop.
17913 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17914
17915static int
17916attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17917 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17918 struct type *default_type)
80180f79
SA
17919{
17920 struct dwarf2_property_baton *baton;
518817b3
SM
17921 struct obstack *obstack
17922 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17923
9a49df9d
AB
17924 gdb_assert (default_type != NULL);
17925
80180f79
SA
17926 if (attr == NULL || prop == NULL)
17927 return 0;
17928
17929 if (attr_form_is_block (attr))
17930 {
8d749320 17931 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17932 baton->property_type = default_type;
80180f79
SA
17933 baton->locexpr.per_cu = cu->per_cu;
17934 baton->locexpr.size = DW_BLOCK (attr)->size;
17935 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17936 switch (attr->name)
17937 {
17938 case DW_AT_string_length:
17939 baton->locexpr.is_reference = true;
17940 break;
17941 default:
17942 baton->locexpr.is_reference = false;
17943 break;
17944 }
80180f79
SA
17945 prop->data.baton = baton;
17946 prop->kind = PROP_LOCEXPR;
17947 gdb_assert (prop->data.baton != NULL);
17948 }
17949 else if (attr_form_is_ref (attr))
17950 {
17951 struct dwarf2_cu *target_cu = cu;
17952 struct die_info *target_die;
17953 struct attribute *target_attr;
17954
17955 target_die = follow_die_ref (die, attr, &target_cu);
17956 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17957 if (target_attr == NULL)
17958 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17959 target_cu);
80180f79
SA
17960 if (target_attr == NULL)
17961 return 0;
17962
df25ebbd 17963 switch (target_attr->name)
80180f79 17964 {
df25ebbd
JB
17965 case DW_AT_location:
17966 if (attr_form_is_section_offset (target_attr))
17967 {
8d749320 17968 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17969 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17970 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17971 prop->data.baton = baton;
17972 prop->kind = PROP_LOCLIST;
17973 gdb_assert (prop->data.baton != NULL);
17974 }
17975 else if (attr_form_is_block (target_attr))
17976 {
8d749320 17977 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17978 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17979 baton->locexpr.per_cu = cu->per_cu;
17980 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17981 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17982 baton->locexpr.is_reference = true;
df25ebbd
JB
17983 prop->data.baton = baton;
17984 prop->kind = PROP_LOCEXPR;
17985 gdb_assert (prop->data.baton != NULL);
17986 }
17987 else
17988 {
17989 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17990 "dynamic property");
17991 return 0;
17992 }
17993 break;
17994 case DW_AT_data_member_location:
17995 {
17996 LONGEST offset;
17997
17998 if (!handle_data_member_location (target_die, target_cu,
17999 &offset))
18000 return 0;
18001
8d749320 18002 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18003 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18004 target_cu);
df25ebbd
JB
18005 baton->offset_info.offset = offset;
18006 baton->offset_info.type = die_type (target_die, target_cu);
18007 prop->data.baton = baton;
18008 prop->kind = PROP_ADDR_OFFSET;
18009 break;
18010 }
80180f79
SA
18011 }
18012 }
18013 else if (attr_form_is_constant (attr))
18014 {
18015 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18016 prop->kind = PROP_CONST;
18017 }
18018 else
18019 {
18020 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18021 dwarf2_name (die, cu));
18022 return 0;
18023 }
18024
18025 return 1;
18026}
18027
11a8b164
AB
18028/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18029 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
18030
18031static struct type *
11a8b164
AB
18032dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18033 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
18034{
18035 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
18036 struct type *int_type;
18037
18038 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18039#define TRY_TYPE(F) \
18040 int_type = (unsigned_p \
18041 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18042 : objfile_type (objfile)->builtin_ ## F); \
18043 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18044 return int_type
18045
18046 TRY_TYPE (char);
18047 TRY_TYPE (short);
18048 TRY_TYPE (int);
18049 TRY_TYPE (long);
18050 TRY_TYPE (long_long);
18051
18052#undef TRY_TYPE
18053
18054 gdb_assert_not_reached ("unable to find suitable integer type");
18055}
18056
11a8b164
AB
18057/* Find an integer type the same size as the address size given in the
18058 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18059 is unsigned or not. */
18060
18061static struct type *
18062dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18063 bool unsigned_p)
18064{
18065 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18066 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18067}
18068
b86352cf
AB
18069/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18070 present (which is valid) then compute the default type based on the
18071 compilation units address size. */
18072
18073static struct type *
18074read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18075{
18076 struct type *index_type = die_type (die, cu);
18077
18078 /* Dwarf-2 specifications explicitly allows to create subrange types
18079 without specifying a base type.
18080 In that case, the base type must be set to the type of
18081 the lower bound, upper bound or count, in that order, if any of these
18082 three attributes references an object that has a type.
18083 If no base type is found, the Dwarf-2 specifications say that
18084 a signed integer type of size equal to the size of an address should
18085 be used.
18086 For the following C code: `extern char gdb_int [];'
18087 GCC produces an empty range DIE.
18088 FIXME: muller/2010-05-28: Possible references to object for low bound,
18089 high bound or count are not yet handled by this code. */
18090 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18091 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18092
18093 return index_type;
18094}
18095
a02abb62
JB
18096/* Read the given DW_AT_subrange DIE. */
18097
f792889a 18098static struct type *
a02abb62
JB
18099read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18100{
4c9ad8c2 18101 struct type *base_type, *orig_base_type;
a02abb62
JB
18102 struct type *range_type;
18103 struct attribute *attr;
729efb13 18104 struct dynamic_prop low, high;
4fae6e18 18105 int low_default_is_valid;
c451ebe5 18106 int high_bound_is_count = 0;
15d034d0 18107 const char *name;
d359392f 18108 ULONGEST negative_mask;
e77813c8 18109
b86352cf
AB
18110 orig_base_type = read_subrange_index_type (die, cu);
18111
4c9ad8c2
TT
18112 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18113 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18114 creating the range type, but we use the result of check_typedef
18115 when examining properties of the type. */
18116 base_type = check_typedef (orig_base_type);
a02abb62 18117
7e314c57
JK
18118 /* The die_type call above may have already set the type for this DIE. */
18119 range_type = get_die_type (die, cu);
18120 if (range_type)
18121 return range_type;
18122
729efb13
SA
18123 low.kind = PROP_CONST;
18124 high.kind = PROP_CONST;
18125 high.data.const_val = 0;
18126
4fae6e18
JK
18127 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18128 omitting DW_AT_lower_bound. */
18129 switch (cu->language)
6e70227d 18130 {
4fae6e18
JK
18131 case language_c:
18132 case language_cplus:
729efb13 18133 low.data.const_val = 0;
4fae6e18
JK
18134 low_default_is_valid = 1;
18135 break;
18136 case language_fortran:
729efb13 18137 low.data.const_val = 1;
4fae6e18
JK
18138 low_default_is_valid = 1;
18139 break;
18140 case language_d:
4fae6e18 18141 case language_objc:
c44af4eb 18142 case language_rust:
729efb13 18143 low.data.const_val = 0;
4fae6e18
JK
18144 low_default_is_valid = (cu->header.version >= 4);
18145 break;
18146 case language_ada:
18147 case language_m2:
18148 case language_pascal:
729efb13 18149 low.data.const_val = 1;
4fae6e18
JK
18150 low_default_is_valid = (cu->header.version >= 4);
18151 break;
18152 default:
729efb13 18153 low.data.const_val = 0;
4fae6e18
JK
18154 low_default_is_valid = 0;
18155 break;
a02abb62
JB
18156 }
18157
e142c38c 18158 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18159 if (attr != nullptr)
9a49df9d 18160 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18161 else if (!low_default_is_valid)
b98664d3 18162 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18163 "- DIE at %s [in module %s]"),
18164 sect_offset_str (die->sect_off),
518817b3 18165 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18166
506f5c41
TV
18167 struct attribute *attr_ub, *attr_count;
18168 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18169 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18170 {
506f5c41 18171 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18172 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18173 {
c451ebe5
SA
18174 /* If bounds are constant do the final calculation here. */
18175 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18176 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18177 else
18178 high_bound_is_count = 1;
c2ff108b 18179 }
506f5c41
TV
18180 else
18181 {
18182 if (attr_ub != NULL)
18183 complaint (_("Unresolved DW_AT_upper_bound "
18184 "- DIE at %s [in module %s]"),
18185 sect_offset_str (die->sect_off),
18186 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18187 if (attr_count != NULL)
18188 complaint (_("Unresolved DW_AT_count "
18189 "- DIE at %s [in module %s]"),
18190 sect_offset_str (die->sect_off),
18191 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18192 }
e77813c8 18193 }
a02abb62 18194
4e962e74
TT
18195 LONGEST bias = 0;
18196 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18197 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18198 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18199
dbb9c2b1
JB
18200 /* Normally, the DWARF producers are expected to use a signed
18201 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18202 But this is unfortunately not always the case, as witnessed
18203 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18204 is used instead. To work around that ambiguity, we treat
18205 the bounds as signed, and thus sign-extend their values, when
18206 the base type is signed. */
6e70227d 18207 negative_mask =
d359392f 18208 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18209 if (low.kind == PROP_CONST
18210 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18211 low.data.const_val |= negative_mask;
18212 if (high.kind == PROP_CONST
18213 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18214 high.data.const_val |= negative_mask;
43bbcdc2 18215
5bbd8269
AB
18216 /* Check for bit and byte strides. */
18217 struct dynamic_prop byte_stride_prop;
18218 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18219 if (attr_byte_stride != nullptr)
18220 {
18221 struct type *prop_type
18222 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18223 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18224 prop_type);
18225 }
18226
18227 struct dynamic_prop bit_stride_prop;
18228 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18229 if (attr_bit_stride != nullptr)
18230 {
18231 /* It only makes sense to have either a bit or byte stride. */
18232 if (attr_byte_stride != nullptr)
18233 {
18234 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18235 "- DIE at %s [in module %s]"),
18236 sect_offset_str (die->sect_off),
18237 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18238 attr_bit_stride = nullptr;
18239 }
18240 else
18241 {
18242 struct type *prop_type
18243 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18244 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18245 prop_type);
18246 }
18247 }
18248
18249 if (attr_byte_stride != nullptr
18250 || attr_bit_stride != nullptr)
18251 {
18252 bool byte_stride_p = (attr_byte_stride != nullptr);
18253 struct dynamic_prop *stride
18254 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18255
18256 range_type
18257 = create_range_type_with_stride (NULL, orig_base_type, &low,
18258 &high, bias, stride, byte_stride_p);
18259 }
18260 else
18261 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18262
c451ebe5
SA
18263 if (high_bound_is_count)
18264 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18265
c2ff108b
JK
18266 /* Ada expects an empty array on no boundary attributes. */
18267 if (attr == NULL && cu->language != language_ada)
729efb13 18268 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18269
39cbfefa
DJ
18270 name = dwarf2_name (die, cu);
18271 if (name)
18272 TYPE_NAME (range_type) = name;
6e70227d 18273
e142c38c 18274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18275 if (attr != nullptr)
a02abb62
JB
18276 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18277
2b4424c3
TT
18278 maybe_set_alignment (cu, die, range_type);
18279
7e314c57
JK
18280 set_die_type (die, range_type, cu);
18281
18282 /* set_die_type should be already done. */
b4ba55a1
JB
18283 set_descriptive_type (range_type, die, cu);
18284
7e314c57 18285 return range_type;
a02abb62 18286}
6e70227d 18287
f792889a 18288static struct type *
81a17f79
JB
18289read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18290{
18291 struct type *type;
81a17f79 18292
518817b3
SM
18293 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18294 NULL);
0114d602 18295 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18296
74a2f8ff 18297 /* In Ada, an unspecified type is typically used when the description
85102364 18298 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18299 such a type, we treat it as a stub, and try to resolve it later on,
18300 when needed. */
18301 if (cu->language == language_ada)
18302 TYPE_STUB (type) = 1;
18303
f792889a 18304 return set_die_type (die, type, cu);
81a17f79 18305}
a02abb62 18306
639d11d3
DC
18307/* Read a single die and all its descendents. Set the die's sibling
18308 field to NULL; set other fields in the die correctly, and set all
18309 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18310 location of the info_ptr after reading all of those dies. PARENT
18311 is the parent of the die in question. */
18312
18313static struct die_info *
dee91e82 18314read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18315 const gdb_byte *info_ptr,
18316 const gdb_byte **new_info_ptr,
dee91e82 18317 struct die_info *parent)
639d11d3
DC
18318{
18319 struct die_info *die;
d521ce57 18320 const gdb_byte *cur_ptr;
639d11d3
DC
18321 int has_children;
18322
bf6af496 18323 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18324 if (die == NULL)
18325 {
18326 *new_info_ptr = cur_ptr;
18327 return NULL;
18328 }
93311388 18329 store_in_ref_table (die, reader->cu);
639d11d3
DC
18330
18331 if (has_children)
bf6af496 18332 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18333 else
18334 {
18335 die->child = NULL;
18336 *new_info_ptr = cur_ptr;
18337 }
18338
18339 die->sibling = NULL;
18340 die->parent = parent;
18341 return die;
18342}
18343
18344/* Read a die, all of its descendents, and all of its siblings; set
18345 all of the fields of all of the dies correctly. Arguments are as
18346 in read_die_and_children. */
18347
18348static struct die_info *
bf6af496 18349read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18350 const gdb_byte *info_ptr,
18351 const gdb_byte **new_info_ptr,
bf6af496 18352 struct die_info *parent)
639d11d3
DC
18353{
18354 struct die_info *first_die, *last_sibling;
d521ce57 18355 const gdb_byte *cur_ptr;
639d11d3 18356
c906108c 18357 cur_ptr = info_ptr;
639d11d3
DC
18358 first_die = last_sibling = NULL;
18359
18360 while (1)
c906108c 18361 {
639d11d3 18362 struct die_info *die
dee91e82 18363 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18364
1d325ec1 18365 if (die == NULL)
c906108c 18366 {
639d11d3
DC
18367 *new_info_ptr = cur_ptr;
18368 return first_die;
c906108c 18369 }
1d325ec1
DJ
18370
18371 if (!first_die)
18372 first_die = die;
c906108c 18373 else
1d325ec1
DJ
18374 last_sibling->sibling = die;
18375
18376 last_sibling = die;
c906108c 18377 }
c906108c
SS
18378}
18379
bf6af496
DE
18380/* Read a die, all of its descendents, and all of its siblings; set
18381 all of the fields of all of the dies correctly. Arguments are as
18382 in read_die_and_children.
18383 This the main entry point for reading a DIE and all its children. */
18384
18385static struct die_info *
18386read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18387 const gdb_byte *info_ptr,
18388 const gdb_byte **new_info_ptr,
bf6af496
DE
18389 struct die_info *parent)
18390{
18391 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18392 new_info_ptr, parent);
18393
b4f54984 18394 if (dwarf_die_debug)
bf6af496
DE
18395 {
18396 fprintf_unfiltered (gdb_stdlog,
18397 "Read die from %s@0x%x of %s:\n",
a32a8923 18398 get_section_name (reader->die_section),
bf6af496
DE
18399 (unsigned) (info_ptr - reader->die_section->buffer),
18400 bfd_get_filename (reader->abfd));
b4f54984 18401 dump_die (die, dwarf_die_debug);
bf6af496
DE
18402 }
18403
18404 return die;
18405}
18406
3019eac3
DE
18407/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18408 attributes.
18409 The caller is responsible for filling in the extra attributes
18410 and updating (*DIEP)->num_attrs.
18411 Set DIEP to point to a newly allocated die with its information,
18412 except for its child, sibling, and parent fields.
18413 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18414
d521ce57 18415static const gdb_byte *
3019eac3 18416read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18417 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18418 int *has_children, int num_extra_attrs)
93311388 18419{
b64f50a1 18420 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18421 struct abbrev_info *abbrev;
18422 struct die_info *die;
18423 struct dwarf2_cu *cu = reader->cu;
18424 bfd *abfd = reader->abfd;
18425
9c541725 18426 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18427 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18428 info_ptr += bytes_read;
18429 if (!abbrev_number)
18430 {
18431 *diep = NULL;
18432 *has_children = 0;
18433 return info_ptr;
18434 }
18435
685af9cd 18436 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18437 if (!abbrev)
348e048f
DE
18438 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18439 abbrev_number,
18440 bfd_get_filename (abfd));
18441
3019eac3 18442 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18443 die->sect_off = sect_off;
93311388
DE
18444 die->tag = abbrev->tag;
18445 die->abbrev = abbrev_number;
18446
3019eac3
DE
18447 /* Make the result usable.
18448 The caller needs to update num_attrs after adding the extra
18449 attributes. */
93311388
DE
18450 die->num_attrs = abbrev->num_attrs;
18451
18452 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18453 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18454 info_ptr);
93311388
DE
18455
18456 *diep = die;
18457 *has_children = abbrev->has_children;
18458 return info_ptr;
18459}
18460
3019eac3
DE
18461/* Read a die and all its attributes.
18462 Set DIEP to point to a newly allocated die with its information,
18463 except for its child, sibling, and parent fields.
18464 Set HAS_CHILDREN to tell whether the die has children or not. */
18465
d521ce57 18466static const gdb_byte *
3019eac3 18467read_full_die (const struct die_reader_specs *reader,
d521ce57 18468 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18469 int *has_children)
18470{
d521ce57 18471 const gdb_byte *result;
bf6af496
DE
18472
18473 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18474
b4f54984 18475 if (dwarf_die_debug)
bf6af496
DE
18476 {
18477 fprintf_unfiltered (gdb_stdlog,
18478 "Read die from %s@0x%x of %s:\n",
a32a8923 18479 get_section_name (reader->die_section),
bf6af496
DE
18480 (unsigned) (info_ptr - reader->die_section->buffer),
18481 bfd_get_filename (reader->abfd));
b4f54984 18482 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18483 }
18484
18485 return result;
3019eac3 18486}
433df2d4
DE
18487\f
18488/* Abbreviation tables.
3019eac3 18489
433df2d4 18490 In DWARF version 2, the description of the debugging information is
c906108c
SS
18491 stored in a separate .debug_abbrev section. Before we read any
18492 dies from a section we read in all abbreviations and install them
433df2d4
DE
18493 in a hash table. */
18494
18495/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18496
685af9cd
TT
18497struct abbrev_info *
18498abbrev_table::alloc_abbrev ()
433df2d4
DE
18499{
18500 struct abbrev_info *abbrev;
18501
685af9cd 18502 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18503 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18504
433df2d4
DE
18505 return abbrev;
18506}
18507
18508/* Add an abbreviation to the table. */
c906108c 18509
685af9cd
TT
18510void
18511abbrev_table::add_abbrev (unsigned int abbrev_number,
18512 struct abbrev_info *abbrev)
433df2d4
DE
18513{
18514 unsigned int hash_number;
18515
18516 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18517 abbrev->next = m_abbrevs[hash_number];
18518 m_abbrevs[hash_number] = abbrev;
433df2d4 18519}
dee91e82 18520
433df2d4
DE
18521/* Look up an abbrev in the table.
18522 Returns NULL if the abbrev is not found. */
18523
685af9cd
TT
18524struct abbrev_info *
18525abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18526{
433df2d4
DE
18527 unsigned int hash_number;
18528 struct abbrev_info *abbrev;
18529
18530 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18531 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18532
18533 while (abbrev)
18534 {
18535 if (abbrev->number == abbrev_number)
18536 return abbrev;
18537 abbrev = abbrev->next;
18538 }
18539 return NULL;
18540}
18541
18542/* Read in an abbrev table. */
18543
685af9cd 18544static abbrev_table_up
ed2dc618
SM
18545abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18546 struct dwarf2_section_info *section,
9c541725 18547 sect_offset sect_off)
433df2d4
DE
18548{
18549 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18550 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18551 const gdb_byte *abbrev_ptr;
c906108c
SS
18552 struct abbrev_info *cur_abbrev;
18553 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18554 unsigned int abbrev_form;
6dfa2fc2 18555 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18556
685af9cd 18557 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18558
433df2d4 18559 dwarf2_read_section (objfile, section);
9c541725 18560 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18561 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18562 abbrev_ptr += bytes_read;
18563
0963b4bd 18564 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18565 while (abbrev_number)
18566 {
6dfa2fc2 18567 cur_attrs.clear ();
685af9cd 18568 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18569
18570 /* read in abbrev header */
18571 cur_abbrev->number = abbrev_number;
aead7601
SM
18572 cur_abbrev->tag
18573 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18574 abbrev_ptr += bytes_read;
18575 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18576 abbrev_ptr += 1;
18577
18578 /* now read in declarations */
22d2f3ab 18579 for (;;)
c906108c 18580 {
43988095
JK
18581 LONGEST implicit_const;
18582
22d2f3ab
JK
18583 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18584 abbrev_ptr += bytes_read;
18585 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18586 abbrev_ptr += bytes_read;
43988095
JK
18587 if (abbrev_form == DW_FORM_implicit_const)
18588 {
18589 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18590 &bytes_read);
18591 abbrev_ptr += bytes_read;
18592 }
18593 else
18594 {
18595 /* Initialize it due to a false compiler warning. */
18596 implicit_const = -1;
18597 }
22d2f3ab
JK
18598
18599 if (abbrev_name == 0)
18600 break;
18601
6dfa2fc2
TT
18602 cur_attrs.emplace_back ();
18603 struct attr_abbrev &cur_attr = cur_attrs.back ();
18604 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18605 cur_attr.form = (enum dwarf_form) abbrev_form;
18606 cur_attr.implicit_const = implicit_const;
22d2f3ab 18607 ++cur_abbrev->num_attrs;
c906108c
SS
18608 }
18609
8d749320
SM
18610 cur_abbrev->attrs =
18611 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18612 cur_abbrev->num_attrs);
6dfa2fc2 18613 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18614 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18615
685af9cd 18616 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18617
18618 /* Get next abbreviation.
18619 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18620 always properly terminated with an abbrev number of 0.
18621 Exit loop if we encounter an abbreviation which we have
18622 already read (which means we are about to read the abbreviations
18623 for the next compile unit) or if the end of the abbreviation
18624 table is reached. */
433df2d4 18625 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18626 break;
18627 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18628 abbrev_ptr += bytes_read;
685af9cd 18629 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18630 break;
18631 }
f3dd6933 18632
433df2d4 18633 return abbrev_table;
c906108c
SS
18634}
18635
72bf9492
DJ
18636/* Returns nonzero if TAG represents a type that we might generate a partial
18637 symbol for. */
18638
18639static int
18640is_type_tag_for_partial (int tag)
18641{
18642 switch (tag)
18643 {
18644#if 0
18645 /* Some types that would be reasonable to generate partial symbols for,
18646 that we don't at present. */
18647 case DW_TAG_array_type:
18648 case DW_TAG_file_type:
18649 case DW_TAG_ptr_to_member_type:
18650 case DW_TAG_set_type:
18651 case DW_TAG_string_type:
18652 case DW_TAG_subroutine_type:
18653#endif
18654 case DW_TAG_base_type:
18655 case DW_TAG_class_type:
680b30c7 18656 case DW_TAG_interface_type:
72bf9492
DJ
18657 case DW_TAG_enumeration_type:
18658 case DW_TAG_structure_type:
18659 case DW_TAG_subrange_type:
18660 case DW_TAG_typedef:
18661 case DW_TAG_union_type:
18662 return 1;
18663 default:
18664 return 0;
18665 }
18666}
18667
18668/* Load all DIEs that are interesting for partial symbols into memory. */
18669
18670static struct partial_die_info *
dee91e82 18671load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18672 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18673{
dee91e82 18674 struct dwarf2_cu *cu = reader->cu;
518817b3 18675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18676 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18677 unsigned int bytes_read;
5afb4e99 18678 unsigned int load_all = 0;
72bf9492
DJ
18679 int nesting_level = 1;
18680
18681 parent_die = NULL;
18682 last_die = NULL;
18683
7adf1e79
DE
18684 gdb_assert (cu->per_cu != NULL);
18685 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18686 load_all = 1;
18687
72bf9492
DJ
18688 cu->partial_dies
18689 = htab_create_alloc_ex (cu->header.length / 12,
18690 partial_die_hash,
18691 partial_die_eq,
18692 NULL,
18693 &cu->comp_unit_obstack,
18694 hashtab_obstack_allocate,
18695 dummy_obstack_deallocate);
18696
72bf9492
DJ
18697 while (1)
18698 {
685af9cd 18699 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18700
18701 /* A NULL abbrev means the end of a series of children. */
18702 if (abbrev == NULL)
18703 {
18704 if (--nesting_level == 0)
cd9983dd
YQ
18705 return first_die;
18706
72bf9492
DJ
18707 info_ptr += bytes_read;
18708 last_die = parent_die;
18709 parent_die = parent_die->die_parent;
18710 continue;
18711 }
18712
98bfdba5
PA
18713 /* Check for template arguments. We never save these; if
18714 they're seen, we just mark the parent, and go on our way. */
18715 if (parent_die != NULL
18716 && cu->language == language_cplus
18717 && (abbrev->tag == DW_TAG_template_type_param
18718 || abbrev->tag == DW_TAG_template_value_param))
18719 {
18720 parent_die->has_template_arguments = 1;
18721
18722 if (!load_all)
18723 {
18724 /* We don't need a partial DIE for the template argument. */
dee91e82 18725 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18726 continue;
18727 }
18728 }
18729
0d99eb77 18730 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18731 Skip their other children. */
18732 if (!load_all
18733 && cu->language == language_cplus
18734 && parent_die != NULL
18735 && parent_die->tag == DW_TAG_subprogram)
18736 {
dee91e82 18737 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18738 continue;
18739 }
18740
5afb4e99
DJ
18741 /* Check whether this DIE is interesting enough to save. Normally
18742 we would not be interested in members here, but there may be
18743 later variables referencing them via DW_AT_specification (for
18744 static members). */
18745 if (!load_all
18746 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18747 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18748 && abbrev->tag != DW_TAG_enumerator
18749 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18750 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18751 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18752 && abbrev->tag != DW_TAG_variable
5afb4e99 18753 && abbrev->tag != DW_TAG_namespace
f55ee35c 18754 && abbrev->tag != DW_TAG_module
95554aad 18755 && abbrev->tag != DW_TAG_member
74921315
KS
18756 && abbrev->tag != DW_TAG_imported_unit
18757 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18758 {
18759 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18760 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18761 continue;
18762 }
18763
6f06d47b
YQ
18764 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18765 abbrev);
cd9983dd 18766
48fbe735 18767 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18768
18769 /* This two-pass algorithm for processing partial symbols has a
18770 high cost in cache pressure. Thus, handle some simple cases
18771 here which cover the majority of C partial symbols. DIEs
18772 which neither have specification tags in them, nor could have
18773 specification tags elsewhere pointing at them, can simply be
18774 processed and discarded.
18775
18776 This segment is also optional; scan_partial_symbols and
18777 add_partial_symbol will handle these DIEs if we chain
18778 them in normally. When compilers which do not emit large
18779 quantities of duplicate debug information are more common,
18780 this code can probably be removed. */
18781
18782 /* Any complete simple types at the top level (pretty much all
18783 of them, for a language without namespaces), can be processed
18784 directly. */
18785 if (parent_die == NULL
cd9983dd
YQ
18786 && pdi.has_specification == 0
18787 && pdi.is_declaration == 0
18788 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18789 || pdi.tag == DW_TAG_base_type
18790 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18791 {
cd9983dd 18792 if (building_psymtab && pdi.name != NULL)
31edb802 18793 add_psymbol_to_list (pdi.name, false,
79748972 18794 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18795 psymbol_placement::STATIC,
1762568f 18796 0, cu->language, objfile);
cd9983dd 18797 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18798 continue;
18799 }
18800
d8228535
JK
18801 /* The exception for DW_TAG_typedef with has_children above is
18802 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18803 type_name_or_error will error on such types later.
d8228535
JK
18804
18805 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18806 it could not find the child DIEs referenced later, this is checked
18807 above. In correct DWARF DW_TAG_typedef should have no children. */
18808
cd9983dd 18809 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18810 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18811 "- DIE at %s [in module %s]"),
cd9983dd 18812 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18813
72bf9492
DJ
18814 /* If we're at the second level, and we're an enumerator, and
18815 our parent has no specification (meaning possibly lives in a
18816 namespace elsewhere), then we can add the partial symbol now
18817 instead of queueing it. */
cd9983dd 18818 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18819 && parent_die != NULL
18820 && parent_die->die_parent == NULL
18821 && parent_die->tag == DW_TAG_enumeration_type
18822 && parent_die->has_specification == 0)
18823 {
cd9983dd 18824 if (pdi.name == NULL)
b98664d3 18825 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18826 else if (building_psymtab)
31edb802 18827 add_psymbol_to_list (pdi.name, false,
79748972 18828 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18829 cu->language == language_cplus
75aedd27
TT
18830 ? psymbol_placement::GLOBAL
18831 : psymbol_placement::STATIC,
1762568f 18832 0, cu->language, objfile);
72bf9492 18833
cd9983dd 18834 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18835 continue;
18836 }
18837
cd9983dd 18838 struct partial_die_info *part_die
6f06d47b 18839 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18840
72bf9492
DJ
18841 /* We'll save this DIE so link it in. */
18842 part_die->die_parent = parent_die;
18843 part_die->die_sibling = NULL;
18844 part_die->die_child = NULL;
18845
18846 if (last_die && last_die == parent_die)
18847 last_die->die_child = part_die;
18848 else if (last_die)
18849 last_die->die_sibling = part_die;
18850
18851 last_die = part_die;
18852
18853 if (first_die == NULL)
18854 first_die = part_die;
18855
18856 /* Maybe add the DIE to the hash table. Not all DIEs that we
18857 find interesting need to be in the hash table, because we
18858 also have the parent/sibling/child chains; only those that we
18859 might refer to by offset later during partial symbol reading.
18860
18861 For now this means things that might have be the target of a
18862 DW_AT_specification, DW_AT_abstract_origin, or
18863 DW_AT_extension. DW_AT_extension will refer only to
18864 namespaces; DW_AT_abstract_origin refers to functions (and
18865 many things under the function DIE, but we do not recurse
18866 into function DIEs during partial symbol reading) and
18867 possibly variables as well; DW_AT_specification refers to
18868 declarations. Declarations ought to have the DW_AT_declaration
18869 flag. It happens that GCC forgets to put it in sometimes, but
18870 only for functions, not for types.
18871
18872 Adding more things than necessary to the hash table is harmless
18873 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18874 wasted time in find_partial_die, when we reread the compilation
18875 unit with load_all_dies set. */
72bf9492 18876
5afb4e99 18877 if (load_all
72929c62 18878 || abbrev->tag == DW_TAG_constant
5afb4e99 18879 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18880 || abbrev->tag == DW_TAG_variable
18881 || abbrev->tag == DW_TAG_namespace
18882 || part_die->is_declaration)
18883 {
18884 void **slot;
18885
18886 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18887 to_underlying (part_die->sect_off),
18888 INSERT);
72bf9492
DJ
18889 *slot = part_die;
18890 }
18891
72bf9492 18892 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18893 we have no reason to follow the children of structures; for other
98bfdba5
PA
18894 languages we have to, so that we can get at method physnames
18895 to infer fully qualified class names, for DW_AT_specification,
18896 and for C++ template arguments. For C++, we also look one level
18897 inside functions to find template arguments (if the name of the
18898 function does not already contain the template arguments).
bc30ff58 18899
0a4b0913
AB
18900 For Ada and Fortran, we need to scan the children of subprograms
18901 and lexical blocks as well because these languages allow the
18902 definition of nested entities that could be interesting for the
18903 debugger, such as nested subprograms for instance. */
72bf9492 18904 if (last_die->has_children
5afb4e99
DJ
18905 && (load_all
18906 || last_die->tag == DW_TAG_namespace
f55ee35c 18907 || last_die->tag == DW_TAG_module
72bf9492 18908 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18909 || (cu->language == language_cplus
18910 && last_die->tag == DW_TAG_subprogram
18911 && (last_die->name == NULL
18912 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18913 || (cu->language != language_c
18914 && (last_die->tag == DW_TAG_class_type
680b30c7 18915 || last_die->tag == DW_TAG_interface_type
72bf9492 18916 || last_die->tag == DW_TAG_structure_type
bc30ff58 18917 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18918 || ((cu->language == language_ada
18919 || cu->language == language_fortran)
bc30ff58
JB
18920 && (last_die->tag == DW_TAG_subprogram
18921 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18922 {
18923 nesting_level++;
18924 parent_die = last_die;
18925 continue;
18926 }
18927
18928 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18929 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18930
18931 /* Back to the top, do it again. */
18932 }
18933}
18934
6f06d47b
YQ
18935partial_die_info::partial_die_info (sect_offset sect_off_,
18936 struct abbrev_info *abbrev)
18937 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18938{
18939}
18940
35cc7ed7
YQ
18941/* Read a minimal amount of information into the minimal die structure.
18942 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18943
48fbe735
YQ
18944const gdb_byte *
18945partial_die_info::read (const struct die_reader_specs *reader,
18946 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18947{
dee91e82 18948 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18949 struct dwarf2_per_objfile *dwarf2_per_objfile
18950 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18951 unsigned int i;
c5aa993b 18952 int has_low_pc_attr = 0;
c906108c 18953 int has_high_pc_attr = 0;
91da1414 18954 int high_pc_relative = 0;
c906108c 18955
fd0a254f 18956 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18957 {
48fbe735
YQ
18958 struct attribute attr;
18959
fd0a254f 18960 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18961
18962 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18963 partial symbol table. */
c906108c
SS
18964 switch (attr.name)
18965 {
18966 case DW_AT_name:
48fbe735 18967 switch (tag)
71c25dea
TT
18968 {
18969 case DW_TAG_compile_unit:
95554aad 18970 case DW_TAG_partial_unit:
348e048f 18971 case DW_TAG_type_unit:
71c25dea
TT
18972 /* Compilation units have a DW_AT_name that is a filename, not
18973 a source language identifier. */
18974 case DW_TAG_enumeration_type:
18975 case DW_TAG_enumerator:
18976 /* These tags always have simple identifiers already; no need
18977 to canonicalize them. */
48fbe735 18978 name = DW_STRING (&attr);
71c25dea
TT
18979 break;
18980 default:
48fbe735
YQ
18981 {
18982 struct objfile *objfile = dwarf2_per_objfile->objfile;
18983
18984 name
18985 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18986 &objfile->per_bfd->storage_obstack);
18987 }
71c25dea
TT
18988 break;
18989 }
c906108c 18990 break;
31ef98ae 18991 case DW_AT_linkage_name:
c906108c 18992 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18993 /* Note that both forms of linkage name might appear. We
18994 assume they will be the same, and we only store the last
18995 one we see. */
48fbe735 18996 linkage_name = DW_STRING (&attr);
c906108c
SS
18997 break;
18998 case DW_AT_low_pc:
18999 has_low_pc_attr = 1;
48fbe735 19000 lowpc = attr_value_as_address (&attr);
c906108c
SS
19001 break;
19002 case DW_AT_high_pc:
19003 has_high_pc_attr = 1;
48fbe735 19004 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
19005 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19006 high_pc_relative = 1;
c906108c
SS
19007 break;
19008 case DW_AT_location:
0963b4bd 19009 /* Support the .debug_loc offsets. */
8e19ed76
PS
19010 if (attr_form_is_block (&attr))
19011 {
48fbe735 19012 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19013 }
3690dd37 19014 else if (attr_form_is_section_offset (&attr))
8e19ed76 19015 {
4d3c2250 19016 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19017 }
19018 else
19019 {
4d3c2250
KB
19020 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19021 "partial symbol information");
8e19ed76 19022 }
c906108c 19023 break;
c906108c 19024 case DW_AT_external:
48fbe735 19025 is_external = DW_UNSND (&attr);
c906108c
SS
19026 break;
19027 case DW_AT_declaration:
48fbe735 19028 is_declaration = DW_UNSND (&attr);
c906108c
SS
19029 break;
19030 case DW_AT_type:
48fbe735 19031 has_type = 1;
c906108c
SS
19032 break;
19033 case DW_AT_abstract_origin:
19034 case DW_AT_specification:
72bf9492 19035 case DW_AT_extension:
48fbe735
YQ
19036 has_specification = 1;
19037 spec_offset = dwarf2_get_ref_die_offset (&attr);
19038 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19039 || cu->per_cu->is_dwz);
c906108c
SS
19040 break;
19041 case DW_AT_sibling:
19042 /* Ignore absolute siblings, they might point outside of
19043 the current compile unit. */
19044 if (attr.form == DW_FORM_ref_addr)
b98664d3 19045 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19046 else
b9502d3f 19047 {
48fbe735 19048 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19049 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19050 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19051
19052 if (sibling_ptr < info_ptr)
b98664d3 19053 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19054 else if (sibling_ptr > reader->buffer_end)
19055 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19056 else
48fbe735 19057 sibling = sibling_ptr;
b9502d3f 19058 }
c906108c 19059 break;
fa4028e9 19060 case DW_AT_byte_size:
48fbe735 19061 has_byte_size = 1;
fa4028e9 19062 break;
ff908ebf 19063 case DW_AT_const_value:
48fbe735 19064 has_const_value = 1;
ff908ebf 19065 break;
68511cec
CES
19066 case DW_AT_calling_convention:
19067 /* DWARF doesn't provide a way to identify a program's source-level
19068 entry point. DW_AT_calling_convention attributes are only meant
19069 to describe functions' calling conventions.
19070
19071 However, because it's a necessary piece of information in
0c1b455e
TT
19072 Fortran, and before DWARF 4 DW_CC_program was the only
19073 piece of debugging information whose definition refers to
19074 a 'main program' at all, several compilers marked Fortran
19075 main programs with DW_CC_program --- even when those
19076 functions use the standard calling conventions.
19077
19078 Although DWARF now specifies a way to provide this
19079 information, we support this practice for backward
19080 compatibility. */
68511cec 19081 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19082 && cu->language == language_fortran)
48fbe735 19083 main_subprogram = 1;
68511cec 19084 break;
481860b3
GB
19085 case DW_AT_inline:
19086 if (DW_UNSND (&attr) == DW_INL_inlined
19087 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19088 may_be_inlined = 1;
481860b3 19089 break;
95554aad
TT
19090
19091 case DW_AT_import:
48fbe735 19092 if (tag == DW_TAG_imported_unit)
36586728 19093 {
48fbe735
YQ
19094 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19095 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19096 || cu->per_cu->is_dwz);
19097 }
95554aad
TT
19098 break;
19099
0c1b455e 19100 case DW_AT_main_subprogram:
48fbe735 19101 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19102 break;
19103
05caa1d2
TT
19104 case DW_AT_ranges:
19105 {
19106 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19107 but that requires a full DIE, so instead we just
19108 reimplement it. */
19109 int need_ranges_base = tag != DW_TAG_compile_unit;
19110 unsigned int ranges_offset = (DW_UNSND (&attr)
19111 + (need_ranges_base
19112 ? cu->ranges_base
19113 : 0));
19114
19115 /* Value of the DW_AT_ranges attribute is the offset in the
19116 .debug_ranges section. */
19117 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19118 nullptr))
19119 has_pc_info = 1;
19120 }
19121 break;
19122
c906108c
SS
19123 default:
19124 break;
19125 }
19126 }
19127
10d06d82
TT
19128 /* For Ada, if both the name and the linkage name appear, we prefer
19129 the latter. This lets "catch exception" work better, regardless
19130 of the order in which the name and linkage name were emitted.
19131 Really, though, this is just a workaround for the fact that gdb
19132 doesn't store both the name and the linkage name. */
19133 if (cu->language == language_ada && linkage_name != nullptr)
19134 name = linkage_name;
19135
91da1414 19136 if (high_pc_relative)
48fbe735 19137 highpc += lowpc;
91da1414 19138
9373cf26
JK
19139 if (has_low_pc_attr && has_high_pc_attr)
19140 {
19141 /* When using the GNU linker, .gnu.linkonce. sections are used to
19142 eliminate duplicate copies of functions and vtables and such.
19143 The linker will arbitrarily choose one and discard the others.
19144 The AT_*_pc values for such functions refer to local labels in
19145 these sections. If the section from that file was discarded, the
19146 labels are not in the output, so the relocs get a value of 0.
19147 If this is a discarded function, mark the pc bounds as invalid,
19148 so that GDB will ignore it. */
48fbe735 19149 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19150 {
48fbe735 19151 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19153
b98664d3 19154 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19155 "for DIE at %s [in module %s]"),
48fbe735
YQ
19156 paddress (gdbarch, lowpc),
19157 sect_offset_str (sect_off),
9d8780f0 19158 objfile_name (objfile));
9373cf26
JK
19159 }
19160 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19161 else if (lowpc >= highpc)
9373cf26 19162 {
48fbe735 19163 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19165
b98664d3 19166 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19167 "for DIE at %s [in module %s]"),
48fbe735
YQ
19168 paddress (gdbarch, lowpc),
19169 paddress (gdbarch, highpc),
19170 sect_offset_str (sect_off),
9c541725 19171 objfile_name (objfile));
9373cf26
JK
19172 }
19173 else
48fbe735 19174 has_pc_info = 1;
9373cf26 19175 }
85cbf3d3 19176
c906108c
SS
19177 return info_ptr;
19178}
19179
72bf9492
DJ
19180/* Find a cached partial DIE at OFFSET in CU. */
19181
d590ff25
YQ
19182struct partial_die_info *
19183dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19184{
19185 struct partial_die_info *lookup_die = NULL;
6f06d47b 19186 struct partial_die_info part_die (sect_off);
72bf9492 19187
9a3c8263 19188 lookup_die = ((struct partial_die_info *)
d590ff25 19189 htab_find_with_hash (partial_dies, &part_die,
9c541725 19190 to_underlying (sect_off)));
72bf9492 19191
72bf9492
DJ
19192 return lookup_die;
19193}
19194
348e048f
DE
19195/* Find a partial DIE at OFFSET, which may or may not be in CU,
19196 except in the case of .debug_types DIEs which do not reference
19197 outside their CU (they do however referencing other types via
55f1336d 19198 DW_FORM_ref_sig8). */
72bf9492 19199
122cf0f2 19200static const struct cu_partial_die_info
9c541725 19201find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19202{
518817b3
SM
19203 struct dwarf2_per_objfile *dwarf2_per_objfile
19204 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19205 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19206 struct dwarf2_per_cu_data *per_cu = NULL;
19207 struct partial_die_info *pd = NULL;
72bf9492 19208
36586728 19209 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19210 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19211 {
d590ff25 19212 pd = cu->find_partial_die (sect_off);
5afb4e99 19213 if (pd != NULL)
fb816e8b 19214 return { cu, pd };
0d99eb77
DE
19215 /* We missed recording what we needed.
19216 Load all dies and try again. */
19217 per_cu = cu->per_cu;
5afb4e99 19218 }
0d99eb77
DE
19219 else
19220 {
19221 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19222 if (cu->per_cu->is_debug_types)
0d99eb77 19223 {
9d8780f0
SM
19224 error (_("Dwarf Error: Type Unit at offset %s contains"
19225 " external reference to offset %s [in module %s].\n"),
19226 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19227 bfd_get_filename (objfile->obfd));
19228 }
9c541725 19229 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19230 dwarf2_per_objfile);
72bf9492 19231
0d99eb77
DE
19232 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19233 load_partial_comp_unit (per_cu);
ae038cb0 19234
0d99eb77 19235 per_cu->cu->last_used = 0;
d590ff25 19236 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19237 }
5afb4e99 19238
dee91e82
DE
19239 /* If we didn't find it, and not all dies have been loaded,
19240 load them all and try again. */
19241
5afb4e99
DJ
19242 if (pd == NULL && per_cu->load_all_dies == 0)
19243 {
5afb4e99 19244 per_cu->load_all_dies = 1;
fd820528
DE
19245
19246 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19247 THIS_CU->cu may already be in use. So we can't just free it and
19248 replace its DIEs with the ones we read in. Instead, we leave those
19249 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19250 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19251 set. */
dee91e82 19252 load_partial_comp_unit (per_cu);
5afb4e99 19253
d590ff25 19254 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19255 }
19256
19257 if (pd == NULL)
19258 internal_error (__FILE__, __LINE__,
9d8780f0 19259 _("could not find partial DIE %s "
3e43a32a 19260 "in cache [from module %s]\n"),
9d8780f0 19261 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19262 return { per_cu->cu, pd };
72bf9492
DJ
19263}
19264
abc72ce4
DE
19265/* See if we can figure out if the class lives in a namespace. We do
19266 this by looking for a member function; its demangled name will
19267 contain namespace info, if there is any. */
19268
19269static void
19270guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19271 struct dwarf2_cu *cu)
19272{
19273 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19274 what template types look like, because the demangler
19275 frequently doesn't give the same name as the debug info. We
19276 could fix this by only using the demangled name to get the
19277 prefix (but see comment in read_structure_type). */
19278
19279 struct partial_die_info *real_pdi;
19280 struct partial_die_info *child_pdi;
19281
19282 /* If this DIE (this DIE's specification, if any) has a parent, then
19283 we should not do this. We'll prepend the parent's fully qualified
19284 name when we create the partial symbol. */
19285
19286 real_pdi = struct_pdi;
19287 while (real_pdi->has_specification)
fb816e8b 19288 {
122cf0f2
AB
19289 auto res = find_partial_die (real_pdi->spec_offset,
19290 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19291 real_pdi = res.pdi;
19292 cu = res.cu;
19293 }
abc72ce4
DE
19294
19295 if (real_pdi->die_parent != NULL)
19296 return;
19297
19298 for (child_pdi = struct_pdi->die_child;
19299 child_pdi != NULL;
19300 child_pdi = child_pdi->die_sibling)
19301 {
19302 if (child_pdi->tag == DW_TAG_subprogram
19303 && child_pdi->linkage_name != NULL)
19304 {
43816ebc
TT
19305 gdb::unique_xmalloc_ptr<char> actual_class_name
19306 (language_class_name_from_physname (cu->language_defn,
19307 child_pdi->linkage_name));
abc72ce4
DE
19308 if (actual_class_name != NULL)
19309 {
518817b3 19310 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19311 struct_pdi->name
021887d8 19312 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19313 actual_class_name.get ());
abc72ce4
DE
19314 }
19315 break;
19316 }
19317 }
19318}
19319
52356b79
YQ
19320void
19321partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19322{
abc72ce4
DE
19323 /* Once we've fixed up a die, there's no point in doing so again.
19324 This also avoids a memory leak if we were to call
19325 guess_partial_die_structure_name multiple times. */
52356b79 19326 if (fixup_called)
abc72ce4
DE
19327 return;
19328
72bf9492
DJ
19329 /* If we found a reference attribute and the DIE has no name, try
19330 to find a name in the referred to DIE. */
19331
52356b79 19332 if (name == NULL && has_specification)
72bf9492
DJ
19333 {
19334 struct partial_die_info *spec_die;
72bf9492 19335
122cf0f2 19336 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19337 spec_die = res.pdi;
19338 cu = res.cu;
72bf9492 19339
52356b79 19340 spec_die->fixup (cu);
72bf9492
DJ
19341
19342 if (spec_die->name)
19343 {
52356b79 19344 name = spec_die->name;
72bf9492
DJ
19345
19346 /* Copy DW_AT_external attribute if it is set. */
19347 if (spec_die->is_external)
52356b79 19348 is_external = spec_die->is_external;
72bf9492
DJ
19349 }
19350 }
19351
19352 /* Set default names for some unnamed DIEs. */
72bf9492 19353
52356b79
YQ
19354 if (name == NULL && tag == DW_TAG_namespace)
19355 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19356
abc72ce4
DE
19357 /* If there is no parent die to provide a namespace, and there are
19358 children, see if we can determine the namespace from their linkage
122d1940 19359 name. */
abc72ce4 19360 if (cu->language == language_cplus
fd5866f6 19361 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19362 && die_parent == NULL
19363 && has_children
19364 && (tag == DW_TAG_class_type
19365 || tag == DW_TAG_structure_type
19366 || tag == DW_TAG_union_type))
19367 guess_partial_die_structure_name (this, cu);
abc72ce4 19368
53832f31
TT
19369 /* GCC might emit a nameless struct or union that has a linkage
19370 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19371 if (name == NULL
19372 && (tag == DW_TAG_class_type
19373 || tag == DW_TAG_interface_type
19374 || tag == DW_TAG_structure_type
19375 || tag == DW_TAG_union_type)
19376 && linkage_name != NULL)
53832f31 19377 {
43816ebc
TT
19378 gdb::unique_xmalloc_ptr<char> demangled
19379 (gdb_demangle (linkage_name, DMGL_TYPES));
19380 if (demangled != nullptr)
53832f31 19381 {
96408a79
SA
19382 const char *base;
19383
19384 /* Strip any leading namespaces/classes, keep only the base name.
19385 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19386 base = strrchr (demangled.get (), ':');
19387 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19388 base++;
19389 else
43816ebc 19390 base = demangled.get ();
96408a79 19391
518817b3 19392 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19393 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19394 }
19395 }
19396
52356b79 19397 fixup_called = 1;
72bf9492
DJ
19398}
19399
a8329558 19400/* Read an attribute value described by an attribute form. */
c906108c 19401
d521ce57 19402static const gdb_byte *
dee91e82
DE
19403read_attribute_value (const struct die_reader_specs *reader,
19404 struct attribute *attr, unsigned form,
43988095 19405 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19406{
dee91e82 19407 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19408 struct dwarf2_per_objfile *dwarf2_per_objfile
19409 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19410 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19411 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19412 bfd *abfd = reader->abfd;
e7c27a73 19413 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19414 unsigned int bytes_read;
19415 struct dwarf_block *blk;
19416
aead7601 19417 attr->form = (enum dwarf_form) form;
a8329558 19418 switch (form)
c906108c 19419 {
c906108c 19420 case DW_FORM_ref_addr:
ae411497 19421 if (cu->header.version == 2)
4568ecf9 19422 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19423 else
4568ecf9
DE
19424 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19425 &cu->header, &bytes_read);
ae411497
TT
19426 info_ptr += bytes_read;
19427 break;
36586728
TT
19428 case DW_FORM_GNU_ref_alt:
19429 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19430 info_ptr += bytes_read;
19431 break;
ae411497 19432 case DW_FORM_addr:
e7c27a73 19433 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19434 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19435 info_ptr += bytes_read;
c906108c
SS
19436 break;
19437 case DW_FORM_block2:
7b5a2f43 19438 blk = dwarf_alloc_block (cu);
c906108c
SS
19439 blk->size = read_2_bytes (abfd, info_ptr);
19440 info_ptr += 2;
19441 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19442 info_ptr += blk->size;
19443 DW_BLOCK (attr) = blk;
19444 break;
19445 case DW_FORM_block4:
7b5a2f43 19446 blk = dwarf_alloc_block (cu);
c906108c
SS
19447 blk->size = read_4_bytes (abfd, info_ptr);
19448 info_ptr += 4;
19449 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19450 info_ptr += blk->size;
19451 DW_BLOCK (attr) = blk;
19452 break;
19453 case DW_FORM_data2:
19454 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19455 info_ptr += 2;
19456 break;
19457 case DW_FORM_data4:
19458 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19459 info_ptr += 4;
19460 break;
19461 case DW_FORM_data8:
19462 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19463 info_ptr += 8;
19464 break;
0224619f
JK
19465 case DW_FORM_data16:
19466 blk = dwarf_alloc_block (cu);
19467 blk->size = 16;
19468 blk->data = read_n_bytes (abfd, info_ptr, 16);
19469 info_ptr += 16;
19470 DW_BLOCK (attr) = blk;
19471 break;
2dc7f7b3
TT
19472 case DW_FORM_sec_offset:
19473 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19474 info_ptr += bytes_read;
19475 break;
c906108c 19476 case DW_FORM_string:
9b1c24c8 19477 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19478 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19479 info_ptr += bytes_read;
19480 break;
4bdf3d34 19481 case DW_FORM_strp:
36586728
TT
19482 if (!cu->per_cu->is_dwz)
19483 {
ed2dc618
SM
19484 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19485 abfd, info_ptr, cu_header,
36586728
TT
19486 &bytes_read);
19487 DW_STRING_IS_CANONICAL (attr) = 0;
19488 info_ptr += bytes_read;
19489 break;
19490 }
19491 /* FALLTHROUGH */
43988095
JK
19492 case DW_FORM_line_strp:
19493 if (!cu->per_cu->is_dwz)
19494 {
ed2dc618
SM
19495 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19496 abfd, info_ptr,
43988095
JK
19497 cu_header, &bytes_read);
19498 DW_STRING_IS_CANONICAL (attr) = 0;
19499 info_ptr += bytes_read;
19500 break;
19501 }
19502 /* FALLTHROUGH */
36586728
TT
19503 case DW_FORM_GNU_strp_alt:
19504 {
ed2dc618 19505 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19506 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19507 &bytes_read);
19508
ed2dc618
SM
19509 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19510 dwz, str_offset);
36586728
TT
19511 DW_STRING_IS_CANONICAL (attr) = 0;
19512 info_ptr += bytes_read;
19513 }
4bdf3d34 19514 break;
2dc7f7b3 19515 case DW_FORM_exprloc:
c906108c 19516 case DW_FORM_block:
7b5a2f43 19517 blk = dwarf_alloc_block (cu);
c906108c
SS
19518 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19519 info_ptr += bytes_read;
19520 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19521 info_ptr += blk->size;
19522 DW_BLOCK (attr) = blk;
19523 break;
19524 case DW_FORM_block1:
7b5a2f43 19525 blk = dwarf_alloc_block (cu);
c906108c
SS
19526 blk->size = read_1_byte (abfd, info_ptr);
19527 info_ptr += 1;
19528 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19529 info_ptr += blk->size;
19530 DW_BLOCK (attr) = blk;
19531 break;
19532 case DW_FORM_data1:
19533 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19534 info_ptr += 1;
19535 break;
19536 case DW_FORM_flag:
19537 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19538 info_ptr += 1;
19539 break;
2dc7f7b3
TT
19540 case DW_FORM_flag_present:
19541 DW_UNSND (attr) = 1;
19542 break;
c906108c
SS
19543 case DW_FORM_sdata:
19544 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19545 info_ptr += bytes_read;
19546 break;
19547 case DW_FORM_udata:
19548 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19549 info_ptr += bytes_read;
19550 break;
19551 case DW_FORM_ref1:
9c541725 19552 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19553 + read_1_byte (abfd, info_ptr));
c906108c
SS
19554 info_ptr += 1;
19555 break;
19556 case DW_FORM_ref2:
9c541725 19557 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19558 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19559 info_ptr += 2;
19560 break;
19561 case DW_FORM_ref4:
9c541725 19562 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19563 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19564 info_ptr += 4;
19565 break;
613e1657 19566 case DW_FORM_ref8:
9c541725 19567 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19568 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19569 info_ptr += 8;
19570 break;
55f1336d 19571 case DW_FORM_ref_sig8:
ac9ec31b 19572 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19573 info_ptr += 8;
19574 break;
c906108c 19575 case DW_FORM_ref_udata:
9c541725 19576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19577 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19578 info_ptr += bytes_read;
19579 break;
c906108c 19580 case DW_FORM_indirect:
a8329558
KW
19581 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19582 info_ptr += bytes_read;
43988095
JK
19583 if (form == DW_FORM_implicit_const)
19584 {
19585 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19586 info_ptr += bytes_read;
19587 }
19588 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19589 info_ptr);
19590 break;
19591 case DW_FORM_implicit_const:
19592 DW_SND (attr) = implicit_const;
a8329558 19593 break;
336d760d 19594 case DW_FORM_addrx:
3019eac3
DE
19595 case DW_FORM_GNU_addr_index:
19596 if (reader->dwo_file == NULL)
19597 {
19598 /* For now flag a hard error.
19599 Later we can turn this into a complaint. */
19600 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19601 dwarf_form_name (form),
19602 bfd_get_filename (abfd));
19603 }
19604 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19605 info_ptr += bytes_read;
19606 break;
cf532bd1 19607 case DW_FORM_strx:
15f18d14
AT
19608 case DW_FORM_strx1:
19609 case DW_FORM_strx2:
19610 case DW_FORM_strx3:
19611 case DW_FORM_strx4:
3019eac3
DE
19612 case DW_FORM_GNU_str_index:
19613 if (reader->dwo_file == NULL)
19614 {
19615 /* For now flag a hard error.
19616 Later we can turn this into a complaint if warranted. */
19617 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19618 dwarf_form_name (form),
19619 bfd_get_filename (abfd));
19620 }
19621 {
15f18d14
AT
19622 ULONGEST str_index;
19623 if (form == DW_FORM_strx1)
19624 {
19625 str_index = read_1_byte (abfd, info_ptr);
19626 info_ptr += 1;
19627 }
19628 else if (form == DW_FORM_strx2)
19629 {
19630 str_index = read_2_bytes (abfd, info_ptr);
19631 info_ptr += 2;
19632 }
19633 else if (form == DW_FORM_strx3)
19634 {
19635 str_index = read_3_bytes (abfd, info_ptr);
19636 info_ptr += 3;
19637 }
19638 else if (form == DW_FORM_strx4)
19639 {
19640 str_index = read_4_bytes (abfd, info_ptr);
19641 info_ptr += 4;
19642 }
19643 else
19644 {
19645 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19646 info_ptr += bytes_read;
19647 }
342587c4 19648 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19649 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19650 }
19651 break;
c906108c 19652 default:
8a3fe4f8 19653 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19654 dwarf_form_name (form),
19655 bfd_get_filename (abfd));
c906108c 19656 }
28e94949 19657
36586728 19658 /* Super hack. */
7771576e 19659 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19660 attr->form = DW_FORM_GNU_ref_alt;
19661
28e94949
JB
19662 /* We have seen instances where the compiler tried to emit a byte
19663 size attribute of -1 which ended up being encoded as an unsigned
19664 0xffffffff. Although 0xffffffff is technically a valid size value,
19665 an object of this size seems pretty unlikely so we can relatively
19666 safely treat these cases as if the size attribute was invalid and
19667 treat them as zero by default. */
19668 if (attr->name == DW_AT_byte_size
19669 && form == DW_FORM_data4
19670 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19671 {
19672 complaint
b98664d3 19673 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19674 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19675 DW_UNSND (attr) = 0;
19676 }
28e94949 19677
c906108c
SS
19678 return info_ptr;
19679}
19680
a8329558
KW
19681/* Read an attribute described by an abbreviated attribute. */
19682
d521ce57 19683static const gdb_byte *
dee91e82
DE
19684read_attribute (const struct die_reader_specs *reader,
19685 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19686 const gdb_byte *info_ptr)
a8329558
KW
19687{
19688 attr->name = abbrev->name;
43988095
JK
19689 return read_attribute_value (reader, attr, abbrev->form,
19690 abbrev->implicit_const, info_ptr);
a8329558
KW
19691}
19692
0963b4bd 19693/* Read dwarf information from a buffer. */
c906108c
SS
19694
19695static unsigned int
a1855c1d 19696read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19697{
fe1b8b76 19698 return bfd_get_8 (abfd, buf);
c906108c
SS
19699}
19700
19701static int
a1855c1d 19702read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19703{
fe1b8b76 19704 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19705}
19706
19707static unsigned int
a1855c1d 19708read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19709{
fe1b8b76 19710 return bfd_get_16 (abfd, buf);
c906108c
SS
19711}
19712
21ae7a4d 19713static int
a1855c1d 19714read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19715{
19716 return bfd_get_signed_16 (abfd, buf);
19717}
19718
15f18d14
AT
19719static unsigned int
19720read_3_bytes (bfd *abfd, const gdb_byte *buf)
19721{
19722 unsigned int result = 0;
19723 for (int i = 0; i < 3; ++i)
19724 {
19725 unsigned char byte = bfd_get_8 (abfd, buf);
19726 buf++;
19727 result |= ((unsigned int) byte << (i * 8));
19728 }
19729 return result;
19730}
19731
c906108c 19732static unsigned int
a1855c1d 19733read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19734{
fe1b8b76 19735 return bfd_get_32 (abfd, buf);
c906108c
SS
19736}
19737
21ae7a4d 19738static int
a1855c1d 19739read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19740{
19741 return bfd_get_signed_32 (abfd, buf);
19742}
19743
93311388 19744static ULONGEST
a1855c1d 19745read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19746{
fe1b8b76 19747 return bfd_get_64 (abfd, buf);
c906108c
SS
19748}
19749
19750static CORE_ADDR
d521ce57 19751read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19752 unsigned int *bytes_read)
c906108c 19753{
e7c27a73 19754 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19755 CORE_ADDR retval = 0;
19756
107d2387 19757 if (cu_header->signed_addr_p)
c906108c 19758 {
107d2387
AC
19759 switch (cu_header->addr_size)
19760 {
19761 case 2:
fe1b8b76 19762 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19763 break;
19764 case 4:
fe1b8b76 19765 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19766 break;
19767 case 8:
fe1b8b76 19768 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19769 break;
19770 default:
8e65ff28 19771 internal_error (__FILE__, __LINE__,
e2e0b3e5 19772 _("read_address: bad switch, signed [in module %s]"),
659b0389 19773 bfd_get_filename (abfd));
107d2387
AC
19774 }
19775 }
19776 else
19777 {
19778 switch (cu_header->addr_size)
19779 {
19780 case 2:
fe1b8b76 19781 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19782 break;
19783 case 4:
fe1b8b76 19784 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19785 break;
19786 case 8:
fe1b8b76 19787 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19788 break;
19789 default:
8e65ff28 19790 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19791 _("read_address: bad switch, "
19792 "unsigned [in module %s]"),
659b0389 19793 bfd_get_filename (abfd));
107d2387 19794 }
c906108c 19795 }
64367e0a 19796
107d2387
AC
19797 *bytes_read = cu_header->addr_size;
19798 return retval;
c906108c
SS
19799}
19800
f7ef9339 19801/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19802 specification allows the initial length to take up either 4 bytes
19803 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19804 bytes describe the length and all offsets will be 8 bytes in length
19805 instead of 4.
19806
f7ef9339
KB
19807 An older, non-standard 64-bit format is also handled by this
19808 function. The older format in question stores the initial length
19809 as an 8-byte quantity without an escape value. Lengths greater
19810 than 2^32 aren't very common which means that the initial 4 bytes
19811 is almost always zero. Since a length value of zero doesn't make
19812 sense for the 32-bit format, this initial zero can be considered to
19813 be an escape value which indicates the presence of the older 64-bit
19814 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19815 greater than 4GB. If it becomes necessary to handle lengths
19816 somewhat larger than 4GB, we could allow other small values (such
19817 as the non-sensical values of 1, 2, and 3) to also be used as
19818 escape values indicating the presence of the old format.
f7ef9339 19819
917c78fc
MK
19820 The value returned via bytes_read should be used to increment the
19821 relevant pointer after calling read_initial_length().
c764a876 19822
613e1657
KB
19823 [ Note: read_initial_length() and read_offset() are based on the
19824 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19825 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19826 from:
19827
f7ef9339 19828 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19829
613e1657
KB
19830 This document is only a draft and is subject to change. (So beware.)
19831
f7ef9339 19832 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19833 determined empirically by examining 64-bit ELF files produced by
19834 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19835
19836 - Kevin, July 16, 2002
613e1657
KB
19837 ] */
19838
19839static LONGEST
d521ce57 19840read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19841{
fe1b8b76 19842 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19843
dd373385 19844 if (length == 0xffffffff)
613e1657 19845 {
fe1b8b76 19846 length = bfd_get_64 (abfd, buf + 4);
613e1657 19847 *bytes_read = 12;
613e1657 19848 }
dd373385 19849 else if (length == 0)
f7ef9339 19850 {
dd373385 19851 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19852 length = bfd_get_64 (abfd, buf);
f7ef9339 19853 *bytes_read = 8;
f7ef9339 19854 }
613e1657
KB
19855 else
19856 {
19857 *bytes_read = 4;
613e1657
KB
19858 }
19859
c764a876
DE
19860 return length;
19861}
dd373385 19862
c764a876
DE
19863/* Cover function for read_initial_length.
19864 Returns the length of the object at BUF, and stores the size of the
19865 initial length in *BYTES_READ and stores the size that offsets will be in
19866 *OFFSET_SIZE.
19867 If the initial length size is not equivalent to that specified in
19868 CU_HEADER then issue a complaint.
19869 This is useful when reading non-comp-unit headers. */
dd373385 19870
c764a876 19871static LONGEST
d521ce57 19872read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19873 const struct comp_unit_head *cu_header,
19874 unsigned int *bytes_read,
19875 unsigned int *offset_size)
19876{
19877 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19878
19879 gdb_assert (cu_header->initial_length_size == 4
19880 || cu_header->initial_length_size == 8
19881 || cu_header->initial_length_size == 12);
19882
19883 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19884 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19885
c764a876 19886 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19887 return length;
613e1657
KB
19888}
19889
19890/* Read an offset from the data stream. The size of the offset is
917c78fc 19891 given by cu_header->offset_size. */
613e1657
KB
19892
19893static LONGEST
d521ce57
TT
19894read_offset (bfd *abfd, const gdb_byte *buf,
19895 const struct comp_unit_head *cu_header,
891d2f0b 19896 unsigned int *bytes_read)
c764a876
DE
19897{
19898 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19899
c764a876
DE
19900 *bytes_read = cu_header->offset_size;
19901 return offset;
19902}
19903
19904/* Read an offset from the data stream. */
19905
19906static LONGEST
d521ce57 19907read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19908{
19909 LONGEST retval = 0;
19910
c764a876 19911 switch (offset_size)
613e1657
KB
19912 {
19913 case 4:
fe1b8b76 19914 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19915 break;
19916 case 8:
fe1b8b76 19917 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19918 break;
19919 default:
8e65ff28 19920 internal_error (__FILE__, __LINE__,
c764a876 19921 _("read_offset_1: bad switch [in module %s]"),
659b0389 19922 bfd_get_filename (abfd));
613e1657
KB
19923 }
19924
917c78fc 19925 return retval;
613e1657
KB
19926}
19927
d521ce57
TT
19928static const gdb_byte *
19929read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19930{
19931 /* If the size of a host char is 8 bits, we can return a pointer
19932 to the buffer, otherwise we have to copy the data to a buffer
19933 allocated on the temporary obstack. */
4bdf3d34 19934 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19935 return buf;
c906108c
SS
19936}
19937
d521ce57
TT
19938static const char *
19939read_direct_string (bfd *abfd, const gdb_byte *buf,
19940 unsigned int *bytes_read_ptr)
c906108c
SS
19941{
19942 /* If the size of a host char is 8 bits, we can return a pointer
19943 to the string, otherwise we have to copy the string to a buffer
19944 allocated on the temporary obstack. */
4bdf3d34 19945 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19946 if (*buf == '\0')
19947 {
19948 *bytes_read_ptr = 1;
19949 return NULL;
19950 }
d521ce57
TT
19951 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19952 return (const char *) buf;
4bdf3d34
JJ
19953}
19954
43988095
JK
19955/* Return pointer to string at section SECT offset STR_OFFSET with error
19956 reporting strings FORM_NAME and SECT_NAME. */
19957
d521ce57 19958static const char *
ed2dc618
SM
19959read_indirect_string_at_offset_from (struct objfile *objfile,
19960 bfd *abfd, LONGEST str_offset,
43988095
JK
19961 struct dwarf2_section_info *sect,
19962 const char *form_name,
19963 const char *sect_name)
19964{
ed2dc618 19965 dwarf2_read_section (objfile, sect);
43988095
JK
19966 if (sect->buffer == NULL)
19967 error (_("%s used without %s section [in module %s]"),
19968 form_name, sect_name, bfd_get_filename (abfd));
19969 if (str_offset >= sect->size)
19970 error (_("%s pointing outside of %s section [in module %s]"),
19971 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19972 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19973 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19974 return NULL;
43988095
JK
19975 return (const char *) (sect->buffer + str_offset);
19976}
19977
19978/* Return pointer to string at .debug_str offset STR_OFFSET. */
19979
19980static const char *
ed2dc618
SM
19981read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19982 bfd *abfd, LONGEST str_offset)
43988095 19983{
ed2dc618
SM
19984 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19985 abfd, str_offset,
43988095
JK
19986 &dwarf2_per_objfile->str,
19987 "DW_FORM_strp", ".debug_str");
19988}
19989
19990/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19991
19992static const char *
ed2dc618
SM
19993read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19994 bfd *abfd, LONGEST str_offset)
43988095 19995{
ed2dc618
SM
19996 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19997 abfd, str_offset,
43988095
JK
19998 &dwarf2_per_objfile->line_str,
19999 "DW_FORM_line_strp",
20000 ".debug_line_str");
c906108c
SS
20001}
20002
36586728
TT
20003/* Read a string at offset STR_OFFSET in the .debug_str section from
20004 the .dwz file DWZ. Throw an error if the offset is too large. If
20005 the string consists of a single NUL byte, return NULL; otherwise
20006 return a pointer to the string. */
20007
d521ce57 20008static const char *
ed2dc618
SM
20009read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20010 LONGEST str_offset)
36586728 20011{
ed2dc618 20012 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20013
20014 if (dwz->str.buffer == NULL)
20015 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20016 "section [in module %s]"),
00f93c44 20017 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20018 if (str_offset >= dwz->str.size)
20019 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20020 ".debug_str section [in module %s]"),
00f93c44 20021 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20022 gdb_assert (HOST_CHAR_BIT == 8);
20023 if (dwz->str.buffer[str_offset] == '\0')
20024 return NULL;
d521ce57 20025 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20026}
20027
43988095
JK
20028/* Return pointer to string at .debug_str offset as read from BUF.
20029 BUF is assumed to be in a compilation unit described by CU_HEADER.
20030 Return *BYTES_READ_PTR count of bytes read from BUF. */
20031
d521ce57 20032static const char *
ed2dc618
SM
20033read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20034 const gdb_byte *buf,
cf2c3c16
TT
20035 const struct comp_unit_head *cu_header,
20036 unsigned int *bytes_read_ptr)
20037{
20038 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20039
ed2dc618 20040 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20041}
20042
43988095
JK
20043/* Return pointer to string at .debug_line_str offset as read from BUF.
20044 BUF is assumed to be in a compilation unit described by CU_HEADER.
20045 Return *BYTES_READ_PTR count of bytes read from BUF. */
20046
20047static const char *
ed2dc618
SM
20048read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20049 bfd *abfd, const gdb_byte *buf,
43988095
JK
20050 const struct comp_unit_head *cu_header,
20051 unsigned int *bytes_read_ptr)
20052{
20053 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20054
ed2dc618
SM
20055 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20056 str_offset);
43988095
JK
20057}
20058
20059ULONGEST
d521ce57 20060read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20061 unsigned int *bytes_read_ptr)
c906108c 20062{
12df843f 20063 ULONGEST result;
ce5d95e1 20064 unsigned int num_read;
870f88f7 20065 int shift;
c906108c
SS
20066 unsigned char byte;
20067
20068 result = 0;
20069 shift = 0;
20070 num_read = 0;
c906108c
SS
20071 while (1)
20072 {
fe1b8b76 20073 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20074 buf++;
20075 num_read++;
12df843f 20076 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20077 if ((byte & 128) == 0)
20078 {
20079 break;
20080 }
20081 shift += 7;
20082 }
20083 *bytes_read_ptr = num_read;
20084 return result;
20085}
20086
12df843f 20087static LONGEST
d521ce57
TT
20088read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20089 unsigned int *bytes_read_ptr)
c906108c 20090{
4dd1b460 20091 ULONGEST result;
870f88f7 20092 int shift, num_read;
c906108c
SS
20093 unsigned char byte;
20094
20095 result = 0;
20096 shift = 0;
c906108c 20097 num_read = 0;
c906108c
SS
20098 while (1)
20099 {
fe1b8b76 20100 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20101 buf++;
20102 num_read++;
4dd1b460 20103 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20104 shift += 7;
20105 if ((byte & 128) == 0)
20106 {
20107 break;
20108 }
20109 }
77e0b926 20110 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20111 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20112 *bytes_read_ptr = num_read;
20113 return result;
20114}
20115
3019eac3
DE
20116/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20117 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20118 ADDR_SIZE is the size of addresses from the CU header. */
20119
20120static CORE_ADDR
ed2dc618
SM
20121read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20122 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20123{
20124 struct objfile *objfile = dwarf2_per_objfile->objfile;
20125 bfd *abfd = objfile->obfd;
20126 const gdb_byte *info_ptr;
20127
20128 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20129 if (dwarf2_per_objfile->addr.buffer == NULL)
20130 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20131 objfile_name (objfile));
3019eac3
DE
20132 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20133 error (_("DW_FORM_addr_index pointing outside of "
20134 ".debug_addr section [in module %s]"),
4262abfb 20135 objfile_name (objfile));
3019eac3
DE
20136 info_ptr = (dwarf2_per_objfile->addr.buffer
20137 + addr_base + addr_index * addr_size);
20138 if (addr_size == 4)
20139 return bfd_get_32 (abfd, info_ptr);
20140 else
20141 return bfd_get_64 (abfd, info_ptr);
20142}
20143
20144/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20145
20146static CORE_ADDR
20147read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20148{
518817b3
SM
20149 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20150 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20151}
20152
20153/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20154
20155static CORE_ADDR
d521ce57 20156read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20157 unsigned int *bytes_read)
20158{
518817b3 20159 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20160 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20161
20162 return read_addr_index (cu, addr_index);
20163}
20164
20165/* Data structure to pass results from dwarf2_read_addr_index_reader
20166 back to dwarf2_read_addr_index. */
20167
20168struct dwarf2_read_addr_index_data
20169{
20170 ULONGEST addr_base;
20171 int addr_size;
20172};
20173
20174/* die_reader_func for dwarf2_read_addr_index. */
20175
20176static void
20177dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20178 const gdb_byte *info_ptr,
3019eac3
DE
20179 struct die_info *comp_unit_die,
20180 int has_children,
20181 void *data)
20182{
20183 struct dwarf2_cu *cu = reader->cu;
20184 struct dwarf2_read_addr_index_data *aidata =
20185 (struct dwarf2_read_addr_index_data *) data;
20186
20187 aidata->addr_base = cu->addr_base;
20188 aidata->addr_size = cu->header.addr_size;
20189}
20190
20191/* Given an index in .debug_addr, fetch the value.
20192 NOTE: This can be called during dwarf expression evaluation,
20193 long after the debug information has been read, and thus per_cu->cu
20194 may no longer exist. */
20195
20196CORE_ADDR
20197dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20198 unsigned int addr_index)
20199{
ed2dc618 20200 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20201 struct dwarf2_cu *cu = per_cu->cu;
20202 ULONGEST addr_base;
20203 int addr_size;
20204
3019eac3
DE
20205 /* We need addr_base and addr_size.
20206 If we don't have PER_CU->cu, we have to get it.
20207 Nasty, but the alternative is storing the needed info in PER_CU,
20208 which at this point doesn't seem justified: it's not clear how frequently
20209 it would get used and it would increase the size of every PER_CU.
20210 Entry points like dwarf2_per_cu_addr_size do a similar thing
20211 so we're not in uncharted territory here.
20212 Alas we need to be a bit more complicated as addr_base is contained
20213 in the DIE.
20214
20215 We don't need to read the entire CU(/TU).
20216 We just need the header and top level die.
a1b64ce1 20217
3019eac3 20218 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20219 For now we skip this optimization. */
3019eac3
DE
20220
20221 if (cu != NULL)
20222 {
20223 addr_base = cu->addr_base;
20224 addr_size = cu->header.addr_size;
20225 }
20226 else
20227 {
20228 struct dwarf2_read_addr_index_data aidata;
20229
a1b64ce1
DE
20230 /* Note: We can't use init_cutu_and_read_dies_simple here,
20231 we need addr_base. */
58f0c718 20232 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20233 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20234 addr_base = aidata.addr_base;
20235 addr_size = aidata.addr_size;
20236 }
20237
ed2dc618
SM
20238 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20239 addr_size);
3019eac3
DE
20240}
20241
cf532bd1 20242/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20243 This is only used by the Fission support. */
3019eac3 20244
d521ce57 20245static const char *
342587c4 20246read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20247{
ed2dc618 20248 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20249 struct dwarf2_per_objfile *dwarf2_per_objfile
20250 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20251 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20252 const char *objf_name = objfile_name (objfile);
3019eac3 20253 bfd *abfd = objfile->obfd;
73869dc2
DE
20254 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20255 struct dwarf2_section_info *str_offsets_section =
20256 &reader->dwo_file->sections.str_offsets;
d521ce57 20257 const gdb_byte *info_ptr;
3019eac3 20258 ULONGEST str_offset;
cf532bd1 20259 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20260
73869dc2
DE
20261 dwarf2_read_section (objfile, str_section);
20262 dwarf2_read_section (objfile, str_offsets_section);
20263 if (str_section->buffer == NULL)
57d63ce2 20264 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20265 " in CU at offset %s [in module %s]"),
20266 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20267 if (str_offsets_section->buffer == NULL)
57d63ce2 20268 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20269 " in CU at offset %s [in module %s]"),
20270 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20271 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20272 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20273 " section in CU at offset %s [in module %s]"),
20274 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20275 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20276 + str_index * cu->header.offset_size);
20277 if (cu->header.offset_size == 4)
20278 str_offset = bfd_get_32 (abfd, info_ptr);
20279 else
20280 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20281 if (str_offset >= str_section->size)
57d63ce2 20282 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20283 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20284 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20285 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20286}
20287
3019eac3
DE
20288/* Return the length of an LEB128 number in BUF. */
20289
20290static int
20291leb128_size (const gdb_byte *buf)
20292{
20293 const gdb_byte *begin = buf;
20294 gdb_byte byte;
20295
20296 while (1)
20297 {
20298 byte = *buf++;
20299 if ((byte & 128) == 0)
20300 return buf - begin;
20301 }
20302}
20303
c906108c 20304static void
e142c38c 20305set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20306{
20307 switch (lang)
20308 {
20309 case DW_LANG_C89:
76bee0cc 20310 case DW_LANG_C99:
0cfd832f 20311 case DW_LANG_C11:
c906108c 20312 case DW_LANG_C:
d1be3247 20313 case DW_LANG_UPC:
e142c38c 20314 cu->language = language_c;
c906108c 20315 break;
9c37b5ae 20316 case DW_LANG_Java:
c906108c 20317 case DW_LANG_C_plus_plus:
0cfd832f
MW
20318 case DW_LANG_C_plus_plus_11:
20319 case DW_LANG_C_plus_plus_14:
e142c38c 20320 cu->language = language_cplus;
c906108c 20321 break;
6aecb9c2
JB
20322 case DW_LANG_D:
20323 cu->language = language_d;
20324 break;
c906108c
SS
20325 case DW_LANG_Fortran77:
20326 case DW_LANG_Fortran90:
b21b22e0 20327 case DW_LANG_Fortran95:
f7de9aab
MW
20328 case DW_LANG_Fortran03:
20329 case DW_LANG_Fortran08:
e142c38c 20330 cu->language = language_fortran;
c906108c 20331 break;
a766d390
DE
20332 case DW_LANG_Go:
20333 cu->language = language_go;
20334 break;
c906108c 20335 case DW_LANG_Mips_Assembler:
e142c38c 20336 cu->language = language_asm;
c906108c
SS
20337 break;
20338 case DW_LANG_Ada83:
8aaf0b47 20339 case DW_LANG_Ada95:
bc5f45f8
JB
20340 cu->language = language_ada;
20341 break;
72019c9c
GM
20342 case DW_LANG_Modula2:
20343 cu->language = language_m2;
20344 break;
fe8e67fd
PM
20345 case DW_LANG_Pascal83:
20346 cu->language = language_pascal;
20347 break;
22566fbd
DJ
20348 case DW_LANG_ObjC:
20349 cu->language = language_objc;
20350 break;
c44af4eb
TT
20351 case DW_LANG_Rust:
20352 case DW_LANG_Rust_old:
20353 cu->language = language_rust;
20354 break;
c906108c
SS
20355 case DW_LANG_Cobol74:
20356 case DW_LANG_Cobol85:
c906108c 20357 default:
e142c38c 20358 cu->language = language_minimal;
c906108c
SS
20359 break;
20360 }
e142c38c 20361 cu->language_defn = language_def (cu->language);
c906108c
SS
20362}
20363
20364/* Return the named attribute or NULL if not there. */
20365
20366static struct attribute *
e142c38c 20367dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20368{
a48e046c 20369 for (;;)
c906108c 20370 {
a48e046c
TT
20371 unsigned int i;
20372 struct attribute *spec = NULL;
20373
20374 for (i = 0; i < die->num_attrs; ++i)
20375 {
20376 if (die->attrs[i].name == name)
20377 return &die->attrs[i];
20378 if (die->attrs[i].name == DW_AT_specification
20379 || die->attrs[i].name == DW_AT_abstract_origin)
20380 spec = &die->attrs[i];
20381 }
20382
20383 if (!spec)
20384 break;
c906108c 20385
f2f0e013 20386 die = follow_die_ref (die, spec, &cu);
f2f0e013 20387 }
c5aa993b 20388
c906108c
SS
20389 return NULL;
20390}
20391
348e048f
DE
20392/* Return the named attribute or NULL if not there,
20393 but do not follow DW_AT_specification, etc.
20394 This is for use in contexts where we're reading .debug_types dies.
20395 Following DW_AT_specification, DW_AT_abstract_origin will take us
20396 back up the chain, and we want to go down. */
20397
20398static struct attribute *
45e58e77 20399dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20400{
20401 unsigned int i;
20402
20403 for (i = 0; i < die->num_attrs; ++i)
20404 if (die->attrs[i].name == name)
20405 return &die->attrs[i];
20406
20407 return NULL;
20408}
20409
7d45c7c3
KB
20410/* Return the string associated with a string-typed attribute, or NULL if it
20411 is either not found or is of an incorrect type. */
20412
20413static const char *
20414dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20415{
20416 struct attribute *attr;
20417 const char *str = NULL;
20418
20419 attr = dwarf2_attr (die, name, cu);
20420
20421 if (attr != NULL)
20422 {
43988095 20423 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20424 || attr->form == DW_FORM_string
cf532bd1 20425 || attr->form == DW_FORM_strx
8fe0f950
AT
20426 || attr->form == DW_FORM_strx1
20427 || attr->form == DW_FORM_strx2
20428 || attr->form == DW_FORM_strx3
20429 || attr->form == DW_FORM_strx4
b3340438 20430 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20431 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20432 str = DW_STRING (attr);
20433 else
b98664d3 20434 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20435 "DIE at %s in module %s"),
20436 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20437 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20438 }
20439
20440 return str;
20441}
20442
a084a2a6 20443/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20444 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20445static const char *
20446dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20447{
20448 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20449 if (dwo_name == nullptr)
20450 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20451 return dwo_name;
20452}
20453
05cf31d1
JB
20454/* Return non-zero iff the attribute NAME is defined for the given DIE,
20455 and holds a non-zero value. This function should only be used for
2dc7f7b3 20456 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20457
20458static int
20459dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20460{
20461 struct attribute *attr = dwarf2_attr (die, name, cu);
20462
20463 return (attr && DW_UNSND (attr));
20464}
20465
3ca72b44 20466static int
e142c38c 20467die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20468{
05cf31d1
JB
20469 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20470 which value is non-zero. However, we have to be careful with
20471 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20472 (via dwarf2_flag_true_p) follows this attribute. So we may
20473 end up accidently finding a declaration attribute that belongs
20474 to a different DIE referenced by the specification attribute,
20475 even though the given DIE does not have a declaration attribute. */
20476 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20477 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20478}
20479
63d06c5c 20480/* Return the die giving the specification for DIE, if there is
f2f0e013 20481 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20482 containing the return value on output. If there is no
20483 specification, but there is an abstract origin, that is
20484 returned. */
63d06c5c
DC
20485
20486static struct die_info *
f2f0e013 20487die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20488{
f2f0e013
DJ
20489 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20490 *spec_cu);
63d06c5c 20491
edb3359d
DJ
20492 if (spec_attr == NULL)
20493 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20494
63d06c5c
DC
20495 if (spec_attr == NULL)
20496 return NULL;
20497 else
f2f0e013 20498 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20499}
c906108c 20500
527f3840
JK
20501/* Stub for free_line_header to match void * callback types. */
20502
20503static void
20504free_line_header_voidp (void *arg)
20505{
9a3c8263 20506 struct line_header *lh = (struct line_header *) arg;
527f3840 20507
fff8551c 20508 delete lh;
527f3840
JK
20509}
20510
fff8551c
PA
20511void
20512line_header::add_include_dir (const char *include_dir)
c906108c 20513{
27e0867f 20514 if (dwarf_line_debug >= 2)
7ba99d21
AT
20515 {
20516 size_t new_size;
20517 if (version >= 5)
20518 new_size = m_include_dirs.size ();
20519 else
20520 new_size = m_include_dirs.size () + 1;
20521 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20522 new_size, include_dir);
20523 }
20524 m_include_dirs.push_back (include_dir);
debd256d 20525}
6e70227d 20526
fff8551c
PA
20527void
20528line_header::add_file_name (const char *name,
ecfb656c 20529 dir_index d_index,
fff8551c
PA
20530 unsigned int mod_time,
20531 unsigned int length)
debd256d 20532{
27e0867f 20533 if (dwarf_line_debug >= 2)
7ba99d21
AT
20534 {
20535 size_t new_size;
20536 if (version >= 5)
20537 new_size = file_names_size ();
20538 else
20539 new_size = file_names_size () + 1;
20540 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20541 new_size, name);
20542 }
20543 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20544}
6e70227d 20545
83769d0b 20546/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20547
20548static struct dwarf2_section_info *
20549get_debug_line_section (struct dwarf2_cu *cu)
20550{
20551 struct dwarf2_section_info *section;
518817b3
SM
20552 struct dwarf2_per_objfile *dwarf2_per_objfile
20553 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20554
20555 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20556 DWO file. */
20557 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20558 section = &cu->dwo_unit->dwo_file->sections.line;
20559 else if (cu->per_cu->is_dwz)
20560 {
ed2dc618 20561 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20562
20563 section = &dwz->line;
20564 }
20565 else
20566 section = &dwarf2_per_objfile->line;
20567
20568 return section;
20569}
20570
43988095
JK
20571/* Read directory or file name entry format, starting with byte of
20572 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20573 entries count and the entries themselves in the described entry
20574 format. */
20575
20576static void
ed2dc618
SM
20577read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20578 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20579 struct line_header *lh,
20580 const struct comp_unit_head *cu_header,
20581 void (*callback) (struct line_header *lh,
20582 const char *name,
ecfb656c 20583 dir_index d_index,
43988095
JK
20584 unsigned int mod_time,
20585 unsigned int length))
20586{
20587 gdb_byte format_count, formati;
20588 ULONGEST data_count, datai;
20589 const gdb_byte *buf = *bufp;
20590 const gdb_byte *format_header_data;
43988095
JK
20591 unsigned int bytes_read;
20592
20593 format_count = read_1_byte (abfd, buf);
20594 buf += 1;
20595 format_header_data = buf;
20596 for (formati = 0; formati < format_count; formati++)
20597 {
20598 read_unsigned_leb128 (abfd, buf, &bytes_read);
20599 buf += bytes_read;
20600 read_unsigned_leb128 (abfd, buf, &bytes_read);
20601 buf += bytes_read;
20602 }
20603
20604 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20605 buf += bytes_read;
20606 for (datai = 0; datai < data_count; datai++)
20607 {
20608 const gdb_byte *format = format_header_data;
20609 struct file_entry fe;
20610
43988095
JK
20611 for (formati = 0; formati < format_count; formati++)
20612 {
ecfb656c 20613 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20614 format += bytes_read;
43988095 20615
ecfb656c 20616 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20617 format += bytes_read;
ecfb656c
PA
20618
20619 gdb::optional<const char *> string;
20620 gdb::optional<unsigned int> uint;
20621
43988095
JK
20622 switch (form)
20623 {
20624 case DW_FORM_string:
ecfb656c 20625 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20626 buf += bytes_read;
20627 break;
20628
20629 case DW_FORM_line_strp:
ed2dc618
SM
20630 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20631 abfd, buf,
ecfb656c
PA
20632 cu_header,
20633 &bytes_read));
43988095
JK
20634 buf += bytes_read;
20635 break;
20636
20637 case DW_FORM_data1:
ecfb656c 20638 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20639 buf += 1;
20640 break;
20641
20642 case DW_FORM_data2:
ecfb656c 20643 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20644 buf += 2;
20645 break;
20646
20647 case DW_FORM_data4:
ecfb656c 20648 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20649 buf += 4;
20650 break;
20651
20652 case DW_FORM_data8:
ecfb656c 20653 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20654 buf += 8;
20655 break;
20656
7ba99d21
AT
20657 case DW_FORM_data16:
20658 /* This is used for MD5, but file_entry does not record MD5s. */
20659 buf += 16;
20660 break;
20661
43988095 20662 case DW_FORM_udata:
ecfb656c 20663 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20664 buf += bytes_read;
20665 break;
20666
20667 case DW_FORM_block:
20668 /* It is valid only for DW_LNCT_timestamp which is ignored by
20669 current GDB. */
20670 break;
20671 }
ecfb656c
PA
20672
20673 switch (content_type)
20674 {
20675 case DW_LNCT_path:
20676 if (string.has_value ())
20677 fe.name = *string;
20678 break;
20679 case DW_LNCT_directory_index:
20680 if (uint.has_value ())
20681 fe.d_index = (dir_index) *uint;
20682 break;
20683 case DW_LNCT_timestamp:
20684 if (uint.has_value ())
20685 fe.mod_time = *uint;
20686 break;
20687 case DW_LNCT_size:
20688 if (uint.has_value ())
20689 fe.length = *uint;
20690 break;
20691 case DW_LNCT_MD5:
20692 break;
20693 default:
b98664d3 20694 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20695 pulongest (content_type));
20696 }
43988095
JK
20697 }
20698
ecfb656c 20699 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20700 }
20701
20702 *bufp = buf;
20703}
20704
debd256d 20705/* Read the statement program header starting at OFFSET in
3019eac3 20706 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20707 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20708 Returns NULL if there is a problem reading the header, e.g., if it
20709 has a version we don't understand.
debd256d
JB
20710
20711 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20712 the returned object point into the dwarf line section buffer,
20713 and must not be freed. */
ae2de4f8 20714
fff8551c 20715static line_header_up
9c541725 20716dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20717{
d521ce57 20718 const gdb_byte *line_ptr;
c764a876 20719 unsigned int bytes_read, offset_size;
debd256d 20720 int i;
d521ce57 20721 const char *cur_dir, *cur_file;
3019eac3
DE
20722 struct dwarf2_section_info *section;
20723 bfd *abfd;
518817b3
SM
20724 struct dwarf2_per_objfile *dwarf2_per_objfile
20725 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20726
36586728 20727 section = get_debug_line_section (cu);
3019eac3
DE
20728 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20729 if (section->buffer == NULL)
debd256d 20730 {
3019eac3 20731 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20732 complaint (_("missing .debug_line.dwo section"));
3019eac3 20733 else
b98664d3 20734 complaint (_("missing .debug_line section"));
debd256d
JB
20735 return 0;
20736 }
20737
fceca515
DE
20738 /* We can't do this until we know the section is non-empty.
20739 Only then do we know we have such a section. */
a32a8923 20740 abfd = get_section_bfd_owner (section);
fceca515 20741
a738430d
MK
20742 /* Make sure that at least there's room for the total_length field.
20743 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20744 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20745 {
4d3c2250 20746 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20747 return 0;
20748 }
20749
fff8551c 20750 line_header_up lh (new line_header ());
debd256d 20751
9c541725 20752 lh->sect_off = sect_off;
527f3840
JK
20753 lh->offset_in_dwz = cu->per_cu->is_dwz;
20754
9c541725 20755 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20756
a738430d 20757 /* Read in the header. */
6e70227d 20758 lh->total_length =
c764a876
DE
20759 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20760 &bytes_read, &offset_size);
debd256d 20761 line_ptr += bytes_read;
7ba99d21
AT
20762
20763 const gdb_byte *start_here = line_ptr;
20764
3019eac3 20765 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20766 {
4d3c2250 20767 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20768 return 0;
20769 }
7ba99d21 20770 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20771 lh->version = read_2_bytes (abfd, line_ptr);
20772 line_ptr += 2;
43988095 20773 if (lh->version > 5)
cd366ee8
DE
20774 {
20775 /* This is a version we don't understand. The format could have
20776 changed in ways we don't handle properly so just punt. */
b98664d3 20777 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20778 return NULL;
20779 }
43988095
JK
20780 if (lh->version >= 5)
20781 {
20782 gdb_byte segment_selector_size;
20783
20784 /* Skip address size. */
20785 read_1_byte (abfd, line_ptr);
20786 line_ptr += 1;
20787
20788 segment_selector_size = read_1_byte (abfd, line_ptr);
20789 line_ptr += 1;
20790 if (segment_selector_size != 0)
20791 {
b98664d3 20792 complaint (_("unsupported segment selector size %u "
43988095
JK
20793 "in .debug_line section"),
20794 segment_selector_size);
20795 return NULL;
20796 }
20797 }
c764a876
DE
20798 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20799 line_ptr += offset_size;
7ba99d21 20800 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20801 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20802 line_ptr += 1;
2dc7f7b3
TT
20803 if (lh->version >= 4)
20804 {
20805 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20806 line_ptr += 1;
20807 }
20808 else
20809 lh->maximum_ops_per_instruction = 1;
20810
20811 if (lh->maximum_ops_per_instruction == 0)
20812 {
20813 lh->maximum_ops_per_instruction = 1;
b98664d3 20814 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20815 "in `.debug_line' section"));
2dc7f7b3
TT
20816 }
20817
debd256d
JB
20818 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20819 line_ptr += 1;
20820 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20821 line_ptr += 1;
20822 lh->line_range = read_1_byte (abfd, line_ptr);
20823 line_ptr += 1;
20824 lh->opcode_base = read_1_byte (abfd, line_ptr);
20825 line_ptr += 1;
fff8551c 20826 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20827
20828 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20829 for (i = 1; i < lh->opcode_base; ++i)
20830 {
20831 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20832 line_ptr += 1;
20833 }
20834
43988095 20835 if (lh->version >= 5)
debd256d 20836 {
43988095 20837 /* Read directory table. */
ed2dc618
SM
20838 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20839 &cu->header,
b926417a 20840 [] (struct line_header *header, const char *name,
ecfb656c 20841 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20842 unsigned int length)
20843 {
b926417a 20844 header->add_include_dir (name);
fff8551c 20845 });
debd256d 20846
43988095 20847 /* Read file name table. */
ed2dc618
SM
20848 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20849 &cu->header,
b926417a 20850 [] (struct line_header *header, const char *name,
ecfb656c 20851 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20852 unsigned int length)
20853 {
b926417a 20854 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20855 });
43988095
JK
20856 }
20857 else
debd256d 20858 {
43988095
JK
20859 /* Read directory table. */
20860 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20861 {
20862 line_ptr += bytes_read;
fff8551c 20863 lh->add_include_dir (cur_dir);
43988095 20864 }
debd256d
JB
20865 line_ptr += bytes_read;
20866
43988095
JK
20867 /* Read file name table. */
20868 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20869 {
ecfb656c
PA
20870 unsigned int mod_time, length;
20871 dir_index d_index;
43988095
JK
20872
20873 line_ptr += bytes_read;
ecfb656c 20874 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20875 line_ptr += bytes_read;
20876 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20877 line_ptr += bytes_read;
20878 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20879 line_ptr += bytes_read;
20880
ecfb656c 20881 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20882 }
20883 line_ptr += bytes_read;
debd256d 20884 }
debd256d 20885
3019eac3 20886 if (line_ptr > (section->buffer + section->size))
b98664d3 20887 complaint (_("line number info header doesn't "
3e43a32a 20888 "fit in `.debug_line' section"));
debd256d 20889
debd256d
JB
20890 return lh;
20891}
c906108c 20892
c6da4cef 20893/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20894 Return the file name of the psymtab for the given file_entry.
c6da4cef 20895 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20896 If space for the result is malloc'd, *NAME_HOLDER will be set.
20897 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20898
d521ce57 20899static const char *
7ba99d21 20900psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20901 const struct partial_symtab *pst,
c89b44cd
TT
20902 const char *comp_dir,
20903 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20904{
d521ce57
TT
20905 const char *include_name = fe.name;
20906 const char *include_name_to_compare = include_name;
72b9f47f 20907 const char *pst_filename;
c6da4cef
DE
20908 int file_is_pst;
20909
8c43009f 20910 const char *dir_name = fe.include_dir (lh);
c6da4cef 20911
c89b44cd 20912 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20913 if (!IS_ABSOLUTE_PATH (include_name)
20914 && (dir_name != NULL || comp_dir != NULL))
20915 {
20916 /* Avoid creating a duplicate psymtab for PST.
20917 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20918 Before we do the comparison, however, we need to account
20919 for DIR_NAME and COMP_DIR.
20920 First prepend dir_name (if non-NULL). If we still don't
20921 have an absolute path prepend comp_dir (if non-NULL).
20922 However, the directory we record in the include-file's
20923 psymtab does not contain COMP_DIR (to match the
20924 corresponding symtab(s)).
20925
20926 Example:
20927
20928 bash$ cd /tmp
20929 bash$ gcc -g ./hello.c
20930 include_name = "hello.c"
20931 dir_name = "."
20932 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20933 DW_AT_name = "./hello.c"
20934
20935 */
c6da4cef
DE
20936
20937 if (dir_name != NULL)
20938 {
c89b44cd
TT
20939 name_holder->reset (concat (dir_name, SLASH_STRING,
20940 include_name, (char *) NULL));
20941 include_name = name_holder->get ();
c6da4cef 20942 include_name_to_compare = include_name;
c6da4cef
DE
20943 }
20944 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20945 {
c89b44cd
TT
20946 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20947 include_name, (char *) NULL));
20948 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20949 }
20950 }
20951
20952 pst_filename = pst->filename;
c89b44cd 20953 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20954 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20955 {
c89b44cd
TT
20956 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20957 pst_filename, (char *) NULL));
20958 pst_filename = copied_name.get ();
c6da4cef
DE
20959 }
20960
1e3fad37 20961 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20962
c6da4cef
DE
20963 if (file_is_pst)
20964 return NULL;
20965 return include_name;
20966}
20967
d9b3de22
DE
20968/* State machine to track the state of the line number program. */
20969
6f77053d 20970class lnp_state_machine
d9b3de22 20971{
6f77053d
PA
20972public:
20973 /* Initialize a machine state for the start of a line number
20974 program. */
804d2729
TT
20975 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20976 bool record_lines_p);
6f77053d 20977
8c43009f
PA
20978 file_entry *current_file ()
20979 {
20980 /* lh->file_names is 0-based, but the file name numbers in the
20981 statement program are 1-based. */
6f77053d
PA
20982 return m_line_header->file_name_at (m_file);
20983 }
20984
20985 /* Record the line in the state machine. END_SEQUENCE is true if
20986 we're processing the end of a sequence. */
20987 void record_line (bool end_sequence);
20988
7ab6656f
OJ
20989 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20990 nop-out rest of the lines in this sequence. */
6f77053d
PA
20991 void check_line_address (struct dwarf2_cu *cu,
20992 const gdb_byte *line_ptr,
7ab6656f 20993 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20994
20995 void handle_set_discriminator (unsigned int discriminator)
20996 {
20997 m_discriminator = discriminator;
20998 m_line_has_non_zero_discriminator |= discriminator != 0;
20999 }
21000
21001 /* Handle DW_LNE_set_address. */
21002 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21003 {
21004 m_op_index = 0;
21005 address += baseaddr;
21006 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21007 }
21008
21009 /* Handle DW_LNS_advance_pc. */
21010 void handle_advance_pc (CORE_ADDR adjust);
21011
21012 /* Handle a special opcode. */
21013 void handle_special_opcode (unsigned char op_code);
21014
21015 /* Handle DW_LNS_advance_line. */
21016 void handle_advance_line (int line_delta)
21017 {
21018 advance_line (line_delta);
21019 }
21020
21021 /* Handle DW_LNS_set_file. */
21022 void handle_set_file (file_name_index file);
21023
21024 /* Handle DW_LNS_negate_stmt. */
21025 void handle_negate_stmt ()
21026 {
21027 m_is_stmt = !m_is_stmt;
21028 }
21029
21030 /* Handle DW_LNS_const_add_pc. */
21031 void handle_const_add_pc ();
21032
21033 /* Handle DW_LNS_fixed_advance_pc. */
21034 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21035 {
21036 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21037 m_op_index = 0;
21038 }
21039
21040 /* Handle DW_LNS_copy. */
21041 void handle_copy ()
21042 {
21043 record_line (false);
21044 m_discriminator = 0;
21045 }
21046
21047 /* Handle DW_LNE_end_sequence. */
21048 void handle_end_sequence ()
21049 {
804d2729 21050 m_currently_recording_lines = true;
6f77053d
PA
21051 }
21052
21053private:
21054 /* Advance the line by LINE_DELTA. */
21055 void advance_line (int line_delta)
21056 {
21057 m_line += line_delta;
21058
21059 if (line_delta != 0)
21060 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21061 }
21062
804d2729
TT
21063 struct dwarf2_cu *m_cu;
21064
6f77053d
PA
21065 gdbarch *m_gdbarch;
21066
21067 /* True if we're recording lines.
21068 Otherwise we're building partial symtabs and are just interested in
21069 finding include files mentioned by the line number program. */
21070 bool m_record_lines_p;
21071
8c43009f 21072 /* The line number header. */
6f77053d 21073 line_header *m_line_header;
8c43009f 21074
6f77053d
PA
21075 /* These are part of the standard DWARF line number state machine,
21076 and initialized according to the DWARF spec. */
d9b3de22 21077
6f77053d 21078 unsigned char m_op_index = 0;
7ba99d21
AT
21079 /* The line table index of the current file. */
21080 file_name_index m_file = 1;
6f77053d
PA
21081 unsigned int m_line = 1;
21082
21083 /* These are initialized in the constructor. */
21084
21085 CORE_ADDR m_address;
21086 bool m_is_stmt;
21087 unsigned int m_discriminator;
d9b3de22
DE
21088
21089 /* Additional bits of state we need to track. */
21090
21091 /* The last file that we called dwarf2_start_subfile for.
21092 This is only used for TLLs. */
6f77053d 21093 unsigned int m_last_file = 0;
d9b3de22 21094 /* The last file a line number was recorded for. */
6f77053d 21095 struct subfile *m_last_subfile = NULL;
d9b3de22 21096
804d2729
TT
21097 /* When true, record the lines we decode. */
21098 bool m_currently_recording_lines = false;
d9b3de22
DE
21099
21100 /* The last line number that was recorded, used to coalesce
21101 consecutive entries for the same line. This can happen, for
21102 example, when discriminators are present. PR 17276. */
6f77053d
PA
21103 unsigned int m_last_line = 0;
21104 bool m_line_has_non_zero_discriminator = false;
8c43009f 21105};
d9b3de22 21106
6f77053d
PA
21107void
21108lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21109{
21110 CORE_ADDR addr_adj = (((m_op_index + adjust)
21111 / m_line_header->maximum_ops_per_instruction)
21112 * m_line_header->minimum_instruction_length);
21113 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21114 m_op_index = ((m_op_index + adjust)
21115 % m_line_header->maximum_ops_per_instruction);
21116}
d9b3de22 21117
6f77053d
PA
21118void
21119lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21120{
6f77053d
PA
21121 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21122 CORE_ADDR addr_adj = (((m_op_index
21123 + (adj_opcode / m_line_header->line_range))
21124 / m_line_header->maximum_ops_per_instruction)
21125 * m_line_header->minimum_instruction_length);
21126 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21127 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21128 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21129
6f77053d
PA
21130 int line_delta = (m_line_header->line_base
21131 + (adj_opcode % m_line_header->line_range));
21132 advance_line (line_delta);
21133 record_line (false);
21134 m_discriminator = 0;
21135}
d9b3de22 21136
6f77053d
PA
21137void
21138lnp_state_machine::handle_set_file (file_name_index file)
21139{
21140 m_file = file;
21141
21142 const file_entry *fe = current_file ();
21143 if (fe == NULL)
21144 dwarf2_debug_line_missing_file_complaint ();
21145 else if (m_record_lines_p)
21146 {
21147 const char *dir = fe->include_dir (m_line_header);
21148
c24bdb02 21149 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21150 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21151 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21152 }
21153}
21154
21155void
21156lnp_state_machine::handle_const_add_pc ()
21157{
21158 CORE_ADDR adjust
21159 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21160
21161 CORE_ADDR addr_adj
21162 = (((m_op_index + adjust)
21163 / m_line_header->maximum_ops_per_instruction)
21164 * m_line_header->minimum_instruction_length);
21165
21166 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21167 m_op_index = ((m_op_index + adjust)
21168 % m_line_header->maximum_ops_per_instruction);
21169}
d9b3de22 21170
a05a36a5
DE
21171/* Return non-zero if we should add LINE to the line number table.
21172 LINE is the line to add, LAST_LINE is the last line that was added,
21173 LAST_SUBFILE is the subfile for LAST_LINE.
21174 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21175 had a non-zero discriminator.
21176
21177 We have to be careful in the presence of discriminators.
21178 E.g., for this line:
21179
21180 for (i = 0; i < 100000; i++);
21181
21182 clang can emit four line number entries for that one line,
21183 each with a different discriminator.
21184 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21185
21186 However, we want gdb to coalesce all four entries into one.
21187 Otherwise the user could stepi into the middle of the line and
21188 gdb would get confused about whether the pc really was in the
21189 middle of the line.
21190
21191 Things are further complicated by the fact that two consecutive
21192 line number entries for the same line is a heuristic used by gcc
21193 to denote the end of the prologue. So we can't just discard duplicate
21194 entries, we have to be selective about it. The heuristic we use is
21195 that we only collapse consecutive entries for the same line if at least
21196 one of those entries has a non-zero discriminator. PR 17276.
21197
21198 Note: Addresses in the line number state machine can never go backwards
21199 within one sequence, thus this coalescing is ok. */
21200
21201static int
804d2729
TT
21202dwarf_record_line_p (struct dwarf2_cu *cu,
21203 unsigned int line, unsigned int last_line,
a05a36a5
DE
21204 int line_has_non_zero_discriminator,
21205 struct subfile *last_subfile)
21206{
c24bdb02 21207 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21208 return 1;
21209 if (line != last_line)
21210 return 1;
21211 /* Same line for the same file that we've seen already.
21212 As a last check, for pr 17276, only record the line if the line
21213 has never had a non-zero discriminator. */
21214 if (!line_has_non_zero_discriminator)
21215 return 1;
21216 return 0;
21217}
21218
804d2729
TT
21219/* Use the CU's builder to record line number LINE beginning at
21220 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21221
21222static void
d9b3de22
DE
21223dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21224 unsigned int line, CORE_ADDR address,
804d2729 21225 struct dwarf2_cu *cu)
252a6764
DE
21226{
21227 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21228
27e0867f
DE
21229 if (dwarf_line_debug)
21230 {
21231 fprintf_unfiltered (gdb_stdlog,
21232 "Recording line %u, file %s, address %s\n",
21233 line, lbasename (subfile->name),
21234 paddress (gdbarch, address));
21235 }
21236
804d2729 21237 if (cu != nullptr)
c24bdb02 21238 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21239}
21240
21241/* Subroutine of dwarf_decode_lines_1 to simplify it.
21242 Mark the end of a set of line number records.
d9b3de22 21243 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21244 If SUBFILE is NULL the request is ignored. */
21245
21246static void
21247dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21248 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21249{
27e0867f
DE
21250 if (subfile == NULL)
21251 return;
21252
21253 if (dwarf_line_debug)
21254 {
21255 fprintf_unfiltered (gdb_stdlog,
21256 "Finishing current line, file %s, address %s\n",
21257 lbasename (subfile->name),
21258 paddress (gdbarch, address));
21259 }
21260
804d2729 21261 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21262}
21263
6f77053d
PA
21264void
21265lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21266{
d9b3de22
DE
21267 if (dwarf_line_debug)
21268 {
21269 fprintf_unfiltered (gdb_stdlog,
21270 "Processing actual line %u: file %u,"
21271 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21272 m_line, m_file,
6f77053d
PA
21273 paddress (m_gdbarch, m_address),
21274 m_is_stmt, m_discriminator);
d9b3de22
DE
21275 }
21276
6f77053d 21277 file_entry *fe = current_file ();
8c43009f
PA
21278
21279 if (fe == NULL)
d9b3de22
DE
21280 dwarf2_debug_line_missing_file_complaint ();
21281 /* For now we ignore lines not starting on an instruction boundary.
21282 But not when processing end_sequence for compatibility with the
21283 previous version of the code. */
6f77053d 21284 else if (m_op_index == 0 || end_sequence)
d9b3de22 21285 {
8c43009f 21286 fe->included_p = 1;
c258c396 21287 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21288 {
c24bdb02 21289 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21290 || end_sequence)
d9b3de22 21291 {
804d2729
TT
21292 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21293 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21294 }
21295
21296 if (!end_sequence)
21297 {
804d2729 21298 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21299 m_line_has_non_zero_discriminator,
21300 m_last_subfile))
d9b3de22 21301 {
c24bdb02 21302 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21303 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21304 builder->get_current_subfile (),
6f77053d 21305 m_line, m_address,
804d2729 21306 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21307 }
c24bdb02 21308 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21309 m_last_line = m_line;
d9b3de22
DE
21310 }
21311 }
21312 }
21313}
21314
804d2729
TT
21315lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21316 line_header *lh, bool record_lines_p)
d9b3de22 21317{
804d2729 21318 m_cu = cu;
6f77053d
PA
21319 m_gdbarch = arch;
21320 m_record_lines_p = record_lines_p;
21321 m_line_header = lh;
d9b3de22 21322
804d2729 21323 m_currently_recording_lines = true;
d9b3de22 21324
d9b3de22
DE
21325 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21326 was a line entry for it so that the backend has a chance to adjust it
21327 and also record it in case it needs it. This is currently used by MIPS
21328 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21329 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21330 m_is_stmt = lh->default_is_stmt;
21331 m_discriminator = 0;
252a6764
DE
21332}
21333
6f77053d
PA
21334void
21335lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21336 const gdb_byte *line_ptr,
7ab6656f 21337 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21338{
7ab6656f
OJ
21339 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21340 the pc range of the CU. However, we restrict the test to only ADDRESS
21341 values of zero to preserve GDB's previous behaviour which is to handle
21342 the specific case of a function being GC'd by the linker. */
924c2928 21343
7ab6656f 21344 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21345 {
21346 /* This line table is for a function which has been
21347 GCd by the linker. Ignore it. PR gdb/12528 */
21348
518817b3 21349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21350 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21351
b98664d3 21352 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21353 line_offset, objfile_name (objfile));
804d2729
TT
21354 m_currently_recording_lines = false;
21355 /* Note: m_currently_recording_lines is left as false until we see
21356 DW_LNE_end_sequence. */
924c2928
DE
21357 }
21358}
21359
f3f5162e 21360/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21361 Process the line number information in LH.
21362 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21363 program in order to set included_p for every referenced header. */
debd256d 21364
c906108c 21365static void
43f3e411
DE
21366dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21367 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21368{
d521ce57
TT
21369 const gdb_byte *line_ptr, *extended_end;
21370 const gdb_byte *line_end;
a8c50c1f 21371 unsigned int bytes_read, extended_len;
699ca60a 21372 unsigned char op_code, extended_op;
e142c38c 21373 CORE_ADDR baseaddr;
518817b3 21374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21375 bfd *abfd = objfile->obfd;
fbf65064 21376 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21377 /* True if we're recording line info (as opposed to building partial
21378 symtabs and just interested in finding include files mentioned by
21379 the line number program). */
21380 bool record_lines_p = !decode_for_pst_p;
e142c38c 21381
6a053cb1 21382 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 21383
debd256d
JB
21384 line_ptr = lh->statement_program_start;
21385 line_end = lh->statement_program_end;
c906108c
SS
21386
21387 /* Read the statement sequences until there's nothing left. */
21388 while (line_ptr < line_end)
21389 {
6f77053d
PA
21390 /* The DWARF line number program state machine. Reset the state
21391 machine at the start of each sequence. */
804d2729 21392 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21393 bool end_sequence = false;
d9b3de22 21394
8c43009f 21395 if (record_lines_p)
c906108c 21396 {
8c43009f
PA
21397 /* Start a subfile for the current file of the state
21398 machine. */
21399 const file_entry *fe = state_machine.current_file ();
21400
21401 if (fe != NULL)
804d2729 21402 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21403 }
21404
a738430d 21405 /* Decode the table. */
d9b3de22 21406 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21407 {
21408 op_code = read_1_byte (abfd, line_ptr);
21409 line_ptr += 1;
9aa1fe7e 21410
debd256d 21411 if (op_code >= lh->opcode_base)
6e70227d 21412 {
8e07a239 21413 /* Special opcode. */
6f77053d 21414 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21415 }
21416 else switch (op_code)
c906108c
SS
21417 {
21418 case DW_LNS_extended_op:
3e43a32a
MS
21419 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21420 &bytes_read);
473b7be6 21421 line_ptr += bytes_read;
a8c50c1f 21422 extended_end = line_ptr + extended_len;
c906108c
SS
21423 extended_op = read_1_byte (abfd, line_ptr);
21424 line_ptr += 1;
21425 switch (extended_op)
21426 {
21427 case DW_LNE_end_sequence:
6f77053d
PA
21428 state_machine.handle_end_sequence ();
21429 end_sequence = true;
c906108c
SS
21430 break;
21431 case DW_LNE_set_address:
d9b3de22
DE
21432 {
21433 CORE_ADDR address
21434 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21435 line_ptr += bytes_read;
6f77053d
PA
21436
21437 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21438 lowpc - baseaddr, address);
6f77053d 21439 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21440 }
c906108c
SS
21441 break;
21442 case DW_LNE_define_file:
debd256d 21443 {
d521ce57 21444 const char *cur_file;
ecfb656c
PA
21445 unsigned int mod_time, length;
21446 dir_index dindex;
6e70227d 21447
3e43a32a
MS
21448 cur_file = read_direct_string (abfd, line_ptr,
21449 &bytes_read);
debd256d 21450 line_ptr += bytes_read;
ecfb656c 21451 dindex = (dir_index)
debd256d
JB
21452 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21453 line_ptr += bytes_read;
21454 mod_time =
21455 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21456 line_ptr += bytes_read;
21457 length =
21458 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21459 line_ptr += bytes_read;
ecfb656c 21460 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21461 }
c906108c 21462 break;
d0c6ba3d 21463 case DW_LNE_set_discriminator:
6f77053d
PA
21464 {
21465 /* The discriminator is not interesting to the
21466 debugger; just ignore it. We still need to
21467 check its value though:
21468 if there are consecutive entries for the same
21469 (non-prologue) line we want to coalesce them.
21470 PR 17276. */
21471 unsigned int discr
21472 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21473 line_ptr += bytes_read;
21474
21475 state_machine.handle_set_discriminator (discr);
21476 }
d0c6ba3d 21477 break;
c906108c 21478 default:
b98664d3 21479 complaint (_("mangled .debug_line section"));
debd256d 21480 return;
c906108c 21481 }
a8c50c1f
DJ
21482 /* Make sure that we parsed the extended op correctly. If e.g.
21483 we expected a different address size than the producer used,
21484 we may have read the wrong number of bytes. */
21485 if (line_ptr != extended_end)
21486 {
b98664d3 21487 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21488 return;
21489 }
c906108c
SS
21490 break;
21491 case DW_LNS_copy:
6f77053d 21492 state_machine.handle_copy ();
c906108c
SS
21493 break;
21494 case DW_LNS_advance_pc:
2dc7f7b3
TT
21495 {
21496 CORE_ADDR adjust
21497 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21498 line_ptr += bytes_read;
6f77053d
PA
21499
21500 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21501 }
c906108c
SS
21502 break;
21503 case DW_LNS_advance_line:
a05a36a5
DE
21504 {
21505 int line_delta
21506 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21507 line_ptr += bytes_read;
6f77053d
PA
21508
21509 state_machine.handle_advance_line (line_delta);
a05a36a5 21510 }
c906108c
SS
21511 break;
21512 case DW_LNS_set_file:
d9b3de22 21513 {
6f77053d 21514 file_name_index file
ecfb656c
PA
21515 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21516 &bytes_read);
d9b3de22 21517 line_ptr += bytes_read;
8c43009f 21518
6f77053d 21519 state_machine.handle_set_file (file);
d9b3de22 21520 }
c906108c
SS
21521 break;
21522 case DW_LNS_set_column:
0ad93d4f 21523 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21524 line_ptr += bytes_read;
21525 break;
21526 case DW_LNS_negate_stmt:
6f77053d 21527 state_machine.handle_negate_stmt ();
c906108c
SS
21528 break;
21529 case DW_LNS_set_basic_block:
c906108c 21530 break;
c2c6d25f
JM
21531 /* Add to the address register of the state machine the
21532 address increment value corresponding to special opcode
a738430d
MK
21533 255. I.e., this value is scaled by the minimum
21534 instruction length since special opcode 255 would have
b021a221 21535 scaled the increment. */
c906108c 21536 case DW_LNS_const_add_pc:
6f77053d 21537 state_machine.handle_const_add_pc ();
c906108c
SS
21538 break;
21539 case DW_LNS_fixed_advance_pc:
3e29f34a 21540 {
6f77053d 21541 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21542 line_ptr += 2;
6f77053d
PA
21543
21544 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21545 }
c906108c 21546 break;
9aa1fe7e 21547 default:
a738430d
MK
21548 {
21549 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21550 int i;
a738430d 21551
debd256d 21552 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21553 {
21554 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21555 line_ptr += bytes_read;
21556 }
21557 }
c906108c
SS
21558 }
21559 }
d9b3de22
DE
21560
21561 if (!end_sequence)
21562 dwarf2_debug_line_missing_end_sequence_complaint ();
21563
21564 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21565 in which case we still finish recording the last line). */
6f77053d 21566 state_machine.record_line (true);
c906108c 21567 }
f3f5162e
DE
21568}
21569
21570/* Decode the Line Number Program (LNP) for the given line_header
21571 structure and CU. The actual information extracted and the type
21572 of structures created from the LNP depends on the value of PST.
21573
21574 1. If PST is NULL, then this procedure uses the data from the program
21575 to create all necessary symbol tables, and their linetables.
21576
21577 2. If PST is not NULL, this procedure reads the program to determine
21578 the list of files included by the unit represented by PST, and
21579 builds all the associated partial symbol tables.
21580
21581 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21582 It is used for relative paths in the line table.
21583 NOTE: When processing partial symtabs (pst != NULL),
21584 comp_dir == pst->dirname.
21585
21586 NOTE: It is important that psymtabs have the same file name (via strcmp)
21587 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21588 symtab we don't use it in the name of the psymtabs we create.
21589 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21590 A good testcase for this is mb-inline.exp.
21591
527f3840
JK
21592 LOWPC is the lowest address in CU (or 0 if not known).
21593
21594 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21595 for its PC<->lines mapping information. Otherwise only the filename
21596 table is read in. */
f3f5162e
DE
21597
21598static void
21599dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21600 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21601 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21602{
518817b3 21603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21604 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21605
527f3840
JK
21606 if (decode_mapping)
21607 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21608
21609 if (decode_for_pst_p)
21610 {
aaa75496
JB
21611 /* Now that we're done scanning the Line Header Program, we can
21612 create the psymtab of each included file. */
7ba99d21
AT
21613 for (auto &file_entry : lh->file_names ())
21614 if (file_entry.included_p == 1)
aaa75496 21615 {
c89b44cd 21616 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21617 const char *include_name =
7ba99d21
AT
21618 psymtab_include_file_name (lh, file_entry, pst,
21619 comp_dir, &name_holder);
c6da4cef 21620 if (include_name != NULL)
aaa75496
JB
21621 dwarf2_create_include_psymtab (include_name, pst, objfile);
21622 }
21623 }
cb1df416
DJ
21624 else
21625 {
21626 /* Make sure a symtab is created for every file, even files
21627 which contain only variables (i.e. no code with associated
21628 line numbers). */
c24bdb02
KS
21629 buildsym_compunit *builder = cu->get_builder ();
21630 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21631
7ba99d21 21632 for (auto &fe : lh->file_names ())
cb1df416 21633 {
804d2729 21634 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21635 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21636 {
c24bdb02 21637 builder->get_current_subfile ()->symtab
804d2729 21638 = allocate_symtab (cust,
c24bdb02 21639 builder->get_current_subfile ()->name);
43f3e411 21640 }
c24bdb02 21641 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21642 }
21643 }
c906108c
SS
21644}
21645
21646/* Start a subfile for DWARF. FILENAME is the name of the file and
21647 DIRNAME the name of the source directory which contains FILENAME
4d663531 21648 or NULL if not known.
c906108c
SS
21649 This routine tries to keep line numbers from identical absolute and
21650 relative file names in a common subfile.
21651
21652 Using the `list' example from the GDB testsuite, which resides in
21653 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21654 of /srcdir/list0.c yields the following debugging information for list0.c:
21655
c5aa993b 21656 DW_AT_name: /srcdir/list0.c
4d663531 21657 DW_AT_comp_dir: /compdir
357e46e7 21658 files.files[0].name: list0.h
c5aa993b 21659 files.files[0].dir: /srcdir
357e46e7 21660 files.files[1].name: list0.c
c5aa993b 21661 files.files[1].dir: /srcdir
c906108c
SS
21662
21663 The line number information for list0.c has to end up in a single
4f1520fb
FR
21664 subfile, so that `break /srcdir/list0.c:1' works as expected.
21665 start_subfile will ensure that this happens provided that we pass the
21666 concatenation of files.files[1].dir and files.files[1].name as the
21667 subfile's name. */
c906108c
SS
21668
21669static void
804d2729
TT
21670dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21671 const char *dirname)
c906108c 21672{
43816ebc 21673 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21674
4d663531 21675 /* In order not to lose the line information directory,
4f1520fb
FR
21676 we concatenate it to the filename when it makes sense.
21677 Note that the Dwarf3 standard says (speaking of filenames in line
21678 information): ``The directory index is ignored for file names
21679 that represent full path names''. Thus ignoring dirname in the
21680 `else' branch below isn't an issue. */
c906108c 21681
d5166ae1 21682 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21683 {
43816ebc
TT
21684 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21685 filename = copy.get ();
d521ce57 21686 }
c906108c 21687
c24bdb02 21688 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21689}
21690
804d2729
TT
21691/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21692 buildsym_compunit constructor. */
f4dc4d17 21693
c24bdb02
KS
21694struct compunit_symtab *
21695dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21696 CORE_ADDR low_pc)
f4dc4d17 21697{
c24bdb02 21698 gdb_assert (m_builder == nullptr);
43f3e411 21699
c24bdb02
KS
21700 m_builder.reset (new struct buildsym_compunit
21701 (per_cu->dwarf2_per_objfile->objfile,
21702 name, comp_dir, language, low_pc));
93b8bea4 21703
c24bdb02 21704 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21705
5922befa 21706 get_builder ()->record_debugformat (xstrprintf ("DWARF %d", this->header.version));
c24bdb02 21707 get_builder ()->record_producer (producer);
f4dc4d17 21708
c24bdb02 21709 processing_has_namespace_info = false;
43f3e411 21710
c24bdb02 21711 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21712}
21713
4c2df51b
DJ
21714static void
21715var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21716 struct dwarf2_cu *cu)
4c2df51b 21717{
518817b3 21718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21719 struct comp_unit_head *cu_header = &cu->header;
21720
4c2df51b
DJ
21721 /* NOTE drow/2003-01-30: There used to be a comment and some special
21722 code here to turn a symbol with DW_AT_external and a
21723 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21724 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21725 with some versions of binutils) where shared libraries could have
21726 relocations against symbols in their debug information - the
21727 minimal symbol would have the right address, but the debug info
21728 would not. It's no longer necessary, because we will explicitly
21729 apply relocations when we read in the debug information now. */
21730
21731 /* A DW_AT_location attribute with no contents indicates that a
21732 variable has been optimized away. */
21733 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21734 {
f1e6e072 21735 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21736 return;
21737 }
21738
21739 /* Handle one degenerate form of location expression specially, to
21740 preserve GDB's previous behavior when section offsets are
336d760d
AT
21741 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21742 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21743
21744 if (attr_form_is_block (attr)
3019eac3
DE
21745 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21746 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21747 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21748 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21749 && (DW_BLOCK (attr)->size
21750 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21751 {
891d2f0b 21752 unsigned int dummy;
4c2df51b 21753
3019eac3 21754 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21755 SET_SYMBOL_VALUE_ADDRESS (sym,
21756 read_address (objfile->obfd,
21757 DW_BLOCK (attr)->data + 1,
21758 cu, &dummy));
3019eac3 21759 else
38583298
TT
21760 SET_SYMBOL_VALUE_ADDRESS
21761 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21762 &dummy));
f1e6e072 21763 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21764 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21765 SET_SYMBOL_VALUE_ADDRESS
21766 (sym,
21767 SYMBOL_VALUE_ADDRESS (sym)
21768 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21769 return;
21770 }
21771
21772 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21773 expression evaluator, and use LOC_COMPUTED only when necessary
21774 (i.e. when the value of a register or memory location is
21775 referenced, or a thread-local block, etc.). Then again, it might
21776 not be worthwhile. I'm assuming that it isn't unless performance
21777 or memory numbers show me otherwise. */
21778
f1e6e072 21779 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21780
f1e6e072 21781 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21782 cu->has_loclist = true;
4c2df51b
DJ
21783}
21784
c906108c
SS
21785/* Given a pointer to a DWARF information entry, figure out if we need
21786 to make a symbol table entry for it, and if so, create a new entry
21787 and return a pointer to it.
21788 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21789 used the passed type.
21790 If SPACE is not NULL, use it to hold the new symbol. If it is
21791 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21792
21793static struct symbol *
5e2db402
TT
21794new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21795 struct symbol *space)
c906108c 21796{
518817b3
SM
21797 struct dwarf2_per_objfile *dwarf2_per_objfile
21798 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21799 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21800 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21801 struct symbol *sym = NULL;
15d034d0 21802 const char *name;
c906108c
SS
21803 struct attribute *attr = NULL;
21804 struct attribute *attr2 = NULL;
e142c38c 21805 CORE_ADDR baseaddr;
e37fd15a
SW
21806 struct pending **list_to_add = NULL;
21807
edb3359d 21808 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21809
6a053cb1 21810 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 21811
94af9270 21812 name = dwarf2_name (die, cu);
c906108c
SS
21813 if (name)
21814 {
94af9270 21815 const char *linkagename;
34eaf542 21816 int suppress_add = 0;
94af9270 21817
34eaf542
TT
21818 if (space)
21819 sym = space;
21820 else
e623cf5d 21821 sym = allocate_symbol (objfile);
c906108c 21822 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21823
21824 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21825 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21826 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21827 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21828
f55ee35c
JK
21829 /* Fortran does not have mangling standard and the mangling does differ
21830 between gfortran, iFort etc. */
21831 if (cu->language == language_fortran
468c0cbb
CB
21832 && symbol_get_demangled_name (sym) == NULL)
21833 symbol_set_demangled_name (sym,
cfc594ee 21834 dwarf2_full_name (name, die, cu),
29df156d 21835 NULL);
f55ee35c 21836
c906108c 21837 /* Default assumptions.
c5aa993b 21838 Use the passed type or decode it from the die. */
176620f1 21839 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21840 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21841 if (type != NULL)
21842 SYMBOL_TYPE (sym) = type;
21843 else
e7c27a73 21844 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21845 attr = dwarf2_attr (die,
21846 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21847 cu);
435d3d88 21848 if (attr != nullptr)
c906108c
SS
21849 {
21850 SYMBOL_LINE (sym) = DW_UNSND (attr);
21851 }
cb1df416 21852
edb3359d
DJ
21853 attr = dwarf2_attr (die,
21854 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21855 cu);
435d3d88 21856 if (attr != nullptr)
cb1df416 21857 {
ecfb656c 21858 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21859 struct file_entry *fe;
9a619af0 21860
ecfb656c
PA
21861 if (cu->line_header != NULL)
21862 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21863 else
21864 fe = NULL;
21865
21866 if (fe == NULL)
b98664d3 21867 complaint (_("file index out of range"));
8c43009f
PA
21868 else
21869 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21870 }
21871
c906108c
SS
21872 switch (die->tag)
21873 {
21874 case DW_TAG_label:
e142c38c 21875 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21876 if (attr != nullptr)
3e29f34a
MR
21877 {
21878 CORE_ADDR addr;
21879
21880 addr = attr_value_as_address (attr);
21881 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21882 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21883 }
0f5238ed
TT
21884 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21885 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21886 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21887 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21888 break;
21889 case DW_TAG_subprogram:
21890 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21891 finish_block. */
f1e6e072 21892 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21894 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21895 || cu->language == language_ada
21896 || cu->language == language_fortran)
c906108c 21897 {
2cfa0c8d 21898 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21899 Ada and Fortran subprograms, whether marked external or
21900 not, are always stored as a global symbol, because we want
21901 to be able to access them globally. For instance, we want
21902 to be able to break on a nested subprogram without having
21903 to specify the context. */
c24bdb02 21904 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21905 }
21906 else
21907 {
e37fd15a 21908 list_to_add = cu->list_in_scope;
c906108c
SS
21909 }
21910 break;
edb3359d
DJ
21911 case DW_TAG_inlined_subroutine:
21912 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21913 finish_block. */
f1e6e072 21914 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21915 SYMBOL_INLINED (sym) = 1;
481860b3 21916 list_to_add = cu->list_in_scope;
edb3359d 21917 break;
34eaf542
TT
21918 case DW_TAG_template_value_param:
21919 suppress_add = 1;
21920 /* Fall through. */
72929c62 21921 case DW_TAG_constant:
c906108c 21922 case DW_TAG_variable:
254e6b9e 21923 case DW_TAG_member:
0963b4bd
MS
21924 /* Compilation with minimal debug info may result in
21925 variables with missing type entries. Change the
21926 misleading `void' type to something sensible. */
c906108c 21927 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21928 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21929
e142c38c 21930 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21931 /* In the case of DW_TAG_member, we should only be called for
21932 static const members. */
21933 if (die->tag == DW_TAG_member)
21934 {
3863f96c
DE
21935 /* dwarf2_add_field uses die_is_declaration,
21936 so we do the same. */
254e6b9e
DE
21937 gdb_assert (die_is_declaration (die, cu));
21938 gdb_assert (attr);
21939 }
435d3d88 21940 if (attr != nullptr)
c906108c 21941 {
e7c27a73 21942 dwarf2_const_value (attr, sym, cu);
e142c38c 21943 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21944 if (!suppress_add)
34eaf542
TT
21945 {
21946 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21947 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21948 else
e37fd15a 21949 list_to_add = cu->list_in_scope;
34eaf542 21950 }
c906108c
SS
21951 break;
21952 }
e142c38c 21953 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21954 if (attr != nullptr)
c906108c 21955 {
e7c27a73 21956 var_decode_location (attr, sym, cu);
e142c38c 21957 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21958
21959 /* Fortran explicitly imports any global symbols to the local
21960 scope by DW_TAG_common_block. */
21961 if (cu->language == language_fortran && die->parent
21962 && die->parent->tag == DW_TAG_common_block)
21963 attr2 = NULL;
21964
caac4577
JG
21965 if (SYMBOL_CLASS (sym) == LOC_STATIC
21966 && SYMBOL_VALUE_ADDRESS (sym) == 0
21967 && !dwarf2_per_objfile->has_section_at_zero)
21968 {
21969 /* When a static variable is eliminated by the linker,
21970 the corresponding debug information is not stripped
21971 out, but the variable address is set to null;
21972 do not add such variables into symbol table. */
21973 }
21974 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21975 {
4b610737
TT
21976 if (SYMBOL_CLASS (sym) == LOC_STATIC
21977 && (objfile->flags & OBJF_MAINLINE) == 0
21978 && dwarf2_per_objfile->can_copy)
21979 {
21980 /* A global static variable might be subject to
21981 copy relocation. We first check for a local
21982 minsym, though, because maybe the symbol was
21983 marked hidden, in which case this would not
21984 apply. */
21985 bound_minimal_symbol found
21986 = (lookup_minimal_symbol_linkage
987012b8 21987 (sym->linkage_name (), objfile));
4b610737
TT
21988 if (found.minsym != nullptr)
21989 sym->maybe_copied = 1;
21990 }
f55ee35c 21991
1c809c68
TT
21992 /* A variable with DW_AT_external is never static,
21993 but it may be block-scoped. */
804d2729 21994 list_to_add
c24bdb02
KS
21995 = ((cu->list_in_scope
21996 == cu->get_builder ()->get_file_symbols ())
21997 ? cu->get_builder ()->get_global_symbols ()
804d2729 21998 : cu->list_in_scope);
1c809c68 21999 }
c906108c 22000 else
e37fd15a 22001 list_to_add = cu->list_in_scope;
c906108c
SS
22002 }
22003 else
22004 {
22005 /* We do not know the address of this symbol.
c5aa993b
JM
22006 If it is an external symbol and we have type information
22007 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22008 The address of the variable will then be determined from
22009 the minimal symbol table whenever the variable is
22010 referenced. */
e142c38c 22011 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22012
22013 /* Fortran explicitly imports any global symbols to the local
22014 scope by DW_TAG_common_block. */
22015 if (cu->language == language_fortran && die->parent
22016 && die->parent->tag == DW_TAG_common_block)
22017 {
22018 /* SYMBOL_CLASS doesn't matter here because
22019 read_common_block is going to reset it. */
22020 if (!suppress_add)
22021 list_to_add = cu->list_in_scope;
22022 }
22023 else if (attr2 && (DW_UNSND (attr2) != 0)
22024 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22025 {
0fe7935b
DJ
22026 /* A variable with DW_AT_external is never static, but it
22027 may be block-scoped. */
804d2729 22028 list_to_add
c24bdb02
KS
22029 = ((cu->list_in_scope
22030 == cu->get_builder ()->get_file_symbols ())
22031 ? cu->get_builder ()->get_global_symbols ()
804d2729 22032 : cu->list_in_scope);
0fe7935b 22033
f1e6e072 22034 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22035 }
442ddf59
JK
22036 else if (!die_is_declaration (die, cu))
22037 {
22038 /* Use the default LOC_OPTIMIZED_OUT class. */
22039 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22040 if (!suppress_add)
22041 list_to_add = cu->list_in_scope;
442ddf59 22042 }
c906108c
SS
22043 }
22044 break;
22045 case DW_TAG_formal_parameter:
a60f3166
TT
22046 {
22047 /* If we are inside a function, mark this as an argument. If
22048 not, we might be looking at an argument to an inlined function
22049 when we do not have enough information to show inlined frames;
22050 pretend it's a local variable in that case so that the user can
22051 still see it. */
804d2729 22052 struct context_stack *curr
c24bdb02 22053 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22054 if (curr != nullptr && curr->name != nullptr)
22055 SYMBOL_IS_ARGUMENT (sym) = 1;
22056 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22057 if (attr != nullptr)
a60f3166
TT
22058 {
22059 var_decode_location (attr, sym, cu);
22060 }
22061 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22062 if (attr != nullptr)
a60f3166
TT
22063 {
22064 dwarf2_const_value (attr, sym, cu);
22065 }
f346a30d 22066
a60f3166
TT
22067 list_to_add = cu->list_in_scope;
22068 }
c906108c
SS
22069 break;
22070 case DW_TAG_unspecified_parameters:
22071 /* From varargs functions; gdb doesn't seem to have any
22072 interest in this information, so just ignore it for now.
22073 (FIXME?) */
22074 break;
34eaf542
TT
22075 case DW_TAG_template_type_param:
22076 suppress_add = 1;
22077 /* Fall through. */
c906108c 22078 case DW_TAG_class_type:
680b30c7 22079 case DW_TAG_interface_type:
c906108c
SS
22080 case DW_TAG_structure_type:
22081 case DW_TAG_union_type:
72019c9c 22082 case DW_TAG_set_type:
c906108c 22083 case DW_TAG_enumeration_type:
f1e6e072 22084 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22085 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22086
63d06c5c 22087 {
9c37b5ae 22088 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22089 really ever be static objects: otherwise, if you try
22090 to, say, break of a class's method and you're in a file
22091 which doesn't mention that class, it won't work unless
22092 the check for all static symbols in lookup_symbol_aux
22093 saves you. See the OtherFileClass tests in
22094 gdb.c++/namespace.exp. */
22095
e37fd15a 22096 if (!suppress_add)
34eaf542 22097 {
c24bdb02 22098 buildsym_compunit *builder = cu->get_builder ();
804d2729 22099 list_to_add
c24bdb02 22100 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22101 && cu->language == language_cplus
c24bdb02 22102 ? builder->get_global_symbols ()
804d2729 22103 : cu->list_in_scope);
63d06c5c 22104
64382290 22105 /* The semantics of C++ state that "struct foo {
9c37b5ae 22106 ... }" also defines a typedef for "foo". */
64382290 22107 if (cu->language == language_cplus
45280282 22108 || cu->language == language_ada
c44af4eb
TT
22109 || cu->language == language_d
22110 || cu->language == language_rust)
64382290
TT
22111 {
22112 /* The symbol's name is already allocated along
22113 with this objfile, so we don't need to
22114 duplicate it for the type. */
22115 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22116 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22117 }
63d06c5c
DC
22118 }
22119 }
c906108c
SS
22120 break;
22121 case DW_TAG_typedef:
f1e6e072 22122 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22123 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22124 list_to_add = cu->list_in_scope;
63d06c5c 22125 break;
c906108c 22126 case DW_TAG_base_type:
a02abb62 22127 case DW_TAG_subrange_type:
f1e6e072 22128 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22129 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22130 list_to_add = cu->list_in_scope;
c906108c
SS
22131 break;
22132 case DW_TAG_enumerator:
e142c38c 22133 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22134 if (attr != nullptr)
c906108c 22135 {
e7c27a73 22136 dwarf2_const_value (attr, sym, cu);
c906108c 22137 }
63d06c5c
DC
22138 {
22139 /* NOTE: carlton/2003-11-10: See comment above in the
22140 DW_TAG_class_type, etc. block. */
22141
804d2729 22142 list_to_add
c24bdb02 22143 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22144 && cu->language == language_cplus
c24bdb02 22145 ? cu->get_builder ()->get_global_symbols ()
804d2729 22146 : cu->list_in_scope);
63d06c5c 22147 }
c906108c 22148 break;
74921315 22149 case DW_TAG_imported_declaration:
5c4e30ca 22150 case DW_TAG_namespace:
f1e6e072 22151 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22152 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22153 break;
530e8392
KB
22154 case DW_TAG_module:
22155 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22156 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22157 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22158 break;
4357ac6c 22159 case DW_TAG_common_block:
f1e6e072 22160 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22161 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22162 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22163 break;
c906108c
SS
22164 default:
22165 /* Not a tag we recognize. Hopefully we aren't processing
22166 trash data, but since we must specifically ignore things
22167 we don't recognize, there is nothing else we should do at
0963b4bd 22168 this point. */
b98664d3 22169 complaint (_("unsupported tag: '%s'"),
4d3c2250 22170 dwarf_tag_name (die->tag));
c906108c
SS
22171 break;
22172 }
df8a16a1 22173
e37fd15a
SW
22174 if (suppress_add)
22175 {
22176 sym->hash_next = objfile->template_symbols;
22177 objfile->template_symbols = sym;
22178 list_to_add = NULL;
22179 }
22180
22181 if (list_to_add != NULL)
d3cb6808 22182 add_symbol_to_list (sym, list_to_add);
e37fd15a 22183
df8a16a1
DJ
22184 /* For the benefit of old versions of GCC, check for anonymous
22185 namespaces based on the demangled name. */
4d4ec4e5 22186 if (!cu->processing_has_namespace_info
94af9270 22187 && cu->language == language_cplus)
c24bdb02 22188 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22189 }
22190 return (sym);
22191}
22192
98bfdba5
PA
22193/* Given an attr with a DW_FORM_dataN value in host byte order,
22194 zero-extend it as appropriate for the symbol's type. The DWARF
22195 standard (v4) is not entirely clear about the meaning of using
22196 DW_FORM_dataN for a constant with a signed type, where the type is
22197 wider than the data. The conclusion of a discussion on the DWARF
22198 list was that this is unspecified. We choose to always zero-extend
22199 because that is the interpretation long in use by GCC. */
c906108c 22200
98bfdba5 22201static gdb_byte *
ff39bb5e 22202dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22203 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22204{
518817b3 22205 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22206 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22207 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22208 LONGEST l = DW_UNSND (attr);
22209
22210 if (bits < sizeof (*value) * 8)
22211 {
22212 l &= ((LONGEST) 1 << bits) - 1;
22213 *value = l;
22214 }
22215 else if (bits == sizeof (*value) * 8)
22216 *value = l;
22217 else
22218 {
224c3ddb 22219 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22220 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22221 return bytes;
22222 }
22223
22224 return NULL;
22225}
22226
22227/* Read a constant value from an attribute. Either set *VALUE, or if
22228 the value does not fit in *VALUE, set *BYTES - either already
22229 allocated on the objfile obstack, or newly allocated on OBSTACK,
22230 or, set *BATON, if we translated the constant to a location
22231 expression. */
22232
22233static void
ff39bb5e 22234dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22235 const char *name, struct obstack *obstack,
22236 struct dwarf2_cu *cu,
d521ce57 22237 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22238 struct dwarf2_locexpr_baton **baton)
22239{
518817b3 22240 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22241 struct comp_unit_head *cu_header = &cu->header;
c906108c 22242 struct dwarf_block *blk;
98bfdba5
PA
22243 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22244 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22245
22246 *value = 0;
22247 *bytes = NULL;
22248 *baton = NULL;
c906108c
SS
22249
22250 switch (attr->form)
22251 {
22252 case DW_FORM_addr:
336d760d 22253 case DW_FORM_addrx:
3019eac3 22254 case DW_FORM_GNU_addr_index:
ac56253d 22255 {
ac56253d
TT
22256 gdb_byte *data;
22257
98bfdba5
PA
22258 if (TYPE_LENGTH (type) != cu_header->addr_size)
22259 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22260 cu_header->addr_size,
98bfdba5 22261 TYPE_LENGTH (type));
ac56253d
TT
22262 /* Symbols of this form are reasonably rare, so we just
22263 piggyback on the existing location code rather than writing
22264 a new implementation of symbol_computed_ops. */
8d749320 22265 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22266 (*baton)->per_cu = cu->per_cu;
22267 gdb_assert ((*baton)->per_cu);
ac56253d 22268
98bfdba5 22269 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22270 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22271 (*baton)->data = data;
ac56253d
TT
22272
22273 data[0] = DW_OP_addr;
22274 store_unsigned_integer (&data[1], cu_header->addr_size,
22275 byte_order, DW_ADDR (attr));
22276 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22277 }
c906108c 22278 break;
4ac36638 22279 case DW_FORM_string:
93b5768b 22280 case DW_FORM_strp:
cf532bd1 22281 case DW_FORM_strx:
3019eac3 22282 case DW_FORM_GNU_str_index:
36586728 22283 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22284 /* DW_STRING is already allocated on the objfile obstack, point
22285 directly to it. */
d521ce57 22286 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22287 break;
c906108c
SS
22288 case DW_FORM_block1:
22289 case DW_FORM_block2:
22290 case DW_FORM_block4:
22291 case DW_FORM_block:
2dc7f7b3 22292 case DW_FORM_exprloc:
0224619f 22293 case DW_FORM_data16:
c906108c 22294 blk = DW_BLOCK (attr);
98bfdba5
PA
22295 if (TYPE_LENGTH (type) != blk->size)
22296 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22297 TYPE_LENGTH (type));
22298 *bytes = blk->data;
c906108c 22299 break;
2df3850c
JM
22300
22301 /* The DW_AT_const_value attributes are supposed to carry the
22302 symbol's value "represented as it would be on the target
22303 architecture." By the time we get here, it's already been
22304 converted to host endianness, so we just need to sign- or
22305 zero-extend it as appropriate. */
22306 case DW_FORM_data1:
3aef2284 22307 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22308 break;
c906108c 22309 case DW_FORM_data2:
3aef2284 22310 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22311 break;
c906108c 22312 case DW_FORM_data4:
3aef2284 22313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22314 break;
c906108c 22315 case DW_FORM_data8:
3aef2284 22316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22317 break;
22318
c906108c 22319 case DW_FORM_sdata:
663c44ac 22320 case DW_FORM_implicit_const:
98bfdba5 22321 *value = DW_SND (attr);
2df3850c
JM
22322 break;
22323
c906108c 22324 case DW_FORM_udata:
98bfdba5 22325 *value = DW_UNSND (attr);
c906108c 22326 break;
2df3850c 22327
c906108c 22328 default:
b98664d3 22329 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22330 dwarf_form_name (attr->form));
98bfdba5 22331 *value = 0;
c906108c
SS
22332 break;
22333 }
22334}
22335
2df3850c 22336
98bfdba5
PA
22337/* Copy constant value from an attribute to a symbol. */
22338
2df3850c 22339static void
ff39bb5e 22340dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22341 struct dwarf2_cu *cu)
2df3850c 22342{
518817b3 22343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22344 LONGEST value;
d521ce57 22345 const gdb_byte *bytes;
98bfdba5 22346 struct dwarf2_locexpr_baton *baton;
2df3850c 22347
98bfdba5 22348 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22349 sym->print_name (),
98bfdba5
PA
22350 &objfile->objfile_obstack, cu,
22351 &value, &bytes, &baton);
2df3850c 22352
98bfdba5
PA
22353 if (baton != NULL)
22354 {
98bfdba5 22355 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22356 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22357 }
22358 else if (bytes != NULL)
22359 {
22360 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22361 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22362 }
22363 else
22364 {
22365 SYMBOL_VALUE (sym) = value;
f1e6e072 22366 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22367 }
2df3850c
JM
22368}
22369
c906108c
SS
22370/* Return the type of the die in question using its DW_AT_type attribute. */
22371
22372static struct type *
e7c27a73 22373die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22374{
c906108c 22375 struct attribute *type_attr;
c906108c 22376
e142c38c 22377 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22378 if (!type_attr)
22379 {
518817b3 22380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22381 /* A missing DW_AT_type represents a void type. */
518817b3 22382 return objfile_type (objfile)->builtin_void;
c906108c 22383 }
348e048f 22384
673bfd45 22385 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22386}
22387
b4ba55a1
JB
22388/* True iff CU's producer generates GNAT Ada auxiliary information
22389 that allows to find parallel types through that information instead
22390 of having to do expensive parallel lookups by type name. */
22391
22392static int
22393need_gnat_info (struct dwarf2_cu *cu)
22394{
de4cb04a
JB
22395 /* Assume that the Ada compiler was GNAT, which always produces
22396 the auxiliary information. */
22397 return (cu->language == language_ada);
b4ba55a1
JB
22398}
22399
b4ba55a1
JB
22400/* Return the auxiliary type of the die in question using its
22401 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22402 attribute is not present. */
22403
22404static struct type *
22405die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22406{
b4ba55a1 22407 struct attribute *type_attr;
b4ba55a1
JB
22408
22409 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22410 if (!type_attr)
22411 return NULL;
22412
673bfd45 22413 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22414}
22415
22416/* If DIE has a descriptive_type attribute, then set the TYPE's
22417 descriptive type accordingly. */
22418
22419static void
22420set_descriptive_type (struct type *type, struct die_info *die,
22421 struct dwarf2_cu *cu)
22422{
22423 struct type *descriptive_type = die_descriptive_type (die, cu);
22424
22425 if (descriptive_type)
22426 {
22427 ALLOCATE_GNAT_AUX_TYPE (type);
22428 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22429 }
22430}
22431
c906108c
SS
22432/* Return the containing type of the die in question using its
22433 DW_AT_containing_type attribute. */
22434
22435static struct type *
e7c27a73 22436die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22437{
c906108c 22438 struct attribute *type_attr;
518817b3 22439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22440
e142c38c 22441 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22442 if (!type_attr)
22443 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22444 "[in module %s]"), objfile_name (objfile));
33ac96f0 22445
673bfd45 22446 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22447}
22448
ac9ec31b
DE
22449/* Return an error marker type to use for the ill formed type in DIE/CU. */
22450
22451static struct type *
22452build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22453{
518817b3
SM
22454 struct dwarf2_per_objfile *dwarf2_per_objfile
22455 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22456 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22457 char *saved;
ac9ec31b 22458
528e1572
SM
22459 std::string message
22460 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22461 objfile_name (objfile),
22462 sect_offset_str (cu->header.sect_off),
22463 sect_offset_str (die->sect_off));
efba19b0 22464 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22465
19f392bc 22466 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22467}
22468
673bfd45 22469/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22470 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22471 DW_AT_containing_type.
673bfd45
DE
22472 If there is no type substitute an error marker. */
22473
c906108c 22474static struct type *
ff39bb5e 22475lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22476 struct dwarf2_cu *cu)
c906108c 22477{
518817b3
SM
22478 struct dwarf2_per_objfile *dwarf2_per_objfile
22479 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22480 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22481 struct type *this_type;
22482
ac9ec31b
DE
22483 gdb_assert (attr->name == DW_AT_type
22484 || attr->name == DW_AT_GNAT_descriptive_type
22485 || attr->name == DW_AT_containing_type);
22486
673bfd45
DE
22487 /* First see if we have it cached. */
22488
36586728
TT
22489 if (attr->form == DW_FORM_GNU_ref_alt)
22490 {
22491 struct dwarf2_per_cu_data *per_cu;
9c541725 22492 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22493
ed2dc618
SM
22494 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22495 dwarf2_per_objfile);
9c541725 22496 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22497 }
7771576e 22498 else if (attr_form_is_ref (attr))
673bfd45 22499 {
9c541725 22500 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22501
9c541725 22502 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22503 }
55f1336d 22504 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22505 {
ac9ec31b 22506 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22507
ac9ec31b 22508 return get_signatured_type (die, signature, cu);
673bfd45
DE
22509 }
22510 else
22511 {
b98664d3 22512 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22513 " at %s [in module %s]"),
22514 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22515 objfile_name (objfile));
ac9ec31b 22516 return build_error_marker_type (cu, die);
673bfd45
DE
22517 }
22518
22519 /* If not cached we need to read it in. */
22520
22521 if (this_type == NULL)
22522 {
ac9ec31b 22523 struct die_info *type_die = NULL;
673bfd45
DE
22524 struct dwarf2_cu *type_cu = cu;
22525
7771576e 22526 if (attr_form_is_ref (attr))
ac9ec31b
DE
22527 type_die = follow_die_ref (die, attr, &type_cu);
22528 if (type_die == NULL)
22529 return build_error_marker_type (cu, die);
22530 /* If we find the type now, it's probably because the type came
3019eac3
DE
22531 from an inter-CU reference and the type's CU got expanded before
22532 ours. */
ac9ec31b 22533 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22534 }
22535
22536 /* If we still don't have a type use an error marker. */
22537
22538 if (this_type == NULL)
ac9ec31b 22539 return build_error_marker_type (cu, die);
673bfd45 22540
f792889a 22541 return this_type;
c906108c
SS
22542}
22543
673bfd45
DE
22544/* Return the type in DIE, CU.
22545 Returns NULL for invalid types.
22546
02142a6c 22547 This first does a lookup in die_type_hash,
673bfd45
DE
22548 and only reads the die in if necessary.
22549
22550 NOTE: This can be called when reading in partial or full symbols. */
22551
f792889a 22552static struct type *
e7c27a73 22553read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22554{
f792889a
DJ
22555 struct type *this_type;
22556
22557 this_type = get_die_type (die, cu);
22558 if (this_type)
22559 return this_type;
22560
673bfd45
DE
22561 return read_type_die_1 (die, cu);
22562}
22563
22564/* Read the type in DIE, CU.
22565 Returns NULL for invalid types. */
22566
22567static struct type *
22568read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22569{
22570 struct type *this_type = NULL;
22571
c906108c
SS
22572 switch (die->tag)
22573 {
22574 case DW_TAG_class_type:
680b30c7 22575 case DW_TAG_interface_type:
c906108c
SS
22576 case DW_TAG_structure_type:
22577 case DW_TAG_union_type:
f792889a 22578 this_type = read_structure_type (die, cu);
c906108c
SS
22579 break;
22580 case DW_TAG_enumeration_type:
f792889a 22581 this_type = read_enumeration_type (die, cu);
c906108c
SS
22582 break;
22583 case DW_TAG_subprogram:
22584 case DW_TAG_subroutine_type:
edb3359d 22585 case DW_TAG_inlined_subroutine:
f792889a 22586 this_type = read_subroutine_type (die, cu);
c906108c
SS
22587 break;
22588 case DW_TAG_array_type:
f792889a 22589 this_type = read_array_type (die, cu);
c906108c 22590 break;
72019c9c 22591 case DW_TAG_set_type:
f792889a 22592 this_type = read_set_type (die, cu);
72019c9c 22593 break;
c906108c 22594 case DW_TAG_pointer_type:
f792889a 22595 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22596 break;
22597 case DW_TAG_ptr_to_member_type:
f792889a 22598 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22599 break;
22600 case DW_TAG_reference_type:
4297a3f0
AV
22601 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22602 break;
22603 case DW_TAG_rvalue_reference_type:
22604 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22605 break;
22606 case DW_TAG_const_type:
f792889a 22607 this_type = read_tag_const_type (die, cu);
c906108c
SS
22608 break;
22609 case DW_TAG_volatile_type:
f792889a 22610 this_type = read_tag_volatile_type (die, cu);
c906108c 22611 break;
06d66ee9
TT
22612 case DW_TAG_restrict_type:
22613 this_type = read_tag_restrict_type (die, cu);
22614 break;
c906108c 22615 case DW_TAG_string_type:
f792889a 22616 this_type = read_tag_string_type (die, cu);
c906108c
SS
22617 break;
22618 case DW_TAG_typedef:
f792889a 22619 this_type = read_typedef (die, cu);
c906108c 22620 break;
a02abb62 22621 case DW_TAG_subrange_type:
f792889a 22622 this_type = read_subrange_type (die, cu);
a02abb62 22623 break;
c906108c 22624 case DW_TAG_base_type:
f792889a 22625 this_type = read_base_type (die, cu);
c906108c 22626 break;
81a17f79 22627 case DW_TAG_unspecified_type:
f792889a 22628 this_type = read_unspecified_type (die, cu);
81a17f79 22629 break;
0114d602
DJ
22630 case DW_TAG_namespace:
22631 this_type = read_namespace_type (die, cu);
22632 break;
f55ee35c
JK
22633 case DW_TAG_module:
22634 this_type = read_module_type (die, cu);
22635 break;
a2c2acaf
MW
22636 case DW_TAG_atomic_type:
22637 this_type = read_tag_atomic_type (die, cu);
22638 break;
c906108c 22639 default:
b98664d3 22640 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22641 dwarf_tag_name (die->tag));
c906108c
SS
22642 break;
22643 }
63d06c5c 22644
f792889a 22645 return this_type;
63d06c5c
DC
22646}
22647
abc72ce4
DE
22648/* See if we can figure out if the class lives in a namespace. We do
22649 this by looking for a member function; its demangled name will
22650 contain namespace info, if there is any.
22651 Return the computed name or NULL.
22652 Space for the result is allocated on the objfile's obstack.
22653 This is the full-die version of guess_partial_die_structure_name.
22654 In this case we know DIE has no useful parent. */
22655
43816ebc 22656static const char *
abc72ce4
DE
22657guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22658{
22659 struct die_info *spec_die;
22660 struct dwarf2_cu *spec_cu;
22661 struct die_info *child;
518817b3 22662 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22663
22664 spec_cu = cu;
22665 spec_die = die_specification (die, &spec_cu);
22666 if (spec_die != NULL)
22667 {
22668 die = spec_die;
22669 cu = spec_cu;
22670 }
22671
22672 for (child = die->child;
22673 child != NULL;
22674 child = child->sibling)
22675 {
22676 if (child->tag == DW_TAG_subprogram)
22677 {
73b9be8b 22678 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22679
7d45c7c3 22680 if (linkage_name != NULL)
abc72ce4 22681 {
43816ebc
TT
22682 gdb::unique_xmalloc_ptr<char> actual_name
22683 (language_class_name_from_physname (cu->language_defn,
22684 linkage_name));
22685 const char *name = NULL;
abc72ce4
DE
22686
22687 if (actual_name != NULL)
22688 {
15d034d0 22689 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22690
22691 if (die_name != NULL
43816ebc 22692 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22693 {
22694 /* Strip off the class name from the full name.
22695 We want the prefix. */
22696 int die_name_len = strlen (die_name);
43816ebc
TT
22697 int actual_name_len = strlen (actual_name.get ());
22698 const char *ptr = actual_name.get ();
abc72ce4
DE
22699
22700 /* Test for '::' as a sanity check. */
22701 if (actual_name_len > die_name_len + 2
43816ebc 22702 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22703 name = obstack_strndup (
e3b94546 22704 &objfile->per_bfd->storage_obstack,
43816ebc 22705 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22706 }
22707 }
abc72ce4
DE
22708 return name;
22709 }
22710 }
22711 }
22712
22713 return NULL;
22714}
22715
96408a79
SA
22716/* GCC might emit a nameless typedef that has a linkage name. Determine the
22717 prefix part in such case. See
22718 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22719
a121b7c1 22720static const char *
96408a79
SA
22721anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22722{
22723 struct attribute *attr;
e6a959d6 22724 const char *base;
96408a79
SA
22725
22726 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22727 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22728 return NULL;
22729
7d45c7c3 22730 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22731 return NULL;
22732
73b9be8b 22733 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22734 if (attr == NULL || DW_STRING (attr) == NULL)
22735 return NULL;
22736
22737 /* dwarf2_name had to be already called. */
22738 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22739
22740 /* Strip the base name, keep any leading namespaces/classes. */
22741 base = strrchr (DW_STRING (attr), ':');
22742 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22743 return "";
22744
518817b3 22745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22746 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22747 DW_STRING (attr),
22748 &base[-1] - DW_STRING (attr));
96408a79
SA
22749}
22750
fdde2d81 22751/* Return the name of the namespace/class that DIE is defined within,
0114d602 22752 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22753
0114d602
DJ
22754 For example, if we're within the method foo() in the following
22755 code:
22756
22757 namespace N {
22758 class C {
22759 void foo () {
22760 }
22761 };
22762 }
22763
22764 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22765
0d5cff50 22766static const char *
e142c38c 22767determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22768{
518817b3
SM
22769 struct dwarf2_per_objfile *dwarf2_per_objfile
22770 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22771 struct die_info *parent, *spec_die;
22772 struct dwarf2_cu *spec_cu;
22773 struct type *parent_type;
a121b7c1 22774 const char *retval;
63d06c5c 22775
9c37b5ae 22776 if (cu->language != language_cplus
c44af4eb
TT
22777 && cu->language != language_fortran && cu->language != language_d
22778 && cu->language != language_rust)
0114d602
DJ
22779 return "";
22780
96408a79
SA
22781 retval = anonymous_struct_prefix (die, cu);
22782 if (retval)
22783 return retval;
22784
0114d602
DJ
22785 /* We have to be careful in the presence of DW_AT_specification.
22786 For example, with GCC 3.4, given the code
22787
22788 namespace N {
22789 void foo() {
22790 // Definition of N::foo.
22791 }
22792 }
22793
22794 then we'll have a tree of DIEs like this:
22795
22796 1: DW_TAG_compile_unit
22797 2: DW_TAG_namespace // N
22798 3: DW_TAG_subprogram // declaration of N::foo
22799 4: DW_TAG_subprogram // definition of N::foo
22800 DW_AT_specification // refers to die #3
22801
22802 Thus, when processing die #4, we have to pretend that we're in
22803 the context of its DW_AT_specification, namely the contex of die
22804 #3. */
22805 spec_cu = cu;
22806 spec_die = die_specification (die, &spec_cu);
22807 if (spec_die == NULL)
22808 parent = die->parent;
22809 else
63d06c5c 22810 {
0114d602
DJ
22811 parent = spec_die->parent;
22812 cu = spec_cu;
63d06c5c 22813 }
0114d602
DJ
22814
22815 if (parent == NULL)
22816 return "";
98bfdba5
PA
22817 else if (parent->building_fullname)
22818 {
22819 const char *name;
22820 const char *parent_name;
22821
22822 /* It has been seen on RealView 2.2 built binaries,
22823 DW_TAG_template_type_param types actually _defined_ as
22824 children of the parent class:
22825
22826 enum E {};
22827 template class <class Enum> Class{};
22828 Class<enum E> class_e;
22829
22830 1: DW_TAG_class_type (Class)
22831 2: DW_TAG_enumeration_type (E)
22832 3: DW_TAG_enumerator (enum1:0)
22833 3: DW_TAG_enumerator (enum2:1)
22834 ...
22835 2: DW_TAG_template_type_param
22836 DW_AT_type DW_FORM_ref_udata (E)
22837
22838 Besides being broken debug info, it can put GDB into an
22839 infinite loop. Consider:
22840
22841 When we're building the full name for Class<E>, we'll start
22842 at Class, and go look over its template type parameters,
22843 finding E. We'll then try to build the full name of E, and
22844 reach here. We're now trying to build the full name of E,
22845 and look over the parent DIE for containing scope. In the
22846 broken case, if we followed the parent DIE of E, we'd again
22847 find Class, and once again go look at its template type
22848 arguments, etc., etc. Simply don't consider such parent die
22849 as source-level parent of this die (it can't be, the language
22850 doesn't allow it), and break the loop here. */
22851 name = dwarf2_name (die, cu);
22852 parent_name = dwarf2_name (parent, cu);
b98664d3 22853 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22854 name ? name : "<unknown>",
22855 parent_name ? parent_name : "<unknown>");
22856 return "";
22857 }
63d06c5c 22858 else
0114d602
DJ
22859 switch (parent->tag)
22860 {
63d06c5c 22861 case DW_TAG_namespace:
0114d602 22862 parent_type = read_type_die (parent, cu);
acebe513
UW
22863 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22864 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22865 Work around this problem here. */
22866 if (cu->language == language_cplus
e86ca25f 22867 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22868 return "";
0114d602 22869 /* We give a name to even anonymous namespaces. */
e86ca25f 22870 return TYPE_NAME (parent_type);
63d06c5c 22871 case DW_TAG_class_type:
680b30c7 22872 case DW_TAG_interface_type:
63d06c5c 22873 case DW_TAG_structure_type:
0114d602 22874 case DW_TAG_union_type:
f55ee35c 22875 case DW_TAG_module:
0114d602 22876 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22877 if (TYPE_NAME (parent_type) != NULL)
22878 return TYPE_NAME (parent_type);
0114d602
DJ
22879 else
22880 /* An anonymous structure is only allowed non-static data
22881 members; no typedefs, no member functions, et cetera.
22882 So it does not need a prefix. */
22883 return "";
abc72ce4 22884 case DW_TAG_compile_unit:
95554aad 22885 case DW_TAG_partial_unit:
abc72ce4
DE
22886 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22887 if (cu->language == language_cplus
fd5866f6 22888 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22889 && die->child != NULL
22890 && (die->tag == DW_TAG_class_type
22891 || die->tag == DW_TAG_structure_type
22892 || die->tag == DW_TAG_union_type))
22893 {
43816ebc 22894 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22895 if (name != NULL)
22896 return name;
22897 }
22898 return "";
0a4b0913
AB
22899 case DW_TAG_subprogram:
22900 /* Nested subroutines in Fortran get a prefix with the name
22901 of the parent's subroutine. */
22902 if (cu->language == language_fortran)
22903 {
22904 if ((die->tag == DW_TAG_subprogram)
22905 && (dwarf2_name (parent, cu) != NULL))
22906 return dwarf2_name (parent, cu);
22907 }
22908 return determine_prefix (parent, cu);
3d567982
TT
22909 case DW_TAG_enumeration_type:
22910 parent_type = read_type_die (parent, cu);
22911 if (TYPE_DECLARED_CLASS (parent_type))
22912 {
e86ca25f
TT
22913 if (TYPE_NAME (parent_type) != NULL)
22914 return TYPE_NAME (parent_type);
3d567982
TT
22915 return "";
22916 }
22917 /* Fall through. */
63d06c5c 22918 default:
8176b9b8 22919 return determine_prefix (parent, cu);
63d06c5c 22920 }
63d06c5c
DC
22921}
22922
3e43a32a
MS
22923/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22924 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22925 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22926 an obconcat, otherwise allocate storage for the result. The CU argument is
22927 used to determine the language and hence, the appropriate separator. */
987504bb 22928
f55ee35c 22929#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22930
22931static char *
f55ee35c
JK
22932typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22933 int physname, struct dwarf2_cu *cu)
63d06c5c 22934{
f55ee35c 22935 const char *lead = "";
5c315b68 22936 const char *sep;
63d06c5c 22937
3e43a32a
MS
22938 if (suffix == NULL || suffix[0] == '\0'
22939 || prefix == NULL || prefix[0] == '\0')
987504bb 22940 sep = "";
45280282
IB
22941 else if (cu->language == language_d)
22942 {
22943 /* For D, the 'main' function could be defined in any module, but it
22944 should never be prefixed. */
22945 if (strcmp (suffix, "D main") == 0)
22946 {
22947 prefix = "";
22948 sep = "";
22949 }
22950 else
22951 sep = ".";
22952 }
f55ee35c
JK
22953 else if (cu->language == language_fortran && physname)
22954 {
22955 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22956 DW_AT_MIPS_linkage_name is preferred and used instead. */
22957
22958 lead = "__";
22959 sep = "_MOD_";
22960 }
987504bb
JJ
22961 else
22962 sep = "::";
63d06c5c 22963
6dd47d34
DE
22964 if (prefix == NULL)
22965 prefix = "";
22966 if (suffix == NULL)
22967 suffix = "";
22968
987504bb
JJ
22969 if (obs == NULL)
22970 {
3e43a32a 22971 char *retval
224c3ddb
SM
22972 = ((char *)
22973 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22974
f55ee35c
JK
22975 strcpy (retval, lead);
22976 strcat (retval, prefix);
6dd47d34
DE
22977 strcat (retval, sep);
22978 strcat (retval, suffix);
63d06c5c
DC
22979 return retval;
22980 }
987504bb
JJ
22981 else
22982 {
22983 /* We have an obstack. */
f55ee35c 22984 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22985 }
63d06c5c
DC
22986}
22987
c906108c
SS
22988/* Return sibling of die, NULL if no sibling. */
22989
f9aca02d 22990static struct die_info *
fba45db2 22991sibling_die (struct die_info *die)
c906108c 22992{
639d11d3 22993 return die->sibling;
c906108c
SS
22994}
22995
71c25dea
TT
22996/* Get name of a die, return NULL if not found. */
22997
15d034d0
TT
22998static const char *
22999dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
23000 struct obstack *obstack)
23001{
23002 if (name && cu->language == language_cplus)
23003 {
2f408ecb 23004 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23005
2f408ecb 23006 if (!canon_name.empty ())
71c25dea 23007 {
2f408ecb 23008 if (canon_name != name)
efba19b0 23009 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23010 }
23011 }
23012
23013 return name;
c906108c
SS
23014}
23015
96553a0c
DE
23016/* Get name of a die, return NULL if not found.
23017 Anonymous namespaces are converted to their magic string. */
9219021c 23018
15d034d0 23019static const char *
e142c38c 23020dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23021{
23022 struct attribute *attr;
518817b3 23023 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23024
e142c38c 23025 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23026 if ((!attr || !DW_STRING (attr))
96553a0c 23027 && die->tag != DW_TAG_namespace
53832f31
TT
23028 && die->tag != DW_TAG_class_type
23029 && die->tag != DW_TAG_interface_type
23030 && die->tag != DW_TAG_structure_type
23031 && die->tag != DW_TAG_union_type)
71c25dea
TT
23032 return NULL;
23033
23034 switch (die->tag)
23035 {
23036 case DW_TAG_compile_unit:
95554aad 23037 case DW_TAG_partial_unit:
71c25dea
TT
23038 /* Compilation units have a DW_AT_name that is a filename, not
23039 a source language identifier. */
23040 case DW_TAG_enumeration_type:
23041 case DW_TAG_enumerator:
23042 /* These tags always have simple identifiers already; no need
23043 to canonicalize them. */
23044 return DW_STRING (attr);
907af001 23045
96553a0c
DE
23046 case DW_TAG_namespace:
23047 if (attr != NULL && DW_STRING (attr) != NULL)
23048 return DW_STRING (attr);
23049 return CP_ANONYMOUS_NAMESPACE_STR;
23050
907af001
UW
23051 case DW_TAG_class_type:
23052 case DW_TAG_interface_type:
23053 case DW_TAG_structure_type:
23054 case DW_TAG_union_type:
23055 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23056 structures or unions. These were of the form "._%d" in GCC 4.1,
23057 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23058 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23059 if (attr && DW_STRING (attr)
61012eef
GB
23060 && (startswith (DW_STRING (attr), "._")
23061 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23062 return NULL;
53832f31
TT
23063
23064 /* GCC might emit a nameless typedef that has a linkage name. See
23065 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23066 if (!attr || DW_STRING (attr) == NULL)
23067 {
73b9be8b 23068 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23069 if (attr == NULL || DW_STRING (attr) == NULL)
23070 return NULL;
23071
df5c6c50
JK
23072 /* Avoid demangling DW_STRING (attr) the second time on a second
23073 call for the same DIE. */
23074 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23075 {
43816ebc
TT
23076 gdb::unique_xmalloc_ptr<char> demangled
23077 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23078
e6a959d6 23079 const char *base;
96408a79 23080
53832f31 23081 /* FIXME: we already did this for the partial symbol... */
34a68019 23082 DW_STRING (attr)
021887d8 23083 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23084 demangled.get ());
53832f31 23085 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23086
23087 /* Strip any leading namespaces/classes, keep only the base name.
23088 DW_AT_name for named DIEs does not contain the prefixes. */
23089 base = strrchr (DW_STRING (attr), ':');
23090 if (base && base > DW_STRING (attr) && base[-1] == ':')
23091 return &base[1];
23092 else
23093 return DW_STRING (attr);
53832f31
TT
23094 }
23095 }
907af001
UW
23096 break;
23097
71c25dea 23098 default:
907af001
UW
23099 break;
23100 }
23101
23102 if (!DW_STRING_IS_CANONICAL (attr))
23103 {
23104 DW_STRING (attr)
23105 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23106 &objfile->per_bfd->storage_obstack);
907af001 23107 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23108 }
907af001 23109 return DW_STRING (attr);
9219021c
DC
23110}
23111
23112/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23113 is none. *EXT_CU is the CU containing DIE on input, and the CU
23114 containing the return value on output. */
9219021c
DC
23115
23116static struct die_info *
f2f0e013 23117dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23118{
23119 struct attribute *attr;
9219021c 23120
f2f0e013 23121 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23122 if (attr == NULL)
23123 return NULL;
23124
f2f0e013 23125 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23126}
23127
fa9c3fa0
TT
23128/* A convenience function that returns an "unknown" DWARF name,
23129 including the value of V. STR is the name of the entity being
23130 printed, e.g., "TAG". */
23131
23132static const char *
23133dwarf_unknown (const char *str, unsigned v)
23134{
23135 char *cell = get_print_cell ();
23136 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23137 return cell;
23138}
23139
c906108c
SS
23140/* Convert a DIE tag into its string name. */
23141
f39c6ffd 23142static const char *
aa1ee363 23143dwarf_tag_name (unsigned tag)
c906108c 23144{
f39c6ffd
TT
23145 const char *name = get_DW_TAG_name (tag);
23146
23147 if (name == NULL)
fa9c3fa0 23148 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23149
23150 return name;
c906108c
SS
23151}
23152
23153/* Convert a DWARF attribute code into its string name. */
23154
f39c6ffd 23155static const char *
aa1ee363 23156dwarf_attr_name (unsigned attr)
c906108c 23157{
f39c6ffd
TT
23158 const char *name;
23159
c764a876 23160#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23161 if (attr == DW_AT_MIPS_fde)
23162 return "DW_AT_MIPS_fde";
23163#else
23164 if (attr == DW_AT_HP_block_index)
23165 return "DW_AT_HP_block_index";
c764a876 23166#endif
f39c6ffd
TT
23167
23168 name = get_DW_AT_name (attr);
23169
23170 if (name == NULL)
fa9c3fa0 23171 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23172
23173 return name;
c906108c
SS
23174}
23175
a084a2a6
AT
23176/* Convert a unit type to corresponding DW_UT name. */
23177
23178static const char *
23179dwarf_unit_type_name (int unit_type) {
23180 switch (unit_type)
23181 {
23182 case 0x01:
23183 return "DW_UT_compile (0x01)";
23184 case 0x02:
23185 return "DW_UT_type (0x02)";
23186 case 0x03:
23187 return "DW_UT_partial (0x03)";
23188 case 0x04:
23189 return "DW_UT_skeleton (0x04)";
23190 case 0x05:
23191 return "DW_UT_split_compile (0x05)";
23192 case 0x06:
23193 return "DW_UT_split_type (0x06)";
23194 case 0x80:
23195 return "DW_UT_lo_user (0x80)";
23196 case 0xff:
23197 return "DW_UT_hi_user (0xff)";
23198 default:
23199 return nullptr;
23200 }
23201}
23202
c906108c
SS
23203/* Convert a DWARF value form code into its string name. */
23204
f39c6ffd 23205static const char *
aa1ee363 23206dwarf_form_name (unsigned form)
c906108c 23207{
f39c6ffd
TT
23208 const char *name = get_DW_FORM_name (form);
23209
23210 if (name == NULL)
fa9c3fa0 23211 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23212
23213 return name;
c906108c
SS
23214}
23215
a121b7c1 23216static const char *
fba45db2 23217dwarf_bool_name (unsigned mybool)
c906108c
SS
23218{
23219 if (mybool)
23220 return "TRUE";
23221 else
23222 return "FALSE";
23223}
23224
23225/* Convert a DWARF type code into its string name. */
23226
f39c6ffd 23227static const char *
aa1ee363 23228dwarf_type_encoding_name (unsigned enc)
c906108c 23229{
f39c6ffd 23230 const char *name = get_DW_ATE_name (enc);
c906108c 23231
f39c6ffd 23232 if (name == NULL)
fa9c3fa0 23233 return dwarf_unknown ("ATE", enc);
c906108c 23234
f39c6ffd 23235 return name;
c906108c 23236}
c906108c 23237
f9aca02d 23238static void
d97bc12b 23239dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23240{
23241 unsigned int i;
23242
d97bc12b 23243 print_spaces (indent, f);
9d8780f0 23244 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23245 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23246 sect_offset_str (die->sect_off));
d97bc12b
DE
23247
23248 if (die->parent != NULL)
23249 {
23250 print_spaces (indent, f);
9d8780f0
SM
23251 fprintf_unfiltered (f, " parent at offset: %s\n",
23252 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23253 }
23254
23255 print_spaces (indent, f);
23256 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23257 dwarf_bool_name (die->child != NULL));
c906108c 23258
d97bc12b
DE
23259 print_spaces (indent, f);
23260 fprintf_unfiltered (f, " attributes:\n");
23261
c906108c
SS
23262 for (i = 0; i < die->num_attrs; ++i)
23263 {
d97bc12b
DE
23264 print_spaces (indent, f);
23265 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23266 dwarf_attr_name (die->attrs[i].name),
23267 dwarf_form_name (die->attrs[i].form));
d97bc12b 23268
c906108c
SS
23269 switch (die->attrs[i].form)
23270 {
c906108c 23271 case DW_FORM_addr:
336d760d 23272 case DW_FORM_addrx:
3019eac3 23273 case DW_FORM_GNU_addr_index:
d97bc12b 23274 fprintf_unfiltered (f, "address: ");
5af949e3 23275 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23276 break;
23277 case DW_FORM_block2:
23278 case DW_FORM_block4:
23279 case DW_FORM_block:
23280 case DW_FORM_block1:
56eb65bd
SP
23281 fprintf_unfiltered (f, "block: size %s",
23282 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23283 break;
2dc7f7b3 23284 case DW_FORM_exprloc:
56eb65bd
SP
23285 fprintf_unfiltered (f, "expression: size %s",
23286 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23287 break;
0224619f
JK
23288 case DW_FORM_data16:
23289 fprintf_unfiltered (f, "constant of 16 bytes");
23290 break;
4568ecf9
DE
23291 case DW_FORM_ref_addr:
23292 fprintf_unfiltered (f, "ref address: ");
23293 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23294 break;
36586728
TT
23295 case DW_FORM_GNU_ref_alt:
23296 fprintf_unfiltered (f, "alt ref address: ");
23297 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23298 break;
10b3939b
DJ
23299 case DW_FORM_ref1:
23300 case DW_FORM_ref2:
23301 case DW_FORM_ref4:
4568ecf9
DE
23302 case DW_FORM_ref8:
23303 case DW_FORM_ref_udata:
d97bc12b 23304 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23305 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23306 break;
c906108c
SS
23307 case DW_FORM_data1:
23308 case DW_FORM_data2:
23309 case DW_FORM_data4:
ce5d95e1 23310 case DW_FORM_data8:
c906108c
SS
23311 case DW_FORM_udata:
23312 case DW_FORM_sdata:
43bbcdc2
PH
23313 fprintf_unfiltered (f, "constant: %s",
23314 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23315 break;
2dc7f7b3
TT
23316 case DW_FORM_sec_offset:
23317 fprintf_unfiltered (f, "section offset: %s",
23318 pulongest (DW_UNSND (&die->attrs[i])));
23319 break;
55f1336d 23320 case DW_FORM_ref_sig8:
ac9ec31b
DE
23321 fprintf_unfiltered (f, "signature: %s",
23322 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23323 break;
c906108c 23324 case DW_FORM_string:
4bdf3d34 23325 case DW_FORM_strp:
43988095 23326 case DW_FORM_line_strp:
cf532bd1 23327 case DW_FORM_strx:
3019eac3 23328 case DW_FORM_GNU_str_index:
36586728 23329 case DW_FORM_GNU_strp_alt:
8285870a 23330 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23331 DW_STRING (&die->attrs[i])
8285870a
JK
23332 ? DW_STRING (&die->attrs[i]) : "",
23333 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23334 break;
23335 case DW_FORM_flag:
23336 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23337 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23338 else
d97bc12b 23339 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23340 break;
2dc7f7b3
TT
23341 case DW_FORM_flag_present:
23342 fprintf_unfiltered (f, "flag: TRUE");
23343 break;
a8329558 23344 case DW_FORM_indirect:
0963b4bd
MS
23345 /* The reader will have reduced the indirect form to
23346 the "base form" so this form should not occur. */
5f48f8f3 23347 fprintf_unfiltered (f,
3e43a32a 23348 "unexpected attribute form: DW_FORM_indirect");
a8329558 23349 break;
663c44ac
JK
23350 case DW_FORM_implicit_const:
23351 fprintf_unfiltered (f, "constant: %s",
23352 plongest (DW_SND (&die->attrs[i])));
23353 break;
c906108c 23354 default:
d97bc12b 23355 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23356 die->attrs[i].form);
d97bc12b 23357 break;
c906108c 23358 }
d97bc12b 23359 fprintf_unfiltered (f, "\n");
c906108c
SS
23360 }
23361}
23362
f9aca02d 23363static void
d97bc12b 23364dump_die_for_error (struct die_info *die)
c906108c 23365{
d97bc12b
DE
23366 dump_die_shallow (gdb_stderr, 0, die);
23367}
23368
23369static void
23370dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23371{
23372 int indent = level * 4;
23373
23374 gdb_assert (die != NULL);
23375
23376 if (level >= max_level)
23377 return;
23378
23379 dump_die_shallow (f, indent, die);
23380
23381 if (die->child != NULL)
c906108c 23382 {
d97bc12b
DE
23383 print_spaces (indent, f);
23384 fprintf_unfiltered (f, " Children:");
23385 if (level + 1 < max_level)
23386 {
23387 fprintf_unfiltered (f, "\n");
23388 dump_die_1 (f, level + 1, max_level, die->child);
23389 }
23390 else
23391 {
3e43a32a
MS
23392 fprintf_unfiltered (f,
23393 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23394 }
23395 }
23396
23397 if (die->sibling != NULL && level > 0)
23398 {
23399 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23400 }
23401}
23402
d97bc12b
DE
23403/* This is called from the pdie macro in gdbinit.in.
23404 It's not static so gcc will keep a copy callable from gdb. */
23405
23406void
23407dump_die (struct die_info *die, int max_level)
23408{
23409 dump_die_1 (gdb_stdlog, 0, max_level, die);
23410}
23411
f9aca02d 23412static void
51545339 23413store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23414{
51545339 23415 void **slot;
c906108c 23416
9c541725
PA
23417 slot = htab_find_slot_with_hash (cu->die_hash, die,
23418 to_underlying (die->sect_off),
b64f50a1 23419 INSERT);
51545339
DJ
23420
23421 *slot = die;
c906108c
SS
23422}
23423
b64f50a1
JK
23424/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23425 required kind. */
23426
23427static sect_offset
ff39bb5e 23428dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23429{
7771576e 23430 if (attr_form_is_ref (attr))
9c541725 23431 return (sect_offset) DW_UNSND (attr);
93311388 23432
b98664d3 23433 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23434 dwarf_form_name (attr->form));
9c541725 23435 return {};
c906108c
SS
23436}
23437
43bbcdc2
PH
23438/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23439 * the value held by the attribute is not constant. */
a02abb62 23440
43bbcdc2 23441static LONGEST
ff39bb5e 23442dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23443{
663c44ac 23444 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23445 return DW_SND (attr);
23446 else if (attr->form == DW_FORM_udata
23447 || attr->form == DW_FORM_data1
23448 || attr->form == DW_FORM_data2
23449 || attr->form == DW_FORM_data4
23450 || attr->form == DW_FORM_data8)
23451 return DW_UNSND (attr);
23452 else
23453 {
0224619f 23454 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23455 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23456 dwarf_form_name (attr->form));
23457 return default_value;
23458 }
23459}
23460
348e048f
DE
23461/* Follow reference or signature attribute ATTR of SRC_DIE.
23462 On entry *REF_CU is the CU of SRC_DIE.
23463 On exit *REF_CU is the CU of the result. */
23464
23465static struct die_info *
ff39bb5e 23466follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23467 struct dwarf2_cu **ref_cu)
23468{
23469 struct die_info *die;
23470
7771576e 23471 if (attr_form_is_ref (attr))
348e048f 23472 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23473 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23474 die = follow_die_sig (src_die, attr, ref_cu);
23475 else
23476 {
23477 dump_die_for_error (src_die);
23478 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23479 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23480 }
23481
23482 return die;
03dd20cc
DJ
23483}
23484
5c631832 23485/* Follow reference OFFSET.
673bfd45
DE
23486 On entry *REF_CU is the CU of the source die referencing OFFSET.
23487 On exit *REF_CU is the CU of the result.
23488 Returns NULL if OFFSET is invalid. */
f504f079 23489
f9aca02d 23490static struct die_info *
9c541725 23491follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23492 struct dwarf2_cu **ref_cu)
c906108c 23493{
10b3939b 23494 struct die_info temp_die;
f2f0e013 23495 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23496 struct dwarf2_per_objfile *dwarf2_per_objfile
23497 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23498
348e048f
DE
23499 gdb_assert (cu->per_cu != NULL);
23500
98bfdba5
PA
23501 target_cu = cu;
23502
3019eac3 23503 if (cu->per_cu->is_debug_types)
348e048f
DE
23504 {
23505 /* .debug_types CUs cannot reference anything outside their CU.
23506 If they need to, they have to reference a signatured type via
55f1336d 23507 DW_FORM_ref_sig8. */
9c541725 23508 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23509 return NULL;
348e048f 23510 }
36586728 23511 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23512 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23513 {
23514 struct dwarf2_per_cu_data *per_cu;
9a619af0 23515
9c541725 23516 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23517 dwarf2_per_objfile);
03dd20cc
DJ
23518
23519 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23520 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23521 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23522
10b3939b
DJ
23523 target_cu = per_cu->cu;
23524 }
98bfdba5
PA
23525 else if (cu->dies == NULL)
23526 {
23527 /* We're loading full DIEs during partial symbol reading. */
23528 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23529 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23530 }
c906108c 23531
f2f0e013 23532 *ref_cu = target_cu;
9c541725 23533 temp_die.sect_off = sect_off;
c24bdb02
KS
23534
23535 if (target_cu != cu)
23536 target_cu->ancestor = cu;
23537
9a3c8263 23538 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23539 &temp_die,
23540 to_underlying (sect_off));
5c631832 23541}
10b3939b 23542
5c631832
JK
23543/* Follow reference attribute ATTR of SRC_DIE.
23544 On entry *REF_CU is the CU of SRC_DIE.
23545 On exit *REF_CU is the CU of the result. */
23546
23547static struct die_info *
ff39bb5e 23548follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23549 struct dwarf2_cu **ref_cu)
23550{
9c541725 23551 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23552 struct dwarf2_cu *cu = *ref_cu;
23553 struct die_info *die;
23554
9c541725 23555 die = follow_die_offset (sect_off,
36586728
TT
23556 (attr->form == DW_FORM_GNU_ref_alt
23557 || cu->per_cu->is_dwz),
23558 ref_cu);
5c631832 23559 if (!die)
9d8780f0
SM
23560 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23561 "at %s [in module %s]"),
23562 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23563 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23564
5c631832
JK
23565 return die;
23566}
23567
9c541725 23568/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23569 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23570 dwarf2_locexpr_baton->data has lifetime of
23571 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23572
23573struct dwarf2_locexpr_baton
9c541725 23574dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23575 struct dwarf2_per_cu_data *per_cu,
23576 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23577 void *baton, bool resolve_abstract_p)
5c631832 23578{
918dd910 23579 struct dwarf2_cu *cu;
5c631832
JK
23580 struct die_info *die;
23581 struct attribute *attr;
23582 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23583 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23584 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23585
918dd910 23586 if (per_cu->cu == NULL)
58f0c718 23587 load_cu (per_cu, false);
918dd910 23588 cu = per_cu->cu;
cc12ce38
DE
23589 if (cu == NULL)
23590 {
23591 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23592 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23593 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23594 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23595 }
918dd910 23596
9c541725 23597 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23598 if (!die)
9d8780f0
SM
23599 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23600 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23601
23602 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23603 if (!attr && resolve_abstract_p
3360b6e7 23604 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23605 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23606 {
23607 CORE_ADDR pc = (*get_frame_pc) (baton);
6a053cb1 23608 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
eba4caf2 23609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23610
3360b6e7
TV
23611 for (const auto &cand_off
23612 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23613 {
3360b6e7
TV
23614 struct dwarf2_cu *cand_cu = cu;
23615 struct die_info *cand
23616 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23617 if (!cand
23618 || !cand->parent
e4a62c65
TV
23619 || cand->parent->tag != DW_TAG_subprogram)
23620 continue;
23621
23622 CORE_ADDR pc_low, pc_high;
23623 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23624 if (pc_low == ((CORE_ADDR) -1))
23625 continue;
23626 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23627 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23628 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23629 continue;
23630
23631 die = cand;
23632 attr = dwarf2_attr (die, DW_AT_location, cu);
23633 break;
23634 }
23635 }
23636
5c631832
JK
23637 if (!attr)
23638 {
e103e986
JK
23639 /* DWARF: "If there is no such attribute, then there is no effect.".
23640 DATA is ignored if SIZE is 0. */
5c631832 23641
e103e986 23642 retval.data = NULL;
5c631832
JK
23643 retval.size = 0;
23644 }
8cf6f0b1
TT
23645 else if (attr_form_is_section_offset (attr))
23646 {
23647 struct dwarf2_loclist_baton loclist_baton;
23648 CORE_ADDR pc = (*get_frame_pc) (baton);
23649 size_t size;
23650
23651 fill_in_loclist_baton (cu, &loclist_baton, attr);
23652
23653 retval.data = dwarf2_find_location_expression (&loclist_baton,
23654 &size, pc);
23655 retval.size = size;
23656 }
5c631832
JK
23657 else
23658 {
23659 if (!attr_form_is_block (attr))
9d8780f0 23660 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23661 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23662 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23663
23664 retval.data = DW_BLOCK (attr)->data;
23665 retval.size = DW_BLOCK (attr)->size;
23666 }
23667 retval.per_cu = cu->per_cu;
918dd910 23668
ed2dc618 23669 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23670
5c631832 23671 return retval;
348e048f
DE
23672}
23673
8b9737bf
TT
23674/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23675 offset. */
23676
23677struct dwarf2_locexpr_baton
23678dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23679 struct dwarf2_per_cu_data *per_cu,
23680 CORE_ADDR (*get_frame_pc) (void *baton),
23681 void *baton)
23682{
9c541725 23683 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23684
9c541725 23685 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23686}
23687
b6807d98
TT
23688/* Write a constant of a given type as target-ordered bytes into
23689 OBSTACK. */
23690
23691static const gdb_byte *
23692write_constant_as_bytes (struct obstack *obstack,
23693 enum bfd_endian byte_order,
23694 struct type *type,
23695 ULONGEST value,
23696 LONGEST *len)
23697{
23698 gdb_byte *result;
23699
23700 *len = TYPE_LENGTH (type);
224c3ddb 23701 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23702 store_unsigned_integer (result, *len, byte_order, value);
23703
23704 return result;
23705}
23706
23707/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23708 pointer to the constant bytes and set LEN to the length of the
23709 data. If memory is needed, allocate it on OBSTACK. If the DIE
23710 does not have a DW_AT_const_value, return NULL. */
23711
23712const gdb_byte *
9c541725 23713dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23714 struct dwarf2_per_cu_data *per_cu,
23715 struct obstack *obstack,
23716 LONGEST *len)
23717{
23718 struct dwarf2_cu *cu;
23719 struct die_info *die;
23720 struct attribute *attr;
23721 const gdb_byte *result = NULL;
23722 struct type *type;
23723 LONGEST value;
23724 enum bfd_endian byte_order;
e3b94546 23725 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23726
b6807d98 23727 if (per_cu->cu == NULL)
58f0c718 23728 load_cu (per_cu, false);
b6807d98 23729 cu = per_cu->cu;
cc12ce38
DE
23730 if (cu == NULL)
23731 {
23732 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23733 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23734 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23735 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23736 }
b6807d98 23737
9c541725 23738 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23739 if (!die)
9d8780f0
SM
23740 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23741 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23742
23743 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23744 if (attr == NULL)
23745 return NULL;
23746
e3b94546 23747 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23748 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23749
23750 switch (attr->form)
23751 {
23752 case DW_FORM_addr:
336d760d 23753 case DW_FORM_addrx:
b6807d98
TT
23754 case DW_FORM_GNU_addr_index:
23755 {
23756 gdb_byte *tem;
23757
23758 *len = cu->header.addr_size;
224c3ddb 23759 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23760 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23761 result = tem;
23762 }
23763 break;
23764 case DW_FORM_string:
23765 case DW_FORM_strp:
cf532bd1 23766 case DW_FORM_strx:
b6807d98
TT
23767 case DW_FORM_GNU_str_index:
23768 case DW_FORM_GNU_strp_alt:
23769 /* DW_STRING is already allocated on the objfile obstack, point
23770 directly to it. */
23771 result = (const gdb_byte *) DW_STRING (attr);
23772 *len = strlen (DW_STRING (attr));
23773 break;
23774 case DW_FORM_block1:
23775 case DW_FORM_block2:
23776 case DW_FORM_block4:
23777 case DW_FORM_block:
23778 case DW_FORM_exprloc:
0224619f 23779 case DW_FORM_data16:
b6807d98
TT
23780 result = DW_BLOCK (attr)->data;
23781 *len = DW_BLOCK (attr)->size;
23782 break;
23783
23784 /* The DW_AT_const_value attributes are supposed to carry the
23785 symbol's value "represented as it would be on the target
23786 architecture." By the time we get here, it's already been
23787 converted to host endianness, so we just need to sign- or
23788 zero-extend it as appropriate. */
23789 case DW_FORM_data1:
23790 type = die_type (die, cu);
23791 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23792 if (result == NULL)
23793 result = write_constant_as_bytes (obstack, byte_order,
23794 type, value, len);
23795 break;
23796 case DW_FORM_data2:
23797 type = die_type (die, cu);
23798 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23799 if (result == NULL)
23800 result = write_constant_as_bytes (obstack, byte_order,
23801 type, value, len);
23802 break;
23803 case DW_FORM_data4:
23804 type = die_type (die, cu);
23805 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23806 if (result == NULL)
23807 result = write_constant_as_bytes (obstack, byte_order,
23808 type, value, len);
23809 break;
23810 case DW_FORM_data8:
23811 type = die_type (die, cu);
23812 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23813 if (result == NULL)
23814 result = write_constant_as_bytes (obstack, byte_order,
23815 type, value, len);
23816 break;
23817
23818 case DW_FORM_sdata:
663c44ac 23819 case DW_FORM_implicit_const:
b6807d98
TT
23820 type = die_type (die, cu);
23821 result = write_constant_as_bytes (obstack, byte_order,
23822 type, DW_SND (attr), len);
23823 break;
23824
23825 case DW_FORM_udata:
23826 type = die_type (die, cu);
23827 result = write_constant_as_bytes (obstack, byte_order,
23828 type, DW_UNSND (attr), len);
23829 break;
23830
23831 default:
b98664d3 23832 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23833 dwarf_form_name (attr->form));
23834 break;
23835 }
23836
23837 return result;
23838}
23839
7942e96e
AA
23840/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23841 valid type for this die is found. */
23842
23843struct type *
9c541725 23844dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23845 struct dwarf2_per_cu_data *per_cu)
23846{
23847 struct dwarf2_cu *cu;
23848 struct die_info *die;
23849
7942e96e 23850 if (per_cu->cu == NULL)
58f0c718 23851 load_cu (per_cu, false);
7942e96e
AA
23852 cu = per_cu->cu;
23853 if (!cu)
23854 return NULL;
23855
9c541725 23856 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23857 if (!die)
23858 return NULL;
23859
23860 return die_type (die, cu);
23861}
23862
8a9b8146
TT
23863/* Return the type of the DIE at DIE_OFFSET in the CU named by
23864 PER_CU. */
23865
23866struct type *
b64f50a1 23867dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23868 struct dwarf2_per_cu_data *per_cu)
23869{
9c541725 23870 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23871 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23872}
23873
ac9ec31b 23874/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23875 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23876 On exit *REF_CU is the CU of the result.
23877 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23878
23879static struct die_info *
ac9ec31b
DE
23880follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23881 struct dwarf2_cu **ref_cu)
348e048f 23882{
348e048f 23883 struct die_info temp_die;
c24bdb02 23884 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23885 struct die_info *die;
23886
ac9ec31b
DE
23887 /* While it might be nice to assert sig_type->type == NULL here,
23888 we can get here for DW_AT_imported_declaration where we need
23889 the DIE not the type. */
348e048f
DE
23890
23891 /* If necessary, add it to the queue and load its DIEs. */
23892
95554aad 23893 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23894 read_signatured_type (sig_type);
348e048f 23895
348e048f 23896 sig_cu = sig_type->per_cu.cu;
69d751e3 23897 gdb_assert (sig_cu != NULL);
9c541725
PA
23898 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23899 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23900 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23901 to_underlying (temp_die.sect_off));
348e048f
DE
23902 if (die)
23903 {
ed2dc618 23904 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23905 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23906
796a7ff8
DE
23907 /* For .gdb_index version 7 keep track of included TUs.
23908 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23909 if (dwarf2_per_objfile->index_table != NULL
23910 && dwarf2_per_objfile->index_table->version <= 7)
23911 {
ae640021 23912 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23913 }
23914
348e048f 23915 *ref_cu = sig_cu;
c24bdb02
KS
23916 if (sig_cu != cu)
23917 sig_cu->ancestor = cu;
23918
348e048f
DE
23919 return die;
23920 }
23921
ac9ec31b
DE
23922 return NULL;
23923}
23924
23925/* Follow signatured type referenced by ATTR in SRC_DIE.
23926 On entry *REF_CU is the CU of SRC_DIE.
23927 On exit *REF_CU is the CU of the result.
23928 The result is the DIE of the type.
23929 If the referenced type cannot be found an error is thrown. */
23930
23931static struct die_info *
ff39bb5e 23932follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23933 struct dwarf2_cu **ref_cu)
23934{
23935 ULONGEST signature = DW_SIGNATURE (attr);
23936 struct signatured_type *sig_type;
23937 struct die_info *die;
23938
23939 gdb_assert (attr->form == DW_FORM_ref_sig8);
23940
a2ce51a0 23941 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23942 /* sig_type will be NULL if the signatured type is missing from
23943 the debug info. */
23944 if (sig_type == NULL)
23945 {
23946 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23947 " from DIE at %s [in module %s]"),
23948 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23949 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23950 }
23951
23952 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23953 if (die == NULL)
23954 {
23955 dump_die_for_error (src_die);
23956 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23957 " from DIE at %s [in module %s]"),
23958 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23959 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23960 }
23961
23962 return die;
23963}
23964
23965/* Get the type specified by SIGNATURE referenced in DIE/CU,
23966 reading in and processing the type unit if necessary. */
23967
23968static struct type *
23969get_signatured_type (struct die_info *die, ULONGEST signature,
23970 struct dwarf2_cu *cu)
23971{
518817b3
SM
23972 struct dwarf2_per_objfile *dwarf2_per_objfile
23973 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23974 struct signatured_type *sig_type;
23975 struct dwarf2_cu *type_cu;
23976 struct die_info *type_die;
23977 struct type *type;
23978
a2ce51a0 23979 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23980 /* sig_type will be NULL if the signatured type is missing from
23981 the debug info. */
23982 if (sig_type == NULL)
23983 {
b98664d3 23984 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23985 " from DIE at %s [in module %s]"),
23986 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23987 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23988 return build_error_marker_type (cu, die);
23989 }
23990
23991 /* If we already know the type we're done. */
23992 if (sig_type->type != NULL)
23993 return sig_type->type;
23994
23995 type_cu = cu;
23996 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23997 if (type_die != NULL)
23998 {
23999 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24000 is created. This is important, for example, because for c++ classes
24001 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24002 type = read_type_die (type_die, type_cu);
24003 if (type == NULL)
24004 {
b98664d3 24005 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24006 " referenced from DIE at %s [in module %s]"),
24007 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24008 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24009 type = build_error_marker_type (cu, die);
24010 }
24011 }
24012 else
24013 {
b98664d3 24014 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24015 " from DIE at %s [in module %s]"),
24016 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24017 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24018 type = build_error_marker_type (cu, die);
24019 }
24020 sig_type->type = type;
24021
24022 return type;
24023}
24024
24025/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24026 reading in and processing the type unit if necessary. */
24027
24028static struct type *
ff39bb5e 24029get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24030 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24031{
24032 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24033 if (attr_form_is_ref (attr))
ac9ec31b
DE
24034 {
24035 struct dwarf2_cu *type_cu = cu;
24036 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24037
24038 return read_type_die (type_die, type_cu);
24039 }
24040 else if (attr->form == DW_FORM_ref_sig8)
24041 {
24042 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24043 }
24044 else
24045 {
518817b3
SM
24046 struct dwarf2_per_objfile *dwarf2_per_objfile
24047 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24048
b98664d3 24049 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24050 " at %s [in module %s]"),
24051 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24052 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24053 return build_error_marker_type (cu, die);
24054 }
348e048f
DE
24055}
24056
e5fe5e75 24057/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24058
24059static void
e5fe5e75 24060load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24061{
52dc124a 24062 struct signatured_type *sig_type;
348e048f 24063
f4dc4d17
DE
24064 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24065 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24066
6721b2ec
DE
24067 /* We have the per_cu, but we need the signatured_type.
24068 Fortunately this is an easy translation. */
24069 gdb_assert (per_cu->is_debug_types);
24070 sig_type = (struct signatured_type *) per_cu;
348e048f 24071
6721b2ec 24072 gdb_assert (per_cu->cu == NULL);
348e048f 24073
52dc124a 24074 read_signatured_type (sig_type);
348e048f 24075
6721b2ec 24076 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24077}
24078
dee91e82
DE
24079/* die_reader_func for read_signatured_type.
24080 This is identical to load_full_comp_unit_reader,
24081 but is kept separate for now. */
348e048f
DE
24082
24083static void
dee91e82 24084read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 24085 const gdb_byte *info_ptr,
dee91e82
DE
24086 struct die_info *comp_unit_die,
24087 int has_children,
24088 void *data)
348e048f 24089{
dee91e82 24090 struct dwarf2_cu *cu = reader->cu;
348e048f 24091
dee91e82
DE
24092 gdb_assert (cu->die_hash == NULL);
24093 cu->die_hash =
24094 htab_create_alloc_ex (cu->header.length / 12,
24095 die_hash,
24096 die_eq,
24097 NULL,
24098 &cu->comp_unit_obstack,
24099 hashtab_obstack_allocate,
24100 dummy_obstack_deallocate);
348e048f 24101
dee91e82
DE
24102 if (has_children)
24103 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24104 &info_ptr, comp_unit_die);
24105 cu->dies = comp_unit_die;
24106 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
24107
24108 /* We try not to read any attributes in this function, because not
9cdd5dbd 24109 all CUs needed for references have been loaded yet, and symbol
348e048f 24110 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
24111 or we won't be able to build types correctly.
24112 Similarly, if we do not read the producer, we can not apply
24113 producer-specific interpretation. */
95554aad 24114 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 24115}
348e048f 24116
3019eac3
DE
24117/* Read in a signatured type and build its CU and DIEs.
24118 If the type is a stub for the real type in a DWO file,
24119 read in the real type from the DWO file as well. */
dee91e82
DE
24120
24121static void
24122read_signatured_type (struct signatured_type *sig_type)
24123{
24124 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24125
3019eac3 24126 gdb_assert (per_cu->is_debug_types);
dee91e82 24127 gdb_assert (per_cu->cu == NULL);
348e048f 24128
58f0c718 24129 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 24130 read_signatured_type_reader, NULL);
7ee85ab1 24131 sig_type->per_cu.tu_read = 1;
c906108c
SS
24132}
24133
c906108c
SS
24134/* Decode simple location descriptions.
24135 Given a pointer to a dwarf block that defines a location, compute
24136 the location and return the value.
24137
4cecd739
DJ
24138 NOTE drow/2003-11-18: This function is called in two situations
24139 now: for the address of static or global variables (partial symbols
24140 only) and for offsets into structures which are expected to be
24141 (more or less) constant. The partial symbol case should go away,
24142 and only the constant case should remain. That will let this
24143 function complain more accurately. A few special modes are allowed
24144 without complaint for global variables (for instance, global
24145 register values and thread-local values).
c906108c
SS
24146
24147 A location description containing no operations indicates that the
4cecd739 24148 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24149 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24150 callers will only want a very basic result and this can become a
21ae7a4d
JK
24151 complaint.
24152
24153 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24154
24155static CORE_ADDR
e7c27a73 24156decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24157{
518817b3 24158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24159 size_t i;
24160 size_t size = blk->size;
d521ce57 24161 const gdb_byte *data = blk->data;
21ae7a4d
JK
24162 CORE_ADDR stack[64];
24163 int stacki;
24164 unsigned int bytes_read, unsnd;
24165 gdb_byte op;
c906108c 24166
21ae7a4d
JK
24167 i = 0;
24168 stacki = 0;
24169 stack[stacki] = 0;
24170 stack[++stacki] = 0;
24171
24172 while (i < size)
24173 {
24174 op = data[i++];
24175 switch (op)
24176 {
24177 case DW_OP_lit0:
24178 case DW_OP_lit1:
24179 case DW_OP_lit2:
24180 case DW_OP_lit3:
24181 case DW_OP_lit4:
24182 case DW_OP_lit5:
24183 case DW_OP_lit6:
24184 case DW_OP_lit7:
24185 case DW_OP_lit8:
24186 case DW_OP_lit9:
24187 case DW_OP_lit10:
24188 case DW_OP_lit11:
24189 case DW_OP_lit12:
24190 case DW_OP_lit13:
24191 case DW_OP_lit14:
24192 case DW_OP_lit15:
24193 case DW_OP_lit16:
24194 case DW_OP_lit17:
24195 case DW_OP_lit18:
24196 case DW_OP_lit19:
24197 case DW_OP_lit20:
24198 case DW_OP_lit21:
24199 case DW_OP_lit22:
24200 case DW_OP_lit23:
24201 case DW_OP_lit24:
24202 case DW_OP_lit25:
24203 case DW_OP_lit26:
24204 case DW_OP_lit27:
24205 case DW_OP_lit28:
24206 case DW_OP_lit29:
24207 case DW_OP_lit30:
24208 case DW_OP_lit31:
24209 stack[++stacki] = op - DW_OP_lit0;
24210 break;
f1bea926 24211
21ae7a4d
JK
24212 case DW_OP_reg0:
24213 case DW_OP_reg1:
24214 case DW_OP_reg2:
24215 case DW_OP_reg3:
24216 case DW_OP_reg4:
24217 case DW_OP_reg5:
24218 case DW_OP_reg6:
24219 case DW_OP_reg7:
24220 case DW_OP_reg8:
24221 case DW_OP_reg9:
24222 case DW_OP_reg10:
24223 case DW_OP_reg11:
24224 case DW_OP_reg12:
24225 case DW_OP_reg13:
24226 case DW_OP_reg14:
24227 case DW_OP_reg15:
24228 case DW_OP_reg16:
24229 case DW_OP_reg17:
24230 case DW_OP_reg18:
24231 case DW_OP_reg19:
24232 case DW_OP_reg20:
24233 case DW_OP_reg21:
24234 case DW_OP_reg22:
24235 case DW_OP_reg23:
24236 case DW_OP_reg24:
24237 case DW_OP_reg25:
24238 case DW_OP_reg26:
24239 case DW_OP_reg27:
24240 case DW_OP_reg28:
24241 case DW_OP_reg29:
24242 case DW_OP_reg30:
24243 case DW_OP_reg31:
24244 stack[++stacki] = op - DW_OP_reg0;
24245 if (i < size)
24246 dwarf2_complex_location_expr_complaint ();
24247 break;
c906108c 24248
21ae7a4d
JK
24249 case DW_OP_regx:
24250 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24251 i += bytes_read;
24252 stack[++stacki] = unsnd;
24253 if (i < size)
24254 dwarf2_complex_location_expr_complaint ();
24255 break;
c906108c 24256
21ae7a4d
JK
24257 case DW_OP_addr:
24258 stack[++stacki] = read_address (objfile->obfd, &data[i],
24259 cu, &bytes_read);
24260 i += bytes_read;
24261 break;
d53d4ac5 24262
21ae7a4d
JK
24263 case DW_OP_const1u:
24264 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24265 i += 1;
24266 break;
24267
24268 case DW_OP_const1s:
24269 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24270 i += 1;
24271 break;
24272
24273 case DW_OP_const2u:
24274 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24275 i += 2;
24276 break;
24277
24278 case DW_OP_const2s:
24279 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24280 i += 2;
24281 break;
d53d4ac5 24282
21ae7a4d
JK
24283 case DW_OP_const4u:
24284 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24285 i += 4;
24286 break;
24287
24288 case DW_OP_const4s:
24289 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24290 i += 4;
24291 break;
24292
585861ea
JK
24293 case DW_OP_const8u:
24294 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24295 i += 8;
24296 break;
24297
21ae7a4d
JK
24298 case DW_OP_constu:
24299 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24300 &bytes_read);
24301 i += bytes_read;
24302 break;
24303
24304 case DW_OP_consts:
24305 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24306 i += bytes_read;
24307 break;
24308
24309 case DW_OP_dup:
24310 stack[stacki + 1] = stack[stacki];
24311 stacki++;
24312 break;
24313
24314 case DW_OP_plus:
24315 stack[stacki - 1] += stack[stacki];
24316 stacki--;
24317 break;
24318
24319 case DW_OP_plus_uconst:
24320 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24321 &bytes_read);
24322 i += bytes_read;
24323 break;
24324
24325 case DW_OP_minus:
24326 stack[stacki - 1] -= stack[stacki];
24327 stacki--;
24328 break;
24329
24330 case DW_OP_deref:
24331 /* If we're not the last op, then we definitely can't encode
24332 this using GDB's address_class enum. This is valid for partial
24333 global symbols, although the variable's address will be bogus
24334 in the psymtab. */
24335 if (i < size)
24336 dwarf2_complex_location_expr_complaint ();
24337 break;
24338
24339 case DW_OP_GNU_push_tls_address:
4aa4e28b 24340 case DW_OP_form_tls_address:
21ae7a4d
JK
24341 /* The top of the stack has the offset from the beginning
24342 of the thread control block at which the variable is located. */
24343 /* Nothing should follow this operator, so the top of stack would
24344 be returned. */
24345 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24346 address will be bogus in the psymtab. Make it always at least
24347 non-zero to not look as a variable garbage collected by linker
24348 which have DW_OP_addr 0. */
21ae7a4d
JK
24349 if (i < size)
24350 dwarf2_complex_location_expr_complaint ();
585861ea 24351 stack[stacki]++;
21ae7a4d
JK
24352 break;
24353
24354 case DW_OP_GNU_uninit:
24355 break;
24356
336d760d 24357 case DW_OP_addrx:
3019eac3 24358 case DW_OP_GNU_addr_index:
49f6c839 24359 case DW_OP_GNU_const_index:
3019eac3
DE
24360 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24361 &bytes_read);
24362 i += bytes_read;
24363 break;
24364
21ae7a4d
JK
24365 default:
24366 {
f39c6ffd 24367 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24368
24369 if (name)
b98664d3 24370 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24371 name);
24372 else
b98664d3 24373 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24374 op);
24375 }
24376
24377 return (stack[stacki]);
d53d4ac5 24378 }
3c6e0cb3 24379
21ae7a4d
JK
24380 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24381 outside of the allocated space. Also enforce minimum>0. */
24382 if (stacki >= ARRAY_SIZE (stack) - 1)
24383 {
b98664d3 24384 complaint (_("location description stack overflow"));
21ae7a4d
JK
24385 return 0;
24386 }
24387
24388 if (stacki <= 0)
24389 {
b98664d3 24390 complaint (_("location description stack underflow"));
21ae7a4d
JK
24391 return 0;
24392 }
24393 }
24394 return (stack[stacki]);
c906108c
SS
24395}
24396
24397/* memory allocation interface */
24398
c906108c 24399static struct dwarf_block *
7b5a2f43 24400dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24401{
8d749320 24402 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24403}
24404
c906108c 24405static struct die_info *
b60c80d6 24406dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24407{
24408 struct die_info *die;
b60c80d6
DJ
24409 size_t size = sizeof (struct die_info);
24410
24411 if (num_attrs > 1)
24412 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24413
b60c80d6 24414 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24415 memset (die, 0, sizeof (struct die_info));
24416 return (die);
24417}
2e276125
JB
24418
24419\f
24420/* Macro support. */
24421
233d95b5
JK
24422/* Return file name relative to the compilation directory of file number I in
24423 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24424 responsible for freeing it. */
233d95b5 24425
2e276125 24426static char *
233d95b5 24427file_file_name (int file, struct line_header *lh)
2e276125 24428{
6a83a1e6
EZ
24429 /* Is the file number a valid index into the line header's file name
24430 table? Remember that file numbers start with one, not zero. */
7ba99d21 24431 if (lh->is_valid_file_index (file))
6a83a1e6 24432 {
7ba99d21 24433 const file_entry *fe = lh->file_name_at (file);
6e70227d 24434
7ba99d21 24435 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24436 {
7ba99d21 24437 const char *dir = fe->include_dir (lh);
8c43009f 24438 if (dir != NULL)
7ba99d21 24439 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24440 }
7ba99d21 24441 return xstrdup (fe->name);
6a83a1e6 24442 }
2e276125
JB
24443 else
24444 {
6a83a1e6
EZ
24445 /* The compiler produced a bogus file number. We can at least
24446 record the macro definitions made in the file, even if we
24447 won't be able to find the file by name. */
24448 char fake_name[80];
9a619af0 24449
8c042590
PM
24450 xsnprintf (fake_name, sizeof (fake_name),
24451 "<bad macro file number %d>", file);
2e276125 24452
b98664d3 24453 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24454 file);
2e276125 24455
6a83a1e6 24456 return xstrdup (fake_name);
2e276125
JB
24457 }
24458}
24459
233d95b5
JK
24460/* Return the full name of file number I in *LH's file name table.
24461 Use COMP_DIR as the name of the current directory of the
24462 compilation. The result is allocated using xmalloc; the caller is
24463 responsible for freeing it. */
24464static char *
24465file_full_name (int file, struct line_header *lh, const char *comp_dir)
24466{
24467 /* Is the file number a valid index into the line header's file name
24468 table? Remember that file numbers start with one, not zero. */
7ba99d21 24469 if (lh->is_valid_file_index (file))
233d95b5
JK
24470 {
24471 char *relative = file_file_name (file, lh);
24472
24473 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24474 return relative;
b36cec19
PA
24475 return reconcat (relative, comp_dir, SLASH_STRING,
24476 relative, (char *) NULL);
233d95b5
JK
24477 }
24478 else
24479 return file_file_name (file, lh);
24480}
24481
2e276125
JB
24482
24483static struct macro_source_file *
804d2729
TT
24484macro_start_file (struct dwarf2_cu *cu,
24485 int file, int line,
2e276125 24486 struct macro_source_file *current_file,
43f3e411 24487 struct line_header *lh)
2e276125 24488{
233d95b5
JK
24489 /* File name relative to the compilation directory of this source file. */
24490 char *file_name = file_file_name (file, lh);
2e276125 24491
2e276125 24492 if (! current_file)
abc9d0dc 24493 {
fc474241
DE
24494 /* Note: We don't create a macro table for this compilation unit
24495 at all until we actually get a filename. */
c24bdb02 24496 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24497
abc9d0dc
TT
24498 /* If we have no current file, then this must be the start_file
24499 directive for the compilation unit's main source file. */
fc474241
DE
24500 current_file = macro_set_main (macro_table, file_name);
24501 macro_define_special (macro_table);
abc9d0dc 24502 }
2e276125 24503 else
233d95b5 24504 current_file = macro_include (current_file, line, file_name);
2e276125 24505
233d95b5 24506 xfree (file_name);
6e70227d 24507
2e276125
JB
24508 return current_file;
24509}
24510
2e276125
JB
24511static const char *
24512consume_improper_spaces (const char *p, const char *body)
24513{
24514 if (*p == ' ')
24515 {
b98664d3 24516 complaint (_("macro definition contains spaces "
3e43a32a 24517 "in formal argument list:\n`%s'"),
4d3c2250 24518 body);
2e276125
JB
24519
24520 while (*p == ' ')
24521 p++;
24522 }
24523
24524 return p;
24525}
24526
24527
24528static void
24529parse_macro_definition (struct macro_source_file *file, int line,
24530 const char *body)
24531{
24532 const char *p;
24533
24534 /* The body string takes one of two forms. For object-like macro
24535 definitions, it should be:
24536
24537 <macro name> " " <definition>
24538
24539 For function-like macro definitions, it should be:
24540
24541 <macro name> "() " <definition>
24542 or
24543 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24544
24545 Spaces may appear only where explicitly indicated, and in the
24546 <definition>.
24547
24548 The Dwarf 2 spec says that an object-like macro's name is always
24549 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24550 the space when the macro's definition is the empty string.
2e276125
JB
24551
24552 The Dwarf 2 spec says that there should be no spaces between the
24553 formal arguments in a function-like macro's formal argument list,
24554 but versions of GCC around March 2002 include spaces after the
24555 commas. */
24556
24557
24558 /* Find the extent of the macro name. The macro name is terminated
24559 by either a space or null character (for an object-like macro) or
24560 an opening paren (for a function-like macro). */
24561 for (p = body; *p; p++)
24562 if (*p == ' ' || *p == '(')
24563 break;
24564
24565 if (*p == ' ' || *p == '\0')
24566 {
24567 /* It's an object-like macro. */
24568 int name_len = p - body;
456e800a 24569 std::string name (body, name_len);
2e276125
JB
24570 const char *replacement;
24571
24572 if (*p == ' ')
24573 replacement = body + name_len + 1;
24574 else
24575 {
4d3c2250 24576 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24577 replacement = body + name_len;
24578 }
6e70227d 24579
456e800a 24580 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24581 }
24582 else if (*p == '(')
24583 {
24584 /* It's a function-like macro. */
456e800a 24585 std::string name (body, p - body);
2e276125
JB
24586 int argc = 0;
24587 int argv_size = 1;
8d749320 24588 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24589
24590 p++;
24591
24592 p = consume_improper_spaces (p, body);
24593
24594 /* Parse the formal argument list. */
24595 while (*p && *p != ')')
24596 {
24597 /* Find the extent of the current argument name. */
24598 const char *arg_start = p;
24599
24600 while (*p && *p != ',' && *p != ')' && *p != ' ')
24601 p++;
24602
24603 if (! *p || p == arg_start)
4d3c2250 24604 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24605 else
24606 {
24607 /* Make sure argv has room for the new argument. */
24608 if (argc >= argv_size)
24609 {
24610 argv_size *= 2;
224c3ddb 24611 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24612 }
24613
3f8a7804 24614 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24615 }
24616
24617 p = consume_improper_spaces (p, body);
24618
24619 /* Consume the comma, if present. */
24620 if (*p == ',')
24621 {
24622 p++;
24623
24624 p = consume_improper_spaces (p, body);
24625 }
24626 }
24627
24628 if (*p == ')')
24629 {
24630 p++;
24631
24632 if (*p == ' ')
24633 /* Perfectly formed definition, no complaints. */
456e800a 24634 macro_define_function (file, line, name.c_str (),
6e70227d 24635 argc, (const char **) argv,
2e276125
JB
24636 p + 1);
24637 else if (*p == '\0')
24638 {
24639 /* Complain, but do define it. */
4d3c2250 24640 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24641 macro_define_function (file, line, name.c_str (),
6e70227d 24642 argc, (const char **) argv,
2e276125
JB
24643 p);
24644 }
24645 else
24646 /* Just complain. */
4d3c2250 24647 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24648 }
24649 else
24650 /* Just complain. */
4d3c2250 24651 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24652
2e276125
JB
24653 {
24654 int i;
24655
24656 for (i = 0; i < argc; i++)
24657 xfree (argv[i]);
24658 }
24659 xfree (argv);
24660 }
24661 else
4d3c2250 24662 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24663}
24664
cf2c3c16
TT
24665/* Skip some bytes from BYTES according to the form given in FORM.
24666 Returns the new pointer. */
2e276125 24667
d521ce57
TT
24668static const gdb_byte *
24669skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24670 enum dwarf_form form,
24671 unsigned int offset_size,
24672 struct dwarf2_section_info *section)
2e276125 24673{
cf2c3c16 24674 unsigned int bytes_read;
2e276125 24675
cf2c3c16 24676 switch (form)
2e276125 24677 {
cf2c3c16
TT
24678 case DW_FORM_data1:
24679 case DW_FORM_flag:
24680 ++bytes;
24681 break;
24682
24683 case DW_FORM_data2:
24684 bytes += 2;
24685 break;
24686
24687 case DW_FORM_data4:
24688 bytes += 4;
24689 break;
24690
24691 case DW_FORM_data8:
24692 bytes += 8;
24693 break;
24694
0224619f
JK
24695 case DW_FORM_data16:
24696 bytes += 16;
24697 break;
24698
cf2c3c16
TT
24699 case DW_FORM_string:
24700 read_direct_string (abfd, bytes, &bytes_read);
24701 bytes += bytes_read;
24702 break;
24703
24704 case DW_FORM_sec_offset:
24705 case DW_FORM_strp:
36586728 24706 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24707 bytes += offset_size;
24708 break;
24709
24710 case DW_FORM_block:
24711 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24712 bytes += bytes_read;
24713 break;
24714
24715 case DW_FORM_block1:
24716 bytes += 1 + read_1_byte (abfd, bytes);
24717 break;
24718 case DW_FORM_block2:
24719 bytes += 2 + read_2_bytes (abfd, bytes);
24720 break;
24721 case DW_FORM_block4:
24722 bytes += 4 + read_4_bytes (abfd, bytes);
24723 break;
24724
336d760d 24725 case DW_FORM_addrx:
cf2c3c16 24726 case DW_FORM_sdata:
cf532bd1 24727 case DW_FORM_strx:
cf2c3c16 24728 case DW_FORM_udata:
3019eac3
DE
24729 case DW_FORM_GNU_addr_index:
24730 case DW_FORM_GNU_str_index:
d521ce57 24731 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24732 if (bytes == NULL)
24733 {
24734 dwarf2_section_buffer_overflow_complaint (section);
24735 return NULL;
24736 }
cf2c3c16
TT
24737 break;
24738
663c44ac
JK
24739 case DW_FORM_implicit_const:
24740 break;
24741
cf2c3c16
TT
24742 default:
24743 {
b98664d3 24744 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24745 form, get_section_name (section));
cf2c3c16
TT
24746 return NULL;
24747 }
2e276125
JB
24748 }
24749
cf2c3c16
TT
24750 return bytes;
24751}
757a13d0 24752
cf2c3c16
TT
24753/* A helper for dwarf_decode_macros that handles skipping an unknown
24754 opcode. Returns an updated pointer to the macro data buffer; or,
24755 on error, issues a complaint and returns NULL. */
757a13d0 24756
d521ce57 24757static const gdb_byte *
cf2c3c16 24758skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24759 const gdb_byte **opcode_definitions,
24760 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24761 bfd *abfd,
24762 unsigned int offset_size,
24763 struct dwarf2_section_info *section)
24764{
24765 unsigned int bytes_read, i;
24766 unsigned long arg;
d521ce57 24767 const gdb_byte *defn;
2e276125 24768
cf2c3c16 24769 if (opcode_definitions[opcode] == NULL)
2e276125 24770 {
b98664d3 24771 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24772 opcode);
24773 return NULL;
24774 }
2e276125 24775
cf2c3c16
TT
24776 defn = opcode_definitions[opcode];
24777 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24778 defn += bytes_read;
2e276125 24779
cf2c3c16
TT
24780 for (i = 0; i < arg; ++i)
24781 {
aead7601
SM
24782 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24783 (enum dwarf_form) defn[i], offset_size,
f664829e 24784 section);
cf2c3c16
TT
24785 if (mac_ptr == NULL)
24786 {
24787 /* skip_form_bytes already issued the complaint. */
24788 return NULL;
24789 }
24790 }
757a13d0 24791
cf2c3c16
TT
24792 return mac_ptr;
24793}
757a13d0 24794
cf2c3c16
TT
24795/* A helper function which parses the header of a macro section.
24796 If the macro section is the extended (for now called "GNU") type,
24797 then this updates *OFFSET_SIZE. Returns a pointer to just after
24798 the header, or issues a complaint and returns NULL on error. */
757a13d0 24799
d521ce57
TT
24800static const gdb_byte *
24801dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24802 bfd *abfd,
d521ce57 24803 const gdb_byte *mac_ptr,
cf2c3c16
TT
24804 unsigned int *offset_size,
24805 int section_is_gnu)
24806{
24807 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24808
cf2c3c16
TT
24809 if (section_is_gnu)
24810 {
24811 unsigned int version, flags;
757a13d0 24812
cf2c3c16 24813 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24814 if (version != 4 && version != 5)
cf2c3c16 24815 {
b98664d3 24816 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24817 version);
24818 return NULL;
24819 }
24820 mac_ptr += 2;
757a13d0 24821
cf2c3c16
TT
24822 flags = read_1_byte (abfd, mac_ptr);
24823 ++mac_ptr;
24824 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24825
cf2c3c16
TT
24826 if ((flags & 2) != 0)
24827 /* We don't need the line table offset. */
24828 mac_ptr += *offset_size;
757a13d0 24829
cf2c3c16
TT
24830 /* Vendor opcode descriptions. */
24831 if ((flags & 4) != 0)
24832 {
24833 unsigned int i, count;
757a13d0 24834
cf2c3c16
TT
24835 count = read_1_byte (abfd, mac_ptr);
24836 ++mac_ptr;
24837 for (i = 0; i < count; ++i)
24838 {
24839 unsigned int opcode, bytes_read;
24840 unsigned long arg;
24841
24842 opcode = read_1_byte (abfd, mac_ptr);
24843 ++mac_ptr;
24844 opcode_definitions[opcode] = mac_ptr;
24845 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24846 mac_ptr += bytes_read;
24847 mac_ptr += arg;
24848 }
757a13d0 24849 }
cf2c3c16 24850 }
757a13d0 24851
cf2c3c16
TT
24852 return mac_ptr;
24853}
757a13d0 24854
cf2c3c16 24855/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24856 including DW_MACRO_import. */
cf2c3c16
TT
24857
24858static void
804d2729 24859dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24860 bfd *abfd,
d521ce57 24861 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24862 struct macro_source_file *current_file,
43f3e411 24863 struct line_header *lh,
cf2c3c16 24864 struct dwarf2_section_info *section,
36586728 24865 int section_is_gnu, int section_is_dwz,
cf2c3c16 24866 unsigned int offset_size,
8fc3fc34 24867 htab_t include_hash)
cf2c3c16 24868{
804d2729
TT
24869 struct dwarf2_per_objfile *dwarf2_per_objfile
24870 = cu->per_cu->dwarf2_per_objfile;
4d663531 24871 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24872 enum dwarf_macro_record_type macinfo_type;
24873 int at_commandline;
d521ce57 24874 const gdb_byte *opcode_definitions[256];
757a13d0 24875
cf2c3c16
TT
24876 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24877 &offset_size, section_is_gnu);
24878 if (mac_ptr == NULL)
24879 {
24880 /* We already issued a complaint. */
24881 return;
24882 }
757a13d0
JK
24883
24884 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24885 GDB is still reading the definitions from command line. First
24886 DW_MACINFO_start_file will need to be ignored as it was already executed
24887 to create CURRENT_FILE for the main source holding also the command line
24888 definitions. On first met DW_MACINFO_start_file this flag is reset to
24889 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24890
24891 at_commandline = 1;
24892
24893 do
24894 {
24895 /* Do we at least have room for a macinfo type byte? */
24896 if (mac_ptr >= mac_end)
24897 {
f664829e 24898 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24899 break;
24900 }
24901
aead7601 24902 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24903 mac_ptr++;
24904
cf2c3c16
TT
24905 /* Note that we rely on the fact that the corresponding GNU and
24906 DWARF constants are the same. */
132448f8
SM
24907 DIAGNOSTIC_PUSH
24908 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24909 switch (macinfo_type)
24910 {
24911 /* A zero macinfo type indicates the end of the macro
24912 information. */
24913 case 0:
24914 break;
2e276125 24915
0af92d60
JK
24916 case DW_MACRO_define:
24917 case DW_MACRO_undef:
24918 case DW_MACRO_define_strp:
24919 case DW_MACRO_undef_strp:
24920 case DW_MACRO_define_sup:
24921 case DW_MACRO_undef_sup:
2e276125 24922 {
891d2f0b 24923 unsigned int bytes_read;
2e276125 24924 int line;
d521ce57 24925 const char *body;
cf2c3c16 24926 int is_define;
2e276125 24927
cf2c3c16
TT
24928 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24929 mac_ptr += bytes_read;
24930
0af92d60
JK
24931 if (macinfo_type == DW_MACRO_define
24932 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24933 {
24934 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24935 mac_ptr += bytes_read;
24936 }
24937 else
24938 {
24939 LONGEST str_offset;
24940
24941 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24942 mac_ptr += offset_size;
2e276125 24943
0af92d60
JK
24944 if (macinfo_type == DW_MACRO_define_sup
24945 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24946 || section_is_dwz)
36586728 24947 {
ed2dc618
SM
24948 struct dwz_file *dwz
24949 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24950
ed2dc618
SM
24951 body = read_indirect_string_from_dwz (objfile,
24952 dwz, str_offset);
36586728
TT
24953 }
24954 else
ed2dc618
SM
24955 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24956 abfd, str_offset);
cf2c3c16
TT
24957 }
24958
0af92d60
JK
24959 is_define = (macinfo_type == DW_MACRO_define
24960 || macinfo_type == DW_MACRO_define_strp
24961 || macinfo_type == DW_MACRO_define_sup);
2e276125 24962 if (! current_file)
757a13d0
JK
24963 {
24964 /* DWARF violation as no main source is present. */
b98664d3 24965 complaint (_("debug info with no main source gives macro %s "
757a13d0 24966 "on line %d: %s"),
cf2c3c16
TT
24967 is_define ? _("definition") : _("undefinition"),
24968 line, body);
757a13d0
JK
24969 break;
24970 }
3e43a32a
MS
24971 if ((line == 0 && !at_commandline)
24972 || (line != 0 && at_commandline))
b98664d3 24973 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24974 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24975 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24976 line == 0 ? _("zero") : _("non-zero"), line, body);
24977
955b06fa 24978 if (body == NULL)
7bede828 24979 {
955b06fa
SDJ
24980 /* Fedora's rpm-build's "debugedit" binary
24981 corrupted .debug_macro sections.
24982
24983 For more info, see
24984 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24985 complaint (_("debug info gives %s invalid macro %s "
24986 "without body (corrupted?) at line %d "
24987 "on file %s"),
24988 at_commandline ? _("command-line") : _("in-file"),
24989 is_define ? _("definition") : _("undefinition"),
24990 line, current_file->filename);
7bede828 24991 }
955b06fa
SDJ
24992 else if (is_define)
24993 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24994 else
24995 {
0af92d60
JK
24996 gdb_assert (macinfo_type == DW_MACRO_undef
24997 || macinfo_type == DW_MACRO_undef_strp
24998 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24999 macro_undef (current_file, line, body);
25000 }
2e276125
JB
25001 }
25002 break;
25003
0af92d60 25004 case DW_MACRO_start_file:
2e276125 25005 {
891d2f0b 25006 unsigned int bytes_read;
2e276125
JB
25007 int line, file;
25008
25009 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25010 mac_ptr += bytes_read;
25011 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25012 mac_ptr += bytes_read;
25013
3e43a32a
MS
25014 if ((line == 0 && !at_commandline)
25015 || (line != 0 && at_commandline))
b98664d3 25016 complaint (_("debug info gives source %d included "
757a13d0
JK
25017 "from %s at %s line %d"),
25018 file, at_commandline ? _("command-line") : _("file"),
25019 line == 0 ? _("zero") : _("non-zero"), line);
25020
25021 if (at_commandline)
25022 {
0af92d60 25023 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25024 pass one. */
757a13d0
JK
25025 at_commandline = 0;
25026 }
25027 else
804d2729
TT
25028 current_file = macro_start_file (cu, file, line, current_file,
25029 lh);
2e276125
JB
25030 }
25031 break;
25032
0af92d60 25033 case DW_MACRO_end_file:
2e276125 25034 if (! current_file)
b98664d3 25035 complaint (_("macro debug info has an unmatched "
3e43a32a 25036 "`close_file' directive"));
2e276125
JB
25037 else
25038 {
25039 current_file = current_file->included_by;
25040 if (! current_file)
25041 {
cf2c3c16 25042 enum dwarf_macro_record_type next_type;
2e276125
JB
25043
25044 /* GCC circa March 2002 doesn't produce the zero
25045 type byte marking the end of the compilation
25046 unit. Complain if it's not there, but exit no
25047 matter what. */
25048
25049 /* Do we at least have room for a macinfo type byte? */
25050 if (mac_ptr >= mac_end)
25051 {
f664829e 25052 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25053 return;
25054 }
25055
25056 /* We don't increment mac_ptr here, so this is just
25057 a look-ahead. */
aead7601
SM
25058 next_type
25059 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25060 mac_ptr);
2e276125 25061 if (next_type != 0)
b98664d3 25062 complaint (_("no terminating 0-type entry for "
3e43a32a 25063 "macros in `.debug_macinfo' section"));
2e276125
JB
25064
25065 return;
25066 }
25067 }
25068 break;
25069
0af92d60
JK
25070 case DW_MACRO_import:
25071 case DW_MACRO_import_sup:
cf2c3c16
TT
25072 {
25073 LONGEST offset;
8fc3fc34 25074 void **slot;
a036ba48
TT
25075 bfd *include_bfd = abfd;
25076 struct dwarf2_section_info *include_section = section;
d521ce57 25077 const gdb_byte *include_mac_end = mac_end;
a036ba48 25078 int is_dwz = section_is_dwz;
d521ce57 25079 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25080
25081 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25082 mac_ptr += offset_size;
25083
0af92d60 25084 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25085 {
ed2dc618 25086 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25087
4d663531 25088 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25089
a036ba48 25090 include_section = &dwz->macro;
a32a8923 25091 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25092 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25093 is_dwz = 1;
25094 }
25095
25096 new_mac_ptr = include_section->buffer + offset;
25097 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25098
8fc3fc34
TT
25099 if (*slot != NULL)
25100 {
25101 /* This has actually happened; see
25102 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25103 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25104 ".debug_macro section"));
25105 }
25106 else
25107 {
d521ce57 25108 *slot = (void *) new_mac_ptr;
36586728 25109
804d2729 25110 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25111 include_mac_end, current_file, lh,
36586728 25112 section, section_is_gnu, is_dwz,
4d663531 25113 offset_size, include_hash);
8fc3fc34 25114
d521ce57 25115 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25116 }
cf2c3c16
TT
25117 }
25118 break;
25119
2e276125 25120 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25121 if (!section_is_gnu)
25122 {
25123 unsigned int bytes_read;
2e276125 25124
ac298888
TT
25125 /* This reads the constant, but since we don't recognize
25126 any vendor extensions, we ignore it. */
25127 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25128 mac_ptr += bytes_read;
25129 read_direct_string (abfd, mac_ptr, &bytes_read);
25130 mac_ptr += bytes_read;
2e276125 25131
cf2c3c16
TT
25132 /* We don't recognize any vendor extensions. */
25133 break;
25134 }
25135 /* FALLTHROUGH */
25136
25137 default:
25138 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25139 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25140 section);
25141 if (mac_ptr == NULL)
25142 return;
25143 break;
2e276125 25144 }
132448f8 25145 DIAGNOSTIC_POP
757a13d0 25146 } while (macinfo_type != 0);
2e276125 25147}
8e19ed76 25148
cf2c3c16 25149static void
09262596 25150dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25151 int section_is_gnu)
cf2c3c16 25152{
518817b3
SM
25153 struct dwarf2_per_objfile *dwarf2_per_objfile
25154 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25155 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25156 struct line_header *lh = cu->line_header;
25157 bfd *abfd;
d521ce57 25158 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25159 struct macro_source_file *current_file = 0;
25160 enum dwarf_macro_record_type macinfo_type;
25161 unsigned int offset_size = cu->header.offset_size;
d521ce57 25162 const gdb_byte *opcode_definitions[256];
8fc3fc34 25163 void **slot;
09262596
DE
25164 struct dwarf2_section_info *section;
25165 const char *section_name;
25166
25167 if (cu->dwo_unit != NULL)
25168 {
25169 if (section_is_gnu)
25170 {
25171 section = &cu->dwo_unit->dwo_file->sections.macro;
25172 section_name = ".debug_macro.dwo";
25173 }
25174 else
25175 {
25176 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25177 section_name = ".debug_macinfo.dwo";
25178 }
25179 }
25180 else
25181 {
25182 if (section_is_gnu)
25183 {
25184 section = &dwarf2_per_objfile->macro;
25185 section_name = ".debug_macro";
25186 }
25187 else
25188 {
25189 section = &dwarf2_per_objfile->macinfo;
25190 section_name = ".debug_macinfo";
25191 }
25192 }
cf2c3c16 25193
bb5ed363 25194 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25195 if (section->buffer == NULL)
25196 {
b98664d3 25197 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25198 return;
25199 }
a32a8923 25200 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25201
25202 /* First pass: Find the name of the base filename.
25203 This filename is needed in order to process all macros whose definition
25204 (or undefinition) comes from the command line. These macros are defined
25205 before the first DW_MACINFO_start_file entry, and yet still need to be
25206 associated to the base file.
25207
25208 To determine the base file name, we scan the macro definitions until we
25209 reach the first DW_MACINFO_start_file entry. We then initialize
25210 CURRENT_FILE accordingly so that any macro definition found before the
25211 first DW_MACINFO_start_file can still be associated to the base file. */
25212
25213 mac_ptr = section->buffer + offset;
25214 mac_end = section->buffer + section->size;
25215
25216 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25217 &offset_size, section_is_gnu);
25218 if (mac_ptr == NULL)
25219 {
25220 /* We already issued a complaint. */
25221 return;
25222 }
25223
25224 do
25225 {
25226 /* Do we at least have room for a macinfo type byte? */
25227 if (mac_ptr >= mac_end)
25228 {
25229 /* Complaint is printed during the second pass as GDB will probably
25230 stop the first pass earlier upon finding
25231 DW_MACINFO_start_file. */
25232 break;
25233 }
25234
aead7601 25235 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25236 mac_ptr++;
25237
25238 /* Note that we rely on the fact that the corresponding GNU and
25239 DWARF constants are the same. */
132448f8
SM
25240 DIAGNOSTIC_PUSH
25241 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25242 switch (macinfo_type)
25243 {
25244 /* A zero macinfo type indicates the end of the macro
25245 information. */
25246 case 0:
25247 break;
25248
0af92d60
JK
25249 case DW_MACRO_define:
25250 case DW_MACRO_undef:
cf2c3c16
TT
25251 /* Only skip the data by MAC_PTR. */
25252 {
25253 unsigned int bytes_read;
25254
25255 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25256 mac_ptr += bytes_read;
25257 read_direct_string (abfd, mac_ptr, &bytes_read);
25258 mac_ptr += bytes_read;
25259 }
25260 break;
25261
0af92d60 25262 case DW_MACRO_start_file:
cf2c3c16
TT
25263 {
25264 unsigned int bytes_read;
25265 int line, file;
25266
25267 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25268 mac_ptr += bytes_read;
25269 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25270 mac_ptr += bytes_read;
25271
804d2729 25272 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25273 }
25274 break;
25275
0af92d60 25276 case DW_MACRO_end_file:
cf2c3c16
TT
25277 /* No data to skip by MAC_PTR. */
25278 break;
25279
0af92d60
JK
25280 case DW_MACRO_define_strp:
25281 case DW_MACRO_undef_strp:
25282 case DW_MACRO_define_sup:
25283 case DW_MACRO_undef_sup:
cf2c3c16
TT
25284 {
25285 unsigned int bytes_read;
25286
25287 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25288 mac_ptr += bytes_read;
25289 mac_ptr += offset_size;
25290 }
25291 break;
25292
0af92d60
JK
25293 case DW_MACRO_import:
25294 case DW_MACRO_import_sup:
cf2c3c16 25295 /* Note that, according to the spec, a transparent include
0af92d60 25296 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25297 skip this opcode. */
25298 mac_ptr += offset_size;
25299 break;
25300
25301 case DW_MACINFO_vendor_ext:
25302 /* Only skip the data by MAC_PTR. */
25303 if (!section_is_gnu)
25304 {
25305 unsigned int bytes_read;
25306
25307 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25308 mac_ptr += bytes_read;
25309 read_direct_string (abfd, mac_ptr, &bytes_read);
25310 mac_ptr += bytes_read;
25311 }
25312 /* FALLTHROUGH */
25313
25314 default:
25315 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25316 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25317 section);
25318 if (mac_ptr == NULL)
25319 return;
25320 break;
25321 }
132448f8 25322 DIAGNOSTIC_POP
cf2c3c16
TT
25323 } while (macinfo_type != 0 && current_file == NULL);
25324
25325 /* Second pass: Process all entries.
25326
25327 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25328 command-line macro definitions/undefinitions. This flag is unset when we
25329 reach the first DW_MACINFO_start_file entry. */
25330
fc4007c9
TT
25331 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25332 htab_eq_pointer,
25333 NULL, xcalloc, xfree));
8fc3fc34 25334 mac_ptr = section->buffer + offset;
fc4007c9 25335 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25336 *slot = (void *) mac_ptr;
804d2729 25337 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25338 current_file, lh, section,
fc4007c9
TT
25339 section_is_gnu, 0, offset_size,
25340 include_hash.get ());
cf2c3c16
TT
25341}
25342
8e19ed76 25343/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25344 if so return true else false. */
380bca97 25345
8e19ed76 25346static int
6e5a29e1 25347attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25348{
25349 return (attr == NULL ? 0 :
25350 attr->form == DW_FORM_block1
25351 || attr->form == DW_FORM_block2
25352 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25353 || attr->form == DW_FORM_block
25354 || attr->form == DW_FORM_exprloc);
8e19ed76 25355}
4c2df51b 25356
c6a0999f
JB
25357/* Return non-zero if ATTR's value is a section offset --- classes
25358 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25359 You may use DW_UNSND (attr) to retrieve such offsets.
25360
25361 Section 7.5.4, "Attribute Encodings", explains that no attribute
25362 may have a value that belongs to more than one of these classes; it
25363 would be ambiguous if we did, because we use the same forms for all
25364 of them. */
380bca97 25365
3690dd37 25366static int
6e5a29e1 25367attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25368{
25369 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25370 || attr->form == DW_FORM_data8
25371 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25372}
25373
3690dd37
JB
25374/* Return non-zero if ATTR's value falls in the 'constant' class, or
25375 zero otherwise. When this function returns true, you can apply
25376 dwarf2_get_attr_constant_value to it.
25377
25378 However, note that for some attributes you must check
25379 attr_form_is_section_offset before using this test. DW_FORM_data4
25380 and DW_FORM_data8 are members of both the constant class, and of
25381 the classes that contain offsets into other debug sections
25382 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25383 that, if an attribute's can be either a constant or one of the
25384 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25385 taken as section offsets, not constants.
25386
25387 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25388 cannot handle that. */
380bca97 25389
3690dd37 25390static int
6e5a29e1 25391attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25392{
25393 switch (attr->form)
25394 {
25395 case DW_FORM_sdata:
25396 case DW_FORM_udata:
25397 case DW_FORM_data1:
25398 case DW_FORM_data2:
25399 case DW_FORM_data4:
25400 case DW_FORM_data8:
663c44ac 25401 case DW_FORM_implicit_const:
3690dd37
JB
25402 return 1;
25403 default:
25404 return 0;
25405 }
25406}
25407
7771576e
SA
25408
25409/* DW_ADDR is always stored already as sect_offset; despite for the forms
25410 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25411
25412static int
6e5a29e1 25413attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25414{
25415 switch (attr->form)
25416 {
25417 case DW_FORM_ref_addr:
25418 case DW_FORM_ref1:
25419 case DW_FORM_ref2:
25420 case DW_FORM_ref4:
25421 case DW_FORM_ref8:
25422 case DW_FORM_ref_udata:
25423 case DW_FORM_GNU_ref_alt:
25424 return 1;
25425 default:
25426 return 0;
25427 }
25428}
25429
3019eac3
DE
25430/* Return the .debug_loc section to use for CU.
25431 For DWO files use .debug_loc.dwo. */
25432
25433static struct dwarf2_section_info *
25434cu_debug_loc_section (struct dwarf2_cu *cu)
25435{
518817b3
SM
25436 struct dwarf2_per_objfile *dwarf2_per_objfile
25437 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25438
3019eac3 25439 if (cu->dwo_unit)
43988095
JK
25440 {
25441 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25442
43988095
JK
25443 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25444 }
25445 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25446 : &dwarf2_per_objfile->loc);
3019eac3
DE
25447}
25448
8cf6f0b1
TT
25449/* A helper function that fills in a dwarf2_loclist_baton. */
25450
25451static void
25452fill_in_loclist_baton (struct dwarf2_cu *cu,
25453 struct dwarf2_loclist_baton *baton,
ff39bb5e 25454 const struct attribute *attr)
8cf6f0b1 25455{
518817b3
SM
25456 struct dwarf2_per_objfile *dwarf2_per_objfile
25457 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25458 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25459
25460 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25461
25462 baton->per_cu = cu->per_cu;
25463 gdb_assert (baton->per_cu);
25464 /* We don't know how long the location list is, but make sure we
25465 don't run off the edge of the section. */
3019eac3
DE
25466 baton->size = section->size - DW_UNSND (attr);
25467 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25468 baton->base_address = cu->base_address;
f664829e 25469 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25470}
25471
4c2df51b 25472static void
ff39bb5e 25473dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25474 struct dwarf2_cu *cu, int is_block)
4c2df51b 25475{
518817b3
SM
25476 struct dwarf2_per_objfile *dwarf2_per_objfile
25477 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25478 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25479 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25480
3690dd37 25481 if (attr_form_is_section_offset (attr)
3019eac3 25482 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25483 the section. If so, fall through to the complaint in the
25484 other branch. */
3019eac3 25485 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25486 {
0d53c4c4 25487 struct dwarf2_loclist_baton *baton;
4c2df51b 25488
8d749320 25489 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25490
8cf6f0b1 25491 fill_in_loclist_baton (cu, baton, attr);
be391dca 25492
d00adf39 25493 if (cu->base_known == 0)
b98664d3 25494 complaint (_("Location list used without "
3e43a32a 25495 "specifying the CU base address."));
4c2df51b 25496
f1e6e072
TT
25497 SYMBOL_ACLASS_INDEX (sym) = (is_block
25498 ? dwarf2_loclist_block_index
25499 : dwarf2_loclist_index);
0d53c4c4
DJ
25500 SYMBOL_LOCATION_BATON (sym) = baton;
25501 }
25502 else
25503 {
25504 struct dwarf2_locexpr_baton *baton;
25505
8d749320 25506 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25507 baton->per_cu = cu->per_cu;
25508 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25509
25510 if (attr_form_is_block (attr))
25511 {
25512 /* Note that we're just copying the block's data pointer
25513 here, not the actual data. We're still pointing into the
6502dd73
DJ
25514 info_buffer for SYM's objfile; right now we never release
25515 that buffer, but when we do clean up properly this may
25516 need to change. */
0d53c4c4
DJ
25517 baton->size = DW_BLOCK (attr)->size;
25518 baton->data = DW_BLOCK (attr)->data;
25519 }
25520 else
25521 {
25522 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25523 sym->natural_name ());
0d53c4c4 25524 baton->size = 0;
0d53c4c4 25525 }
6e70227d 25526
f1e6e072
TT
25527 SYMBOL_ACLASS_INDEX (sym) = (is_block
25528 ? dwarf2_locexpr_block_index
25529 : dwarf2_locexpr_index);
0d53c4c4
DJ
25530 SYMBOL_LOCATION_BATON (sym) = baton;
25531 }
4c2df51b 25532}
6502dd73 25533
9aa1f1e3
TT
25534/* Return the OBJFILE associated with the compilation unit CU. If CU
25535 came from a separate debuginfo file, then the master objfile is
25536 returned. */
ae0d2f24
UW
25537
25538struct objfile *
25539dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25540{
e3b94546 25541 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25542
25543 /* Return the master objfile, so that we can report and look up the
25544 correct file containing this variable. */
25545 if (objfile->separate_debug_objfile_backlink)
25546 objfile = objfile->separate_debug_objfile_backlink;
25547
25548 return objfile;
25549}
25550
96408a79
SA
25551/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25552 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25553 CU_HEADERP first. */
25554
25555static const struct comp_unit_head *
25556per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25557 struct dwarf2_per_cu_data *per_cu)
25558{
d521ce57 25559 const gdb_byte *info_ptr;
96408a79
SA
25560
25561 if (per_cu->cu)
25562 return &per_cu->cu->header;
25563
9c541725 25564 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25565
25566 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25567 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25568 rcuh_kind::COMPILE);
96408a79
SA
25569
25570 return cu_headerp;
25571}
25572
ae0d2f24
UW
25573/* Return the address size given in the compilation unit header for CU. */
25574
98714339 25575int
ae0d2f24
UW
25576dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25577{
96408a79
SA
25578 struct comp_unit_head cu_header_local;
25579 const struct comp_unit_head *cu_headerp;
c471e790 25580
96408a79
SA
25581 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25582
25583 return cu_headerp->addr_size;
ae0d2f24
UW
25584}
25585
9eae7c52
TT
25586/* Return the offset size given in the compilation unit header for CU. */
25587
25588int
25589dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25590{
96408a79
SA
25591 struct comp_unit_head cu_header_local;
25592 const struct comp_unit_head *cu_headerp;
9c6c53f7 25593
96408a79
SA
25594 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25595
25596 return cu_headerp->offset_size;
25597}
25598
25599/* See its dwarf2loc.h declaration. */
25600
25601int
25602dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25603{
25604 struct comp_unit_head cu_header_local;
25605 const struct comp_unit_head *cu_headerp;
25606
25607 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25608
25609 if (cu_headerp->version == 2)
25610 return cu_headerp->addr_size;
25611 else
25612 return cu_headerp->offset_size;
181cebd4
JK
25613}
25614
9aa1f1e3
TT
25615/* Return the text offset of the CU. The returned offset comes from
25616 this CU's objfile. If this objfile came from a separate debuginfo
25617 file, then the offset may be different from the corresponding
25618 offset in the parent objfile. */
25619
25620CORE_ADDR
25621dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25622{
e3b94546 25623 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3 25624
6a053cb1 25625 return objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9aa1f1e3
TT
25626}
25627
9a49df9d
AB
25628/* Return a type that is a generic pointer type, the size of which matches
25629 the address size given in the compilation unit header for PER_CU. */
25630static struct type *
25631dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25632{
25633 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25634 struct type *void_type = objfile_type (objfile)->builtin_void;
25635 struct type *addr_type = lookup_pointer_type (void_type);
25636 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25637
25638 if (TYPE_LENGTH (addr_type) == addr_size)
25639 return addr_type;
25640
25641 addr_type
25642 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25643 return addr_type;
25644}
25645
43988095
JK
25646/* Return DWARF version number of PER_CU. */
25647
25648short
25649dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25650{
25651 return per_cu->dwarf_version;
25652}
25653
348e048f
DE
25654/* Locate the .debug_info compilation unit from CU's objfile which contains
25655 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25656
25657static struct dwarf2_per_cu_data *
9c541725 25658dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25659 unsigned int offset_in_dwz,
ed2dc618 25660 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25661{
25662 struct dwarf2_per_cu_data *this_cu;
25663 int low, high;
25664
ae038cb0 25665 low = 0;
b76e467d 25666 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25667 while (high > low)
25668 {
36586728 25669 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25670 int mid = low + (high - low) / 2;
9a619af0 25671
36586728 25672 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25673 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25674 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25675 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25676 high = mid;
25677 else
25678 low = mid + 1;
25679 }
25680 gdb_assert (low == high);
36586728 25681 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25682 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25683 {
36586728 25684 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25685 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25686 "offset %s [in module %s]"),
25687 sect_offset_str (sect_off),
ed2dc618 25688 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25689
9c541725
PA
25690 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25691 <= sect_off);
ae038cb0
DJ
25692 return dwarf2_per_objfile->all_comp_units[low-1];
25693 }
25694 else
25695 {
b76e467d 25696 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25697 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25698 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25699 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25700 return this_cu;
25701 }
25702}
25703
23745b47 25704/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25705
fcd3b13d
SM
25706dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25707 : per_cu (per_cu_),
9068261f
AB
25708 mark (false),
25709 has_loclist (false),
25710 checked_producer (false),
25711 producer_is_gxx_lt_4_6 (false),
25712 producer_is_gcc_lt_4_3 (false),
eb77c9df 25713 producer_is_icc (false),
9068261f 25714 producer_is_icc_lt_14 (false),
c258c396 25715 producer_is_codewarrior (false),
9068261f 25716 processing_has_namespace_info (false)
93311388 25717{
fcd3b13d
SM
25718 per_cu->cu = this;
25719}
25720
25721/* Destroy a dwarf2_cu. */
25722
25723dwarf2_cu::~dwarf2_cu ()
25724{
25725 per_cu->cu = NULL;
9816fde3
JK
25726}
25727
25728/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25729
25730static void
95554aad
TT
25731prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25732 enum language pretend_language)
9816fde3
JK
25733{
25734 struct attribute *attr;
25735
25736 /* Set the language we're debugging. */
25737 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25738 if (attr != nullptr)
9816fde3
JK
25739 set_cu_language (DW_UNSND (attr), cu);
25740 else
9cded63f 25741 {
95554aad 25742 cu->language = pretend_language;
9cded63f
TT
25743 cu->language_defn = language_def (cu->language);
25744 }
dee91e82 25745
7d45c7c3 25746 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25747}
25748
ae038cb0
DJ
25749/* Increase the age counter on each cached compilation unit, and free
25750 any that are too old. */
25751
25752static void
ed2dc618 25753age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25754{
25755 struct dwarf2_per_cu_data *per_cu, **last_chain;
25756
25757 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25758 per_cu = dwarf2_per_objfile->read_in_chain;
25759 while (per_cu != NULL)
25760 {
25761 per_cu->cu->last_used ++;
b4f54984 25762 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25763 dwarf2_mark (per_cu->cu);
25764 per_cu = per_cu->cu->read_in_chain;
25765 }
25766
25767 per_cu = dwarf2_per_objfile->read_in_chain;
25768 last_chain = &dwarf2_per_objfile->read_in_chain;
25769 while (per_cu != NULL)
25770 {
25771 struct dwarf2_per_cu_data *next_cu;
25772
25773 next_cu = per_cu->cu->read_in_chain;
25774
25775 if (!per_cu->cu->mark)
25776 {
fcd3b13d 25777 delete per_cu->cu;
ae038cb0
DJ
25778 *last_chain = next_cu;
25779 }
25780 else
25781 last_chain = &per_cu->cu->read_in_chain;
25782
25783 per_cu = next_cu;
25784 }
25785}
25786
25787/* Remove a single compilation unit from the cache. */
25788
25789static void
dee91e82 25790free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25791{
25792 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25793 struct dwarf2_per_objfile *dwarf2_per_objfile
25794 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25795
25796 per_cu = dwarf2_per_objfile->read_in_chain;
25797 last_chain = &dwarf2_per_objfile->read_in_chain;
25798 while (per_cu != NULL)
25799 {
25800 struct dwarf2_per_cu_data *next_cu;
25801
25802 next_cu = per_cu->cu->read_in_chain;
25803
dee91e82 25804 if (per_cu == target_per_cu)
ae038cb0 25805 {
fcd3b13d 25806 delete per_cu->cu;
dee91e82 25807 per_cu->cu = NULL;
ae038cb0
DJ
25808 *last_chain = next_cu;
25809 break;
25810 }
25811 else
25812 last_chain = &per_cu->cu->read_in_chain;
25813
25814 per_cu = next_cu;
25815 }
25816}
25817
dee91e82
DE
25818/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25819 We store these in a hash table separate from the DIEs, and preserve them
25820 when the DIEs are flushed out of cache.
25821
25822 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25823 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25824 or the type may come from a DWO file. Furthermore, while it's more logical
25825 to use per_cu->section+offset, with Fission the section with the data is in
25826 the DWO file but we don't know that section at the point we need it.
25827 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25828 because we can enter the lookup routine, get_die_type_at_offset, from
25829 outside this file, and thus won't necessarily have PER_CU->cu.
25830 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25831
dee91e82 25832struct dwarf2_per_cu_offset_and_type
1c379e20 25833{
dee91e82 25834 const struct dwarf2_per_cu_data *per_cu;
9c541725 25835 sect_offset sect_off;
1c379e20
DJ
25836 struct type *type;
25837};
25838
dee91e82 25839/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25840
25841static hashval_t
dee91e82 25842per_cu_offset_and_type_hash (const void *item)
1c379e20 25843{
9a3c8263
SM
25844 const struct dwarf2_per_cu_offset_and_type *ofs
25845 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25846
9c541725 25847 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25848}
25849
dee91e82 25850/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25851
25852static int
dee91e82 25853per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25854{
9a3c8263
SM
25855 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25856 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25857 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25858 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25859
dee91e82 25860 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25861 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25862}
25863
25864/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25865 table if necessary. For convenience, return TYPE.
25866
25867 The DIEs reading must have careful ordering to:
85102364 25868 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25869 reading current DIE.
25870 * Not trying to dereference contents of still incompletely read in types
25871 while reading in other DIEs.
25872 * Enable referencing still incompletely read in types just by a pointer to
25873 the type without accessing its fields.
25874
25875 Therefore caller should follow these rules:
25876 * Try to fetch any prerequisite types we may need to build this DIE type
25877 before building the type and calling set_die_type.
e71ec853 25878 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25879 possible before fetching more types to complete the current type.
25880 * Make the type as complete as possible before fetching more types. */
1c379e20 25881
f792889a 25882static struct type *
1c379e20
DJ
25883set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25884{
518817b3
SM
25885 struct dwarf2_per_objfile *dwarf2_per_objfile
25886 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25887 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25888 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25889 struct attribute *attr;
25890 struct dynamic_prop prop;
1c379e20 25891
b4ba55a1
JB
25892 /* For Ada types, make sure that the gnat-specific data is always
25893 initialized (if not already set). There are a few types where
25894 we should not be doing so, because the type-specific area is
25895 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25896 where the type-specific area is used to store the floatformat).
25897 But this is not a problem, because the gnat-specific information
25898 is actually not needed for these types. */
25899 if (need_gnat_info (cu)
25900 && TYPE_CODE (type) != TYPE_CODE_FUNC
25901 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25902 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25903 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25904 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25905 && !HAVE_GNAT_AUX_INFO (type))
25906 INIT_GNAT_SPECIFIC (type);
25907
3f2f83dd
KB
25908 /* Read DW_AT_allocated and set in type. */
25909 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25910 if (attr_form_is_block (attr))
25911 {
9a49df9d
AB
25912 struct type *prop_type
25913 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25914 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25915 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25916 }
25917 else if (attr != NULL)
25918 {
b98664d3 25919 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25920 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25921 sect_offset_str (die->sect_off));
3f2f83dd
KB
25922 }
25923
25924 /* Read DW_AT_associated and set in type. */
25925 attr = dwarf2_attr (die, DW_AT_associated, cu);
25926 if (attr_form_is_block (attr))
25927 {
9a49df9d
AB
25928 struct type *prop_type
25929 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25930 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25931 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25932 }
25933 else if (attr != NULL)
25934 {
b98664d3 25935 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25936 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25937 sect_offset_str (die->sect_off));
3f2f83dd
KB
25938 }
25939
3cdcd0ce
JB
25940 /* Read DW_AT_data_location and set in type. */
25941 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25942 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25943 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25944 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25945
dee91e82 25946 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25947 {
dee91e82
DE
25948 dwarf2_per_objfile->die_type_hash =
25949 htab_create_alloc_ex (127,
25950 per_cu_offset_and_type_hash,
25951 per_cu_offset_and_type_eq,
25952 NULL,
25953 &objfile->objfile_obstack,
25954 hashtab_obstack_allocate,
25955 dummy_obstack_deallocate);
f792889a 25956 }
1c379e20 25957
dee91e82 25958 ofs.per_cu = cu->per_cu;
9c541725 25959 ofs.sect_off = die->sect_off;
1c379e20 25960 ofs.type = type;
dee91e82
DE
25961 slot = (struct dwarf2_per_cu_offset_and_type **)
25962 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25963 if (*slot)
b98664d3 25964 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25965 sect_offset_str (die->sect_off));
8d749320
SM
25966 *slot = XOBNEW (&objfile->objfile_obstack,
25967 struct dwarf2_per_cu_offset_and_type);
1c379e20 25968 **slot = ofs;
f792889a 25969 return type;
1c379e20
DJ
25970}
25971
9c541725 25972/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25973 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25974
25975static struct type *
9c541725 25976get_die_type_at_offset (sect_offset sect_off,
673bfd45 25977 struct dwarf2_per_cu_data *per_cu)
1c379e20 25978{
dee91e82 25979 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25980 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25981
dee91e82 25982 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25983 return NULL;
1c379e20 25984
dee91e82 25985 ofs.per_cu = per_cu;
9c541725 25986 ofs.sect_off = sect_off;
9a3c8263
SM
25987 slot = ((struct dwarf2_per_cu_offset_and_type *)
25988 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25989 if (slot)
25990 return slot->type;
25991 else
25992 return NULL;
25993}
25994
02142a6c 25995/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25996 or return NULL if DIE does not have a saved type. */
25997
25998static struct type *
25999get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26000{
9c541725 26001 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
26002}
26003
10b3939b
DJ
26004/* Add a dependence relationship from CU to REF_PER_CU. */
26005
26006static void
26007dwarf2_add_dependence (struct dwarf2_cu *cu,
26008 struct dwarf2_per_cu_data *ref_per_cu)
26009{
26010 void **slot;
26011
26012 if (cu->dependencies == NULL)
26013 cu->dependencies
26014 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26015 NULL, &cu->comp_unit_obstack,
26016 hashtab_obstack_allocate,
26017 dummy_obstack_deallocate);
26018
26019 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26020 if (*slot == NULL)
26021 *slot = ref_per_cu;
26022}
1c379e20 26023
f504f079
DE
26024/* Subroutine of dwarf2_mark to pass to htab_traverse.
26025 Set the mark field in every compilation unit in the
ae038cb0
DJ
26026 cache that we must keep because we are keeping CU. */
26027
10b3939b
DJ
26028static int
26029dwarf2_mark_helper (void **slot, void *data)
26030{
26031 struct dwarf2_per_cu_data *per_cu;
26032
26033 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26034
26035 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26036 reading of the chain. As such dependencies remain valid it is not much
26037 useful to track and undo them during QUIT cleanups. */
26038 if (per_cu->cu == NULL)
26039 return 1;
26040
10b3939b
DJ
26041 if (per_cu->cu->mark)
26042 return 1;
9068261f 26043 per_cu->cu->mark = true;
10b3939b
DJ
26044
26045 if (per_cu->cu->dependencies != NULL)
26046 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26047
26048 return 1;
26049}
26050
f504f079
DE
26051/* Set the mark field in CU and in every other compilation unit in the
26052 cache that we must keep because we are keeping CU. */
26053
ae038cb0
DJ
26054static void
26055dwarf2_mark (struct dwarf2_cu *cu)
26056{
26057 if (cu->mark)
26058 return;
9068261f 26059 cu->mark = true;
10b3939b
DJ
26060 if (cu->dependencies != NULL)
26061 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26062}
26063
26064static void
26065dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26066{
26067 while (per_cu)
26068 {
9068261f 26069 per_cu->cu->mark = false;
ae038cb0
DJ
26070 per_cu = per_cu->cu->read_in_chain;
26071 }
72bf9492
DJ
26072}
26073
72bf9492
DJ
26074/* Trivial hash function for partial_die_info: the hash value of a DIE
26075 is its offset in .debug_info for this objfile. */
26076
26077static hashval_t
26078partial_die_hash (const void *item)
26079{
9a3c8263
SM
26080 const struct partial_die_info *part_die
26081 = (const struct partial_die_info *) item;
9a619af0 26082
9c541725 26083 return to_underlying (part_die->sect_off);
72bf9492
DJ
26084}
26085
26086/* Trivial comparison function for partial_die_info structures: two DIEs
26087 are equal if they have the same offset. */
26088
26089static int
26090partial_die_eq (const void *item_lhs, const void *item_rhs)
26091{
9a3c8263
SM
26092 const struct partial_die_info *part_die_lhs
26093 = (const struct partial_die_info *) item_lhs;
26094 const struct partial_die_info *part_die_rhs
26095 = (const struct partial_die_info *) item_rhs;
9a619af0 26096
9c541725 26097 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26098}
26099
3c3bb058
AB
26100struct cmd_list_element *set_dwarf_cmdlist;
26101struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26102
26103static void
981a3fb3 26104set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26105{
b4f54984 26106 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26107 gdb_stdout);
ae038cb0
DJ
26108}
26109
26110static void
981a3fb3 26111show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26112{
b4f54984 26113 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26114}
26115
491144b5 26116bool dwarf_always_disassemble;
437afbb8 26117
437afbb8 26118static void
cd4fb1b2
SM
26119show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26120 struct cmd_list_element *c, const char *value)
9291a0cd 26121{
cd4fb1b2
SM
26122 fprintf_filtered (file,
26123 _("Whether to always disassemble "
26124 "DWARF expressions is %s.\n"),
26125 value);
9291a0cd
TT
26126}
26127
9291a0cd 26128static void
cd4fb1b2
SM
26129show_check_physname (struct ui_file *file, int from_tty,
26130 struct cmd_list_element *c, const char *value)
9291a0cd 26131{
cd4fb1b2
SM
26132 fprintf_filtered (file,
26133 _("Whether to check \"physname\" is %s.\n"),
26134 value);
9291a0cd
TT
26135}
26136
cd4fb1b2
SM
26137void
26138_initialize_dwarf2_read (void)
9291a0cd 26139{
cd4fb1b2
SM
26140 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26141Set DWARF specific variables.\n\
590042fc 26142Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26143 &set_dwarf_cmdlist, "maintenance set dwarf ",
26144 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26145
cd4fb1b2 26146 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26147Show DWARF specific variables.\n\
26148Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26149 &show_dwarf_cmdlist, "maintenance show dwarf ",
26150 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26151
cd4fb1b2
SM
26152 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26153 &dwarf_max_cache_age, _("\
26154Set the upper bound on the age of cached DWARF compilation units."), _("\
26155Show the upper bound on the age of cached DWARF compilation units."), _("\
26156A higher limit means that cached compilation units will be stored\n\
26157in memory longer, and more total memory will be used. Zero disables\n\
26158caching, which can slow down startup."),
26159 NULL,
26160 show_dwarf_max_cache_age,
26161 &set_dwarf_cmdlist,
26162 &show_dwarf_cmdlist);
156942c7 26163
cd4fb1b2
SM
26164 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26165 &dwarf_always_disassemble, _("\
26166Set whether `info address' always disassembles DWARF expressions."), _("\
26167Show whether `info address' always disassembles DWARF expressions."), _("\
26168When enabled, DWARF expressions are always printed in an assembly-like\n\
26169syntax. When disabled, expressions will be printed in a more\n\
26170conversational style, when possible."),
26171 NULL,
26172 show_dwarf_always_disassemble,
26173 &set_dwarf_cmdlist,
26174 &show_dwarf_cmdlist);
9291a0cd 26175
cd4fb1b2
SM
26176 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26177Set debugging of the DWARF reader."), _("\
26178Show debugging of the DWARF reader."), _("\
26179When enabled (non-zero), debugging messages are printed during DWARF\n\
26180reading and symtab expansion. A value of 1 (one) provides basic\n\
26181information. A value greater than 1 provides more verbose information."),
26182 NULL,
26183 NULL,
26184 &setdebuglist, &showdebuglist);
9291a0cd 26185
cd4fb1b2
SM
26186 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26187Set debugging of the DWARF DIE reader."), _("\
26188Show debugging of the DWARF DIE reader."), _("\
26189When enabled (non-zero), DIEs are dumped after they are read in.\n\
26190The value is the maximum depth to print."),
26191 NULL,
26192 NULL,
26193 &setdebuglist, &showdebuglist);
9291a0cd 26194
cd4fb1b2
SM
26195 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26196Set debugging of the dwarf line reader."), _("\
26197Show debugging of the dwarf line reader."), _("\
26198When enabled (non-zero), line number entries are dumped as they are read in.\n\
26199A value of 1 (one) provides basic information.\n\
26200A value greater than 1 provides more verbose information."),
26201 NULL,
26202 NULL,
26203 &setdebuglist, &showdebuglist);
437afbb8 26204
cd4fb1b2
SM
26205 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26206Set cross-checking of \"physname\" code against demangler."), _("\
26207Show cross-checking of \"physname\" code against demangler."), _("\
26208When enabled, GDB's internal \"physname\" code is checked against\n\
26209the demangler."),
26210 NULL, show_check_physname,
26211 &setdebuglist, &showdebuglist);
900e11f9 26212
e615022a
DE
26213 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26214 no_class, &use_deprecated_index_sections, _("\
26215Set whether to use deprecated gdb_index sections."), _("\
26216Show whether to use deprecated gdb_index sections."), _("\
26217When enabled, deprecated .gdb_index sections are used anyway.\n\
26218Normally they are ignored either because of a missing feature or\n\
26219performance issue.\n\
26220Warning: This option must be enabled before gdb reads the file."),
26221 NULL,
26222 NULL,
26223 &setlist, &showlist);
26224
f1e6e072
TT
26225 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26226 &dwarf2_locexpr_funcs);
26227 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26228 &dwarf2_loclist_funcs);
26229
26230 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26231 &dwarf2_block_frame_base_locexpr_funcs);
26232 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26233 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26234
26235#if GDB_SELF_TEST
26236 selftests::register_test ("dw2_expand_symtabs_matching",
26237 selftests::dw2_expand_symtabs_matching::run_test);
26238#endif
6502dd73 26239}
This page took 4.971256 seconds and 4 git commands to generate.