doc: fix false claim about second argument to gdb
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
42a4f53d 3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
4de283e4
TT
35#include "bfd.h"
36#include "elf-bfd.h"
37#include "symtab.h"
38#include "gdbtypes.h"
39#include "objfiles.h"
d55e5aa6 40#include "dwarf2.h"
4de283e4
TT
41#include "buildsym.h"
42#include "demangle.h"
43#include "gdb-demangle.h"
44#include "expression.h"
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"
59#include <sys/stat.h>
60#include "completer.h"
61#include "common/vec.h"
62#include "c-lang.h"
d55e5aa6 63#include "go-lang.h"
4de283e4
TT
64#include "valprint.h"
65#include "gdbcore.h" /* for gnutarget */
66#include "gdb/gdb-index.h"
67#include <ctype.h>
68#include "gdb_bfd.h"
69#include "f-lang.h"
70#include "source.h"
71#include "common/filestuff.h"
72#include "build-id.h"
d55e5aa6 73#include "namespace.h"
4de283e4
TT
74#include "common/gdb_unlinker.h"
75#include "common/function-view.h"
76#include "common/gdb_optional.h"
77#include "common/underlying.h"
78#include "common/byte-vector.h"
79#include "common/hash_enum.h"
80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4
TT
82#include <fcntl.h>
83#include <sys/types.h>
84#include <algorithm>
85#include <unordered_set>
86#include <unordered_map>
87#include "common/selftest.h"
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
4de283e4 92#include "common/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
5bfd760d 111static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 112
f1e6e072
TT
113/* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115static int dwarf2_locexpr_index;
116static int dwarf2_loclist_index;
117static int dwarf2_locexpr_block_index;
118static int dwarf2_loclist_block_index;
119
3f563c84
PA
120/* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133struct name_component
134{
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143};
144
44ed8f3e
PA
145/* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148struct mapped_index_base
149{
22ca247e
TT
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
152
44ed8f3e
PA
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
156
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
159
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
162
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
165
166 /* Return whether the name at IDX in the symbol table should be
167 ignored. */
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
169 {
170 return false;
171 }
172
173 /* Build the symbol name component sorted vector, if we haven't
174 yet. */
175 void build_name_components ();
176
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
179 vector. */
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
183
184 /* Prevent deleting/destroying via a base class pointer. */
185protected:
186 ~mapped_index_base() = default;
187};
188
9291a0cd
TT
189/* A description of the mapped index. The file format is described in
190 a comment by the code that writes the index. */
fc898b42 191struct mapped_index final : public mapped_index_base
9291a0cd 192{
f00a2de2
PA
193 /* A slot/bucket in the symbol table hash. */
194 struct symbol_table_slot
195 {
196 const offset_type name;
197 const offset_type vec;
198 };
199
559a7a62 200 /* Index data format version. */
3063847f 201 int version = 0;
559a7a62 202
f00a2de2
PA
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
b11b1f88 205
3876f04e 206 /* The symbol table, implemented as a hash table. */
f00a2de2 207 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 208
9291a0cd 209 /* A pointer to the constant pool. */
3063847f 210 const char *constant_pool = nullptr;
3f563c84 211
44ed8f3e
PA
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
9ab08412 215 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 216 }
5c58de74 217
3f563c84
PA
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
44ed8f3e 220 const char *symbol_name_at (offset_type idx) const override
f00a2de2 221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 222
44ed8f3e
PA
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
9291a0cd
TT
225};
226
927aa2e7
JK
227/* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
fc898b42 229struct mapped_debug_names final : public mapped_index_base
927aa2e7 230{
ed2dc618
SM
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
927aa2e7
JK
277};
278
cd4fb1b2 279/* See dwarf2read.h. */
ed2dc618 280
cd4fb1b2 281dwarf2_per_objfile *
ed2dc618
SM
282get_dwarf2_per_objfile (struct objfile *objfile)
283{
5bfd760d 284 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 285}
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
43988095 298 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 299 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 300 { ".debug_macro", ".zdebug_macro" },
251d32d9 301 { ".debug_str", ".zdebug_str" },
43988095 302 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 303 { ".debug_ranges", ".zdebug_ranges" },
43988095 304 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 305 { ".debug_types", ".zdebug_types" },
3019eac3 306 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
307 { ".debug_frame", ".zdebug_frame" },
308 { ".eh_frame", NULL },
24d3216f 309 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
310 { ".debug_names", ".zdebug_names" },
311 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 312 23
251d32d9 313};
c906108c 314
80626a55 315/* List of DWO/DWP sections. */
3019eac3 316
80626a55 317static const struct dwop_section_names
3019eac3
DE
318{
319 struct dwarf2_section_names abbrev_dwo;
320 struct dwarf2_section_names info_dwo;
321 struct dwarf2_section_names line_dwo;
322 struct dwarf2_section_names loc_dwo;
43988095 323 struct dwarf2_section_names loclists_dwo;
09262596
DE
324 struct dwarf2_section_names macinfo_dwo;
325 struct dwarf2_section_names macro_dwo;
3019eac3
DE
326 struct dwarf2_section_names str_dwo;
327 struct dwarf2_section_names str_offsets_dwo;
328 struct dwarf2_section_names types_dwo;
80626a55
DE
329 struct dwarf2_section_names cu_index;
330 struct dwarf2_section_names tu_index;
3019eac3 331}
80626a55 332dwop_section_names =
3019eac3
DE
333{
334 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
335 { ".debug_info.dwo", ".zdebug_info.dwo" },
336 { ".debug_line.dwo", ".zdebug_line.dwo" },
337 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 338 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
339 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
340 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
341 { ".debug_str.dwo", ".zdebug_str.dwo" },
342 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
343 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
344 { ".debug_cu_index", ".zdebug_cu_index" },
345 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
346};
347
c906108c
SS
348/* local data types */
349
107d2387
AC
350/* The data in a compilation unit header, after target2host
351 translation, looks like this. */
c906108c 352struct comp_unit_head
a738430d 353{
c764a876 354 unsigned int length;
a738430d 355 short version;
a738430d
MK
356 unsigned char addr_size;
357 unsigned char signed_addr_p;
9c541725 358 sect_offset abbrev_sect_off;
57349743 359
a738430d
MK
360 /* Size of file offsets; either 4 or 8. */
361 unsigned int offset_size;
57349743 362
a738430d
MK
363 /* Size of the length field; either 4 or 12. */
364 unsigned int initial_length_size;
57349743 365
43988095
JK
366 enum dwarf_unit_type unit_type;
367
a738430d
MK
368 /* Offset to the first byte of this compilation unit header in the
369 .debug_info section, for resolving relative reference dies. */
9c541725 370 sect_offset sect_off;
57349743 371
d00adf39
DE
372 /* Offset to first die in this cu from the start of the cu.
373 This will be the first byte following the compilation unit header. */
9c541725 374 cu_offset first_die_cu_offset;
43988095
JK
375
376 /* 64-bit signature of this type unit - it is valid only for
377 UNIT_TYPE DW_UT_type. */
378 ULONGEST signature;
379
380 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 381 cu_offset type_cu_offset_in_tu;
a738430d 382};
c906108c 383
3da10d80
KS
384/* Type used for delaying computation of method physnames.
385 See comments for compute_delayed_physnames. */
386struct delayed_method_info
387{
388 /* The type to which the method is attached, i.e., its parent class. */
389 struct type *type;
390
391 /* The index of the method in the type's function fieldlists. */
392 int fnfield_index;
393
394 /* The index of the method in the fieldlist. */
395 int index;
396
397 /* The name of the DIE. */
398 const char *name;
399
400 /* The DIE associated with this method. */
401 struct die_info *die;
402};
403
e7c27a73
DJ
404/* Internal state when decoding a particular compilation unit. */
405struct dwarf2_cu
406{
fcd3b13d
SM
407 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
408 ~dwarf2_cu ();
409
410 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
411
c24bdb02
KS
412 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
413 Create the set of symtabs used by this TU, or if this TU is sharing
414 symtabs with another TU and the symtabs have already been created
415 then restore those symtabs in the line header.
416 We don't need the pc/line-number mapping for type units. */
417 void setup_type_unit_groups (struct die_info *die);
418
419 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
420 buildsym_compunit constructor. */
421 struct compunit_symtab *start_symtab (const char *name,
422 const char *comp_dir,
423 CORE_ADDR low_pc);
424
425 /* Reset the builder. */
426 void reset_builder () { m_builder.reset (); }
427
d00adf39 428 /* The header of the compilation unit. */
fcd3b13d 429 struct comp_unit_head header {};
e142c38c 430
d00adf39 431 /* Base address of this compilation unit. */
fcd3b13d 432 CORE_ADDR base_address = 0;
d00adf39
DE
433
434 /* Non-zero if base_address has been set. */
fcd3b13d 435 int base_known = 0;
d00adf39 436
e142c38c 437 /* The language we are debugging. */
fcd3b13d
SM
438 enum language language = language_unknown;
439 const struct language_defn *language_defn = nullptr;
e142c38c 440
fcd3b13d 441 const char *producer = nullptr;
b0f35d58 442
c24bdb02 443private:
804d2729
TT
444 /* The symtab builder for this CU. This is only non-NULL when full
445 symbols are being read. */
c24bdb02 446 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 447
c24bdb02 448public:
e142c38c
DJ
449 /* The generic symbol table building routines have separate lists for
450 file scope symbols and all all other scopes (local scopes). So
451 we need to select the right one to pass to add_symbol_to_list().
452 We do it by keeping a pointer to the correct list in list_in_scope.
453
454 FIXME: The original dwarf code just treated the file scope as the
455 first local scope, and all other local scopes as nested local
456 scopes, and worked fine. Check to see if we really need to
457 distinguish these in buildsym.c. */
fcd3b13d 458 struct pending **list_in_scope = nullptr;
e142c38c 459
b64f50a1
JK
460 /* Hash table holding all the loaded partial DIEs
461 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 462 htab_t partial_dies = nullptr;
72bf9492
DJ
463
464 /* Storage for things with the same lifetime as this read-in compilation
465 unit, including partial DIEs. */
fcd3b13d 466 auto_obstack comp_unit_obstack;
72bf9492 467
ae038cb0
DJ
468 /* When multiple dwarf2_cu structures are living in memory, this field
469 chains them all together, so that they can be released efficiently.
470 We will probably also want a generation counter so that most-recently-used
471 compilation units are cached... */
fcd3b13d 472 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 473
69d751e3 474 /* Backlink to our per_cu entry. */
ae038cb0
DJ
475 struct dwarf2_per_cu_data *per_cu;
476
477 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 478 int last_used = 0;
ae038cb0 479
b64f50a1
JK
480 /* A hash table of DIE cu_offset for following references with
481 die_info->offset.sect_off as hash. */
fcd3b13d 482 htab_t die_hash = nullptr;
10b3939b
DJ
483
484 /* Full DIEs if read in. */
fcd3b13d 485 struct die_info *dies = nullptr;
10b3939b
DJ
486
487 /* A set of pointers to dwarf2_per_cu_data objects for compilation
488 units referenced by this one. Only set during full symbol processing;
489 partial symbol tables do not have dependencies. */
fcd3b13d 490 htab_t dependencies = nullptr;
10b3939b 491
cb1df416 492 /* Header data from the line table, during full symbol processing. */
fcd3b13d 493 struct line_header *line_header = nullptr;
4c8aa72d
PA
494 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
495 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
496 this is the DW_TAG_compile_unit die for this CU. We'll hold on
497 to the line header as long as this DIE is being processed. See
498 process_die_scope. */
fcd3b13d 499 die_info *line_header_die_owner = nullptr;
cb1df416 500
3da10d80
KS
501 /* A list of methods which need to have physnames computed
502 after all type information has been read. */
c89b44cd 503 std::vector<delayed_method_info> method_list;
3da10d80 504
96408a79 505 /* To be copied to symtab->call_site_htab. */
fcd3b13d 506 htab_t call_site_htab = nullptr;
96408a79 507
034e5797
DE
508 /* Non-NULL if this CU came from a DWO file.
509 There is an invariant here that is important to remember:
510 Except for attributes copied from the top level DIE in the "main"
511 (or "stub") file in preparation for reading the DWO file
512 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
513 Either there isn't a DWO file (in which case this is NULL and the point
514 is moot), or there is and either we're not going to read it (in which
515 case this is NULL) or there is and we are reading it (in which case this
516 is non-NULL). */
fcd3b13d 517 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
518
519 /* The DW_AT_addr_base attribute if present, zero otherwise
520 (zero is a valid value though).
1dbab08b 521 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 522 ULONGEST addr_base = 0;
3019eac3 523
2e3cf129
DE
524 /* The DW_AT_ranges_base attribute if present, zero otherwise
525 (zero is a valid value though).
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 527 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
528 be used without needing to know whether DWO files are in use or not.
529 N.B. This does not apply to DW_AT_ranges appearing in
530 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
531 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
532 DW_AT_ranges_base *would* have to be applied, and we'd have to care
533 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 534 ULONGEST ranges_base = 0;
2e3cf129 535
c9317f21
TT
536 /* When reading debug info generated by older versions of rustc, we
537 have to rewrite some union types to be struct types with a
538 variant part. This rewriting must be done after the CU is fully
539 read in, because otherwise at the point of rewriting some struct
540 type might not have been fully processed. So, we keep a list of
541 all such types here and process them after expansion. */
542 std::vector<struct type *> rust_unions;
543
ae038cb0 544 /* Mark used when releasing cached dies. */
9068261f 545 bool mark : 1;
ae038cb0 546
8be455d7
JK
547 /* This CU references .debug_loc. See the symtab->locations_valid field.
548 This test is imperfect as there may exist optimized debug code not using
549 any location list and still facing inlining issues if handled as
550 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 551 bool has_loclist : 1;
ba919b58 552
9068261f 553 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
554 if all the producer_is_* fields are valid. This information is cached
555 because profiling CU expansion showed excessive time spent in
556 producer_is_gxx_lt_4_6. */
9068261f
AB
557 bool checked_producer : 1;
558 bool producer_is_gxx_lt_4_6 : 1;
559 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 560 bool producer_is_icc : 1;
9068261f 561 bool producer_is_icc_lt_14 : 1;
c258c396 562 bool producer_is_codewarrior : 1;
4d4ec4e5 563
9068261f 564 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
565 debugging info for C++ namespaces. GCC 3.3.x did not produce
566 this information, but later versions do. */
567
9068261f 568 bool processing_has_namespace_info : 1;
d590ff25
YQ
569
570 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
571
572 /* If this CU was inherited by another CU (via specification,
573 abstract_origin, etc), this is the ancestor CU. */
574 dwarf2_cu *ancestor;
575
576 /* Get the buildsym_compunit for this CU. */
577 buildsym_compunit *get_builder ()
578 {
579 /* If this CU has a builder associated with it, use that. */
580 if (m_builder != nullptr)
581 return m_builder.get ();
582
583 /* Otherwise, search ancestors for a valid builder. */
584 if (ancestor != nullptr)
585 return ancestor->get_builder ();
586
587 return nullptr;
588 }
e7c27a73
DJ
589};
590
094b34ac
DE
591/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
592 This includes type_unit_group and quick_file_names. */
593
594struct stmt_list_hash
595{
596 /* The DWO unit this table is from or NULL if there is none. */
597 struct dwo_unit *dwo_unit;
598
599 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 600 sect_offset line_sect_off;
094b34ac
DE
601};
602
f4dc4d17
DE
603/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
604 an object of this type. */
605
606struct type_unit_group
607{
0186c6a7 608 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
609 To simplify things we create an artificial CU that "includes" all the
610 type units using this stmt_list so that the rest of the code still has
611 a "per_cu" handle on the symtab.
612 This PER_CU is recognized by having no section. */
8a0459fd 613#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
614 struct dwarf2_per_cu_data per_cu;
615
0186c6a7
DE
616 /* The TUs that share this DW_AT_stmt_list entry.
617 This is added to while parsing type units to build partial symtabs,
618 and is deleted afterwards and not used again. */
619 VEC (sig_type_ptr) *tus;
f4dc4d17 620
43f3e411 621 /* The compunit symtab.
094b34ac 622 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
623 so we create an essentially anonymous symtab as the compunit symtab. */
624 struct compunit_symtab *compunit_symtab;
f4dc4d17 625
094b34ac
DE
626 /* The data used to construct the hash key. */
627 struct stmt_list_hash hash;
f4dc4d17
DE
628
629 /* The number of symtabs from the line header.
630 The value here must match line_header.num_file_names. */
631 unsigned int num_symtabs;
632
633 /* The symbol tables for this TU (obtained from the files listed in
634 DW_AT_stmt_list).
635 WARNING: The order of entries here must match the order of entries
636 in the line header. After the first TU using this type_unit_group, the
637 line header for the subsequent TUs is recreated from this. This is done
638 because we need to use the same symtabs for each TU using the same
639 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
640 there's no guarantee the line header doesn't have duplicate entries. */
641 struct symtab **symtabs;
642};
643
73869dc2 644/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
645
646struct dwo_sections
647{
648 struct dwarf2_section_info abbrev;
3019eac3
DE
649 struct dwarf2_section_info line;
650 struct dwarf2_section_info loc;
43988095 651 struct dwarf2_section_info loclists;
09262596
DE
652 struct dwarf2_section_info macinfo;
653 struct dwarf2_section_info macro;
3019eac3
DE
654 struct dwarf2_section_info str;
655 struct dwarf2_section_info str_offsets;
80626a55
DE
656 /* In the case of a virtual DWO file, these two are unused. */
657 struct dwarf2_section_info info;
3019eac3
DE
658 VEC (dwarf2_section_info_def) *types;
659};
660
c88ee1f0 661/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
662
663struct dwo_unit
664{
665 /* Backlink to the containing struct dwo_file. */
666 struct dwo_file *dwo_file;
667
668 /* The "id" that distinguishes this CU/TU.
669 .debug_info calls this "dwo_id", .debug_types calls this "signature".
670 Since signatures came first, we stick with it for consistency. */
671 ULONGEST signature;
672
673 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 674 struct dwarf2_section_info *section;
3019eac3 675
9c541725
PA
676 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
677 sect_offset sect_off;
3019eac3
DE
678 unsigned int length;
679
680 /* For types, offset in the type's DIE of the type defined by this TU. */
681 cu_offset type_offset_in_tu;
682};
683
73869dc2
DE
684/* include/dwarf2.h defines the DWP section codes.
685 It defines a max value but it doesn't define a min value, which we
686 use for error checking, so provide one. */
687
688enum dwp_v2_section_ids
689{
690 DW_SECT_MIN = 1
691};
692
80626a55 693/* Data for one DWO file.
57d63ce2
DE
694
695 This includes virtual DWO files (a virtual DWO file is a DWO file as it
696 appears in a DWP file). DWP files don't really have DWO files per se -
697 comdat folding of types "loses" the DWO file they came from, and from
698 a high level view DWP files appear to contain a mass of random types.
699 However, to maintain consistency with the non-DWP case we pretend DWP
700 files contain virtual DWO files, and we assign each TU with one virtual
701 DWO file (generally based on the line and abbrev section offsets -
702 a heuristic that seems to work in practice). */
3019eac3
DE
703
704struct dwo_file
705{
0ac5b59e 706 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
0ac5b59e
DE
710 const char *dwo_name;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir;
3019eac3 714
80626a55
DE
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 bfd *dbfd;
3019eac3 718
73869dc2
DE
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
3019eac3
DE
722 struct dwo_sections sections;
723
33c5cd75
DB
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_t cus;
3019eac3
DE
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_t tus;
733};
734
80626a55
DE
735/* These sections are what may appear in a DWP file. */
736
737struct dwp_sections
738{
73869dc2 739 /* These are used by both DWP version 1 and 2. */
80626a55
DE
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
73869dc2
DE
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
80626a55
DE
760};
761
73869dc2
DE
762/* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 764
73869dc2 765struct virtual_v1_dwo_sections
80626a55
DE
766{
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 774 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
775 struct dwarf2_section_info info_or_types;
776};
777
73869dc2
DE
778/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783struct virtual_v2_dwo_sections
784{
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807};
808
80626a55
DE
809/* Contents of DWP hash tables. */
810
811struct dwp_hash_table
812{
73869dc2 813 uint32_t version, nr_columns;
80626a55 814 uint32_t nr_units, nr_slots;
73869dc2
DE
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826#define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
80626a55
DE
838};
839
840/* Data for one DWP file. */
841
842struct dwp_file
843{
400174b1
TT
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
80626a55
DE
850 /* Name of the file. */
851 const char *name;
852
73869dc2 853 /* File format version. */
400174b1 854 int version = 0;
73869dc2 855
93417882 856 /* The bfd. */
400174b1 857 gdb_bfd_ref_ptr dbfd;
80626a55
DE
858
859 /* Section info for this file. */
400174b1 860 struct dwp_sections sections {};
80626a55 861
57d63ce2 862 /* Table of CUs in the file. */
400174b1 863 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
864
865 /* Table of TUs in the file. */
400174b1 866 const struct dwp_hash_table *tus = nullptr;
80626a55 867
19ac8c2e 868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
869 htab_t loaded_cus {};
870 htab_t loaded_tus {};
80626a55 871
73869dc2
DE
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
400174b1
TT
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
80626a55
DE
876};
877
0963b4bd
MS
878/* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
dee91e82 881 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
882
883struct die_reader_specs
884{
a32a8923 885 /* The bfd of die_section. */
93311388
DE
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
80626a55 891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
892 struct dwo_file *dwo_file;
893
dee91e82 894 /* The section the die comes from.
3019eac3 895 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
d521ce57 899 const gdb_byte *buffer;
f664829e
DE
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
a2ce51a0
DE
903
904 /* The value of the DW_AT_comp_dir attribute. */
905 const char *comp_dir;
685af9cd
TT
906
907 /* The abbreviation table to use when reading the DIEs. */
908 struct abbrev_table *abbrev_table;
93311388
DE
909};
910
fd820528 911/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 912typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 913 const gdb_byte *info_ptr,
dee91e82
DE
914 struct die_info *comp_unit_die,
915 int has_children,
916 void *data);
917
ecfb656c
PA
918/* A 1-based directory index. This is a strong typedef to prevent
919 accidentally using a directory index as a 0-based index into an
920 array/vector. */
921enum class dir_index : unsigned int {};
922
923/* Likewise, a 1-based file name index. */
924enum class file_name_index : unsigned int {};
925
52059ffd
TT
926struct file_entry
927{
fff8551c
PA
928 file_entry () = default;
929
ecfb656c 930 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
931 unsigned int mod_time_, unsigned int length_)
932 : name (name_),
ecfb656c 933 d_index (d_index_),
fff8551c
PA
934 mod_time (mod_time_),
935 length (length_)
936 {}
937
ecfb656c
PA
938 /* Return the include directory at D_INDEX stored in LH. Returns
939 NULL if D_INDEX is out of bounds. */
8c43009f
PA
940 const char *include_dir (const line_header *lh) const;
941
fff8551c
PA
942 /* The file name. Note this is an observing pointer. The memory is
943 owned by debug_line_buffer. */
944 const char *name {};
945
8c43009f 946 /* The directory index (1-based). */
ecfb656c 947 dir_index d_index {};
fff8551c
PA
948
949 unsigned int mod_time {};
950
951 unsigned int length {};
952
953 /* True if referenced by the Line Number Program. */
954 bool included_p {};
955
83769d0b 956 /* The associated symbol table, if any. */
fff8551c 957 struct symtab *symtab {};
52059ffd
TT
958};
959
debd256d
JB
960/* The line number information for a compilation unit (found in the
961 .debug_line section) begins with a "statement program header",
962 which contains the following information. */
963struct line_header
964{
fff8551c
PA
965 line_header ()
966 : offset_in_dwz {}
967 {}
968
969 /* Add an entry to the include directory table. */
970 void add_include_dir (const char *include_dir);
971
972 /* Add an entry to the file name table. */
ecfb656c 973 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
974 unsigned int mod_time, unsigned int length);
975
ecfb656c 976 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 977 is out of bounds. */
ecfb656c 978 const char *include_dir_at (dir_index index) const
8c43009f 979 {
ecfb656c
PA
980 /* Convert directory index number (1-based) to vector index
981 (0-based). */
982 size_t vec_index = to_underlying (index) - 1;
983
984 if (vec_index >= include_dirs.size ())
8c43009f 985 return NULL;
ecfb656c 986 return include_dirs[vec_index];
8c43009f
PA
987 }
988
ecfb656c 989 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 990 is out of bounds. */
ecfb656c 991 file_entry *file_name_at (file_name_index index)
8c43009f 992 {
ecfb656c
PA
993 /* Convert file name index number (1-based) to vector index
994 (0-based). */
995 size_t vec_index = to_underlying (index) - 1;
996
997 if (vec_index >= file_names.size ())
fff8551c 998 return NULL;
ecfb656c 999 return &file_names[vec_index];
fff8551c
PA
1000 }
1001
527f3840 1002 /* Offset of line number information in .debug_line section. */
9c541725 1003 sect_offset sect_off {};
527f3840
JK
1004
1005 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1006 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1007
1008 unsigned int total_length {};
1009 unsigned short version {};
1010 unsigned int header_length {};
1011 unsigned char minimum_instruction_length {};
1012 unsigned char maximum_ops_per_instruction {};
1013 unsigned char default_is_stmt {};
1014 int line_base {};
1015 unsigned char line_range {};
1016 unsigned char opcode_base {};
debd256d
JB
1017
1018 /* standard_opcode_lengths[i] is the number of operands for the
1019 standard opcode whose value is i. This means that
1020 standard_opcode_lengths[0] is unused, and the last meaningful
1021 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1022 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1023
fff8551c
PA
1024 /* The include_directories table. Note these are observing
1025 pointers. The memory is owned by debug_line_buffer. */
1026 std::vector<const char *> include_dirs;
debd256d 1027
fff8551c
PA
1028 /* The file_names table. */
1029 std::vector<file_entry> file_names;
debd256d
JB
1030
1031 /* The start and end of the statement program following this
6502dd73 1032 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1033 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1034};
c906108c 1035
fff8551c
PA
1036typedef std::unique_ptr<line_header> line_header_up;
1037
8c43009f
PA
1038const char *
1039file_entry::include_dir (const line_header *lh) const
1040{
ecfb656c 1041 return lh->include_dir_at (d_index);
8c43009f
PA
1042}
1043
c906108c 1044/* When we construct a partial symbol table entry we only
0963b4bd 1045 need this much information. */
6f06d47b 1046struct partial_die_info : public allocate_on_obstack
c906108c 1047 {
6f06d47b
YQ
1048 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1049
1050 /* Disable assign but still keep copy ctor, which is needed
1051 load_partial_dies. */
1052 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1053
52356b79
YQ
1054 /* Adjust the partial die before generating a symbol for it. This
1055 function may set the is_external flag or change the DIE's
1056 name. */
1057 void fixup (struct dwarf2_cu *cu);
1058
48fbe735
YQ
1059 /* Read a minimal amount of information into the minimal die
1060 structure. */
1061 const gdb_byte *read (const struct die_reader_specs *reader,
1062 const struct abbrev_info &abbrev,
1063 const gdb_byte *info_ptr);
1064
72bf9492 1065 /* Offset of this DIE. */
6f06d47b 1066 const sect_offset sect_off;
72bf9492
DJ
1067
1068 /* DWARF-2 tag for this DIE. */
6f06d47b 1069 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1070
72bf9492 1071 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1072 const unsigned int has_children : 1;
1073
72bf9492
DJ
1074 unsigned int is_external : 1;
1075 unsigned int is_declaration : 1;
1076 unsigned int has_type : 1;
1077 unsigned int has_specification : 1;
1078 unsigned int has_pc_info : 1;
481860b3 1079 unsigned int may_be_inlined : 1;
72bf9492 1080
0c1b455e
TT
1081 /* This DIE has been marked DW_AT_main_subprogram. */
1082 unsigned int main_subprogram : 1;
1083
72bf9492
DJ
1084 /* Flag set if the SCOPE field of this structure has been
1085 computed. */
1086 unsigned int scope_set : 1;
1087
fa4028e9
JB
1088 /* Flag set if the DIE has a byte_size attribute. */
1089 unsigned int has_byte_size : 1;
1090
ff908ebf
AW
1091 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1092 unsigned int has_const_value : 1;
1093
98bfdba5
PA
1094 /* Flag set if any of the DIE's children are template arguments. */
1095 unsigned int has_template_arguments : 1;
1096
52356b79 1097 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1098 unsigned int fixup_called : 1;
1099
36586728
TT
1100 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1101 unsigned int is_dwz : 1;
1102
1103 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1104 unsigned int spec_is_dwz : 1;
1105
72bf9492 1106 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1107 sometimes a default name for unnamed DIEs. */
6f06d47b 1108 const char *name = nullptr;
72bf9492 1109
abc72ce4 1110 /* The linkage name, if present. */
6f06d47b 1111 const char *linkage_name = nullptr;
abc72ce4 1112
72bf9492
DJ
1113 /* The scope to prepend to our children. This is generally
1114 allocated on the comp_unit_obstack, so will disappear
1115 when this compilation unit leaves the cache. */
6f06d47b 1116 const char *scope = nullptr;
72bf9492 1117
95554aad
TT
1118 /* Some data associated with the partial DIE. The tag determines
1119 which field is live. */
1120 union
1121 {
1122 /* The location description associated with this DIE, if any. */
1123 struct dwarf_block *locdesc;
1124 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1125 sect_offset sect_off;
6f06d47b 1126 } d {};
72bf9492
DJ
1127
1128 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1129 CORE_ADDR lowpc = 0;
1130 CORE_ADDR highpc = 0;
72bf9492 1131
93311388 1132 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1133 DW_AT_sibling, if any. */
48fbe735
YQ
1134 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1135 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1136 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1137
1138 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1139 DW_AT_specification (or DW_AT_abstract_origin or
1140 DW_AT_extension). */
6f06d47b 1141 sect_offset spec_offset {};
72bf9492
DJ
1142
1143 /* Pointers to this DIE's parent, first child, and next sibling,
1144 if any. */
6f06d47b
YQ
1145 struct partial_die_info *die_parent = nullptr;
1146 struct partial_die_info *die_child = nullptr;
1147 struct partial_die_info *die_sibling = nullptr;
1148
1149 friend struct partial_die_info *
1150 dwarf2_cu::find_partial_die (sect_offset sect_off);
1151
1152 private:
1153 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1154 partial_die_info (sect_offset sect_off)
1155 : partial_die_info (sect_off, DW_TAG_padding, 0)
1156 {
1157 }
1158
1159 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1160 int has_children_)
1161 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1162 {
1163 is_external = 0;
1164 is_declaration = 0;
1165 has_type = 0;
1166 has_specification = 0;
1167 has_pc_info = 0;
1168 may_be_inlined = 0;
1169 main_subprogram = 0;
1170 scope_set = 0;
1171 has_byte_size = 0;
1172 has_const_value = 0;
1173 has_template_arguments = 0;
1174 fixup_called = 0;
1175 is_dwz = 0;
1176 spec_is_dwz = 0;
1177 }
c906108c
SS
1178 };
1179
0963b4bd 1180/* This data structure holds the information of an abbrev. */
c906108c
SS
1181struct abbrev_info
1182 {
1183 unsigned int number; /* number identifying abbrev */
1184 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1185 unsigned short has_children; /* boolean */
1186 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1187 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1188 struct abbrev_info *next; /* next in chain */
1189 };
1190
1191struct attr_abbrev
1192 {
9d25dd43
DE
1193 ENUM_BITFIELD(dwarf_attribute) name : 16;
1194 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1195
1196 /* It is valid only if FORM is DW_FORM_implicit_const. */
1197 LONGEST implicit_const;
c906108c
SS
1198 };
1199
433df2d4
DE
1200/* Size of abbrev_table.abbrev_hash_table. */
1201#define ABBREV_HASH_SIZE 121
1202
1203/* Top level data structure to contain an abbreviation table. */
1204
1205struct abbrev_table
1206{
685af9cd
TT
1207 explicit abbrev_table (sect_offset off)
1208 : sect_off (off)
1209 {
4a17f768 1210 m_abbrevs =
685af9cd 1211 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1212 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1213 }
1214
1215 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1216
1217 /* Allocate space for a struct abbrev_info object in
1218 ABBREV_TABLE. */
1219 struct abbrev_info *alloc_abbrev ();
1220
1221 /* Add an abbreviation to the table. */
1222 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1223
1224 /* Look up an abbrev in the table.
1225 Returns NULL if the abbrev is not found. */
1226
1227 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1228
1229
f4dc4d17
DE
1230 /* Where the abbrev table came from.
1231 This is used as a sanity check when the table is used. */
685af9cd 1232 const sect_offset sect_off;
433df2d4
DE
1233
1234 /* Storage for the abbrev table. */
685af9cd 1235 auto_obstack abbrev_obstack;
433df2d4 1236
4a17f768
YQ
1237private:
1238
433df2d4
DE
1239 /* Hash table of abbrevs.
1240 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1241 It could be statically allocated, but the previous code didn't so we
1242 don't either. */
4a17f768 1243 struct abbrev_info **m_abbrevs;
433df2d4
DE
1244};
1245
685af9cd
TT
1246typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1247
0963b4bd 1248/* Attributes have a name and a value. */
b60c80d6
DJ
1249struct attribute
1250 {
9d25dd43 1251 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1252 ENUM_BITFIELD(dwarf_form) form : 15;
1253
1254 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1255 field should be in u.str (existing only for DW_STRING) but it is kept
1256 here for better struct attribute alignment. */
1257 unsigned int string_is_canonical : 1;
1258
b60c80d6
DJ
1259 union
1260 {
15d034d0 1261 const char *str;
b60c80d6 1262 struct dwarf_block *blk;
43bbcdc2
PH
1263 ULONGEST unsnd;
1264 LONGEST snd;
b60c80d6 1265 CORE_ADDR addr;
ac9ec31b 1266 ULONGEST signature;
b60c80d6
DJ
1267 }
1268 u;
1269 };
1270
0963b4bd 1271/* This data structure holds a complete die structure. */
c906108c
SS
1272struct die_info
1273 {
76815b17
DE
1274 /* DWARF-2 tag for this DIE. */
1275 ENUM_BITFIELD(dwarf_tag) tag : 16;
1276
1277 /* Number of attributes */
98bfdba5
PA
1278 unsigned char num_attrs;
1279
1280 /* True if we're presently building the full type name for the
1281 type derived from this DIE. */
1282 unsigned char building_fullname : 1;
76815b17 1283
adde2bff
DE
1284 /* True if this die is in process. PR 16581. */
1285 unsigned char in_process : 1;
1286
76815b17
DE
1287 /* Abbrev number */
1288 unsigned int abbrev;
1289
93311388 1290 /* Offset in .debug_info or .debug_types section. */
9c541725 1291 sect_offset sect_off;
78ba4af6
JB
1292
1293 /* The dies in a compilation unit form an n-ary tree. PARENT
1294 points to this die's parent; CHILD points to the first child of
1295 this node; and all the children of a given node are chained
4950bc1c 1296 together via their SIBLING fields. */
639d11d3
DC
1297 struct die_info *child; /* Its first child, if any. */
1298 struct die_info *sibling; /* Its next sibling, if any. */
1299 struct die_info *parent; /* Its parent, if any. */
c906108c 1300
b60c80d6
DJ
1301 /* An array of attributes, with NUM_ATTRS elements. There may be
1302 zero, but it's not common and zero-sized arrays are not
1303 sufficiently portable C. */
1304 struct attribute attrs[1];
c906108c
SS
1305 };
1306
0963b4bd 1307/* Get at parts of an attribute structure. */
c906108c
SS
1308
1309#define DW_STRING(attr) ((attr)->u.str)
8285870a 1310#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1311#define DW_UNSND(attr) ((attr)->u.unsnd)
1312#define DW_BLOCK(attr) ((attr)->u.blk)
1313#define DW_SND(attr) ((attr)->u.snd)
1314#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1315#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1316
0963b4bd 1317/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1318struct dwarf_block
1319 {
56eb65bd 1320 size_t size;
1d6edc3c
JK
1321
1322 /* Valid only if SIZE is not zero. */
d521ce57 1323 const gdb_byte *data;
c906108c
SS
1324 };
1325
c906108c
SS
1326#ifndef ATTR_ALLOC_CHUNK
1327#define ATTR_ALLOC_CHUNK 4
1328#endif
1329
c906108c
SS
1330/* Allocate fields for structs, unions and enums in this size. */
1331#ifndef DW_FIELD_ALLOC_CHUNK
1332#define DW_FIELD_ALLOC_CHUNK 4
1333#endif
1334
c906108c
SS
1335/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1336 but this would require a corresponding change in unpack_field_as_long
1337 and friends. */
1338static int bits_per_byte = 8;
1339
2ddeaf8a
TT
1340/* When reading a variant or variant part, we track a bit more
1341 information about the field, and store it in an object of this
1342 type. */
1343
1344struct variant_field
1345{
1346 /* If we see a DW_TAG_variant, then this will be the discriminant
1347 value. */
1348 ULONGEST discriminant_value;
1349 /* If we see a DW_TAG_variant, then this will be set if this is the
1350 default branch. */
1351 bool default_branch;
1352 /* While reading a DW_TAG_variant_part, this will be set if this
1353 field is the discriminant. */
1354 bool is_discriminant;
1355};
1356
52059ffd
TT
1357struct nextfield
1358{
be2daae6
TT
1359 int accessibility = 0;
1360 int virtuality = 0;
2ddeaf8a 1361 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1362 struct variant_field variant {};
1363 struct field field {};
52059ffd
TT
1364};
1365
1366struct fnfieldlist
1367{
be2daae6
TT
1368 const char *name = nullptr;
1369 std::vector<struct fn_field> fnfields;
52059ffd
TT
1370};
1371
c906108c
SS
1372/* The routines that read and process dies for a C struct or C++ class
1373 pass lists of data member fields and lists of member function fields
1374 in an instance of a field_info structure, as defined below. */
1375struct field_info
c5aa993b 1376 {
0963b4bd 1377 /* List of data member and baseclasses fields. */
be2daae6
TT
1378 std::vector<struct nextfield> fields;
1379 std::vector<struct nextfield> baseclasses;
c906108c 1380
7d0ccb61 1381 /* Number of fields (including baseclasses). */
be2daae6 1382 int nfields = 0;
c906108c 1383
c5aa993b 1384 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1385 int non_public_fields = 0;
c906108c 1386
c5aa993b
JM
1387 /* Member function fieldlist array, contains name of possibly overloaded
1388 member function, number of overloaded member functions and a pointer
1389 to the head of the member function field chain. */
be2daae6 1390 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1391
1392 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1393 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1394 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1395
1396 /* Nested types defined by this class and the number of elements in this
1397 list. */
be2daae6 1398 std::vector<struct decl_field> nested_types_list;
c5aa993b 1399 };
c906108c 1400
10b3939b
DJ
1401/* One item on the queue of compilation units to read in full symbols
1402 for. */
1403struct dwarf2_queue_item
1404{
1405 struct dwarf2_per_cu_data *per_cu;
95554aad 1406 enum language pretend_language;
10b3939b
DJ
1407 struct dwarf2_queue_item *next;
1408};
1409
1410/* The current queue. */
1411static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1412
ae038cb0
DJ
1413/* Loaded secondary compilation units are kept in memory until they
1414 have not been referenced for the processing of this many
1415 compilation units. Set this to zero to disable caching. Cache
1416 sizes of up to at least twenty will improve startup time for
1417 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1418static int dwarf_max_cache_age = 5;
920d2a44 1419static void
b4f54984
DE
1420show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1421 struct cmd_list_element *c, const char *value)
920d2a44 1422{
3e43a32a 1423 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1424 "DWARF compilation units is %s.\n"),
920d2a44
AC
1425 value);
1426}
4390d890 1427\f
c906108c
SS
1428/* local function prototypes */
1429
a32a8923
DE
1430static const char *get_section_name (const struct dwarf2_section_info *);
1431
1432static const char *get_section_file_name (const struct dwarf2_section_info *);
1433
918dd910
JK
1434static void dwarf2_find_base_address (struct die_info *die,
1435 struct dwarf2_cu *cu);
1436
0018ea6f
DE
1437static struct partial_symtab *create_partial_symtab
1438 (struct dwarf2_per_cu_data *per_cu, const char *name);
1439
f1902523
JK
1440static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1441 const gdb_byte *info_ptr,
1442 struct die_info *type_unit_die,
1443 int has_children, void *data);
1444
ed2dc618
SM
1445static void dwarf2_build_psymtabs_hard
1446 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1447
72bf9492
DJ
1448static void scan_partial_symbols (struct partial_die_info *,
1449 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1450 int, struct dwarf2_cu *);
c906108c 1451
72bf9492
DJ
1452static void add_partial_symbol (struct partial_die_info *,
1453 struct dwarf2_cu *);
63d06c5c 1454
72bf9492
DJ
1455static void add_partial_namespace (struct partial_die_info *pdi,
1456 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1457 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1458
5d7cb8df 1459static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1460 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1461 struct dwarf2_cu *cu);
1462
72bf9492
DJ
1463static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1464 struct dwarf2_cu *cu);
91c24f0a 1465
bc30ff58
JB
1466static void add_partial_subprogram (struct partial_die_info *pdi,
1467 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1468 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1469
257e7a09
YQ
1470static void dwarf2_read_symtab (struct partial_symtab *,
1471 struct objfile *);
c906108c 1472
a14ed312 1473static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1474
685af9cd 1475static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1476 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1477 sect_offset);
433df2d4 1478
d521ce57 1479static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1480
dee91e82 1481static struct partial_die_info *load_partial_dies
d521ce57 1482 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1483
fb816e8b
TV
1484/* A pair of partial_die_info and compilation unit. */
1485struct cu_partial_die_info
1486{
1487 /* The compilation unit of the partial_die_info. */
1488 struct dwarf2_cu *cu;
1489 /* A partial_die_info. */
1490 struct partial_die_info *pdi;
122cf0f2
AB
1491
1492 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1493 : cu (cu),
1494 pdi (pdi)
1495 { /* Nothhing. */ }
1496
1497private:
1498 cu_partial_die_info () = delete;
fb816e8b
TV
1499};
1500
122cf0f2
AB
1501static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1502 struct dwarf2_cu *);
72bf9492 1503
d521ce57
TT
1504static const gdb_byte *read_attribute (const struct die_reader_specs *,
1505 struct attribute *, struct attr_abbrev *,
1506 const gdb_byte *);
a8329558 1507
a1855c1d 1508static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1509
a1855c1d 1510static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1511
a1855c1d 1512static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1513
15f18d14
AT
1514/* Read the next three bytes (little-endian order) as an unsigned integer. */
1515static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1516
a1855c1d 1517static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1518
a1855c1d 1519static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1520
d521ce57 1521static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1522 unsigned int *);
c906108c 1523
d521ce57 1524static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1525
1526static LONGEST read_checked_initial_length_and_offset
d521ce57 1527 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1528 unsigned int *, unsigned int *);
613e1657 1529
d521ce57
TT
1530static LONGEST read_offset (bfd *, const gdb_byte *,
1531 const struct comp_unit_head *,
c764a876
DE
1532 unsigned int *);
1533
d521ce57 1534static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1535
ed2dc618
SM
1536static sect_offset read_abbrev_offset
1537 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1538 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1539
d521ce57 1540static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1541
d521ce57 1542static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1543
ed2dc618
SM
1544static const char *read_indirect_string
1545 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1546 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1547
ed2dc618
SM
1548static const char *read_indirect_line_string
1549 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1550 const struct comp_unit_head *, unsigned int *);
36586728 1551
ed2dc618
SM
1552static const char *read_indirect_string_at_offset
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1554 LONGEST str_offset);
927aa2e7 1555
ed2dc618
SM
1556static const char *read_indirect_string_from_dwz
1557 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1558
d521ce57 1559static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1560
d521ce57
TT
1561static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1562 const gdb_byte *,
3019eac3
DE
1563 unsigned int *);
1564
d521ce57 1565static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1566 ULONGEST str_index);
3019eac3 1567
e142c38c 1568static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1569
e142c38c
DJ
1570static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1571 struct dwarf2_cu *);
c906108c 1572
348e048f 1573static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1574 unsigned int);
348e048f 1575
7d45c7c3
KB
1576static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1577 struct dwarf2_cu *cu);
1578
05cf31d1
JB
1579static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1580 struct dwarf2_cu *cu);
1581
e142c38c 1582static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1583
e142c38c 1584static struct die_info *die_specification (struct die_info *die,
f2f0e013 1585 struct dwarf2_cu **);
63d06c5c 1586
9c541725 1587static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1588 struct dwarf2_cu *cu);
debd256d 1589
f3f5162e 1590static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1591 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1592 CORE_ADDR, int decode_mapping);
c906108c 1593
804d2729
TT
1594static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1595 const char *);
c906108c 1596
a14ed312 1597static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1598 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1599
ff39bb5e 1600static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1601 struct dwarf2_cu *);
c906108c 1602
ff39bb5e 1603static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1604 struct type *type,
1605 const char *name,
1606 struct obstack *obstack,
12df843f 1607 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1608 const gdb_byte **bytes,
98bfdba5 1609 struct dwarf2_locexpr_baton **baton);
2df3850c 1610
e7c27a73 1611static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1612
b4ba55a1
JB
1613static int need_gnat_info (struct dwarf2_cu *);
1614
3e43a32a
MS
1615static struct type *die_descriptive_type (struct die_info *,
1616 struct dwarf2_cu *);
b4ba55a1
JB
1617
1618static void set_descriptive_type (struct type *, struct die_info *,
1619 struct dwarf2_cu *);
1620
e7c27a73
DJ
1621static struct type *die_containing_type (struct die_info *,
1622 struct dwarf2_cu *);
c906108c 1623
ff39bb5e 1624static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1625 struct dwarf2_cu *);
c906108c 1626
f792889a 1627static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1628
673bfd45
DE
1629static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1630
0d5cff50 1631static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1632
6e70227d 1633static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1634 const char *suffix, int physname,
1635 struct dwarf2_cu *cu);
63d06c5c 1636
e7c27a73 1637static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1638
348e048f
DE
1639static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1640
e7c27a73 1641static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1642
e7c27a73 1643static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1644
96408a79
SA
1645static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1646
71a3c369
TT
1647static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1648
ff013f42
JK
1649static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1650 struct dwarf2_cu *, struct partial_symtab *);
1651
3a2b436a 1652/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1653 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1654enum pc_bounds_kind
1655{
e385593e 1656 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1657 PC_BOUNDS_NOT_PRESENT,
1658
e385593e
JK
1659 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1660 were present but they do not form a valid range of PC addresses. */
1661 PC_BOUNDS_INVALID,
1662
3a2b436a
JK
1663 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1664 PC_BOUNDS_RANGES,
1665
1666 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1667 PC_BOUNDS_HIGH_LOW,
1668};
1669
1670static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1671 CORE_ADDR *, CORE_ADDR *,
1672 struct dwarf2_cu *,
1673 struct partial_symtab *);
c906108c 1674
fae299cd
DC
1675static void get_scope_pc_bounds (struct die_info *,
1676 CORE_ADDR *, CORE_ADDR *,
1677 struct dwarf2_cu *);
1678
801e3a5b
JB
1679static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1680 CORE_ADDR, struct dwarf2_cu *);
1681
a14ed312 1682static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1683 struct dwarf2_cu *);
c906108c 1684
a14ed312 1685static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1686 struct type *, struct dwarf2_cu *);
c906108c 1687
a14ed312 1688static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1689 struct die_info *, struct type *,
e7c27a73 1690 struct dwarf2_cu *);
c906108c 1691
a14ed312 1692static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1693 struct type *,
1694 struct dwarf2_cu *);
c906108c 1695
134d01f1 1696static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1697
e7c27a73 1698static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1699
e7c27a73 1700static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1701
5d7cb8df
JK
1702static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1703
804d2729 1704static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1705
27aa8d6a
SW
1706static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1707
74921315
KS
1708static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1709
f55ee35c
JK
1710static struct type *read_module_type (struct die_info *die,
1711 struct dwarf2_cu *cu);
1712
38d518c9 1713static const char *namespace_name (struct die_info *die,
e142c38c 1714 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1715
134d01f1 1716static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1717
e7c27a73 1718static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1719
6e70227d 1720static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1721 struct dwarf2_cu *);
1722
bf6af496 1723static struct die_info *read_die_and_siblings_1
d521ce57 1724 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1725 struct die_info *);
639d11d3 1726
dee91e82 1727static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1728 const gdb_byte *info_ptr,
1729 const gdb_byte **new_info_ptr,
639d11d3
DC
1730 struct die_info *parent);
1731
d521ce57
TT
1732static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1733 struct die_info **, const gdb_byte *,
1734 int *, int);
3019eac3 1735
d521ce57
TT
1736static const gdb_byte *read_full_die (const struct die_reader_specs *,
1737 struct die_info **, const gdb_byte *,
1738 int *);
93311388 1739
e7c27a73 1740static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1741
15d034d0
TT
1742static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1743 struct obstack *);
71c25dea 1744
15d034d0 1745static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1746
15d034d0 1747static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1748 struct die_info *die,
1749 struct dwarf2_cu *cu);
1750
ca69b9e6
DE
1751static const char *dwarf2_physname (const char *name, struct die_info *die,
1752 struct dwarf2_cu *cu);
1753
e142c38c 1754static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1755 struct dwarf2_cu **);
9219021c 1756
f39c6ffd 1757static const char *dwarf_tag_name (unsigned int);
c906108c 1758
f39c6ffd 1759static const char *dwarf_attr_name (unsigned int);
c906108c 1760
f39c6ffd 1761static const char *dwarf_form_name (unsigned int);
c906108c 1762
a121b7c1 1763static const char *dwarf_bool_name (unsigned int);
c906108c 1764
f39c6ffd 1765static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1766
f9aca02d 1767static struct die_info *sibling_die (struct die_info *);
c906108c 1768
d97bc12b
DE
1769static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1770
1771static void dump_die_for_error (struct die_info *);
1772
1773static void dump_die_1 (struct ui_file *, int level, int max_level,
1774 struct die_info *);
c906108c 1775
d97bc12b 1776/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1777
51545339 1778static void store_in_ref_table (struct die_info *,
10b3939b 1779 struct dwarf2_cu *);
c906108c 1780
ff39bb5e 1781static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1782
ff39bb5e 1783static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1784
348e048f 1785static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1786 const struct attribute *,
348e048f
DE
1787 struct dwarf2_cu **);
1788
10b3939b 1789static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1790 const struct attribute *,
f2f0e013 1791 struct dwarf2_cu **);
c906108c 1792
348e048f 1793static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1794 const struct attribute *,
348e048f
DE
1795 struct dwarf2_cu **);
1796
ac9ec31b
DE
1797static struct type *get_signatured_type (struct die_info *, ULONGEST,
1798 struct dwarf2_cu *);
1799
1800static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1801 const struct attribute *,
ac9ec31b
DE
1802 struct dwarf2_cu *);
1803
e5fe5e75 1804static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1805
52dc124a 1806static void read_signatured_type (struct signatured_type *);
348e048f 1807
63e43d3a
PMR
1808static int attr_to_dynamic_prop (const struct attribute *attr,
1809 struct die_info *die, struct dwarf2_cu *cu,
1810 struct dynamic_prop *prop);
1811
c906108c
SS
1812/* memory allocation interface */
1813
7b5a2f43 1814static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1815
b60c80d6 1816static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1817
43f3e411 1818static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1819
6e5a29e1 1820static int attr_form_is_block (const struct attribute *);
8e19ed76 1821
6e5a29e1 1822static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1823
6e5a29e1 1824static int attr_form_is_constant (const struct attribute *);
3690dd37 1825
6e5a29e1 1826static int attr_form_is_ref (const struct attribute *);
7771576e 1827
8cf6f0b1
TT
1828static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1829 struct dwarf2_loclist_baton *baton,
ff39bb5e 1830 const struct attribute *attr);
8cf6f0b1 1831
ff39bb5e 1832static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1833 struct symbol *sym,
f1e6e072
TT
1834 struct dwarf2_cu *cu,
1835 int is_block);
4c2df51b 1836
d521ce57
TT
1837static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1838 const gdb_byte *info_ptr,
1839 struct abbrev_info *abbrev);
4bb7a0a7 1840
72bf9492
DJ
1841static hashval_t partial_die_hash (const void *item);
1842
1843static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1844
ae038cb0 1845static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1846 (sect_offset sect_off, unsigned int offset_in_dwz,
1847 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1848
9816fde3 1849static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1850 struct die_info *comp_unit_die,
1851 enum language pretend_language);
93311388 1852
ed2dc618 1853static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1854
dee91e82 1855static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1856
f792889a
DJ
1857static struct type *set_die_type (struct die_info *, struct type *,
1858 struct dwarf2_cu *);
1c379e20 1859
ed2dc618 1860static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1861
ed2dc618 1862static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1863
58f0c718 1864static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1865 enum language);
10b3939b 1866
95554aad
TT
1867static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1868 enum language);
10b3939b 1869
f4dc4d17
DE
1870static void process_full_type_unit (struct dwarf2_per_cu_data *,
1871 enum language);
1872
10b3939b
DJ
1873static void dwarf2_add_dependence (struct dwarf2_cu *,
1874 struct dwarf2_per_cu_data *);
1875
ae038cb0
DJ
1876static void dwarf2_mark (struct dwarf2_cu *);
1877
1878static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1879
b64f50a1 1880static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1881 struct dwarf2_per_cu_data *);
673bfd45 1882
f792889a 1883static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1884
95554aad
TT
1885static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1886 enum language pretend_language);
1887
ed2dc618 1888static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1889
b303c6f6
AB
1890/* Class, the destructor of which frees all allocated queue entries. This
1891 will only have work to do if an error was thrown while processing the
1892 dwarf. If no error was thrown then the queue entries should have all
1893 been processed, and freed, as we went along. */
1894
1895class dwarf2_queue_guard
1896{
1897public:
1898 dwarf2_queue_guard () = default;
1899
1900 /* Free any entries remaining on the queue. There should only be
1901 entries left if we hit an error while processing the dwarf. */
1902 ~dwarf2_queue_guard ()
1903 {
1904 struct dwarf2_queue_item *item, *last;
1905
1906 item = dwarf2_queue;
1907 while (item)
1908 {
1909 /* Anything still marked queued is likely to be in an
1910 inconsistent state, so discard it. */
1911 if (item->per_cu->queued)
1912 {
1913 if (item->per_cu->cu != NULL)
1914 free_one_cached_comp_unit (item->per_cu);
1915 item->per_cu->queued = 0;
1916 }
1917
1918 last = item;
1919 item = item->next;
1920 xfree (last);
1921 }
1922
1923 dwarf2_queue = dwarf2_queue_tail = NULL;
1924 }
1925};
1926
d721ba37
PA
1927/* The return type of find_file_and_directory. Note, the enclosed
1928 string pointers are only valid while this object is valid. */
1929
1930struct file_and_directory
1931{
1932 /* The filename. This is never NULL. */
1933 const char *name;
1934
1935 /* The compilation directory. NULL if not known. If we needed to
1936 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1937 points directly to the DW_AT_comp_dir string attribute owned by
1938 the obstack that owns the DIE. */
1939 const char *comp_dir;
1940
1941 /* If we needed to build a new string for comp_dir, this is what
1942 owns the storage. */
1943 std::string comp_dir_storage;
1944};
1945
1946static file_and_directory find_file_and_directory (struct die_info *die,
1947 struct dwarf2_cu *cu);
9291a0cd
TT
1948
1949static char *file_full_name (int file, struct line_header *lh,
1950 const char *comp_dir);
1951
43988095
JK
1952/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1953enum class rcuh_kind { COMPILE, TYPE };
1954
d521ce57 1955static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1956 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1957 struct comp_unit_head *header,
36586728 1958 struct dwarf2_section_info *section,
d521ce57 1959 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1960 rcuh_kind section_kind);
36586728 1961
fd820528 1962static void init_cutu_and_read_dies
f4dc4d17 1963 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1964 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1965 die_reader_func_ftype *die_reader_func, void *data);
1966
dee91e82
DE
1967static void init_cutu_and_read_dies_simple
1968 (struct dwarf2_per_cu_data *this_cu,
1969 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1970
673bfd45 1971static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1972
3019eac3
DE
1973static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1974
57d63ce2 1975static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1976 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1977 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1978 ULONGEST signature, int is_debug_types);
a2ce51a0 1979
ed2dc618
SM
1980static struct dwp_file *get_dwp_file
1981 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1982
3019eac3 1983static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1984 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1985
1986static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1987 (struct signatured_type *, const char *, const char *);
3019eac3 1988
89e63ee4
DE
1989static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1990
263db9a1 1991static void free_dwo_file (struct dwo_file *);
3019eac3 1992
263db9a1
TT
1993/* A unique_ptr helper to free a dwo_file. */
1994
1995struct dwo_file_deleter
ed2dc618 1996{
263db9a1
TT
1997 void operator() (struct dwo_file *df) const
1998 {
1999 free_dwo_file (df);
2000 }
ed2dc618
SM
2001};
2002
263db9a1
TT
2003/* A unique pointer to a dwo_file. */
2004
2005typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
2006
ed2dc618 2007static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2008
1b80a9fa 2009static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2010
2011static void free_line_header_voidp (void *arg);
4390d890
DE
2012\f
2013/* Various complaints about symbol reading that don't abort the process. */
2014
2015static void
2016dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2017{
b98664d3 2018 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2019}
2020
2021static void
2022dwarf2_debug_line_missing_file_complaint (void)
2023{
b98664d3 2024 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2025}
2026
2027static void
2028dwarf2_debug_line_missing_end_sequence_complaint (void)
2029{
b98664d3 2030 complaint (_(".debug_line section has line "
4390d890
DE
2031 "program sequence without an end"));
2032}
2033
2034static void
2035dwarf2_complex_location_expr_complaint (void)
2036{
b98664d3 2037 complaint (_("location expression too complex"));
4390d890
DE
2038}
2039
2040static void
2041dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2042 int arg3)
2043{
b98664d3 2044 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2045 arg1, arg2, arg3);
2046}
2047
2048static void
2049dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2050{
b98664d3 2051 complaint (_("debug info runs off end of %s section"
4390d890 2052 " [in module %s]"),
a32a8923
DE
2053 get_section_name (section),
2054 get_section_file_name (section));
4390d890 2055}
1b80a9fa 2056
4390d890
DE
2057static void
2058dwarf2_macro_malformed_definition_complaint (const char *arg1)
2059{
b98664d3 2060 complaint (_("macro debug info contains a "
4390d890
DE
2061 "malformed macro definition:\n`%s'"),
2062 arg1);
2063}
2064
2065static void
2066dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2067{
b98664d3 2068 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2069 arg1, arg2);
2070}
527f3840
JK
2071
2072/* Hash function for line_header_hash. */
2073
2074static hashval_t
2075line_header_hash (const struct line_header *ofs)
2076{
9c541725 2077 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2078}
2079
2080/* Hash function for htab_create_alloc_ex for line_header_hash. */
2081
2082static hashval_t
2083line_header_hash_voidp (const void *item)
2084{
9a3c8263 2085 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2086
2087 return line_header_hash (ofs);
2088}
2089
2090/* Equality function for line_header_hash. */
2091
2092static int
2093line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2094{
9a3c8263
SM
2095 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2096 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2097
9c541725 2098 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2099 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2100}
2101
4390d890 2102\f
9291a0cd 2103
31aa7e4e
JB
2104/* Read the given attribute value as an address, taking the attribute's
2105 form into account. */
2106
2107static CORE_ADDR
2108attr_value_as_address (struct attribute *attr)
2109{
2110 CORE_ADDR addr;
2111
336d760d
AT
2112 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2113 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2114 {
2115 /* Aside from a few clearly defined exceptions, attributes that
2116 contain an address must always be in DW_FORM_addr form.
2117 Unfortunately, some compilers happen to be violating this
2118 requirement by encoding addresses using other forms, such
2119 as DW_FORM_data4 for example. For those broken compilers,
2120 we try to do our best, without any guarantee of success,
2121 to interpret the address correctly. It would also be nice
2122 to generate a complaint, but that would require us to maintain
2123 a list of legitimate cases where a non-address form is allowed,
2124 as well as update callers to pass in at least the CU's DWARF
2125 version. This is more overhead than what we're willing to
2126 expand for a pretty rare case. */
2127 addr = DW_UNSND (attr);
2128 }
2129 else
2130 addr = DW_ADDR (attr);
2131
2132 return addr;
2133}
2134
330cdd98
PA
2135/* See declaration. */
2136
2137dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2138 const dwarf2_debug_sections *names)
2139 : objfile (objfile_)
2140{
2141 if (names == NULL)
2142 names = &dwarf2_elf_names;
2143
2144 bfd *obfd = objfile->obfd;
2145
2146 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2147 locate_sections (obfd, sec, *names);
2148}
2149
fc8e7e75
SM
2150static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2151
330cdd98
PA
2152dwarf2_per_objfile::~dwarf2_per_objfile ()
2153{
2154 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2155 free_cached_comp_units ();
2156
2157 if (quick_file_names_table)
2158 htab_delete (quick_file_names_table);
2159
2160 if (line_header_hash)
2161 htab_delete (line_header_hash);
2162
b76e467d
SM
2163 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2164 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2165
b2bdb8cf
SM
2166 for (signatured_type *sig_type : all_type_units)
2167 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2168
2169 VEC_free (dwarf2_section_info_def, types);
2170
2171 if (dwo_files != NULL)
2172 free_dwo_files (dwo_files, objfile);
fc8e7e75 2173
330cdd98
PA
2174 /* Everything else should be on the objfile obstack. */
2175}
2176
2177/* See declaration. */
2178
2179void
2180dwarf2_per_objfile::free_cached_comp_units ()
2181{
2182 dwarf2_per_cu_data *per_cu = read_in_chain;
2183 dwarf2_per_cu_data **last_chain = &read_in_chain;
2184 while (per_cu != NULL)
2185 {
2186 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2187
fcd3b13d 2188 delete per_cu->cu;
330cdd98
PA
2189 *last_chain = next_cu;
2190 per_cu = next_cu;
2191 }
2192}
2193
11ed8cad
TT
2194/* A helper class that calls free_cached_comp_units on
2195 destruction. */
2196
2197class free_cached_comp_units
2198{
2199public:
2200
2201 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2202 : m_per_objfile (per_objfile)
2203 {
2204 }
2205
2206 ~free_cached_comp_units ()
2207 {
2208 m_per_objfile->free_cached_comp_units ();
2209 }
2210
2211 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2212
2213private:
2214
2215 dwarf2_per_objfile *m_per_objfile;
2216};
2217
c906108c 2218/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2219 information and return true if we have enough to do something.
2220 NAMES points to the dwarf2 section names, or is NULL if the standard
2221 ELF names are used. */
c906108c
SS
2222
2223int
251d32d9
TG
2224dwarf2_has_info (struct objfile *objfile,
2225 const struct dwarf2_debug_sections *names)
c906108c 2226{
97cbe998
SDJ
2227 if (objfile->flags & OBJF_READNEVER)
2228 return 0;
2229
ed2dc618
SM
2230 struct dwarf2_per_objfile *dwarf2_per_objfile
2231 = get_dwarf2_per_objfile (objfile);
2232
2233 if (dwarf2_per_objfile == NULL)
5bfd760d
TT
2234 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2235 names);
2236
73869dc2 2237 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2238 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2239 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2240 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2241}
2242
2243/* Return the containing section of virtual section SECTION. */
2244
2245static struct dwarf2_section_info *
2246get_containing_section (const struct dwarf2_section_info *section)
2247{
2248 gdb_assert (section->is_virtual);
2249 return section->s.containing_section;
c906108c
SS
2250}
2251
a32a8923
DE
2252/* Return the bfd owner of SECTION. */
2253
2254static struct bfd *
2255get_section_bfd_owner (const struct dwarf2_section_info *section)
2256{
73869dc2
DE
2257 if (section->is_virtual)
2258 {
2259 section = get_containing_section (section);
2260 gdb_assert (!section->is_virtual);
2261 }
049412e3 2262 return section->s.section->owner;
a32a8923
DE
2263}
2264
2265/* Return the bfd section of SECTION.
2266 Returns NULL if the section is not present. */
2267
2268static asection *
2269get_section_bfd_section (const struct dwarf2_section_info *section)
2270{
73869dc2
DE
2271 if (section->is_virtual)
2272 {
2273 section = get_containing_section (section);
2274 gdb_assert (!section->is_virtual);
2275 }
049412e3 2276 return section->s.section;
a32a8923
DE
2277}
2278
2279/* Return the name of SECTION. */
2280
2281static const char *
2282get_section_name (const struct dwarf2_section_info *section)
2283{
2284 asection *sectp = get_section_bfd_section (section);
2285
2286 gdb_assert (sectp != NULL);
2287 return bfd_section_name (get_section_bfd_owner (section), sectp);
2288}
2289
2290/* Return the name of the file SECTION is in. */
2291
2292static const char *
2293get_section_file_name (const struct dwarf2_section_info *section)
2294{
2295 bfd *abfd = get_section_bfd_owner (section);
2296
2297 return bfd_get_filename (abfd);
2298}
2299
2300/* Return the id of SECTION.
2301 Returns 0 if SECTION doesn't exist. */
2302
2303static int
2304get_section_id (const struct dwarf2_section_info *section)
2305{
2306 asection *sectp = get_section_bfd_section (section);
2307
2308 if (sectp == NULL)
2309 return 0;
2310 return sectp->id;
2311}
2312
2313/* Return the flags of SECTION.
73869dc2 2314 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2315
2316static int
2317get_section_flags (const struct dwarf2_section_info *section)
2318{
2319 asection *sectp = get_section_bfd_section (section);
2320
2321 gdb_assert (sectp != NULL);
2322 return bfd_get_section_flags (sectp->owner, sectp);
2323}
2324
251d32d9
TG
2325/* When loading sections, we look either for uncompressed section or for
2326 compressed section names. */
233a11ab
CS
2327
2328static int
251d32d9
TG
2329section_is_p (const char *section_name,
2330 const struct dwarf2_section_names *names)
233a11ab 2331{
251d32d9
TG
2332 if (names->normal != NULL
2333 && strcmp (section_name, names->normal) == 0)
2334 return 1;
2335 if (names->compressed != NULL
2336 && strcmp (section_name, names->compressed) == 0)
2337 return 1;
2338 return 0;
233a11ab
CS
2339}
2340
330cdd98 2341/* See declaration. */
c906108c 2342
330cdd98
PA
2343void
2344dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2345 const dwarf2_debug_sections &names)
c906108c 2346{
dc7650b8 2347 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2348
dc7650b8
JK
2349 if ((aflag & SEC_HAS_CONTENTS) == 0)
2350 {
2351 }
330cdd98 2352 else if (section_is_p (sectp->name, &names.info))
c906108c 2353 {
330cdd98
PA
2354 this->info.s.section = sectp;
2355 this->info.size = bfd_get_section_size (sectp);
c906108c 2356 }
330cdd98 2357 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2358 {
330cdd98
PA
2359 this->abbrev.s.section = sectp;
2360 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2361 }
330cdd98 2362 else if (section_is_p (sectp->name, &names.line))
c906108c 2363 {
330cdd98
PA
2364 this->line.s.section = sectp;
2365 this->line.size = bfd_get_section_size (sectp);
c906108c 2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.loc))
c906108c 2368 {
330cdd98
PA
2369 this->loc.s.section = sectp;
2370 this->loc.size = bfd_get_section_size (sectp);
c906108c 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.loclists))
43988095 2373 {
330cdd98
PA
2374 this->loclists.s.section = sectp;
2375 this->loclists.size = bfd_get_section_size (sectp);
43988095 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2378 {
330cdd98
PA
2379 this->macinfo.s.section = sectp;
2380 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2383 {
330cdd98
PA
2384 this->macro.s.section = sectp;
2385 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.str))
c906108c 2388 {
330cdd98
PA
2389 this->str.s.section = sectp;
2390 this->str.size = bfd_get_section_size (sectp);
c906108c 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.line_str))
43988095 2393 {
330cdd98
PA
2394 this->line_str.s.section = sectp;
2395 this->line_str.size = bfd_get_section_size (sectp);
43988095 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2398 {
330cdd98
PA
2399 this->addr.s.section = sectp;
2400 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2403 {
330cdd98
PA
2404 this->frame.s.section = sectp;
2405 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2408 {
330cdd98
PA
2409 this->eh_frame.s.section = sectp;
2410 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2413 {
330cdd98
PA
2414 this->ranges.s.section = sectp;
2415 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2416 }
330cdd98 2417 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2418 {
330cdd98
PA
2419 this->rnglists.s.section = sectp;
2420 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2421 }
330cdd98 2422 else if (section_is_p (sectp->name, &names.types))
348e048f 2423 {
8b70b953
TT
2424 struct dwarf2_section_info type_section;
2425
2426 memset (&type_section, 0, sizeof (type_section));
049412e3 2427 type_section.s.section = sectp;
8b70b953
TT
2428 type_section.size = bfd_get_section_size (sectp);
2429
330cdd98 2430 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2431 &type_section);
348e048f 2432 }
330cdd98 2433 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2434 {
330cdd98
PA
2435 this->gdb_index.s.section = sectp;
2436 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2437 }
927aa2e7
JK
2438 else if (section_is_p (sectp->name, &names.debug_names))
2439 {
2440 this->debug_names.s.section = sectp;
2441 this->debug_names.size = bfd_get_section_size (sectp);
2442 }
2443 else if (section_is_p (sectp->name, &names.debug_aranges))
2444 {
2445 this->debug_aranges.s.section = sectp;
2446 this->debug_aranges.size = bfd_get_section_size (sectp);
2447 }
dce234bc 2448
b4e1fd61 2449 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2450 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2451 this->has_section_at_zero = true;
c906108c
SS
2452}
2453
fceca515
DE
2454/* A helper function that decides whether a section is empty,
2455 or not present. */
9e0ac564
TT
2456
2457static int
19ac8c2e 2458dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2459{
73869dc2
DE
2460 if (section->is_virtual)
2461 return section->size == 0;
049412e3 2462 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2463}
2464
cd4fb1b2 2465/* See dwarf2read.h. */
c906108c 2466
cd4fb1b2
SM
2467void
2468dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2469{
a32a8923 2470 asection *sectp;
3019eac3 2471 bfd *abfd;
dce234bc 2472 gdb_byte *buf, *retbuf;
c906108c 2473
be391dca
TT
2474 if (info->readin)
2475 return;
dce234bc 2476 info->buffer = NULL;
be391dca 2477 info->readin = 1;
188dd5d6 2478
9e0ac564 2479 if (dwarf2_section_empty_p (info))
dce234bc 2480 return;
c906108c 2481
a32a8923 2482 sectp = get_section_bfd_section (info);
3019eac3 2483
73869dc2
DE
2484 /* If this is a virtual section we need to read in the real one first. */
2485 if (info->is_virtual)
2486 {
2487 struct dwarf2_section_info *containing_section =
2488 get_containing_section (info);
2489
2490 gdb_assert (sectp != NULL);
2491 if ((sectp->flags & SEC_RELOC) != 0)
2492 {
2493 error (_("Dwarf Error: DWP format V2 with relocations is not"
2494 " supported in section %s [in module %s]"),
2495 get_section_name (info), get_section_file_name (info));
2496 }
2497 dwarf2_read_section (objfile, containing_section);
2498 /* Other code should have already caught virtual sections that don't
2499 fit. */
2500 gdb_assert (info->virtual_offset + info->size
2501 <= containing_section->size);
2502 /* If the real section is empty or there was a problem reading the
2503 section we shouldn't get here. */
2504 gdb_assert (containing_section->buffer != NULL);
2505 info->buffer = containing_section->buffer + info->virtual_offset;
2506 return;
2507 }
2508
4bf44c1c
TT
2509 /* If the section has relocations, we must read it ourselves.
2510 Otherwise we attach it to the BFD. */
2511 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2512 {
d521ce57 2513 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2514 return;
dce234bc 2515 }
dce234bc 2516
224c3ddb 2517 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2518 info->buffer = buf;
dce234bc
PP
2519
2520 /* When debugging .o files, we may need to apply relocations; see
2521 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2522 We never compress sections in .o files, so we only need to
2523 try this when the section is not compressed. */
ac8035ab 2524 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2525 if (retbuf != NULL)
2526 {
2527 info->buffer = retbuf;
2528 return;
2529 }
2530
a32a8923
DE
2531 abfd = get_section_bfd_owner (info);
2532 gdb_assert (abfd != NULL);
2533
dce234bc
PP
2534 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2535 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2536 {
2537 error (_("Dwarf Error: Can't read DWARF data"
2538 " in section %s [in module %s]"),
2539 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2540 }
dce234bc
PP
2541}
2542
9e0ac564
TT
2543/* A helper function that returns the size of a section in a safe way.
2544 If you are positive that the section has been read before using the
2545 size, then it is safe to refer to the dwarf2_section_info object's
2546 "size" field directly. In other cases, you must call this
2547 function, because for compressed sections the size field is not set
2548 correctly until the section has been read. */
2549
2550static bfd_size_type
2551dwarf2_section_size (struct objfile *objfile,
2552 struct dwarf2_section_info *info)
2553{
2554 if (!info->readin)
2555 dwarf2_read_section (objfile, info);
2556 return info->size;
2557}
2558
dce234bc 2559/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2560 SECTION_NAME. */
af34e669 2561
dce234bc 2562void
3017a003
TG
2563dwarf2_get_section_info (struct objfile *objfile,
2564 enum dwarf2_section_enum sect,
d521ce57 2565 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2566 bfd_size_type *sizep)
2567{
5bfd760d 2568 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2569 struct dwarf2_section_info *info;
a3b2a86b
TT
2570
2571 /* We may see an objfile without any DWARF, in which case we just
2572 return nothing. */
2573 if (data == NULL)
2574 {
2575 *sectp = NULL;
2576 *bufp = NULL;
2577 *sizep = 0;
2578 return;
2579 }
3017a003
TG
2580 switch (sect)
2581 {
2582 case DWARF2_DEBUG_FRAME:
2583 info = &data->frame;
2584 break;
2585 case DWARF2_EH_FRAME:
2586 info = &data->eh_frame;
2587 break;
2588 default:
2589 gdb_assert_not_reached ("unexpected section");
2590 }
dce234bc 2591
9e0ac564 2592 dwarf2_read_section (objfile, info);
dce234bc 2593
a32a8923 2594 *sectp = get_section_bfd_section (info);
dce234bc
PP
2595 *bufp = info->buffer;
2596 *sizep = info->size;
2597}
2598
36586728
TT
2599/* A helper function to find the sections for a .dwz file. */
2600
2601static void
2602locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2603{
9a3c8263 2604 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2605
2606 /* Note that we only support the standard ELF names, because .dwz
2607 is ELF-only (at the time of writing). */
2608 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2609 {
049412e3 2610 dwz_file->abbrev.s.section = sectp;
36586728
TT
2611 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2612 }
2613 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2614 {
049412e3 2615 dwz_file->info.s.section = sectp;
36586728
TT
2616 dwz_file->info.size = bfd_get_section_size (sectp);
2617 }
2618 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2619 {
049412e3 2620 dwz_file->str.s.section = sectp;
36586728
TT
2621 dwz_file->str.size = bfd_get_section_size (sectp);
2622 }
2623 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2624 {
049412e3 2625 dwz_file->line.s.section = sectp;
36586728
TT
2626 dwz_file->line.size = bfd_get_section_size (sectp);
2627 }
2628 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2629 {
049412e3 2630 dwz_file->macro.s.section = sectp;
36586728
TT
2631 dwz_file->macro.size = bfd_get_section_size (sectp);
2632 }
2ec9a5e0
TT
2633 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2634 {
049412e3 2635 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2636 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2637 }
927aa2e7
JK
2638 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2639 {
2640 dwz_file->debug_names.s.section = sectp;
2641 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2642 }
36586728
TT
2643}
2644
c4973306 2645/* See dwarf2read.h. */
36586728 2646
c4973306 2647struct dwz_file *
ed2dc618 2648dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2649{
36586728 2650 const char *filename;
acd13123 2651 bfd_size_type buildid_len_arg;
dc294be5
TT
2652 size_t buildid_len;
2653 bfd_byte *buildid;
36586728
TT
2654
2655 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2656 return dwarf2_per_objfile->dwz_file.get ();
36586728 2657
4db1a1dc 2658 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2659 gdb::unique_xmalloc_ptr<char> data
2660 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2661 &buildid_len_arg, &buildid));
4db1a1dc
TT
2662 if (data == NULL)
2663 {
2664 if (bfd_get_error () == bfd_error_no_error)
2665 return NULL;
2666 error (_("could not read '.gnu_debugaltlink' section: %s"),
2667 bfd_errmsg (bfd_get_error ()));
2668 }
791afaa2
TT
2669
2670 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2671
acd13123
TT
2672 buildid_len = (size_t) buildid_len_arg;
2673
791afaa2 2674 filename = data.get ();
d721ba37
PA
2675
2676 std::string abs_storage;
36586728
TT
2677 if (!IS_ABSOLUTE_PATH (filename))
2678 {
14278e1f
TT
2679 gdb::unique_xmalloc_ptr<char> abs
2680 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2681
14278e1f 2682 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2683 filename = abs_storage.c_str ();
36586728
TT
2684 }
2685
dc294be5
TT
2686 /* First try the file name given in the section. If that doesn't
2687 work, try to use the build-id instead. */
192b62ce 2688 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2689 if (dwz_bfd != NULL)
36586728 2690 {
192b62ce 2691 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2692 dwz_bfd.reset (nullptr);
36586728
TT
2693 }
2694
dc294be5
TT
2695 if (dwz_bfd == NULL)
2696 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2697
2698 if (dwz_bfd == NULL)
2699 error (_("could not find '.gnu_debugaltlink' file for %s"),
2700 objfile_name (dwarf2_per_objfile->objfile));
2701
7ff8cb8c
TT
2702 std::unique_ptr<struct dwz_file> result
2703 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2704
7ff8cb8c
TT
2705 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2706 result.get ());
36586728 2707
7ff8cb8c
TT
2708 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2709 result->dwz_bfd.get ());
2710 dwarf2_per_objfile->dwz_file = std::move (result);
2711 return dwarf2_per_objfile->dwz_file.get ();
36586728 2712}
9291a0cd 2713\f
7b9f3c50
DE
2714/* DWARF quick_symbols_functions support. */
2715
2716/* TUs can share .debug_line entries, and there can be a lot more TUs than
2717 unique line tables, so we maintain a separate table of all .debug_line
2718 derived entries to support the sharing.
2719 All the quick functions need is the list of file names. We discard the
2720 line_header when we're done and don't need to record it here. */
2721struct quick_file_names
2722{
094b34ac
DE
2723 /* The data used to construct the hash key. */
2724 struct stmt_list_hash hash;
7b9f3c50
DE
2725
2726 /* The number of entries in file_names, real_names. */
2727 unsigned int num_file_names;
2728
2729 /* The file names from the line table, after being run through
2730 file_full_name. */
2731 const char **file_names;
2732
2733 /* The file names from the line table after being run through
2734 gdb_realpath. These are computed lazily. */
2735 const char **real_names;
2736};
2737
2738/* When using the index (and thus not using psymtabs), each CU has an
2739 object of this type. This is used to hold information needed by
2740 the various "quick" methods. */
2741struct dwarf2_per_cu_quick_data
2742{
2743 /* The file table. This can be NULL if there was no file table
2744 or it's currently not read in.
2745 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2746 struct quick_file_names *file_names;
2747
2748 /* The corresponding symbol table. This is NULL if symbols for this
2749 CU have not yet been read. */
43f3e411 2750 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2751
2752 /* A temporary mark bit used when iterating over all CUs in
2753 expand_symtabs_matching. */
2754 unsigned int mark : 1;
2755
2756 /* True if we've tried to read the file table and found there isn't one.
2757 There will be no point in trying to read it again next time. */
2758 unsigned int no_file_data : 1;
2759};
2760
094b34ac
DE
2761/* Utility hash function for a stmt_list_hash. */
2762
2763static hashval_t
2764hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2765{
2766 hashval_t v = 0;
2767
2768 if (stmt_list_hash->dwo_unit != NULL)
2769 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2770 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2771 return v;
2772}
2773
2774/* Utility equality function for a stmt_list_hash. */
2775
2776static int
2777eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2778 const struct stmt_list_hash *rhs)
2779{
2780 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2781 return 0;
2782 if (lhs->dwo_unit != NULL
2783 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2784 return 0;
2785
9c541725 2786 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2787}
2788
7b9f3c50
DE
2789/* Hash function for a quick_file_names. */
2790
2791static hashval_t
2792hash_file_name_entry (const void *e)
2793{
9a3c8263
SM
2794 const struct quick_file_names *file_data
2795 = (const struct quick_file_names *) e;
7b9f3c50 2796
094b34ac 2797 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2798}
2799
2800/* Equality function for a quick_file_names. */
2801
2802static int
2803eq_file_name_entry (const void *a, const void *b)
2804{
9a3c8263
SM
2805 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2806 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2807
094b34ac 2808 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2809}
2810
2811/* Delete function for a quick_file_names. */
2812
2813static void
2814delete_file_name_entry (void *e)
2815{
9a3c8263 2816 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2817 int i;
2818
2819 for (i = 0; i < file_data->num_file_names; ++i)
2820 {
2821 xfree ((void*) file_data->file_names[i]);
2822 if (file_data->real_names)
2823 xfree ((void*) file_data->real_names[i]);
2824 }
2825
2826 /* The space for the struct itself lives on objfile_obstack,
2827 so we don't free it here. */
2828}
2829
2830/* Create a quick_file_names hash table. */
2831
2832static htab_t
2833create_quick_file_names_table (unsigned int nr_initial_entries)
2834{
2835 return htab_create_alloc (nr_initial_entries,
2836 hash_file_name_entry, eq_file_name_entry,
2837 delete_file_name_entry, xcalloc, xfree);
2838}
9291a0cd 2839
918dd910
JK
2840/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2841 have to be created afterwards. You should call age_cached_comp_units after
2842 processing PER_CU->CU. dw2_setup must have been already called. */
2843
2844static void
58f0c718 2845load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2846{
3019eac3 2847 if (per_cu->is_debug_types)
e5fe5e75 2848 load_full_type_unit (per_cu);
918dd910 2849 else
58f0c718 2850 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2851
cc12ce38
DE
2852 if (per_cu->cu == NULL)
2853 return; /* Dummy CU. */
2dc860c0
DE
2854
2855 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2856}
2857
a0f42c21 2858/* Read in the symbols for PER_CU. */
2fdf6df6 2859
9291a0cd 2860static void
58f0c718 2861dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2862{
ed2dc618 2863 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2864
f4dc4d17
DE
2865 /* Skip type_unit_groups, reading the type units they contain
2866 is handled elsewhere. */
2867 if (IS_TYPE_UNIT_GROUP (per_cu))
2868 return;
2869
b303c6f6
AB
2870 /* The destructor of dwarf2_queue_guard frees any entries left on
2871 the queue. After this point we're guaranteed to leave this function
2872 with the dwarf queue empty. */
2873 dwarf2_queue_guard q_guard;
9291a0cd 2874
95554aad 2875 if (dwarf2_per_objfile->using_index
43f3e411 2876 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2877 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2878 {
2879 queue_comp_unit (per_cu, language_minimal);
58f0c718 2880 load_cu (per_cu, skip_partial);
89e63ee4
DE
2881
2882 /* If we just loaded a CU from a DWO, and we're working with an index
2883 that may badly handle TUs, load all the TUs in that DWO as well.
2884 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2885 if (!per_cu->is_debug_types
cc12ce38 2886 && per_cu->cu != NULL
89e63ee4
DE
2887 && per_cu->cu->dwo_unit != NULL
2888 && dwarf2_per_objfile->index_table != NULL
2889 && dwarf2_per_objfile->index_table->version <= 7
2890 /* DWP files aren't supported yet. */
ed2dc618 2891 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2892 queue_and_load_all_dwo_tus (per_cu);
95554aad 2893 }
9291a0cd 2894
ed2dc618 2895 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2896
2897 /* Age the cache, releasing compilation units that have not
2898 been used recently. */
ed2dc618 2899 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2900}
2901
2902/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2903 the objfile from which this CU came. Returns the resulting symbol
2904 table. */
2fdf6df6 2905
43f3e411 2906static struct compunit_symtab *
58f0c718 2907dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2908{
ed2dc618
SM
2909 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2910
95554aad 2911 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2912 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2913 {
11ed8cad 2914 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2915 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2916 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2917 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2918 }
f194fefb 2919
43f3e411 2920 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2921}
2922
ff4c9fec 2923/* See declaration. */
f4dc4d17 2924
ff4c9fec
SM
2925dwarf2_per_cu_data *
2926dwarf2_per_objfile::get_cutu (int index)
2927{
b76e467d 2928 if (index >= this->all_comp_units.size ())
ff4c9fec 2929 {
b76e467d 2930 index -= this->all_comp_units.size ();
b2bdb8cf 2931 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2932 return &this->all_type_units[index]->per_cu;
2933 }
f4dc4d17 2934
ff4c9fec
SM
2935 return this->all_comp_units[index];
2936}
f4dc4d17 2937
ff4c9fec 2938/* See declaration. */
2fdf6df6 2939
ff4c9fec
SM
2940dwarf2_per_cu_data *
2941dwarf2_per_objfile::get_cu (int index)
1fd400ff 2942{
b76e467d 2943 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2944
ff4c9fec 2945 return this->all_comp_units[index];
f4dc4d17
DE
2946}
2947
ff4c9fec 2948/* See declaration. */
f4dc4d17 2949
ff4c9fec
SM
2950signatured_type *
2951dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2952{
b2bdb8cf 2953 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2954
ff4c9fec 2955 return this->all_type_units[index];
1fd400ff
TT
2956}
2957
4b514bc8
JK
2958/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2959 objfile_obstack, and constructed with the specified field
2960 values. */
2961
2962static dwarf2_per_cu_data *
ed2dc618 2963create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2964 struct dwarf2_section_info *section,
2965 int is_dwz,
2966 sect_offset sect_off, ULONGEST length)
2967{
ed2dc618 2968 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2969 dwarf2_per_cu_data *the_cu
2970 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2971 struct dwarf2_per_cu_data);
2972 the_cu->sect_off = sect_off;
2973 the_cu->length = length;
e3b94546 2974 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2975 the_cu->section = section;
2976 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2977 struct dwarf2_per_cu_quick_data);
2978 the_cu->is_dwz = is_dwz;
2979 return the_cu;
2980}
2981
2ec9a5e0
TT
2982/* A helper for create_cus_from_index that handles a given list of
2983 CUs. */
2fdf6df6 2984
74a0d9f6 2985static void
12359b5e 2986create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2987 const gdb_byte *cu_list, offset_type n_elements,
2988 struct dwarf2_section_info *section,
b76e467d 2989 int is_dwz)
9291a0cd 2990{
12359b5e 2991 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2992 {
74a0d9f6 2993 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2994
2995 sect_offset sect_off
2996 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2997 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2998 cu_list += 2 * 8;
2999
b76e467d 3000 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3001 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3002 sect_off, length);
b76e467d 3003 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3004 }
9291a0cd
TT
3005}
3006
2ec9a5e0 3007/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3008 the CU objects for this objfile. */
2ec9a5e0 3009
74a0d9f6 3010static void
12359b5e 3011create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3012 const gdb_byte *cu_list, offset_type cu_list_elements,
3013 const gdb_byte *dwz_list, offset_type dwz_elements)
3014{
b76e467d
SM
3015 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3016 dwarf2_per_objfile->all_comp_units.reserve
3017 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3018
12359b5e 3019 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3020 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3021
3022 if (dwz_elements == 0)
74a0d9f6 3023 return;
2ec9a5e0 3024
12359b5e
SM
3025 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3026 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3027 &dwz->info, 1);
2ec9a5e0
TT
3028}
3029
1fd400ff 3030/* Create the signatured type hash table from the index. */
673bfd45 3031
74a0d9f6 3032static void
12359b5e
SM
3033create_signatured_type_table_from_index
3034 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3035 struct dwarf2_section_info *section,
3036 const gdb_byte *bytes,
3037 offset_type elements)
1fd400ff 3038{
12359b5e 3039 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3040
b2bdb8cf
SM
3041 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3042 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3043
12359b5e 3044 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3045
12359b5e 3046 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3047 {
52dc124a 3048 struct signatured_type *sig_type;
9c541725 3049 ULONGEST signature;
1fd400ff 3050 void **slot;
9c541725 3051 cu_offset type_offset_in_tu;
1fd400ff 3052
74a0d9f6 3053 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3054 sect_offset sect_off
3055 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3056 type_offset_in_tu
3057 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3058 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3059 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3060 bytes += 3 * 8;
3061
52dc124a 3062 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3063 struct signatured_type);
52dc124a 3064 sig_type->signature = signature;
9c541725 3065 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3066 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3067 sig_type->per_cu.section = section;
9c541725 3068 sig_type->per_cu.sect_off = sect_off;
e3b94546 3069 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3070 sig_type->per_cu.v.quick
1fd400ff
TT
3071 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3072 struct dwarf2_per_cu_quick_data);
3073
52dc124a
DE
3074 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3075 *slot = sig_type;
1fd400ff 3076
b2bdb8cf 3077 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3078 }
3079
673bfd45 3080 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3081}
3082
927aa2e7
JK
3083/* Create the signatured type hash table from .debug_names. */
3084
3085static void
3086create_signatured_type_table_from_debug_names
ed2dc618 3087 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3088 const mapped_debug_names &map,
3089 struct dwarf2_section_info *section,
3090 struct dwarf2_section_info *abbrev_section)
3091{
ed2dc618
SM
3092 struct objfile *objfile = dwarf2_per_objfile->objfile;
3093
927aa2e7
JK
3094 dwarf2_read_section (objfile, section);
3095 dwarf2_read_section (objfile, abbrev_section);
3096
b2bdb8cf
SM
3097 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3098 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3099
3100 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3101
3102 for (uint32_t i = 0; i < map.tu_count; ++i)
3103 {
3104 struct signatured_type *sig_type;
927aa2e7 3105 void **slot;
927aa2e7
JK
3106
3107 sect_offset sect_off
3108 = (sect_offset) (extract_unsigned_integer
3109 (map.tu_table_reordered + i * map.offset_size,
3110 map.offset_size,
3111 map.dwarf5_byte_order));
3112
3113 comp_unit_head cu_header;
ed2dc618
SM
3114 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3115 abbrev_section,
927aa2e7
JK
3116 section->buffer + to_underlying (sect_off),
3117 rcuh_kind::TYPE);
3118
3119 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3120 struct signatured_type);
3121 sig_type->signature = cu_header.signature;
3122 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3123 sig_type->per_cu.is_debug_types = 1;
3124 sig_type->per_cu.section = section;
3125 sig_type->per_cu.sect_off = sect_off;
e3b94546 3126 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3127 sig_type->per_cu.v.quick
3128 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3129 struct dwarf2_per_cu_quick_data);
3130
3131 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3132 *slot = sig_type;
3133
b2bdb8cf 3134 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3135 }
3136
3137 dwarf2_per_objfile->signatured_types = sig_types_hash;
3138}
3139
9291a0cd
TT
3140/* Read the address map data from the mapped index, and use it to
3141 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3142
9291a0cd 3143static void
ed2dc618
SM
3144create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3145 struct mapped_index *index)
9291a0cd 3146{
ed2dc618 3147 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3148 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3149 const gdb_byte *iter, *end;
9291a0cd 3150 struct addrmap *mutable_map;
9291a0cd
TT
3151 CORE_ADDR baseaddr;
3152
8268c778
PA
3153 auto_obstack temp_obstack;
3154
9291a0cd
TT
3155 mutable_map = addrmap_create_mutable (&temp_obstack);
3156
f00a2de2
PA
3157 iter = index->address_table.data ();
3158 end = iter + index->address_table.size ();
9291a0cd
TT
3159
3160 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3161
3162 while (iter < end)
3163 {
3164 ULONGEST hi, lo, cu_index;
3165 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3166 iter += 8;
3167 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3168 iter += 8;
3169 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3170 iter += 4;
f652bce2 3171
24a55014 3172 if (lo > hi)
f652bce2 3173 {
b98664d3 3174 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3175 hex_string (lo), hex_string (hi));
24a55014 3176 continue;
f652bce2 3177 }
24a55014 3178
b76e467d 3179 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3180 {
b98664d3 3181 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3182 (unsigned) cu_index);
24a55014 3183 continue;
f652bce2 3184 }
24a55014 3185
79748972
TT
3186 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3187 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3188 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3189 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3190 }
3191
d320c2b5 3192 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3193 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3194}
3195
927aa2e7
JK
3196/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3197 populate the objfile's psymtabs_addrmap. */
3198
3199static void
ed2dc618 3200create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3201 struct dwarf2_section_info *section)
3202{
ed2dc618 3203 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3204 bfd *abfd = objfile->obfd;
3205 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3206 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3207 SECT_OFF_TEXT (objfile));
3208
3209 auto_obstack temp_obstack;
3210 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3211
3212 std::unordered_map<sect_offset,
3213 dwarf2_per_cu_data *,
3214 gdb::hash_enum<sect_offset>>
3215 debug_info_offset_to_per_cu;
b76e467d 3216 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3217 {
927aa2e7
JK
3218 const auto insertpair
3219 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3220 if (!insertpair.second)
3221 {
3222 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3223 "debug_info_offset %s, ignoring .debug_aranges."),
3224 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3225 return;
3226 }
3227 }
3228
3229 dwarf2_read_section (objfile, section);
3230
3231 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3232
3233 const gdb_byte *addr = section->buffer;
3234
3235 while (addr < section->buffer + section->size)
3236 {
3237 const gdb_byte *const entry_addr = addr;
3238 unsigned int bytes_read;
3239
3240 const LONGEST entry_length = read_initial_length (abfd, addr,
3241 &bytes_read);
3242 addr += bytes_read;
3243
3244 const gdb_byte *const entry_end = addr + entry_length;
3245 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3246 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3247 if (addr + entry_length > section->buffer + section->size)
3248 {
3249 warning (_("Section .debug_aranges in %s entry at offset %zu "
3250 "length %s exceeds section length %s, "
3251 "ignoring .debug_aranges."),
3252 objfile_name (objfile), entry_addr - section->buffer,
3253 plongest (bytes_read + entry_length),
3254 pulongest (section->size));
3255 return;
3256 }
3257
3258 /* The version number. */
3259 const uint16_t version = read_2_bytes (abfd, addr);
3260 addr += 2;
3261 if (version != 2)
3262 {
3263 warning (_("Section .debug_aranges in %s entry at offset %zu "
3264 "has unsupported version %d, ignoring .debug_aranges."),
3265 objfile_name (objfile), entry_addr - section->buffer,
3266 version);
3267 return;
3268 }
3269
3270 const uint64_t debug_info_offset
3271 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3272 addr += offset_size;
3273 const auto per_cu_it
3274 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3275 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3276 {
3277 warning (_("Section .debug_aranges in %s entry at offset %zu "
3278 "debug_info_offset %s does not exists, "
3279 "ignoring .debug_aranges."),
3280 objfile_name (objfile), entry_addr - section->buffer,
3281 pulongest (debug_info_offset));
3282 return;
3283 }
3284 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3285
3286 const uint8_t address_size = *addr++;
3287 if (address_size < 1 || address_size > 8)
3288 {
3289 warning (_("Section .debug_aranges in %s entry at offset %zu "
3290 "address_size %u is invalid, ignoring .debug_aranges."),
3291 objfile_name (objfile), entry_addr - section->buffer,
3292 address_size);
3293 return;
3294 }
3295
3296 const uint8_t segment_selector_size = *addr++;
3297 if (segment_selector_size != 0)
3298 {
3299 warning (_("Section .debug_aranges in %s entry at offset %zu "
3300 "segment_selector_size %u is not supported, "
3301 "ignoring .debug_aranges."),
3302 objfile_name (objfile), entry_addr - section->buffer,
3303 segment_selector_size);
3304 return;
3305 }
3306
3307 /* Must pad to an alignment boundary that is twice the address
3308 size. It is undocumented by the DWARF standard but GCC does
3309 use it. */
3310 for (size_t padding = ((-(addr - section->buffer))
3311 & (2 * address_size - 1));
3312 padding > 0; padding--)
3313 if (*addr++ != 0)
3314 {
3315 warning (_("Section .debug_aranges in %s entry at offset %zu "
3316 "padding is not zero, ignoring .debug_aranges."),
3317 objfile_name (objfile), entry_addr - section->buffer);
3318 return;
3319 }
3320
3321 for (;;)
3322 {
3323 if (addr + 2 * address_size > entry_end)
3324 {
3325 warning (_("Section .debug_aranges in %s entry at offset %zu "
3326 "address list is not properly terminated, "
3327 "ignoring .debug_aranges."),
3328 objfile_name (objfile), entry_addr - section->buffer);
3329 return;
3330 }
3331 ULONGEST start = extract_unsigned_integer (addr, address_size,
3332 dwarf5_byte_order);
3333 addr += address_size;
3334 ULONGEST length = extract_unsigned_integer (addr, address_size,
3335 dwarf5_byte_order);
3336 addr += address_size;
3337 if (start == 0 && length == 0)
3338 break;
3339 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3340 {
3341 /* Symbol was eliminated due to a COMDAT group. */
3342 continue;
3343 }
3344 ULONGEST end = start + length;
79748972
TT
3345 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3346 - baseaddr);
3347 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3348 - baseaddr);
927aa2e7
JK
3349 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3350 }
3351 }
3352
d320c2b5 3353 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3354 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3355}
3356
9291a0cd
TT
3357/* Find a slot in the mapped index INDEX for the object named NAME.
3358 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3359 constant pool and return true. If NAME cannot be found, return
3360 false. */
2fdf6df6 3361
109483d9 3362static bool
9291a0cd
TT
3363find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3364 offset_type **vec_out)
3365{
0cf03b49 3366 offset_type hash;
9291a0cd 3367 offset_type slot, step;
559a7a62 3368 int (*cmp) (const char *, const char *);
9291a0cd 3369
791afaa2 3370 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3371 if (current_language->la_language == language_cplus
45280282
IB
3372 || current_language->la_language == language_fortran
3373 || current_language->la_language == language_d)
0cf03b49
JK
3374 {
3375 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3376 not contain any. */
a8719064 3377
72998fb3 3378 if (strchr (name, '(') != NULL)
0cf03b49 3379 {
109483d9 3380 without_params = cp_remove_params (name);
0cf03b49 3381
72998fb3 3382 if (without_params != NULL)
791afaa2 3383 name = without_params.get ();
0cf03b49
JK
3384 }
3385 }
3386
559a7a62 3387 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3388 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3389 simulate our NAME being searched is also lowercased. */
3390 hash = mapped_index_string_hash ((index->version == 4
3391 && case_sensitivity == case_sensitive_off
3392 ? 5 : index->version),
3393 name);
3394
f00a2de2
PA
3395 slot = hash & (index->symbol_table.size () - 1);
3396 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3398
3399 for (;;)
3400 {
9291a0cd 3401 const char *str;
f00a2de2
PA
3402
3403 const auto &bucket = index->symbol_table[slot];
3404 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3405 return false;
9291a0cd 3406
f00a2de2 3407 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3408 if (!cmp (name, str))
9291a0cd
TT
3409 {
3410 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3411 + MAYBE_SWAP (bucket.vec));
109483d9 3412 return true;
9291a0cd
TT
3413 }
3414
f00a2de2 3415 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3416 }
3417}
3418
4485a1c1
SM
3419/* A helper function that reads the .gdb_index from BUFFER and fills
3420 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3421 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3422 ok to use deprecated sections.
3423
3424 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3425 out parameters that are filled in with information about the CU and
3426 TU lists in the section.
3427
4485a1c1 3428 Returns true if all went well, false otherwise. */
2fdf6df6 3429
d33bc52e 3430static bool
4485a1c1
SM
3431read_gdb_index_from_buffer (struct objfile *objfile,
3432 const char *filename,
3433 bool deprecated_ok,
3434 gdb::array_view<const gdb_byte> buffer,
3435 struct mapped_index *map,
3436 const gdb_byte **cu_list,
3437 offset_type *cu_list_elements,
3438 const gdb_byte **types_list,
3439 offset_type *types_list_elements)
3440{
3441 const gdb_byte *addr = &buffer[0];
82430852 3442
9291a0cd 3443 /* Version check. */
4485a1c1 3444 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3445 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3446 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3447 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3448 indices. */
831adc1f 3449 if (version < 4)
481860b3
GB
3450 {
3451 static int warning_printed = 0;
3452 if (!warning_printed)
3453 {
3454 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3455 filename);
481860b3
GB
3456 warning_printed = 1;
3457 }
3458 return 0;
3459 }
3460 /* Index version 4 uses a different hash function than index version
3461 5 and later.
3462
3463 Versions earlier than 6 did not emit psymbols for inlined
3464 functions. Using these files will cause GDB not to be able to
3465 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3466 indices unless the user has done
3467 "set use-deprecated-index-sections on". */
2ec9a5e0 3468 if (version < 6 && !deprecated_ok)
481860b3
GB
3469 {
3470 static int warning_printed = 0;
3471 if (!warning_printed)
3472 {
e615022a
DE
3473 warning (_("\
3474Skipping deprecated .gdb_index section in %s.\n\
3475Do \"set use-deprecated-index-sections on\" before the file is read\n\
3476to use the section anyway."),
2ec9a5e0 3477 filename);
481860b3
GB
3478 warning_printed = 1;
3479 }
3480 return 0;
3481 }
796a7ff8 3482 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3483 of the TU (for symbols coming from TUs),
3484 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3485 Plus gold-generated indices can have duplicate entries for global symbols,
3486 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3487 These are just performance bugs, and we can't distinguish gdb-generated
3488 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3489
481860b3 3490 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3491 longer backward compatible. */
796a7ff8 3492 if (version > 8)
594e8718 3493 return 0;
9291a0cd 3494
559a7a62 3495 map->version = version;
9291a0cd 3496
4485a1c1 3497 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3498
4485a1c1 3499 int i = 0;
2ec9a5e0
TT
3500 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3501 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3502 / 8);
1fd400ff
TT
3503 ++i;
3504
2ec9a5e0
TT
3505 *types_list = addr + MAYBE_SWAP (metadata[i]);
3506 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3507 - MAYBE_SWAP (metadata[i]))
3508 / 8);
987d643c 3509 ++i;
1fd400ff 3510
f00a2de2
PA
3511 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3512 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3513 map->address_table
3514 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3515 ++i;
3516
f00a2de2
PA
3517 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3518 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 map->symbol_table
3520 = gdb::array_view<mapped_index::symbol_table_slot>
3521 ((mapped_index::symbol_table_slot *) symbol_table,
3522 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3523
f00a2de2 3524 ++i;
f9d83a0b 3525 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3526
2ec9a5e0
TT
3527 return 1;
3528}
3529
4485a1c1
SM
3530/* Callback types for dwarf2_read_gdb_index. */
3531
3532typedef gdb::function_view
3533 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3534 get_gdb_index_contents_ftype;
3535typedef gdb::function_view
3536 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3537 get_gdb_index_contents_dwz_ftype;
3538
927aa2e7 3539/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3540 elements of all the CUs and return 1. Otherwise, return 0. */
3541
3542static int
4485a1c1
SM
3543dwarf2_read_gdb_index
3544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3545 get_gdb_index_contents_ftype get_gdb_index_contents,
3546 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3547{
2ec9a5e0
TT
3548 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3549 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3550 struct dwz_file *dwz;
12359b5e 3551 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3552
4485a1c1
SM
3553 gdb::array_view<const gdb_byte> main_index_contents
3554 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3555
3556 if (main_index_contents.empty ())
3557 return 0;
3558
3063847f 3559 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3560 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3561 use_deprecated_index_sections,
3562 main_index_contents, map.get (), &cu_list,
3563 &cu_list_elements, &types_list,
3564 &types_list_elements))
2ec9a5e0
TT
3565 return 0;
3566
0fefef59 3567 /* Don't use the index if it's empty. */
3063847f 3568 if (map->symbol_table.empty ())
0fefef59
DE
3569 return 0;
3570
2ec9a5e0
TT
3571 /* If there is a .dwz file, read it so we can get its CU list as
3572 well. */
ed2dc618 3573 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3574 if (dwz != NULL)
2ec9a5e0 3575 {
2ec9a5e0
TT
3576 struct mapped_index dwz_map;
3577 const gdb_byte *dwz_types_ignore;
3578 offset_type dwz_types_elements_ignore;
3579
4485a1c1
SM
3580 gdb::array_view<const gdb_byte> dwz_index_content
3581 = get_gdb_index_contents_dwz (objfile, dwz);
3582
3583 if (dwz_index_content.empty ())
3584 return 0;
3585
3586 if (!read_gdb_index_from_buffer (objfile,
3587 bfd_get_filename (dwz->dwz_bfd), 1,
3588 dwz_index_content, &dwz_map,
3589 &dwz_list, &dwz_list_elements,
3590 &dwz_types_ignore,
3591 &dwz_types_elements_ignore))
2ec9a5e0
TT
3592 {
3593 warning (_("could not read '.gdb_index' section from %s; skipping"),
3594 bfd_get_filename (dwz->dwz_bfd));
3595 return 0;
3596 }
3597 }
3598
12359b5e
SM
3599 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3600 dwz_list, dwz_list_elements);
1fd400ff 3601
8b70b953
TT
3602 if (types_list_elements)
3603 {
3604 struct dwarf2_section_info *section;
3605
3606 /* We can only handle a single .debug_types when we have an
3607 index. */
3608 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3609 return 0;
3610
3611 section = VEC_index (dwarf2_section_info_def,
3612 dwarf2_per_objfile->types, 0);
3613
12359b5e
SM
3614 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3615 types_list, types_list_elements);
8b70b953 3616 }
9291a0cd 3617
3063847f 3618 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3619
3063847f 3620 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3621 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3622 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3623 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3624
3625 return 1;
3626}
3627
dee91e82 3628/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3629
dee91e82
DE
3630static void
3631dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3632 const gdb_byte *info_ptr,
dee91e82
DE
3633 struct die_info *comp_unit_die,
3634 int has_children,
3635 void *data)
9291a0cd 3636{
dee91e82 3637 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3638 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3639 struct dwarf2_per_objfile *dwarf2_per_objfile
3640 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3641 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3642 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3643 struct attribute *attr;
dee91e82 3644 int i;
7b9f3c50
DE
3645 void **slot;
3646 struct quick_file_names *qfn;
9291a0cd 3647
0186c6a7
DE
3648 gdb_assert (! this_cu->is_debug_types);
3649
07261596
TT
3650 /* Our callers never want to match partial units -- instead they
3651 will match the enclosing full CU. */
3652 if (comp_unit_die->tag == DW_TAG_partial_unit)
3653 {
3654 this_cu->v.quick->no_file_data = 1;
3655 return;
3656 }
3657
0186c6a7 3658 lh_cu = this_cu;
7b9f3c50 3659 slot = NULL;
dee91e82 3660
fff8551c 3661 line_header_up lh;
9c541725 3662 sect_offset line_offset {};
fff8551c 3663
dee91e82 3664 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3665 if (attr)
3666 {
7b9f3c50
DE
3667 struct quick_file_names find_entry;
3668
9c541725 3669 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3670
3671 /* We may have already read in this line header (TU line header sharing).
3672 If we have we're done. */
094b34ac 3673 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3674 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3675 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3676 &find_entry, INSERT);
3677 if (*slot != NULL)
3678 {
9a3c8263 3679 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3680 return;
7b9f3c50
DE
3681 }
3682
3019eac3 3683 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3684 }
3685 if (lh == NULL)
3686 {
094b34ac 3687 lh_cu->v.quick->no_file_data = 1;
dee91e82 3688 return;
9291a0cd
TT
3689 }
3690
8d749320 3691 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3692 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3693 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3694 gdb_assert (slot != NULL);
3695 *slot = qfn;
9291a0cd 3696
d721ba37 3697 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3698
fff8551c 3699 qfn->num_file_names = lh->file_names.size ();
8d749320 3700 qfn->file_names =
fff8551c
PA
3701 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3702 for (i = 0; i < lh->file_names.size (); ++i)
3703 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3704 qfn->real_names = NULL;
9291a0cd 3705
094b34ac 3706 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3707}
3708
3709/* A helper for the "quick" functions which attempts to read the line
3710 table for THIS_CU. */
3711
3712static struct quick_file_names *
e4a48d9d 3713dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3714{
0186c6a7
DE
3715 /* This should never be called for TUs. */
3716 gdb_assert (! this_cu->is_debug_types);
3717 /* Nor type unit groups. */
3718 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3719
dee91e82
DE
3720 if (this_cu->v.quick->file_names != NULL)
3721 return this_cu->v.quick->file_names;
3722 /* If we know there is no line data, no point in looking again. */
3723 if (this_cu->v.quick->no_file_data)
3724 return NULL;
3725
0186c6a7 3726 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3727
3728 if (this_cu->v.quick->no_file_data)
3729 return NULL;
3730 return this_cu->v.quick->file_names;
9291a0cd
TT
3731}
3732
3733/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3734 real path for a given file name from the line table. */
2fdf6df6 3735
9291a0cd 3736static const char *
7b9f3c50
DE
3737dw2_get_real_path (struct objfile *objfile,
3738 struct quick_file_names *qfn, int index)
9291a0cd 3739{
7b9f3c50
DE
3740 if (qfn->real_names == NULL)
3741 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3742 qfn->num_file_names, const char *);
9291a0cd 3743
7b9f3c50 3744 if (qfn->real_names[index] == NULL)
14278e1f 3745 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3746
7b9f3c50 3747 return qfn->real_names[index];
9291a0cd
TT
3748}
3749
3750static struct symtab *
3751dw2_find_last_source_symtab (struct objfile *objfile)
3752{
ed2dc618
SM
3753 struct dwarf2_per_objfile *dwarf2_per_objfile
3754 = get_dwarf2_per_objfile (objfile);
b76e467d 3755 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3756 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3757
43f3e411
DE
3758 if (cust == NULL)
3759 return NULL;
ed2dc618 3760
43f3e411 3761 return compunit_primary_filetab (cust);
9291a0cd
TT
3762}
3763
7b9f3c50
DE
3764/* Traversal function for dw2_forget_cached_source_info. */
3765
3766static int
3767dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3768{
7b9f3c50 3769 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3770
7b9f3c50 3771 if (file_data->real_names)
9291a0cd 3772 {
7b9f3c50 3773 int i;
9291a0cd 3774
7b9f3c50 3775 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3776 {
7b9f3c50
DE
3777 xfree ((void*) file_data->real_names[i]);
3778 file_data->real_names[i] = NULL;
9291a0cd
TT
3779 }
3780 }
7b9f3c50
DE
3781
3782 return 1;
3783}
3784
3785static void
3786dw2_forget_cached_source_info (struct objfile *objfile)
3787{
ed2dc618
SM
3788 struct dwarf2_per_objfile *dwarf2_per_objfile
3789 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3790
3791 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3792 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3793}
3794
f8eba3c6
TT
3795/* Helper function for dw2_map_symtabs_matching_filename that expands
3796 the symtabs and calls the iterator. */
3797
3798static int
3799dw2_map_expand_apply (struct objfile *objfile,
3800 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3801 const char *name, const char *real_path,
14bc53a8 3802 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3803{
43f3e411 3804 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3805
3806 /* Don't visit already-expanded CUs. */
43f3e411 3807 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3808 return 0;
3809
3810 /* This may expand more than one symtab, and we want to iterate over
3811 all of them. */
58f0c718 3812 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3813
14bc53a8
PA
3814 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3815 last_made, callback);
f8eba3c6
TT
3816}
3817
3818/* Implementation of the map_symtabs_matching_filename method. */
3819
14bc53a8
PA
3820static bool
3821dw2_map_symtabs_matching_filename
3822 (struct objfile *objfile, const char *name, const char *real_path,
3823 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3824{
c011a4f4 3825 const char *name_basename = lbasename (name);
ed2dc618
SM
3826 struct dwarf2_per_objfile *dwarf2_per_objfile
3827 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3828
848e3e78
DE
3829 /* The rule is CUs specify all the files, including those used by
3830 any TU, so there's no need to scan TUs here. */
f4dc4d17 3831
b76e467d 3832 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3833 {
3d7bb9d9 3834 /* We only need to look at symtabs not already expanded. */
43f3e411 3835 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3836 continue;
3837
b76e467d 3838 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3839 if (file_data == NULL)
9291a0cd
TT
3840 continue;
3841
b76e467d 3842 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3843 {
7b9f3c50 3844 const char *this_name = file_data->file_names[j];
da235a7c 3845 const char *this_real_name;
9291a0cd 3846
af529f8f 3847 if (compare_filenames_for_search (this_name, name))
9291a0cd 3848 {
f5b95b50 3849 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3850 callback))
3851 return true;
288e77a7 3852 continue;
4aac40c8 3853 }
9291a0cd 3854
c011a4f4
DE
3855 /* Before we invoke realpath, which can get expensive when many
3856 files are involved, do a quick comparison of the basenames. */
3857 if (! basenames_may_differ
3858 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3859 continue;
3860
da235a7c
JK
3861 this_real_name = dw2_get_real_path (objfile, file_data, j);
3862 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3863 {
da235a7c 3864 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3865 callback))
3866 return true;
288e77a7 3867 continue;
da235a7c 3868 }
9291a0cd 3869
da235a7c
JK
3870 if (real_path != NULL)
3871 {
af529f8f
JK
3872 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3873 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3874 if (this_real_name != NULL
af529f8f 3875 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3876 {
f5b95b50 3877 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3878 callback))
3879 return true;
288e77a7 3880 continue;
9291a0cd
TT
3881 }
3882 }
3883 }
3884 }
3885
14bc53a8 3886 return false;
9291a0cd
TT
3887}
3888
da51c347
DE
3889/* Struct used to manage iterating over all CUs looking for a symbol. */
3890
3891struct dw2_symtab_iterator
9291a0cd 3892{
ed2dc618
SM
3893 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3894 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3895 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3896 int want_specific_block;
3897 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3898 Unused if !WANT_SPECIFIC_BLOCK. */
3899 int block_index;
3900 /* The kind of symbol we're looking for. */
3901 domain_enum domain;
3902 /* The list of CUs from the index entry of the symbol,
3903 or NULL if not found. */
3904 offset_type *vec;
3905 /* The next element in VEC to look at. */
3906 int next;
3907 /* The number of elements in VEC, or zero if there is no match. */
3908 int length;
8943b874
DE
3909 /* Have we seen a global version of the symbol?
3910 If so we can ignore all further global instances.
3911 This is to work around gold/15646, inefficient gold-generated
3912 indices. */
3913 int global_seen;
da51c347 3914};
9291a0cd 3915
da51c347
DE
3916/* Initialize the index symtab iterator ITER.
3917 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3918 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3919
9291a0cd 3920static void
da51c347 3921dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3922 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3923 int want_specific_block,
3924 int block_index,
3925 domain_enum domain,
3926 const char *name)
3927{
ed2dc618 3928 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3929 iter->want_specific_block = want_specific_block;
3930 iter->block_index = block_index;
3931 iter->domain = domain;
3932 iter->next = 0;
8943b874 3933 iter->global_seen = 0;
da51c347 3934
3063847f 3935 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3936
3937 /* index is NULL if OBJF_READNOW. */
3938 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3939 iter->length = MAYBE_SWAP (*iter->vec);
3940 else
3941 {
3942 iter->vec = NULL;
3943 iter->length = 0;
3944 }
3945}
3946
3947/* Return the next matching CU or NULL if there are no more. */
3948
3949static struct dwarf2_per_cu_data *
3950dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3951{
ed2dc618
SM
3952 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3953
da51c347
DE
3954 for ( ; iter->next < iter->length; ++iter->next)
3955 {
3956 offset_type cu_index_and_attrs =
3957 MAYBE_SWAP (iter->vec[iter->next + 1]);
3958 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3959 int want_static = iter->block_index != GLOBAL_BLOCK;
3960 /* This value is only valid for index versions >= 7. */
3961 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3962 gdb_index_symbol_kind symbol_kind =
3963 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3964 /* Only check the symbol attributes if they're present.
3965 Indices prior to version 7 don't record them,
3966 and indices >= 7 may elide them for certain symbols
3967 (gold does this). */
3968 int attrs_valid =
ed2dc618 3969 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3970 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3971
3190f0c6 3972 /* Don't crash on bad data. */
b76e467d 3973 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3974 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3975 {
b98664d3 3976 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3977 " [in module %s]"),
3978 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3979 continue;
3980 }
3981
ff4c9fec 3982 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3983
da51c347 3984 /* Skip if already read in. */
43f3e411 3985 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3986 continue;
3987
8943b874
DE
3988 /* Check static vs global. */
3989 if (attrs_valid)
3990 {
3991 if (iter->want_specific_block
3992 && want_static != is_static)
3993 continue;
3994 /* Work around gold/15646. */
3995 if (!is_static && iter->global_seen)
3996 continue;
3997 if (!is_static)
3998 iter->global_seen = 1;
3999 }
da51c347
DE
4000
4001 /* Only check the symbol's kind if it has one. */
4002 if (attrs_valid)
4003 {
4004 switch (iter->domain)
4005 {
4006 case VAR_DOMAIN:
4007 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4008 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4009 /* Some types are also in VAR_DOMAIN. */
4010 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4011 continue;
4012 break;
4013 case STRUCT_DOMAIN:
4014 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4015 continue;
4016 break;
4017 case LABEL_DOMAIN:
4018 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4019 continue;
4020 break;
4021 default:
4022 break;
4023 }
4024 }
4025
4026 ++iter->next;
4027 return per_cu;
4028 }
4029
4030 return NULL;
4031}
4032
43f3e411 4033static struct compunit_symtab *
da51c347
DE
4034dw2_lookup_symbol (struct objfile *objfile, int block_index,
4035 const char *name, domain_enum domain)
9291a0cd 4036{
43f3e411 4037 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4038 struct dwarf2_per_objfile *dwarf2_per_objfile
4039 = get_dwarf2_per_objfile (objfile);
9291a0cd 4040
b5ec771e
PA
4041 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4042
ed2dc618
SM
4043 struct dw2_symtab_iterator iter;
4044 struct dwarf2_per_cu_data *per_cu;
da51c347 4045
ed2dc618 4046 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4047
ed2dc618
SM
4048 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4049 {
4050 struct symbol *sym, *with_opaque = NULL;
58f0c718 4051 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4052 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4053 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4054
ed2dc618
SM
4055 sym = block_find_symbol (block, name, domain,
4056 block_find_non_opaque_type_preferred,
4057 &with_opaque);
b2e2f908 4058
ed2dc618
SM
4059 /* Some caution must be observed with overloaded functions
4060 and methods, since the index will not contain any overload
4061 information (but NAME might contain it). */
da51c347 4062
ed2dc618
SM
4063 if (sym != NULL
4064 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4065 return stab;
4066 if (with_opaque != NULL
4067 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4068 stab_best = stab;
da51c347 4069
ed2dc618 4070 /* Keep looking through other CUs. */
9291a0cd 4071 }
9291a0cd 4072
da51c347 4073 return stab_best;
9291a0cd
TT
4074}
4075
4076static void
4077dw2_print_stats (struct objfile *objfile)
4078{
ed2dc618
SM
4079 struct dwarf2_per_objfile *dwarf2_per_objfile
4080 = get_dwarf2_per_objfile (objfile);
b76e467d 4081 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4082 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4083 int count = 0;
9291a0cd 4084
ed2dc618 4085 for (int i = 0; i < total; ++i)
9291a0cd 4086 {
ff4c9fec 4087 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4088
43f3e411 4089 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4090 ++count;
4091 }
e4a48d9d 4092 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4093 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4094}
4095
779bd270
DE
4096/* This dumps minimal information about the index.
4097 It is called via "mt print objfiles".
4098 One use is to verify .gdb_index has been loaded by the
4099 gdb.dwarf2/gdb-index.exp testcase. */
4100
9291a0cd
TT
4101static void
4102dw2_dump (struct objfile *objfile)
4103{
ed2dc618
SM
4104 struct dwarf2_per_objfile *dwarf2_per_objfile
4105 = get_dwarf2_per_objfile (objfile);
4106
779bd270
DE
4107 gdb_assert (dwarf2_per_objfile->using_index);
4108 printf_filtered (".gdb_index:");
4109 if (dwarf2_per_objfile->index_table != NULL)
4110 {
4111 printf_filtered (" version %d\n",
4112 dwarf2_per_objfile->index_table->version);
4113 }
4114 else
4115 printf_filtered (" faked for \"readnow\"\n");
4116 printf_filtered ("\n");
9291a0cd
TT
4117}
4118
9291a0cd
TT
4119static void
4120dw2_expand_symtabs_for_function (struct objfile *objfile,
4121 const char *func_name)
4122{
ed2dc618
SM
4123 struct dwarf2_per_objfile *dwarf2_per_objfile
4124 = get_dwarf2_per_objfile (objfile);
da51c347 4125
ed2dc618
SM
4126 struct dw2_symtab_iterator iter;
4127 struct dwarf2_per_cu_data *per_cu;
da51c347 4128
ed2dc618
SM
4129 /* Note: It doesn't matter what we pass for block_index here. */
4130 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4131 func_name);
da51c347 4132
ed2dc618 4133 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4134 dw2_instantiate_symtab (per_cu, false);
da51c347 4135
9291a0cd
TT
4136}
4137
4138static void
4139dw2_expand_all_symtabs (struct objfile *objfile)
4140{
ed2dc618
SM
4141 struct dwarf2_per_objfile *dwarf2_per_objfile
4142 = get_dwarf2_per_objfile (objfile);
b76e467d 4143 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4144 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4145
ed2dc618 4146 for (int i = 0; i < total_units; ++i)
9291a0cd 4147 {
ff4c9fec 4148 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4149
58f0c718
TT
4150 /* We don't want to directly expand a partial CU, because if we
4151 read it with the wrong language, then assertion failures can
4152 be triggered later on. See PR symtab/23010. So, tell
4153 dw2_instantiate_symtab to skip partial CUs -- any important
4154 partial CU will be read via DW_TAG_imported_unit anyway. */
4155 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4156 }
4157}
4158
4159static void
652a8996
JK
4160dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4161 const char *fullname)
9291a0cd 4162{
ed2dc618
SM
4163 struct dwarf2_per_objfile *dwarf2_per_objfile
4164 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4165
4166 /* We don't need to consider type units here.
4167 This is only called for examining code, e.g. expand_line_sal.
4168 There can be an order of magnitude (or more) more type units
4169 than comp units, and we avoid them if we can. */
4170
b76e467d 4171 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4172 {
3d7bb9d9 4173 /* We only need to look at symtabs not already expanded. */
43f3e411 4174 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4175 continue;
4176
b76e467d 4177 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4178 if (file_data == NULL)
9291a0cd
TT
4179 continue;
4180
b76e467d 4181 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4182 {
652a8996
JK
4183 const char *this_fullname = file_data->file_names[j];
4184
4185 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4186 {
58f0c718 4187 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4188 break;
4189 }
4190 }
4191 }
4192}
4193
9291a0cd 4194static void
ade7ed9e 4195dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4196 const char * name, domain_enum domain,
ade7ed9e 4197 int global,
582942f4 4198 int (*callback) (const struct block *,
40658b94 4199 struct symbol *, void *),
b5ec771e 4200 void *data, symbol_name_match_type match,
2edb89d3 4201 symbol_compare_ftype *ordered_compare)
9291a0cd 4202{
40658b94 4203 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4204 current language is Ada for a non-Ada objfile using GNU index. As Ada
4205 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4206}
4207
b5ec771e
PA
4208/* Symbol name matcher for .gdb_index names.
4209
4210 Symbol names in .gdb_index have a few particularities:
4211
4212 - There's no indication of which is the language of each symbol.
4213
4214 Since each language has its own symbol name matching algorithm,
4215 and we don't know which language is the right one, we must match
3f563c84
PA
4216 each symbol against all languages. This would be a potential
4217 performance problem if it were not mitigated by the
4218 mapped_index::name_components lookup table, which significantly
4219 reduces the number of times we need to call into this matcher,
4220 making it a non-issue.
b5ec771e
PA
4221
4222 - Symbol names in the index have no overload (parameter)
4223 information. I.e., in C++, "foo(int)" and "foo(long)" both
4224 appear as "foo" in the index, for example.
4225
4226 This means that the lookup names passed to the symbol name
4227 matcher functions must have no parameter information either
4228 because (e.g.) symbol search name "foo" does not match
4229 lookup-name "foo(int)" [while swapping search name for lookup
4230 name would match].
4231*/
4232class gdb_index_symbol_name_matcher
4233{
4234public:
4235 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4236 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4237
4238 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4239 Returns true if any matcher matches. */
4240 bool matches (const char *symbol_name);
4241
4242private:
4243 /* A reference to the lookup name we're matching against. */
4244 const lookup_name_info &m_lookup_name;
4245
4246 /* A vector holding all the different symbol name matchers, for all
4247 languages. */
4248 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4249};
4250
4251gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4252 (const lookup_name_info &lookup_name)
4253 : m_lookup_name (lookup_name)
4254{
4255 /* Prepare the vector of comparison functions upfront, to avoid
4256 doing the same work for each symbol. Care is taken to avoid
4257 matching with the same matcher more than once if/when multiple
4258 languages use the same matcher function. */
4259 auto &matchers = m_symbol_name_matcher_funcs;
4260 matchers.reserve (nr_languages);
4261
4262 matchers.push_back (default_symbol_name_matcher);
4263
4264 for (int i = 0; i < nr_languages; i++)
4265 {
4266 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4267 symbol_name_matcher_ftype *name_matcher
618daa93 4268 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4269
4270 /* Don't insert the same comparison routine more than once.
4271 Note that we do this linear walk instead of a seemingly
4272 cheaper sorted insert, or use a std::set or something like
4273 that, because relative order of function addresses is not
4274 stable. This is not a problem in practice because the number
4275 of supported languages is low, and the cost here is tiny
4276 compared to the number of searches we'll do afterwards using
4277 this object. */
4278 if (name_matcher != default_symbol_name_matcher
4279 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4280 == matchers.end ()))
4281 matchers.push_back (name_matcher);
b5ec771e
PA
4282 }
4283}
4284
4285bool
4286gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4287{
4288 for (auto matches_name : m_symbol_name_matcher_funcs)
4289 if (matches_name (symbol_name, m_lookup_name, NULL))
4290 return true;
4291
4292 return false;
4293}
4294
e1ef7d7a
PA
4295/* Starting from a search name, return the string that finds the upper
4296 bound of all strings that start with SEARCH_NAME in a sorted name
4297 list. Returns the empty string to indicate that the upper bound is
4298 the end of the list. */
4299
4300static std::string
4301make_sort_after_prefix_name (const char *search_name)
4302{
4303 /* When looking to complete "func", we find the upper bound of all
4304 symbols that start with "func" by looking for where we'd insert
4305 the closest string that would follow "func" in lexicographical
4306 order. Usually, that's "func"-with-last-character-incremented,
4307 i.e. "fund". Mind non-ASCII characters, though. Usually those
4308 will be UTF-8 multi-byte sequences, but we can't be certain.
4309 Especially mind the 0xff character, which is a valid character in
4310 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4311 rule out compilers allowing it in identifiers. Note that
4312 conveniently, strcmp/strcasecmp are specified to compare
4313 characters interpreted as unsigned char. So what we do is treat
4314 the whole string as a base 256 number composed of a sequence of
4315 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4316 to 0, and carries 1 to the following more-significant position.
4317 If the very first character in SEARCH_NAME ends up incremented
4318 and carries/overflows, then the upper bound is the end of the
4319 list. The string after the empty string is also the empty
4320 string.
4321
4322 Some examples of this operation:
4323
4324 SEARCH_NAME => "+1" RESULT
4325
4326 "abc" => "abd"
4327 "ab\xff" => "ac"
4328 "\xff" "a" "\xff" => "\xff" "b"
4329 "\xff" => ""
4330 "\xff\xff" => ""
4331 "" => ""
4332
4333 Then, with these symbols for example:
4334
4335 func
4336 func1
4337 fund
4338
4339 completing "func" looks for symbols between "func" and
4340 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4341 which finds "func" and "func1", but not "fund".
4342
4343 And with:
4344
4345 funcÿ (Latin1 'ÿ' [0xff])
4346 funcÿ1
4347 fund
4348
4349 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4350 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4351
4352 And with:
4353
4354 ÿÿ (Latin1 'ÿ' [0xff])
4355 ÿÿ1
4356
4357 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4358 the end of the list.
4359 */
4360 std::string after = search_name;
4361 while (!after.empty () && (unsigned char) after.back () == 0xff)
4362 after.pop_back ();
4363 if (!after.empty ())
4364 after.back () = (unsigned char) after.back () + 1;
4365 return after;
4366}
4367
5c58de74 4368/* See declaration. */
61d96d7e 4369
5c58de74
PA
4370std::pair<std::vector<name_component>::const_iterator,
4371 std::vector<name_component>::const_iterator>
44ed8f3e 4372mapped_index_base::find_name_components_bounds
5c58de74 4373 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4374{
5c58de74
PA
4375 auto *name_cmp
4376 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4377
4378 const char *cplus
c62446b1 4379 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4380
3f563c84
PA
4381 /* Comparison function object for lower_bound that matches against a
4382 given symbol name. */
4383 auto lookup_compare_lower = [&] (const name_component &elem,
4384 const char *name)
4385 {
5c58de74 4386 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4387 const char *elem_name = elem_qualified + elem.name_offset;
4388 return name_cmp (elem_name, name) < 0;
4389 };
4390
4391 /* Comparison function object for upper_bound that matches against a
4392 given symbol name. */
4393 auto lookup_compare_upper = [&] (const char *name,
4394 const name_component &elem)
4395 {
5c58de74 4396 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4397 const char *elem_name = elem_qualified + elem.name_offset;
4398 return name_cmp (name, elem_name) < 0;
4399 };
4400
5c58de74
PA
4401 auto begin = this->name_components.begin ();
4402 auto end = this->name_components.end ();
3f563c84
PA
4403
4404 /* Find the lower bound. */
4405 auto lower = [&] ()
4406 {
5c58de74 4407 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4408 return begin;
4409 else
4410 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4411 } ();
4412
4413 /* Find the upper bound. */
4414 auto upper = [&] ()
4415 {
5c58de74 4416 if (lookup_name_without_params.completion_mode ())
3f563c84 4417 {
e1ef7d7a
PA
4418 /* In completion mode, we want UPPER to point past all
4419 symbols names that have the same prefix. I.e., with
4420 these symbols, and completing "func":
4421
4422 function << lower bound
4423 function1
4424 other_function << upper bound
4425
4426 We find the upper bound by looking for the insertion
4427 point of "func"-with-last-character-incremented,
4428 i.e. "fund". */
4429 std::string after = make_sort_after_prefix_name (cplus);
4430 if (after.empty ())
3f563c84 4431 return end;
e6b2f5ef
PA
4432 return std::lower_bound (lower, end, after.c_str (),
4433 lookup_compare_lower);
3f563c84
PA
4434 }
4435 else
4436 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4437 } ();
4438
5c58de74
PA
4439 return {lower, upper};
4440}
4441
4442/* See declaration. */
4443
4444void
44ed8f3e 4445mapped_index_base::build_name_components ()
5c58de74
PA
4446{
4447 if (!this->name_components.empty ())
4448 return;
4449
4450 this->name_components_casing = case_sensitivity;
4451 auto *name_cmp
4452 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4453
4454 /* The code below only knows how to break apart components of C++
4455 symbol names (and other languages that use '::' as
4456 namespace/module separator). If we add support for wild matching
4457 to some language that uses some other operator (E.g., Ada, Go and
4458 D use '.'), then we'll need to try splitting the symbol name
4459 according to that language too. Note that Ada does support wild
4460 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4461 auto count = this->symbol_name_count ();
4462 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4463 {
44ed8f3e 4464 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4465 continue;
4466
4467 const char *name = this->symbol_name_at (idx);
4468
4469 /* Add each name component to the name component table. */
4470 unsigned int previous_len = 0;
4471 for (unsigned int current_len = cp_find_first_component (name);
4472 name[current_len] != '\0';
4473 current_len += cp_find_first_component (name + current_len))
4474 {
4475 gdb_assert (name[current_len] == ':');
4476 this->name_components.push_back ({previous_len, idx});
4477 /* Skip the '::'. */
4478 current_len += 2;
4479 previous_len = current_len;
4480 }
4481 this->name_components.push_back ({previous_len, idx});
4482 }
4483
4484 /* Sort name_components elements by name. */
4485 auto name_comp_compare = [&] (const name_component &left,
4486 const name_component &right)
4487 {
4488 const char *left_qualified = this->symbol_name_at (left.idx);
4489 const char *right_qualified = this->symbol_name_at (right.idx);
4490
4491 const char *left_name = left_qualified + left.name_offset;
4492 const char *right_name = right_qualified + right.name_offset;
4493
4494 return name_cmp (left_name, right_name) < 0;
4495 };
4496
4497 std::sort (this->name_components.begin (),
4498 this->name_components.end (),
4499 name_comp_compare);
4500}
4501
4502/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4503 mapped_index_base instead of the containing objfile. This is split
4504 to a separate function in order to be able to unit test the
4505 name_components matching using a mock mapped_index_base. For each
5c58de74 4506 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4507 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4508
4509static void
4510dw2_expand_symtabs_matching_symbol
44ed8f3e 4511 (mapped_index_base &index,
5c58de74
PA
4512 const lookup_name_info &lookup_name_in,
4513 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4514 enum search_domain kind,
4515 gdb::function_view<void (offset_type)> match_callback)
4516{
4517 lookup_name_info lookup_name_without_params
4518 = lookup_name_in.make_ignore_params ();
4519 gdb_index_symbol_name_matcher lookup_name_matcher
4520 (lookup_name_without_params);
4521
4522 /* Build the symbol name component sorted vector, if we haven't
4523 yet. */
4524 index.build_name_components ();
4525
4526 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4527
3f563c84
PA
4528 /* Now for each symbol name in range, check to see if we have a name
4529 match, and if so, call the MATCH_CALLBACK callback. */
4530
4531 /* The same symbol may appear more than once in the range though.
4532 E.g., if we're looking for symbols that complete "w", and we have
4533 a symbol named "w1::w2", we'll find the two name components for
4534 that same symbol in the range. To be sure we only call the
4535 callback once per symbol, we first collect the symbol name
4536 indexes that matched in a temporary vector and ignore
4537 duplicates. */
4538 std::vector<offset_type> matches;
5c58de74 4539 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4540
5c58de74 4541 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4542 {
5c58de74 4543 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4544
4545 if (!lookup_name_matcher.matches (qualified)
4546 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4547 continue;
4548
5c58de74 4549 matches.push_back (bounds.first->idx);
3f563c84
PA
4550 }
4551
4552 std::sort (matches.begin (), matches.end ());
4553
4554 /* Finally call the callback, once per match. */
4555 ULONGEST prev = -1;
4556 for (offset_type idx : matches)
4557 {
4558 if (prev != idx)
4559 {
4560 match_callback (idx);
4561 prev = idx;
4562 }
4563 }
4564
4565 /* Above we use a type wider than idx's for 'prev', since 0 and
4566 (offset_type)-1 are both possible values. */
4567 static_assert (sizeof (prev) > sizeof (offset_type), "");
4568}
4569
c62446b1
PA
4570#if GDB_SELF_TEST
4571
4572namespace selftests { namespace dw2_expand_symtabs_matching {
4573
a3c5fafd
PA
4574/* A mock .gdb_index/.debug_names-like name index table, enough to
4575 exercise dw2_expand_symtabs_matching_symbol, which works with the
4576 mapped_index_base interface. Builds an index from the symbol list
4577 passed as parameter to the constructor. */
4578class mock_mapped_index : public mapped_index_base
c62446b1
PA
4579{
4580public:
a3c5fafd
PA
4581 mock_mapped_index (gdb::array_view<const char *> symbols)
4582 : m_symbol_table (symbols)
c62446b1
PA
4583 {}
4584
a3c5fafd 4585 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4586
a3c5fafd 4587 /* Return the number of names in the symbol table. */
632e107b 4588 size_t symbol_name_count () const override
c62446b1 4589 {
a3c5fafd 4590 return m_symbol_table.size ();
c62446b1
PA
4591 }
4592
a3c5fafd 4593 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4594 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4595 {
4596 return m_symbol_table[idx];
4597 }
c62446b1 4598
a3c5fafd
PA
4599private:
4600 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4601};
4602
4603/* Convenience function that converts a NULL pointer to a "<null>"
4604 string, to pass to print routines. */
4605
4606static const char *
4607string_or_null (const char *str)
4608{
4609 return str != NULL ? str : "<null>";
4610}
4611
4612/* Check if a lookup_name_info built from
4613 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4614 index. EXPECTED_LIST is the list of expected matches, in expected
4615 matching order. If no match expected, then an empty list is
4616 specified. Returns true on success. On failure prints a warning
4617 indicating the file:line that failed, and returns false. */
4618
4619static bool
4620check_match (const char *file, int line,
4621 mock_mapped_index &mock_index,
4622 const char *name, symbol_name_match_type match_type,
4623 bool completion_mode,
4624 std::initializer_list<const char *> expected_list)
4625{
4626 lookup_name_info lookup_name (name, match_type, completion_mode);
4627
4628 bool matched = true;
4629
4630 auto mismatch = [&] (const char *expected_str,
4631 const char *got)
4632 {
4633 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4634 "expected=\"%s\", got=\"%s\"\n"),
4635 file, line,
4636 (match_type == symbol_name_match_type::FULL
4637 ? "FULL" : "WILD"),
4638 name, string_or_null (expected_str), string_or_null (got));
4639 matched = false;
4640 };
4641
4642 auto expected_it = expected_list.begin ();
4643 auto expected_end = expected_list.end ();
4644
a3c5fafd 4645 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4646 NULL, ALL_DOMAIN,
4647 [&] (offset_type idx)
4648 {
a3c5fafd 4649 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4650 const char *expected_str
4651 = expected_it == expected_end ? NULL : *expected_it++;
4652
4653 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4654 mismatch (expected_str, matched_name);
4655 });
4656
4657 const char *expected_str
4658 = expected_it == expected_end ? NULL : *expected_it++;
4659 if (expected_str != NULL)
4660 mismatch (expected_str, NULL);
4661
4662 return matched;
4663}
4664
4665/* The symbols added to the mock mapped_index for testing (in
4666 canonical form). */
4667static const char *test_symbols[] = {
4668 "function",
4669 "std::bar",
4670 "std::zfunction",
4671 "std::zfunction2",
4672 "w1::w2",
4673 "ns::foo<char*>",
4674 "ns::foo<int>",
4675 "ns::foo<long>",
a20714ff
PA
4676 "ns2::tmpl<int>::foo2",
4677 "(anonymous namespace)::A::B::C",
c62446b1 4678
e1ef7d7a
PA
4679 /* These are used to check that the increment-last-char in the
4680 matching algorithm for completion doesn't match "t1_fund" when
4681 completing "t1_func". */
4682 "t1_func",
4683 "t1_func1",
4684 "t1_fund",
4685 "t1_fund1",
4686
4687 /* A UTF-8 name with multi-byte sequences to make sure that
4688 cp-name-parser understands this as a single identifier ("função"
4689 is "function" in PT). */
4690 u8"u8função",
4691
4692 /* \377 (0xff) is Latin1 'ÿ'. */
4693 "yfunc\377",
4694
4695 /* \377 (0xff) is Latin1 'ÿ'. */
4696 "\377",
4697 "\377\377123",
4698
c62446b1
PA
4699 /* A name with all sorts of complications. Starts with "z" to make
4700 it easier for the completion tests below. */
4701#define Z_SYM_NAME \
4702 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4703 "::tuple<(anonymous namespace)::ui*, " \
4704 "std::default_delete<(anonymous namespace)::ui>, void>"
4705
4706 Z_SYM_NAME
4707};
4708
a3c5fafd
PA
4709/* Returns true if the mapped_index_base::find_name_component_bounds
4710 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4711 in completion mode. */
5c58de74
PA
4712
4713static bool
a3c5fafd 4714check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4715 const char *search_name,
4716 gdb::array_view<const char *> expected_syms)
4717{
4718 lookup_name_info lookup_name (search_name,
4719 symbol_name_match_type::FULL, true);
4720
4721 auto bounds = index.find_name_components_bounds (lookup_name);
4722
4723 size_t distance = std::distance (bounds.first, bounds.second);
4724 if (distance != expected_syms.size ())
4725 return false;
4726
4727 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4728 {
4729 auto nc_elem = bounds.first + exp_elem;
4730 const char *qualified = index.symbol_name_at (nc_elem->idx);
4731 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4732 return false;
4733 }
4734
4735 return true;
4736}
4737
4738/* Test the lower-level mapped_index::find_name_component_bounds
4739 method. */
4740
c62446b1 4741static void
5c58de74
PA
4742test_mapped_index_find_name_component_bounds ()
4743{
4744 mock_mapped_index mock_index (test_symbols);
4745
a3c5fafd 4746 mock_index.build_name_components ();
5c58de74
PA
4747
4748 /* Test the lower-level mapped_index::find_name_component_bounds
4749 method in completion mode. */
4750 {
4751 static const char *expected_syms[] = {
4752 "t1_func",
4753 "t1_func1",
5c58de74
PA
4754 };
4755
a3c5fafd 4756 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4757 "t1_func", expected_syms));
4758 }
4759
4760 /* Check that the increment-last-char in the name matching algorithm
4761 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4762 {
4763 static const char *expected_syms1[] = {
4764 "\377",
4765 "\377\377123",
4766 };
a3c5fafd 4767 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4768 "\377", expected_syms1));
4769
4770 static const char *expected_syms2[] = {
4771 "\377\377123",
4772 };
a3c5fafd 4773 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4774 "\377\377", expected_syms2));
4775 }
4776}
4777
4778/* Test dw2_expand_symtabs_matching_symbol. */
4779
4780static void
4781test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4782{
4783 mock_mapped_index mock_index (test_symbols);
4784
4785 /* We let all tests run until the end even if some fails, for debug
4786 convenience. */
4787 bool any_mismatch = false;
4788
4789 /* Create the expected symbols list (an initializer_list). Needed
4790 because lists have commas, and we need to pass them to CHECK,
4791 which is a macro. */
4792#define EXPECT(...) { __VA_ARGS__ }
4793
4794 /* Wrapper for check_match that passes down the current
4795 __FILE__/__LINE__. */
4796#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4797 any_mismatch |= !check_match (__FILE__, __LINE__, \
4798 mock_index, \
4799 NAME, MATCH_TYPE, COMPLETION_MODE, \
4800 EXPECTED_LIST)
4801
4802 /* Identity checks. */
4803 for (const char *sym : test_symbols)
4804 {
4805 /* Should be able to match all existing symbols. */
4806 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4807 EXPECT (sym));
4808
4809 /* Should be able to match all existing symbols with
4810 parameters. */
4811 std::string with_params = std::string (sym) + "(int)";
4812 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4813 EXPECT (sym));
4814
4815 /* Should be able to match all existing symbols with
4816 parameters and qualifiers. */
4817 with_params = std::string (sym) + " ( int ) const";
4818 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4819 EXPECT (sym));
4820
4821 /* This should really find sym, but cp-name-parser.y doesn't
4822 know about lvalue/rvalue qualifiers yet. */
4823 with_params = std::string (sym) + " ( int ) &&";
4824 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4825 {});
4826 }
4827
e1ef7d7a
PA
4828 /* Check that the name matching algorithm for completion doesn't get
4829 confused with Latin1 'ÿ' / 0xff. */
4830 {
4831 static const char str[] = "\377";
4832 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4833 EXPECT ("\377", "\377\377123"));
4834 }
4835
4836 /* Check that the increment-last-char in the matching algorithm for
4837 completion doesn't match "t1_fund" when completing "t1_func". */
4838 {
4839 static const char str[] = "t1_func";
4840 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4841 EXPECT ("t1_func", "t1_func1"));
4842 }
4843
c62446b1
PA
4844 /* Check that completion mode works at each prefix of the expected
4845 symbol name. */
4846 {
4847 static const char str[] = "function(int)";
4848 size_t len = strlen (str);
4849 std::string lookup;
4850
4851 for (size_t i = 1; i < len; i++)
4852 {
4853 lookup.assign (str, i);
4854 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4855 EXPECT ("function"));
4856 }
4857 }
4858
4859 /* While "w" is a prefix of both components, the match function
4860 should still only be called once. */
4861 {
4862 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4863 EXPECT ("w1::w2"));
a20714ff
PA
4864 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4865 EXPECT ("w1::w2"));
c62446b1
PA
4866 }
4867
4868 /* Same, with a "complicated" symbol. */
4869 {
4870 static const char str[] = Z_SYM_NAME;
4871 size_t len = strlen (str);
4872 std::string lookup;
4873
4874 for (size_t i = 1; i < len; i++)
4875 {
4876 lookup.assign (str, i);
4877 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4878 EXPECT (Z_SYM_NAME));
4879 }
4880 }
4881
4882 /* In FULL mode, an incomplete symbol doesn't match. */
4883 {
4884 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4885 {});
4886 }
4887
4888 /* A complete symbol with parameters matches any overload, since the
4889 index has no overload info. */
4890 {
4891 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4892 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4893 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4894 EXPECT ("std::zfunction", "std::zfunction2"));
4895 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4896 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4897 }
4898
4899 /* Check that whitespace is ignored appropriately. A symbol with a
4900 template argument list. */
4901 {
4902 static const char expected[] = "ns::foo<int>";
4903 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4904 EXPECT (expected));
a20714ff
PA
4905 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4906 EXPECT (expected));
c62446b1
PA
4907 }
4908
4909 /* Check that whitespace is ignored appropriately. A symbol with a
4910 template argument list that includes a pointer. */
4911 {
4912 static const char expected[] = "ns::foo<char*>";
4913 /* Try both completion and non-completion modes. */
4914 static const bool completion_mode[2] = {false, true};
4915 for (size_t i = 0; i < 2; i++)
4916 {
4917 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4918 completion_mode[i], EXPECT (expected));
a20714ff
PA
4919 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4920 completion_mode[i], EXPECT (expected));
c62446b1
PA
4921
4922 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4923 completion_mode[i], EXPECT (expected));
a20714ff
PA
4924 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4925 completion_mode[i], EXPECT (expected));
c62446b1
PA
4926 }
4927 }
4928
4929 {
4930 /* Check method qualifiers are ignored. */
4931 static const char expected[] = "ns::foo<char*>";
4932 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4933 symbol_name_match_type::FULL, true, EXPECT (expected));
4934 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4935 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4936 CHECK_MATCH ("foo < char * > ( int ) const",
4937 symbol_name_match_type::WILD, true, EXPECT (expected));
4938 CHECK_MATCH ("foo < char * > ( int ) &&",
4939 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4940 }
4941
4942 /* Test lookup names that don't match anything. */
4943 {
a20714ff
PA
4944 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4945 {});
4946
c62446b1
PA
4947 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4948 {});
4949 }
4950
a20714ff
PA
4951 /* Some wild matching tests, exercising "(anonymous namespace)",
4952 which should not be confused with a parameter list. */
4953 {
4954 static const char *syms[] = {
4955 "A::B::C",
4956 "B::C",
4957 "C",
4958 "A :: B :: C ( int )",
4959 "B :: C ( int )",
4960 "C ( int )",
4961 };
4962
4963 for (const char *s : syms)
4964 {
4965 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4966 EXPECT ("(anonymous namespace)::A::B::C"));
4967 }
4968 }
4969
4970 {
4971 static const char expected[] = "ns2::tmpl<int>::foo2";
4972 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4973 EXPECT (expected));
4974 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4975 EXPECT (expected));
4976 }
4977
c62446b1
PA
4978 SELF_CHECK (!any_mismatch);
4979
4980#undef EXPECT
4981#undef CHECK_MATCH
4982}
4983
5c58de74
PA
4984static void
4985run_test ()
4986{
4987 test_mapped_index_find_name_component_bounds ();
4988 test_dw2_expand_symtabs_matching_symbol ();
4989}
4990
c62446b1
PA
4991}} // namespace selftests::dw2_expand_symtabs_matching
4992
4993#endif /* GDB_SELF_TEST */
4994
4b514bc8
JK
4995/* If FILE_MATCHER is NULL or if PER_CU has
4996 dwarf2_per_cu_quick_data::MARK set (see
4997 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4998 EXPANSION_NOTIFY on it. */
4999
5000static void
5001dw2_expand_symtabs_matching_one
5002 (struct dwarf2_per_cu_data *per_cu,
5003 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5004 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5005{
5006 if (file_matcher == NULL || per_cu->v.quick->mark)
5007 {
5008 bool symtab_was_null
5009 = (per_cu->v.quick->compunit_symtab == NULL);
5010
58f0c718 5011 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5012
5013 if (expansion_notify != NULL
5014 && symtab_was_null
5015 && per_cu->v.quick->compunit_symtab != NULL)
5016 expansion_notify (per_cu->v.quick->compunit_symtab);
5017 }
5018}
5019
3f563c84
PA
5020/* Helper for dw2_expand_matching symtabs. Called on each symbol
5021 matched, to expand corresponding CUs that were marked. IDX is the
5022 index of the symbol name that matched. */
5023
5024static void
5025dw2_expand_marked_cus
ed2dc618 5026 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5027 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5028 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5029 search_domain kind)
5030{
3f563c84
PA
5031 offset_type *vec, vec_len, vec_idx;
5032 bool global_seen = false;
ed2dc618 5033 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5034
61920122 5035 vec = (offset_type *) (index.constant_pool
f00a2de2 5036 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5037 vec_len = MAYBE_SWAP (vec[0]);
5038 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5039 {
61920122
PA
5040 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5041 /* This value is only valid for index versions >= 7. */
5042 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5043 gdb_index_symbol_kind symbol_kind =
5044 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5045 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5046 /* Only check the symbol attributes if they're present.
5047 Indices prior to version 7 don't record them,
5048 and indices >= 7 may elide them for certain symbols
5049 (gold does this). */
5050 int attrs_valid =
5051 (index.version >= 7
5052 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5053
5054 /* Work around gold/15646. */
5055 if (attrs_valid)
9291a0cd 5056 {
61920122
PA
5057 if (!is_static && global_seen)
5058 continue;
5059 if (!is_static)
5060 global_seen = true;
5061 }
3190f0c6 5062
61920122
PA
5063 /* Only check the symbol's kind if it has one. */
5064 if (attrs_valid)
5065 {
5066 switch (kind)
8943b874 5067 {
61920122
PA
5068 case VARIABLES_DOMAIN:
5069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5070 continue;
5071 break;
5072 case FUNCTIONS_DOMAIN:
5073 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5074 continue;
61920122
PA
5075 break;
5076 case TYPES_DOMAIN:
5077 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5078 continue;
5079 break;
5080 default:
5081 break;
8943b874 5082 }
61920122 5083 }
8943b874 5084
61920122 5085 /* Don't crash on bad data. */
b76e467d 5086 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5087 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5088 {
b98664d3 5089 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5090 " [in module %s]"),
5091 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5092 continue;
5093 }
5094
ff4c9fec 5095 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5096 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5097 expansion_notify);
61920122
PA
5098 }
5099}
5100
4b514bc8
JK
5101/* If FILE_MATCHER is non-NULL, set all the
5102 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5103 that match FILE_MATCHER. */
5104
61920122 5105static void
4b514bc8 5106dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5107 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5108 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5109{
4b514bc8 5110 if (file_matcher == NULL)
61920122
PA
5111 return;
5112
4b514bc8
JK
5113 objfile *const objfile = dwarf2_per_objfile->objfile;
5114
5115 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5116 htab_eq_pointer,
5117 NULL, xcalloc, xfree));
5118 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5119 htab_eq_pointer,
5120 NULL, xcalloc, xfree));
61920122 5121
4b514bc8
JK
5122 /* The rule is CUs specify all the files, including those used by
5123 any TU, so there's no need to scan TUs here. */
61920122 5124
b76e467d 5125 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5126 {
927aa2e7
JK
5127 QUIT;
5128
5129 per_cu->v.quick->mark = 0;
5130
5131 /* We only need to look at symtabs not already expanded. */
5132 if (per_cu->v.quick->compunit_symtab)
5133 continue;
5134
b76e467d 5135 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5136 if (file_data == NULL)
5137 continue;
5138
5139 if (htab_find (visited_not_found.get (), file_data) != NULL)
5140 continue;
5141 else if (htab_find (visited_found.get (), file_data) != NULL)
5142 {
5143 per_cu->v.quick->mark = 1;
5144 continue;
5145 }
5146
b76e467d 5147 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5148 {
5149 const char *this_real_name;
5150
5151 if (file_matcher (file_data->file_names[j], false))
5152 {
5153 per_cu->v.quick->mark = 1;
5154 break;
5155 }
5156
5157 /* Before we invoke realpath, which can get expensive when many
5158 files are involved, do a quick comparison of the basenames. */
5159 if (!basenames_may_differ
5160 && !file_matcher (lbasename (file_data->file_names[j]),
5161 true))
5162 continue;
5163
5164 this_real_name = dw2_get_real_path (objfile, file_data, j);
5165 if (file_matcher (this_real_name, false))
5166 {
5167 per_cu->v.quick->mark = 1;
5168 break;
5169 }
5170 }
5171
b76e467d
SM
5172 void **slot = htab_find_slot (per_cu->v.quick->mark
5173 ? visited_found.get ()
5174 : visited_not_found.get (),
5175 file_data, INSERT);
927aa2e7
JK
5176 *slot = file_data;
5177 }
5178}
5179
5180static void
5181dw2_expand_symtabs_matching
5182 (struct objfile *objfile,
5183 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5184 const lookup_name_info &lookup_name,
5185 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5186 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5187 enum search_domain kind)
5188{
ed2dc618
SM
5189 struct dwarf2_per_objfile *dwarf2_per_objfile
5190 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5191
5192 /* index_table is NULL if OBJF_READNOW. */
5193 if (!dwarf2_per_objfile->index_table)
5194 return;
5195
ed2dc618 5196 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5197
5198 mapped_index &index = *dwarf2_per_objfile->index_table;
5199
5200 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5201 symbol_matcher,
5202 kind, [&] (offset_type idx)
5203 {
ed2dc618 5204 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5205 expansion_notify, kind);
5206 });
5207}
5208
5209/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5210 symtab. */
5211
5212static struct compunit_symtab *
5213recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5214 CORE_ADDR pc)
5215{
5216 int i;
5217
5218 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5219 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5220 return cust;
5221
5222 if (cust->includes == NULL)
5223 return NULL;
5224
5225 for (i = 0; cust->includes[i]; ++i)
5226 {
5227 struct compunit_symtab *s = cust->includes[i];
5228
5229 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5230 if (s != NULL)
5231 return s;
5232 }
5233
5234 return NULL;
5235}
5236
5237static struct compunit_symtab *
5238dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5239 struct bound_minimal_symbol msymbol,
5240 CORE_ADDR pc,
5241 struct obj_section *section,
5242 int warn_if_readin)
5243{
5244 struct dwarf2_per_cu_data *data;
5245 struct compunit_symtab *result;
5246
d320c2b5 5247 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5248 return NULL;
5249
79748972
TT
5250 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5251 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5252 data = (struct dwarf2_per_cu_data *) addrmap_find
5253 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5254 if (!data)
5255 return NULL;
5256
5257 if (warn_if_readin && data->v.quick->compunit_symtab)
5258 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5259 paddress (get_objfile_arch (objfile), pc));
5260
5261 result
58f0c718
TT
5262 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5263 false),
927aa2e7
JK
5264 pc);
5265 gdb_assert (result != NULL);
5266 return result;
5267}
5268
5269static void
5270dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5271 void *data, int need_fullname)
5272{
ed2dc618
SM
5273 struct dwarf2_per_objfile *dwarf2_per_objfile
5274 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5275
5276 if (!dwarf2_per_objfile->filenames_cache)
5277 {
5278 dwarf2_per_objfile->filenames_cache.emplace ();
5279
5280 htab_up visited (htab_create_alloc (10,
5281 htab_hash_pointer, htab_eq_pointer,
5282 NULL, xcalloc, xfree));
5283
5284 /* The rule is CUs specify all the files, including those used
5285 by any TU, so there's no need to scan TUs here. We can
5286 ignore file names coming from already-expanded CUs. */
5287
b76e467d 5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5289 {
927aa2e7
JK
5290 if (per_cu->v.quick->compunit_symtab)
5291 {
5292 void **slot = htab_find_slot (visited.get (),
5293 per_cu->v.quick->file_names,
5294 INSERT);
5295
5296 *slot = per_cu->v.quick->file_names;
5297 }
5298 }
5299
b76e467d 5300 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5301 {
927aa2e7
JK
5302 /* We only need to look at symtabs not already expanded. */
5303 if (per_cu->v.quick->compunit_symtab)
5304 continue;
5305
b76e467d 5306 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5307 if (file_data == NULL)
5308 continue;
5309
b76e467d 5310 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5311 if (*slot)
5312 {
5313 /* Already visited. */
5314 continue;
5315 }
5316 *slot = file_data;
5317
5318 for (int j = 0; j < file_data->num_file_names; ++j)
5319 {
5320 const char *filename = file_data->file_names[j];
5321 dwarf2_per_objfile->filenames_cache->seen (filename);
5322 }
5323 }
5324 }
5325
5326 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5327 {
5328 gdb::unique_xmalloc_ptr<char> this_real_name;
5329
5330 if (need_fullname)
5331 this_real_name = gdb_realpath (filename);
5332 (*fun) (filename, this_real_name.get (), data);
5333 });
5334}
5335
5336static int
5337dw2_has_symbols (struct objfile *objfile)
5338{
5339 return 1;
5340}
5341
5342const struct quick_symbol_functions dwarf2_gdb_index_functions =
5343{
5344 dw2_has_symbols,
5345 dw2_find_last_source_symtab,
5346 dw2_forget_cached_source_info,
5347 dw2_map_symtabs_matching_filename,
5348 dw2_lookup_symbol,
5349 dw2_print_stats,
5350 dw2_dump,
927aa2e7
JK
5351 dw2_expand_symtabs_for_function,
5352 dw2_expand_all_symtabs,
5353 dw2_expand_symtabs_with_fullname,
5354 dw2_map_matching_symbols,
5355 dw2_expand_symtabs_matching,
5356 dw2_find_pc_sect_compunit_symtab,
5357 NULL,
5358 dw2_map_symbol_filenames
5359};
5360
5361/* DWARF-5 debug_names reader. */
5362
5363/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5364static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5365
5366/* A helper function that reads the .debug_names section in SECTION
5367 and fills in MAP. FILENAME is the name of the file containing the
5368 section; it is used for error reporting.
5369
5370 Returns true if all went well, false otherwise. */
5371
5372static bool
5373read_debug_names_from_section (struct objfile *objfile,
5374 const char *filename,
5375 struct dwarf2_section_info *section,
5376 mapped_debug_names &map)
5377{
5378 if (dwarf2_section_empty_p (section))
5379 return false;
5380
5381 /* Older elfutils strip versions could keep the section in the main
5382 executable while splitting it for the separate debug info file. */
5383 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5384 return false;
5385
5386 dwarf2_read_section (objfile, section);
5387
5388 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5389
5390 const gdb_byte *addr = section->buffer;
5391
5392 bfd *const abfd = get_section_bfd_owner (section);
5393
5394 unsigned int bytes_read;
5395 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5396 addr += bytes_read;
5397
5398 map.dwarf5_is_dwarf64 = bytes_read != 4;
5399 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5400 if (bytes_read + length != section->size)
5401 {
5402 /* There may be multiple per-CU indices. */
5403 warning (_("Section .debug_names in %s length %s does not match "
5404 "section length %s, ignoring .debug_names."),
5405 filename, plongest (bytes_read + length),
5406 pulongest (section->size));
5407 return false;
5408 }
5409
5410 /* The version number. */
5411 uint16_t version = read_2_bytes (abfd, addr);
5412 addr += 2;
5413 if (version != 5)
5414 {
5415 warning (_("Section .debug_names in %s has unsupported version %d, "
5416 "ignoring .debug_names."),
5417 filename, version);
5418 return false;
5419 }
5420
5421 /* Padding. */
5422 uint16_t padding = read_2_bytes (abfd, addr);
5423 addr += 2;
5424 if (padding != 0)
5425 {
5426 warning (_("Section .debug_names in %s has unsupported padding %d, "
5427 "ignoring .debug_names."),
5428 filename, padding);
5429 return false;
5430 }
5431
5432 /* comp_unit_count - The number of CUs in the CU list. */
5433 map.cu_count = read_4_bytes (abfd, addr);
5434 addr += 4;
5435
5436 /* local_type_unit_count - The number of TUs in the local TU
5437 list. */
5438 map.tu_count = read_4_bytes (abfd, addr);
5439 addr += 4;
5440
5441 /* foreign_type_unit_count - The number of TUs in the foreign TU
5442 list. */
5443 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445 if (foreign_tu_count != 0)
5446 {
5447 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5448 "ignoring .debug_names."),
5449 filename, static_cast<unsigned long> (foreign_tu_count));
5450 return false;
5451 }
5452
5453 /* bucket_count - The number of hash buckets in the hash lookup
5454 table. */
5455 map.bucket_count = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* name_count - The number of unique names in the index. */
5459 map.name_count = read_4_bytes (abfd, addr);
5460 addr += 4;
5461
5462 /* abbrev_table_size - The size in bytes of the abbreviations
5463 table. */
5464 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5465 addr += 4;
5466
5467 /* augmentation_string_size - The size in bytes of the augmentation
5468 string. This value is rounded up to a multiple of 4. */
5469 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5470 addr += 4;
5471 map.augmentation_is_gdb = ((augmentation_string_size
5472 == sizeof (dwarf5_augmentation))
5473 && memcmp (addr, dwarf5_augmentation,
5474 sizeof (dwarf5_augmentation)) == 0);
5475 augmentation_string_size += (-augmentation_string_size) & 3;
5476 addr += augmentation_string_size;
5477
5478 /* List of CUs */
5479 map.cu_table_reordered = addr;
5480 addr += map.cu_count * map.offset_size;
5481
5482 /* List of Local TUs */
5483 map.tu_table_reordered = addr;
5484 addr += map.tu_count * map.offset_size;
5485
5486 /* Hash Lookup Table */
5487 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5488 addr += map.bucket_count * 4;
5489 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5490 addr += map.name_count * 4;
5491
5492 /* Name Table */
5493 map.name_table_string_offs_reordered = addr;
5494 addr += map.name_count * map.offset_size;
5495 map.name_table_entry_offs_reordered = addr;
5496 addr += map.name_count * map.offset_size;
5497
5498 const gdb_byte *abbrev_table_start = addr;
5499 for (;;)
5500 {
927aa2e7
JK
5501 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503 if (index_num == 0)
5504 break;
5505
5506 const auto insertpair
5507 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5508 if (!insertpair.second)
5509 {
5510 warning (_("Section .debug_names in %s has duplicate index %s, "
5511 "ignoring .debug_names."),
5512 filename, pulongest (index_num));
5513 return false;
5514 }
5515 mapped_debug_names::index_val &indexval = insertpair.first->second;
5516 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518
5519 for (;;)
5520 {
5521 mapped_debug_names::index_val::attr attr;
5522 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5523 addr += bytes_read;
5524 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5525 addr += bytes_read;
5526 if (attr.form == DW_FORM_implicit_const)
5527 {
5528 attr.implicit_const = read_signed_leb128 (abfd, addr,
5529 &bytes_read);
5530 addr += bytes_read;
5531 }
5532 if (attr.dw_idx == 0 && attr.form == 0)
5533 break;
5534 indexval.attr_vec.push_back (std::move (attr));
5535 }
5536 }
5537 if (addr != abbrev_table_start + abbrev_table_size)
5538 {
5539 warning (_("Section .debug_names in %s has abbreviation_table "
5540 "of size %zu vs. written as %u, ignoring .debug_names."),
5541 filename, addr - abbrev_table_start, abbrev_table_size);
5542 return false;
5543 }
5544 map.entry_pool = addr;
5545
5546 return true;
5547}
5548
5549/* A helper for create_cus_from_debug_names that handles the MAP's CU
5550 list. */
5551
5552static void
ed2dc618 5553create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5554 const mapped_debug_names &map,
5555 dwarf2_section_info &section,
b76e467d 5556 bool is_dwz)
927aa2e7
JK
5557{
5558 sect_offset sect_off_prev;
5559 for (uint32_t i = 0; i <= map.cu_count; ++i)
5560 {
5561 sect_offset sect_off_next;
5562 if (i < map.cu_count)
5563 {
5564 sect_off_next
5565 = (sect_offset) (extract_unsigned_integer
5566 (map.cu_table_reordered + i * map.offset_size,
5567 map.offset_size,
5568 map.dwarf5_byte_order));
5569 }
5570 else
5571 sect_off_next = (sect_offset) section.size;
5572 if (i >= 1)
5573 {
5574 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5575 dwarf2_per_cu_data *per_cu
ed2dc618 5576 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5577 sect_off_prev, length);
b76e467d 5578 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5579 }
5580 sect_off_prev = sect_off_next;
5581 }
5582}
5583
5584/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5585 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5586
5587static void
ed2dc618 5588create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5589 const mapped_debug_names &map,
5590 const mapped_debug_names &dwz_map)
5591{
b76e467d
SM
5592 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5593 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5594
ed2dc618
SM
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5596 dwarf2_per_objfile->info,
b76e467d 5597 false /* is_dwz */);
927aa2e7
JK
5598
5599 if (dwz_map.cu_count == 0)
5600 return;
5601
ed2dc618
SM
5602 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5603 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5604 true /* is_dwz */);
927aa2e7
JK
5605}
5606
5607/* Read .debug_names. If everything went ok, initialize the "quick"
5608 elements of all the CUs and return true. Otherwise, return false. */
5609
5610static bool
ed2dc618 5611dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5612{
22ca247e
TT
5613 std::unique_ptr<mapped_debug_names> map
5614 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5615 mapped_debug_names dwz_map (dwarf2_per_objfile);
5616 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5617
5618 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5619 &dwarf2_per_objfile->debug_names,
22ca247e 5620 *map))
927aa2e7
JK
5621 return false;
5622
5623 /* Don't use the index if it's empty. */
22ca247e 5624 if (map->name_count == 0)
927aa2e7
JK
5625 return false;
5626
5627 /* If there is a .dwz file, read it so we can get its CU list as
5628 well. */
ed2dc618 5629 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5630 if (dwz != NULL)
5631 {
5632 if (!read_debug_names_from_section (objfile,
5633 bfd_get_filename (dwz->dwz_bfd),
5634 &dwz->debug_names, dwz_map))
5635 {
5636 warning (_("could not read '.debug_names' section from %s; skipping"),
5637 bfd_get_filename (dwz->dwz_bfd));
5638 return false;
5639 }
5640 }
5641
22ca247e 5642 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5643
22ca247e 5644 if (map->tu_count != 0)
927aa2e7
JK
5645 {
5646 /* We can only handle a single .debug_types when we have an
5647 index. */
5648 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5649 return false;
5650
5651 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5652 dwarf2_per_objfile->types, 0);
5653
5654 create_signatured_type_table_from_debug_names
22ca247e 5655 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5656 }
5657
ed2dc618
SM
5658 create_addrmap_from_aranges (dwarf2_per_objfile,
5659 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5660
22ca247e 5661 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5662 dwarf2_per_objfile->using_index = 1;
5663 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5664 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5665
5666 return true;
5667}
5668
927aa2e7
JK
5669/* Type used to manage iterating over all CUs looking for a symbol for
5670 .debug_names. */
5671
5672class dw2_debug_names_iterator
5673{
5674public:
5675 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5676 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5677 dw2_debug_names_iterator (const mapped_debug_names &map,
5678 bool want_specific_block,
5679 block_enum block_index, domain_enum domain,
5680 const char *name)
5681 : m_map (map), m_want_specific_block (want_specific_block),
5682 m_block_index (block_index), m_domain (domain),
5683 m_addr (find_vec_in_debug_names (map, name))
5684 {}
5685
5686 dw2_debug_names_iterator (const mapped_debug_names &map,
5687 search_domain search, uint32_t namei)
5688 : m_map (map),
5689 m_search (search),
5690 m_addr (find_vec_in_debug_names (map, namei))
5691 {}
5692
5693 /* Return the next matching CU or NULL if there are no more. */
5694 dwarf2_per_cu_data *next ();
5695
5696private:
5697 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5698 const char *name);
5699 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5700 uint32_t namei);
5701
5702 /* The internalized form of .debug_names. */
5703 const mapped_debug_names &m_map;
5704
5705 /* If true, only look for symbols that match BLOCK_INDEX. */
5706 const bool m_want_specific_block = false;
5707
5708 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5709 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5710 value. */
5711 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5712
5713 /* The kind of symbol we're looking for. */
5714 const domain_enum m_domain = UNDEF_DOMAIN;
5715 const search_domain m_search = ALL_DOMAIN;
5716
5717 /* The list of CUs from the index entry of the symbol, or NULL if
5718 not found. */
5719 const gdb_byte *m_addr;
5720};
5721
5722const char *
5723mapped_debug_names::namei_to_name (uint32_t namei) const
5724{
5725 const ULONGEST namei_string_offs
5726 = extract_unsigned_integer ((name_table_string_offs_reordered
5727 + namei * offset_size),
5728 offset_size,
5729 dwarf5_byte_order);
5730 return read_indirect_string_at_offset
ed2dc618 5731 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5732}
5733
5734/* Find a slot in .debug_names for the object named NAME. If NAME is
5735 found, return pointer to its pool data. If NAME cannot be found,
5736 return NULL. */
5737
5738const gdb_byte *
5739dw2_debug_names_iterator::find_vec_in_debug_names
5740 (const mapped_debug_names &map, const char *name)
5741{
5742 int (*cmp) (const char *, const char *);
5743
5744 if (current_language->la_language == language_cplus
5745 || current_language->la_language == language_fortran
5746 || current_language->la_language == language_d)
5747 {
5748 /* NAME is already canonical. Drop any qualifiers as
5749 .debug_names does not contain any. */
5750
5751 if (strchr (name, '(') != NULL)
5752 {
5753 gdb::unique_xmalloc_ptr<char> without_params
5754 = cp_remove_params (name);
5755
5756 if (without_params != NULL)
5757 {
5758 name = without_params.get();
5759 }
5760 }
5761 }
5762
5763 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5764
5765 const uint32_t full_hash = dwarf5_djb_hash (name);
5766 uint32_t namei
5767 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5768 (map.bucket_table_reordered
5769 + (full_hash % map.bucket_count)), 4,
5770 map.dwarf5_byte_order);
5771 if (namei == 0)
5772 return NULL;
5773 --namei;
5774 if (namei >= map.name_count)
5775 {
b98664d3 5776 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5777 "[in module %s]"),
5778 namei, map.name_count,
ed2dc618 5779 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5780 return NULL;
5781 }
5782
5783 for (;;)
5784 {
5785 const uint32_t namei_full_hash
5786 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5787 (map.hash_table_reordered + namei), 4,
5788 map.dwarf5_byte_order);
5789 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5790 return NULL;
5791
5792 if (full_hash == namei_full_hash)
5793 {
5794 const char *const namei_string = map.namei_to_name (namei);
5795
5796#if 0 /* An expensive sanity check. */
5797 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5798 {
b98664d3 5799 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5800 "[in module %s]"),
5801 namei, objfile_name (dwarf2_per_objfile->objfile));
5802 return NULL;
5803 }
5804#endif
5805
5806 if (cmp (namei_string, name) == 0)
5807 {
5808 const ULONGEST namei_entry_offs
5809 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5810 + namei * map.offset_size),
5811 map.offset_size, map.dwarf5_byte_order);
5812 return map.entry_pool + namei_entry_offs;
5813 }
5814 }
5815
5816 ++namei;
5817 if (namei >= map.name_count)
5818 return NULL;
5819 }
5820}
5821
5822const gdb_byte *
5823dw2_debug_names_iterator::find_vec_in_debug_names
5824 (const mapped_debug_names &map, uint32_t namei)
5825{
5826 if (namei >= map.name_count)
5827 {
b98664d3 5828 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5829 "[in module %s]"),
5830 namei, map.name_count,
ed2dc618 5831 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5832 return NULL;
5833 }
5834
5835 const ULONGEST namei_entry_offs
5836 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5837 + namei * map.offset_size),
5838 map.offset_size, map.dwarf5_byte_order);
5839 return map.entry_pool + namei_entry_offs;
5840}
5841
5842/* See dw2_debug_names_iterator. */
5843
5844dwarf2_per_cu_data *
5845dw2_debug_names_iterator::next ()
5846{
5847 if (m_addr == NULL)
5848 return NULL;
5849
ed2dc618
SM
5850 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5851 struct objfile *objfile = dwarf2_per_objfile->objfile;
5852 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5853
5854 again:
5855
5856 unsigned int bytes_read;
5857 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5858 m_addr += bytes_read;
5859 if (abbrev == 0)
5860 return NULL;
5861
5862 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5863 if (indexval_it == m_map.abbrev_map.cend ())
5864 {
b98664d3 5865 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5866 "[in module %s]"),
ed2dc618 5867 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5868 return NULL;
5869 }
5870 const mapped_debug_names::index_val &indexval = indexval_it->second;
5871 bool have_is_static = false;
5872 bool is_static;
5873 dwarf2_per_cu_data *per_cu = NULL;
5874 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5875 {
5876 ULONGEST ull;
5877 switch (attr.form)
5878 {
5879 case DW_FORM_implicit_const:
5880 ull = attr.implicit_const;
5881 break;
5882 case DW_FORM_flag_present:
5883 ull = 1;
5884 break;
5885 case DW_FORM_udata:
5886 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5887 m_addr += bytes_read;
5888 break;
5889 default:
b98664d3 5890 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5891 dwarf_form_name (attr.form),
ed2dc618 5892 objfile_name (objfile));
927aa2e7
JK
5893 return NULL;
5894 }
5895 switch (attr.dw_idx)
5896 {
5897 case DW_IDX_compile_unit:
5898 /* Don't crash on bad data. */
b76e467d 5899 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5900 {
b98664d3 5901 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
ff4c9fec 5907 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5908 break;
8af5c486
JK
5909 case DW_IDX_type_unit:
5910 /* Don't crash on bad data. */
b2bdb8cf 5911 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5912 {
b98664d3 5913 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5914 " [in module %s]"),
5915 pulongest (ull),
5916 objfile_name (dwarf2_per_objfile->objfile));
5917 continue;
5918 }
ff4c9fec 5919 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5920 break;
927aa2e7
JK
5921 case DW_IDX_GNU_internal:
5922 if (!m_map.augmentation_is_gdb)
5923 break;
5924 have_is_static = true;
5925 is_static = true;
5926 break;
5927 case DW_IDX_GNU_external:
5928 if (!m_map.augmentation_is_gdb)
5929 break;
5930 have_is_static = true;
5931 is_static = false;
5932 break;
5933 }
5934 }
5935
5936 /* Skip if already read in. */
5937 if (per_cu->v.quick->compunit_symtab)
5938 goto again;
5939
5940 /* Check static vs global. */
5941 if (have_is_static)
5942 {
5943 const bool want_static = m_block_index != GLOBAL_BLOCK;
5944 if (m_want_specific_block && want_static != is_static)
5945 goto again;
5946 }
5947
5948 /* Match dw2_symtab_iter_next, symbol_kind
5949 and debug_names::psymbol_tag. */
5950 switch (m_domain)
5951 {
5952 case VAR_DOMAIN:
5953 switch (indexval.dwarf_tag)
5954 {
5955 case DW_TAG_variable:
5956 case DW_TAG_subprogram:
5957 /* Some types are also in VAR_DOMAIN. */
5958 case DW_TAG_typedef:
5959 case DW_TAG_structure_type:
5960 break;
5961 default:
5962 goto again;
5963 }
5964 break;
5965 case STRUCT_DOMAIN:
5966 switch (indexval.dwarf_tag)
5967 {
5968 case DW_TAG_typedef:
5969 case DW_TAG_structure_type:
5970 break;
5971 default:
5972 goto again;
5973 }
5974 break;
5975 case LABEL_DOMAIN:
5976 switch (indexval.dwarf_tag)
5977 {
5978 case 0:
5979 case DW_TAG_variable:
5980 break;
5981 default:
5982 goto again;
5983 }
5984 break;
5985 default:
5986 break;
5987 }
5988
5989 /* Match dw2_expand_symtabs_matching, symbol_kind and
5990 debug_names::psymbol_tag. */
5991 switch (m_search)
4b514bc8 5992 {
927aa2e7
JK
5993 case VARIABLES_DOMAIN:
5994 switch (indexval.dwarf_tag)
4b514bc8 5995 {
927aa2e7
JK
5996 case DW_TAG_variable:
5997 break;
5998 default:
5999 goto again;
4b514bc8 6000 }
927aa2e7
JK
6001 break;
6002 case FUNCTIONS_DOMAIN:
6003 switch (indexval.dwarf_tag)
4b514bc8 6004 {
927aa2e7
JK
6005 case DW_TAG_subprogram:
6006 break;
6007 default:
6008 goto again;
4b514bc8 6009 }
927aa2e7
JK
6010 break;
6011 case TYPES_DOMAIN:
6012 switch (indexval.dwarf_tag)
6013 {
6014 case DW_TAG_typedef:
6015 case DW_TAG_structure_type:
6016 break;
6017 default:
6018 goto again;
6019 }
6020 break;
6021 default:
6022 break;
4b514bc8 6023 }
927aa2e7
JK
6024
6025 return per_cu;
4b514bc8 6026}
61920122 6027
927aa2e7
JK
6028static struct compunit_symtab *
6029dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6030 const char *name, domain_enum domain)
4b514bc8 6031{
927aa2e7 6032 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6033 struct dwarf2_per_objfile *dwarf2_per_objfile
6034 = get_dwarf2_per_objfile (objfile);
61920122 6035
927aa2e7
JK
6036 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6037 if (!mapp)
61920122 6038 {
927aa2e7
JK
6039 /* index is NULL if OBJF_READNOW. */
6040 return NULL;
6041 }
6042 const auto &map = *mapp;
9291a0cd 6043
927aa2e7
JK
6044 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6045 block_index, domain, name);
9703b513 6046
927aa2e7
JK
6047 struct compunit_symtab *stab_best = NULL;
6048 struct dwarf2_per_cu_data *per_cu;
6049 while ((per_cu = iter.next ()) != NULL)
6050 {
6051 struct symbol *sym, *with_opaque = NULL;
58f0c718 6052 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6053 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6054 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6055
927aa2e7
JK
6056 sym = block_find_symbol (block, name, domain,
6057 block_find_non_opaque_type_preferred,
6058 &with_opaque);
9703b513 6059
927aa2e7
JK
6060 /* Some caution must be observed with overloaded functions and
6061 methods, since the index will not contain any overload
6062 information (but NAME might contain it). */
a3ec0bb1 6063
927aa2e7
JK
6064 if (sym != NULL
6065 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6066 return stab;
6067 if (with_opaque != NULL
6068 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6069 stab_best = stab;
9703b513 6070
927aa2e7 6071 /* Keep looking through other CUs. */
9703b513
TT
6072 }
6073
927aa2e7 6074 return stab_best;
9703b513
TT
6075}
6076
927aa2e7
JK
6077/* This dumps minimal information about .debug_names. It is called
6078 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6079 uses this to verify that .debug_names has been loaded. */
9291a0cd 6080
927aa2e7
JK
6081static void
6082dw2_debug_names_dump (struct objfile *objfile)
6083{
ed2dc618
SM
6084 struct dwarf2_per_objfile *dwarf2_per_objfile
6085 = get_dwarf2_per_objfile (objfile);
6086
927aa2e7
JK
6087 gdb_assert (dwarf2_per_objfile->using_index);
6088 printf_filtered (".debug_names:");
6089 if (dwarf2_per_objfile->debug_names_table)
6090 printf_filtered (" exists\n");
6091 else
6092 printf_filtered (" faked for \"readnow\"\n");
6093 printf_filtered ("\n");
9291a0cd
TT
6094}
6095
9291a0cd 6096static void
927aa2e7
JK
6097dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6098 const char *func_name)
9291a0cd 6099{
ed2dc618
SM
6100 struct dwarf2_per_objfile *dwarf2_per_objfile
6101 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6102
927aa2e7
JK
6103 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6104 if (dwarf2_per_objfile->debug_names_table)
24c79950 6105 {
927aa2e7 6106 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6107
927aa2e7
JK
6108 /* Note: It doesn't matter what we pass for block_index here. */
6109 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6110 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6111
927aa2e7
JK
6112 struct dwarf2_per_cu_data *per_cu;
6113 while ((per_cu = iter.next ()) != NULL)
58f0c718 6114 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6115 }
6116}
24c79950 6117
927aa2e7
JK
6118static void
6119dw2_debug_names_expand_symtabs_matching
6120 (struct objfile *objfile,
6121 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6122 const lookup_name_info &lookup_name,
6123 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6124 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6125 enum search_domain kind)
6126{
ed2dc618
SM
6127 struct dwarf2_per_objfile *dwarf2_per_objfile
6128 = get_dwarf2_per_objfile (objfile);
9291a0cd 6129
927aa2e7
JK
6130 /* debug_names_table is NULL if OBJF_READNOW. */
6131 if (!dwarf2_per_objfile->debug_names_table)
6132 return;
9291a0cd 6133
ed2dc618 6134 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6135
44ed8f3e 6136 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6137
44ed8f3e
PA
6138 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6139 symbol_matcher,
6140 kind, [&] (offset_type namei)
927aa2e7 6141 {
927aa2e7
JK
6142 /* The name was matched, now expand corresponding CUs that were
6143 marked. */
6144 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6145
927aa2e7
JK
6146 struct dwarf2_per_cu_data *per_cu;
6147 while ((per_cu = iter.next ()) != NULL)
6148 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6149 expansion_notify);
44ed8f3e 6150 });
9291a0cd
TT
6151}
6152
927aa2e7 6153const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6154{
6155 dw2_has_symbols,
6156 dw2_find_last_source_symtab,
6157 dw2_forget_cached_source_info,
f8eba3c6 6158 dw2_map_symtabs_matching_filename,
927aa2e7 6159 dw2_debug_names_lookup_symbol,
9291a0cd 6160 dw2_print_stats,
927aa2e7 6161 dw2_debug_names_dump,
927aa2e7 6162 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6163 dw2_expand_all_symtabs,
652a8996 6164 dw2_expand_symtabs_with_fullname,
40658b94 6165 dw2_map_matching_symbols,
927aa2e7 6166 dw2_debug_names_expand_symtabs_matching,
43f3e411 6167 dw2_find_pc_sect_compunit_symtab,
71a3c369 6168 NULL,
9291a0cd
TT
6169 dw2_map_symbol_filenames
6170};
6171
4485a1c1
SM
6172/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6173 to either a dwarf2_per_objfile or dwz_file object. */
6174
6175template <typename T>
6176static gdb::array_view<const gdb_byte>
6177get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6178{
6179 dwarf2_section_info *section = &section_owner->gdb_index;
6180
6181 if (dwarf2_section_empty_p (section))
6182 return {};
6183
6184 /* Older elfutils strip versions could keep the section in the main
6185 executable while splitting it for the separate debug info file. */
6186 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6187 return {};
6188
6189 dwarf2_read_section (obj, section);
6190
8bebfcda
PA
6191 /* dwarf2_section_info::size is a bfd_size_type, while
6192 gdb::array_view works with size_t. On 32-bit hosts, with
6193 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6194 is 32-bit. So we need an explicit narrowing conversion here.
6195 This is fine, because it's impossible to allocate or mmap an
6196 array/buffer larger than what size_t can represent. */
6197 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6198}
6199
87d6a7aa
SM
6200/* Lookup the index cache for the contents of the index associated to
6201 DWARF2_OBJ. */
6202
6203static gdb::array_view<const gdb_byte>
6204get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6205{
6206 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6207 if (build_id == nullptr)
6208 return {};
6209
6210 return global_index_cache.lookup_gdb_index (build_id,
6211 &dwarf2_obj->index_cache_res);
6212}
6213
6214/* Same as the above, but for DWZ. */
6215
6216static gdb::array_view<const gdb_byte>
6217get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6218{
6219 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6220 if (build_id == nullptr)
6221 return {};
6222
6223 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6224}
6225
3c0aa29a 6226/* See symfile.h. */
9291a0cd 6227
3c0aa29a
PA
6228bool
6229dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6230{
ed2dc618
SM
6231 struct dwarf2_per_objfile *dwarf2_per_objfile
6232 = get_dwarf2_per_objfile (objfile);
6233
9291a0cd
TT
6234 /* If we're about to read full symbols, don't bother with the
6235 indices. In this case we also don't care if some other debug
6236 format is making psymtabs, because they are all about to be
6237 expanded anyway. */
6238 if ((objfile->flags & OBJF_READNOW))
6239 {
9291a0cd 6240 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6241 create_all_comp_units (dwarf2_per_objfile);
6242 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6243 dwarf2_per_objfile->quick_file_names_table
6244 = create_quick_file_names_table
6245 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6246
b76e467d 6247 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6248 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6249 {
ff4c9fec 6250 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6251
e254ef6a
DE
6252 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6253 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6254 }
6255
6256 /* Return 1 so that gdb sees the "quick" functions. However,
6257 these functions will be no-ops because we will have expanded
6258 all symtabs. */
3c0aa29a
PA
6259 *index_kind = dw_index_kind::GDB_INDEX;
6260 return true;
9291a0cd
TT
6261 }
6262
ed2dc618 6263 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6264 {
6265 *index_kind = dw_index_kind::DEBUG_NAMES;
6266 return true;
6267 }
927aa2e7 6268
4485a1c1
SM
6269 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6270 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6271 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6272 {
6273 *index_kind = dw_index_kind::GDB_INDEX;
6274 return true;
6275 }
9291a0cd 6276
87d6a7aa
SM
6277 /* ... otherwise, try to find the index in the index cache. */
6278 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6279 get_gdb_index_contents_from_cache,
6280 get_gdb_index_contents_from_cache_dwz))
6281 {
6282 global_index_cache.hit ();
6283 *index_kind = dw_index_kind::GDB_INDEX;
6284 return true;
6285 }
6286
6287 global_index_cache.miss ();
3c0aa29a 6288 return false;
9291a0cd
TT
6289}
6290
6291\f
6292
dce234bc
PP
6293/* Build a partial symbol table. */
6294
6295void
f29dff0a 6296dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6297{
ed2dc618
SM
6298 struct dwarf2_per_objfile *dwarf2_per_objfile
6299 = get_dwarf2_per_objfile (objfile);
c9bf0622 6300
6eee24ce 6301 init_psymbol_list (objfile, 1024);
c906108c 6302
a70b8144 6303 try
c9bf0622
TT
6304 {
6305 /* This isn't really ideal: all the data we allocate on the
6306 objfile's obstack is still uselessly kept around. However,
6307 freeing it seems unsafe. */
906768f9 6308 psymtab_discarder psymtabs (objfile);
ed2dc618 6309 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6310 psymtabs.keep ();
87d6a7aa
SM
6311
6312 /* (maybe) store an index in the cache. */
6313 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6314 }
230d2906 6315 catch (const gdb_exception_error &except)
492d29ea
PA
6316 {
6317 exception_print (gdb_stderr, except);
6318 }
c906108c 6319}
c906108c 6320
1ce1cefd
DE
6321/* Return the total length of the CU described by HEADER. */
6322
6323static unsigned int
6324get_cu_length (const struct comp_unit_head *header)
6325{
6326 return header->initial_length_size + header->length;
6327}
6328
9c541725 6329/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6330
9c541725
PA
6331static inline bool
6332offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6333{
9c541725
PA
6334 sect_offset bottom = cu_header->sect_off;
6335 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6336
9c541725 6337 return sect_off >= bottom && sect_off < top;
45452591
DE
6338}
6339
3b80fe9b
DE
6340/* Find the base address of the compilation unit for range lists and
6341 location lists. It will normally be specified by DW_AT_low_pc.
6342 In DWARF-3 draft 4, the base address could be overridden by
6343 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6344 compilation units with discontinuous ranges. */
6345
6346static void
6347dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6348{
6349 struct attribute *attr;
6350
6351 cu->base_known = 0;
6352 cu->base_address = 0;
6353
6354 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6355 if (attr)
6356 {
31aa7e4e 6357 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6358 cu->base_known = 1;
6359 }
6360 else
6361 {
6362 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6363 if (attr)
6364 {
31aa7e4e 6365 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6366 cu->base_known = 1;
6367 }
6368 }
6369}
6370
93311388 6371/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6372 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6373 NOTE: This leaves members offset, first_die_offset to be filled in
6374 by the caller. */
107d2387 6375
d521ce57 6376static const gdb_byte *
107d2387 6377read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6378 const gdb_byte *info_ptr,
6379 struct dwarf2_section_info *section,
6380 rcuh_kind section_kind)
107d2387
AC
6381{
6382 int signed_addr;
891d2f0b 6383 unsigned int bytes_read;
43988095
JK
6384 const char *filename = get_section_file_name (section);
6385 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6386
6387 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6388 cu_header->initial_length_size = bytes_read;
6389 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6390 info_ptr += bytes_read;
107d2387 6391 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6392 if (cu_header->version < 2 || cu_header->version > 5)
6393 error (_("Dwarf Error: wrong version in compilation unit header "
6394 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6395 cu_header->version, filename);
107d2387 6396 info_ptr += 2;
43988095
JK
6397 if (cu_header->version < 5)
6398 switch (section_kind)
6399 {
6400 case rcuh_kind::COMPILE:
6401 cu_header->unit_type = DW_UT_compile;
6402 break;
6403 case rcuh_kind::TYPE:
6404 cu_header->unit_type = DW_UT_type;
6405 break;
6406 default:
6407 internal_error (__FILE__, __LINE__,
6408 _("read_comp_unit_head: invalid section_kind"));
6409 }
6410 else
6411 {
6412 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6413 (read_1_byte (abfd, info_ptr));
6414 info_ptr += 1;
6415 switch (cu_header->unit_type)
6416 {
6417 case DW_UT_compile:
6418 if (section_kind != rcuh_kind::COMPILE)
6419 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6420 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6421 filename);
6422 break;
6423 case DW_UT_type:
6424 section_kind = rcuh_kind::TYPE;
6425 break;
6426 default:
6427 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6428 "(is %d, should be %d or %d) [in module %s]"),
6429 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6430 }
6431
6432 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6433 info_ptr += 1;
6434 }
9c541725
PA
6435 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6436 cu_header,
6437 &bytes_read);
613e1657 6438 info_ptr += bytes_read;
43988095
JK
6439 if (cu_header->version < 5)
6440 {
6441 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6442 info_ptr += 1;
6443 }
107d2387
AC
6444 signed_addr = bfd_get_sign_extend_vma (abfd);
6445 if (signed_addr < 0)
8e65ff28 6446 internal_error (__FILE__, __LINE__,
e2e0b3e5 6447 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6448 cu_header->signed_addr_p = signed_addr;
c764a876 6449
43988095
JK
6450 if (section_kind == rcuh_kind::TYPE)
6451 {
6452 LONGEST type_offset;
6453
6454 cu_header->signature = read_8_bytes (abfd, info_ptr);
6455 info_ptr += 8;
6456
6457 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6458 info_ptr += bytes_read;
9c541725
PA
6459 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6460 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6461 error (_("Dwarf Error: Too big type_offset in compilation unit "
6462 "header (is %s) [in module %s]"), plongest (type_offset),
6463 filename);
6464 }
6465
107d2387
AC
6466 return info_ptr;
6467}
6468
36586728
TT
6469/* Helper function that returns the proper abbrev section for
6470 THIS_CU. */
6471
6472static struct dwarf2_section_info *
6473get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6474{
6475 struct dwarf2_section_info *abbrev;
ed2dc618 6476 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6477
6478 if (this_cu->is_dwz)
ed2dc618 6479 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6480 else
6481 abbrev = &dwarf2_per_objfile->abbrev;
6482
6483 return abbrev;
6484}
6485
9ff913ba
DE
6486/* Subroutine of read_and_check_comp_unit_head and
6487 read_and_check_type_unit_head to simplify them.
6488 Perform various error checking on the header. */
6489
6490static void
ed2dc618
SM
6491error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6492 struct comp_unit_head *header,
4bdcc0c1
DE
6493 struct dwarf2_section_info *section,
6494 struct dwarf2_section_info *abbrev_section)
9ff913ba 6495{
a32a8923 6496 const char *filename = get_section_file_name (section);
9ff913ba 6497
9c541725 6498 if (to_underlying (header->abbrev_sect_off)
36586728 6499 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6500 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6501 "(offset %s + 6) [in module %s]"),
6502 sect_offset_str (header->abbrev_sect_off),
6503 sect_offset_str (header->sect_off),
9ff913ba
DE
6504 filename);
6505
9c541725 6506 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6507 avoid potential 32-bit overflow. */
9c541725 6508 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6509 > section->size)
9c541725 6510 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6511 "(offset %s + 0) [in module %s]"),
6512 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6513 filename);
6514}
6515
6516/* Read in a CU/TU header and perform some basic error checking.
6517 The contents of the header are stored in HEADER.
6518 The result is a pointer to the start of the first DIE. */
adabb602 6519
d521ce57 6520static const gdb_byte *
ed2dc618
SM
6521read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6522 struct comp_unit_head *header,
9ff913ba 6523 struct dwarf2_section_info *section,
4bdcc0c1 6524 struct dwarf2_section_info *abbrev_section,
d521ce57 6525 const gdb_byte *info_ptr,
43988095 6526 rcuh_kind section_kind)
72bf9492 6527{
d521ce57 6528 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6529
9c541725 6530 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6531
43988095 6532 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6533
9c541725 6534 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6535
ed2dc618
SM
6536 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6537 abbrev_section);
9ff913ba
DE
6538
6539 return info_ptr;
348e048f
DE
6540}
6541
f4dc4d17
DE
6542/* Fetch the abbreviation table offset from a comp or type unit header. */
6543
6544static sect_offset
ed2dc618
SM
6545read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6546 struct dwarf2_section_info *section,
9c541725 6547 sect_offset sect_off)
f4dc4d17 6548{
a32a8923 6549 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6550 const gdb_byte *info_ptr;
ac298888 6551 unsigned int initial_length_size, offset_size;
43988095 6552 uint16_t version;
f4dc4d17
DE
6553
6554 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6555 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6556 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6557 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6558 info_ptr += initial_length_size;
6559
6560 version = read_2_bytes (abfd, info_ptr);
6561 info_ptr += 2;
6562 if (version >= 5)
6563 {
6564 /* Skip unit type and address size. */
6565 info_ptr += 2;
6566 }
6567
9c541725 6568 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6569}
6570
aaa75496
JB
6571/* Allocate a new partial symtab for file named NAME and mark this new
6572 partial symtab as being an include of PST. */
6573
6574static void
d521ce57 6575dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6576 struct objfile *objfile)
6577{
6578 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6579
fbd9ab74
JK
6580 if (!IS_ABSOLUTE_PATH (subpst->filename))
6581 {
6582 /* It shares objfile->objfile_obstack. */
6583 subpst->dirname = pst->dirname;
6584 }
6585
a9342b62 6586 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6587 subpst->dependencies[0] = pst;
6588 subpst->number_of_dependencies = 1;
6589
aaa75496 6590 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6591
6592 /* No private part is necessary for include psymtabs. This property
6593 can be used to differentiate between such include psymtabs and
10b3939b 6594 the regular ones. */
58a9656e 6595 subpst->read_symtab_private = NULL;
aaa75496
JB
6596}
6597
6598/* Read the Line Number Program data and extract the list of files
6599 included by the source file represented by PST. Build an include
d85a05f0 6600 partial symtab for each of these included files. */
aaa75496
JB
6601
6602static void
6603dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6604 struct die_info *die,
6605 struct partial_symtab *pst)
aaa75496 6606{
fff8551c 6607 line_header_up lh;
d85a05f0 6608 struct attribute *attr;
aaa75496 6609
d85a05f0
DJ
6610 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6611 if (attr)
9c541725 6612 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6613 if (lh == NULL)
6614 return; /* No linetable, so no includes. */
6615
79748972
TT
6616 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6617 that we pass in the raw text_low here; that is ok because we're
6618 only decoding the line table to make include partial symtabs, and
6619 so the addresses aren't really used. */
4ae976d1 6620 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6621 pst->raw_text_low (), 1);
aaa75496
JB
6622}
6623
348e048f 6624static hashval_t
52dc124a 6625hash_signatured_type (const void *item)
348e048f 6626{
9a3c8263
SM
6627 const struct signatured_type *sig_type
6628 = (const struct signatured_type *) item;
9a619af0 6629
348e048f 6630 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6631 return sig_type->signature;
348e048f
DE
6632}
6633
6634static int
52dc124a 6635eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6636{
9a3c8263
SM
6637 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6638 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6639
348e048f
DE
6640 return lhs->signature == rhs->signature;
6641}
6642
1fd400ff
TT
6643/* Allocate a hash table for signatured types. */
6644
6645static htab_t
673bfd45 6646allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6647{
6648 return htab_create_alloc_ex (41,
52dc124a
DE
6649 hash_signatured_type,
6650 eq_signatured_type,
1fd400ff
TT
6651 NULL,
6652 &objfile->objfile_obstack,
6653 hashtab_obstack_allocate,
6654 dummy_obstack_deallocate);
6655}
6656
d467dd73 6657/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6658
6659static int
d467dd73 6660add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6661{
9a3c8263 6662 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6663 std::vector<signatured_type *> *all_type_units
6664 = (std::vector<signatured_type *> *) datum;
1fd400ff 6665
b2bdb8cf 6666 all_type_units->push_back (sigt);
1fd400ff
TT
6667
6668 return 1;
6669}
6670
78d4d2c5 6671/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6672 and fill them into TYPES_HTAB. It will process only type units,
6673 therefore DW_UT_type. */
c88ee1f0 6674
78d4d2c5 6675static void
ed2dc618
SM
6676create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6677 struct dwo_file *dwo_file,
43988095
JK
6678 dwarf2_section_info *section, htab_t &types_htab,
6679 rcuh_kind section_kind)
348e048f 6680{
3019eac3 6681 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6682 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6683 bfd *abfd;
6684 const gdb_byte *info_ptr, *end_ptr;
348e048f 6685
4bdcc0c1
DE
6686 abbrev_section = (dwo_file != NULL
6687 ? &dwo_file->sections.abbrev
6688 : &dwarf2_per_objfile->abbrev);
6689
b4f54984 6690 if (dwarf_read_debug)
43988095
JK
6691 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6692 get_section_name (section),
a32a8923 6693 get_section_file_name (abbrev_section));
09406207 6694
78d4d2c5
JK
6695 dwarf2_read_section (objfile, section);
6696 info_ptr = section->buffer;
348e048f 6697
78d4d2c5
JK
6698 if (info_ptr == NULL)
6699 return;
348e048f 6700
78d4d2c5
JK
6701 /* We can't set abfd until now because the section may be empty or
6702 not present, in which case the bfd is unknown. */
6703 abfd = get_section_bfd_owner (section);
348e048f 6704
78d4d2c5
JK
6705 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6706 because we don't need to read any dies: the signature is in the
6707 header. */
3019eac3 6708
78d4d2c5
JK
6709 end_ptr = info_ptr + section->size;
6710 while (info_ptr < end_ptr)
6711 {
78d4d2c5
JK
6712 struct signatured_type *sig_type;
6713 struct dwo_unit *dwo_tu;
6714 void **slot;
6715 const gdb_byte *ptr = info_ptr;
6716 struct comp_unit_head header;
6717 unsigned int length;
8b70b953 6718
9c541725 6719 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6720
a49dd8dd
JK
6721 /* Initialize it due to a false compiler warning. */
6722 header.signature = -1;
9c541725 6723 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6724
78d4d2c5
JK
6725 /* We need to read the type's signature in order to build the hash
6726 table, but we don't need anything else just yet. */
348e048f 6727
ed2dc618 6728 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6729 abbrev_section, ptr, section_kind);
348e048f 6730
78d4d2c5 6731 length = get_cu_length (&header);
6caca83c 6732
78d4d2c5
JK
6733 /* Skip dummy type units. */
6734 if (ptr >= info_ptr + length
43988095
JK
6735 || peek_abbrev_code (abfd, ptr) == 0
6736 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6737 {
6738 info_ptr += length;
6739 continue;
6740 }
dee91e82 6741
78d4d2c5
JK
6742 if (types_htab == NULL)
6743 {
6744 if (dwo_file)
6745 types_htab = allocate_dwo_unit_table (objfile);
6746 else
6747 types_htab = allocate_signatured_type_table (objfile);
6748 }
8b70b953 6749
78d4d2c5
JK
6750 if (dwo_file)
6751 {
6752 sig_type = NULL;
6753 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6754 struct dwo_unit);
6755 dwo_tu->dwo_file = dwo_file;
43988095 6756 dwo_tu->signature = header.signature;
9c541725 6757 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6758 dwo_tu->section = section;
9c541725 6759 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6760 dwo_tu->length = length;
6761 }
6762 else
6763 {
6764 /* N.B.: type_offset is not usable if this type uses a DWO file.
6765 The real type_offset is in the DWO file. */
6766 dwo_tu = NULL;
6767 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6768 struct signatured_type);
43988095 6769 sig_type->signature = header.signature;
9c541725 6770 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6771 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6772 sig_type->per_cu.is_debug_types = 1;
6773 sig_type->per_cu.section = section;
9c541725 6774 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6775 sig_type->per_cu.length = length;
6776 }
6777
6778 slot = htab_find_slot (types_htab,
6779 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6780 INSERT);
6781 gdb_assert (slot != NULL);
6782 if (*slot != NULL)
6783 {
9c541725 6784 sect_offset dup_sect_off;
0349ea22 6785
3019eac3
DE
6786 if (dwo_file)
6787 {
78d4d2c5
JK
6788 const struct dwo_unit *dup_tu
6789 = (const struct dwo_unit *) *slot;
6790
9c541725 6791 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6792 }
6793 else
6794 {
78d4d2c5
JK
6795 const struct signatured_type *dup_tu
6796 = (const struct signatured_type *) *slot;
6797
9c541725 6798 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6799 }
8b70b953 6800
b98664d3 6801 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6802 " the entry at offset %s, signature %s"),
6803 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6804 hex_string (header.signature));
78d4d2c5
JK
6805 }
6806 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6807
78d4d2c5 6808 if (dwarf_read_debug > 1)
9d8780f0
SM
6809 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6810 sect_offset_str (sect_off),
43988095 6811 hex_string (header.signature));
3019eac3 6812
78d4d2c5
JK
6813 info_ptr += length;
6814 }
6815}
3019eac3 6816
78d4d2c5
JK
6817/* Create the hash table of all entries in the .debug_types
6818 (or .debug_types.dwo) section(s).
6819 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6820 otherwise it is NULL.
b3c8eb43 6821
78d4d2c5 6822 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6823
78d4d2c5 6824 Note: This function processes DWO files only, not DWP files. */
348e048f 6825
78d4d2c5 6826static void
ed2dc618
SM
6827create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6828 struct dwo_file *dwo_file,
78d4d2c5
JK
6829 VEC (dwarf2_section_info_def) *types,
6830 htab_t &types_htab)
6831{
6832 int ix;
6833 struct dwarf2_section_info *section;
6834
6835 if (VEC_empty (dwarf2_section_info_def, types))
6836 return;
348e048f 6837
78d4d2c5
JK
6838 for (ix = 0;
6839 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6840 ++ix)
ed2dc618
SM
6841 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6842 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6843}
6844
6845/* Create the hash table of all entries in the .debug_types section,
6846 and initialize all_type_units.
6847 The result is zero if there is an error (e.g. missing .debug_types section),
6848 otherwise non-zero. */
6849
6850static int
ed2dc618 6851create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6852{
78d4d2c5 6853 htab_t types_htab = NULL;
3019eac3 6854
ed2dc618
SM
6855 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6856 &dwarf2_per_objfile->info, types_htab,
43988095 6857 rcuh_kind::COMPILE);
ed2dc618
SM
6858 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6859 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6860 if (types_htab == NULL)
6861 {
6862 dwarf2_per_objfile->signatured_types = NULL;
6863 return 0;
6864 }
6865
348e048f
DE
6866 dwarf2_per_objfile->signatured_types = types_htab;
6867
b2bdb8cf
SM
6868 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6869 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6870
6871 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6872 &dwarf2_per_objfile->all_type_units);
1fd400ff 6873
348e048f
DE
6874 return 1;
6875}
6876
6aa5f3a6
DE
6877/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6878 If SLOT is non-NULL, it is the entry to use in the hash table.
6879 Otherwise we find one. */
6880
6881static struct signatured_type *
ed2dc618
SM
6882add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6883 void **slot)
6aa5f3a6
DE
6884{
6885 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6886
b2bdb8cf
SM
6887 if (dwarf2_per_objfile->all_type_units.size ()
6888 == dwarf2_per_objfile->all_type_units.capacity ())
6889 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6890
b2bdb8cf
SM
6891 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6892 struct signatured_type);
6893
6894 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6895 sig_type->signature = sig;
6896 sig_type->per_cu.is_debug_types = 1;
6897 if (dwarf2_per_objfile->using_index)
6898 {
6899 sig_type->per_cu.v.quick =
6900 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6901 struct dwarf2_per_cu_quick_data);
6902 }
6903
6904 if (slot == NULL)
6905 {
6906 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6907 sig_type, INSERT);
6908 }
6909 gdb_assert (*slot == NULL);
6910 *slot = sig_type;
6911 /* The rest of sig_type must be filled in by the caller. */
6912 return sig_type;
6913}
6914
a2ce51a0
DE
6915/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6916 Fill in SIG_ENTRY with DWO_ENTRY. */
6917
6918static void
ed2dc618 6919fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6920 struct signatured_type *sig_entry,
6921 struct dwo_unit *dwo_entry)
6922{
7ee85ab1 6923 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6924 gdb_assert (! sig_entry->per_cu.queued);
6925 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6926 if (dwarf2_per_objfile->using_index)
6927 {
6928 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6929 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6930 }
6931 else
6932 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6933 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6934 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6935 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6936 gdb_assert (sig_entry->dwo_unit == NULL);
6937
6938 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6939 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6940 sig_entry->per_cu.length = dwo_entry->length;
6941 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6942 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6943 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6944 sig_entry->dwo_unit = dwo_entry;
6945}
6946
6947/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6948 If we haven't read the TU yet, create the signatured_type data structure
6949 for a TU to be read in directly from a DWO file, bypassing the stub.
6950 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6951 using .gdb_index, then when reading a CU we want to stay in the DWO file
6952 containing that CU. Otherwise we could end up reading several other DWO
6953 files (due to comdat folding) to process the transitive closure of all the
6954 mentioned TUs, and that can be slow. The current DWO file will have every
6955 type signature that it needs.
a2ce51a0
DE
6956 We only do this for .gdb_index because in the psymtab case we already have
6957 to read all the DWOs to build the type unit groups. */
6958
6959static struct signatured_type *
6960lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6961{
518817b3
SM
6962 struct dwarf2_per_objfile *dwarf2_per_objfile
6963 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
6965 struct dwo_file *dwo_file;
6966 struct dwo_unit find_dwo_entry, *dwo_entry;
6967 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6968 void **slot;
a2ce51a0
DE
6969
6970 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6971
6aa5f3a6
DE
6972 /* If TU skeletons have been removed then we may not have read in any
6973 TUs yet. */
6974 if (dwarf2_per_objfile->signatured_types == NULL)
6975 {
6976 dwarf2_per_objfile->signatured_types
6977 = allocate_signatured_type_table (objfile);
6978 }
a2ce51a0
DE
6979
6980 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6981 Use the global signatured_types array to do our own comdat-folding
6982 of types. If this is the first time we're reading this TU, and
6983 the TU has an entry in .gdb_index, replace the recorded data from
6984 .gdb_index with this TU. */
a2ce51a0 6985
a2ce51a0 6986 find_sig_entry.signature = sig;
6aa5f3a6
DE
6987 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6988 &find_sig_entry, INSERT);
9a3c8263 6989 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6990
6991 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6992 read. Don't reassign the global entry to point to this DWO if that's
6993 the case. Also note that if the TU is already being read, it may not
6994 have come from a DWO, the program may be a mix of Fission-compiled
6995 code and non-Fission-compiled code. */
6996
6997 /* Have we already tried to read this TU?
6998 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6999 needn't exist in the global table yet). */
7000 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7001 return sig_entry;
7002
6aa5f3a6
DE
7003 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7004 dwo_unit of the TU itself. */
7005 dwo_file = cu->dwo_unit->dwo_file;
7006
a2ce51a0
DE
7007 /* Ok, this is the first time we're reading this TU. */
7008 if (dwo_file->tus == NULL)
7009 return NULL;
7010 find_dwo_entry.signature = sig;
9a3c8263 7011 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7012 if (dwo_entry == NULL)
7013 return NULL;
7014
6aa5f3a6
DE
7015 /* If the global table doesn't have an entry for this TU, add one. */
7016 if (sig_entry == NULL)
ed2dc618 7017 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7018
ed2dc618 7019 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7020 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7021 return sig_entry;
7022}
7023
a2ce51a0
DE
7024/* Subroutine of lookup_signatured_type.
7025 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7026 then try the DWP file. If the TU stub (skeleton) has been removed then
7027 it won't be in .gdb_index. */
a2ce51a0
DE
7028
7029static struct signatured_type *
7030lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7031{
518817b3
SM
7032 struct dwarf2_per_objfile *dwarf2_per_objfile
7033 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7034 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7035 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7036 struct dwo_unit *dwo_entry;
7037 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7038 void **slot;
a2ce51a0
DE
7039
7040 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7041 gdb_assert (dwp_file != NULL);
7042
6aa5f3a6
DE
7043 /* If TU skeletons have been removed then we may not have read in any
7044 TUs yet. */
7045 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7046 {
6aa5f3a6
DE
7047 dwarf2_per_objfile->signatured_types
7048 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7049 }
7050
6aa5f3a6
DE
7051 find_sig_entry.signature = sig;
7052 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7053 &find_sig_entry, INSERT);
9a3c8263 7054 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7055
7056 /* Have we already tried to read this TU?
7057 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7058 needn't exist in the global table yet). */
7059 if (sig_entry != NULL)
7060 return sig_entry;
7061
a2ce51a0
DE
7062 if (dwp_file->tus == NULL)
7063 return NULL;
ed2dc618 7064 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7065 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7066 if (dwo_entry == NULL)
7067 return NULL;
7068
ed2dc618
SM
7069 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7070 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7071
a2ce51a0
DE
7072 return sig_entry;
7073}
7074
380bca97 7075/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7076 Returns NULL if signature SIG is not present in the table.
7077 It is up to the caller to complain about this. */
348e048f
DE
7078
7079static struct signatured_type *
a2ce51a0 7080lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7081{
518817b3
SM
7082 struct dwarf2_per_objfile *dwarf2_per_objfile
7083 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7084
a2ce51a0
DE
7085 if (cu->dwo_unit
7086 && dwarf2_per_objfile->using_index)
7087 {
7088 /* We're in a DWO/DWP file, and we're using .gdb_index.
7089 These cases require special processing. */
ed2dc618 7090 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7091 return lookup_dwo_signatured_type (cu, sig);
7092 else
7093 return lookup_dwp_signatured_type (cu, sig);
7094 }
7095 else
7096 {
7097 struct signatured_type find_entry, *entry;
348e048f 7098
a2ce51a0
DE
7099 if (dwarf2_per_objfile->signatured_types == NULL)
7100 return NULL;
7101 find_entry.signature = sig;
9a3c8263
SM
7102 entry = ((struct signatured_type *)
7103 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7104 return entry;
7105 }
348e048f 7106}
42e7ad6c
DE
7107\f
7108/* Low level DIE reading support. */
348e048f 7109
d85a05f0
DJ
7110/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7111
7112static void
7113init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7114 struct dwarf2_cu *cu,
3019eac3 7115 struct dwarf2_section_info *section,
685af9cd
TT
7116 struct dwo_file *dwo_file,
7117 struct abbrev_table *abbrev_table)
d85a05f0 7118{
fceca515 7119 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7120 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7121 reader->cu = cu;
3019eac3 7122 reader->dwo_file = dwo_file;
dee91e82
DE
7123 reader->die_section = section;
7124 reader->buffer = section->buffer;
f664829e 7125 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7126 reader->comp_dir = NULL;
685af9cd 7127 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7128}
7129
b0c7bfa9
DE
7130/* Subroutine of init_cutu_and_read_dies to simplify it.
7131 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7132 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7133 already.
7134
7135 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7136 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7137 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7138 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7139 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7140 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7141 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7142 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7143 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7144 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7145 kept around for at least as long as *RESULT_READER.
7146
b0c7bfa9
DE
7147 The result is non-zero if a valid (non-dummy) DIE was found. */
7148
7149static int
7150read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7151 struct dwo_unit *dwo_unit,
b0c7bfa9 7152 struct die_info *stub_comp_unit_die,
a2ce51a0 7153 const char *stub_comp_dir,
b0c7bfa9 7154 struct die_reader_specs *result_reader,
d521ce57 7155 const gdb_byte **result_info_ptr,
b0c7bfa9 7156 struct die_info **result_comp_unit_die,
685af9cd
TT
7157 int *result_has_children,
7158 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7159{
ed2dc618 7160 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7161 struct objfile *objfile = dwarf2_per_objfile->objfile;
7162 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7163 bfd *abfd;
d521ce57 7164 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7165 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7166 int i,num_extra_attrs;
7167 struct dwarf2_section_info *dwo_abbrev_section;
7168 struct attribute *attr;
7169 struct die_info *comp_unit_die;
7170
b0aeadb3
DE
7171 /* At most one of these may be provided. */
7172 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7173
b0c7bfa9
DE
7174 /* These attributes aren't processed until later:
7175 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7176 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7177 referenced later. However, these attributes are found in the stub
7178 which we won't have later. In order to not impose this complication
7179 on the rest of the code, we read them here and copy them to the
7180 DWO CU/TU die. */
b0c7bfa9
DE
7181
7182 stmt_list = NULL;
7183 low_pc = NULL;
7184 high_pc = NULL;
7185 ranges = NULL;
7186 comp_dir = NULL;
7187
7188 if (stub_comp_unit_die != NULL)
7189 {
7190 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7191 DWO file. */
7192 if (! this_cu->is_debug_types)
7193 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7194 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7195 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7196 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7197 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7198
7199 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7200 We need the value before we can process DW_FORM_GNU_addr_index
7201 or DW_FORM_addrx. */
b0c7bfa9
DE
7202 cu->addr_base = 0;
7203 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7204 if (attr)
7205 cu->addr_base = DW_UNSND (attr);
7206
7207 /* There should be a DW_AT_ranges_base attribute here (if needed).
7208 We need the value before we can process DW_AT_ranges. */
7209 cu->ranges_base = 0;
7210 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7211 if (attr)
7212 cu->ranges_base = DW_UNSND (attr);
7213 }
a2ce51a0
DE
7214 else if (stub_comp_dir != NULL)
7215 {
7216 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7217 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7218 comp_dir->name = DW_AT_comp_dir;
7219 comp_dir->form = DW_FORM_string;
7220 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7221 DW_STRING (comp_dir) = stub_comp_dir;
7222 }
b0c7bfa9
DE
7223
7224 /* Set up for reading the DWO CU/TU. */
7225 cu->dwo_unit = dwo_unit;
685af9cd 7226 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7227 dwarf2_read_section (objfile, section);
a32a8923 7228 abfd = get_section_bfd_owner (section);
9c541725
PA
7229 begin_info_ptr = info_ptr = (section->buffer
7230 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7231 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7232
7233 if (this_cu->is_debug_types)
7234 {
b0c7bfa9
DE
7235 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7236
ed2dc618
SM
7237 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7238 &cu->header, section,
b0c7bfa9 7239 dwo_abbrev_section,
43988095 7240 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7241 /* This is not an assert because it can be caused by bad debug info. */
43988095 7242 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7243 {
7244 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7245 " TU at offset %s [in module %s]"),
a2ce51a0 7246 hex_string (sig_type->signature),
43988095 7247 hex_string (cu->header.signature),
9d8780f0 7248 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7249 bfd_get_filename (abfd));
7250 }
9c541725 7251 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7252 /* For DWOs coming from DWP files, we don't know the CU length
7253 nor the type's offset in the TU until now. */
7254 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7255 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7256
7257 /* Establish the type offset that can be used to lookup the type.
7258 For DWO files, we don't know it until now. */
9c541725
PA
7259 sig_type->type_offset_in_section
7260 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7261 }
7262 else
7263 {
ed2dc618
SM
7264 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7265 &cu->header, section,
b0c7bfa9 7266 dwo_abbrev_section,
43988095 7267 info_ptr, rcuh_kind::COMPILE);
9c541725 7268 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7269 /* For DWOs coming from DWP files, we don't know the CU length
7270 until now. */
7271 dwo_unit->length = get_cu_length (&cu->header);
7272 }
7273
685af9cd
TT
7274 *result_dwo_abbrev_table
7275 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7276 cu->header.abbrev_sect_off);
7277 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7278 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7279
7280 /* Read in the die, but leave space to copy over the attributes
7281 from the stub. This has the benefit of simplifying the rest of
7282 the code - all the work to maintain the illusion of a single
7283 DW_TAG_{compile,type}_unit DIE is done here. */
7284 num_extra_attrs = ((stmt_list != NULL)
7285 + (low_pc != NULL)
7286 + (high_pc != NULL)
7287 + (ranges != NULL)
7288 + (comp_dir != NULL));
7289 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7290 result_has_children, num_extra_attrs);
7291
7292 /* Copy over the attributes from the stub to the DIE we just read in. */
7293 comp_unit_die = *result_comp_unit_die;
7294 i = comp_unit_die->num_attrs;
7295 if (stmt_list != NULL)
7296 comp_unit_die->attrs[i++] = *stmt_list;
7297 if (low_pc != NULL)
7298 comp_unit_die->attrs[i++] = *low_pc;
7299 if (high_pc != NULL)
7300 comp_unit_die->attrs[i++] = *high_pc;
7301 if (ranges != NULL)
7302 comp_unit_die->attrs[i++] = *ranges;
7303 if (comp_dir != NULL)
7304 comp_unit_die->attrs[i++] = *comp_dir;
7305 comp_unit_die->num_attrs += num_extra_attrs;
7306
b4f54984 7307 if (dwarf_die_debug)
bf6af496
DE
7308 {
7309 fprintf_unfiltered (gdb_stdlog,
7310 "Read die from %s@0x%x of %s:\n",
a32a8923 7311 get_section_name (section),
bf6af496
DE
7312 (unsigned) (begin_info_ptr - section->buffer),
7313 bfd_get_filename (abfd));
b4f54984 7314 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7315 }
7316
a2ce51a0
DE
7317 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7318 TUs by skipping the stub and going directly to the entry in the DWO file.
7319 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7320 to get it via circuitous means. Blech. */
7321 if (comp_dir != NULL)
7322 result_reader->comp_dir = DW_STRING (comp_dir);
7323
b0c7bfa9
DE
7324 /* Skip dummy compilation units. */
7325 if (info_ptr >= begin_info_ptr + dwo_unit->length
7326 || peek_abbrev_code (abfd, info_ptr) == 0)
7327 return 0;
7328
7329 *result_info_ptr = info_ptr;
7330 return 1;
7331}
7332
7333/* Subroutine of init_cutu_and_read_dies to simplify it.
7334 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7335 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7336
7337static struct dwo_unit *
7338lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7339 struct die_info *comp_unit_die)
7340{
7341 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7342 ULONGEST signature;
7343 struct dwo_unit *dwo_unit;
7344 const char *comp_dir, *dwo_name;
7345
a2ce51a0
DE
7346 gdb_assert (cu != NULL);
7347
b0c7bfa9 7348 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7349 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7350 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7351
7352 if (this_cu->is_debug_types)
7353 {
7354 struct signatured_type *sig_type;
7355
7356 /* Since this_cu is the first member of struct signatured_type,
7357 we can go from a pointer to one to a pointer to the other. */
7358 sig_type = (struct signatured_type *) this_cu;
7359 signature = sig_type->signature;
7360 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7361 }
7362 else
7363 {
7364 struct attribute *attr;
7365
7366 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7367 if (! attr)
7368 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7369 " [in module %s]"),
e3b94546 7370 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7371 signature = DW_UNSND (attr);
7372 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7373 signature);
7374 }
7375
b0c7bfa9
DE
7376 return dwo_unit;
7377}
7378
a2ce51a0 7379/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7380 See it for a description of the parameters.
fcd3b13d 7381 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7382
7383static void
6aa5f3a6
DE
7384init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7385 int use_existing_cu, int keep,
a2ce51a0
DE
7386 die_reader_func_ftype *die_reader_func,
7387 void *data)
7388{
fcd3b13d 7389 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7390 struct signatured_type *sig_type;
a2ce51a0
DE
7391 struct die_reader_specs reader;
7392 const gdb_byte *info_ptr;
7393 struct die_info *comp_unit_die;
7394 int has_children;
ed2dc618 7395 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7396
7397 /* Verify we can do the following downcast, and that we have the
7398 data we need. */
7399 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7400 sig_type = (struct signatured_type *) this_cu;
7401 gdb_assert (sig_type->dwo_unit != NULL);
7402
6aa5f3a6
DE
7403 if (use_existing_cu && this_cu->cu != NULL)
7404 {
7405 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7406 /* There's no need to do the rereading_dwo_cu handling that
7407 init_cutu_and_read_dies does since we don't read the stub. */
7408 }
7409 else
7410 {
7411 /* If !use_existing_cu, this_cu->cu must be NULL. */
7412 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7413 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7414 }
7415
7416 /* A future optimization, if needed, would be to use an existing
7417 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7418 could share abbrev tables. */
a2ce51a0 7419
685af9cd
TT
7420 /* The abbreviation table used by READER, this must live at least as long as
7421 READER. */
7422 abbrev_table_up dwo_abbrev_table;
7423
a2ce51a0 7424 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7425 NULL /* stub_comp_unit_die */,
7426 sig_type->dwo_unit->dwo_file->comp_dir,
7427 &reader, &info_ptr,
685af9cd
TT
7428 &comp_unit_die, &has_children,
7429 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7430 {
7431 /* Dummy die. */
a2ce51a0
DE
7432 return;
7433 }
7434
7435 /* All the "real" work is done here. */
7436 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7437
6aa5f3a6 7438 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7439 but the alternative is making the latter more complex.
7440 This function is only for the special case of using DWO files directly:
7441 no point in overly complicating the general case just to handle this. */
fcd3b13d 7442 if (new_cu != NULL && keep)
a2ce51a0 7443 {
fcd3b13d
SM
7444 /* Link this CU into read_in_chain. */
7445 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7446 dwarf2_per_objfile->read_in_chain = this_cu;
7447 /* The chain owns it now. */
7448 new_cu.release ();
a2ce51a0 7449 }
a2ce51a0
DE
7450}
7451
fd820528 7452/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7453 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7454
f4dc4d17
DE
7455 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7456 Otherwise the table specified in the comp unit header is read in and used.
7457 This is an optimization for when we already have the abbrev table.
7458
dee91e82
DE
7459 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7460 Otherwise, a new CU is allocated with xmalloc.
7461
7462 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7463 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7464
7465 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7466 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7467
70221824 7468static void
fd820528 7469init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7470 struct abbrev_table *abbrev_table,
fd820528 7471 int use_existing_cu, int keep,
58f0c718 7472 bool skip_partial,
fd820528
DE
7473 die_reader_func_ftype *die_reader_func,
7474 void *data)
c906108c 7475{
ed2dc618 7476 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7477 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7478 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7479 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7480 struct dwarf2_cu *cu;
d521ce57 7481 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7482 struct die_reader_specs reader;
d85a05f0 7483 struct die_info *comp_unit_die;
dee91e82 7484 int has_children;
d85a05f0 7485 struct attribute *attr;
dee91e82 7486 struct signatured_type *sig_type = NULL;
4bdcc0c1 7487 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7488 /* Non-zero if CU currently points to a DWO file and we need to
7489 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7490 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7491 int rereading_dwo_cu = 0;
c906108c 7492
b4f54984 7493 if (dwarf_die_debug)
9d8780f0 7494 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7495 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7496 sect_offset_str (this_cu->sect_off));
09406207 7497
dee91e82
DE
7498 if (use_existing_cu)
7499 gdb_assert (keep);
23745b47 7500
a2ce51a0
DE
7501 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7502 file (instead of going through the stub), short-circuit all of this. */
7503 if (this_cu->reading_dwo_directly)
7504 {
7505 /* Narrow down the scope of possibilities to have to understand. */
7506 gdb_assert (this_cu->is_debug_types);
7507 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7508 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7509 die_reader_func, data);
a2ce51a0
DE
7510 return;
7511 }
7512
dee91e82
DE
7513 /* This is cheap if the section is already read in. */
7514 dwarf2_read_section (objfile, section);
7515
9c541725 7516 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7517
7518 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7519
fcd3b13d 7520 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7521 if (use_existing_cu && this_cu->cu != NULL)
7522 {
7523 cu = this_cu->cu;
42e7ad6c
DE
7524 /* If this CU is from a DWO file we need to start over, we need to
7525 refetch the attributes from the skeleton CU.
7526 This could be optimized by retrieving those attributes from when we
7527 were here the first time: the previous comp_unit_die was stored in
7528 comp_unit_obstack. But there's no data yet that we need this
7529 optimization. */
7530 if (cu->dwo_unit != NULL)
7531 rereading_dwo_cu = 1;
dee91e82
DE
7532 }
7533 else
7534 {
7535 /* If !use_existing_cu, this_cu->cu must be NULL. */
7536 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7537 new_cu.reset (new dwarf2_cu (this_cu));
7538 cu = new_cu.get ();
42e7ad6c 7539 }
dee91e82 7540
b0c7bfa9 7541 /* Get the header. */
9c541725 7542 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7543 {
7544 /* We already have the header, there's no need to read it in again. */
9c541725 7545 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7546 }
7547 else
7548 {
3019eac3 7549 if (this_cu->is_debug_types)
dee91e82 7550 {
ed2dc618
SM
7551 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7552 &cu->header, section,
4bdcc0c1 7553 abbrev_section, info_ptr,
43988095 7554 rcuh_kind::TYPE);
dee91e82 7555
42e7ad6c
DE
7556 /* Since per_cu is the first member of struct signatured_type,
7557 we can go from a pointer to one to a pointer to the other. */
7558 sig_type = (struct signatured_type *) this_cu;
43988095 7559 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7560 gdb_assert (sig_type->type_offset_in_tu
7561 == cu->header.type_cu_offset_in_tu);
7562 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7563
42e7ad6c
DE
7564 /* LENGTH has not been set yet for type units if we're
7565 using .gdb_index. */
1ce1cefd 7566 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7567
7568 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7569 sig_type->type_offset_in_section =
7570 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7571
7572 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7573 }
7574 else
7575 {
ed2dc618
SM
7576 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7577 &cu->header, section,
4bdcc0c1 7578 abbrev_section,
43988095
JK
7579 info_ptr,
7580 rcuh_kind::COMPILE);
dee91e82 7581
9c541725 7582 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7583 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7584 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7585 }
7586 }
10b3939b 7587
6caca83c 7588 /* Skip dummy compilation units. */
dee91e82 7589 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7590 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7591 return;
6caca83c 7592
433df2d4
DE
7593 /* If we don't have them yet, read the abbrevs for this compilation unit.
7594 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7595 done (own the table through ABBREV_TABLE_HOLDER). */
7596 abbrev_table_up abbrev_table_holder;
f4dc4d17 7597 if (abbrev_table != NULL)
685af9cd
TT
7598 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7599 else
f4dc4d17 7600 {
685af9cd
TT
7601 abbrev_table_holder
7602 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7603 cu->header.abbrev_sect_off);
7604 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7605 }
af703f96 7606
dee91e82 7607 /* Read the top level CU/TU die. */
685af9cd 7608 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7609 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7610
58f0c718
TT
7611 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7612 return;
7613
b0c7bfa9 7614 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7615 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7616 table from the DWO file and pass the ownership over to us. It will be
7617 referenced from READER, so we must make sure to free it after we're done
7618 with READER.
7619
b0c7bfa9
DE
7620 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7621 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7622 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7623 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7624 if (attr)
7625 {
3019eac3 7626 struct dwo_unit *dwo_unit;
b0c7bfa9 7627 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7628
7629 if (has_children)
6a506a2d 7630 {
b98664d3 7631 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7632 " has children (offset %s) [in module %s]"),
7633 sect_offset_str (this_cu->sect_off),
7634 bfd_get_filename (abfd));
6a506a2d 7635 }
b0c7bfa9 7636 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7637 if (dwo_unit != NULL)
3019eac3 7638 {
6a506a2d 7639 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7640 comp_unit_die, NULL,
6a506a2d 7641 &reader, &info_ptr,
685af9cd
TT
7642 &dwo_comp_unit_die, &has_children,
7643 &dwo_abbrev_table) == 0)
6a506a2d
DE
7644 {
7645 /* Dummy die. */
6a506a2d
DE
7646 return;
7647 }
7648 comp_unit_die = dwo_comp_unit_die;
7649 }
7650 else
7651 {
7652 /* Yikes, we couldn't find the rest of the DIE, we only have
7653 the stub. A complaint has already been logged. There's
7654 not much more we can do except pass on the stub DIE to
7655 die_reader_func. We don't want to throw an error on bad
7656 debug info. */
3019eac3
DE
7657 }
7658 }
7659
b0c7bfa9 7660 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7661 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7662
b0c7bfa9 7663 /* Done, clean up. */
fcd3b13d 7664 if (new_cu != NULL && keep)
348e048f 7665 {
fcd3b13d
SM
7666 /* Link this CU into read_in_chain. */
7667 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7668 dwarf2_per_objfile->read_in_chain = this_cu;
7669 /* The chain owns it now. */
7670 new_cu.release ();
348e048f 7671 }
dee91e82
DE
7672}
7673
33e80786
DE
7674/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7675 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7676 to have already done the lookup to find the DWO file).
dee91e82
DE
7677
7678 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7679 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7680
7681 We fill in THIS_CU->length.
7682
7683 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7684 linker) then DIE_READER_FUNC will not get called.
7685
7686 THIS_CU->cu is always freed when done.
3019eac3
DE
7687 This is done in order to not leave THIS_CU->cu in a state where we have
7688 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7689
7690static void
7691init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7692 struct dwo_file *dwo_file,
dee91e82
DE
7693 die_reader_func_ftype *die_reader_func,
7694 void *data)
7695{
ed2dc618 7696 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7697 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7698 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7699 bfd *abfd = get_section_bfd_owner (section);
33e80786 7700 struct dwarf2_section_info *abbrev_section;
d521ce57 7701 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7702 struct die_reader_specs reader;
dee91e82
DE
7703 struct die_info *comp_unit_die;
7704 int has_children;
7705
b4f54984 7706 if (dwarf_die_debug)
9d8780f0 7707 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7708 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7709 sect_offset_str (this_cu->sect_off));
09406207 7710
dee91e82
DE
7711 gdb_assert (this_cu->cu == NULL);
7712
33e80786
DE
7713 abbrev_section = (dwo_file != NULL
7714 ? &dwo_file->sections.abbrev
7715 : get_abbrev_section_for_cu (this_cu));
7716
dee91e82
DE
7717 /* This is cheap if the section is already read in. */
7718 dwarf2_read_section (objfile, section);
7719
fcd3b13d 7720 struct dwarf2_cu cu (this_cu);
dee91e82 7721
9c541725 7722 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7723 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7724 &cu.header, section,
4bdcc0c1 7725 abbrev_section, info_ptr,
43988095
JK
7726 (this_cu->is_debug_types
7727 ? rcuh_kind::TYPE
7728 : rcuh_kind::COMPILE));
dee91e82 7729
1ce1cefd 7730 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7731
7732 /* Skip dummy compilation units. */
7733 if (info_ptr >= begin_info_ptr + this_cu->length
7734 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7735 return;
72bf9492 7736
685af9cd
TT
7737 abbrev_table_up abbrev_table
7738 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7739 cu.header.abbrev_sect_off);
dee91e82 7740
685af9cd 7741 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7742 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7743
7744 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7745}
7746
3019eac3
DE
7747/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7748 does not lookup the specified DWO file.
7749 This cannot be used to read DWO files.
dee91e82
DE
7750
7751 THIS_CU->cu is always freed when done.
3019eac3
DE
7752 This is done in order to not leave THIS_CU->cu in a state where we have
7753 to care whether it refers to the "main" CU or the DWO CU.
7754 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7755
7756static void
7757init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7758 die_reader_func_ftype *die_reader_func,
7759 void *data)
7760{
33e80786 7761 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7762}
0018ea6f
DE
7763\f
7764/* Type Unit Groups.
dee91e82 7765
0018ea6f
DE
7766 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7767 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7768 so that all types coming from the same compilation (.o file) are grouped
7769 together. A future step could be to put the types in the same symtab as
7770 the CU the types ultimately came from. */
ff013f42 7771
f4dc4d17
DE
7772static hashval_t
7773hash_type_unit_group (const void *item)
7774{
9a3c8263
SM
7775 const struct type_unit_group *tu_group
7776 = (const struct type_unit_group *) item;
f4dc4d17 7777
094b34ac 7778 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7779}
348e048f
DE
7780
7781static int
f4dc4d17 7782eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7783{
9a3c8263
SM
7784 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7785 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7786
094b34ac 7787 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7788}
348e048f 7789
f4dc4d17
DE
7790/* Allocate a hash table for type unit groups. */
7791
7792static htab_t
ed2dc618 7793allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7794{
7795 return htab_create_alloc_ex (3,
7796 hash_type_unit_group,
7797 eq_type_unit_group,
7798 NULL,
ed2dc618 7799 &objfile->objfile_obstack,
f4dc4d17
DE
7800 hashtab_obstack_allocate,
7801 dummy_obstack_deallocate);
7802}
dee91e82 7803
f4dc4d17
DE
7804/* Type units that don't have DW_AT_stmt_list are grouped into their own
7805 partial symtabs. We combine several TUs per psymtab to not let the size
7806 of any one psymtab grow too big. */
7807#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7808#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7809
094b34ac 7810/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7811 Create the type_unit_group object used to hold one or more TUs. */
7812
7813static struct type_unit_group *
094b34ac 7814create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7815{
518817b3
SM
7816 struct dwarf2_per_objfile *dwarf2_per_objfile
7817 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7818 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7819 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7820 struct type_unit_group *tu_group;
f4dc4d17
DE
7821
7822 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7823 struct type_unit_group);
094b34ac 7824 per_cu = &tu_group->per_cu;
518817b3 7825 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7826
094b34ac
DE
7827 if (dwarf2_per_objfile->using_index)
7828 {
7829 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7830 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7831 }
7832 else
7833 {
9c541725 7834 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7835 struct partial_symtab *pst;
528e1572 7836 std::string name;
094b34ac
DE
7837
7838 /* Give the symtab a useful name for debug purposes. */
7839 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7840 name = string_printf ("<type_units_%d>",
7841 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7842 else
528e1572 7843 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7844
528e1572 7845 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7846 pst->anonymous = 1;
094b34ac 7847 }
f4dc4d17 7848
094b34ac 7849 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7850 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7851
7852 return tu_group;
7853}
7854
094b34ac
DE
7855/* Look up the type_unit_group for type unit CU, and create it if necessary.
7856 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7857
7858static struct type_unit_group *
ff39bb5e 7859get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7860{
518817b3
SM
7861 struct dwarf2_per_objfile *dwarf2_per_objfile
7862 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7863 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7864 struct type_unit_group *tu_group;
7865 void **slot;
7866 unsigned int line_offset;
7867 struct type_unit_group type_unit_group_for_lookup;
7868
7869 if (dwarf2_per_objfile->type_unit_groups == NULL)
7870 {
7871 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7872 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7873 }
7874
7875 /* Do we need to create a new group, or can we use an existing one? */
7876
7877 if (stmt_list)
7878 {
7879 line_offset = DW_UNSND (stmt_list);
7880 ++tu_stats->nr_symtab_sharers;
7881 }
7882 else
7883 {
7884 /* Ugh, no stmt_list. Rare, but we have to handle it.
7885 We can do various things here like create one group per TU or
7886 spread them over multiple groups to split up the expansion work.
7887 To avoid worst case scenarios (too many groups or too large groups)
7888 we, umm, group them in bunches. */
7889 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7890 | (tu_stats->nr_stmt_less_type_units
7891 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7892 ++tu_stats->nr_stmt_less_type_units;
7893 }
7894
094b34ac 7895 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7896 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7897 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7898 &type_unit_group_for_lookup, INSERT);
7899 if (*slot != NULL)
7900 {
9a3c8263 7901 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7902 gdb_assert (tu_group != NULL);
7903 }
7904 else
7905 {
9c541725 7906 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7907 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7908 *slot = tu_group;
7909 ++tu_stats->nr_symtabs;
7910 }
7911
7912 return tu_group;
7913}
0018ea6f
DE
7914\f
7915/* Partial symbol tables. */
7916
7917/* Create a psymtab named NAME and assign it to PER_CU.
7918
7919 The caller must fill in the following details:
7920 dirname, textlow, texthigh. */
7921
7922static struct partial_symtab *
7923create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7924{
e3b94546 7925 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7926 struct partial_symtab *pst;
7927
939652a5 7928 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7929
7930 pst->psymtabs_addrmap_supported = 1;
7931
7932 /* This is the glue that links PST into GDB's symbol API. */
7933 pst->read_symtab_private = per_cu;
7934 pst->read_symtab = dwarf2_read_symtab;
7935 per_cu->v.psymtab = pst;
7936
7937 return pst;
7938}
7939
b93601f3
TT
7940/* The DATA object passed to process_psymtab_comp_unit_reader has this
7941 type. */
7942
7943struct process_psymtab_comp_unit_data
7944{
7945 /* True if we are reading a DW_TAG_partial_unit. */
7946
7947 int want_partial_unit;
7948
7949 /* The "pretend" language that is used if the CU doesn't declare a
7950 language. */
7951
7952 enum language pretend_language;
7953};
7954
0018ea6f
DE
7955/* die_reader_func for process_psymtab_comp_unit. */
7956
7957static void
7958process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7959 const gdb_byte *info_ptr,
0018ea6f
DE
7960 struct die_info *comp_unit_die,
7961 int has_children,
7962 void *data)
7963{
7964 struct dwarf2_cu *cu = reader->cu;
518817b3 7965 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7966 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7967 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7968 CORE_ADDR baseaddr;
7969 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7970 struct partial_symtab *pst;
3a2b436a 7971 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7972 const char *filename;
9a3c8263
SM
7973 struct process_psymtab_comp_unit_data *info
7974 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7975
b93601f3 7976 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7977 return;
7978
7979 gdb_assert (! per_cu->is_debug_types);
7980
b93601f3 7981 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 7982
0018ea6f 7983 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7984 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7985 if (filename == NULL)
0018ea6f 7986 filename = "";
0018ea6f
DE
7987
7988 pst = create_partial_symtab (per_cu, filename);
7989
7990 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7991 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7992
7993 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7994
7995 dwarf2_find_base_address (comp_unit_die, cu);
7996
7997 /* Possibly set the default values of LOWPC and HIGHPC from
7998 `DW_AT_ranges'. */
3a2b436a
JK
7999 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8000 &best_highpc, cu, pst);
8001 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8002 {
8003 CORE_ADDR low
8004 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8005 - baseaddr);
8006 CORE_ADDR high
8007 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8008 - baseaddr - 1);
8009 /* Store the contiguous range if it is not empty; it can be
8010 empty for CUs with no code. */
d320c2b5
TT
8011 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8012 low, high, pst);
79748972 8013 }
0018ea6f
DE
8014
8015 /* Check if comp unit has_children.
8016 If so, read the rest of the partial symbols from this comp unit.
8017 If not, there's no more debug_info for this comp unit. */
8018 if (has_children)
8019 {
8020 struct partial_die_info *first_die;
8021 CORE_ADDR lowpc, highpc;
8022
8023 lowpc = ((CORE_ADDR) -1);
8024 highpc = ((CORE_ADDR) 0);
8025
8026 first_die = load_partial_dies (reader, info_ptr, 1);
8027
8028 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8029 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8030
8031 /* If we didn't find a lowpc, set it to highpc to avoid
8032 complaints from `maint check'. */
8033 if (lowpc == ((CORE_ADDR) -1))
8034 lowpc = highpc;
8035
8036 /* If the compilation unit didn't have an explicit address range,
8037 then use the information extracted from its child dies. */
e385593e 8038 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8039 {
8040 best_lowpc = lowpc;
8041 best_highpc = highpc;
8042 }
8043 }
4ae976d1 8044 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8045 best_lowpc + baseaddr)
8046 - baseaddr);
4ae976d1 8047 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8048 best_highpc + baseaddr)
8049 - baseaddr);
0018ea6f 8050
8763cede 8051 end_psymtab_common (objfile, pst);
0018ea6f
DE
8052
8053 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8054 {
8055 int i;
8056 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8057 struct dwarf2_per_cu_data *iter;
8058
8059 /* Fill in 'dependencies' here; we fill in 'users' in a
8060 post-pass. */
8061 pst->number_of_dependencies = len;
a9342b62
TT
8062 pst->dependencies
8063 = objfile->partial_symtabs->allocate_dependencies (len);
0018ea6f
DE
8064 for (i = 0;
8065 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8066 i, iter);
8067 ++i)
8068 pst->dependencies[i] = iter->v.psymtab;
8069
8070 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8071 }
8072
8073 /* Get the list of files included in the current compilation unit,
8074 and build a psymtab for each of them. */
8075 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8076
b4f54984 8077 if (dwarf_read_debug)
b926417a
TT
8078 fprintf_unfiltered (gdb_stdlog,
8079 "Psymtab for %s unit @%s: %s - %s"
8080 ", %d global, %d static syms\n",
8081 per_cu->is_debug_types ? "type" : "comp",
8082 sect_offset_str (per_cu->sect_off),
8083 paddress (gdbarch, pst->text_low (objfile)),
8084 paddress (gdbarch, pst->text_high (objfile)),
8085 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8086}
8087
8088/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8089 Process compilation unit THIS_CU for a psymtab. */
8090
8091static void
8092process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8093 int want_partial_unit,
8094 enum language pretend_language)
0018ea6f
DE
8095{
8096 /* If this compilation unit was already read in, free the
8097 cached copy in order to read it in again. This is
8098 necessary because we skipped some symbols when we first
8099 read in the compilation unit (see load_partial_dies).
8100 This problem could be avoided, but the benefit is unclear. */
8101 if (this_cu->cu != NULL)
8102 free_one_cached_comp_unit (this_cu);
8103
f1902523 8104 if (this_cu->is_debug_types)
58f0c718
TT
8105 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8106 build_type_psymtabs_reader, NULL);
f1902523
JK
8107 else
8108 {
8109 process_psymtab_comp_unit_data info;
8110 info.want_partial_unit = want_partial_unit;
8111 info.pretend_language = pretend_language;
58f0c718 8112 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8113 process_psymtab_comp_unit_reader, &info);
8114 }
0018ea6f
DE
8115
8116 /* Age out any secondary CUs. */
ed2dc618 8117 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8118}
f4dc4d17
DE
8119
8120/* Reader function for build_type_psymtabs. */
8121
8122static void
8123build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8124 const gdb_byte *info_ptr,
f4dc4d17
DE
8125 struct die_info *type_unit_die,
8126 int has_children,
8127 void *data)
8128{
ed2dc618 8129 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8130 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8131 struct objfile *objfile = dwarf2_per_objfile->objfile;
8132 struct dwarf2_cu *cu = reader->cu;
8133 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8134 struct signatured_type *sig_type;
f4dc4d17
DE
8135 struct type_unit_group *tu_group;
8136 struct attribute *attr;
8137 struct partial_die_info *first_die;
8138 CORE_ADDR lowpc, highpc;
8139 struct partial_symtab *pst;
8140
8141 gdb_assert (data == NULL);
0186c6a7
DE
8142 gdb_assert (per_cu->is_debug_types);
8143 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8144
8145 if (! has_children)
8146 return;
8147
8148 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8149 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8150
0186c6a7 8151 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8152
8153 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8154 pst = create_partial_symtab (per_cu, "");
8155 pst->anonymous = 1;
8156
8157 first_die = load_partial_dies (reader, info_ptr, 1);
8158
8159 lowpc = (CORE_ADDR) -1;
8160 highpc = (CORE_ADDR) 0;
8161 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8162
8763cede 8163 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8164}
8165
73051182
DE
8166/* Struct used to sort TUs by their abbreviation table offset. */
8167
8168struct tu_abbrev_offset
8169{
b2bdb8cf
SM
8170 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8171 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8172 {}
8173
8174 signatured_type *sig_type;
73051182
DE
8175 sect_offset abbrev_offset;
8176};
8177
484cf504 8178/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8179
484cf504
TT
8180static bool
8181sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8182 const struct tu_abbrev_offset &b)
73051182 8183{
484cf504 8184 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8185}
8186
8187/* Efficiently read all the type units.
8188 This does the bulk of the work for build_type_psymtabs.
8189
8190 The efficiency is because we sort TUs by the abbrev table they use and
8191 only read each abbrev table once. In one program there are 200K TUs
8192 sharing 8K abbrev tables.
8193
8194 The main purpose of this function is to support building the
8195 dwarf2_per_objfile->type_unit_groups table.
8196 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8197 can collapse the search space by grouping them by stmt_list.
8198 The savings can be significant, in the same program from above the 200K TUs
8199 share 8K stmt_list tables.
8200
8201 FUNC is expected to call get_type_unit_group, which will create the
8202 struct type_unit_group if necessary and add it to
8203 dwarf2_per_objfile->type_unit_groups. */
8204
8205static void
ed2dc618 8206build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8207{
73051182 8208 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8209 abbrev_table_up abbrev_table;
73051182 8210 sect_offset abbrev_offset;
73051182
DE
8211
8212 /* It's up to the caller to not call us multiple times. */
8213 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8214
b2bdb8cf 8215 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8216 return;
8217
8218 /* TUs typically share abbrev tables, and there can be way more TUs than
8219 abbrev tables. Sort by abbrev table to reduce the number of times we
8220 read each abbrev table in.
8221 Alternatives are to punt or to maintain a cache of abbrev tables.
8222 This is simpler and efficient enough for now.
8223
8224 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8225 symtab to use). Typically TUs with the same abbrev offset have the same
8226 stmt_list value too so in practice this should work well.
8227
8228 The basic algorithm here is:
8229
8230 sort TUs by abbrev table
8231 for each TU with same abbrev table:
8232 read abbrev table if first user
8233 read TU top level DIE
8234 [IWBN if DWO skeletons had DW_AT_stmt_list]
8235 call FUNC */
8236
b4f54984 8237 if (dwarf_read_debug)
73051182
DE
8238 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8239
8240 /* Sort in a separate table to maintain the order of all_type_units
8241 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8242 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8243 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8244
8245 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8246 sorted_by_abbrev.emplace_back
8247 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8248 sig_type->per_cu.section,
8249 sig_type->per_cu.sect_off));
73051182 8250
484cf504
TT
8251 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8252 sort_tu_by_abbrev_offset);
73051182 8253
9c541725 8254 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8255
b2bdb8cf 8256 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8257 {
73051182
DE
8258 /* Switch to the next abbrev table if necessary. */
8259 if (abbrev_table == NULL
b2bdb8cf 8260 || tu.abbrev_offset != abbrev_offset)
73051182 8261 {
b2bdb8cf 8262 abbrev_offset = tu.abbrev_offset;
73051182 8263 abbrev_table =
ed2dc618
SM
8264 abbrev_table_read_table (dwarf2_per_objfile,
8265 &dwarf2_per_objfile->abbrev,
73051182
DE
8266 abbrev_offset);
8267 ++tu_stats->nr_uniq_abbrev_tables;
8268 }
8269
b2bdb8cf 8270 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8271 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8272 }
6aa5f3a6 8273}
73051182 8274
6aa5f3a6
DE
8275/* Print collected type unit statistics. */
8276
8277static void
ed2dc618 8278print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8279{
8280 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8281
8282 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8283 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8284 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8285 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8286 tu_stats->nr_uniq_abbrev_tables);
8287 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8288 tu_stats->nr_symtabs);
8289 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8290 tu_stats->nr_symtab_sharers);
8291 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8292 tu_stats->nr_stmt_less_type_units);
8293 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8294 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8295}
8296
f4dc4d17
DE
8297/* Traversal function for build_type_psymtabs. */
8298
8299static int
8300build_type_psymtab_dependencies (void **slot, void *info)
8301{
ed2dc618
SM
8302 struct dwarf2_per_objfile *dwarf2_per_objfile
8303 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8304 struct objfile *objfile = dwarf2_per_objfile->objfile;
8305 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8306 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8307 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8308 int len = VEC_length (sig_type_ptr, tu_group->tus);
8309 struct signatured_type *iter;
f4dc4d17
DE
8310 int i;
8311
8312 gdb_assert (len > 0);
0186c6a7 8313 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8314
8315 pst->number_of_dependencies = len;
a9342b62 8316 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
f4dc4d17 8317 for (i = 0;
0186c6a7 8318 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8319 ++i)
8320 {
0186c6a7
DE
8321 gdb_assert (iter->per_cu.is_debug_types);
8322 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8323 iter->type_unit_group = tu_group;
f4dc4d17
DE
8324 }
8325
0186c6a7 8326 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8327
8328 return 1;
8329}
8330
8331/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8332 Build partial symbol tables for the .debug_types comp-units. */
8333
8334static void
ed2dc618 8335build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8336{
ed2dc618 8337 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8338 return;
8339
ed2dc618 8340 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8341}
f4dc4d17 8342
6aa5f3a6
DE
8343/* Traversal function for process_skeletonless_type_unit.
8344 Read a TU in a DWO file and build partial symbols for it. */
8345
8346static int
8347process_skeletonless_type_unit (void **slot, void *info)
8348{
8349 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8350 struct dwarf2_per_objfile *dwarf2_per_objfile
8351 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8352 struct signatured_type find_entry, *entry;
8353
8354 /* If this TU doesn't exist in the global table, add it and read it in. */
8355
8356 if (dwarf2_per_objfile->signatured_types == NULL)
8357 {
8358 dwarf2_per_objfile->signatured_types
ed2dc618 8359 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8360 }
8361
8362 find_entry.signature = dwo_unit->signature;
8363 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8364 INSERT);
8365 /* If we've already seen this type there's nothing to do. What's happening
8366 is we're doing our own version of comdat-folding here. */
8367 if (*slot != NULL)
8368 return 1;
8369
8370 /* This does the job that create_all_type_units would have done for
8371 this TU. */
ed2dc618
SM
8372 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8373 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8374 *slot = entry;
8375
8376 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8377 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8378 build_type_psymtabs_reader, NULL);
8379
8380 return 1;
8381}
8382
8383/* Traversal function for process_skeletonless_type_units. */
8384
8385static int
8386process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8387{
8388 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8389
8390 if (dwo_file->tus != NULL)
8391 {
8392 htab_traverse_noresize (dwo_file->tus,
8393 process_skeletonless_type_unit, info);
8394 }
8395
8396 return 1;
8397}
8398
8399/* Scan all TUs of DWO files, verifying we've processed them.
8400 This is needed in case a TU was emitted without its skeleton.
8401 Note: This can't be done until we know what all the DWO files are. */
8402
8403static void
ed2dc618 8404process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8405{
8406 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8407 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8408 && dwarf2_per_objfile->dwo_files != NULL)
8409 {
8410 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8411 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8412 dwarf2_per_objfile);
6aa5f3a6 8413 }
348e048f
DE
8414}
8415
ed2dc618 8416/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8417
8418static void
ed2dc618 8419set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8420{
b76e467d 8421 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8422 {
95554aad 8423 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8424
36586728
TT
8425 if (pst == NULL)
8426 continue;
8427
b76e467d 8428 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8429 {
8430 /* Set the 'user' field only if it is not already set. */
8431 if (pst->dependencies[j]->user == NULL)
8432 pst->dependencies[j]->user = pst;
8433 }
8434 }
8435}
8436
93311388
DE
8437/* Build the partial symbol table by doing a quick pass through the
8438 .debug_info and .debug_abbrev sections. */
72bf9492 8439
93311388 8440static void
ed2dc618 8441dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8442{
ed2dc618 8443 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8444
b4f54984 8445 if (dwarf_read_debug)
45cfd468
DE
8446 {
8447 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8448 objfile_name (objfile));
45cfd468
DE
8449 }
8450
98bfdba5
PA
8451 dwarf2_per_objfile->reading_partial_symbols = 1;
8452
be391dca 8453 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8454
93311388
DE
8455 /* Any cached compilation units will be linked by the per-objfile
8456 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8457 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8458
ed2dc618 8459 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8460
ed2dc618 8461 create_all_comp_units (dwarf2_per_objfile);
c906108c 8462
60606b2c
TT
8463 /* Create a temporary address map on a temporary obstack. We later
8464 copy this to the final obstack. */
8268c778 8465 auto_obstack temp_obstack;
791afaa2
TT
8466
8467 scoped_restore save_psymtabs_addrmap
d320c2b5 8468 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8469 addrmap_create_mutable (&temp_obstack));
72bf9492 8470
b76e467d
SM
8471 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8472 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8473
6aa5f3a6 8474 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8475 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8476
8477 /* Now that all TUs have been processed we can fill in the dependencies. */
8478 if (dwarf2_per_objfile->type_unit_groups != NULL)
8479 {
8480 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8481 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8482 }
8483
b4f54984 8484 if (dwarf_read_debug)
ed2dc618 8485 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8486
ed2dc618 8487 set_partial_user (dwarf2_per_objfile);
95554aad 8488
d320c2b5
TT
8489 objfile->partial_symtabs->psymtabs_addrmap
8490 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8491 objfile->partial_symtabs->obstack ());
791afaa2
TT
8492 /* At this point we want to keep the address map. */
8493 save_psymtabs_addrmap.release ();
ff013f42 8494
b4f54984 8495 if (dwarf_read_debug)
45cfd468 8496 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8497 objfile_name (objfile));
ae038cb0
DJ
8498}
8499
3019eac3 8500/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8501
8502static void
dee91e82 8503load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8504 const gdb_byte *info_ptr,
dee91e82
DE
8505 struct die_info *comp_unit_die,
8506 int has_children,
8507 void *data)
ae038cb0 8508{
dee91e82 8509 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8510
95554aad 8511 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8512
ae038cb0
DJ
8513 /* Check if comp unit has_children.
8514 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8515 If not, there's no more debug_info for this comp unit. */
d85a05f0 8516 if (has_children)
dee91e82
DE
8517 load_partial_dies (reader, info_ptr, 0);
8518}
98bfdba5 8519
dee91e82
DE
8520/* Load the partial DIEs for a secondary CU into memory.
8521 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8522
dee91e82
DE
8523static void
8524load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8525{
58f0c718 8526 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8527 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8528}
8529
ae038cb0 8530static void
ed2dc618 8531read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8532 struct dwarf2_section_info *section,
f1902523 8533 struct dwarf2_section_info *abbrev_section,
b76e467d 8534 unsigned int is_dwz)
ae038cb0 8535{
d521ce57 8536 const gdb_byte *info_ptr;
ed2dc618 8537 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8538
b4f54984 8539 if (dwarf_read_debug)
bf6af496 8540 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8541 get_section_name (section),
8542 get_section_file_name (section));
bf6af496 8543
36586728 8544 dwarf2_read_section (objfile, section);
ae038cb0 8545
36586728 8546 info_ptr = section->buffer;
6e70227d 8547
36586728 8548 while (info_ptr < section->buffer + section->size)
ae038cb0 8549 {
ae038cb0 8550 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8551
9c541725 8552 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8553
f1902523 8554 comp_unit_head cu_header;
ed2dc618
SM
8555 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8556 abbrev_section, info_ptr,
8557 rcuh_kind::COMPILE);
ae038cb0
DJ
8558
8559 /* Save the compilation unit for later lookup. */
f1902523
JK
8560 if (cu_header.unit_type != DW_UT_type)
8561 {
8562 this_cu = XOBNEW (&objfile->objfile_obstack,
8563 struct dwarf2_per_cu_data);
8564 memset (this_cu, 0, sizeof (*this_cu));
8565 }
8566 else
8567 {
8568 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8569 struct signatured_type);
8570 memset (sig_type, 0, sizeof (*sig_type));
8571 sig_type->signature = cu_header.signature;
8572 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8573 this_cu = &sig_type->per_cu;
8574 }
8575 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8576 this_cu->sect_off = sect_off;
f1902523 8577 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8578 this_cu->is_dwz = is_dwz;
e3b94546 8579 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8580 this_cu->section = section;
ae038cb0 8581
b76e467d 8582 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8583
8584 info_ptr = info_ptr + this_cu->length;
8585 }
36586728
TT
8586}
8587
8588/* Create a list of all compilation units in OBJFILE.
8589 This is only done for -readnow and building partial symtabs. */
8590
8591static void
ed2dc618 8592create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8593{
b76e467d 8594 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8595 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8596 &dwarf2_per_objfile->abbrev, 0);
36586728 8597
b76e467d 8598 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8599 if (dwz != NULL)
ed2dc618 8600 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8601 1);
c906108c
SS
8602}
8603
5734ee8b 8604/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8605 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8606 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8607 DW_AT_ranges). See the comments of add_partial_subprogram on how
8608 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8609
72bf9492
DJ
8610static void
8611scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8612 CORE_ADDR *highpc, int set_addrmap,
8613 struct dwarf2_cu *cu)
c906108c 8614{
72bf9492 8615 struct partial_die_info *pdi;
c906108c 8616
91c24f0a
DC
8617 /* Now, march along the PDI's, descending into ones which have
8618 interesting children but skipping the children of the other ones,
8619 until we reach the end of the compilation unit. */
c906108c 8620
72bf9492 8621 pdi = first_die;
91c24f0a 8622
72bf9492
DJ
8623 while (pdi != NULL)
8624 {
52356b79 8625 pdi->fixup (cu);
c906108c 8626
f55ee35c 8627 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8628 children, so we need to look at them. Ditto for anonymous
8629 enums. */
933c6fe4 8630
72bf9492 8631 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8632 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8633 || pdi->tag == DW_TAG_imported_unit
8634 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8635 {
72bf9492 8636 switch (pdi->tag)
c906108c
SS
8637 {
8638 case DW_TAG_subprogram:
b1dc1806 8639 case DW_TAG_inlined_subroutine:
cdc07690 8640 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8641 break;
72929c62 8642 case DW_TAG_constant:
c906108c
SS
8643 case DW_TAG_variable:
8644 case DW_TAG_typedef:
91c24f0a 8645 case DW_TAG_union_type:
72bf9492 8646 if (!pdi->is_declaration)
63d06c5c 8647 {
72bf9492 8648 add_partial_symbol (pdi, cu);
63d06c5c
DC
8649 }
8650 break;
c906108c 8651 case DW_TAG_class_type:
680b30c7 8652 case DW_TAG_interface_type:
c906108c 8653 case DW_TAG_structure_type:
72bf9492 8654 if (!pdi->is_declaration)
c906108c 8655 {
72bf9492 8656 add_partial_symbol (pdi, cu);
c906108c 8657 }
b7fee5a3
KS
8658 if ((cu->language == language_rust
8659 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8660 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8661 set_addrmap, cu);
c906108c 8662 break;
91c24f0a 8663 case DW_TAG_enumeration_type:
72bf9492
DJ
8664 if (!pdi->is_declaration)
8665 add_partial_enumeration (pdi, cu);
c906108c
SS
8666 break;
8667 case DW_TAG_base_type:
a02abb62 8668 case DW_TAG_subrange_type:
c906108c 8669 /* File scope base type definitions are added to the partial
c5aa993b 8670 symbol table. */
72bf9492 8671 add_partial_symbol (pdi, cu);
c906108c 8672 break;
d9fa45fe 8673 case DW_TAG_namespace:
cdc07690 8674 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8675 break;
5d7cb8df 8676 case DW_TAG_module:
cdc07690 8677 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8678 break;
95554aad
TT
8679 case DW_TAG_imported_unit:
8680 {
8681 struct dwarf2_per_cu_data *per_cu;
8682
f4dc4d17
DE
8683 /* For now we don't handle imported units in type units. */
8684 if (cu->per_cu->is_debug_types)
8685 {
8686 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8687 " supported in type units [in module %s]"),
518817b3 8688 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8689 }
8690
e3b94546
SM
8691 per_cu = dwarf2_find_containing_comp_unit
8692 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8693 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8694
8695 /* Go read the partial unit, if needed. */
8696 if (per_cu->v.psymtab == NULL)
b93601f3 8697 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8698
f4dc4d17 8699 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8700 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8701 }
8702 break;
74921315
KS
8703 case DW_TAG_imported_declaration:
8704 add_partial_symbol (pdi, cu);
8705 break;
c906108c
SS
8706 default:
8707 break;
8708 }
8709 }
8710
72bf9492
DJ
8711 /* If the die has a sibling, skip to the sibling. */
8712
8713 pdi = pdi->die_sibling;
8714 }
8715}
8716
8717/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8718
72bf9492 8719 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8720 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8721 Enumerators are an exception; they use the scope of their parent
8722 enumeration type, i.e. the name of the enumeration type is not
8723 prepended to the enumerator.
91c24f0a 8724
72bf9492
DJ
8725 There are two complexities. One is DW_AT_specification; in this
8726 case "parent" means the parent of the target of the specification,
8727 instead of the direct parent of the DIE. The other is compilers
8728 which do not emit DW_TAG_namespace; in this case we try to guess
8729 the fully qualified name of structure types from their members'
8730 linkage names. This must be done using the DIE's children rather
8731 than the children of any DW_AT_specification target. We only need
8732 to do this for structures at the top level, i.e. if the target of
8733 any DW_AT_specification (if any; otherwise the DIE itself) does not
8734 have a parent. */
8735
8736/* Compute the scope prefix associated with PDI's parent, in
8737 compilation unit CU. The result will be allocated on CU's
8738 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8739 field. NULL is returned if no prefix is necessary. */
15d034d0 8740static const char *
72bf9492
DJ
8741partial_die_parent_scope (struct partial_die_info *pdi,
8742 struct dwarf2_cu *cu)
8743{
15d034d0 8744 const char *grandparent_scope;
72bf9492 8745 struct partial_die_info *parent, *real_pdi;
91c24f0a 8746
72bf9492
DJ
8747 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8748 then this means the parent of the specification DIE. */
8749
8750 real_pdi = pdi;
72bf9492 8751 while (real_pdi->has_specification)
fb816e8b 8752 {
122cf0f2
AB
8753 auto res = find_partial_die (real_pdi->spec_offset,
8754 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8755 real_pdi = res.pdi;
8756 cu = res.cu;
8757 }
72bf9492
DJ
8758
8759 parent = real_pdi->die_parent;
8760 if (parent == NULL)
8761 return NULL;
8762
8763 if (parent->scope_set)
8764 return parent->scope;
8765
52356b79 8766 parent->fixup (cu);
72bf9492 8767
10b3939b 8768 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8769
acebe513
UW
8770 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8771 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8772 Work around this problem here. */
8773 if (cu->language == language_cplus
6e70227d 8774 && parent->tag == DW_TAG_namespace
acebe513
UW
8775 && strcmp (parent->name, "::") == 0
8776 && grandparent_scope == NULL)
8777 {
8778 parent->scope = NULL;
8779 parent->scope_set = 1;
8780 return NULL;
8781 }
8782
9c6c53f7
SA
8783 if (pdi->tag == DW_TAG_enumerator)
8784 /* Enumerators should not get the name of the enumeration as a prefix. */
8785 parent->scope = grandparent_scope;
8786 else if (parent->tag == DW_TAG_namespace
f55ee35c 8787 || parent->tag == DW_TAG_module
72bf9492
DJ
8788 || parent->tag == DW_TAG_structure_type
8789 || parent->tag == DW_TAG_class_type
680b30c7 8790 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8791 || parent->tag == DW_TAG_union_type
8792 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8793 {
8794 if (grandparent_scope == NULL)
8795 parent->scope = parent->name;
8796 else
3e43a32a
MS
8797 parent->scope = typename_concat (&cu->comp_unit_obstack,
8798 grandparent_scope,
f55ee35c 8799 parent->name, 0, cu);
72bf9492 8800 }
72bf9492
DJ
8801 else
8802 {
8803 /* FIXME drow/2004-04-01: What should we be doing with
8804 function-local names? For partial symbols, we should probably be
8805 ignoring them. */
fa9c3fa0
TT
8806 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8807 dwarf_tag_name (parent->tag),
8808 sect_offset_str (pdi->sect_off));
72bf9492 8809 parent->scope = grandparent_scope;
c906108c
SS
8810 }
8811
72bf9492
DJ
8812 parent->scope_set = 1;
8813 return parent->scope;
8814}
8815
8816/* Return the fully scoped name associated with PDI, from compilation unit
8817 CU. The result will be allocated with malloc. */
4568ecf9 8818
72bf9492
DJ
8819static char *
8820partial_die_full_name (struct partial_die_info *pdi,
8821 struct dwarf2_cu *cu)
8822{
15d034d0 8823 const char *parent_scope;
72bf9492 8824
98bfdba5
PA
8825 /* If this is a template instantiation, we can not work out the
8826 template arguments from partial DIEs. So, unfortunately, we have
8827 to go through the full DIEs. At least any work we do building
8828 types here will be reused if full symbols are loaded later. */
8829 if (pdi->has_template_arguments)
8830 {
52356b79 8831 pdi->fixup (cu);
98bfdba5
PA
8832
8833 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8834 {
8835 struct die_info *die;
8836 struct attribute attr;
8837 struct dwarf2_cu *ref_cu = cu;
8838
b64f50a1 8839 /* DW_FORM_ref_addr is using section offset. */
b4069958 8840 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8841 attr.form = DW_FORM_ref_addr;
9c541725 8842 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8843 die = follow_die_ref (NULL, &attr, &ref_cu);
8844
8845 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8846 }
8847 }
8848
72bf9492
DJ
8849 parent_scope = partial_die_parent_scope (pdi, cu);
8850 if (parent_scope == NULL)
8851 return NULL;
8852 else
f55ee35c 8853 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8854}
8855
8856static void
72bf9492 8857add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8858{
518817b3
SM
8859 struct dwarf2_per_objfile *dwarf2_per_objfile
8860 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8861 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8862 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8863 CORE_ADDR addr = 0;
15d034d0 8864 const char *actual_name = NULL;
e142c38c 8865 CORE_ADDR baseaddr;
15d034d0 8866 char *built_actual_name;
e142c38c
DJ
8867
8868 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8869
15d034d0
TT
8870 built_actual_name = partial_die_full_name (pdi, cu);
8871 if (built_actual_name != NULL)
8872 actual_name = built_actual_name;
63d06c5c 8873
72bf9492
DJ
8874 if (actual_name == NULL)
8875 actual_name = pdi->name;
8876
c906108c
SS
8877 switch (pdi->tag)
8878 {
b1dc1806 8879 case DW_TAG_inlined_subroutine:
c906108c 8880 case DW_TAG_subprogram:
79748972
TT
8881 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8882 - baseaddr);
2cfa0c8d 8883 if (pdi->is_external || cu->language == language_ada)
c906108c 8884 {
2cfa0c8d
JB
8885 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8886 of the global scope. But in Ada, we want to be able to access
8887 nested procedures globally. So all Ada subprograms are stored
8888 in the global scope. */
f47fb265 8889 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8890 built_actual_name != NULL,
f47fb265 8891 VAR_DOMAIN, LOC_BLOCK,
79748972 8892 SECT_OFF_TEXT (objfile),
75aedd27 8893 psymbol_placement::GLOBAL,
79748972
TT
8894 addr,
8895 cu->language, objfile);
c906108c
SS
8896 }
8897 else
8898 {
f47fb265 8899 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8900 built_actual_name != NULL,
f47fb265 8901 VAR_DOMAIN, LOC_BLOCK,
79748972 8902 SECT_OFF_TEXT (objfile),
75aedd27 8903 psymbol_placement::STATIC,
1762568f 8904 addr, cu->language, objfile);
c906108c 8905 }
0c1b455e
TT
8906
8907 if (pdi->main_subprogram && actual_name != NULL)
8908 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8909 break;
72929c62 8910 case DW_TAG_constant:
75aedd27
TT
8911 add_psymbol_to_list (actual_name, strlen (actual_name),
8912 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8913 -1, (pdi->is_external
8914 ? psymbol_placement::GLOBAL
8915 : psymbol_placement::STATIC),
8916 0, cu->language, objfile);
72929c62 8917 break;
c906108c 8918 case DW_TAG_variable:
95554aad
TT
8919 if (pdi->d.locdesc)
8920 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8921
95554aad 8922 if (pdi->d.locdesc
caac4577
JG
8923 && addr == 0
8924 && !dwarf2_per_objfile->has_section_at_zero)
8925 {
8926 /* A global or static variable may also have been stripped
8927 out by the linker if unused, in which case its address
8928 will be nullified; do not add such variables into partial
8929 symbol table then. */
8930 }
8931 else if (pdi->is_external)
c906108c
SS
8932 {
8933 /* Global Variable.
8934 Don't enter into the minimal symbol tables as there is
8935 a minimal symbol table entry from the ELF symbols already.
8936 Enter into partial symbol table if it has a location
8937 descriptor or a type.
8938 If the location descriptor is missing, new_symbol will create
8939 a LOC_UNRESOLVED symbol, the address of the variable will then
8940 be determined from the minimal symbol table whenever the variable
8941 is referenced.
8942 The address for the partial symbol table entry is not
8943 used by GDB, but it comes in handy for debugging partial symbol
8944 table building. */
8945
95554aad 8946 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8947 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8948 built_actual_name != NULL,
f47fb265 8949 VAR_DOMAIN, LOC_STATIC,
79748972 8950 SECT_OFF_TEXT (objfile),
75aedd27 8951 psymbol_placement::GLOBAL,
79748972 8952 addr, cu->language, objfile);
c906108c
SS
8953 }
8954 else
8955 {
ff908ebf
AW
8956 int has_loc = pdi->d.locdesc != NULL;
8957
8958 /* Static Variable. Skip symbols whose value we cannot know (those
8959 without location descriptors or constant values). */
8960 if (!has_loc && !pdi->has_const_value)
decbce07 8961 {
15d034d0 8962 xfree (built_actual_name);
decbce07
MS
8963 return;
8964 }
ff908ebf 8965
f47fb265 8966 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8967 built_actual_name != NULL,
f47fb265 8968 VAR_DOMAIN, LOC_STATIC,
79748972 8969 SECT_OFF_TEXT (objfile),
75aedd27 8970 psymbol_placement::STATIC,
79748972 8971 has_loc ? addr : 0,
f47fb265 8972 cu->language, objfile);
c906108c
SS
8973 }
8974 break;
8975 case DW_TAG_typedef:
8976 case DW_TAG_base_type:
a02abb62 8977 case DW_TAG_subrange_type:
38d518c9 8978 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8979 built_actual_name != NULL,
79748972 8980 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8981 psymbol_placement::STATIC,
1762568f 8982 0, cu->language, objfile);
c906108c 8983 break;
74921315 8984 case DW_TAG_imported_declaration:
72bf9492
DJ
8985 case DW_TAG_namespace:
8986 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8987 built_actual_name != NULL,
79748972 8988 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8989 psymbol_placement::GLOBAL,
1762568f 8990 0, cu->language, objfile);
72bf9492 8991 break;
530e8392 8992 case DW_TAG_module:
a5fd13a9
BH
8993 /* With Fortran 77 there might be a "BLOCK DATA" module
8994 available without any name. If so, we skip the module as it
8995 doesn't bring any value. */
8996 if (actual_name != nullptr)
8997 add_psymbol_to_list (actual_name, strlen (actual_name),
8998 built_actual_name != NULL,
8999 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9000 psymbol_placement::GLOBAL,
9001 0, cu->language, objfile);
530e8392 9002 break;
c906108c 9003 case DW_TAG_class_type:
680b30c7 9004 case DW_TAG_interface_type:
c906108c
SS
9005 case DW_TAG_structure_type:
9006 case DW_TAG_union_type:
9007 case DW_TAG_enumeration_type:
fa4028e9
JB
9008 /* Skip external references. The DWARF standard says in the section
9009 about "Structure, Union, and Class Type Entries": "An incomplete
9010 structure, union or class type is represented by a structure,
9011 union or class entry that does not have a byte size attribute
9012 and that has a DW_AT_declaration attribute." */
9013 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9014 {
15d034d0 9015 xfree (built_actual_name);
decbce07
MS
9016 return;
9017 }
fa4028e9 9018
63d06c5c
DC
9019 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9020 static vs. global. */
38d518c9 9021 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9022 built_actual_name != NULL,
79748972 9023 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9024 cu->language == language_cplus
75aedd27
TT
9025 ? psymbol_placement::GLOBAL
9026 : psymbol_placement::STATIC,
1762568f 9027 0, cu->language, objfile);
c906108c 9028
c906108c
SS
9029 break;
9030 case DW_TAG_enumerator:
38d518c9 9031 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9032 built_actual_name != NULL,
79748972 9033 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9034 cu->language == language_cplus
75aedd27
TT
9035 ? psymbol_placement::GLOBAL
9036 : psymbol_placement::STATIC,
1762568f 9037 0, cu->language, objfile);
c906108c
SS
9038 break;
9039 default:
9040 break;
9041 }
5c4e30ca 9042
15d034d0 9043 xfree (built_actual_name);
c906108c
SS
9044}
9045
5c4e30ca
DC
9046/* Read a partial die corresponding to a namespace; also, add a symbol
9047 corresponding to that namespace to the symbol table. NAMESPACE is
9048 the name of the enclosing namespace. */
91c24f0a 9049
72bf9492
DJ
9050static void
9051add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9052 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9053 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9054{
72bf9492 9055 /* Add a symbol for the namespace. */
e7c27a73 9056
72bf9492 9057 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9058
9059 /* Now scan partial symbols in that namespace. */
9060
91c24f0a 9061 if (pdi->has_children)
cdc07690 9062 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9063}
9064
5d7cb8df
JK
9065/* Read a partial die corresponding to a Fortran module. */
9066
9067static void
9068add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9069 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9070{
530e8392
KB
9071 /* Add a symbol for the namespace. */
9072
9073 add_partial_symbol (pdi, cu);
9074
f55ee35c 9075 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9076
9077 if (pdi->has_children)
cdc07690 9078 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9079}
9080
b1dc1806
XR
9081/* Read a partial die corresponding to a subprogram or an inlined
9082 subprogram and create a partial symbol for that subprogram.
9083 When the CU language allows it, this routine also defines a partial
9084 symbol for each nested subprogram that this subprogram contains.
9085 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9086 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9087
cdc07690
YQ
9088 PDI may also be a lexical block, in which case we simply search
9089 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9090 Again, this is only performed when the CU language allows this
9091 type of definitions. */
9092
9093static void
9094add_partial_subprogram (struct partial_die_info *pdi,
9095 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9096 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9097{
b1dc1806 9098 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9099 {
9100 if (pdi->has_pc_info)
9101 {
9102 if (pdi->lowpc < *lowpc)
9103 *lowpc = pdi->lowpc;
9104 if (pdi->highpc > *highpc)
9105 *highpc = pdi->highpc;
cdc07690 9106 if (set_addrmap)
5734ee8b 9107 {
518817b3 9108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9110 CORE_ADDR baseaddr;
b926417a
TT
9111 CORE_ADDR this_highpc;
9112 CORE_ADDR this_lowpc;
5734ee8b
DJ
9113
9114 baseaddr = ANOFFSET (objfile->section_offsets,
9115 SECT_OFF_TEXT (objfile));
b926417a
TT
9116 this_lowpc
9117 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9118 pdi->lowpc + baseaddr)
9119 - baseaddr);
9120 this_highpc
9121 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9122 pdi->highpc + baseaddr)
9123 - baseaddr);
d320c2b5 9124 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9125 this_lowpc, this_highpc - 1,
9291a0cd 9126 cu->per_cu->v.psymtab);
5734ee8b 9127 }
481860b3
GB
9128 }
9129
9130 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9131 {
bc30ff58 9132 if (!pdi->is_declaration)
e8d05480
JB
9133 /* Ignore subprogram DIEs that do not have a name, they are
9134 illegal. Do not emit a complaint at this point, we will
9135 do so when we convert this psymtab into a symtab. */
9136 if (pdi->name)
9137 add_partial_symbol (pdi, cu);
bc30ff58
JB
9138 }
9139 }
6e70227d 9140
bc30ff58
JB
9141 if (! pdi->has_children)
9142 return;
9143
9144 if (cu->language == language_ada)
9145 {
9146 pdi = pdi->die_child;
9147 while (pdi != NULL)
9148 {
52356b79 9149 pdi->fixup (cu);
bc30ff58 9150 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9151 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9152 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9153 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9154 pdi = pdi->die_sibling;
9155 }
9156 }
9157}
9158
91c24f0a
DC
9159/* Read a partial die corresponding to an enumeration type. */
9160
72bf9492
DJ
9161static void
9162add_partial_enumeration (struct partial_die_info *enum_pdi,
9163 struct dwarf2_cu *cu)
91c24f0a 9164{
72bf9492 9165 struct partial_die_info *pdi;
91c24f0a
DC
9166
9167 if (enum_pdi->name != NULL)
72bf9492
DJ
9168 add_partial_symbol (enum_pdi, cu);
9169
9170 pdi = enum_pdi->die_child;
9171 while (pdi)
91c24f0a 9172 {
72bf9492 9173 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9174 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9175 else
72bf9492
DJ
9176 add_partial_symbol (pdi, cu);
9177 pdi = pdi->die_sibling;
91c24f0a 9178 }
91c24f0a
DC
9179}
9180
6caca83c
CC
9181/* Return the initial uleb128 in the die at INFO_PTR. */
9182
9183static unsigned int
d521ce57 9184peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9185{
9186 unsigned int bytes_read;
9187
9188 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9189}
9190
685af9cd
TT
9191/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9192 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9193
4bb7a0a7
DJ
9194 Return the corresponding abbrev, or NULL if the number is zero (indicating
9195 an empty DIE). In either case *BYTES_READ will be set to the length of
9196 the initial number. */
9197
9198static struct abbrev_info *
685af9cd
TT
9199peek_die_abbrev (const die_reader_specs &reader,
9200 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9201{
685af9cd 9202 dwarf2_cu *cu = reader.cu;
518817b3 9203 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9204 unsigned int abbrev_number
9205 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9206
9207 if (abbrev_number == 0)
9208 return NULL;
9209
685af9cd 9210 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9211 if (!abbrev)
9212 {
422b9917 9213 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9214 " at offset %s [in module %s]"),
422b9917 9215 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9216 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9217 }
9218
9219 return abbrev;
9220}
9221
93311388
DE
9222/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9223 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9224 DIE. Any children of the skipped DIEs will also be skipped. */
9225
d521ce57
TT
9226static const gdb_byte *
9227skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9228{
4bb7a0a7
DJ
9229 while (1)
9230 {
685af9cd
TT
9231 unsigned int bytes_read;
9232 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9233
4bb7a0a7
DJ
9234 if (abbrev == NULL)
9235 return info_ptr + bytes_read;
9236 else
dee91e82 9237 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9238 }
9239}
9240
93311388
DE
9241/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9242 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9243 abbrev corresponding to that skipped uleb128 should be passed in
9244 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9245 children. */
9246
d521ce57
TT
9247static const gdb_byte *
9248skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9249 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9250{
9251 unsigned int bytes_read;
9252 struct attribute attr;
dee91e82
DE
9253 bfd *abfd = reader->abfd;
9254 struct dwarf2_cu *cu = reader->cu;
d521ce57 9255 const gdb_byte *buffer = reader->buffer;
f664829e 9256 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9257 unsigned int form, i;
9258
9259 for (i = 0; i < abbrev->num_attrs; i++)
9260 {
9261 /* The only abbrev we care about is DW_AT_sibling. */
9262 if (abbrev->attrs[i].name == DW_AT_sibling)
9263 {
dee91e82 9264 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9265 if (attr.form == DW_FORM_ref_addr)
b98664d3 9266 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9267 else
b9502d3f 9268 {
9c541725
PA
9269 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9270 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9271
9272 if (sibling_ptr < info_ptr)
b98664d3 9273 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9274 else if (sibling_ptr > reader->buffer_end)
9275 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9276 else
9277 return sibling_ptr;
9278 }
4bb7a0a7
DJ
9279 }
9280
9281 /* If it isn't DW_AT_sibling, skip this attribute. */
9282 form = abbrev->attrs[i].form;
9283 skip_attribute:
9284 switch (form)
9285 {
4bb7a0a7 9286 case DW_FORM_ref_addr:
ae411497
TT
9287 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9288 and later it is offset sized. */
9289 if (cu->header.version == 2)
9290 info_ptr += cu->header.addr_size;
9291 else
9292 info_ptr += cu->header.offset_size;
9293 break;
36586728
TT
9294 case DW_FORM_GNU_ref_alt:
9295 info_ptr += cu->header.offset_size;
9296 break;
ae411497 9297 case DW_FORM_addr:
4bb7a0a7
DJ
9298 info_ptr += cu->header.addr_size;
9299 break;
9300 case DW_FORM_data1:
9301 case DW_FORM_ref1:
9302 case DW_FORM_flag:
9303 info_ptr += 1;
9304 break;
2dc7f7b3 9305 case DW_FORM_flag_present:
43988095 9306 case DW_FORM_implicit_const:
2dc7f7b3 9307 break;
4bb7a0a7
DJ
9308 case DW_FORM_data2:
9309 case DW_FORM_ref2:
9310 info_ptr += 2;
9311 break;
9312 case DW_FORM_data4:
9313 case DW_FORM_ref4:
9314 info_ptr += 4;
9315 break;
9316 case DW_FORM_data8:
9317 case DW_FORM_ref8:
55f1336d 9318 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9319 info_ptr += 8;
9320 break;
0224619f
JK
9321 case DW_FORM_data16:
9322 info_ptr += 16;
9323 break;
4bb7a0a7 9324 case DW_FORM_string:
9b1c24c8 9325 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9326 info_ptr += bytes_read;
9327 break;
2dc7f7b3 9328 case DW_FORM_sec_offset:
4bb7a0a7 9329 case DW_FORM_strp:
36586728 9330 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9331 info_ptr += cu->header.offset_size;
9332 break;
2dc7f7b3 9333 case DW_FORM_exprloc:
4bb7a0a7
DJ
9334 case DW_FORM_block:
9335 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9336 info_ptr += bytes_read;
9337 break;
9338 case DW_FORM_block1:
9339 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9340 break;
9341 case DW_FORM_block2:
9342 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9343 break;
9344 case DW_FORM_block4:
9345 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9346 break;
336d760d 9347 case DW_FORM_addrx:
cf532bd1 9348 case DW_FORM_strx:
4bb7a0a7
DJ
9349 case DW_FORM_sdata:
9350 case DW_FORM_udata:
9351 case DW_FORM_ref_udata:
3019eac3
DE
9352 case DW_FORM_GNU_addr_index:
9353 case DW_FORM_GNU_str_index:
d521ce57 9354 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9355 break;
9356 case DW_FORM_indirect:
9357 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9358 info_ptr += bytes_read;
9359 /* We need to continue parsing from here, so just go back to
9360 the top. */
9361 goto skip_attribute;
9362
9363 default:
3e43a32a
MS
9364 error (_("Dwarf Error: Cannot handle %s "
9365 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9366 dwarf_form_name (form),
9367 bfd_get_filename (abfd));
9368 }
9369 }
9370
9371 if (abbrev->has_children)
dee91e82 9372 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9373 else
9374 return info_ptr;
9375}
9376
93311388 9377/* Locate ORIG_PDI's sibling.
dee91e82 9378 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9379
d521ce57 9380static const gdb_byte *
dee91e82
DE
9381locate_pdi_sibling (const struct die_reader_specs *reader,
9382 struct partial_die_info *orig_pdi,
d521ce57 9383 const gdb_byte *info_ptr)
91c24f0a
DC
9384{
9385 /* Do we know the sibling already? */
72bf9492 9386
91c24f0a
DC
9387 if (orig_pdi->sibling)
9388 return orig_pdi->sibling;
9389
9390 /* Are there any children to deal with? */
9391
9392 if (!orig_pdi->has_children)
9393 return info_ptr;
9394
4bb7a0a7 9395 /* Skip the children the long way. */
91c24f0a 9396
dee91e82 9397 return skip_children (reader, info_ptr);
91c24f0a
DC
9398}
9399
257e7a09 9400/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9401 not NULL. */
c906108c
SS
9402
9403static void
257e7a09
YQ
9404dwarf2_read_symtab (struct partial_symtab *self,
9405 struct objfile *objfile)
c906108c 9406{
ed2dc618
SM
9407 struct dwarf2_per_objfile *dwarf2_per_objfile
9408 = get_dwarf2_per_objfile (objfile);
9409
257e7a09 9410 if (self->readin)
c906108c 9411 {
442e4d9c 9412 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9413 self->filename);
442e4d9c
YQ
9414 }
9415 else
9416 {
9417 if (info_verbose)
c906108c 9418 {
442e4d9c 9419 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9420 self->filename);
442e4d9c 9421 gdb_flush (gdb_stdout);
c906108c 9422 }
c906108c 9423
442e4d9c
YQ
9424 /* If this psymtab is constructed from a debug-only objfile, the
9425 has_section_at_zero flag will not necessarily be correct. We
9426 can get the correct value for this flag by looking at the data
9427 associated with the (presumably stripped) associated objfile. */
9428 if (objfile->separate_debug_objfile_backlink)
9429 {
9430 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9431 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9432
442e4d9c
YQ
9433 dwarf2_per_objfile->has_section_at_zero
9434 = dpo_backlink->has_section_at_zero;
9435 }
b2ab525c 9436
442e4d9c 9437 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9438
257e7a09 9439 psymtab_to_symtab_1 (self);
c906108c 9440
442e4d9c
YQ
9441 /* Finish up the debug error message. */
9442 if (info_verbose)
9443 printf_filtered (_("done.\n"));
c906108c 9444 }
95554aad 9445
ed2dc618 9446 process_cu_includes (dwarf2_per_objfile);
c906108c 9447}
9cdd5dbd
DE
9448\f
9449/* Reading in full CUs. */
c906108c 9450
10b3939b
DJ
9451/* Add PER_CU to the queue. */
9452
9453static void
95554aad
TT
9454queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9455 enum language pretend_language)
10b3939b
DJ
9456{
9457 struct dwarf2_queue_item *item;
9458
9459 per_cu->queued = 1;
8d749320 9460 item = XNEW (struct dwarf2_queue_item);
10b3939b 9461 item->per_cu = per_cu;
95554aad 9462 item->pretend_language = pretend_language;
10b3939b
DJ
9463 item->next = NULL;
9464
9465 if (dwarf2_queue == NULL)
9466 dwarf2_queue = item;
9467 else
9468 dwarf2_queue_tail->next = item;
9469
9470 dwarf2_queue_tail = item;
9471}
9472
89e63ee4
DE
9473/* If PER_CU is not yet queued, add it to the queue.
9474 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9475 dependency.
0907af0c 9476 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9477 meaning either PER_CU is already queued or it is already loaded.
9478
9479 N.B. There is an invariant here that if a CU is queued then it is loaded.
9480 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9481
9482static int
89e63ee4 9483maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9484 struct dwarf2_per_cu_data *per_cu,
9485 enum language pretend_language)
9486{
9487 /* We may arrive here during partial symbol reading, if we need full
9488 DIEs to process an unusual case (e.g. template arguments). Do
9489 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9490 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9491 {
9492 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9493 return 1;
9494 return 0;
9495 }
9496
9497 /* Mark the dependence relation so that we don't flush PER_CU
9498 too early. */
89e63ee4
DE
9499 if (dependent_cu != NULL)
9500 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9501
9502 /* If it's already on the queue, we have nothing to do. */
9503 if (per_cu->queued)
9504 return 0;
9505
9506 /* If the compilation unit is already loaded, just mark it as
9507 used. */
9508 if (per_cu->cu != NULL)
9509 {
9510 per_cu->cu->last_used = 0;
9511 return 0;
9512 }
9513
9514 /* Add it to the queue. */
9515 queue_comp_unit (per_cu, pretend_language);
9516
9517 return 1;
9518}
9519
10b3939b
DJ
9520/* Process the queue. */
9521
9522static void
ed2dc618 9523process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9524{
9525 struct dwarf2_queue_item *item, *next_item;
9526
b4f54984 9527 if (dwarf_read_debug)
45cfd468
DE
9528 {
9529 fprintf_unfiltered (gdb_stdlog,
9530 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9531 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9532 }
9533
03dd20cc
DJ
9534 /* The queue starts out with one item, but following a DIE reference
9535 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9536 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9537 {
cc12ce38
DE
9538 if ((dwarf2_per_objfile->using_index
9539 ? !item->per_cu->v.quick->compunit_symtab
9540 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9541 /* Skip dummy CUs. */
9542 && item->per_cu->cu != NULL)
f4dc4d17
DE
9543 {
9544 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9545 unsigned int debug_print_threshold;
247f5c4f 9546 char buf[100];
f4dc4d17 9547
247f5c4f 9548 if (per_cu->is_debug_types)
f4dc4d17 9549 {
247f5c4f
DE
9550 struct signatured_type *sig_type =
9551 (struct signatured_type *) per_cu;
9552
9d8780f0 9553 sprintf (buf, "TU %s at offset %s",
73be47f5 9554 hex_string (sig_type->signature),
9d8780f0 9555 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9556 /* There can be 100s of TUs.
9557 Only print them in verbose mode. */
9558 debug_print_threshold = 2;
f4dc4d17 9559 }
247f5c4f 9560 else
73be47f5 9561 {
9d8780f0
SM
9562 sprintf (buf, "CU at offset %s",
9563 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9564 debug_print_threshold = 1;
9565 }
247f5c4f 9566
b4f54984 9567 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9568 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9569
9570 if (per_cu->is_debug_types)
9571 process_full_type_unit (per_cu, item->pretend_language);
9572 else
9573 process_full_comp_unit (per_cu, item->pretend_language);
9574
b4f54984 9575 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9576 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9577 }
10b3939b
DJ
9578
9579 item->per_cu->queued = 0;
9580 next_item = item->next;
9581 xfree (item);
9582 }
9583
9584 dwarf2_queue_tail = NULL;
45cfd468 9585
b4f54984 9586 if (dwarf_read_debug)
45cfd468
DE
9587 {
9588 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9589 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9590 }
10b3939b
DJ
9591}
9592
10b3939b
DJ
9593/* Read in full symbols for PST, and anything it depends on. */
9594
c906108c 9595static void
fba45db2 9596psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9597{
10b3939b 9598 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9599 int i;
9600
95554aad
TT
9601 if (pst->readin)
9602 return;
9603
aaa75496 9604 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9605 if (!pst->dependencies[i]->readin
9606 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9607 {
9608 /* Inform about additional files that need to be read in. */
9609 if (info_verbose)
9610 {
a3f17187 9611 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9612 fputs_filtered (" ", gdb_stdout);
9613 wrap_here ("");
9614 fputs_filtered ("and ", gdb_stdout);
9615 wrap_here ("");
9616 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9617 wrap_here (""); /* Flush output. */
aaa75496
JB
9618 gdb_flush (gdb_stdout);
9619 }
9620 psymtab_to_symtab_1 (pst->dependencies[i]);
9621 }
9622
9a3c8263 9623 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9624
9625 if (per_cu == NULL)
aaa75496
JB
9626 {
9627 /* It's an include file, no symbols to read for it.
9628 Everything is in the parent symtab. */
9629 pst->readin = 1;
9630 return;
9631 }
c906108c 9632
58f0c718 9633 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9634}
9635
dee91e82
DE
9636/* Trivial hash function for die_info: the hash value of a DIE
9637 is its offset in .debug_info for this objfile. */
10b3939b 9638
dee91e82
DE
9639static hashval_t
9640die_hash (const void *item)
10b3939b 9641{
9a3c8263 9642 const struct die_info *die = (const struct die_info *) item;
6502dd73 9643
9c541725 9644 return to_underlying (die->sect_off);
dee91e82 9645}
63d06c5c 9646
dee91e82
DE
9647/* Trivial comparison function for die_info structures: two DIEs
9648 are equal if they have the same offset. */
98bfdba5 9649
dee91e82
DE
9650static int
9651die_eq (const void *item_lhs, const void *item_rhs)
9652{
9a3c8263
SM
9653 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9654 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9655
9c541725 9656 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9657}
c906108c 9658
dee91e82
DE
9659/* die_reader_func for load_full_comp_unit.
9660 This is identical to read_signatured_type_reader,
9661 but is kept separate for now. */
c906108c 9662
dee91e82
DE
9663static void
9664load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9665 const gdb_byte *info_ptr,
dee91e82
DE
9666 struct die_info *comp_unit_die,
9667 int has_children,
9668 void *data)
9669{
9670 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9671 enum language *language_ptr = (enum language *) data;
6caca83c 9672
dee91e82
DE
9673 gdb_assert (cu->die_hash == NULL);
9674 cu->die_hash =
9675 htab_create_alloc_ex (cu->header.length / 12,
9676 die_hash,
9677 die_eq,
9678 NULL,
9679 &cu->comp_unit_obstack,
9680 hashtab_obstack_allocate,
9681 dummy_obstack_deallocate);
e142c38c 9682
dee91e82
DE
9683 if (has_children)
9684 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9685 &info_ptr, comp_unit_die);
9686 cu->dies = comp_unit_die;
9687 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9688
9689 /* We try not to read any attributes in this function, because not
9cdd5dbd 9690 all CUs needed for references have been loaded yet, and symbol
10b3939b 9691 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9692 or we won't be able to build types correctly.
9693 Similarly, if we do not read the producer, we can not apply
9694 producer-specific interpretation. */
95554aad 9695 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9696}
10b3939b 9697
dee91e82 9698/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9699
dee91e82 9700static void
95554aad 9701load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9702 bool skip_partial,
95554aad 9703 enum language pretend_language)
dee91e82 9704{
3019eac3 9705 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9706
58f0c718 9707 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9708 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9709}
9710
3da10d80
KS
9711/* Add a DIE to the delayed physname list. */
9712
9713static void
9714add_to_method_list (struct type *type, int fnfield_index, int index,
9715 const char *name, struct die_info *die,
9716 struct dwarf2_cu *cu)
9717{
9718 struct delayed_method_info mi;
9719 mi.type = type;
9720 mi.fnfield_index = fnfield_index;
9721 mi.index = index;
9722 mi.name = name;
9723 mi.die = die;
c89b44cd 9724 cu->method_list.push_back (mi);
3da10d80
KS
9725}
9726
3693fdb3
PA
9727/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9728 "const" / "volatile". If so, decrements LEN by the length of the
9729 modifier and return true. Otherwise return false. */
9730
9731template<size_t N>
9732static bool
9733check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9734{
9735 size_t mod_len = sizeof (mod) - 1;
9736 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9737 {
9738 len -= mod_len;
9739 return true;
9740 }
9741 return false;
9742}
9743
3da10d80
KS
9744/* Compute the physnames of any methods on the CU's method list.
9745
9746 The computation of method physnames is delayed in order to avoid the
9747 (bad) condition that one of the method's formal parameters is of an as yet
9748 incomplete type. */
9749
9750static void
9751compute_delayed_physnames (struct dwarf2_cu *cu)
9752{
3693fdb3 9753 /* Only C++ delays computing physnames. */
c89b44cd 9754 if (cu->method_list.empty ())
3693fdb3
PA
9755 return;
9756 gdb_assert (cu->language == language_cplus);
9757
52941706 9758 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9759 {
1d06ead6 9760 const char *physname;
3da10d80 9761 struct fn_fieldlist *fn_flp
c89b44cd
TT
9762 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9763 physname = dwarf2_physname (mi.name, mi.die, cu);
9764 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9765 = physname ? physname : "";
3693fdb3
PA
9766
9767 /* Since there's no tag to indicate whether a method is a
9768 const/volatile overload, extract that information out of the
9769 demangled name. */
9770 if (physname != NULL)
9771 {
9772 size_t len = strlen (physname);
9773
9774 while (1)
9775 {
9776 if (physname[len] == ')') /* shortcut */
9777 break;
9778 else if (check_modifier (physname, len, " const"))
c89b44cd 9779 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9780 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9781 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9782 else
9783 break;
9784 }
9785 }
3da10d80 9786 }
c89b44cd
TT
9787
9788 /* The list is no longer needed. */
9789 cu->method_list.clear ();
3da10d80
KS
9790}
9791
a766d390
DE
9792/* Go objects should be embedded in a DW_TAG_module DIE,
9793 and it's not clear if/how imported objects will appear.
9794 To keep Go support simple until that's worked out,
9795 go back through what we've read and create something usable.
9796 We could do this while processing each DIE, and feels kinda cleaner,
9797 but that way is more invasive.
9798 This is to, for example, allow the user to type "p var" or "b main"
9799 without having to specify the package name, and allow lookups
9800 of module.object to work in contexts that use the expression
9801 parser. */
9802
9803static void
9804fixup_go_packaging (struct dwarf2_cu *cu)
9805{
9806 char *package_name = NULL;
9807 struct pending *list;
9808 int i;
9809
c24bdb02 9810 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9811 list != NULL;
9812 list = list->next)
a766d390
DE
9813 {
9814 for (i = 0; i < list->nsyms; ++i)
9815 {
9816 struct symbol *sym = list->symbol[i];
9817
9818 if (SYMBOL_LANGUAGE (sym) == language_go
9819 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9820 {
9821 char *this_package_name = go_symbol_package_name (sym);
9822
9823 if (this_package_name == NULL)
9824 continue;
9825 if (package_name == NULL)
9826 package_name = this_package_name;
9827 else
9828 {
518817b3
SM
9829 struct objfile *objfile
9830 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9831 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9832 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9833 (symbol_symtab (sym) != NULL
9834 ? symtab_to_filename_for_display
9835 (symbol_symtab (sym))
e3b94546 9836 : objfile_name (objfile)),
a766d390
DE
9837 this_package_name, package_name);
9838 xfree (this_package_name);
9839 }
9840 }
9841 }
9842 }
9843
9844 if (package_name != NULL)
9845 {
518817b3 9846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9847 const char *saved_package_name
224c3ddb
SM
9848 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9849 package_name,
9850 strlen (package_name));
19f392bc
UW
9851 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9852 saved_package_name);
a766d390
DE
9853 struct symbol *sym;
9854
e623cf5d 9855 sym = allocate_symbol (objfile);
f85f34ed 9856 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9857 SYMBOL_SET_NAMES (sym, saved_package_name,
9858 strlen (saved_package_name), 0, objfile);
a766d390
DE
9859 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9860 e.g., "main" finds the "main" module and not C's main(). */
9861 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9862 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9863 SYMBOL_TYPE (sym) = type;
9864
c24bdb02 9865 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9866
9867 xfree (package_name);
9868 }
9869}
9870
c9317f21
TT
9871/* Allocate a fully-qualified name consisting of the two parts on the
9872 obstack. */
9873
9874static const char *
9875rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9876{
9877 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9878}
9879
9880/* A helper that allocates a struct discriminant_info to attach to a
9881 union type. */
9882
9883static struct discriminant_info *
9884alloc_discriminant_info (struct type *type, int discriminant_index,
9885 int default_index)
9886{
9887 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9888 gdb_assert (discriminant_index == -1
9889 || (discriminant_index >= 0
9890 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9891 gdb_assert (default_index == -1
c7b15a66 9892 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9893
9894 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9895
9896 struct discriminant_info *disc
9897 = ((struct discriminant_info *)
9898 TYPE_ZALLOC (type,
9899 offsetof (struct discriminant_info, discriminants)
9900 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9901 disc->default_index = default_index;
9902 disc->discriminant_index = discriminant_index;
9903
9904 struct dynamic_prop prop;
9905 prop.kind = PROP_UNDEFINED;
9906 prop.data.baton = disc;
9907
9908 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9909
9910 return disc;
9911}
9912
9913/* Some versions of rustc emitted enums in an unusual way.
9914
9915 Ordinary enums were emitted as unions. The first element of each
9916 structure in the union was named "RUST$ENUM$DISR". This element
9917 held the discriminant.
9918
9919 These versions of Rust also implemented the "non-zero"
9920 optimization. When the enum had two values, and one is empty and
9921 the other holds a pointer that cannot be zero, the pointer is used
9922 as the discriminant, with a zero value meaning the empty variant.
9923 Here, the union's first member is of the form
9924 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9925 where the fieldnos are the indices of the fields that should be
9926 traversed in order to find the field (which may be several fields deep)
9927 and the variantname is the name of the variant of the case when the
9928 field is zero.
9929
9930 This function recognizes whether TYPE is of one of these forms,
9931 and, if so, smashes it to be a variant type. */
9932
9933static void
9934quirk_rust_enum (struct type *type, struct objfile *objfile)
9935{
9936 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9937
9938 /* We don't need to deal with empty enums. */
9939 if (TYPE_NFIELDS (type) == 0)
9940 return;
9941
9942#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9943 if (TYPE_NFIELDS (type) == 1
9944 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9945 {
9946 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9947
9948 /* Decode the field name to find the offset of the
9949 discriminant. */
9950 ULONGEST bit_offset = 0;
9951 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9952 while (name[0] >= '0' && name[0] <= '9')
9953 {
9954 char *tail;
9955 unsigned long index = strtoul (name, &tail, 10);
9956 name = tail;
9957 if (*name != '$'
9958 || index >= TYPE_NFIELDS (field_type)
9959 || (TYPE_FIELD_LOC_KIND (field_type, index)
9960 != FIELD_LOC_KIND_BITPOS))
9961 {
b98664d3 9962 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9963 "[in module %s]"),
9964 TYPE_FIELD_NAME (type, 0),
9965 objfile_name (objfile));
9966 return;
9967 }
9968 ++name;
9969
9970 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9971 field_type = TYPE_FIELD_TYPE (field_type, index);
9972 }
9973
9974 /* Make a union to hold the variants. */
9975 struct type *union_type = alloc_type (objfile);
9976 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9977 TYPE_NFIELDS (union_type) = 3;
9978 TYPE_FIELDS (union_type)
9979 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9980 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9981 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9982
9983 /* Put the discriminant must at index 0. */
9984 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9985 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9986 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9987 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9988
9989 /* The order of fields doesn't really matter, so put the real
9990 field at index 1 and the data-less field at index 2. */
9991 struct discriminant_info *disc
9992 = alloc_discriminant_info (union_type, 0, 1);
9993 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9994 TYPE_FIELD_NAME (union_type, 1)
9995 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9996 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9997 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9998 TYPE_FIELD_NAME (union_type, 1));
9999
10000 const char *dataless_name
10001 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10002 name);
10003 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10004 dataless_name);
10005 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10006 /* NAME points into the original discriminant name, which
10007 already has the correct lifetime. */
10008 TYPE_FIELD_NAME (union_type, 2) = name;
10009 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10010 disc->discriminants[2] = 0;
10011
10012 /* Smash this type to be a structure type. We have to do this
10013 because the type has already been recorded. */
10014 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10015 TYPE_NFIELDS (type) = 1;
10016 TYPE_FIELDS (type)
10017 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10018
10019 /* Install the variant part. */
10020 TYPE_FIELD_TYPE (type, 0) = union_type;
10021 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10022 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10023 }
10024 else if (TYPE_NFIELDS (type) == 1)
10025 {
10026 /* We assume that a union with a single field is a univariant
10027 enum. */
10028 /* Smash this type to be a structure type. We have to do this
10029 because the type has already been recorded. */
10030 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10031
10032 /* Make a union to hold the variants. */
10033 struct type *union_type = alloc_type (objfile);
10034 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10035 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10036 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10037 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10038 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10039
10040 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10041 const char *variant_name
10042 = rust_last_path_segment (TYPE_NAME (field_type));
10043 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10044 TYPE_NAME (field_type)
10045 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10046 TYPE_NAME (type), variant_name);
c9317f21
TT
10047
10048 /* Install the union in the outer struct type. */
10049 TYPE_NFIELDS (type) = 1;
10050 TYPE_FIELDS (type)
10051 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10052 TYPE_FIELD_TYPE (type, 0) = union_type;
10053 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10054 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10055
10056 alloc_discriminant_info (union_type, -1, 0);
10057 }
10058 else
10059 {
10060 struct type *disr_type = nullptr;
10061 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10062 {
10063 disr_type = TYPE_FIELD_TYPE (type, i);
10064
a037790e
TT
10065 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10066 {
10067 /* All fields of a true enum will be structs. */
10068 return;
10069 }
10070 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10071 {
10072 /* Could be data-less variant, so keep going. */
a037790e 10073 disr_type = nullptr;
c9317f21
TT
10074 }
10075 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10076 "RUST$ENUM$DISR") != 0)
10077 {
10078 /* Not a Rust enum. */
10079 return;
10080 }
10081 else
10082 {
10083 /* Found one. */
10084 break;
10085 }
10086 }
10087
10088 /* If we got here without a discriminant, then it's probably
10089 just a union. */
10090 if (disr_type == nullptr)
10091 return;
10092
10093 /* Smash this type to be a structure type. We have to do this
10094 because the type has already been recorded. */
10095 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10096
10097 /* Make a union to hold the variants. */
10098 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10099 struct type *union_type = alloc_type (objfile);
10100 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10101 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10102 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10103 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10104 TYPE_FIELDS (union_type)
10105 = (struct field *) TYPE_ZALLOC (union_type,
10106 (TYPE_NFIELDS (union_type)
10107 * sizeof (struct field)));
10108
10109 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10110 TYPE_NFIELDS (type) * sizeof (struct field));
10111
10112 /* Install the discriminant at index 0 in the union. */
10113 TYPE_FIELD (union_type, 0) = *disr_field;
10114 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10115 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10116
10117 /* Install the union in the outer struct type. */
10118 TYPE_FIELD_TYPE (type, 0) = union_type;
10119 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10120 TYPE_NFIELDS (type) = 1;
10121
10122 /* Set the size and offset of the union type. */
10123 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10124
10125 /* We need a way to find the correct discriminant given a
10126 variant name. For convenience we build a map here. */
10127 struct type *enum_type = FIELD_TYPE (*disr_field);
10128 std::unordered_map<std::string, ULONGEST> discriminant_map;
10129 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10130 {
10131 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10132 {
10133 const char *name
10134 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10135 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10136 }
10137 }
10138
10139 int n_fields = TYPE_NFIELDS (union_type);
10140 struct discriminant_info *disc
10141 = alloc_discriminant_info (union_type, 0, -1);
10142 /* Skip the discriminant here. */
10143 for (int i = 1; i < n_fields; ++i)
10144 {
10145 /* Find the final word in the name of this variant's type.
10146 That name can be used to look up the correct
10147 discriminant. */
10148 const char *variant_name
10149 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10150 i)));
10151
10152 auto iter = discriminant_map.find (variant_name);
10153 if (iter != discriminant_map.end ())
10154 disc->discriminants[i] = iter->second;
10155
bedda9ac 10156 /* Remove the discriminant field, if it exists. */
c9317f21 10157 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10158 if (TYPE_NFIELDS (sub_type) > 0)
10159 {
10160 --TYPE_NFIELDS (sub_type);
10161 ++TYPE_FIELDS (sub_type);
10162 }
c9317f21
TT
10163 TYPE_FIELD_NAME (union_type, i) = variant_name;
10164 TYPE_NAME (sub_type)
10165 = rust_fully_qualify (&objfile->objfile_obstack,
10166 TYPE_NAME (type), variant_name);
10167 }
10168 }
10169}
10170
10171/* Rewrite some Rust unions to be structures with variants parts. */
10172
10173static void
10174rust_union_quirks (struct dwarf2_cu *cu)
10175{
10176 gdb_assert (cu->language == language_rust);
52941706
SM
10177 for (type *type_ : cu->rust_unions)
10178 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10179 /* We don't need this any more. */
10180 cu->rust_unions.clear ();
c9317f21
TT
10181}
10182
95554aad
TT
10183/* Return the symtab for PER_CU. This works properly regardless of
10184 whether we're using the index or psymtabs. */
10185
43f3e411
DE
10186static struct compunit_symtab *
10187get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10188{
ed2dc618 10189 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10190 ? per_cu->v.quick->compunit_symtab
10191 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10192}
10193
10194/* A helper function for computing the list of all symbol tables
10195 included by PER_CU. */
10196
10197static void
4c39bc03 10198recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10199 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10200 struct dwarf2_per_cu_data *per_cu,
43f3e411 10201 struct compunit_symtab *immediate_parent)
95554aad
TT
10202{
10203 void **slot;
10204 int ix;
43f3e411 10205 struct compunit_symtab *cust;
95554aad
TT
10206 struct dwarf2_per_cu_data *iter;
10207
10208 slot = htab_find_slot (all_children, per_cu, INSERT);
10209 if (*slot != NULL)
10210 {
10211 /* This inclusion and its children have been processed. */
10212 return;
10213 }
10214
10215 *slot = per_cu;
10216 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10217 cust = get_compunit_symtab (per_cu);
10218 if (cust != NULL)
ec94af83
DE
10219 {
10220 /* If this is a type unit only add its symbol table if we haven't
10221 seen it yet (type unit per_cu's can share symtabs). */
10222 if (per_cu->is_debug_types)
10223 {
43f3e411 10224 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10225 if (*slot == NULL)
10226 {
43f3e411 10227 *slot = cust;
4c39bc03 10228 result->push_back (cust);
43f3e411
DE
10229 if (cust->user == NULL)
10230 cust->user = immediate_parent;
ec94af83
DE
10231 }
10232 }
10233 else
f9125b6c 10234 {
4c39bc03 10235 result->push_back (cust);
43f3e411
DE
10236 if (cust->user == NULL)
10237 cust->user = immediate_parent;
f9125b6c 10238 }
ec94af83 10239 }
95554aad
TT
10240
10241 for (ix = 0;
796a7ff8 10242 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10243 ++ix)
ec94af83
DE
10244 {
10245 recursively_compute_inclusions (result, all_children,
43f3e411 10246 all_type_symtabs, iter, cust);
ec94af83 10247 }
95554aad
TT
10248}
10249
43f3e411 10250/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10251 PER_CU. */
10252
10253static void
43f3e411 10254compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10255{
f4dc4d17
DE
10256 gdb_assert (! per_cu->is_debug_types);
10257
796a7ff8 10258 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10259 {
10260 int ix, len;
ec94af83 10261 struct dwarf2_per_cu_data *per_cu_iter;
4c39bc03 10262 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10263 htab_t all_children, all_type_symtabs;
43f3e411 10264 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10265
10266 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10267 if (cust == NULL)
95554aad
TT
10268 return;
10269
10270 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10271 NULL, xcalloc, xfree);
ec94af83
DE
10272 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10273 NULL, xcalloc, xfree);
95554aad
TT
10274
10275 for (ix = 0;
796a7ff8 10276 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10277 ix, per_cu_iter);
95554aad 10278 ++ix)
ec94af83
DE
10279 {
10280 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10281 all_type_symtabs, per_cu_iter,
43f3e411 10282 cust);
ec94af83 10283 }
95554aad 10284
ec94af83 10285 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10286 len = result_symtabs.size ();
43f3e411 10287 cust->includes
ed2dc618 10288 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10289 struct compunit_symtab *, len + 1);
4c39bc03
TT
10290 memcpy (cust->includes, result_symtabs.data (),
10291 len * sizeof (compunit_symtab *));
43f3e411 10292 cust->includes[len] = NULL;
95554aad 10293
95554aad 10294 htab_delete (all_children);
ec94af83 10295 htab_delete (all_type_symtabs);
95554aad
TT
10296 }
10297}
10298
10299/* Compute the 'includes' field for the symtabs of all the CUs we just
10300 read. */
10301
10302static void
ed2dc618 10303process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10304{
71b73764 10305 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10306 {
10307 if (! iter->is_debug_types)
43f3e411 10308 compute_compunit_symtab_includes (iter);
f4dc4d17 10309 }
95554aad 10310
c5d0225d 10311 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10312}
10313
9cdd5dbd 10314/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10315 already been loaded into memory. */
10316
10317static void
95554aad
TT
10318process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10319 enum language pretend_language)
10b3939b 10320{
10b3939b 10321 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10322 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10323 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10324 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10325 CORE_ADDR lowpc, highpc;
43f3e411 10326 struct compunit_symtab *cust;
10b3939b 10327 CORE_ADDR baseaddr;
4359dff1 10328 struct block *static_block;
3e29f34a 10329 CORE_ADDR addr;
10b3939b
DJ
10330
10331 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10332
c89b44cd
TT
10333 /* Clear the list here in case something was left over. */
10334 cu->method_list.clear ();
10b3939b 10335
95554aad
TT
10336 cu->language = pretend_language;
10337 cu->language_defn = language_def (cu->language);
10338
c906108c 10339 /* Do line number decoding in read_file_scope () */
10b3939b 10340 process_die (cu->dies, cu);
c906108c 10341
a766d390
DE
10342 /* For now fudge the Go package. */
10343 if (cu->language == language_go)
10344 fixup_go_packaging (cu);
10345
3da10d80
KS
10346 /* Now that we have processed all the DIEs in the CU, all the types
10347 should be complete, and it should now be safe to compute all of the
10348 physnames. */
10349 compute_delayed_physnames (cu);
3da10d80 10350
c9317f21
TT
10351 if (cu->language == language_rust)
10352 rust_union_quirks (cu);
10353
fae299cd
DC
10354 /* Some compilers don't define a DW_AT_high_pc attribute for the
10355 compilation unit. If the DW_AT_high_pc is missing, synthesize
10356 it, by scanning the DIE's below the compilation unit. */
10b3939b 10357 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10358
3e29f34a 10359 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10360 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10361
10362 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10363 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10364 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10365 addrmap to help ensure it has an accurate map of pc values belonging to
10366 this comp unit. */
10367 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10368
c24bdb02 10369 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10370 SECT_OFF_TEXT (objfile),
10371 0);
c906108c 10372
43f3e411 10373 if (cust != NULL)
c906108c 10374 {
df15bd07 10375 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10376
8be455d7
JK
10377 /* Set symtab language to language from DW_AT_language. If the
10378 compilation is from a C file generated by language preprocessors, do
10379 not set the language if it was already deduced by start_subfile. */
43f3e411 10380 if (!(cu->language == language_c
40e3ad0e 10381 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10382 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10383
10384 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10385 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10386 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10387 there were bugs in prologue debug info, fixed later in GCC-4.5
10388 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10389
10390 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10391 needed, it would be wrong due to missing DW_AT_producer there.
10392
10393 Still one can confuse GDB by using non-standard GCC compilation
10394 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10395 */
ab260dad 10396 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10397 cust->locations_valid = 1;
e0d00bc7
JK
10398
10399 if (gcc_4_minor >= 5)
43f3e411 10400 cust->epilogue_unwind_valid = 1;
96408a79 10401
43f3e411 10402 cust->call_site_htab = cu->call_site_htab;
c906108c 10403 }
9291a0cd
TT
10404
10405 if (dwarf2_per_objfile->using_index)
43f3e411 10406 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10407 else
10408 {
10409 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10410 pst->compunit_symtab = cust;
9291a0cd
TT
10411 pst->readin = 1;
10412 }
c906108c 10413
95554aad 10414 /* Push it for inclusion processing later. */
c5d0225d 10415 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10416
10417 /* Not needed any more. */
c24bdb02 10418 cu->reset_builder ();
f4dc4d17 10419}
45cfd468 10420
f4dc4d17
DE
10421/* Generate full symbol information for type unit PER_CU, whose DIEs have
10422 already been loaded into memory. */
10423
10424static void
10425process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10426 enum language pretend_language)
10427{
10428 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10429 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10430 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10431 struct compunit_symtab *cust;
0186c6a7
DE
10432 struct signatured_type *sig_type;
10433
10434 gdb_assert (per_cu->is_debug_types);
10435 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10436
c89b44cd
TT
10437 /* Clear the list here in case something was left over. */
10438 cu->method_list.clear ();
f4dc4d17 10439
f4dc4d17
DE
10440 cu->language = pretend_language;
10441 cu->language_defn = language_def (cu->language);
10442
10443 /* The symbol tables are set up in read_type_unit_scope. */
10444 process_die (cu->dies, cu);
10445
10446 /* For now fudge the Go package. */
10447 if (cu->language == language_go)
10448 fixup_go_packaging (cu);
10449
10450 /* Now that we have processed all the DIEs in the CU, all the types
10451 should be complete, and it should now be safe to compute all of the
10452 physnames. */
10453 compute_delayed_physnames (cu);
f4dc4d17 10454
c9317f21
TT
10455 if (cu->language == language_rust)
10456 rust_union_quirks (cu);
10457
f4dc4d17
DE
10458 /* TUs share symbol tables.
10459 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10460 of it with end_expandable_symtab. Otherwise, complete the addition of
10461 this TU's symbols to the existing symtab. */
43f3e411 10462 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10463 {
c24bdb02
KS
10464 buildsym_compunit *builder = cu->get_builder ();
10465 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10466 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10467
43f3e411 10468 if (cust != NULL)
f4dc4d17
DE
10469 {
10470 /* Set symtab language to language from DW_AT_language. If the
10471 compilation is from a C file generated by language preprocessors,
10472 do not set the language if it was already deduced by
10473 start_subfile. */
43f3e411
DE
10474 if (!(cu->language == language_c
10475 && COMPUNIT_FILETABS (cust)->language != language_c))
10476 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10477 }
10478 }
10479 else
10480 {
c24bdb02 10481 cu->get_builder ()->augment_type_symtab ();
43f3e411 10482 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10483 }
10484
10485 if (dwarf2_per_objfile->using_index)
43f3e411 10486 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10487 else
10488 {
10489 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10490 pst->compunit_symtab = cust;
f4dc4d17 10491 pst->readin = 1;
45cfd468 10492 }
804d2729
TT
10493
10494 /* Not needed any more. */
c24bdb02 10495 cu->reset_builder ();
c906108c
SS
10496}
10497
95554aad
TT
10498/* Process an imported unit DIE. */
10499
10500static void
10501process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10502{
10503 struct attribute *attr;
10504
f4dc4d17
DE
10505 /* For now we don't handle imported units in type units. */
10506 if (cu->per_cu->is_debug_types)
10507 {
10508 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10509 " supported in type units [in module %s]"),
518817b3 10510 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10511 }
10512
95554aad
TT
10513 attr = dwarf2_attr (die, DW_AT_import, cu);
10514 if (attr != NULL)
10515 {
9c541725
PA
10516 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10517 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10518 dwarf2_per_cu_data *per_cu
e3b94546 10519 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10520 cu->per_cu->dwarf2_per_objfile);
95554aad 10521
69d751e3 10522 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10523 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10524 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10525
796a7ff8 10526 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10527 per_cu);
10528 }
10529}
10530
4c8aa72d
PA
10531/* RAII object that represents a process_die scope: i.e.,
10532 starts/finishes processing a DIE. */
10533class process_die_scope
adde2bff 10534{
4c8aa72d
PA
10535public:
10536 process_die_scope (die_info *die, dwarf2_cu *cu)
10537 : m_die (die), m_cu (cu)
10538 {
10539 /* We should only be processing DIEs not already in process. */
10540 gdb_assert (!m_die->in_process);
10541 m_die->in_process = true;
10542 }
8c3cb9fa 10543
4c8aa72d
PA
10544 ~process_die_scope ()
10545 {
10546 m_die->in_process = false;
10547
10548 /* If we're done processing the DIE for the CU that owns the line
10549 header, we don't need the line header anymore. */
10550 if (m_cu->line_header_die_owner == m_die)
10551 {
10552 delete m_cu->line_header;
10553 m_cu->line_header = NULL;
10554 m_cu->line_header_die_owner = NULL;
10555 }
10556 }
10557
10558private:
10559 die_info *m_die;
10560 dwarf2_cu *m_cu;
10561};
adde2bff 10562
c906108c
SS
10563/* Process a die and its children. */
10564
10565static void
e7c27a73 10566process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10567{
4c8aa72d 10568 process_die_scope scope (die, cu);
adde2bff 10569
c906108c
SS
10570 switch (die->tag)
10571 {
10572 case DW_TAG_padding:
10573 break;
10574 case DW_TAG_compile_unit:
95554aad 10575 case DW_TAG_partial_unit:
e7c27a73 10576 read_file_scope (die, cu);
c906108c 10577 break;
348e048f
DE
10578 case DW_TAG_type_unit:
10579 read_type_unit_scope (die, cu);
10580 break;
c906108c 10581 case DW_TAG_subprogram:
c906108c 10582 case DW_TAG_inlined_subroutine:
edb3359d 10583 read_func_scope (die, cu);
c906108c
SS
10584 break;
10585 case DW_TAG_lexical_block:
14898363
L
10586 case DW_TAG_try_block:
10587 case DW_TAG_catch_block:
e7c27a73 10588 read_lexical_block_scope (die, cu);
c906108c 10589 break;
216f72a1 10590 case DW_TAG_call_site:
96408a79
SA
10591 case DW_TAG_GNU_call_site:
10592 read_call_site_scope (die, cu);
10593 break;
c906108c 10594 case DW_TAG_class_type:
680b30c7 10595 case DW_TAG_interface_type:
c906108c
SS
10596 case DW_TAG_structure_type:
10597 case DW_TAG_union_type:
134d01f1 10598 process_structure_scope (die, cu);
c906108c
SS
10599 break;
10600 case DW_TAG_enumeration_type:
134d01f1 10601 process_enumeration_scope (die, cu);
c906108c 10602 break;
134d01f1 10603
f792889a
DJ
10604 /* These dies have a type, but processing them does not create
10605 a symbol or recurse to process the children. Therefore we can
10606 read them on-demand through read_type_die. */
c906108c 10607 case DW_TAG_subroutine_type:
72019c9c 10608 case DW_TAG_set_type:
c906108c 10609 case DW_TAG_array_type:
c906108c 10610 case DW_TAG_pointer_type:
c906108c 10611 case DW_TAG_ptr_to_member_type:
c906108c 10612 case DW_TAG_reference_type:
4297a3f0 10613 case DW_TAG_rvalue_reference_type:
c906108c 10614 case DW_TAG_string_type:
c906108c 10615 break;
134d01f1 10616
c906108c 10617 case DW_TAG_base_type:
a02abb62 10618 case DW_TAG_subrange_type:
cb249c71 10619 case DW_TAG_typedef:
134d01f1
DJ
10620 /* Add a typedef symbol for the type definition, if it has a
10621 DW_AT_name. */
f792889a 10622 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10623 break;
c906108c 10624 case DW_TAG_common_block:
e7c27a73 10625 read_common_block (die, cu);
c906108c
SS
10626 break;
10627 case DW_TAG_common_inclusion:
10628 break;
d9fa45fe 10629 case DW_TAG_namespace:
9068261f 10630 cu->processing_has_namespace_info = true;
e7c27a73 10631 read_namespace (die, cu);
d9fa45fe 10632 break;
5d7cb8df 10633 case DW_TAG_module:
9068261f 10634 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10635 read_module (die, cu);
10636 break;
d9fa45fe 10637 case DW_TAG_imported_declaration:
9068261f 10638 cu->processing_has_namespace_info = true;
74921315
KS
10639 if (read_namespace_alias (die, cu))
10640 break;
86a73007
TT
10641 /* The declaration is not a global namespace alias. */
10642 /* Fall through. */
d9fa45fe 10643 case DW_TAG_imported_module:
9068261f 10644 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10645 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10646 || cu->language != language_fortran))
b98664d3 10647 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10648 dwarf_tag_name (die->tag));
10649 read_import_statement (die, cu);
d9fa45fe 10650 break;
95554aad
TT
10651
10652 case DW_TAG_imported_unit:
10653 process_imported_unit_die (die, cu);
10654 break;
10655
71a3c369
TT
10656 case DW_TAG_variable:
10657 read_variable (die, cu);
10658 break;
10659
c906108c 10660 default:
e7c27a73 10661 new_symbol (die, NULL, cu);
c906108c
SS
10662 break;
10663 }
10664}
ca69b9e6
DE
10665\f
10666/* DWARF name computation. */
c906108c 10667
94af9270
KS
10668/* A helper function for dwarf2_compute_name which determines whether DIE
10669 needs to have the name of the scope prepended to the name listed in the
10670 die. */
10671
10672static int
10673die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10674{
1c809c68
TT
10675 struct attribute *attr;
10676
94af9270
KS
10677 switch (die->tag)
10678 {
10679 case DW_TAG_namespace:
10680 case DW_TAG_typedef:
10681 case DW_TAG_class_type:
10682 case DW_TAG_interface_type:
10683 case DW_TAG_structure_type:
10684 case DW_TAG_union_type:
10685 case DW_TAG_enumeration_type:
10686 case DW_TAG_enumerator:
10687 case DW_TAG_subprogram:
08a76f8a 10688 case DW_TAG_inlined_subroutine:
94af9270 10689 case DW_TAG_member:
74921315 10690 case DW_TAG_imported_declaration:
94af9270
KS
10691 return 1;
10692
10693 case DW_TAG_variable:
c2b0a229 10694 case DW_TAG_constant:
94af9270
KS
10695 /* We only need to prefix "globally" visible variables. These include
10696 any variable marked with DW_AT_external or any variable that
10697 lives in a namespace. [Variables in anonymous namespaces
10698 require prefixing, but they are not DW_AT_external.] */
10699
10700 if (dwarf2_attr (die, DW_AT_specification, cu))
10701 {
10702 struct dwarf2_cu *spec_cu = cu;
9a619af0 10703
94af9270
KS
10704 return die_needs_namespace (die_specification (die, &spec_cu),
10705 spec_cu);
10706 }
10707
1c809c68 10708 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10709 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10710 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10711 return 0;
10712 /* A variable in a lexical block of some kind does not need a
10713 namespace, even though in C++ such variables may be external
10714 and have a mangled name. */
10715 if (die->parent->tag == DW_TAG_lexical_block
10716 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10717 || die->parent->tag == DW_TAG_catch_block
10718 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10719 return 0;
10720 return 1;
94af9270
KS
10721
10722 default:
10723 return 0;
10724 }
10725}
10726
73b9be8b
KS
10727/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10728 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10729 defined for the given DIE. */
10730
10731static struct attribute *
10732dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10733{
10734 struct attribute *attr;
10735
10736 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10737 if (attr == NULL)
10738 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10739
10740 return attr;
10741}
10742
10743/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10744 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10745 defined for the given DIE. */
10746
10747static const char *
10748dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10749{
10750 const char *linkage_name;
10751
10752 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10753 if (linkage_name == NULL)
10754 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10755
10756 return linkage_name;
10757}
10758
94af9270 10759/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10760 compute the physname for the object, which include a method's:
9c37b5ae 10761 - formal parameters (C++),
a766d390 10762 - receiver type (Go),
a766d390
DE
10763
10764 The term "physname" is a bit confusing.
10765 For C++, for example, it is the demangled name.
10766 For Go, for example, it's the mangled name.
94af9270 10767
af6b7be1
JB
10768 For Ada, return the DIE's linkage name rather than the fully qualified
10769 name. PHYSNAME is ignored..
10770
94af9270
KS
10771 The result is allocated on the objfile_obstack and canonicalized. */
10772
10773static const char *
15d034d0
TT
10774dwarf2_compute_name (const char *name,
10775 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10776 int physname)
10777{
518817b3 10778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10779
94af9270
KS
10780 if (name == NULL)
10781 name = dwarf2_name (die, cu);
10782
2ee7123e
DE
10783 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10784 but otherwise compute it by typename_concat inside GDB.
10785 FIXME: Actually this is not really true, or at least not always true.
10786 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10787 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10788 will set the demangled name to the result of dwarf2_full_name, and it is
10789 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10790 if (cu->language == language_ada
10791 || (cu->language == language_fortran && physname))
10792 {
10793 /* For Ada unit, we prefer the linkage name over the name, as
10794 the former contains the exported name, which the user expects
10795 to be able to reference. Ideally, we want the user to be able
10796 to reference this entity using either natural or linkage name,
10797 but we haven't started looking at this enhancement yet. */
73b9be8b 10798 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10799
2ee7123e
DE
10800 if (linkage_name != NULL)
10801 return linkage_name;
f55ee35c
JK
10802 }
10803
94af9270
KS
10804 /* These are the only languages we know how to qualify names in. */
10805 if (name != NULL
9c37b5ae 10806 && (cu->language == language_cplus
c44af4eb
TT
10807 || cu->language == language_fortran || cu->language == language_d
10808 || cu->language == language_rust))
94af9270
KS
10809 {
10810 if (die_needs_namespace (die, cu))
10811 {
0d5cff50 10812 const char *prefix;
34a68019 10813 const char *canonical_name = NULL;
94af9270 10814
d7e74731
PA
10815 string_file buf;
10816
94af9270 10817 prefix = determine_prefix (die, cu);
94af9270
KS
10818 if (*prefix != '\0')
10819 {
f55ee35c
JK
10820 char *prefixed_name = typename_concat (NULL, prefix, name,
10821 physname, cu);
9a619af0 10822
d7e74731 10823 buf.puts (prefixed_name);
94af9270
KS
10824 xfree (prefixed_name);
10825 }
10826 else
d7e74731 10827 buf.puts (name);
94af9270 10828
98bfdba5
PA
10829 /* Template parameters may be specified in the DIE's DW_AT_name, or
10830 as children with DW_TAG_template_type_param or
10831 DW_TAG_value_type_param. If the latter, add them to the name
10832 here. If the name already has template parameters, then
10833 skip this step; some versions of GCC emit both, and
10834 it is more efficient to use the pre-computed name.
10835
10836 Something to keep in mind about this process: it is very
10837 unlikely, or in some cases downright impossible, to produce
10838 something that will match the mangled name of a function.
10839 If the definition of the function has the same debug info,
10840 we should be able to match up with it anyway. But fallbacks
10841 using the minimal symbol, for instance to find a method
10842 implemented in a stripped copy of libstdc++, will not work.
10843 If we do not have debug info for the definition, we will have to
10844 match them up some other way.
10845
10846 When we do name matching there is a related problem with function
10847 templates; two instantiated function templates are allowed to
10848 differ only by their return types, which we do not add here. */
10849
10850 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10851 {
10852 struct attribute *attr;
10853 struct die_info *child;
10854 int first = 1;
10855
10856 die->building_fullname = 1;
10857
10858 for (child = die->child; child != NULL; child = child->sibling)
10859 {
10860 struct type *type;
12df843f 10861 LONGEST value;
d521ce57 10862 const gdb_byte *bytes;
98bfdba5
PA
10863 struct dwarf2_locexpr_baton *baton;
10864 struct value *v;
10865
10866 if (child->tag != DW_TAG_template_type_param
10867 && child->tag != DW_TAG_template_value_param)
10868 continue;
10869
10870 if (first)
10871 {
d7e74731 10872 buf.puts ("<");
98bfdba5
PA
10873 first = 0;
10874 }
10875 else
d7e74731 10876 buf.puts (", ");
98bfdba5
PA
10877
10878 attr = dwarf2_attr (child, DW_AT_type, cu);
10879 if (attr == NULL)
10880 {
b98664d3 10881 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10882 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10883 continue;
10884 }
10885 type = die_type (child, cu);
10886
10887 if (child->tag == DW_TAG_template_type_param)
10888 {
c1ec8cea
TT
10889 c_print_type (type, "", &buf, -1, 0, cu->language,
10890 &type_print_raw_options);
98bfdba5
PA
10891 continue;
10892 }
10893
10894 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10895 if (attr == NULL)
10896 {
b98664d3 10897 complaint (_("template parameter missing "
3e43a32a 10898 "DW_AT_const_value"));
d7e74731 10899 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10900 continue;
10901 }
10902
10903 dwarf2_const_value_attr (attr, type, name,
10904 &cu->comp_unit_obstack, cu,
10905 &value, &bytes, &baton);
10906
10907 if (TYPE_NOSIGN (type))
10908 /* GDB prints characters as NUMBER 'CHAR'. If that's
10909 changed, this can use value_print instead. */
d7e74731 10910 c_printchar (value, type, &buf);
98bfdba5
PA
10911 else
10912 {
10913 struct value_print_options opts;
10914
10915 if (baton != NULL)
10916 v = dwarf2_evaluate_loc_desc (type, NULL,
10917 baton->data,
10918 baton->size,
10919 baton->per_cu);
10920 else if (bytes != NULL)
10921 {
10922 v = allocate_value (type);
10923 memcpy (value_contents_writeable (v), bytes,
10924 TYPE_LENGTH (type));
10925 }
10926 else
10927 v = value_from_longest (type, value);
10928
3e43a32a
MS
10929 /* Specify decimal so that we do not depend on
10930 the radix. */
98bfdba5
PA
10931 get_formatted_print_options (&opts, 'd');
10932 opts.raw = 1;
d7e74731 10933 value_print (v, &buf, &opts);
98bfdba5 10934 release_value (v);
98bfdba5
PA
10935 }
10936 }
10937
10938 die->building_fullname = 0;
10939
10940 if (!first)
10941 {
10942 /* Close the argument list, with a space if necessary
10943 (nested templates). */
d7e74731
PA
10944 if (!buf.empty () && buf.string ().back () == '>')
10945 buf.puts (" >");
98bfdba5 10946 else
d7e74731 10947 buf.puts (">");
98bfdba5
PA
10948 }
10949 }
10950
9c37b5ae 10951 /* For C++ methods, append formal parameter type
94af9270 10952 information, if PHYSNAME. */
6e70227d 10953
94af9270 10954 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10955 && cu->language == language_cplus)
94af9270
KS
10956 {
10957 struct type *type = read_type_die (die, cu);
10958
d7e74731 10959 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10960 &type_print_raw_options);
94af9270 10961
9c37b5ae 10962 if (cu->language == language_cplus)
94af9270 10963 {
60430eff
DJ
10964 /* Assume that an artificial first parameter is
10965 "this", but do not crash if it is not. RealView
10966 marks unnamed (and thus unused) parameters as
10967 artificial; there is no way to differentiate
10968 the two cases. */
94af9270
KS
10969 if (TYPE_NFIELDS (type) > 0
10970 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10971 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10972 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10973 0))))
d7e74731 10974 buf.puts (" const");
94af9270
KS
10975 }
10976 }
10977
d7e74731 10978 const std::string &intermediate_name = buf.string ();
94af9270
KS
10979
10980 if (cu->language == language_cplus)
34a68019 10981 canonical_name
322a8516 10982 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10983 &objfile->per_bfd->storage_obstack);
10984
10985 /* If we only computed INTERMEDIATE_NAME, or if
10986 INTERMEDIATE_NAME is already canonical, then we need to
10987 copy it to the appropriate obstack. */
322a8516 10988 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10989 name = ((const char *)
10990 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10991 intermediate_name.c_str (),
10992 intermediate_name.length ()));
34a68019
TT
10993 else
10994 name = canonical_name;
94af9270
KS
10995 }
10996 }
10997
10998 return name;
10999}
11000
0114d602
DJ
11001/* Return the fully qualified name of DIE, based on its DW_AT_name.
11002 If scope qualifiers are appropriate they will be added. The result
34a68019 11003 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11004 not have a name. NAME may either be from a previous call to
11005 dwarf2_name or NULL.
11006
9c37b5ae 11007 The output string will be canonicalized (if C++). */
0114d602
DJ
11008
11009static const char *
15d034d0 11010dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11011{
94af9270
KS
11012 return dwarf2_compute_name (name, die, cu, 0);
11013}
0114d602 11014
94af9270
KS
11015/* Construct a physname for the given DIE in CU. NAME may either be
11016 from a previous call to dwarf2_name or NULL. The result will be
11017 allocated on the objfile_objstack or NULL if the DIE does not have a
11018 name.
0114d602 11019
9c37b5ae 11020 The output string will be canonicalized (if C++). */
0114d602 11021
94af9270 11022static const char *
15d034d0 11023dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11024{
518817b3 11025 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11026 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11027 int need_copy = 1;
11028
11029 /* In this case dwarf2_compute_name is just a shortcut not building anything
11030 on its own. */
11031 if (!die_needs_namespace (die, cu))
11032 return dwarf2_compute_name (name, die, cu, 1);
11033
73b9be8b 11034 mangled = dw2_linkage_name (die, cu);
900e11f9 11035
e98c9e7c
TT
11036 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11037 See https://github.com/rust-lang/rust/issues/32925. */
11038 if (cu->language == language_rust && mangled != NULL
11039 && strchr (mangled, '{') != NULL)
11040 mangled = NULL;
11041
900e11f9
JK
11042 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11043 has computed. */
791afaa2 11044 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11045 if (mangled != NULL)
900e11f9 11046 {
900e11f9 11047
59cc4834
JB
11048 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11049 {
11050 /* Do nothing (do not demangle the symbol name). */
11051 }
11052 else if (cu->language == language_go)
a766d390 11053 {
5e2db402
TT
11054 /* This is a lie, but we already lie to the caller new_symbol.
11055 new_symbol assumes we return the mangled name.
a766d390 11056 This just undoes that lie until things are cleaned up. */
a766d390
DE
11057 }
11058 else
11059 {
0eb876f5
JB
11060 /* Use DMGL_RET_DROP for C++ template functions to suppress
11061 their return type. It is easier for GDB users to search
11062 for such functions as `name(params)' than `long name(params)'.
11063 In such case the minimal symbol names do not match the full
11064 symbol names but for template functions there is never a need
11065 to look up their definition from their declaration so
11066 the only disadvantage remains the minimal symbol variant
11067 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11068 demangled.reset (gdb_demangle (mangled,
11069 (DMGL_PARAMS | DMGL_ANSI
11070 | DMGL_RET_DROP)));
a766d390 11071 }
900e11f9 11072 if (demangled)
791afaa2 11073 canon = demangled.get ();
900e11f9
JK
11074 else
11075 {
11076 canon = mangled;
11077 need_copy = 0;
11078 }
11079 }
11080
11081 if (canon == NULL || check_physname)
11082 {
11083 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11084
11085 if (canon != NULL && strcmp (physname, canon) != 0)
11086 {
11087 /* It may not mean a bug in GDB. The compiler could also
11088 compute DW_AT_linkage_name incorrectly. But in such case
11089 GDB would need to be bug-to-bug compatible. */
11090
b98664d3 11091 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11092 "(from linkage <%s>) - DIE at %s [in module %s]"),
11093 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11094 objfile_name (objfile));
900e11f9
JK
11095
11096 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11097 is available here - over computed PHYSNAME. It is safer
11098 against both buggy GDB and buggy compilers. */
11099
11100 retval = canon;
11101 }
11102 else
11103 {
11104 retval = physname;
11105 need_copy = 0;
11106 }
11107 }
11108 else
11109 retval = canon;
11110
11111 if (need_copy)
224c3ddb
SM
11112 retval = ((const char *)
11113 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11114 retval, strlen (retval)));
900e11f9 11115
900e11f9 11116 return retval;
0114d602
DJ
11117}
11118
74921315
KS
11119/* Inspect DIE in CU for a namespace alias. If one exists, record
11120 a new symbol for it.
11121
11122 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11123
11124static int
11125read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11126{
11127 struct attribute *attr;
11128
11129 /* If the die does not have a name, this is not a namespace
11130 alias. */
11131 attr = dwarf2_attr (die, DW_AT_name, cu);
11132 if (attr != NULL)
11133 {
11134 int num;
11135 struct die_info *d = die;
11136 struct dwarf2_cu *imported_cu = cu;
11137
11138 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11139 keep inspecting DIEs until we hit the underlying import. */
11140#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11141 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11142 {
11143 attr = dwarf2_attr (d, DW_AT_import, cu);
11144 if (attr == NULL)
11145 break;
11146
11147 d = follow_die_ref (d, attr, &imported_cu);
11148 if (d->tag != DW_TAG_imported_declaration)
11149 break;
11150 }
11151
11152 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11153 {
b98664d3 11154 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11155 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11156 return 0;
11157 }
11158
11159 if (attr != NULL)
11160 {
11161 struct type *type;
9c541725 11162 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11163
9c541725 11164 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11165 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11166 {
11167 /* This declaration is a global namespace alias. Add
11168 a symbol for it whose type is the aliased namespace. */
11169 new_symbol (die, type, cu);
11170 return 1;
11171 }
11172 }
11173 }
11174
11175 return 0;
11176}
11177
22cee43f 11178/* Return the using directives repository (global or local?) to use in the
804d2729 11179 current context for CU.
22cee43f
PMR
11180
11181 For Ada, imported declarations can materialize renamings, which *may* be
11182 global. However it is impossible (for now?) in DWARF to distinguish
11183 "external" imported declarations and "static" ones. As all imported
11184 declarations seem to be static in all other languages, make them all CU-wide
11185 global only in Ada. */
11186
11187static struct using_direct **
804d2729 11188using_directives (struct dwarf2_cu *cu)
22cee43f 11189{
c24bdb02
KS
11190 if (cu->language == language_ada
11191 && cu->get_builder ()->outermost_context_p ())
11192 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11193 else
c24bdb02 11194 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11195}
11196
27aa8d6a
SW
11197/* Read the import statement specified by the given die and record it. */
11198
11199static void
11200read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11201{
518817b3 11202 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11203 struct attribute *import_attr;
32019081 11204 struct die_info *imported_die, *child_die;
de4affc9 11205 struct dwarf2_cu *imported_cu;
27aa8d6a 11206 const char *imported_name;
794684b6 11207 const char *imported_name_prefix;
13387711
SW
11208 const char *canonical_name;
11209 const char *import_alias;
11210 const char *imported_declaration = NULL;
794684b6 11211 const char *import_prefix;
eb1e02fd 11212 std::vector<const char *> excludes;
13387711 11213
27aa8d6a
SW
11214 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11215 if (import_attr == NULL)
11216 {
b98664d3 11217 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11218 dwarf_tag_name (die->tag));
11219 return;
11220 }
11221
de4affc9
CC
11222 imported_cu = cu;
11223 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11224 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11225 if (imported_name == NULL)
11226 {
11227 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11228
11229 The import in the following code:
11230 namespace A
11231 {
11232 typedef int B;
11233 }
11234
11235 int main ()
11236 {
11237 using A::B;
11238 B b;
11239 return b;
11240 }
11241
11242 ...
11243 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11244 <52> DW_AT_decl_file : 1
11245 <53> DW_AT_decl_line : 6
11246 <54> DW_AT_import : <0x75>
11247 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11248 <59> DW_AT_name : B
11249 <5b> DW_AT_decl_file : 1
11250 <5c> DW_AT_decl_line : 2
11251 <5d> DW_AT_type : <0x6e>
11252 ...
11253 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11254 <76> DW_AT_byte_size : 4
11255 <77> DW_AT_encoding : 5 (signed)
11256
11257 imports the wrong die ( 0x75 instead of 0x58 ).
11258 This case will be ignored until the gcc bug is fixed. */
11259 return;
11260 }
11261
82856980
SW
11262 /* Figure out the local name after import. */
11263 import_alias = dwarf2_name (die, cu);
27aa8d6a 11264
794684b6
SW
11265 /* Figure out where the statement is being imported to. */
11266 import_prefix = determine_prefix (die, cu);
11267
11268 /* Figure out what the scope of the imported die is and prepend it
11269 to the name of the imported die. */
de4affc9 11270 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11271
f55ee35c
JK
11272 if (imported_die->tag != DW_TAG_namespace
11273 && imported_die->tag != DW_TAG_module)
794684b6 11274 {
13387711
SW
11275 imported_declaration = imported_name;
11276 canonical_name = imported_name_prefix;
794684b6 11277 }
13387711 11278 else if (strlen (imported_name_prefix) > 0)
12aaed36 11279 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11280 imported_name_prefix,
11281 (cu->language == language_d ? "." : "::"),
11282 imported_name, (char *) NULL);
13387711
SW
11283 else
11284 canonical_name = imported_name;
794684b6 11285
32019081
JK
11286 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11287 for (child_die = die->child; child_die && child_die->tag;
11288 child_die = sibling_die (child_die))
11289 {
11290 /* DWARF-4: A Fortran use statement with a “rename list” may be
11291 represented by an imported module entry with an import attribute
11292 referring to the module and owned entries corresponding to those
11293 entities that are renamed as part of being imported. */
11294
11295 if (child_die->tag != DW_TAG_imported_declaration)
11296 {
b98664d3 11297 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11298 "- DIE at %s [in module %s]"),
11299 sect_offset_str (child_die->sect_off),
11300 objfile_name (objfile));
32019081
JK
11301 continue;
11302 }
11303
11304 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11305 if (import_attr == NULL)
11306 {
b98664d3 11307 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11308 dwarf_tag_name (child_die->tag));
11309 continue;
11310 }
11311
11312 imported_cu = cu;
11313 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11314 &imported_cu);
11315 imported_name = dwarf2_name (imported_die, imported_cu);
11316 if (imported_name == NULL)
11317 {
b98664d3 11318 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11319 "imported name - DIE at %s [in module %s]"),
11320 sect_offset_str (child_die->sect_off),
11321 objfile_name (objfile));
32019081
JK
11322 continue;
11323 }
11324
eb1e02fd 11325 excludes.push_back (imported_name);
32019081
JK
11326
11327 process_die (child_die, cu);
11328 }
11329
804d2729 11330 add_using_directive (using_directives (cu),
22cee43f
PMR
11331 import_prefix,
11332 canonical_name,
11333 import_alias,
11334 imported_declaration,
11335 excludes,
11336 0,
11337 &objfile->objfile_obstack);
27aa8d6a
SW
11338}
11339
5230b05a
WT
11340/* ICC<14 does not output the required DW_AT_declaration on incomplete
11341 types, but gives them a size of zero. Starting with version 14,
11342 ICC is compatible with GCC. */
11343
9068261f 11344static bool
5230b05a
WT
11345producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11346{
11347 if (!cu->checked_producer)
11348 check_producer (cu);
11349
11350 return cu->producer_is_icc_lt_14;
11351}
11352
eb77c9df
AB
11353/* ICC generates a DW_AT_type for C void functions. This was observed on
11354 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11355 which says that void functions should not have a DW_AT_type. */
11356
11357static bool
11358producer_is_icc (struct dwarf2_cu *cu)
11359{
11360 if (!cu->checked_producer)
11361 check_producer (cu);
11362
11363 return cu->producer_is_icc;
11364}
11365
1b80a9fa
JK
11366/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11367 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11368 this, it was first present in GCC release 4.3.0. */
11369
9068261f 11370static bool
1b80a9fa
JK
11371producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11372{
11373 if (!cu->checked_producer)
11374 check_producer (cu);
11375
11376 return cu->producer_is_gcc_lt_4_3;
11377}
11378
d721ba37
PA
11379static file_and_directory
11380find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11381{
d721ba37
PA
11382 file_and_directory res;
11383
9291a0cd
TT
11384 /* Find the filename. Do not use dwarf2_name here, since the filename
11385 is not a source language identifier. */
d721ba37
PA
11386 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11387 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11388
d721ba37
PA
11389 if (res.comp_dir == NULL
11390 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11391 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11392 {
d721ba37
PA
11393 res.comp_dir_storage = ldirname (res.name);
11394 if (!res.comp_dir_storage.empty ())
11395 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11396 }
d721ba37 11397 if (res.comp_dir != NULL)
9291a0cd
TT
11398 {
11399 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11400 directory, get rid of it. */
d721ba37 11401 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11402
d721ba37
PA
11403 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11404 res.comp_dir = cp + 1;
9291a0cd
TT
11405 }
11406
d721ba37
PA
11407 if (res.name == NULL)
11408 res.name = "<unknown>";
11409
11410 return res;
9291a0cd
TT
11411}
11412
f4dc4d17
DE
11413/* Handle DW_AT_stmt_list for a compilation unit.
11414 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11415 COMP_DIR is the compilation directory. LOWPC is passed to
11416 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11417
11418static void
11419handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11420 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11421{
518817b3
SM
11422 struct dwarf2_per_objfile *dwarf2_per_objfile
11423 = cu->per_cu->dwarf2_per_objfile;
527f3840 11424 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11425 struct attribute *attr;
527f3840
JK
11426 struct line_header line_header_local;
11427 hashval_t line_header_local_hash;
527f3840
JK
11428 void **slot;
11429 int decode_mapping;
2ab95328 11430
f4dc4d17
DE
11431 gdb_assert (! cu->per_cu->is_debug_types);
11432
2ab95328 11433 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11434 if (attr == NULL)
11435 return;
11436
9c541725 11437 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11438
11439 /* The line header hash table is only created if needed (it exists to
11440 prevent redundant reading of the line table for partial_units).
11441 If we're given a partial_unit, we'll need it. If we're given a
11442 compile_unit, then use the line header hash table if it's already
11443 created, but don't create one just yet. */
11444
11445 if (dwarf2_per_objfile->line_header_hash == NULL
11446 && die->tag == DW_TAG_partial_unit)
2ab95328 11447 {
527f3840
JK
11448 dwarf2_per_objfile->line_header_hash
11449 = htab_create_alloc_ex (127, line_header_hash_voidp,
11450 line_header_eq_voidp,
11451 free_line_header_voidp,
11452 &objfile->objfile_obstack,
11453 hashtab_obstack_allocate,
11454 dummy_obstack_deallocate);
11455 }
2ab95328 11456
9c541725 11457 line_header_local.sect_off = line_offset;
527f3840
JK
11458 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11459 line_header_local_hash = line_header_hash (&line_header_local);
11460 if (dwarf2_per_objfile->line_header_hash != NULL)
11461 {
11462 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11463 &line_header_local,
11464 line_header_local_hash, NO_INSERT);
11465
11466 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11467 is not present in *SLOT (since if there is something in *SLOT then
11468 it will be for a partial_unit). */
11469 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11470 {
527f3840 11471 gdb_assert (*slot != NULL);
9a3c8263 11472 cu->line_header = (struct line_header *) *slot;
527f3840 11473 return;
dee91e82 11474 }
2ab95328 11475 }
527f3840
JK
11476
11477 /* dwarf_decode_line_header does not yet provide sufficient information.
11478 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11479 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11480 if (lh == NULL)
527f3840 11481 return;
4c8aa72d
PA
11482
11483 cu->line_header = lh.release ();
11484 cu->line_header_die_owner = die;
527f3840
JK
11485
11486 if (dwarf2_per_objfile->line_header_hash == NULL)
11487 slot = NULL;
11488 else
11489 {
11490 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11491 &line_header_local,
11492 line_header_local_hash, INSERT);
11493 gdb_assert (slot != NULL);
11494 }
11495 if (slot != NULL && *slot == NULL)
11496 {
11497 /* This newly decoded line number information unit will be owned
11498 by line_header_hash hash table. */
11499 *slot = cu->line_header;
4c8aa72d 11500 cu->line_header_die_owner = NULL;
527f3840
JK
11501 }
11502 else
11503 {
11504 /* We cannot free any current entry in (*slot) as that struct line_header
11505 may be already used by multiple CUs. Create only temporary decoded
11506 line_header for this CU - it may happen at most once for each line
11507 number information unit. And if we're not using line_header_hash
11508 then this is what we want as well. */
11509 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11510 }
11511 decode_mapping = (die->tag != DW_TAG_partial_unit);
11512 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11513 decode_mapping);
fff8551c 11514
2ab95328
TT
11515}
11516
95554aad 11517/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11518
c906108c 11519static void
e7c27a73 11520read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11521{
518817b3
SM
11522 struct dwarf2_per_objfile *dwarf2_per_objfile
11523 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11524 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11525 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11526 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11527 CORE_ADDR highpc = ((CORE_ADDR) 0);
11528 struct attribute *attr;
c906108c 11529 struct die_info *child_die;
e142c38c 11530 CORE_ADDR baseaddr;
6e70227d 11531
380618d6 11532 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11533 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11534
fae299cd 11535 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11536
11537 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11538 from finish_block. */
2acceee2 11539 if (lowpc == ((CORE_ADDR) -1))
c906108c 11540 lowpc = highpc;
3e29f34a 11541 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11542
d721ba37 11543 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11544
f4b8a18d
KW
11545 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11546 standardised yet. As a workaround for the language detection we fall
11547 back to the DW_AT_producer string. */
11548 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11549 cu->language = language_opencl;
11550
3019eac3
DE
11551 /* Similar hack for Go. */
11552 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11553 set_cu_language (DW_LANG_Go, cu);
11554
c24bdb02 11555 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11556
11557 /* Decode line number information if present. We do this before
11558 processing child DIEs, so that the line header table is available
11559 for DW_AT_decl_file. */
d721ba37 11560 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11561
11562 /* Process all dies in compilation unit. */
11563 if (die->child != NULL)
11564 {
11565 child_die = die->child;
11566 while (child_die && child_die->tag)
11567 {
11568 process_die (child_die, cu);
11569 child_die = sibling_die (child_die);
11570 }
11571 }
11572
11573 /* Decode macro information, if present. Dwarf 2 macro information
11574 refers to information in the line number info statement program
11575 header, so we can only read it if we've read the header
11576 successfully. */
0af92d60
JK
11577 attr = dwarf2_attr (die, DW_AT_macros, cu);
11578 if (attr == NULL)
11579 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11580 if (attr && cu->line_header)
11581 {
11582 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11583 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11584
43f3e411 11585 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11586 }
11587 else
11588 {
11589 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11590 if (attr && cu->line_header)
11591 {
11592 unsigned int macro_offset = DW_UNSND (attr);
11593
43f3e411 11594 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11595 }
11596 }
3019eac3
DE
11597}
11598
c24bdb02
KS
11599void
11600dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11601{
f4dc4d17
DE
11602 struct type_unit_group *tu_group;
11603 int first_time;
3019eac3 11604 struct attribute *attr;
9c541725 11605 unsigned int i;
0186c6a7 11606 struct signatured_type *sig_type;
3019eac3 11607
f4dc4d17 11608 gdb_assert (per_cu->is_debug_types);
0186c6a7 11609 sig_type = (struct signatured_type *) per_cu;
3019eac3 11610
c24bdb02 11611 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11612
f4dc4d17 11613 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11614 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11615 if (sig_type->type_unit_group == NULL)
c24bdb02 11616 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11617 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11618
11619 /* If we've already processed this stmt_list there's no real need to
11620 do it again, we could fake it and just recreate the part we need
11621 (file name,index -> symtab mapping). If data shows this optimization
11622 is useful we can do it then. */
43f3e411 11623 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11624
11625 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11626 debug info. */
fff8551c 11627 line_header_up lh;
f4dc4d17 11628 if (attr != NULL)
3019eac3 11629 {
9c541725 11630 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11631 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11632 }
11633 if (lh == NULL)
11634 {
11635 if (first_time)
c24bdb02 11636 start_symtab ("", NULL, 0);
f4dc4d17
DE
11637 else
11638 {
11639 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11640 gdb_assert (m_builder == nullptr);
804d2729 11641 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11642 m_builder.reset (new struct buildsym_compunit
11643 (COMPUNIT_OBJFILE (cust), "",
11644 COMPUNIT_DIRNAME (cust),
11645 compunit_language (cust),
11646 0, cust));
f4dc4d17 11647 }
f4dc4d17 11648 return;
3019eac3
DE
11649 }
11650
c24bdb02
KS
11651 line_header = lh.release ();
11652 line_header_die_owner = die;
3019eac3 11653
f4dc4d17
DE
11654 if (first_time)
11655 {
c24bdb02 11656 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11657
1fd60fc0
DE
11658 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11659 still initializing it, and our caller (a few levels up)
11660 process_full_type_unit still needs to know if this is the first
11661 time. */
11662
c24bdb02 11663 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11664 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11665 line_header->file_names.size ());
3019eac3 11666
c24bdb02 11667 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11668 {
c24bdb02 11669 file_entry &fe = line_header->file_names[i];
3019eac3 11670
c24bdb02
KS
11671 dwarf2_start_subfile (this, fe.name,
11672 fe.include_dir (line_header));
11673 buildsym_compunit *b = get_builder ();
11674 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11675 {
4c8aa72d
PA
11676 /* NOTE: start_subfile will recognize when it's been
11677 passed a file it has already seen. So we can't
11678 assume there's a simple mapping from
11679 cu->line_header->file_names to subfiles, plus
11680 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11681 b->get_current_subfile ()->symtab
11682 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11683 }
11684
c24bdb02 11685 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11686 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11687 }
11688 }
11689 else
3019eac3 11690 {
c24bdb02 11691 gdb_assert (m_builder == nullptr);
804d2729 11692 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11693 m_builder.reset (new struct buildsym_compunit
11694 (COMPUNIT_OBJFILE (cust), "",
11695 COMPUNIT_DIRNAME (cust),
11696 compunit_language (cust),
11697 0, cust));
f4dc4d17 11698
c24bdb02 11699 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11700 {
c24bdb02 11701 file_entry &fe = line_header->file_names[i];
f4dc4d17 11702
4c8aa72d 11703 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11704 }
3019eac3
DE
11705 }
11706
f4dc4d17
DE
11707 /* The main symtab is allocated last. Type units don't have DW_AT_name
11708 so they don't have a "real" (so to speak) symtab anyway.
11709 There is later code that will assign the main symtab to all symbols
11710 that don't have one. We need to handle the case of a symbol with a
11711 missing symtab (DW_AT_decl_file) anyway. */
11712}
3019eac3 11713
f4dc4d17
DE
11714/* Process DW_TAG_type_unit.
11715 For TUs we want to skip the first top level sibling if it's not the
11716 actual type being defined by this TU. In this case the first top
11717 level sibling is there to provide context only. */
3019eac3 11718
f4dc4d17
DE
11719static void
11720read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11721{
11722 struct die_info *child_die;
3019eac3 11723
f4dc4d17
DE
11724 prepare_one_comp_unit (cu, die, language_minimal);
11725
11726 /* Initialize (or reinitialize) the machinery for building symtabs.
11727 We do this before processing child DIEs, so that the line header table
11728 is available for DW_AT_decl_file. */
c24bdb02 11729 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11730
11731 if (die->child != NULL)
11732 {
11733 child_die = die->child;
11734 while (child_die && child_die->tag)
11735 {
11736 process_die (child_die, cu);
11737 child_die = sibling_die (child_die);
11738 }
11739 }
3019eac3
DE
11740}
11741\f
80626a55
DE
11742/* DWO/DWP files.
11743
11744 http://gcc.gnu.org/wiki/DebugFission
11745 http://gcc.gnu.org/wiki/DebugFissionDWP
11746
11747 To simplify handling of both DWO files ("object" files with the DWARF info)
11748 and DWP files (a file with the DWOs packaged up into one file), we treat
11749 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11750
11751static hashval_t
11752hash_dwo_file (const void *item)
11753{
9a3c8263 11754 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11755 hashval_t hash;
3019eac3 11756
a2ce51a0
DE
11757 hash = htab_hash_string (dwo_file->dwo_name);
11758 if (dwo_file->comp_dir != NULL)
11759 hash += htab_hash_string (dwo_file->comp_dir);
11760 return hash;
3019eac3
DE
11761}
11762
11763static int
11764eq_dwo_file (const void *item_lhs, const void *item_rhs)
11765{
9a3c8263
SM
11766 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11767 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11768
a2ce51a0
DE
11769 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11770 return 0;
11771 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11772 return lhs->comp_dir == rhs->comp_dir;
11773 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11774}
11775
11776/* Allocate a hash table for DWO files. */
11777
11778static htab_t
ed2dc618 11779allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11780{
3019eac3
DE
11781 return htab_create_alloc_ex (41,
11782 hash_dwo_file,
11783 eq_dwo_file,
11784 NULL,
11785 &objfile->objfile_obstack,
11786 hashtab_obstack_allocate,
11787 dummy_obstack_deallocate);
11788}
11789
80626a55
DE
11790/* Lookup DWO file DWO_NAME. */
11791
11792static void **
ed2dc618
SM
11793lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11794 const char *dwo_name,
11795 const char *comp_dir)
80626a55
DE
11796{
11797 struct dwo_file find_entry;
11798 void **slot;
11799
11800 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11801 dwarf2_per_objfile->dwo_files
11802 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11803
11804 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11805 find_entry.dwo_name = dwo_name;
11806 find_entry.comp_dir = comp_dir;
80626a55
DE
11807 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11808
11809 return slot;
11810}
11811
3019eac3
DE
11812static hashval_t
11813hash_dwo_unit (const void *item)
11814{
9a3c8263 11815 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11816
11817 /* This drops the top 32 bits of the id, but is ok for a hash. */
11818 return dwo_unit->signature;
11819}
11820
11821static int
11822eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11823{
9a3c8263
SM
11824 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11825 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11826
11827 /* The signature is assumed to be unique within the DWO file.
11828 So while object file CU dwo_id's always have the value zero,
11829 that's OK, assuming each object file DWO file has only one CU,
11830 and that's the rule for now. */
11831 return lhs->signature == rhs->signature;
11832}
11833
11834/* Allocate a hash table for DWO CUs,TUs.
11835 There is one of these tables for each of CUs,TUs for each DWO file. */
11836
11837static htab_t
11838allocate_dwo_unit_table (struct objfile *objfile)
11839{
11840 /* Start out with a pretty small number.
11841 Generally DWO files contain only one CU and maybe some TUs. */
11842 return htab_create_alloc_ex (3,
11843 hash_dwo_unit,
11844 eq_dwo_unit,
11845 NULL,
11846 &objfile->objfile_obstack,
11847 hashtab_obstack_allocate,
11848 dummy_obstack_deallocate);
11849}
11850
80626a55 11851/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11852
19c3d4c9 11853struct create_dwo_cu_data
3019eac3
DE
11854{
11855 struct dwo_file *dwo_file;
19c3d4c9 11856 struct dwo_unit dwo_unit;
3019eac3
DE
11857};
11858
19c3d4c9 11859/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11860
11861static void
19c3d4c9
DE
11862create_dwo_cu_reader (const struct die_reader_specs *reader,
11863 const gdb_byte *info_ptr,
11864 struct die_info *comp_unit_die,
11865 int has_children,
11866 void *datap)
3019eac3
DE
11867{
11868 struct dwarf2_cu *cu = reader->cu;
9c541725 11869 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11870 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11871 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11872 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11873 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11874 struct attribute *attr;
3019eac3
DE
11875
11876 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11877 if (attr == NULL)
11878 {
b98664d3 11879 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11880 " its dwo_id [in module %s]"),
9d8780f0 11881 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11882 return;
11883 }
11884
3019eac3
DE
11885 dwo_unit->dwo_file = dwo_file;
11886 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11887 dwo_unit->section = section;
9c541725 11888 dwo_unit->sect_off = sect_off;
3019eac3
DE
11889 dwo_unit->length = cu->per_cu->length;
11890
b4f54984 11891 if (dwarf_read_debug)
9d8780f0
SM
11892 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11893 sect_offset_str (sect_off),
9c541725 11894 hex_string (dwo_unit->signature));
3019eac3
DE
11895}
11896
33c5cd75 11897/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11898 Note: This function processes DWO files only, not DWP files. */
3019eac3 11899
33c5cd75 11900static void
ed2dc618
SM
11901create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11902 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11903 htab_t &cus_htab)
3019eac3
DE
11904{
11905 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11906 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11907
33c5cd75
DB
11908 dwarf2_read_section (objfile, &section);
11909 info_ptr = section.buffer;
3019eac3
DE
11910
11911 if (info_ptr == NULL)
33c5cd75 11912 return;
3019eac3 11913
b4f54984 11914 if (dwarf_read_debug)
19c3d4c9
DE
11915 {
11916 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11917 get_section_name (&section),
11918 get_section_file_name (&section));
19c3d4c9 11919 }
3019eac3 11920
33c5cd75 11921 end_ptr = info_ptr + section.size;
3019eac3
DE
11922 while (info_ptr < end_ptr)
11923 {
11924 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11925 struct create_dwo_cu_data create_dwo_cu_data;
11926 struct dwo_unit *dwo_unit;
11927 void **slot;
11928 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11929
19c3d4c9
DE
11930 memset (&create_dwo_cu_data.dwo_unit, 0,
11931 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11932 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11933 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11934 per_cu.is_debug_types = 0;
33c5cd75
DB
11935 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11936 per_cu.section = &section;
c5ed0576 11937 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11938
11939 init_cutu_and_read_dies_no_follow (
11940 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11941 info_ptr += per_cu.length;
11942
11943 // If the unit could not be parsed, skip it.
11944 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11945 continue;
3019eac3 11946
33c5cd75
DB
11947 if (cus_htab == NULL)
11948 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11949
33c5cd75
DB
11950 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11951 *dwo_unit = create_dwo_cu_data.dwo_unit;
11952 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11953 gdb_assert (slot != NULL);
11954 if (*slot != NULL)
19c3d4c9 11955 {
33c5cd75
DB
11956 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11957 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11958
b98664d3 11959 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11960 " the entry at offset %s, signature %s"),
11961 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11962 hex_string (dwo_unit->signature));
19c3d4c9 11963 }
33c5cd75 11964 *slot = (void *)dwo_unit;
3019eac3 11965 }
3019eac3
DE
11966}
11967
80626a55
DE
11968/* DWP file .debug_{cu,tu}_index section format:
11969 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11970
d2415c6c
DE
11971 DWP Version 1:
11972
80626a55
DE
11973 Both index sections have the same format, and serve to map a 64-bit
11974 signature to a set of section numbers. Each section begins with a header,
11975 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11976 indexes, and a pool of 32-bit section numbers. The index sections will be
11977 aligned at 8-byte boundaries in the file.
11978
d2415c6c
DE
11979 The index section header consists of:
11980
11981 V, 32 bit version number
11982 -, 32 bits unused
11983 N, 32 bit number of compilation units or type units in the index
11984 M, 32 bit number of slots in the hash table
80626a55 11985
d2415c6c 11986 Numbers are recorded using the byte order of the application binary.
80626a55 11987
d2415c6c
DE
11988 The hash table begins at offset 16 in the section, and consists of an array
11989 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11990 order of the application binary). Unused slots in the hash table are 0.
11991 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11992
d2415c6c
DE
11993 The parallel table begins immediately after the hash table
11994 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11995 array of 32-bit indexes (using the byte order of the application binary),
11996 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11997 table contains a 32-bit index into the pool of section numbers. For unused
11998 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11999
73869dc2
DE
12000 The pool of section numbers begins immediately following the hash table
12001 (at offset 16 + 12 * M from the beginning of the section). The pool of
12002 section numbers consists of an array of 32-bit words (using the byte order
12003 of the application binary). Each item in the array is indexed starting
12004 from 0. The hash table entry provides the index of the first section
12005 number in the set. Additional section numbers in the set follow, and the
12006 set is terminated by a 0 entry (section number 0 is not used in ELF).
12007
12008 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12009 section must be the first entry in the set, and the .debug_abbrev.dwo must
12010 be the second entry. Other members of the set may follow in any order.
12011
12012 ---
12013
12014 DWP Version 2:
12015
12016 DWP Version 2 combines all the .debug_info, etc. sections into one,
12017 and the entries in the index tables are now offsets into these sections.
12018 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12019 section.
12020
12021 Index Section Contents:
12022 Header
12023 Hash Table of Signatures dwp_hash_table.hash_table
12024 Parallel Table of Indices dwp_hash_table.unit_table
12025 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12026 Table of Section Sizes dwp_hash_table.v2.sizes
12027
12028 The index section header consists of:
12029
12030 V, 32 bit version number
12031 L, 32 bit number of columns in the table of section offsets
12032 N, 32 bit number of compilation units or type units in the index
12033 M, 32 bit number of slots in the hash table
12034
12035 Numbers are recorded using the byte order of the application binary.
12036
12037 The hash table has the same format as version 1.
12038 The parallel table of indices has the same format as version 1,
12039 except that the entries are origin-1 indices into the table of sections
12040 offsets and the table of section sizes.
12041
12042 The table of offsets begins immediately following the parallel table
12043 (at offset 16 + 12 * M from the beginning of the section). The table is
12044 a two-dimensional array of 32-bit words (using the byte order of the
12045 application binary), with L columns and N+1 rows, in row-major order.
12046 Each row in the array is indexed starting from 0. The first row provides
12047 a key to the remaining rows: each column in this row provides an identifier
12048 for a debug section, and the offsets in the same column of subsequent rows
12049 refer to that section. The section identifiers are:
12050
12051 DW_SECT_INFO 1 .debug_info.dwo
12052 DW_SECT_TYPES 2 .debug_types.dwo
12053 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12054 DW_SECT_LINE 4 .debug_line.dwo
12055 DW_SECT_LOC 5 .debug_loc.dwo
12056 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12057 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12058 DW_SECT_MACRO 8 .debug_macro.dwo
12059
12060 The offsets provided by the CU and TU index sections are the base offsets
12061 for the contributions made by each CU or TU to the corresponding section
12062 in the package file. Each CU and TU header contains an abbrev_offset
12063 field, used to find the abbreviations table for that CU or TU within the
12064 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12065 be interpreted as relative to the base offset given in the index section.
12066 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12067 should be interpreted as relative to the base offset for .debug_line.dwo,
12068 and offsets into other debug sections obtained from DWARF attributes should
12069 also be interpreted as relative to the corresponding base offset.
12070
12071 The table of sizes begins immediately following the table of offsets.
12072 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12073 with L columns and N rows, in row-major order. Each row in the array is
12074 indexed starting from 1 (row 0 is shared by the two tables).
12075
12076 ---
12077
12078 Hash table lookup is handled the same in version 1 and 2:
12079
12080 We assume that N and M will not exceed 2^32 - 1.
12081 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12082
d2415c6c
DE
12083 Given a 64-bit compilation unit signature or a type signature S, an entry
12084 in the hash table is located as follows:
80626a55 12085
d2415c6c
DE
12086 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12087 the low-order k bits all set to 1.
80626a55 12088
d2415c6c 12089 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12090
d2415c6c
DE
12091 3) If the hash table entry at index H matches the signature, use that
12092 entry. If the hash table entry at index H is unused (all zeroes),
12093 terminate the search: the signature is not present in the table.
80626a55 12094
d2415c6c 12095 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12096
d2415c6c 12097 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12098 to stop at an unused slot or find the match. */
80626a55
DE
12099
12100/* Create a hash table to map DWO IDs to their CU/TU entry in
12101 .debug_{info,types}.dwo in DWP_FILE.
12102 Returns NULL if there isn't one.
12103 Note: This function processes DWP files only, not DWO files. */
12104
12105static struct dwp_hash_table *
ed2dc618
SM
12106create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12107 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12108{
12109 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12110 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12111 const gdb_byte *index_ptr, *index_end;
80626a55 12112 struct dwarf2_section_info *index;
73869dc2 12113 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12114 struct dwp_hash_table *htab;
12115
12116 if (is_debug_types)
12117 index = &dwp_file->sections.tu_index;
12118 else
12119 index = &dwp_file->sections.cu_index;
12120
12121 if (dwarf2_section_empty_p (index))
12122 return NULL;
12123 dwarf2_read_section (objfile, index);
12124
12125 index_ptr = index->buffer;
12126 index_end = index_ptr + index->size;
12127
12128 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12129 index_ptr += 4;
12130 if (version == 2)
12131 nr_columns = read_4_bytes (dbfd, index_ptr);
12132 else
12133 nr_columns = 0;
12134 index_ptr += 4;
80626a55
DE
12135 nr_units = read_4_bytes (dbfd, index_ptr);
12136 index_ptr += 4;
12137 nr_slots = read_4_bytes (dbfd, index_ptr);
12138 index_ptr += 4;
12139
73869dc2 12140 if (version != 1 && version != 2)
80626a55 12141 {
21aa081e 12142 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12143 " [in module %s]"),
21aa081e 12144 pulongest (version), dwp_file->name);
80626a55
DE
12145 }
12146 if (nr_slots != (nr_slots & -nr_slots))
12147 {
21aa081e 12148 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12149 " is not power of 2 [in module %s]"),
21aa081e 12150 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12151 }
12152
12153 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12154 htab->version = version;
12155 htab->nr_columns = nr_columns;
80626a55
DE
12156 htab->nr_units = nr_units;
12157 htab->nr_slots = nr_slots;
12158 htab->hash_table = index_ptr;
12159 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12160
12161 /* Exit early if the table is empty. */
12162 if (nr_slots == 0 || nr_units == 0
12163 || (version == 2 && nr_columns == 0))
12164 {
12165 /* All must be zero. */
12166 if (nr_slots != 0 || nr_units != 0
12167 || (version == 2 && nr_columns != 0))
12168 {
b98664d3 12169 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12170 " all zero [in modules %s]"),
12171 dwp_file->name);
12172 }
12173 return htab;
12174 }
12175
12176 if (version == 1)
12177 {
12178 htab->section_pool.v1.indices =
12179 htab->unit_table + sizeof (uint32_t) * nr_slots;
12180 /* It's harder to decide whether the section is too small in v1.
12181 V1 is deprecated anyway so we punt. */
12182 }
12183 else
12184 {
12185 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12186 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12187 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12188 /* Reverse map for error checking. */
12189 int ids_seen[DW_SECT_MAX + 1];
12190 int i;
12191
12192 if (nr_columns < 2)
12193 {
12194 error (_("Dwarf Error: bad DWP hash table, too few columns"
12195 " in section table [in module %s]"),
12196 dwp_file->name);
12197 }
12198 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12199 {
12200 error (_("Dwarf Error: bad DWP hash table, too many columns"
12201 " in section table [in module %s]"),
12202 dwp_file->name);
12203 }
04fd5eed
GB
12204 memset (ids, 255, sizeof_ids);
12205 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12206 for (i = 0; i < nr_columns; ++i)
12207 {
12208 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12209
12210 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12211 {
12212 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12213 " in section table [in module %s]"),
12214 id, dwp_file->name);
12215 }
12216 if (ids_seen[id] != -1)
12217 {
12218 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12219 " id %d in section table [in module %s]"),
12220 id, dwp_file->name);
12221 }
12222 ids_seen[id] = i;
12223 ids[i] = id;
12224 }
12225 /* Must have exactly one info or types section. */
12226 if (((ids_seen[DW_SECT_INFO] != -1)
12227 + (ids_seen[DW_SECT_TYPES] != -1))
12228 != 1)
12229 {
12230 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12231 " DWO info/types section [in module %s]"),
12232 dwp_file->name);
12233 }
12234 /* Must have an abbrev section. */
12235 if (ids_seen[DW_SECT_ABBREV] == -1)
12236 {
12237 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12238 " section [in module %s]"),
12239 dwp_file->name);
12240 }
12241 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12242 htab->section_pool.v2.sizes =
12243 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12244 * nr_units * nr_columns);
12245 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12246 * nr_units * nr_columns))
12247 > index_end)
12248 {
12249 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12250 " [in module %s]"),
12251 dwp_file->name);
12252 }
12253 }
80626a55
DE
12254
12255 return htab;
12256}
12257
12258/* Update SECTIONS with the data from SECTP.
12259
12260 This function is like the other "locate" section routines that are
12261 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12262 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12263
12264 The result is non-zero for success, or zero if an error was found. */
12265
12266static int
73869dc2
DE
12267locate_v1_virtual_dwo_sections (asection *sectp,
12268 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12269{
12270 const struct dwop_section_names *names = &dwop_section_names;
12271
12272 if (section_is_p (sectp->name, &names->abbrev_dwo))
12273 {
12274 /* There can be only one. */
049412e3 12275 if (sections->abbrev.s.section != NULL)
80626a55 12276 return 0;
049412e3 12277 sections->abbrev.s.section = sectp;
80626a55
DE
12278 sections->abbrev.size = bfd_get_section_size (sectp);
12279 }
12280 else if (section_is_p (sectp->name, &names->info_dwo)
12281 || section_is_p (sectp->name, &names->types_dwo))
12282 {
12283 /* There can be only one. */
049412e3 12284 if (sections->info_or_types.s.section != NULL)
80626a55 12285 return 0;
049412e3 12286 sections->info_or_types.s.section = sectp;
80626a55
DE
12287 sections->info_or_types.size = bfd_get_section_size (sectp);
12288 }
12289 else if (section_is_p (sectp->name, &names->line_dwo))
12290 {
12291 /* There can be only one. */
049412e3 12292 if (sections->line.s.section != NULL)
80626a55 12293 return 0;
049412e3 12294 sections->line.s.section = sectp;
80626a55
DE
12295 sections->line.size = bfd_get_section_size (sectp);
12296 }
12297 else if (section_is_p (sectp->name, &names->loc_dwo))
12298 {
12299 /* There can be only one. */
049412e3 12300 if (sections->loc.s.section != NULL)
80626a55 12301 return 0;
049412e3 12302 sections->loc.s.section = sectp;
80626a55
DE
12303 sections->loc.size = bfd_get_section_size (sectp);
12304 }
12305 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12306 {
12307 /* There can be only one. */
049412e3 12308 if (sections->macinfo.s.section != NULL)
80626a55 12309 return 0;
049412e3 12310 sections->macinfo.s.section = sectp;
80626a55
DE
12311 sections->macinfo.size = bfd_get_section_size (sectp);
12312 }
12313 else if (section_is_p (sectp->name, &names->macro_dwo))
12314 {
12315 /* There can be only one. */
049412e3 12316 if (sections->macro.s.section != NULL)
80626a55 12317 return 0;
049412e3 12318 sections->macro.s.section = sectp;
80626a55
DE
12319 sections->macro.size = bfd_get_section_size (sectp);
12320 }
12321 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12322 {
12323 /* There can be only one. */
049412e3 12324 if (sections->str_offsets.s.section != NULL)
80626a55 12325 return 0;
049412e3 12326 sections->str_offsets.s.section = sectp;
80626a55
DE
12327 sections->str_offsets.size = bfd_get_section_size (sectp);
12328 }
12329 else
12330 {
12331 /* No other kind of section is valid. */
12332 return 0;
12333 }
12334
12335 return 1;
12336}
12337
73869dc2
DE
12338/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12339 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12340 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12341 This is for DWP version 1 files. */
80626a55
DE
12342
12343static struct dwo_unit *
ed2dc618
SM
12344create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12345 struct dwp_file *dwp_file,
73869dc2
DE
12346 uint32_t unit_index,
12347 const char *comp_dir,
12348 ULONGEST signature, int is_debug_types)
80626a55
DE
12349{
12350 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12351 const struct dwp_hash_table *dwp_htab =
12352 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12353 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12354 const char *kind = is_debug_types ? "TU" : "CU";
12355 struct dwo_file *dwo_file;
12356 struct dwo_unit *dwo_unit;
73869dc2 12357 struct virtual_v1_dwo_sections sections;
80626a55 12358 void **dwo_file_slot;
80626a55
DE
12359 int i;
12360
73869dc2
DE
12361 gdb_assert (dwp_file->version == 1);
12362
b4f54984 12363 if (dwarf_read_debug)
80626a55 12364 {
73869dc2 12365 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12366 kind,
73869dc2 12367 pulongest (unit_index), hex_string (signature),
80626a55
DE
12368 dwp_file->name);
12369 }
12370
19ac8c2e 12371 /* Fetch the sections of this DWO unit.
80626a55
DE
12372 Put a limit on the number of sections we look for so that bad data
12373 doesn't cause us to loop forever. */
12374
73869dc2 12375#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12376 (1 /* .debug_info or .debug_types */ \
12377 + 1 /* .debug_abbrev */ \
12378 + 1 /* .debug_line */ \
12379 + 1 /* .debug_loc */ \
12380 + 1 /* .debug_str_offsets */ \
19ac8c2e 12381 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12382 + 1 /* trailing zero */)
12383
12384 memset (&sections, 0, sizeof (sections));
80626a55 12385
73869dc2 12386 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12387 {
12388 asection *sectp;
12389 uint32_t section_nr =
12390 read_4_bytes (dbfd,
73869dc2
DE
12391 dwp_htab->section_pool.v1.indices
12392 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12393
12394 if (section_nr == 0)
12395 break;
12396 if (section_nr >= dwp_file->num_sections)
12397 {
12398 error (_("Dwarf Error: bad DWP hash table, section number too large"
12399 " [in module %s]"),
12400 dwp_file->name);
12401 }
12402
12403 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12404 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12405 {
12406 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12407 " [in module %s]"),
12408 dwp_file->name);
12409 }
12410 }
12411
12412 if (i < 2
a32a8923
DE
12413 || dwarf2_section_empty_p (&sections.info_or_types)
12414 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12415 {
12416 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12417 " [in module %s]"),
12418 dwp_file->name);
12419 }
73869dc2 12420 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12421 {
12422 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12423 " [in module %s]"),
12424 dwp_file->name);
12425 }
12426
12427 /* It's easier for the rest of the code if we fake a struct dwo_file and
12428 have dwo_unit "live" in that. At least for now.
12429
12430 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12431 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12432 file, we can combine them back into a virtual DWO file to save space
12433 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12434 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12435
791afaa2
TT
12436 std::string virtual_dwo_name =
12437 string_printf ("virtual-dwo/%d-%d-%d-%d",
12438 get_section_id (&sections.abbrev),
12439 get_section_id (&sections.line),
12440 get_section_id (&sections.loc),
12441 get_section_id (&sections.str_offsets));
80626a55 12442 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12443 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12444 virtual_dwo_name.c_str (),
12445 comp_dir);
80626a55
DE
12446 /* Create one if necessary. */
12447 if (*dwo_file_slot == NULL)
12448 {
b4f54984 12449 if (dwarf_read_debug)
80626a55
DE
12450 {
12451 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12452 virtual_dwo_name.c_str ());
80626a55
DE
12453 }
12454 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12455 dwo_file->dwo_name
12456 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12457 virtual_dwo_name.c_str (),
12458 virtual_dwo_name.size ());
0ac5b59e 12459 dwo_file->comp_dir = comp_dir;
80626a55
DE
12460 dwo_file->sections.abbrev = sections.abbrev;
12461 dwo_file->sections.line = sections.line;
12462 dwo_file->sections.loc = sections.loc;
12463 dwo_file->sections.macinfo = sections.macinfo;
12464 dwo_file->sections.macro = sections.macro;
12465 dwo_file->sections.str_offsets = sections.str_offsets;
12466 /* The "str" section is global to the entire DWP file. */
12467 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12468 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12469 there's no need to record it in dwo_file.
12470 Also, we can't simply record type sections in dwo_file because
12471 we record a pointer into the vector in dwo_unit. As we collect more
12472 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12473 for it, invalidating all copies of pointers into the previous
12474 contents. */
80626a55
DE
12475 *dwo_file_slot = dwo_file;
12476 }
12477 else
12478 {
b4f54984 12479 if (dwarf_read_debug)
80626a55
DE
12480 {
12481 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12482 virtual_dwo_name.c_str ());
80626a55 12483 }
9a3c8263 12484 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12485 }
80626a55
DE
12486
12487 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12488 dwo_unit->dwo_file = dwo_file;
12489 dwo_unit->signature = signature;
8d749320
SM
12490 dwo_unit->section =
12491 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12492 *dwo_unit->section = sections.info_or_types;
57d63ce2 12493 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12494
12495 return dwo_unit;
12496}
12497
73869dc2
DE
12498/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12499 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12500 piece within that section used by a TU/CU, return a virtual section
12501 of just that piece. */
12502
12503static struct dwarf2_section_info
ed2dc618
SM
12504create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12505 struct dwarf2_section_info *section,
73869dc2
DE
12506 bfd_size_type offset, bfd_size_type size)
12507{
12508 struct dwarf2_section_info result;
12509 asection *sectp;
12510
12511 gdb_assert (section != NULL);
12512 gdb_assert (!section->is_virtual);
12513
12514 memset (&result, 0, sizeof (result));
12515 result.s.containing_section = section;
12516 result.is_virtual = 1;
12517
12518 if (size == 0)
12519 return result;
12520
12521 sectp = get_section_bfd_section (section);
12522
12523 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12524 bounds of the real section. This is a pretty-rare event, so just
12525 flag an error (easier) instead of a warning and trying to cope. */
12526 if (sectp == NULL
12527 || offset + size > bfd_get_section_size (sectp))
12528 {
73869dc2
DE
12529 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12530 " in section %s [in module %s]"),
12531 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12532 objfile_name (dwarf2_per_objfile->objfile));
12533 }
12534
12535 result.virtual_offset = offset;
12536 result.size = size;
12537 return result;
12538}
12539
12540/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12541 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12542 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12543 This is for DWP version 2 files. */
12544
12545static struct dwo_unit *
ed2dc618
SM
12546create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12547 struct dwp_file *dwp_file,
73869dc2
DE
12548 uint32_t unit_index,
12549 const char *comp_dir,
12550 ULONGEST signature, int is_debug_types)
12551{
12552 struct objfile *objfile = dwarf2_per_objfile->objfile;
12553 const struct dwp_hash_table *dwp_htab =
12554 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12555 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12556 const char *kind = is_debug_types ? "TU" : "CU";
12557 struct dwo_file *dwo_file;
12558 struct dwo_unit *dwo_unit;
12559 struct virtual_v2_dwo_sections sections;
12560 void **dwo_file_slot;
73869dc2
DE
12561 int i;
12562
12563 gdb_assert (dwp_file->version == 2);
12564
b4f54984 12565 if (dwarf_read_debug)
73869dc2
DE
12566 {
12567 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12568 kind,
12569 pulongest (unit_index), hex_string (signature),
12570 dwp_file->name);
12571 }
12572
12573 /* Fetch the section offsets of this DWO unit. */
12574
12575 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12576
12577 for (i = 0; i < dwp_htab->nr_columns; ++i)
12578 {
12579 uint32_t offset = read_4_bytes (dbfd,
12580 dwp_htab->section_pool.v2.offsets
12581 + (((unit_index - 1) * dwp_htab->nr_columns
12582 + i)
12583 * sizeof (uint32_t)));
12584 uint32_t size = read_4_bytes (dbfd,
12585 dwp_htab->section_pool.v2.sizes
12586 + (((unit_index - 1) * dwp_htab->nr_columns
12587 + i)
12588 * sizeof (uint32_t)));
12589
12590 switch (dwp_htab->section_pool.v2.section_ids[i])
12591 {
12592 case DW_SECT_INFO:
12593 case DW_SECT_TYPES:
12594 sections.info_or_types_offset = offset;
12595 sections.info_or_types_size = size;
12596 break;
12597 case DW_SECT_ABBREV:
12598 sections.abbrev_offset = offset;
12599 sections.abbrev_size = size;
12600 break;
12601 case DW_SECT_LINE:
12602 sections.line_offset = offset;
12603 sections.line_size = size;
12604 break;
12605 case DW_SECT_LOC:
12606 sections.loc_offset = offset;
12607 sections.loc_size = size;
12608 break;
12609 case DW_SECT_STR_OFFSETS:
12610 sections.str_offsets_offset = offset;
12611 sections.str_offsets_size = size;
12612 break;
12613 case DW_SECT_MACINFO:
12614 sections.macinfo_offset = offset;
12615 sections.macinfo_size = size;
12616 break;
12617 case DW_SECT_MACRO:
12618 sections.macro_offset = offset;
12619 sections.macro_size = size;
12620 break;
12621 }
12622 }
12623
12624 /* It's easier for the rest of the code if we fake a struct dwo_file and
12625 have dwo_unit "live" in that. At least for now.
12626
12627 The DWP file can be made up of a random collection of CUs and TUs.
12628 However, for each CU + set of TUs that came from the same original DWO
12629 file, we can combine them back into a virtual DWO file to save space
12630 (fewer struct dwo_file objects to allocate). Remember that for really
12631 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12632
791afaa2
TT
12633 std::string virtual_dwo_name =
12634 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12635 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12636 (long) (sections.line_size ? sections.line_offset : 0),
12637 (long) (sections.loc_size ? sections.loc_offset : 0),
12638 (long) (sections.str_offsets_size
12639 ? sections.str_offsets_offset : 0));
73869dc2 12640 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12641 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12642 virtual_dwo_name.c_str (),
12643 comp_dir);
73869dc2
DE
12644 /* Create one if necessary. */
12645 if (*dwo_file_slot == NULL)
12646 {
b4f54984 12647 if (dwarf_read_debug)
73869dc2
DE
12648 {
12649 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12650 virtual_dwo_name.c_str ());
73869dc2
DE
12651 }
12652 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12653 dwo_file->dwo_name
12654 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12655 virtual_dwo_name.c_str (),
12656 virtual_dwo_name.size ());
73869dc2
DE
12657 dwo_file->comp_dir = comp_dir;
12658 dwo_file->sections.abbrev =
ed2dc618 12659 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12660 sections.abbrev_offset, sections.abbrev_size);
12661 dwo_file->sections.line =
ed2dc618 12662 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12663 sections.line_offset, sections.line_size);
12664 dwo_file->sections.loc =
ed2dc618 12665 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12666 sections.loc_offset, sections.loc_size);
12667 dwo_file->sections.macinfo =
ed2dc618 12668 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12669 sections.macinfo_offset, sections.macinfo_size);
12670 dwo_file->sections.macro =
ed2dc618 12671 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12672 sections.macro_offset, sections.macro_size);
12673 dwo_file->sections.str_offsets =
ed2dc618
SM
12674 create_dwp_v2_section (dwarf2_per_objfile,
12675 &dwp_file->sections.str_offsets,
73869dc2
DE
12676 sections.str_offsets_offset,
12677 sections.str_offsets_size);
12678 /* The "str" section is global to the entire DWP file. */
12679 dwo_file->sections.str = dwp_file->sections.str;
12680 /* The info or types section is assigned below to dwo_unit,
12681 there's no need to record it in dwo_file.
12682 Also, we can't simply record type sections in dwo_file because
12683 we record a pointer into the vector in dwo_unit. As we collect more
12684 types we'll grow the vector and eventually have to reallocate space
12685 for it, invalidating all copies of pointers into the previous
12686 contents. */
12687 *dwo_file_slot = dwo_file;
12688 }
12689 else
12690 {
b4f54984 12691 if (dwarf_read_debug)
73869dc2
DE
12692 {
12693 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12694 virtual_dwo_name.c_str ());
73869dc2 12695 }
9a3c8263 12696 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12697 }
73869dc2
DE
12698
12699 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12700 dwo_unit->dwo_file = dwo_file;
12701 dwo_unit->signature = signature;
8d749320
SM
12702 dwo_unit->section =
12703 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12704 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12705 is_debug_types
73869dc2
DE
12706 ? &dwp_file->sections.types
12707 : &dwp_file->sections.info,
12708 sections.info_or_types_offset,
12709 sections.info_or_types_size);
12710 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12711
12712 return dwo_unit;
12713}
12714
57d63ce2
DE
12715/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12716 Returns NULL if the signature isn't found. */
80626a55
DE
12717
12718static struct dwo_unit *
ed2dc618
SM
12719lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12720 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12721 ULONGEST signature, int is_debug_types)
80626a55 12722{
57d63ce2
DE
12723 const struct dwp_hash_table *dwp_htab =
12724 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12725 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12726 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12727 uint32_t hash = signature & mask;
12728 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12729 unsigned int i;
12730 void **slot;
870f88f7 12731 struct dwo_unit find_dwo_cu;
80626a55
DE
12732
12733 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12734 find_dwo_cu.signature = signature;
19ac8c2e
DE
12735 slot = htab_find_slot (is_debug_types
12736 ? dwp_file->loaded_tus
12737 : dwp_file->loaded_cus,
12738 &find_dwo_cu, INSERT);
80626a55
DE
12739
12740 if (*slot != NULL)
9a3c8263 12741 return (struct dwo_unit *) *slot;
80626a55
DE
12742
12743 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12744 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12745 {
12746 ULONGEST signature_in_table;
12747
12748 signature_in_table =
57d63ce2 12749 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12750 if (signature_in_table == signature)
12751 {
57d63ce2
DE
12752 uint32_t unit_index =
12753 read_4_bytes (dbfd,
12754 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12755
73869dc2
DE
12756 if (dwp_file->version == 1)
12757 {
ed2dc618
SM
12758 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12759 dwp_file, unit_index,
73869dc2
DE
12760 comp_dir, signature,
12761 is_debug_types);
12762 }
12763 else
12764 {
ed2dc618
SM
12765 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12766 dwp_file, unit_index,
73869dc2
DE
12767 comp_dir, signature,
12768 is_debug_types);
12769 }
9a3c8263 12770 return (struct dwo_unit *) *slot;
80626a55
DE
12771 }
12772 if (signature_in_table == 0)
12773 return NULL;
12774 hash = (hash + hash2) & mask;
12775 }
12776
12777 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12778 " [in module %s]"),
12779 dwp_file->name);
12780}
12781
ab5088bf 12782/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12783 Open the file specified by FILE_NAME and hand it off to BFD for
12784 preliminary analysis. Return a newly initialized bfd *, which
12785 includes a canonicalized copy of FILE_NAME.
80626a55 12786 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12787 SEARCH_CWD is true if the current directory is to be searched.
12788 It will be searched before debug-file-directory.
13aaf454
DE
12789 If successful, the file is added to the bfd include table of the
12790 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12791 If unable to find/open the file, return NULL.
3019eac3
DE
12792 NOTE: This function is derived from symfile_bfd_open. */
12793
192b62ce 12794static gdb_bfd_ref_ptr
ed2dc618
SM
12795try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12796 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12797{
24b9144d 12798 int desc;
9c02c129
DE
12799 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12800 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12801 to debug_file_directory. */
e0cc99a6 12802 const char *search_path;
9c02c129
DE
12803 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12804
e0cc99a6 12805 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12806 if (search_cwd)
12807 {
12808 if (*debug_file_directory != '\0')
e0cc99a6
TT
12809 {
12810 search_path_holder.reset (concat (".", dirname_separator_string,
12811 debug_file_directory,
12812 (char *) NULL));
12813 search_path = search_path_holder.get ();
12814 }
6ac97d4c 12815 else
e0cc99a6 12816 search_path = ".";
6ac97d4c 12817 }
9c02c129 12818 else
e0cc99a6 12819 search_path = debug_file_directory;
3019eac3 12820
24b9144d 12821 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12822 if (is_dwp)
12823 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12824
12825 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12826 desc = openp (search_path, flags, file_name,
3019eac3
DE
12827 O_RDONLY | O_BINARY, &absolute_name);
12828 if (desc < 0)
12829 return NULL;
12830
e0cc99a6
TT
12831 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12832 gnutarget, desc));
9c02c129
DE
12833 if (sym_bfd == NULL)
12834 return NULL;
192b62ce 12835 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12836
192b62ce
TT
12837 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12838 return NULL;
3019eac3 12839
13aaf454
DE
12840 /* Success. Record the bfd as having been included by the objfile's bfd.
12841 This is important because things like demangled_names_hash lives in the
12842 objfile's per_bfd space and may have references to things like symbol
12843 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12844 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12845
3019eac3
DE
12846 return sym_bfd;
12847}
12848
ab5088bf 12849/* Try to open DWO file FILE_NAME.
3019eac3
DE
12850 COMP_DIR is the DW_AT_comp_dir attribute.
12851 The result is the bfd handle of the file.
12852 If there is a problem finding or opening the file, return NULL.
12853 Upon success, the canonicalized path of the file is stored in the bfd,
12854 same as symfile_bfd_open. */
12855
192b62ce 12856static gdb_bfd_ref_ptr
ed2dc618
SM
12857open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12858 const char *file_name, const char *comp_dir)
3019eac3 12859{
80626a55 12860 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12861 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12862 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12863
12864 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12865
12866 if (comp_dir != NULL)
12867 {
b36cec19
PA
12868 char *path_to_try = concat (comp_dir, SLASH_STRING,
12869 file_name, (char *) NULL);
3019eac3
DE
12870
12871 /* NOTE: If comp_dir is a relative path, this will also try the
12872 search path, which seems useful. */
ed2dc618
SM
12873 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12874 path_to_try,
12875 0 /*is_dwp*/,
192b62ce 12876 1 /*search_cwd*/));
3019eac3
DE
12877 xfree (path_to_try);
12878 if (abfd != NULL)
12879 return abfd;
12880 }
12881
12882 /* That didn't work, try debug-file-directory, which, despite its name,
12883 is a list of paths. */
12884
12885 if (*debug_file_directory == '\0')
12886 return NULL;
12887
ed2dc618
SM
12888 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12889 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12890}
12891
80626a55
DE
12892/* This function is mapped across the sections and remembers the offset and
12893 size of each of the DWO debugging sections we are interested in. */
12894
12895static void
12896dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12897{
9a3c8263 12898 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12899 const struct dwop_section_names *names = &dwop_section_names;
12900
12901 if (section_is_p (sectp->name, &names->abbrev_dwo))
12902 {
049412e3 12903 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12904 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12905 }
12906 else if (section_is_p (sectp->name, &names->info_dwo))
12907 {
049412e3 12908 dwo_sections->info.s.section = sectp;
80626a55
DE
12909 dwo_sections->info.size = bfd_get_section_size (sectp);
12910 }
12911 else if (section_is_p (sectp->name, &names->line_dwo))
12912 {
049412e3 12913 dwo_sections->line.s.section = sectp;
80626a55
DE
12914 dwo_sections->line.size = bfd_get_section_size (sectp);
12915 }
12916 else if (section_is_p (sectp->name, &names->loc_dwo))
12917 {
049412e3 12918 dwo_sections->loc.s.section = sectp;
80626a55
DE
12919 dwo_sections->loc.size = bfd_get_section_size (sectp);
12920 }
12921 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12922 {
049412e3 12923 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12924 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12925 }
12926 else if (section_is_p (sectp->name, &names->macro_dwo))
12927 {
049412e3 12928 dwo_sections->macro.s.section = sectp;
80626a55
DE
12929 dwo_sections->macro.size = bfd_get_section_size (sectp);
12930 }
12931 else if (section_is_p (sectp->name, &names->str_dwo))
12932 {
049412e3 12933 dwo_sections->str.s.section = sectp;
80626a55
DE
12934 dwo_sections->str.size = bfd_get_section_size (sectp);
12935 }
12936 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12937 {
049412e3 12938 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12939 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12940 }
12941 else if (section_is_p (sectp->name, &names->types_dwo))
12942 {
12943 struct dwarf2_section_info type_section;
12944
12945 memset (&type_section, 0, sizeof (type_section));
049412e3 12946 type_section.s.section = sectp;
80626a55
DE
12947 type_section.size = bfd_get_section_size (sectp);
12948 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12949 &type_section);
12950 }
12951}
12952
ab5088bf 12953/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12954 by PER_CU. This is for the non-DWP case.
80626a55 12955 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12956
12957static struct dwo_file *
0ac5b59e
DE
12958open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12959 const char *dwo_name, const char *comp_dir)
3019eac3 12960{
ed2dc618 12961 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12962 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12963
ed2dc618 12964 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12965 if (dbfd == NULL)
12966 {
b4f54984 12967 if (dwarf_read_debug)
80626a55
DE
12968 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12969 return NULL;
12970 }
263db9a1
TT
12971
12972 /* We use a unique pointer here, despite the obstack allocation,
12973 because a dwo_file needs some cleanup if it is abandoned. */
12974 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12975 struct dwo_file));
0ac5b59e
DE
12976 dwo_file->dwo_name = dwo_name;
12977 dwo_file->comp_dir = comp_dir;
192b62ce 12978 dwo_file->dbfd = dbfd.release ();
3019eac3 12979
192b62ce
TT
12980 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12981 &dwo_file->sections);
3019eac3 12982
ed2dc618
SM
12983 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12984 dwo_file->cus);
3019eac3 12985
263db9a1 12986 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12987 dwo_file->sections.types, dwo_file->tus);
3019eac3 12988
b4f54984 12989 if (dwarf_read_debug)
80626a55
DE
12990 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12991
263db9a1 12992 return dwo_file.release ();
3019eac3
DE
12993}
12994
80626a55 12995/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12996 size of each of the DWP debugging sections common to version 1 and 2 that
12997 we are interested in. */
3019eac3 12998
80626a55 12999static void
73869dc2
DE
13000dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13001 void *dwp_file_ptr)
3019eac3 13002{
9a3c8263 13003 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13004 const struct dwop_section_names *names = &dwop_section_names;
13005 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13006
80626a55 13007 /* Record the ELF section number for later lookup: this is what the
73869dc2 13008 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13009 gdb_assert (elf_section_nr < dwp_file->num_sections);
13010 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13011
80626a55
DE
13012 /* Look for specific sections that we need. */
13013 if (section_is_p (sectp->name, &names->str_dwo))
13014 {
049412e3 13015 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13016 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13017 }
13018 else if (section_is_p (sectp->name, &names->cu_index))
13019 {
049412e3 13020 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13021 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13022 }
13023 else if (section_is_p (sectp->name, &names->tu_index))
13024 {
049412e3 13025 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13026 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13027 }
13028}
3019eac3 13029
73869dc2
DE
13030/* This function is mapped across the sections and remembers the offset and
13031 size of each of the DWP version 2 debugging sections that we are interested
13032 in. This is split into a separate function because we don't know if we
13033 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13034
13035static void
13036dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13037{
9a3c8263 13038 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13039 const struct dwop_section_names *names = &dwop_section_names;
13040 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13041
13042 /* Record the ELF section number for later lookup: this is what the
13043 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13044 gdb_assert (elf_section_nr < dwp_file->num_sections);
13045 dwp_file->elf_sections[elf_section_nr] = sectp;
13046
13047 /* Look for specific sections that we need. */
13048 if (section_is_p (sectp->name, &names->abbrev_dwo))
13049 {
049412e3 13050 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13051 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13052 }
13053 else if (section_is_p (sectp->name, &names->info_dwo))
13054 {
049412e3 13055 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13056 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13057 }
13058 else if (section_is_p (sectp->name, &names->line_dwo))
13059 {
049412e3 13060 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13061 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13062 }
13063 else if (section_is_p (sectp->name, &names->loc_dwo))
13064 {
049412e3 13065 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13066 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13067 }
13068 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13069 {
049412e3 13070 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13071 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13072 }
13073 else if (section_is_p (sectp->name, &names->macro_dwo))
13074 {
049412e3 13075 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13076 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13077 }
13078 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13079 {
049412e3 13080 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13081 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13082 }
13083 else if (section_is_p (sectp->name, &names->types_dwo))
13084 {
049412e3 13085 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13086 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13087 }
13088}
13089
80626a55 13090/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13091
80626a55
DE
13092static hashval_t
13093hash_dwp_loaded_cutus (const void *item)
13094{
9a3c8263 13095 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13096
80626a55
DE
13097 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13098 return dwo_unit->signature;
3019eac3
DE
13099}
13100
80626a55 13101/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13102
80626a55
DE
13103static int
13104eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13105{
9a3c8263
SM
13106 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13107 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13108
80626a55
DE
13109 return dua->signature == dub->signature;
13110}
3019eac3 13111
80626a55 13112/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13113
80626a55
DE
13114static htab_t
13115allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13116{
13117 return htab_create_alloc_ex (3,
13118 hash_dwp_loaded_cutus,
13119 eq_dwp_loaded_cutus,
13120 NULL,
13121 &objfile->objfile_obstack,
13122 hashtab_obstack_allocate,
13123 dummy_obstack_deallocate);
13124}
3019eac3 13125
ab5088bf
DE
13126/* Try to open DWP file FILE_NAME.
13127 The result is the bfd handle of the file.
13128 If there is a problem finding or opening the file, return NULL.
13129 Upon success, the canonicalized path of the file is stored in the bfd,
13130 same as symfile_bfd_open. */
13131
192b62ce 13132static gdb_bfd_ref_ptr
ed2dc618
SM
13133open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13134 const char *file_name)
ab5088bf 13135{
ed2dc618
SM
13136 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13137 1 /*is_dwp*/,
192b62ce 13138 1 /*search_cwd*/));
6ac97d4c
DE
13139 if (abfd != NULL)
13140 return abfd;
13141
13142 /* Work around upstream bug 15652.
13143 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13144 [Whether that's a "bug" is debatable, but it is getting in our way.]
13145 We have no real idea where the dwp file is, because gdb's realpath-ing
13146 of the executable's path may have discarded the needed info.
13147 [IWBN if the dwp file name was recorded in the executable, akin to
13148 .gnu_debuglink, but that doesn't exist yet.]
13149 Strip the directory from FILE_NAME and search again. */
13150 if (*debug_file_directory != '\0')
13151 {
13152 /* Don't implicitly search the current directory here.
13153 If the user wants to search "." to handle this case,
13154 it must be added to debug-file-directory. */
ed2dc618
SM
13155 return try_open_dwop_file (dwarf2_per_objfile,
13156 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13157 0 /*search_cwd*/);
13158 }
13159
13160 return NULL;
ab5088bf
DE
13161}
13162
80626a55
DE
13163/* Initialize the use of the DWP file for the current objfile.
13164 By convention the name of the DWP file is ${objfile}.dwp.
13165 The result is NULL if it can't be found. */
a766d390 13166
400174b1 13167static std::unique_ptr<struct dwp_file>
ed2dc618 13168open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13169{
13170 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13171
82bf32bc
JK
13172 /* Try to find first .dwp for the binary file before any symbolic links
13173 resolving. */
6c447423
DE
13174
13175 /* If the objfile is a debug file, find the name of the real binary
13176 file and get the name of dwp file from there. */
d721ba37 13177 std::string dwp_name;
6c447423
DE
13178 if (objfile->separate_debug_objfile_backlink != NULL)
13179 {
13180 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13181 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13182
d721ba37 13183 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13184 }
13185 else
d721ba37
PA
13186 dwp_name = objfile->original_name;
13187
13188 dwp_name += ".dwp";
80626a55 13189
ed2dc618 13190 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13191 if (dbfd == NULL
13192 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13193 {
13194 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13195 dwp_name = objfile_name (objfile);
13196 dwp_name += ".dwp";
ed2dc618 13197 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13198 }
13199
80626a55
DE
13200 if (dbfd == NULL)
13201 {
b4f54984 13202 if (dwarf_read_debug)
d721ba37 13203 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13204 return std::unique_ptr<dwp_file> ();
3019eac3 13205 }
400174b1
TT
13206
13207 const char *name = bfd_get_filename (dbfd.get ());
13208 std::unique_ptr<struct dwp_file> dwp_file
13209 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13210
0a0f4c01 13211 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13212 dwp_file->elf_sections =
13213 OBSTACK_CALLOC (&objfile->objfile_obstack,
13214 dwp_file->num_sections, asection *);
13215
400174b1
TT
13216 bfd_map_over_sections (dwp_file->dbfd.get (),
13217 dwarf2_locate_common_dwp_sections,
13218 dwp_file.get ());
80626a55 13219
400174b1
TT
13220 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13221 0);
80626a55 13222
400174b1
TT
13223 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13224 1);
80626a55 13225
73869dc2 13226 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13227 if (dwp_file->cus && dwp_file->tus
13228 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13229 {
13230 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13231 pretty bizarre. We use pulongest here because that's the established
4d65956b 13232 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13233 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13234 " TU version %s [in DWP file %s]"),
13235 pulongest (dwp_file->cus->version),
d721ba37 13236 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13237 }
08302ed2
DE
13238
13239 if (dwp_file->cus)
13240 dwp_file->version = dwp_file->cus->version;
13241 else if (dwp_file->tus)
13242 dwp_file->version = dwp_file->tus->version;
13243 else
13244 dwp_file->version = 2;
73869dc2
DE
13245
13246 if (dwp_file->version == 2)
400174b1
TT
13247 bfd_map_over_sections (dwp_file->dbfd.get (),
13248 dwarf2_locate_v2_dwp_sections,
13249 dwp_file.get ());
73869dc2 13250
19ac8c2e
DE
13251 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13252 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13253
b4f54984 13254 if (dwarf_read_debug)
80626a55
DE
13255 {
13256 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13257 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13258 " %s CUs, %s TUs\n",
13259 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13260 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13261 }
13262
13263 return dwp_file;
3019eac3 13264}
c906108c 13265
ab5088bf
DE
13266/* Wrapper around open_and_init_dwp_file, only open it once. */
13267
13268static struct dwp_file *
ed2dc618 13269get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13270{
13271 if (! dwarf2_per_objfile->dwp_checked)
13272 {
ed2dc618
SM
13273 dwarf2_per_objfile->dwp_file
13274 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13275 dwarf2_per_objfile->dwp_checked = 1;
13276 }
400174b1 13277 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13278}
13279
80626a55
DE
13280/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13281 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13282 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13283 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13284 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13285
13286 This is called, for example, when wanting to read a variable with a
13287 complex location. Therefore we don't want to do file i/o for every call.
13288 Therefore we don't want to look for a DWO file on every call.
13289 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13290 then we check if we've already seen DWO_NAME, and only THEN do we check
13291 for a DWO file.
13292
1c658ad5 13293 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13294 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13295
3019eac3 13296static struct dwo_unit *
80626a55
DE
13297lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13298 const char *dwo_name, const char *comp_dir,
13299 ULONGEST signature, int is_debug_types)
3019eac3 13300{
ed2dc618 13301 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13302 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13303 const char *kind = is_debug_types ? "TU" : "CU";
13304 void **dwo_file_slot;
3019eac3 13305 struct dwo_file *dwo_file;
80626a55 13306 struct dwp_file *dwp_file;
cb1df416 13307
6a506a2d
DE
13308 /* First see if there's a DWP file.
13309 If we have a DWP file but didn't find the DWO inside it, don't
13310 look for the original DWO file. It makes gdb behave differently
13311 depending on whether one is debugging in the build tree. */
cf2c3c16 13312
ed2dc618 13313 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13314 if (dwp_file != NULL)
cf2c3c16 13315 {
80626a55
DE
13316 const struct dwp_hash_table *dwp_htab =
13317 is_debug_types ? dwp_file->tus : dwp_file->cus;
13318
13319 if (dwp_htab != NULL)
13320 {
13321 struct dwo_unit *dwo_cutu =
ed2dc618 13322 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13323 signature, is_debug_types);
80626a55
DE
13324
13325 if (dwo_cutu != NULL)
13326 {
b4f54984 13327 if (dwarf_read_debug)
80626a55
DE
13328 {
13329 fprintf_unfiltered (gdb_stdlog,
13330 "Virtual DWO %s %s found: @%s\n",
13331 kind, hex_string (signature),
13332 host_address_to_string (dwo_cutu));
13333 }
13334 return dwo_cutu;
13335 }
13336 }
13337 }
6a506a2d 13338 else
80626a55 13339 {
6a506a2d 13340 /* No DWP file, look for the DWO file. */
80626a55 13341
ed2dc618
SM
13342 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13343 dwo_name, comp_dir);
6a506a2d 13344 if (*dwo_file_slot == NULL)
80626a55 13345 {
6a506a2d
DE
13346 /* Read in the file and build a table of the CUs/TUs it contains. */
13347 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13348 }
6a506a2d 13349 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13350 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13351
6a506a2d 13352 if (dwo_file != NULL)
19c3d4c9 13353 {
6a506a2d
DE
13354 struct dwo_unit *dwo_cutu = NULL;
13355
13356 if (is_debug_types && dwo_file->tus)
13357 {
13358 struct dwo_unit find_dwo_cutu;
13359
13360 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13361 find_dwo_cutu.signature = signature;
9a3c8263
SM
13362 dwo_cutu
13363 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13364 }
33c5cd75 13365 else if (!is_debug_types && dwo_file->cus)
80626a55 13366 {
33c5cd75
DB
13367 struct dwo_unit find_dwo_cutu;
13368
13369 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13370 find_dwo_cutu.signature = signature;
13371 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13372 &find_dwo_cutu);
6a506a2d
DE
13373 }
13374
13375 if (dwo_cutu != NULL)
13376 {
b4f54984 13377 if (dwarf_read_debug)
6a506a2d
DE
13378 {
13379 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13380 kind, dwo_name, hex_string (signature),
13381 host_address_to_string (dwo_cutu));
13382 }
13383 return dwo_cutu;
80626a55
DE
13384 }
13385 }
2e276125 13386 }
9cdd5dbd 13387
80626a55
DE
13388 /* We didn't find it. This could mean a dwo_id mismatch, or
13389 someone deleted the DWO/DWP file, or the search path isn't set up
13390 correctly to find the file. */
13391
b4f54984 13392 if (dwarf_read_debug)
80626a55
DE
13393 {
13394 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13395 kind, dwo_name, hex_string (signature));
13396 }
3019eac3 13397
6656a72d
DE
13398 /* This is a warning and not a complaint because it can be caused by
13399 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13400 {
13401 /* Print the name of the DWP file if we looked there, helps the user
13402 better diagnose the problem. */
791afaa2 13403 std::string dwp_text;
43942612
DE
13404
13405 if (dwp_file != NULL)
791afaa2
TT
13406 dwp_text = string_printf (" [in DWP file %s]",
13407 lbasename (dwp_file->name));
43942612 13408
9d8780f0 13409 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13410 " [in module %s]"),
13411 kind, dwo_name, hex_string (signature),
791afaa2 13412 dwp_text.c_str (),
43942612 13413 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13414 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13415 }
3019eac3 13416 return NULL;
5fb290d7
DJ
13417}
13418
80626a55
DE
13419/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13420 See lookup_dwo_cutu_unit for details. */
13421
13422static struct dwo_unit *
13423lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13424 const char *dwo_name, const char *comp_dir,
13425 ULONGEST signature)
13426{
13427 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13428}
13429
13430/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13431 See lookup_dwo_cutu_unit for details. */
13432
13433static struct dwo_unit *
13434lookup_dwo_type_unit (struct signatured_type *this_tu,
13435 const char *dwo_name, const char *comp_dir)
13436{
13437 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13438}
13439
89e63ee4
DE
13440/* Traversal function for queue_and_load_all_dwo_tus. */
13441
13442static int
13443queue_and_load_dwo_tu (void **slot, void *info)
13444{
13445 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13446 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13447 ULONGEST signature = dwo_unit->signature;
13448 struct signatured_type *sig_type =
13449 lookup_dwo_signatured_type (per_cu->cu, signature);
13450
13451 if (sig_type != NULL)
13452 {
13453 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13454
13455 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13456 a real dependency of PER_CU on SIG_TYPE. That is detected later
13457 while processing PER_CU. */
13458 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13459 load_full_type_unit (sig_cu);
13460 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13461 }
13462
13463 return 1;
13464}
13465
13466/* Queue all TUs contained in the DWO of PER_CU to be read in.
13467 The DWO may have the only definition of the type, though it may not be
13468 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13469 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13470
13471static void
13472queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13473{
13474 struct dwo_unit *dwo_unit;
13475 struct dwo_file *dwo_file;
13476
13477 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13478 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13479 gdb_assert (per_cu->cu != NULL);
13480
13481 dwo_unit = per_cu->cu->dwo_unit;
13482 gdb_assert (dwo_unit != NULL);
13483
13484 dwo_file = dwo_unit->dwo_file;
13485 if (dwo_file->tus != NULL)
13486 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13487}
13488
3019eac3 13489/* Free all resources associated with DWO_FILE.
5dafb3d1 13490 Close the DWO file and munmap the sections. */
348e048f
DE
13491
13492static void
5dafb3d1 13493free_dwo_file (struct dwo_file *dwo_file)
348e048f 13494{
5c6fa7ab 13495 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13496 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13497
3019eac3
DE
13498 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13499}
348e048f 13500
3019eac3 13501/* Traversal function for free_dwo_files. */
2ab95328 13502
3019eac3
DE
13503static int
13504free_dwo_file_from_slot (void **slot, void *info)
13505{
13506 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13507
5dafb3d1 13508 free_dwo_file (dwo_file);
348e048f 13509
3019eac3
DE
13510 return 1;
13511}
348e048f 13512
3019eac3 13513/* Free all resources associated with DWO_FILES. */
348e048f 13514
3019eac3
DE
13515static void
13516free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13517{
13518 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13519}
3019eac3
DE
13520\f
13521/* Read in various DIEs. */
348e048f 13522
d389af10 13523/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13524 Inherit only the children of the DW_AT_abstract_origin DIE not being
13525 already referenced by DW_AT_abstract_origin from the children of the
13526 current DIE. */
d389af10
JK
13527
13528static void
13529inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13530{
13531 struct die_info *child_die;
791afaa2 13532 sect_offset *offsetp;
d389af10
JK
13533 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13534 struct die_info *origin_die;
13535 /* Iterator of the ORIGIN_DIE children. */
13536 struct die_info *origin_child_die;
d389af10 13537 struct attribute *attr;
cd02d79d
PA
13538 struct dwarf2_cu *origin_cu;
13539 struct pending **origin_previous_list_in_scope;
d389af10
JK
13540
13541 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13542 if (!attr)
13543 return;
13544
cd02d79d
PA
13545 /* Note that following die references may follow to a die in a
13546 different cu. */
13547
13548 origin_cu = cu;
13549 origin_die = follow_die_ref (die, attr, &origin_cu);
13550
13551 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13552 symbols in. */
13553 origin_previous_list_in_scope = origin_cu->list_in_scope;
13554 origin_cu->list_in_scope = cu->list_in_scope;
13555
edb3359d
DJ
13556 if (die->tag != origin_die->tag
13557 && !(die->tag == DW_TAG_inlined_subroutine
13558 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13559 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13560 sect_offset_str (die->sect_off),
13561 sect_offset_str (origin_die->sect_off));
d389af10 13562
791afaa2 13563 std::vector<sect_offset> offsets;
d389af10 13564
3ea89b92
PMR
13565 for (child_die = die->child;
13566 child_die && child_die->tag;
13567 child_die = sibling_die (child_die))
13568 {
13569 struct die_info *child_origin_die;
13570 struct dwarf2_cu *child_origin_cu;
13571
13572 /* We are trying to process concrete instance entries:
216f72a1 13573 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13574 it's not relevant to our analysis here. i.e. detecting DIEs that are
13575 present in the abstract instance but not referenced in the concrete
13576 one. */
216f72a1
JK
13577 if (child_die->tag == DW_TAG_call_site
13578 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13579 continue;
13580
c38f313d
DJ
13581 /* For each CHILD_DIE, find the corresponding child of
13582 ORIGIN_DIE. If there is more than one layer of
13583 DW_AT_abstract_origin, follow them all; there shouldn't be,
13584 but GCC versions at least through 4.4 generate this (GCC PR
13585 40573). */
3ea89b92
PMR
13586 child_origin_die = child_die;
13587 child_origin_cu = cu;
c38f313d
DJ
13588 while (1)
13589 {
cd02d79d
PA
13590 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13591 child_origin_cu);
c38f313d
DJ
13592 if (attr == NULL)
13593 break;
cd02d79d
PA
13594 child_origin_die = follow_die_ref (child_origin_die, attr,
13595 &child_origin_cu);
c38f313d
DJ
13596 }
13597
d389af10
JK
13598 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13599 counterpart may exist. */
c38f313d 13600 if (child_origin_die != child_die)
d389af10 13601 {
edb3359d
DJ
13602 if (child_die->tag != child_origin_die->tag
13603 && !(child_die->tag == DW_TAG_inlined_subroutine
13604 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13605 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13606 "different tags"),
9d8780f0
SM
13607 sect_offset_str (child_die->sect_off),
13608 sect_offset_str (child_origin_die->sect_off));
c38f313d 13609 if (child_origin_die->parent != origin_die)
b98664d3 13610 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13611 "different parents"),
9d8780f0
SM
13612 sect_offset_str (child_die->sect_off),
13613 sect_offset_str (child_origin_die->sect_off));
c38f313d 13614 else
791afaa2 13615 offsets.push_back (child_origin_die->sect_off);
d389af10 13616 }
d389af10 13617 }
791afaa2
TT
13618 std::sort (offsets.begin (), offsets.end ());
13619 sect_offset *offsets_end = offsets.data () + offsets.size ();
13620 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13621 if (offsetp[-1] == *offsetp)
b98664d3 13622 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13623 "to DIE %s as their abstract origin"),
13624 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13625
791afaa2 13626 offsetp = offsets.data ();
d389af10
JK
13627 origin_child_die = origin_die->child;
13628 while (origin_child_die && origin_child_die->tag)
13629 {
13630 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13631 while (offsetp < offsets_end
9c541725 13632 && *offsetp < origin_child_die->sect_off)
d389af10 13633 offsetp++;
b64f50a1 13634 if (offsetp >= offsets_end
9c541725 13635 || *offsetp > origin_child_die->sect_off)
d389af10 13636 {
adde2bff
DE
13637 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13638 Check whether we're already processing ORIGIN_CHILD_DIE.
13639 This can happen with mutually referenced abstract_origins.
13640 PR 16581. */
13641 if (!origin_child_die->in_process)
13642 process_die (origin_child_die, origin_cu);
d389af10
JK
13643 }
13644 origin_child_die = sibling_die (origin_child_die);
13645 }
cd02d79d 13646 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13647}
13648
c906108c 13649static void
e7c27a73 13650read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13651{
518817b3 13652 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13653 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13654 struct context_stack *newobj;
c906108c
SS
13655 CORE_ADDR lowpc;
13656 CORE_ADDR highpc;
13657 struct die_info *child_die;
edb3359d 13658 struct attribute *attr, *call_line, *call_file;
15d034d0 13659 const char *name;
e142c38c 13660 CORE_ADDR baseaddr;
801e3a5b 13661 struct block *block;
edb3359d 13662 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13663 std::vector<struct symbol *> template_args;
34eaf542 13664 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13665
13666 if (inlined_func)
13667 {
13668 /* If we do not have call site information, we can't show the
13669 caller of this inlined function. That's too confusing, so
13670 only use the scope for local variables. */
13671 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13672 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13673 if (call_line == NULL || call_file == NULL)
13674 {
13675 read_lexical_block_scope (die, cu);
13676 return;
13677 }
13678 }
c906108c 13679
e142c38c
DJ
13680 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13681
94af9270 13682 name = dwarf2_name (die, cu);
c906108c 13683
e8d05480
JB
13684 /* Ignore functions with missing or empty names. These are actually
13685 illegal according to the DWARF standard. */
13686 if (name == NULL)
13687 {
b98664d3 13688 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13689 sect_offset_str (die->sect_off));
e8d05480
JB
13690 return;
13691 }
13692
13693 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13694 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13695 <= PC_BOUNDS_INVALID)
e8d05480 13696 {
ae4d0c03
PM
13697 attr = dwarf2_attr (die, DW_AT_external, cu);
13698 if (!attr || !DW_UNSND (attr))
b98664d3 13699 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13700 "for subprogram DIE at %s"),
13701 sect_offset_str (die->sect_off));
e8d05480
JB
13702 return;
13703 }
c906108c 13704
3e29f34a
MR
13705 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13706 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13707
34eaf542
TT
13708 /* If we have any template arguments, then we must allocate a
13709 different sort of symbol. */
13710 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13711 {
13712 if (child_die->tag == DW_TAG_template_type_param
13713 || child_die->tag == DW_TAG_template_value_param)
13714 {
e623cf5d 13715 templ_func = allocate_template_symbol (objfile);
cf724bc9 13716 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13717 break;
13718 }
13719 }
13720
c24bdb02 13721 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13722 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13723 (struct symbol *) templ_func);
4c2df51b 13724
81873cc8
TV
13725 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13726 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13727 cu->language);
13728
4cecd739
DJ
13729 /* If there is a location expression for DW_AT_frame_base, record
13730 it. */
e142c38c 13731 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13732 if (attr)
fe978cb0 13733 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13734
63e43d3a
PMR
13735 /* If there is a location for the static link, record it. */
13736 newobj->static_link = NULL;
13737 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13738 if (attr)
13739 {
224c3ddb
SM
13740 newobj->static_link
13741 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13742 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13743 }
13744
c24bdb02 13745 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13746
639d11d3 13747 if (die->child != NULL)
c906108c 13748 {
639d11d3 13749 child_die = die->child;
c906108c
SS
13750 while (child_die && child_die->tag)
13751 {
34eaf542
TT
13752 if (child_die->tag == DW_TAG_template_type_param
13753 || child_die->tag == DW_TAG_template_value_param)
13754 {
13755 struct symbol *arg = new_symbol (child_die, NULL, cu);
13756
f1078f66 13757 if (arg != NULL)
2f4732b0 13758 template_args.push_back (arg);
34eaf542
TT
13759 }
13760 else
13761 process_die (child_die, cu);
c906108c
SS
13762 child_die = sibling_die (child_die);
13763 }
13764 }
13765
d389af10
JK
13766 inherit_abstract_dies (die, cu);
13767
4a811a97
UW
13768 /* If we have a DW_AT_specification, we might need to import using
13769 directives from the context of the specification DIE. See the
13770 comment in determine_prefix. */
13771 if (cu->language == language_cplus
13772 && dwarf2_attr (die, DW_AT_specification, cu))
13773 {
13774 struct dwarf2_cu *spec_cu = cu;
13775 struct die_info *spec_die = die_specification (die, &spec_cu);
13776
13777 while (spec_die)
13778 {
13779 child_die = spec_die->child;
13780 while (child_die && child_die->tag)
13781 {
13782 if (child_die->tag == DW_TAG_imported_module)
13783 process_die (child_die, spec_cu);
13784 child_die = sibling_die (child_die);
13785 }
13786
13787 /* In some cases, GCC generates specification DIEs that
13788 themselves contain DW_AT_specification attributes. */
13789 spec_die = die_specification (spec_die, &spec_cu);
13790 }
13791 }
13792
c24bdb02 13793 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13794 /* Make a block for the local symbols within. */
c24bdb02 13795 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13796 cstk.static_link, lowpc, highpc);
801e3a5b 13797
df8a16a1 13798 /* For C++, set the block's scope. */
45280282
IB
13799 if ((cu->language == language_cplus
13800 || cu->language == language_fortran
c44af4eb
TT
13801 || cu->language == language_d
13802 || cu->language == language_rust)
4d4ec4e5 13803 && cu->processing_has_namespace_info)
195a3f6c
TT
13804 block_set_scope (block, determine_prefix (die, cu),
13805 &objfile->objfile_obstack);
df8a16a1 13806
801e3a5b
JB
13807 /* If we have address ranges, record them. */
13808 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13809
a60f3166 13810 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13811
34eaf542 13812 /* Attach template arguments to function. */
2f4732b0 13813 if (!template_args.empty ())
34eaf542
TT
13814 {
13815 gdb_assert (templ_func != NULL);
13816
2f4732b0 13817 templ_func->n_template_arguments = template_args.size ();
34eaf542 13818 templ_func->template_arguments
8d749320
SM
13819 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13820 templ_func->n_template_arguments);
34eaf542 13821 memcpy (templ_func->template_arguments,
2f4732b0 13822 template_args.data (),
34eaf542 13823 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13824
13825 /* Make sure that the symtab is set on the new symbols. Even
13826 though they don't appear in this symtab directly, other parts
13827 of gdb assume that symbols do, and this is reasonably
13828 true. */
8634679f 13829 for (symbol *sym : template_args)
3e1d3d8c 13830 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13831 }
13832
208d8187
JB
13833 /* In C++, we can have functions nested inside functions (e.g., when
13834 a function declares a class that has methods). This means that
13835 when we finish processing a function scope, we may need to go
13836 back to building a containing block's symbol lists. */
c24bdb02
KS
13837 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13838 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13839
921e78cf
JB
13840 /* If we've finished processing a top-level function, subsequent
13841 symbols go in the file symbol list. */
c24bdb02
KS
13842 if (cu->get_builder ()->outermost_context_p ())
13843 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13844}
13845
13846/* Process all the DIES contained within a lexical block scope. Start
13847 a new scope, process the dies, and then close the scope. */
13848
13849static void
e7c27a73 13850read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13851{
518817b3 13852 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13854 CORE_ADDR lowpc, highpc;
13855 struct die_info *child_die;
e142c38c
DJ
13856 CORE_ADDR baseaddr;
13857
13858 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13859
13860 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13861 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13862 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13863 be nasty. Might be easier to properly extend generic blocks to
af34e669 13864 describe ranges. */
e385593e
JK
13865 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13866 {
13867 case PC_BOUNDS_NOT_PRESENT:
13868 /* DW_TAG_lexical_block has no attributes, process its children as if
13869 there was no wrapping by that DW_TAG_lexical_block.
13870 GCC does no longer produces such DWARF since GCC r224161. */
13871 for (child_die = die->child;
13872 child_die != NULL && child_die->tag;
13873 child_die = sibling_die (child_die))
13874 process_die (child_die, cu);
13875 return;
13876 case PC_BOUNDS_INVALID:
13877 return;
13878 }
3e29f34a
MR
13879 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13880 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13881
c24bdb02 13882 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13883 if (die->child != NULL)
c906108c 13884 {
639d11d3 13885 child_die = die->child;
c906108c
SS
13886 while (child_die && child_die->tag)
13887 {
e7c27a73 13888 process_die (child_die, cu);
c906108c
SS
13889 child_die = sibling_die (child_die);
13890 }
13891 }
3ea89b92 13892 inherit_abstract_dies (die, cu);
c24bdb02 13893 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13894
c24bdb02
KS
13895 if (*cu->get_builder ()->get_local_symbols () != NULL
13896 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13897 {
801e3a5b 13898 struct block *block
c24bdb02 13899 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13900 cstk.start_addr, highpc);
801e3a5b
JB
13901
13902 /* Note that recording ranges after traversing children, as we
13903 do here, means that recording a parent's ranges entails
13904 walking across all its children's ranges as they appear in
13905 the address map, which is quadratic behavior.
13906
13907 It would be nicer to record the parent's ranges before
13908 traversing its children, simply overriding whatever you find
13909 there. But since we don't even decide whether to create a
13910 block until after we've traversed its children, that's hard
13911 to do. */
13912 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13913 }
c24bdb02
KS
13914 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13915 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13916}
13917
216f72a1 13918/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13919
13920static void
13921read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13922{
518817b3 13923 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13924 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13925 CORE_ADDR pc, baseaddr;
13926 struct attribute *attr;
13927 struct call_site *call_site, call_site_local;
13928 void **slot;
13929 int nparams;
13930 struct die_info *child_die;
13931
13932 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13933
216f72a1
JK
13934 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13935 if (attr == NULL)
13936 {
13937 /* This was a pre-DWARF-5 GNU extension alias
13938 for DW_AT_call_return_pc. */
13939 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13940 }
96408a79
SA
13941 if (!attr)
13942 {
b98664d3 13943 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13944 "DIE %s [in module %s]"),
13945 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13946 return;
13947 }
31aa7e4e 13948 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13949 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13950
13951 if (cu->call_site_htab == NULL)
13952 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13953 NULL, &objfile->objfile_obstack,
13954 hashtab_obstack_allocate, NULL);
13955 call_site_local.pc = pc;
13956 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13957 if (*slot != NULL)
13958 {
b98664d3 13959 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13960 "DIE %s [in module %s]"),
13961 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13962 objfile_name (objfile));
96408a79
SA
13963 return;
13964 }
13965
13966 /* Count parameters at the caller. */
13967
13968 nparams = 0;
13969 for (child_die = die->child; child_die && child_die->tag;
13970 child_die = sibling_die (child_die))
13971 {
216f72a1
JK
13972 if (child_die->tag != DW_TAG_call_site_parameter
13973 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13974 {
b98664d3 13975 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13976 "DW_TAG_call_site child DIE %s [in module %s]"),
13977 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13978 objfile_name (objfile));
96408a79
SA
13979 continue;
13980 }
13981
13982 nparams++;
13983 }
13984
224c3ddb
SM
13985 call_site
13986 = ((struct call_site *)
13987 obstack_alloc (&objfile->objfile_obstack,
13988 sizeof (*call_site)
13989 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13990 *slot = call_site;
13991 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13992 call_site->pc = pc;
13993
216f72a1
JK
13994 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13995 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13996 {
13997 struct die_info *func_die;
13998
13999 /* Skip also over DW_TAG_inlined_subroutine. */
14000 for (func_die = die->parent;
14001 func_die && func_die->tag != DW_TAG_subprogram
14002 && func_die->tag != DW_TAG_subroutine_type;
14003 func_die = func_die->parent);
14004
216f72a1
JK
14005 /* DW_AT_call_all_calls is a superset
14006 of DW_AT_call_all_tail_calls. */
96408a79 14007 if (func_die
216f72a1 14008 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14009 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14010 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14011 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14012 {
14013 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14014 not complete. But keep CALL_SITE for look ups via call_site_htab,
14015 both the initial caller containing the real return address PC and
14016 the final callee containing the current PC of a chain of tail
14017 calls do not need to have the tail call list complete. But any
14018 function candidate for a virtual tail call frame searched via
14019 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14020 determined unambiguously. */
14021 }
14022 else
14023 {
14024 struct type *func_type = NULL;
14025
14026 if (func_die)
14027 func_type = get_die_type (func_die, cu);
14028 if (func_type != NULL)
14029 {
14030 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14031
14032 /* Enlist this call site to the function. */
14033 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14034 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14035 }
14036 else
b98664d3 14037 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14038 "DIE %s [in module %s]"),
14039 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14040 }
14041 }
14042
216f72a1
JK
14043 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14044 if (attr == NULL)
14045 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14046 if (attr == NULL)
14047 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14048 if (attr == NULL)
216f72a1
JK
14049 {
14050 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14051 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14052 }
96408a79
SA
14053 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14054 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14055 /* Keep NULL DWARF_BLOCK. */;
14056 else if (attr_form_is_block (attr))
14057 {
14058 struct dwarf2_locexpr_baton *dlbaton;
14059
8d749320 14060 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14061 dlbaton->data = DW_BLOCK (attr)->data;
14062 dlbaton->size = DW_BLOCK (attr)->size;
14063 dlbaton->per_cu = cu->per_cu;
14064
14065 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14066 }
7771576e 14067 else if (attr_form_is_ref (attr))
96408a79 14068 {
96408a79
SA
14069 struct dwarf2_cu *target_cu = cu;
14070 struct die_info *target_die;
14071
ac9ec31b 14072 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14073 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14074 if (die_is_declaration (target_die, target_cu))
14075 {
7d45c7c3 14076 const char *target_physname;
9112db09
JK
14077
14078 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14079 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14080 if (target_physname == NULL)
9112db09 14081 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14082 if (target_physname == NULL)
b98664d3 14083 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14084 "physname, for referencing DIE %s [in module %s]"),
14085 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14086 else
7d455152 14087 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14088 }
14089 else
14090 {
14091 CORE_ADDR lowpc;
14092
14093 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14094 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14095 <= PC_BOUNDS_INVALID)
b98664d3 14096 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14097 "low pc, for referencing DIE %s [in module %s]"),
14098 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14099 else
3e29f34a
MR
14100 {
14101 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14102 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14103 }
96408a79
SA
14104 }
14105 }
14106 else
b98664d3 14107 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14108 "block nor reference, for DIE %s [in module %s]"),
14109 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14110
14111 call_site->per_cu = cu->per_cu;
14112
14113 for (child_die = die->child;
14114 child_die && child_die->tag;
14115 child_die = sibling_die (child_die))
14116 {
96408a79 14117 struct call_site_parameter *parameter;
1788b2d3 14118 struct attribute *loc, *origin;
96408a79 14119
216f72a1
JK
14120 if (child_die->tag != DW_TAG_call_site_parameter
14121 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14122 {
14123 /* Already printed the complaint above. */
14124 continue;
14125 }
14126
14127 gdb_assert (call_site->parameter_count < nparams);
14128 parameter = &call_site->parameter[call_site->parameter_count];
14129
1788b2d3
JK
14130 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14131 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14132 register is contained in DW_AT_call_value. */
96408a79 14133
24c5c679 14134 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14135 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14136 if (origin == NULL)
14137 {
14138 /* This was a pre-DWARF-5 GNU extension alias
14139 for DW_AT_call_parameter. */
14140 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14141 }
7771576e 14142 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14143 {
1788b2d3 14144 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14145
14146 sect_offset sect_off
14147 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14148 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14149 {
14150 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14151 binding can be done only inside one CU. Such referenced DIE
14152 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14153 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14154 "DW_TAG_call_site child DIE %s [in module %s]"),
14155 sect_offset_str (child_die->sect_off),
9c541725 14156 objfile_name (objfile));
d76b7dbc
JK
14157 continue;
14158 }
9c541725
PA
14159 parameter->u.param_cu_off
14160 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14161 }
14162 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14163 {
b98664d3 14164 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14165 "DW_TAG_call_site child DIE %s [in module %s]"),
14166 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14167 continue;
14168 }
24c5c679 14169 else
96408a79 14170 {
24c5c679
JK
14171 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14172 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14173 if (parameter->u.dwarf_reg != -1)
14174 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14175 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14176 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14177 &parameter->u.fb_offset))
14178 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14179 else
14180 {
b98664d3 14181 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14182 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14183 "DW_TAG_call_site child DIE %s "
24c5c679 14184 "[in module %s]"),
9d8780f0 14185 sect_offset_str (child_die->sect_off),
9c541725 14186 objfile_name (objfile));
24c5c679
JK
14187 continue;
14188 }
96408a79
SA
14189 }
14190
216f72a1
JK
14191 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14192 if (attr == NULL)
14193 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14194 if (!attr_form_is_block (attr))
14195 {
b98664d3 14196 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14197 "DW_TAG_call_site child DIE %s [in module %s]"),
14198 sect_offset_str (child_die->sect_off),
9c541725 14199 objfile_name (objfile));
96408a79
SA
14200 continue;
14201 }
14202 parameter->value = DW_BLOCK (attr)->data;
14203 parameter->value_size = DW_BLOCK (attr)->size;
14204
14205 /* Parameters are not pre-cleared by memset above. */
14206 parameter->data_value = NULL;
14207 parameter->data_value_size = 0;
14208 call_site->parameter_count++;
14209
216f72a1
JK
14210 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14211 if (attr == NULL)
14212 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14213 if (attr)
14214 {
14215 if (!attr_form_is_block (attr))
b98664d3 14216 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14217 "DW_TAG_call_site child DIE %s [in module %s]"),
14218 sect_offset_str (child_die->sect_off),
9c541725 14219 objfile_name (objfile));
96408a79
SA
14220 else
14221 {
14222 parameter->data_value = DW_BLOCK (attr)->data;
14223 parameter->data_value_size = DW_BLOCK (attr)->size;
14224 }
14225 }
14226 }
14227}
14228
71a3c369
TT
14229/* Helper function for read_variable. If DIE represents a virtual
14230 table, then return the type of the concrete object that is
14231 associated with the virtual table. Otherwise, return NULL. */
14232
14233static struct type *
14234rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14235{
14236 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14237 if (attr == NULL)
14238 return NULL;
14239
14240 /* Find the type DIE. */
14241 struct die_info *type_die = NULL;
14242 struct dwarf2_cu *type_cu = cu;
14243
14244 if (attr_form_is_ref (attr))
14245 type_die = follow_die_ref (die, attr, &type_cu);
14246 if (type_die == NULL)
14247 return NULL;
14248
14249 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14250 return NULL;
14251 return die_containing_type (type_die, type_cu);
14252}
14253
14254/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14255
14256static void
14257read_variable (struct die_info *die, struct dwarf2_cu *cu)
14258{
14259 struct rust_vtable_symbol *storage = NULL;
14260
14261 if (cu->language == language_rust)
14262 {
14263 struct type *containing_type = rust_containing_type (die, cu);
14264
14265 if (containing_type != NULL)
14266 {
518817b3 14267 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14268
14269 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14270 struct rust_vtable_symbol);
14271 initialize_objfile_symbol (storage);
14272 storage->concrete_type = containing_type;
cf724bc9 14273 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14274 }
14275 }
14276
e4a62c65
TV
14277 struct symbol *res = new_symbol (die, NULL, cu, storage);
14278 struct attribute *abstract_origin
14279 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14280 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14281 if (res == NULL && loc && abstract_origin)
14282 {
14283 /* We have a variable without a name, but with a location and an abstract
14284 origin. This may be a concrete instance of an abstract variable
14285 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14286 later. */
14287 struct dwarf2_cu *origin_cu = cu;
14288 struct die_info *origin_die
14289 = follow_die_ref (die, abstract_origin, &origin_cu);
14290 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14291 dpo->abstract_to_concrete[origin_die].push_back (die);
14292 }
71a3c369
TT
14293}
14294
43988095
JK
14295/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14296 reading .debug_rnglists.
14297 Callback's type should be:
14298 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14299 Return true if the attributes are present and valid, otherwise,
14300 return false. */
14301
14302template <typename Callback>
14303static bool
14304dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14305 Callback &&callback)
14306{
ed2dc618 14307 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14308 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14309 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14310 bfd *obfd = objfile->obfd;
43988095
JK
14311 /* Base address selection entry. */
14312 CORE_ADDR base;
14313 int found_base;
43988095 14314 const gdb_byte *buffer;
43988095
JK
14315 CORE_ADDR baseaddr;
14316 bool overflow = false;
14317
14318 found_base = cu->base_known;
14319 base = cu->base_address;
14320
14321 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14322 if (offset >= dwarf2_per_objfile->rnglists.size)
14323 {
b98664d3 14324 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14325 offset);
14326 return false;
14327 }
14328 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14329
14330 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14331
14332 while (1)
14333 {
7814882a
JK
14334 /* Initialize it due to a false compiler warning. */
14335 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14336 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14337 + dwarf2_per_objfile->rnglists.size);
14338 unsigned int bytes_read;
14339
14340 if (buffer == buf_end)
14341 {
14342 overflow = true;
14343 break;
14344 }
14345 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14346 switch (rlet)
14347 {
14348 case DW_RLE_end_of_list:
14349 break;
14350 case DW_RLE_base_address:
14351 if (buffer + cu->header.addr_size > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 base = read_address (obfd, buffer, cu, &bytes_read);
14357 found_base = 1;
14358 buffer += bytes_read;
14359 break;
14360 case DW_RLE_start_length:
14361 if (buffer + cu->header.addr_size > buf_end)
14362 {
14363 overflow = true;
14364 break;
14365 }
14366 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14367 buffer += bytes_read;
14368 range_end = (range_beginning
14369 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14370 buffer += bytes_read;
14371 if (buffer > buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 break;
14377 case DW_RLE_offset_pair:
14378 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14379 buffer += bytes_read;
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14386 buffer += bytes_read;
14387 if (buffer > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 break;
14393 case DW_RLE_start_end:
14394 if (buffer + 2 * cu->header.addr_size > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14400 buffer += bytes_read;
14401 range_end = read_address (obfd, buffer, cu, &bytes_read);
14402 buffer += bytes_read;
14403 break;
14404 default:
b98664d3 14405 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14406 return false;
14407 }
14408 if (rlet == DW_RLE_end_of_list || overflow)
14409 break;
14410 if (rlet == DW_RLE_base_address)
14411 continue;
14412
14413 if (!found_base)
14414 {
14415 /* We have no valid base address for the ranges
14416 data. */
b98664d3 14417 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14418 return false;
14419 }
14420
14421 if (range_beginning > range_end)
14422 {
14423 /* Inverted range entries are invalid. */
b98664d3 14424 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14425 return false;
14426 }
14427
14428 /* Empty range entries have no effect. */
14429 if (range_beginning == range_end)
14430 continue;
14431
14432 range_beginning += base;
14433 range_end += base;
14434
14435 /* A not-uncommon case of bad debug info.
14436 Don't pollute the addrmap with bad data. */
14437 if (range_beginning + baseaddr == 0
14438 && !dwarf2_per_objfile->has_section_at_zero)
14439 {
b98664d3 14440 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14441 " [in module %s]"), objfile_name (objfile));
14442 continue;
14443 }
14444
14445 callback (range_beginning, range_end);
14446 }
14447
14448 if (overflow)
14449 {
b98664d3 14450 complaint (_("Offset %d is not terminated "
43988095
JK
14451 "for DW_AT_ranges attribute"),
14452 offset);
14453 return false;
14454 }
14455
14456 return true;
14457}
14458
14459/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14460 Callback's type should be:
14461 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14462 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14463
43988095 14464template <typename Callback>
43039443 14465static int
5f46c5a5 14466dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14467 Callback &&callback)
43039443 14468{
ed2dc618 14469 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14470 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14471 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14472 struct comp_unit_head *cu_header = &cu->header;
14473 bfd *obfd = objfile->obfd;
14474 unsigned int addr_size = cu_header->addr_size;
14475 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14476 /* Base address selection entry. */
14477 CORE_ADDR base;
14478 int found_base;
14479 unsigned int dummy;
d521ce57 14480 const gdb_byte *buffer;
ff013f42 14481 CORE_ADDR baseaddr;
43039443 14482
43988095
JK
14483 if (cu_header->version >= 5)
14484 return dwarf2_rnglists_process (offset, cu, callback);
14485
d00adf39
DE
14486 found_base = cu->base_known;
14487 base = cu->base_address;
43039443 14488
be391dca 14489 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14490 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14491 {
b98664d3 14492 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14493 offset);
14494 return 0;
14495 }
dce234bc 14496 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14497
e7030f15 14498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14499
43039443
JK
14500 while (1)
14501 {
14502 CORE_ADDR range_beginning, range_end;
14503
14504 range_beginning = read_address (obfd, buffer, cu, &dummy);
14505 buffer += addr_size;
14506 range_end = read_address (obfd, buffer, cu, &dummy);
14507 buffer += addr_size;
14508 offset += 2 * addr_size;
14509
14510 /* An end of list marker is a pair of zero addresses. */
14511 if (range_beginning == 0 && range_end == 0)
14512 /* Found the end of list entry. */
14513 break;
14514
14515 /* Each base address selection entry is a pair of 2 values.
14516 The first is the largest possible address, the second is
14517 the base address. Check for a base address here. */
14518 if ((range_beginning & mask) == mask)
14519 {
28d2bfb9
AB
14520 /* If we found the largest possible address, then we already
14521 have the base address in range_end. */
14522 base = range_end;
43039443
JK
14523 found_base = 1;
14524 continue;
14525 }
14526
14527 if (!found_base)
14528 {
14529 /* We have no valid base address for the ranges
14530 data. */
b98664d3 14531 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14532 return 0;
14533 }
14534
9277c30c
UW
14535 if (range_beginning > range_end)
14536 {
14537 /* Inverted range entries are invalid. */
b98664d3 14538 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14539 return 0;
14540 }
14541
14542 /* Empty range entries have no effect. */
14543 if (range_beginning == range_end)
14544 continue;
14545
43039443
JK
14546 range_beginning += base;
14547 range_end += base;
14548
01093045
DE
14549 /* A not-uncommon case of bad debug info.
14550 Don't pollute the addrmap with bad data. */
14551 if (range_beginning + baseaddr == 0
14552 && !dwarf2_per_objfile->has_section_at_zero)
14553 {
b98664d3 14554 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14555 " [in module %s]"), objfile_name (objfile));
01093045
DE
14556 continue;
14557 }
14558
5f46c5a5
JK
14559 callback (range_beginning, range_end);
14560 }
14561
14562 return 1;
14563}
14564
14565/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14566 Return 1 if the attributes are present and valid, otherwise, return 0.
14567 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14568
14569static int
14570dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14571 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14572 struct partial_symtab *ranges_pst)
14573{
518817b3 14574 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14576 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14577 SECT_OFF_TEXT (objfile));
14578 int low_set = 0;
14579 CORE_ADDR low = 0;
14580 CORE_ADDR high = 0;
14581 int retval;
14582
14583 retval = dwarf2_ranges_process (offset, cu,
14584 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14585 {
9277c30c 14586 if (ranges_pst != NULL)
3e29f34a
MR
14587 {
14588 CORE_ADDR lowpc;
14589 CORE_ADDR highpc;
14590
79748972
TT
14591 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14592 range_beginning + baseaddr)
14593 - baseaddr);
14594 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14595 range_end + baseaddr)
14596 - baseaddr);
d320c2b5
TT
14597 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14598 lowpc, highpc - 1, ranges_pst);
3e29f34a 14599 }
ff013f42 14600
43039443
JK
14601 /* FIXME: This is recording everything as a low-high
14602 segment of consecutive addresses. We should have a
14603 data structure for discontiguous block ranges
14604 instead. */
14605 if (! low_set)
14606 {
14607 low = range_beginning;
14608 high = range_end;
14609 low_set = 1;
14610 }
14611 else
14612 {
14613 if (range_beginning < low)
14614 low = range_beginning;
14615 if (range_end > high)
14616 high = range_end;
14617 }
5f46c5a5
JK
14618 });
14619 if (!retval)
14620 return 0;
43039443
JK
14621
14622 if (! low_set)
14623 /* If the first entry is an end-of-list marker, the range
14624 describes an empty scope, i.e. no instructions. */
14625 return 0;
14626
14627 if (low_return)
14628 *low_return = low;
14629 if (high_return)
14630 *high_return = high;
14631 return 1;
14632}
14633
3a2b436a
JK
14634/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14635 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14636 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14637
3a2b436a 14638static enum pc_bounds_kind
af34e669 14639dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14640 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14641 struct partial_symtab *pst)
c906108c 14642{
518817b3
SM
14643 struct dwarf2_per_objfile *dwarf2_per_objfile
14644 = cu->per_cu->dwarf2_per_objfile;
c906108c 14645 struct attribute *attr;
91da1414 14646 struct attribute *attr_high;
af34e669
DJ
14647 CORE_ADDR low = 0;
14648 CORE_ADDR high = 0;
e385593e 14649 enum pc_bounds_kind ret;
c906108c 14650
91da1414
MW
14651 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14652 if (attr_high)
af34e669 14653 {
e142c38c 14654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14655 if (attr)
91da1414 14656 {
31aa7e4e
JB
14657 low = attr_value_as_address (attr);
14658 high = attr_value_as_address (attr_high);
14659 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14660 high += low;
91da1414 14661 }
af34e669
DJ
14662 else
14663 /* Found high w/o low attribute. */
e385593e 14664 return PC_BOUNDS_INVALID;
af34e669
DJ
14665
14666 /* Found consecutive range of addresses. */
3a2b436a 14667 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14668 }
c906108c 14669 else
af34e669 14670 {
e142c38c 14671 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14672 if (attr != NULL)
14673 {
ab435259
DE
14674 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14675 We take advantage of the fact that DW_AT_ranges does not appear
14676 in DW_TAG_compile_unit of DWO files. */
14677 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14678 unsigned int ranges_offset = (DW_UNSND (attr)
14679 + (need_ranges_base
14680 ? cu->ranges_base
14681 : 0));
2e3cf129 14682
af34e669 14683 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14684 .debug_ranges section. */
2e3cf129 14685 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14686 return PC_BOUNDS_INVALID;
43039443 14687 /* Found discontinuous range of addresses. */
3a2b436a 14688 ret = PC_BOUNDS_RANGES;
af34e669 14689 }
e385593e
JK
14690 else
14691 return PC_BOUNDS_NOT_PRESENT;
af34e669 14692 }
c906108c 14693
48fbe735 14694 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14695 if (high <= low)
e385593e 14696 return PC_BOUNDS_INVALID;
c906108c
SS
14697
14698 /* When using the GNU linker, .gnu.linkonce. sections are used to
14699 eliminate duplicate copies of functions and vtables and such.
14700 The linker will arbitrarily choose one and discard the others.
14701 The AT_*_pc values for such functions refer to local labels in
14702 these sections. If the section from that file was discarded, the
14703 labels are not in the output, so the relocs get a value of 0.
14704 If this is a discarded function, mark the pc bounds as invalid,
14705 so that GDB will ignore it. */
72dca2f5 14706 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14707 return PC_BOUNDS_INVALID;
c906108c
SS
14708
14709 *lowpc = low;
96408a79
SA
14710 if (highpc)
14711 *highpc = high;
af34e669 14712 return ret;
c906108c
SS
14713}
14714
b084d499
JB
14715/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14716 its low and high PC addresses. Do nothing if these addresses could not
14717 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14718 and HIGHPC to the high address if greater than HIGHPC. */
14719
14720static void
14721dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14722 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14723 struct dwarf2_cu *cu)
14724{
14725 CORE_ADDR low, high;
14726 struct die_info *child = die->child;
14727
e385593e 14728 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14729 {
325fac50
PA
14730 *lowpc = std::min (*lowpc, low);
14731 *highpc = std::max (*highpc, high);
b084d499
JB
14732 }
14733
14734 /* If the language does not allow nested subprograms (either inside
14735 subprograms or lexical blocks), we're done. */
14736 if (cu->language != language_ada)
14737 return;
6e70227d 14738
b084d499
JB
14739 /* Check all the children of the given DIE. If it contains nested
14740 subprograms, then check their pc bounds. Likewise, we need to
14741 check lexical blocks as well, as they may also contain subprogram
14742 definitions. */
14743 while (child && child->tag)
14744 {
14745 if (child->tag == DW_TAG_subprogram
14746 || child->tag == DW_TAG_lexical_block)
14747 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14748 child = sibling_die (child);
14749 }
14750}
14751
fae299cd
DC
14752/* Get the low and high pc's represented by the scope DIE, and store
14753 them in *LOWPC and *HIGHPC. If the correct values can't be
14754 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14755
14756static void
14757get_scope_pc_bounds (struct die_info *die,
14758 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14759 struct dwarf2_cu *cu)
14760{
14761 CORE_ADDR best_low = (CORE_ADDR) -1;
14762 CORE_ADDR best_high = (CORE_ADDR) 0;
14763 CORE_ADDR current_low, current_high;
14764
3a2b436a 14765 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14766 >= PC_BOUNDS_RANGES)
fae299cd
DC
14767 {
14768 best_low = current_low;
14769 best_high = current_high;
14770 }
14771 else
14772 {
14773 struct die_info *child = die->child;
14774
14775 while (child && child->tag)
14776 {
14777 switch (child->tag) {
14778 case DW_TAG_subprogram:
b084d499 14779 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14780 break;
14781 case DW_TAG_namespace:
f55ee35c 14782 case DW_TAG_module:
fae299cd
DC
14783 /* FIXME: carlton/2004-01-16: Should we do this for
14784 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14785 that current GCC's always emit the DIEs corresponding
14786 to definitions of methods of classes as children of a
14787 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14788 the DIEs giving the declarations, which could be
14789 anywhere). But I don't see any reason why the
14790 standards says that they have to be there. */
14791 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14792
14793 if (current_low != ((CORE_ADDR) -1))
14794 {
325fac50
PA
14795 best_low = std::min (best_low, current_low);
14796 best_high = std::max (best_high, current_high);
fae299cd
DC
14797 }
14798 break;
14799 default:
0963b4bd 14800 /* Ignore. */
fae299cd
DC
14801 break;
14802 }
14803
14804 child = sibling_die (child);
14805 }
14806 }
14807
14808 *lowpc = best_low;
14809 *highpc = best_high;
14810}
14811
801e3a5b
JB
14812/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14813 in DIE. */
380bca97 14814
801e3a5b
JB
14815static void
14816dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14817 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14818{
518817b3 14819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14820 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14821 struct attribute *attr;
91da1414 14822 struct attribute *attr_high;
801e3a5b 14823
91da1414
MW
14824 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14825 if (attr_high)
801e3a5b 14826 {
801e3a5b
JB
14827 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14828 if (attr)
14829 {
31aa7e4e
JB
14830 CORE_ADDR low = attr_value_as_address (attr);
14831 CORE_ADDR high = attr_value_as_address (attr_high);
14832
14833 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14834 high += low;
9a619af0 14835
3e29f34a
MR
14836 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14837 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14838 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14839 }
14840 }
14841
14842 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14843 if (attr)
14844 {
ab435259
DE
14845 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14846 We take advantage of the fact that DW_AT_ranges does not appear
14847 in DW_TAG_compile_unit of DWO files. */
14848 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14849
14850 /* The value of the DW_AT_ranges attribute is the offset of the
14851 address range list in the .debug_ranges section. */
ab435259
DE
14852 unsigned long offset = (DW_UNSND (attr)
14853 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14854
2d5f09ec 14855 std::vector<blockrange> blockvec;
5f46c5a5
JK
14856 dwarf2_ranges_process (offset, cu,
14857 [&] (CORE_ADDR start, CORE_ADDR end)
14858 {
58fdfd2c
JK
14859 start += baseaddr;
14860 end += baseaddr;
5f46c5a5
JK
14861 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14862 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14863 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14864 blockvec.emplace_back (start, end);
5f46c5a5 14865 });
2d5f09ec
KB
14866
14867 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14868 }
14869}
14870
685b1105
JK
14871/* Check whether the producer field indicates either of GCC < 4.6, or the
14872 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14873
685b1105
JK
14874static void
14875check_producer (struct dwarf2_cu *cu)
60d5a603 14876{
38360086 14877 int major, minor;
60d5a603
JK
14878
14879 if (cu->producer == NULL)
14880 {
14881 /* For unknown compilers expect their behavior is DWARF version
14882 compliant.
14883
14884 GCC started to support .debug_types sections by -gdwarf-4 since
14885 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14886 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14887 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14888 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14889 }
b1ffba5a 14890 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14891 {
38360086
MW
14892 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14893 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14894 }
5230b05a 14895 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14896 {
14897 cu->producer_is_icc = true;
14898 cu->producer_is_icc_lt_14 = major < 14;
14899 }
c258c396
JD
14900 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14901 cu->producer_is_codewarrior = true;
685b1105
JK
14902 else
14903 {
14904 /* For other non-GCC compilers, expect their behavior is DWARF version
14905 compliant. */
60d5a603
JK
14906 }
14907
9068261f 14908 cu->checked_producer = true;
685b1105 14909}
ba919b58 14910
685b1105
JK
14911/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14912 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14913 during 4.6.0 experimental. */
14914
9068261f 14915static bool
685b1105
JK
14916producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14917{
14918 if (!cu->checked_producer)
14919 check_producer (cu);
14920
14921 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14922}
14923
c258c396
JD
14924
14925/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14926 with incorrect is_stmt attributes. */
14927
14928static bool
14929producer_is_codewarrior (struct dwarf2_cu *cu)
14930{
14931 if (!cu->checked_producer)
14932 check_producer (cu);
14933
14934 return cu->producer_is_codewarrior;
14935}
14936
60d5a603
JK
14937/* Return the default accessibility type if it is not overriden by
14938 DW_AT_accessibility. */
14939
14940static enum dwarf_access_attribute
14941dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14942{
14943 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14944 {
14945 /* The default DWARF 2 accessibility for members is public, the default
14946 accessibility for inheritance is private. */
14947
14948 if (die->tag != DW_TAG_inheritance)
14949 return DW_ACCESS_public;
14950 else
14951 return DW_ACCESS_private;
14952 }
14953 else
14954 {
14955 /* DWARF 3+ defines the default accessibility a different way. The same
14956 rules apply now for DW_TAG_inheritance as for the members and it only
14957 depends on the container kind. */
14958
14959 if (die->parent->tag == DW_TAG_class_type)
14960 return DW_ACCESS_private;
14961 else
14962 return DW_ACCESS_public;
14963 }
14964}
14965
74ac6d43
TT
14966/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14967 offset. If the attribute was not found return 0, otherwise return
14968 1. If it was found but could not properly be handled, set *OFFSET
14969 to 0. */
14970
14971static int
14972handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14973 LONGEST *offset)
14974{
14975 struct attribute *attr;
14976
14977 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14978 if (attr != NULL)
14979 {
14980 *offset = 0;
14981
14982 /* Note that we do not check for a section offset first here.
14983 This is because DW_AT_data_member_location is new in DWARF 4,
14984 so if we see it, we can assume that a constant form is really
14985 a constant and not a section offset. */
14986 if (attr_form_is_constant (attr))
14987 *offset = dwarf2_get_attr_constant_value (attr, 0);
14988 else if (attr_form_is_section_offset (attr))
14989 dwarf2_complex_location_expr_complaint ();
14990 else if (attr_form_is_block (attr))
14991 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14992 else
14993 dwarf2_complex_location_expr_complaint ();
14994
14995 return 1;
14996 }
14997
14998 return 0;
14999}
15000
c906108c
SS
15001/* Add an aggregate field to the field list. */
15002
15003static void
107d2387 15004dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15005 struct dwarf2_cu *cu)
6e70227d 15006{
518817b3 15007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15009 struct nextfield *new_field;
15010 struct attribute *attr;
15011 struct field *fp;
15d034d0 15012 const char *fieldname = "";
c906108c 15013
7d0ccb61
DJ
15014 if (die->tag == DW_TAG_inheritance)
15015 {
be2daae6
TT
15016 fip->baseclasses.emplace_back ();
15017 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15018 }
15019 else
15020 {
be2daae6
TT
15021 fip->fields.emplace_back ();
15022 new_field = &fip->fields.back ();
7d0ccb61 15023 }
be2daae6 15024
c906108c
SS
15025 fip->nfields++;
15026
e142c38c 15027 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15028 if (attr)
15029 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15030 else
15031 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15032 if (new_field->accessibility != DW_ACCESS_public)
15033 fip->non_public_fields = 1;
60d5a603 15034
e142c38c 15035 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15036 if (attr)
15037 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15038 else
15039 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15040
15041 fp = &new_field->field;
a9a9bd0f 15042
e142c38c 15043 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15044 {
74ac6d43
TT
15045 LONGEST offset;
15046
a9a9bd0f 15047 /* Data member other than a C++ static data member. */
6e70227d 15048
c906108c 15049 /* Get type of field. */
e7c27a73 15050 fp->type = die_type (die, cu);
c906108c 15051
d6a843b5 15052 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15053
c906108c 15054 /* Get bit size of field (zero if none). */
e142c38c 15055 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15056 if (attr)
15057 {
15058 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15059 }
15060 else
15061 {
15062 FIELD_BITSIZE (*fp) = 0;
15063 }
15064
15065 /* Get bit offset of field. */
74ac6d43
TT
15066 if (handle_data_member_location (die, cu, &offset))
15067 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15068 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15069 if (attr)
15070 {
5e2b427d 15071 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15072 {
15073 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15074 additional bit offset from the MSB of the containing
15075 anonymous object to the MSB of the field. We don't
15076 have to do anything special since we don't need to
15077 know the size of the anonymous object. */
f41f5e61 15078 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15079 }
15080 else
15081 {
15082 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15083 MSB of the anonymous object, subtract off the number of
15084 bits from the MSB of the field to the MSB of the
15085 object, and then subtract off the number of bits of
15086 the field itself. The result is the bit offset of
15087 the LSB of the field. */
c906108c
SS
15088 int anonymous_size;
15089 int bit_offset = DW_UNSND (attr);
15090
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15092 if (attr)
15093 {
15094 /* The size of the anonymous object containing
15095 the bit field is explicit, so use the
15096 indicated size (in bytes). */
15097 anonymous_size = DW_UNSND (attr);
15098 }
15099 else
15100 {
15101 /* The size of the anonymous object containing
15102 the bit field must be inferred from the type
15103 attribute of the data member containing the
15104 bit field. */
15105 anonymous_size = TYPE_LENGTH (fp->type);
15106 }
f41f5e61
PA
15107 SET_FIELD_BITPOS (*fp,
15108 (FIELD_BITPOS (*fp)
15109 + anonymous_size * bits_per_byte
15110 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15111 }
15112 }
da5b30da
AA
15113 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15114 if (attr != NULL)
15115 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15116 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15117
15118 /* Get name of field. */
39cbfefa
DJ
15119 fieldname = dwarf2_name (die, cu);
15120 if (fieldname == NULL)
15121 fieldname = "";
d8151005
DJ
15122
15123 /* The name is already allocated along with this objfile, so we don't
15124 need to duplicate it for the type. */
15125 fp->name = fieldname;
c906108c
SS
15126
15127 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15128 pointer or virtual base class pointer) to private. */
e142c38c 15129 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15130 {
d48cc9dd 15131 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15132 new_field->accessibility = DW_ACCESS_private;
15133 fip->non_public_fields = 1;
15134 }
15135 }
a9a9bd0f 15136 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15137 {
a9a9bd0f
DC
15138 /* C++ static member. */
15139
15140 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15141 is a declaration, but all versions of G++ as of this writing
15142 (so through at least 3.2.1) incorrectly generate
15143 DW_TAG_variable tags. */
6e70227d 15144
ff355380 15145 const char *physname;
c906108c 15146
a9a9bd0f 15147 /* Get name of field. */
39cbfefa
DJ
15148 fieldname = dwarf2_name (die, cu);
15149 if (fieldname == NULL)
c906108c
SS
15150 return;
15151
254e6b9e 15152 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15153 if (attr
15154 /* Only create a symbol if this is an external value.
15155 new_symbol checks this and puts the value in the global symbol
15156 table, which we want. If it is not external, new_symbol
15157 will try to put the value in cu->list_in_scope which is wrong. */
15158 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15159 {
15160 /* A static const member, not much different than an enum as far as
15161 we're concerned, except that we can support more types. */
15162 new_symbol (die, NULL, cu);
15163 }
15164
2df3850c 15165 /* Get physical name. */
ff355380 15166 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15167
d8151005
DJ
15168 /* The name is already allocated along with this objfile, so we don't
15169 need to duplicate it for the type. */
15170 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15171 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15172 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15173 }
15174 else if (die->tag == DW_TAG_inheritance)
15175 {
74ac6d43 15176 LONGEST offset;
d4b96c9a 15177
74ac6d43
TT
15178 /* C++ base class field. */
15179 if (handle_data_member_location (die, cu, &offset))
15180 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15181 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15182 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15183 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15184 }
2ddeaf8a
TT
15185 else if (die->tag == DW_TAG_variant_part)
15186 {
15187 /* process_structure_scope will treat this DIE as a union. */
15188 process_structure_scope (die, cu);
15189
15190 /* The variant part is relative to the start of the enclosing
15191 structure. */
15192 SET_FIELD_BITPOS (*fp, 0);
15193 fp->type = get_die_type (die, cu);
15194 fp->artificial = 1;
15195 fp->name = "<<variant>>";
c8c81635
TT
15196
15197 /* Normally a DW_TAG_variant_part won't have a size, but our
15198 representation requires one, so set it to the maximum of the
15199 child sizes. */
15200 if (TYPE_LENGTH (fp->type) == 0)
15201 {
15202 unsigned max = 0;
15203 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15204 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15205 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15206 TYPE_LENGTH (fp->type) = max;
15207 }
2ddeaf8a
TT
15208 }
15209 else
15210 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15211}
15212
883fd55a
KS
15213/* Can the type given by DIE define another type? */
15214
15215static bool
15216type_can_define_types (const struct die_info *die)
15217{
15218 switch (die->tag)
15219 {
15220 case DW_TAG_typedef:
15221 case DW_TAG_class_type:
15222 case DW_TAG_structure_type:
15223 case DW_TAG_union_type:
15224 case DW_TAG_enumeration_type:
15225 return true;
15226
15227 default:
15228 return false;
15229 }
15230}
15231
15232/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15233
15234static void
883fd55a
KS
15235dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15236 struct dwarf2_cu *cu)
6e70227d 15237{
be2daae6
TT
15238 struct decl_field fp;
15239 memset (&fp, 0, sizeof (fp));
98751a41 15240
883fd55a 15241 gdb_assert (type_can_define_types (die));
98751a41 15242
883fd55a 15243 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15244 fp.name = dwarf2_name (die, cu);
15245 fp.type = read_type_die (die, cu);
98751a41 15246
c191a687
KS
15247 /* Save accessibility. */
15248 enum dwarf_access_attribute accessibility;
15249 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15250 if (attr != NULL)
15251 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15252 else
15253 accessibility = dwarf2_default_access_attribute (die, cu);
15254 switch (accessibility)
15255 {
15256 case DW_ACCESS_public:
15257 /* The assumed value if neither private nor protected. */
15258 break;
15259 case DW_ACCESS_private:
be2daae6 15260 fp.is_private = 1;
c191a687
KS
15261 break;
15262 case DW_ACCESS_protected:
be2daae6 15263 fp.is_protected = 1;
c191a687
KS
15264 break;
15265 default:
b98664d3 15266 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15267 }
15268
883fd55a 15269 if (die->tag == DW_TAG_typedef)
be2daae6 15270 fip->typedef_field_list.push_back (fp);
883fd55a 15271 else
be2daae6 15272 fip->nested_types_list.push_back (fp);
98751a41
JK
15273}
15274
c906108c
SS
15275/* Create the vector of fields, and attach it to the type. */
15276
15277static void
fba45db2 15278dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15279 struct dwarf2_cu *cu)
c906108c
SS
15280{
15281 int nfields = fip->nfields;
15282
15283 /* Record the field count, allocate space for the array of fields,
15284 and create blank accessibility bitfields if necessary. */
15285 TYPE_NFIELDS (type) = nfields;
15286 TYPE_FIELDS (type) = (struct field *)
be2daae6 15287 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15288
b4ba55a1 15289 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15290 {
15291 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15292
15293 TYPE_FIELD_PRIVATE_BITS (type) =
15294 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15295 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15296
15297 TYPE_FIELD_PROTECTED_BITS (type) =
15298 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15299 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15300
774b6a14
TT
15301 TYPE_FIELD_IGNORE_BITS (type) =
15302 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15303 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15304 }
15305
15306 /* If the type has baseclasses, allocate and clear a bit vector for
15307 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15308 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15309 {
be2daae6 15310 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15311 unsigned char *pointer;
c906108c
SS
15312
15313 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15314 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15315 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15316 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15317 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15318 }
15319
2ddeaf8a
TT
15320 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15321 {
15322 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15323
be2daae6 15324 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15325 {
be2daae6
TT
15326 struct nextfield &field = fip->fields[index];
15327
15328 if (field.variant.is_discriminant)
2ddeaf8a 15329 di->discriminant_index = index;
be2daae6 15330 else if (field.variant.default_branch)
2ddeaf8a
TT
15331 di->default_index = index;
15332 else
be2daae6 15333 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15334 }
15335 }
15336
be2daae6
TT
15337 /* Copy the saved-up fields into the field vector. */
15338 for (int i = 0; i < nfields; ++i)
c906108c 15339 {
be2daae6
TT
15340 struct nextfield &field
15341 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15342 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15343
be2daae6
TT
15344 TYPE_FIELD (type, i) = field.field;
15345 switch (field.accessibility)
c906108c 15346 {
c5aa993b 15347 case DW_ACCESS_private:
b4ba55a1 15348 if (cu->language != language_ada)
be2daae6 15349 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15350 break;
c906108c 15351
c5aa993b 15352 case DW_ACCESS_protected:
b4ba55a1 15353 if (cu->language != language_ada)
be2daae6 15354 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15355 break;
c906108c 15356
c5aa993b
JM
15357 case DW_ACCESS_public:
15358 break;
c906108c 15359
c5aa993b
JM
15360 default:
15361 /* Unknown accessibility. Complain and treat it as public. */
15362 {
b98664d3 15363 complaint (_("unsupported accessibility %d"),
be2daae6 15364 field.accessibility);
c5aa993b
JM
15365 }
15366 break;
c906108c 15367 }
be2daae6 15368 if (i < fip->baseclasses.size ())
c906108c 15369 {
be2daae6 15370 switch (field.virtuality)
c906108c 15371 {
c5aa993b
JM
15372 case DW_VIRTUALITY_virtual:
15373 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15374 if (cu->language == language_ada)
a73c6dcd 15375 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15376 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15377 break;
c906108c
SS
15378 }
15379 }
c906108c
SS
15380 }
15381}
15382
7d27a96d
TT
15383/* Return true if this member function is a constructor, false
15384 otherwise. */
15385
15386static int
15387dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15388{
15389 const char *fieldname;
fe978cb0 15390 const char *type_name;
7d27a96d
TT
15391 int len;
15392
15393 if (die->parent == NULL)
15394 return 0;
15395
15396 if (die->parent->tag != DW_TAG_structure_type
15397 && die->parent->tag != DW_TAG_union_type
15398 && die->parent->tag != DW_TAG_class_type)
15399 return 0;
15400
15401 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15402 type_name = dwarf2_name (die->parent, cu);
15403 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15404 return 0;
15405
15406 len = strlen (fieldname);
fe978cb0
PA
15407 return (strncmp (fieldname, type_name, len) == 0
15408 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15409}
15410
c906108c
SS
15411/* Add a member function to the proper fieldlist. */
15412
15413static void
107d2387 15414dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15415 struct type *type, struct dwarf2_cu *cu)
c906108c 15416{
518817b3 15417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15418 struct attribute *attr;
c906108c 15419 int i;
be2daae6 15420 struct fnfieldlist *flp = nullptr;
c906108c 15421 struct fn_field *fnp;
15d034d0 15422 const char *fieldname;
f792889a 15423 struct type *this_type;
60d5a603 15424 enum dwarf_access_attribute accessibility;
c906108c 15425
b4ba55a1 15426 if (cu->language == language_ada)
a73c6dcd 15427 error (_("unexpected member function in Ada type"));
b4ba55a1 15428
2df3850c 15429 /* Get name of member function. */
39cbfefa
DJ
15430 fieldname = dwarf2_name (die, cu);
15431 if (fieldname == NULL)
2df3850c 15432 return;
c906108c 15433
c906108c 15434 /* Look up member function name in fieldlist. */
be2daae6 15435 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15436 {
27bfe10e 15437 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15438 {
15439 flp = &fip->fnfieldlists[i];
15440 break;
15441 }
c906108c
SS
15442 }
15443
be2daae6
TT
15444 /* Create a new fnfieldlist if necessary. */
15445 if (flp == nullptr)
c906108c 15446 {
be2daae6
TT
15447 fip->fnfieldlists.emplace_back ();
15448 flp = &fip->fnfieldlists.back ();
c906108c 15449 flp->name = fieldname;
be2daae6 15450 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15451 }
15452
be2daae6
TT
15453 /* Create a new member function field and add it to the vector of
15454 fnfieldlists. */
15455 flp->fnfields.emplace_back ();
15456 fnp = &flp->fnfields.back ();
3da10d80
KS
15457
15458 /* Delay processing of the physname until later. */
9c37b5ae 15459 if (cu->language == language_cplus)
be2daae6
TT
15460 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15461 die, cu);
3da10d80
KS
15462 else
15463 {
1d06ead6 15464 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15465 fnp->physname = physname ? physname : "";
15466 }
15467
c906108c 15468 fnp->type = alloc_type (objfile);
f792889a
DJ
15469 this_type = read_type_die (die, cu);
15470 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15471 {
f792889a 15472 int nparams = TYPE_NFIELDS (this_type);
c906108c 15473
f792889a 15474 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15475 of the method itself (TYPE_CODE_METHOD). */
15476 smash_to_method_type (fnp->type, type,
f792889a
DJ
15477 TYPE_TARGET_TYPE (this_type),
15478 TYPE_FIELDS (this_type),
15479 TYPE_NFIELDS (this_type),
15480 TYPE_VARARGS (this_type));
c906108c
SS
15481
15482 /* Handle static member functions.
c5aa993b 15483 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15484 member functions. G++ helps GDB by marking the first
15485 parameter for non-static member functions (which is the this
15486 pointer) as artificial. We obtain this information from
15487 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15488 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15489 fnp->voffset = VOFFSET_STATIC;
15490 }
15491 else
b98664d3 15492 complaint (_("member function type missing for '%s'"),
3da10d80 15493 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15494
15495 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15496 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15497 fnp->fcontext = die_containing_type (die, cu);
c906108c 15498
3e43a32a
MS
15499 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15500 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15501
15502 /* Get accessibility. */
e142c38c 15503 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15504 if (attr)
aead7601 15505 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15506 else
15507 accessibility = dwarf2_default_access_attribute (die, cu);
15508 switch (accessibility)
c906108c 15509 {
60d5a603
JK
15510 case DW_ACCESS_private:
15511 fnp->is_private = 1;
15512 break;
15513 case DW_ACCESS_protected:
15514 fnp->is_protected = 1;
15515 break;
c906108c
SS
15516 }
15517
b02dede2 15518 /* Check for artificial methods. */
e142c38c 15519 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15520 if (attr && DW_UNSND (attr) != 0)
15521 fnp->is_artificial = 1;
15522
7d27a96d
TT
15523 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15524
0d564a31 15525 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15526 function. For older versions of GCC, this is an offset in the
15527 appropriate virtual table, as specified by DW_AT_containing_type.
15528 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15529 to the object address. */
15530
e142c38c 15531 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15532 if (attr)
8e19ed76 15533 {
aec5aa8b 15534 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15535 {
aec5aa8b
TT
15536 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15537 {
15538 /* Old-style GCC. */
15539 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15540 }
15541 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15542 || (DW_BLOCK (attr)->size > 1
15543 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15544 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15545 {
aec5aa8b
TT
15546 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15547 if ((fnp->voffset % cu->header.addr_size) != 0)
15548 dwarf2_complex_location_expr_complaint ();
15549 else
15550 fnp->voffset /= cu->header.addr_size;
15551 fnp->voffset += 2;
15552 }
15553 else
15554 dwarf2_complex_location_expr_complaint ();
15555
15556 if (!fnp->fcontext)
7e993ebf
KS
15557 {
15558 /* If there is no `this' field and no DW_AT_containing_type,
15559 we cannot actually find a base class context for the
15560 vtable! */
15561 if (TYPE_NFIELDS (this_type) == 0
15562 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15563 {
b98664d3 15564 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15565 "function \"%s\" (offset %s)"),
15566 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15567 }
15568 else
15569 {
15570 fnp->fcontext
15571 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15572 }
15573 }
aec5aa8b 15574 }
3690dd37 15575 else if (attr_form_is_section_offset (attr))
8e19ed76 15576 {
4d3c2250 15577 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15578 }
15579 else
15580 {
4d3c2250
KB
15581 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15582 fieldname);
8e19ed76 15583 }
0d564a31 15584 }
d48cc9dd
DJ
15585 else
15586 {
15587 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15588 if (attr && DW_UNSND (attr))
15589 {
15590 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15591 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15592 "but the vtable offset is not specified"),
9d8780f0 15593 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15594 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15595 TYPE_CPLUS_DYNAMIC (type) = 1;
15596 }
15597 }
c906108c
SS
15598}
15599
15600/* Create the vector of member function fields, and attach it to the type. */
15601
15602static void
fba45db2 15603dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15604 struct dwarf2_cu *cu)
c906108c 15605{
b4ba55a1 15606 if (cu->language == language_ada)
a73c6dcd 15607 error (_("unexpected member functions in Ada type"));
b4ba55a1 15608
c906108c
SS
15609 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15610 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15611 TYPE_ALLOC (type,
15612 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15613
be2daae6 15614 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15615 {
be2daae6 15616 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15617 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15618
be2daae6
TT
15619 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15620 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15621 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15622 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15623
15624 for (int k = 0; k < nf.fnfields.size (); ++k)
15625 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15626 }
15627
be2daae6 15628 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15629}
15630
1168df01
JB
15631/* Returns non-zero if NAME is the name of a vtable member in CU's
15632 language, zero otherwise. */
15633static int
15634is_vtable_name (const char *name, struct dwarf2_cu *cu)
15635{
15636 static const char vptr[] = "_vptr";
15637
9c37b5ae
TT
15638 /* Look for the C++ form of the vtable. */
15639 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15640 return 1;
15641
15642 return 0;
15643}
15644
c0dd20ea 15645/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15646 functions, with the ABI-specified layout. If TYPE describes
15647 such a structure, smash it into a member function type.
61049d3b
DJ
15648
15649 GCC shouldn't do this; it should just output pointer to member DIEs.
15650 This is GCC PR debug/28767. */
c0dd20ea 15651
0b92b5bb
TT
15652static void
15653quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15654{
09e2d7c7 15655 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15656
15657 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15658 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15659 return;
c0dd20ea
DJ
15660
15661 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15662 if (TYPE_FIELD_NAME (type, 0) == NULL
15663 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15664 || TYPE_FIELD_NAME (type, 1) == NULL
15665 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15666 return;
c0dd20ea
DJ
15667
15668 /* Find the type of the method. */
0b92b5bb 15669 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15670 if (pfn_type == NULL
15671 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15672 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15673 return;
c0dd20ea
DJ
15674
15675 /* Look for the "this" argument. */
15676 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15677 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15678 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15679 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15680 return;
c0dd20ea 15681
09e2d7c7 15682 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15683 new_type = alloc_type (objfile);
09e2d7c7 15684 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15685 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15686 TYPE_VARARGS (pfn_type));
0b92b5bb 15687 smash_to_methodptr_type (type, new_type);
c0dd20ea 15688}
1168df01 15689
2b4424c3
TT
15690/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15691 appropriate error checking and issuing complaints if there is a
15692 problem. */
15693
15694static ULONGEST
15695get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15696{
15697 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15698
15699 if (attr == nullptr)
15700 return 0;
15701
15702 if (!attr_form_is_constant (attr))
15703 {
b98664d3 15704 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15705 " - DIE at %s [in module %s]"),
15706 sect_offset_str (die->sect_off),
15707 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15708 return 0;
15709 }
15710
15711 ULONGEST align;
15712 if (attr->form == DW_FORM_sdata)
15713 {
15714 LONGEST val = DW_SND (attr);
15715 if (val < 0)
15716 {
b98664d3 15717 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15718 " - DIE at %s [in module %s]"),
15719 sect_offset_str (die->sect_off),
15720 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15721 return 0;
15722 }
15723 align = val;
15724 }
15725 else
15726 align = DW_UNSND (attr);
15727
15728 if (align == 0)
15729 {
b98664d3 15730 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15731 " - DIE at %s [in module %s]"),
15732 sect_offset_str (die->sect_off),
15733 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15734 return 0;
15735 }
15736 if ((align & (align - 1)) != 0)
15737 {
b98664d3 15738 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15739 " - DIE at %s [in module %s]"),
15740 sect_offset_str (die->sect_off),
15741 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15742 return 0;
15743 }
15744
15745 return align;
15746}
15747
15748/* If the DIE has a DW_AT_alignment attribute, use its value to set
15749 the alignment for TYPE. */
15750
15751static void
15752maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15753 struct type *type)
15754{
15755 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15756 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15757 " - DIE at %s [in module %s]"),
15758 sect_offset_str (die->sect_off),
15759 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15760}
685b1105 15761
c906108c 15762/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15763 (definition) to create a type for the structure or union. Fill in
15764 the type's name and general properties; the members will not be
83655187
DE
15765 processed until process_structure_scope. A symbol table entry for
15766 the type will also not be done until process_structure_scope (assuming
15767 the type has a name).
c906108c 15768
c767944b
DJ
15769 NOTE: we need to call these functions regardless of whether or not the
15770 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15771 structure or union. This gets the type entered into our set of
83655187 15772 user defined types. */
c906108c 15773
f792889a 15774static struct type *
134d01f1 15775read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15776{
518817b3 15777 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15778 struct type *type;
15779 struct attribute *attr;
15d034d0 15780 const char *name;
c906108c 15781
348e048f
DE
15782 /* If the definition of this type lives in .debug_types, read that type.
15783 Don't follow DW_AT_specification though, that will take us back up
15784 the chain and we want to go down. */
45e58e77 15785 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15786 if (attr)
15787 {
ac9ec31b 15788 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15789
ac9ec31b 15790 /* The type's CU may not be the same as CU.
02142a6c 15791 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15792 return set_die_type (die, type, cu);
15793 }
15794
c0dd20ea 15795 type = alloc_type (objfile);
c906108c 15796 INIT_CPLUS_SPECIFIC (type);
93311388 15797
39cbfefa
DJ
15798 name = dwarf2_name (die, cu);
15799 if (name != NULL)
c906108c 15800 {
987504bb 15801 if (cu->language == language_cplus
c44af4eb
TT
15802 || cu->language == language_d
15803 || cu->language == language_rust)
63d06c5c 15804 {
15d034d0 15805 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15806
15807 /* dwarf2_full_name might have already finished building the DIE's
15808 type. If so, there is no need to continue. */
15809 if (get_die_type (die, cu) != NULL)
15810 return get_die_type (die, cu);
15811
e86ca25f 15812 TYPE_NAME (type) = full_name;
63d06c5c
DC
15813 }
15814 else
15815 {
d8151005
DJ
15816 /* The name is already allocated along with this objfile, so
15817 we don't need to duplicate it for the type. */
e86ca25f 15818 TYPE_NAME (type) = name;
63d06c5c 15819 }
c906108c
SS
15820 }
15821
15822 if (die->tag == DW_TAG_structure_type)
15823 {
15824 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15825 }
15826 else if (die->tag == DW_TAG_union_type)
15827 {
15828 TYPE_CODE (type) = TYPE_CODE_UNION;
15829 }
2ddeaf8a
TT
15830 else if (die->tag == DW_TAG_variant_part)
15831 {
15832 TYPE_CODE (type) = TYPE_CODE_UNION;
15833 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15834 }
c906108c
SS
15835 else
15836 {
4753d33b 15837 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15838 }
15839
0cc2414c
TT
15840 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15841 TYPE_DECLARED_CLASS (type) = 1;
15842
e142c38c 15843 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15844 if (attr)
15845 {
155bfbd3
JB
15846 if (attr_form_is_constant (attr))
15847 TYPE_LENGTH (type) = DW_UNSND (attr);
15848 else
15849 {
15850 /* For the moment, dynamic type sizes are not supported
15851 by GDB's struct type. The actual size is determined
15852 on-demand when resolving the type of a given object,
15853 so set the type's length to zero for now. Otherwise,
15854 we record an expression as the length, and that expression
15855 could lead to a very large value, which could eventually
15856 lead to us trying to allocate that much memory when creating
15857 a value of that type. */
15858 TYPE_LENGTH (type) = 0;
15859 }
c906108c
SS
15860 }
15861 else
15862 {
15863 TYPE_LENGTH (type) = 0;
15864 }
15865
2b4424c3
TT
15866 maybe_set_alignment (cu, die, type);
15867
5230b05a 15868 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15869 {
5230b05a
WT
15870 /* ICC<14 does not output the required DW_AT_declaration on
15871 incomplete types, but gives them a size of zero. */
422b1cb0 15872 TYPE_STUB (type) = 1;
685b1105
JK
15873 }
15874 else
15875 TYPE_STUB_SUPPORTED (type) = 1;
15876
dc718098 15877 if (die_is_declaration (die, cu))
876cecd0 15878 TYPE_STUB (type) = 1;
a6c727b2
DJ
15879 else if (attr == NULL && die->child == NULL
15880 && producer_is_realview (cu->producer))
15881 /* RealView does not output the required DW_AT_declaration
15882 on incomplete types. */
15883 TYPE_STUB (type) = 1;
dc718098 15884
c906108c
SS
15885 /* We need to add the type field to the die immediately so we don't
15886 infinitely recurse when dealing with pointers to the structure
0963b4bd 15887 type within the structure itself. */
1c379e20 15888 set_die_type (die, type, cu);
c906108c 15889
7e314c57
JK
15890 /* set_die_type should be already done. */
15891 set_descriptive_type (type, die, cu);
15892
c767944b
DJ
15893 return type;
15894}
15895
2ddeaf8a
TT
15896/* A helper for process_structure_scope that handles a single member
15897 DIE. */
15898
15899static void
15900handle_struct_member_die (struct die_info *child_die, struct type *type,
15901 struct field_info *fi,
15902 std::vector<struct symbol *> *template_args,
15903 struct dwarf2_cu *cu)
15904{
15905 if (child_die->tag == DW_TAG_member
15906 || child_die->tag == DW_TAG_variable
15907 || child_die->tag == DW_TAG_variant_part)
15908 {
15909 /* NOTE: carlton/2002-11-05: A C++ static data member
15910 should be a DW_TAG_member that is a declaration, but
15911 all versions of G++ as of this writing (so through at
15912 least 3.2.1) incorrectly generate DW_TAG_variable
15913 tags for them instead. */
15914 dwarf2_add_field (fi, child_die, cu);
15915 }
15916 else if (child_die->tag == DW_TAG_subprogram)
15917 {
15918 /* Rust doesn't have member functions in the C++ sense.
15919 However, it does emit ordinary functions as children
15920 of a struct DIE. */
15921 if (cu->language == language_rust)
15922 read_func_scope (child_die, cu);
15923 else
15924 {
15925 /* C++ member function. */
15926 dwarf2_add_member_fn (fi, child_die, type, cu);
15927 }
15928 }
15929 else if (child_die->tag == DW_TAG_inheritance)
15930 {
15931 /* C++ base class field. */
15932 dwarf2_add_field (fi, child_die, cu);
15933 }
15934 else if (type_can_define_types (child_die))
15935 dwarf2_add_type_defn (fi, child_die, cu);
15936 else if (child_die->tag == DW_TAG_template_type_param
15937 || child_die->tag == DW_TAG_template_value_param)
15938 {
15939 struct symbol *arg = new_symbol (child_die, NULL, cu);
15940
15941 if (arg != NULL)
15942 template_args->push_back (arg);
15943 }
15944 else if (child_die->tag == DW_TAG_variant)
15945 {
15946 /* In a variant we want to get the discriminant and also add a
15947 field for our sole member child. */
15948 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15949
bde09ab7 15950 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15951 variant_child != NULL;
15952 variant_child = sibling_die (variant_child))
15953 {
15954 if (variant_child->tag == DW_TAG_member)
15955 {
15956 handle_struct_member_die (variant_child, type, fi,
15957 template_args, cu);
15958 /* Only handle the one. */
15959 break;
15960 }
15961 }
15962
15963 /* We don't handle this but we might as well report it if we see
15964 it. */
15965 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15966 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15967 " - DIE at %s [in module %s]"),
15968 sect_offset_str (child_die->sect_off),
15969 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15970
15971 /* The first field was just added, so we can stash the
15972 discriminant there. */
be2daae6 15973 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15974 if (discr == NULL)
be2daae6 15975 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15976 else
be2daae6 15977 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15978 }
15979}
15980
c767944b
DJ
15981/* Finish creating a structure or union type, including filling in
15982 its members and creating a symbol for it. */
15983
15984static void
15985process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15986{
518817b3 15987 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15988 struct die_info *child_die;
c767944b
DJ
15989 struct type *type;
15990
15991 type = get_die_type (die, cu);
15992 if (type == NULL)
15993 type = read_structure_type (die, cu);
15994
2ddeaf8a
TT
15995 /* When reading a DW_TAG_variant_part, we need to notice when we
15996 read the discriminant member, so we can record it later in the
15997 discriminant_info. */
15998 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15999 sect_offset discr_offset;
3e1d3d8c 16000 bool has_template_parameters = false;
2ddeaf8a
TT
16001
16002 if (is_variant_part)
16003 {
16004 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16005 if (discr == NULL)
16006 {
16007 /* Maybe it's a univariant form, an extension we support.
16008 In this case arrange not to check the offset. */
16009 is_variant_part = false;
16010 }
16011 else if (attr_form_is_ref (discr))
16012 {
16013 struct dwarf2_cu *target_cu = cu;
16014 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16015
16016 discr_offset = target_die->sect_off;
16017 }
16018 else
16019 {
b98664d3 16020 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16021 " - DIE at %s [in module %s]"),
16022 sect_offset_str (die->sect_off),
16023 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16024 is_variant_part = false;
16025 }
16026 }
16027
e142c38c 16028 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16029 {
16030 struct field_info fi;
2f4732b0 16031 std::vector<struct symbol *> template_args;
c906108c 16032
639d11d3 16033 child_die = die->child;
c906108c
SS
16034
16035 while (child_die && child_die->tag)
16036 {
2ddeaf8a 16037 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16038
2ddeaf8a 16039 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16040 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16041
c906108c
SS
16042 child_die = sibling_die (child_die);
16043 }
16044
34eaf542 16045 /* Attach template arguments to type. */
2f4732b0 16046 if (!template_args.empty ())
34eaf542 16047 {
3e1d3d8c 16048 has_template_parameters = true;
34eaf542 16049 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16050 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16051 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16052 = XOBNEWVEC (&objfile->objfile_obstack,
16053 struct symbol *,
16054 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16055 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16056 template_args.data (),
34eaf542
TT
16057 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16058 * sizeof (struct symbol *)));
34eaf542
TT
16059 }
16060
c906108c
SS
16061 /* Attach fields and member functions to the type. */
16062 if (fi.nfields)
e7c27a73 16063 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16064 if (!fi.fnfieldlists.empty ())
c906108c 16065 {
e7c27a73 16066 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16067
c5aa993b 16068 /* Get the type which refers to the base class (possibly this
c906108c 16069 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16070 class from the DW_AT_containing_type attribute. This use of
16071 DW_AT_containing_type is a GNU extension. */
c906108c 16072
e142c38c 16073 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16074 {
e7c27a73 16075 struct type *t = die_containing_type (die, cu);
c906108c 16076
ae6ae975 16077 set_type_vptr_basetype (type, t);
c906108c
SS
16078 if (type == t)
16079 {
c906108c
SS
16080 int i;
16081
16082 /* Our own class provides vtbl ptr. */
16083 for (i = TYPE_NFIELDS (t) - 1;
16084 i >= TYPE_N_BASECLASSES (t);
16085 --i)
16086 {
0d5cff50 16087 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16088
1168df01 16089 if (is_vtable_name (fieldname, cu))
c906108c 16090 {
ae6ae975 16091 set_type_vptr_fieldno (type, i);
c906108c
SS
16092 break;
16093 }
16094 }
16095
16096 /* Complain if virtual function table field not found. */
16097 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16098 complaint (_("virtual function table pointer "
3e43a32a 16099 "not found when defining class '%s'"),
e86ca25f 16100 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16101 }
16102 else
16103 {
ae6ae975 16104 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16105 }
16106 }
f6235d4c 16107 else if (cu->producer
61012eef 16108 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16109 {
16110 /* The IBM XLC compiler does not provide direct indication
16111 of the containing type, but the vtable pointer is
16112 always named __vfp. */
16113
16114 int i;
16115
16116 for (i = TYPE_NFIELDS (type) - 1;
16117 i >= TYPE_N_BASECLASSES (type);
16118 --i)
16119 {
16120 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16121 {
ae6ae975
DE
16122 set_type_vptr_fieldno (type, i);
16123 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16124 break;
16125 }
16126 }
16127 }
c906108c 16128 }
98751a41
JK
16129
16130 /* Copy fi.typedef_field_list linked list elements content into the
16131 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16132 if (!fi.typedef_field_list.empty ())
98751a41 16133 {
be2daae6 16134 int count = fi.typedef_field_list.size ();
98751a41 16135
a0d7a4ff 16136 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16137 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16138 = ((struct decl_field *)
be2daae6
TT
16139 TYPE_ALLOC (type,
16140 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16141 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16142
be2daae6
TT
16143 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16144 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16145 }
c767944b 16146
883fd55a
KS
16147 /* Copy fi.nested_types_list linked list elements content into the
16148 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16149 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16150 {
be2daae6 16151 int count = fi.nested_types_list.size ();
883fd55a
KS
16152
16153 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16154 TYPE_NESTED_TYPES_ARRAY (type)
16155 = ((struct decl_field *)
be2daae6
TT
16156 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16157 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16158
be2daae6
TT
16159 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16160 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16161 }
c906108c 16162 }
63d06c5c 16163
bb5ed363 16164 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16165 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16166 cu->rust_unions.push_back (type);
0b92b5bb 16167
90aeadfc
DC
16168 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16169 snapshots) has been known to create a die giving a declaration
16170 for a class that has, as a child, a die giving a definition for a
16171 nested class. So we have to process our children even if the
16172 current die is a declaration. Normally, of course, a declaration
16173 won't have any children at all. */
134d01f1 16174
ca040673
DE
16175 child_die = die->child;
16176
90aeadfc
DC
16177 while (child_die != NULL && child_die->tag)
16178 {
16179 if (child_die->tag == DW_TAG_member
16180 || child_die->tag == DW_TAG_variable
34eaf542
TT
16181 || child_die->tag == DW_TAG_inheritance
16182 || child_die->tag == DW_TAG_template_value_param
16183 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16184 {
90aeadfc 16185 /* Do nothing. */
134d01f1 16186 }
90aeadfc
DC
16187 else
16188 process_die (child_die, cu);
134d01f1 16189
90aeadfc 16190 child_die = sibling_die (child_die);
134d01f1
DJ
16191 }
16192
fa4028e9
JB
16193 /* Do not consider external references. According to the DWARF standard,
16194 these DIEs are identified by the fact that they have no byte_size
16195 attribute, and a declaration attribute. */
16196 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16197 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16198 {
16199 struct symbol *sym = new_symbol (die, type, cu);
16200
16201 if (has_template_parameters)
16202 {
a776957c
TT
16203 struct symtab *symtab;
16204 if (sym != nullptr)
16205 symtab = symbol_symtab (sym);
16206 else if (cu->line_header != nullptr)
16207 {
16208 /* Any related symtab will do. */
16209 symtab
16210 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16211 }
16212 else
16213 {
16214 symtab = nullptr;
16215 complaint (_("could not find suitable "
16216 "symtab for template parameter"
16217 " - DIE at %s [in module %s]"),
16218 sect_offset_str (die->sect_off),
16219 objfile_name (objfile));
16220 }
16221
16222 if (symtab != nullptr)
16223 {
16224 /* Make sure that the symtab is set on the new symbols.
16225 Even though they don't appear in this symtab directly,
16226 other parts of gdb assume that symbols do, and this is
16227 reasonably true. */
16228 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16229 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16230 }
3e1d3d8c
TT
16231 }
16232 }
134d01f1
DJ
16233}
16234
55426c9d
JB
16235/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16236 update TYPE using some information only available in DIE's children. */
16237
16238static void
16239update_enumeration_type_from_children (struct die_info *die,
16240 struct type *type,
16241 struct dwarf2_cu *cu)
16242{
60f7655a 16243 struct die_info *child_die;
55426c9d
JB
16244 int unsigned_enum = 1;
16245 int flag_enum = 1;
16246 ULONGEST mask = 0;
55426c9d 16247
8268c778 16248 auto_obstack obstack;
55426c9d 16249
60f7655a
DE
16250 for (child_die = die->child;
16251 child_die != NULL && child_die->tag;
16252 child_die = sibling_die (child_die))
55426c9d
JB
16253 {
16254 struct attribute *attr;
16255 LONGEST value;
16256 const gdb_byte *bytes;
16257 struct dwarf2_locexpr_baton *baton;
16258 const char *name;
60f7655a 16259
55426c9d
JB
16260 if (child_die->tag != DW_TAG_enumerator)
16261 continue;
16262
16263 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16264 if (attr == NULL)
16265 continue;
16266
16267 name = dwarf2_name (child_die, cu);
16268 if (name == NULL)
16269 name = "<anonymous enumerator>";
16270
16271 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16272 &value, &bytes, &baton);
16273 if (value < 0)
16274 {
16275 unsigned_enum = 0;
16276 flag_enum = 0;
16277 }
16278 else if ((mask & value) != 0)
16279 flag_enum = 0;
16280 else
16281 mask |= value;
16282
16283 /* If we already know that the enum type is neither unsigned, nor
16284 a flag type, no need to look at the rest of the enumerates. */
16285 if (!unsigned_enum && !flag_enum)
16286 break;
55426c9d
JB
16287 }
16288
16289 if (unsigned_enum)
16290 TYPE_UNSIGNED (type) = 1;
16291 if (flag_enum)
16292 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16293}
16294
134d01f1
DJ
16295/* Given a DW_AT_enumeration_type die, set its type. We do not
16296 complete the type's fields yet, or create any symbols. */
c906108c 16297
f792889a 16298static struct type *
134d01f1 16299read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16300{
518817b3 16301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16302 struct type *type;
c906108c 16303 struct attribute *attr;
0114d602 16304 const char *name;
134d01f1 16305
348e048f
DE
16306 /* If the definition of this type lives in .debug_types, read that type.
16307 Don't follow DW_AT_specification though, that will take us back up
16308 the chain and we want to go down. */
45e58e77 16309 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16310 if (attr)
16311 {
ac9ec31b 16312 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16313
ac9ec31b 16314 /* The type's CU may not be the same as CU.
02142a6c 16315 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16316 return set_die_type (die, type, cu);
16317 }
16318
c906108c
SS
16319 type = alloc_type (objfile);
16320
16321 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16322 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16323 if (name != NULL)
e86ca25f 16324 TYPE_NAME (type) = name;
c906108c 16325
0626fc76
TT
16326 attr = dwarf2_attr (die, DW_AT_type, cu);
16327 if (attr != NULL)
16328 {
16329 struct type *underlying_type = die_type (die, cu);
16330
16331 TYPE_TARGET_TYPE (type) = underlying_type;
16332 }
16333
e142c38c 16334 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16335 if (attr)
16336 {
16337 TYPE_LENGTH (type) = DW_UNSND (attr);
16338 }
16339 else
16340 {
16341 TYPE_LENGTH (type) = 0;
16342 }
16343
2b4424c3
TT
16344 maybe_set_alignment (cu, die, type);
16345
137033e9
JB
16346 /* The enumeration DIE can be incomplete. In Ada, any type can be
16347 declared as private in the package spec, and then defined only
16348 inside the package body. Such types are known as Taft Amendment
16349 Types. When another package uses such a type, an incomplete DIE
16350 may be generated by the compiler. */
02eb380e 16351 if (die_is_declaration (die, cu))
876cecd0 16352 TYPE_STUB (type) = 1;
02eb380e 16353
0626fc76
TT
16354 /* Finish the creation of this type by using the enum's children.
16355 We must call this even when the underlying type has been provided
16356 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16357 update_enumeration_type_from_children (die, type, cu);
16358
0626fc76
TT
16359 /* If this type has an underlying type that is not a stub, then we
16360 may use its attributes. We always use the "unsigned" attribute
16361 in this situation, because ordinarily we guess whether the type
16362 is unsigned -- but the guess can be wrong and the underlying type
16363 can tell us the reality. However, we defer to a local size
16364 attribute if one exists, because this lets the compiler override
16365 the underlying type if needed. */
16366 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16367 {
16368 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16369 if (TYPE_LENGTH (type) == 0)
16370 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16371 if (TYPE_RAW_ALIGN (type) == 0
16372 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16373 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16374 }
16375
3d567982
TT
16376 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16377
f792889a 16378 return set_die_type (die, type, cu);
134d01f1
DJ
16379}
16380
16381/* Given a pointer to a die which begins an enumeration, process all
16382 the dies that define the members of the enumeration, and create the
16383 symbol for the enumeration type.
16384
16385 NOTE: We reverse the order of the element list. */
16386
16387static void
16388process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16389{
f792889a 16390 struct type *this_type;
134d01f1 16391
f792889a
DJ
16392 this_type = get_die_type (die, cu);
16393 if (this_type == NULL)
16394 this_type = read_enumeration_type (die, cu);
9dc481d3 16395
639d11d3 16396 if (die->child != NULL)
c906108c 16397 {
9dc481d3
DE
16398 struct die_info *child_die;
16399 struct symbol *sym;
16400 struct field *fields = NULL;
16401 int num_fields = 0;
15d034d0 16402 const char *name;
9dc481d3 16403
639d11d3 16404 child_die = die->child;
c906108c
SS
16405 while (child_die && child_die->tag)
16406 {
16407 if (child_die->tag != DW_TAG_enumerator)
16408 {
e7c27a73 16409 process_die (child_die, cu);
c906108c
SS
16410 }
16411 else
16412 {
39cbfefa
DJ
16413 name = dwarf2_name (child_die, cu);
16414 if (name)
c906108c 16415 {
f792889a 16416 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16417
16418 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16419 {
16420 fields = (struct field *)
16421 xrealloc (fields,
16422 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16423 * sizeof (struct field));
c906108c
SS
16424 }
16425
3567439c 16426 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16427 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16428 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16429 FIELD_BITSIZE (fields[num_fields]) = 0;
16430
16431 num_fields++;
16432 }
16433 }
16434
16435 child_die = sibling_die (child_die);
16436 }
16437
16438 if (num_fields)
16439 {
f792889a
DJ
16440 TYPE_NFIELDS (this_type) = num_fields;
16441 TYPE_FIELDS (this_type) = (struct field *)
16442 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16443 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16444 sizeof (struct field) * num_fields);
b8c9b27d 16445 xfree (fields);
c906108c 16446 }
c906108c 16447 }
134d01f1 16448
6c83ed52
TT
16449 /* If we are reading an enum from a .debug_types unit, and the enum
16450 is a declaration, and the enum is not the signatured type in the
16451 unit, then we do not want to add a symbol for it. Adding a
16452 symbol would in some cases obscure the true definition of the
16453 enum, giving users an incomplete type when the definition is
16454 actually available. Note that we do not want to do this for all
16455 enums which are just declarations, because C++0x allows forward
16456 enum declarations. */
3019eac3 16457 if (cu->per_cu->is_debug_types
6c83ed52
TT
16458 && die_is_declaration (die, cu))
16459 {
52dc124a 16460 struct signatured_type *sig_type;
6c83ed52 16461
c0f78cd4 16462 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16463 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16464 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16465 return;
16466 }
16467
f792889a 16468 new_symbol (die, this_type, cu);
c906108c
SS
16469}
16470
16471/* Extract all information from a DW_TAG_array_type DIE and put it in
16472 the DIE's type field. For now, this only handles one dimensional
16473 arrays. */
16474
f792889a 16475static struct type *
e7c27a73 16476read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16477{
518817b3 16478 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16479 struct die_info *child_die;
7e314c57 16480 struct type *type;
c906108c 16481 struct type *element_type, *range_type, *index_type;
c906108c 16482 struct attribute *attr;
15d034d0 16483 const char *name;
a405673c 16484 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16485 unsigned int bit_stride = 0;
c906108c 16486
e7c27a73 16487 element_type = die_type (die, cu);
c906108c 16488
7e314c57
JK
16489 /* The die_type call above may have already set the type for this DIE. */
16490 type = get_die_type (die, cu);
16491 if (type)
16492 return type;
16493
dc53a7ad
JB
16494 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16495 if (attr != NULL)
a405673c
JB
16496 {
16497 int stride_ok;
16498
16499 byte_stride_prop
16500 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16501 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16502 if (!stride_ok)
16503 {
b98664d3 16504 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16505 " - DIE at %s [in module %s]"),
16506 sect_offset_str (die->sect_off),
518817b3 16507 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16508 /* Ignore this attribute. We will likely not be able to print
16509 arrays of this type correctly, but there is little we can do
16510 to help if we cannot read the attribute's value. */
16511 byte_stride_prop = NULL;
16512 }
16513 }
dc53a7ad
JB
16514
16515 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16516 if (attr != NULL)
16517 bit_stride = DW_UNSND (attr);
16518
c906108c
SS
16519 /* Irix 6.2 native cc creates array types without children for
16520 arrays with unspecified length. */
639d11d3 16521 if (die->child == NULL)
c906108c 16522 {
46bf5051 16523 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16524 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16525 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16526 byte_stride_prop, bit_stride);
f792889a 16527 return set_die_type (die, type, cu);
c906108c
SS
16528 }
16529
791afaa2 16530 std::vector<struct type *> range_types;
639d11d3 16531 child_die = die->child;
c906108c
SS
16532 while (child_die && child_die->tag)
16533 {
16534 if (child_die->tag == DW_TAG_subrange_type)
16535 {
f792889a 16536 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16537
f792889a 16538 if (child_type != NULL)
a02abb62 16539 {
0963b4bd
MS
16540 /* The range type was succesfully read. Save it for the
16541 array type creation. */
791afaa2 16542 range_types.push_back (child_type);
a02abb62 16543 }
c906108c
SS
16544 }
16545 child_die = sibling_die (child_die);
16546 }
16547
16548 /* Dwarf2 dimensions are output from left to right, create the
16549 necessary array types in backwards order. */
7ca2d3a3 16550
c906108c 16551 type = element_type;
7ca2d3a3
DL
16552
16553 if (read_array_order (die, cu) == DW_ORD_col_major)
16554 {
16555 int i = 0;
9a619af0 16556
791afaa2 16557 while (i < range_types.size ())
dc53a7ad 16558 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16559 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16560 }
16561 else
16562 {
791afaa2 16563 size_t ndim = range_types.size ();
7ca2d3a3 16564 while (ndim-- > 0)
dc53a7ad 16565 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16566 byte_stride_prop, bit_stride);
7ca2d3a3 16567 }
c906108c 16568
f5f8a009
EZ
16569 /* Understand Dwarf2 support for vector types (like they occur on
16570 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16571 array type. This is not part of the Dwarf2/3 standard yet, but a
16572 custom vendor extension. The main difference between a regular
16573 array and the vector variant is that vectors are passed by value
16574 to functions. */
e142c38c 16575 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16576 if (attr)
ea37ba09 16577 make_vector_type (type);
f5f8a009 16578
dbc98a8b
KW
16579 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16580 implementation may choose to implement triple vectors using this
16581 attribute. */
16582 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16583 if (attr)
16584 {
16585 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16586 TYPE_LENGTH (type) = DW_UNSND (attr);
16587 else
b98664d3 16588 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16589 "than the total size of elements"));
dbc98a8b
KW
16590 }
16591
39cbfefa
DJ
16592 name = dwarf2_name (die, cu);
16593 if (name)
16594 TYPE_NAME (type) = name;
6e70227d 16595
2b4424c3
TT
16596 maybe_set_alignment (cu, die, type);
16597
0963b4bd 16598 /* Install the type in the die. */
7e314c57
JK
16599 set_die_type (die, type, cu);
16600
16601 /* set_die_type should be already done. */
b4ba55a1
JB
16602 set_descriptive_type (type, die, cu);
16603
7e314c57 16604 return type;
c906108c
SS
16605}
16606
7ca2d3a3 16607static enum dwarf_array_dim_ordering
6e70227d 16608read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16609{
16610 struct attribute *attr;
16611
16612 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16613
aead7601
SM
16614 if (attr)
16615 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16616
0963b4bd
MS
16617 /* GNU F77 is a special case, as at 08/2004 array type info is the
16618 opposite order to the dwarf2 specification, but data is still
16619 laid out as per normal fortran.
7ca2d3a3 16620
0963b4bd
MS
16621 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16622 version checking. */
7ca2d3a3 16623
905e0470
PM
16624 if (cu->language == language_fortran
16625 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16626 {
16627 return DW_ORD_row_major;
16628 }
16629
6e70227d 16630 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16631 {
16632 case array_column_major:
16633 return DW_ORD_col_major;
16634 case array_row_major:
16635 default:
16636 return DW_ORD_row_major;
16637 };
16638}
16639
72019c9c 16640/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16641 the DIE's type field. */
72019c9c 16642
f792889a 16643static struct type *
72019c9c
GM
16644read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16645{
7e314c57
JK
16646 struct type *domain_type, *set_type;
16647 struct attribute *attr;
f792889a 16648
7e314c57
JK
16649 domain_type = die_type (die, cu);
16650
16651 /* The die_type call above may have already set the type for this DIE. */
16652 set_type = get_die_type (die, cu);
16653 if (set_type)
16654 return set_type;
16655
16656 set_type = create_set_type (NULL, domain_type);
16657
16658 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16659 if (attr)
16660 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16661
2b4424c3
TT
16662 maybe_set_alignment (cu, die, set_type);
16663
f792889a 16664 return set_die_type (die, set_type, cu);
72019c9c 16665}
7ca2d3a3 16666
0971de02
TT
16667/* A helper for read_common_block that creates a locexpr baton.
16668 SYM is the symbol which we are marking as computed.
16669 COMMON_DIE is the DIE for the common block.
16670 COMMON_LOC is the location expression attribute for the common
16671 block itself.
16672 MEMBER_LOC is the location expression attribute for the particular
16673 member of the common block that we are processing.
16674 CU is the CU from which the above come. */
16675
16676static void
16677mark_common_block_symbol_computed (struct symbol *sym,
16678 struct die_info *common_die,
16679 struct attribute *common_loc,
16680 struct attribute *member_loc,
16681 struct dwarf2_cu *cu)
16682{
518817b3
SM
16683 struct dwarf2_per_objfile *dwarf2_per_objfile
16684 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16685 struct objfile *objfile = dwarf2_per_objfile->objfile;
16686 struct dwarf2_locexpr_baton *baton;
16687 gdb_byte *ptr;
16688 unsigned int cu_off;
16689 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16690 LONGEST offset = 0;
16691
16692 gdb_assert (common_loc && member_loc);
16693 gdb_assert (attr_form_is_block (common_loc));
16694 gdb_assert (attr_form_is_block (member_loc)
16695 || attr_form_is_constant (member_loc));
16696
8d749320 16697 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16698 baton->per_cu = cu->per_cu;
16699 gdb_assert (baton->per_cu);
16700
16701 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16702
16703 if (attr_form_is_constant (member_loc))
16704 {
16705 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16706 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16707 }
16708 else
16709 baton->size += DW_BLOCK (member_loc)->size;
16710
224c3ddb 16711 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16712 baton->data = ptr;
16713
16714 *ptr++ = DW_OP_call4;
9c541725 16715 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16716 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16717 ptr += 4;
16718
16719 if (attr_form_is_constant (member_loc))
16720 {
16721 *ptr++ = DW_OP_addr;
16722 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16723 ptr += cu->header.addr_size;
16724 }
16725 else
16726 {
16727 /* We have to copy the data here, because DW_OP_call4 will only
16728 use a DW_AT_location attribute. */
16729 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16730 ptr += DW_BLOCK (member_loc)->size;
16731 }
16732
16733 *ptr++ = DW_OP_plus;
16734 gdb_assert (ptr - baton->data == baton->size);
16735
0971de02 16736 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16737 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16738}
16739
4357ac6c
TT
16740/* Create appropriate locally-scoped variables for all the
16741 DW_TAG_common_block entries. Also create a struct common_block
16742 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16743 is used to sepate the common blocks name namespace from regular
16744 variable names. */
c906108c
SS
16745
16746static void
e7c27a73 16747read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16748{
0971de02
TT
16749 struct attribute *attr;
16750
16751 attr = dwarf2_attr (die, DW_AT_location, cu);
16752 if (attr)
16753 {
16754 /* Support the .debug_loc offsets. */
16755 if (attr_form_is_block (attr))
16756 {
16757 /* Ok. */
16758 }
16759 else if (attr_form_is_section_offset (attr))
16760 {
16761 dwarf2_complex_location_expr_complaint ();
16762 attr = NULL;
16763 }
16764 else
16765 {
16766 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16767 "common block member");
16768 attr = NULL;
16769 }
16770 }
16771
639d11d3 16772 if (die->child != NULL)
c906108c 16773 {
518817b3 16774 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16775 struct die_info *child_die;
16776 size_t n_entries = 0, size;
16777 struct common_block *common_block;
16778 struct symbol *sym;
74ac6d43 16779
4357ac6c
TT
16780 for (child_die = die->child;
16781 child_die && child_die->tag;
16782 child_die = sibling_die (child_die))
16783 ++n_entries;
16784
16785 size = (sizeof (struct common_block)
16786 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16787 common_block
16788 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16789 size);
4357ac6c
TT
16790 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16791 common_block->n_entries = 0;
16792
16793 for (child_die = die->child;
16794 child_die && child_die->tag;
16795 child_die = sibling_die (child_die))
16796 {
16797 /* Create the symbol in the DW_TAG_common_block block in the current
16798 symbol scope. */
e7c27a73 16799 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16800 if (sym != NULL)
16801 {
16802 struct attribute *member_loc;
16803
16804 common_block->contents[common_block->n_entries++] = sym;
16805
16806 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16807 cu);
16808 if (member_loc)
16809 {
16810 /* GDB has handled this for a long time, but it is
16811 not specified by DWARF. It seems to have been
16812 emitted by gfortran at least as recently as:
16813 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16814 complaint (_("Variable in common block has "
0971de02 16815 "DW_AT_data_member_location "
9d8780f0
SM
16816 "- DIE at %s [in module %s]"),
16817 sect_offset_str (child_die->sect_off),
518817b3 16818 objfile_name (objfile));
0971de02
TT
16819
16820 if (attr_form_is_section_offset (member_loc))
16821 dwarf2_complex_location_expr_complaint ();
16822 else if (attr_form_is_constant (member_loc)
16823 || attr_form_is_block (member_loc))
16824 {
16825 if (attr)
16826 mark_common_block_symbol_computed (sym, die, attr,
16827 member_loc, cu);
16828 }
16829 else
16830 dwarf2_complex_location_expr_complaint ();
16831 }
16832 }
c906108c 16833 }
4357ac6c
TT
16834
16835 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16836 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16837 }
16838}
16839
0114d602 16840/* Create a type for a C++ namespace. */
d9fa45fe 16841
0114d602
DJ
16842static struct type *
16843read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16844{
518817b3 16845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16846 const char *previous_prefix, *name;
9219021c 16847 int is_anonymous;
0114d602
DJ
16848 struct type *type;
16849
16850 /* For extensions, reuse the type of the original namespace. */
16851 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16852 {
16853 struct die_info *ext_die;
16854 struct dwarf2_cu *ext_cu = cu;
9a619af0 16855
0114d602
DJ
16856 ext_die = dwarf2_extension (die, &ext_cu);
16857 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16858
16859 /* EXT_CU may not be the same as CU.
02142a6c 16860 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16861 return set_die_type (die, type, cu);
16862 }
9219021c 16863
e142c38c 16864 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16865
16866 /* Now build the name of the current namespace. */
16867
0114d602
DJ
16868 previous_prefix = determine_prefix (die, cu);
16869 if (previous_prefix[0] != '\0')
16870 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16871 previous_prefix, name, 0, cu);
0114d602
DJ
16872
16873 /* Create the type. */
19f392bc 16874 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16875
60531b24 16876 return set_die_type (die, type, cu);
0114d602
DJ
16877}
16878
22cee43f 16879/* Read a namespace scope. */
0114d602
DJ
16880
16881static void
16882read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16883{
518817b3 16884 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16885 int is_anonymous;
9219021c 16886
5c4e30ca
DC
16887 /* Add a symbol associated to this if we haven't seen the namespace
16888 before. Also, add a using directive if it's an anonymous
16889 namespace. */
9219021c 16890
f2f0e013 16891 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16892 {
16893 struct type *type;
16894
0114d602 16895 type = read_type_die (die, cu);
e7c27a73 16896 new_symbol (die, type, cu);
5c4e30ca 16897
e8e80198 16898 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16899 if (is_anonymous)
0114d602
DJ
16900 {
16901 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16902
eb1e02fd 16903 std::vector<const char *> excludes;
804d2729 16904 add_using_directive (using_directives (cu),
22cee43f 16905 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16906 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16907 }
5c4e30ca 16908 }
9219021c 16909
639d11d3 16910 if (die->child != NULL)
d9fa45fe 16911 {
639d11d3 16912 struct die_info *child_die = die->child;
6e70227d 16913
d9fa45fe
DC
16914 while (child_die && child_die->tag)
16915 {
e7c27a73 16916 process_die (child_die, cu);
d9fa45fe
DC
16917 child_die = sibling_die (child_die);
16918 }
16919 }
38d518c9
EZ
16920}
16921
f55ee35c
JK
16922/* Read a Fortran module as type. This DIE can be only a declaration used for
16923 imported module. Still we need that type as local Fortran "use ... only"
16924 declaration imports depend on the created type in determine_prefix. */
16925
16926static struct type *
16927read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16928{
518817b3 16929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16930 const char *module_name;
f55ee35c
JK
16931 struct type *type;
16932
16933 module_name = dwarf2_name (die, cu);
19f392bc 16934 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16935
f55ee35c
JK
16936 return set_die_type (die, type, cu);
16937}
16938
5d7cb8df
JK
16939/* Read a Fortran module. */
16940
16941static void
16942read_module (struct die_info *die, struct dwarf2_cu *cu)
16943{
16944 struct die_info *child_die = die->child;
530e8392
KB
16945 struct type *type;
16946
16947 type = read_type_die (die, cu);
16948 new_symbol (die, type, cu);
5d7cb8df 16949
5d7cb8df
JK
16950 while (child_die && child_die->tag)
16951 {
16952 process_die (child_die, cu);
16953 child_die = sibling_die (child_die);
16954 }
16955}
16956
38d518c9
EZ
16957/* Return the name of the namespace represented by DIE. Set
16958 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16959 namespace. */
16960
16961static const char *
e142c38c 16962namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16963{
16964 struct die_info *current_die;
16965 const char *name = NULL;
16966
16967 /* Loop through the extensions until we find a name. */
16968
16969 for (current_die = die;
16970 current_die != NULL;
f2f0e013 16971 current_die = dwarf2_extension (die, &cu))
38d518c9 16972 {
96553a0c
DE
16973 /* We don't use dwarf2_name here so that we can detect the absence
16974 of a name -> anonymous namespace. */
7d45c7c3 16975 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16976
38d518c9
EZ
16977 if (name != NULL)
16978 break;
16979 }
16980
16981 /* Is it an anonymous namespace? */
16982
16983 *is_anonymous = (name == NULL);
16984 if (*is_anonymous)
2b1dbab0 16985 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16986
16987 return name;
d9fa45fe
DC
16988}
16989
c906108c
SS
16990/* Extract all information from a DW_TAG_pointer_type DIE and add to
16991 the user defined type vector. */
16992
f792889a 16993static struct type *
e7c27a73 16994read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16995{
518817b3
SM
16996 struct gdbarch *gdbarch
16997 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16998 struct comp_unit_head *cu_header = &cu->header;
c906108c 16999 struct type *type;
8b2dbe47
KB
17000 struct attribute *attr_byte_size;
17001 struct attribute *attr_address_class;
17002 int byte_size, addr_class;
7e314c57
JK
17003 struct type *target_type;
17004
17005 target_type = die_type (die, cu);
c906108c 17006
7e314c57
JK
17007 /* The die_type call above may have already set the type for this DIE. */
17008 type = get_die_type (die, cu);
17009 if (type)
17010 return type;
17011
17012 type = lookup_pointer_type (target_type);
8b2dbe47 17013
e142c38c 17014 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17015 if (attr_byte_size)
17016 byte_size = DW_UNSND (attr_byte_size);
c906108c 17017 else
8b2dbe47
KB
17018 byte_size = cu_header->addr_size;
17019
e142c38c 17020 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17021 if (attr_address_class)
17022 addr_class = DW_UNSND (attr_address_class);
17023 else
17024 addr_class = DW_ADDR_none;
17025
2b4424c3
TT
17026 ULONGEST alignment = get_alignment (cu, die);
17027
17028 /* If the pointer size, alignment, or address class is different
17029 than the default, create a type variant marked as such and set
17030 the length accordingly. */
17031 if (TYPE_LENGTH (type) != byte_size
17032 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17033 && alignment != TYPE_RAW_ALIGN (type))
17034 || addr_class != DW_ADDR_none)
c906108c 17035 {
5e2b427d 17036 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17037 {
17038 int type_flags;
17039
849957d9 17040 type_flags = gdbarch_address_class_type_flags
5e2b427d 17041 (gdbarch, byte_size, addr_class);
876cecd0
TT
17042 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17043 == 0);
8b2dbe47
KB
17044 type = make_type_with_address_space (type, type_flags);
17045 }
17046 else if (TYPE_LENGTH (type) != byte_size)
17047 {
b98664d3 17048 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17049 }
2b4424c3
TT
17050 else if (TYPE_RAW_ALIGN (type) != alignment)
17051 {
b98664d3 17052 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17053 " - DIE at %s [in module %s]"),
17054 sect_offset_str (die->sect_off),
17055 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17056 }
6e70227d 17057 else
9a619af0
MS
17058 {
17059 /* Should we also complain about unhandled address classes? */
17060 }
c906108c 17061 }
8b2dbe47
KB
17062
17063 TYPE_LENGTH (type) = byte_size;
2b4424c3 17064 set_type_align (type, alignment);
f792889a 17065 return set_die_type (die, type, cu);
c906108c
SS
17066}
17067
17068/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17069 the user defined type vector. */
17070
f792889a 17071static struct type *
e7c27a73 17072read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17073{
17074 struct type *type;
17075 struct type *to_type;
17076 struct type *domain;
17077
e7c27a73
DJ
17078 to_type = die_type (die, cu);
17079 domain = die_containing_type (die, cu);
0d5de010 17080
7e314c57
JK
17081 /* The calls above may have already set the type for this DIE. */
17082 type = get_die_type (die, cu);
17083 if (type)
17084 return type;
17085
0d5de010
DJ
17086 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17087 type = lookup_methodptr_type (to_type);
7078baeb
TT
17088 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17089 {
518817b3
SM
17090 struct type *new_type
17091 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17092
17093 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17094 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17095 TYPE_VARARGS (to_type));
17096 type = lookup_methodptr_type (new_type);
17097 }
0d5de010
DJ
17098 else
17099 type = lookup_memberptr_type (to_type, domain);
c906108c 17100
f792889a 17101 return set_die_type (die, type, cu);
c906108c
SS
17102}
17103
4297a3f0 17104/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17105 the user defined type vector. */
17106
f792889a 17107static struct type *
4297a3f0
AV
17108read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17109 enum type_code refcode)
c906108c 17110{
e7c27a73 17111 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17112 struct type *type, *target_type;
c906108c
SS
17113 struct attribute *attr;
17114
4297a3f0
AV
17115 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17116
7e314c57
JK
17117 target_type = die_type (die, cu);
17118
17119 /* The die_type call above may have already set the type for this DIE. */
17120 type = get_die_type (die, cu);
17121 if (type)
17122 return type;
17123
4297a3f0 17124 type = lookup_reference_type (target_type, refcode);
e142c38c 17125 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17126 if (attr)
17127 {
17128 TYPE_LENGTH (type) = DW_UNSND (attr);
17129 }
17130 else
17131 {
107d2387 17132 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17133 }
2b4424c3 17134 maybe_set_alignment (cu, die, type);
f792889a 17135 return set_die_type (die, type, cu);
c906108c
SS
17136}
17137
cf363f18
MW
17138/* Add the given cv-qualifiers to the element type of the array. GCC
17139 outputs DWARF type qualifiers that apply to an array, not the
17140 element type. But GDB relies on the array element type to carry
17141 the cv-qualifiers. This mimics section 6.7.3 of the C99
17142 specification. */
17143
17144static struct type *
17145add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17146 struct type *base_type, int cnst, int voltl)
17147{
17148 struct type *el_type, *inner_array;
17149
17150 base_type = copy_type (base_type);
17151 inner_array = base_type;
17152
17153 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17154 {
17155 TYPE_TARGET_TYPE (inner_array) =
17156 copy_type (TYPE_TARGET_TYPE (inner_array));
17157 inner_array = TYPE_TARGET_TYPE (inner_array);
17158 }
17159
17160 el_type = TYPE_TARGET_TYPE (inner_array);
17161 cnst |= TYPE_CONST (el_type);
17162 voltl |= TYPE_VOLATILE (el_type);
17163 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17164
17165 return set_die_type (die, base_type, cu);
17166}
17167
f792889a 17168static struct type *
e7c27a73 17169read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17170{
f792889a 17171 struct type *base_type, *cv_type;
c906108c 17172
e7c27a73 17173 base_type = die_type (die, cu);
7e314c57
JK
17174
17175 /* The die_type call above may have already set the type for this DIE. */
17176 cv_type = get_die_type (die, cu);
17177 if (cv_type)
17178 return cv_type;
17179
2f608a3a
KW
17180 /* In case the const qualifier is applied to an array type, the element type
17181 is so qualified, not the array type (section 6.7.3 of C99). */
17182 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17183 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17184
f792889a
DJ
17185 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17186 return set_die_type (die, cv_type, cu);
c906108c
SS
17187}
17188
f792889a 17189static struct type *
e7c27a73 17190read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17191{
f792889a 17192 struct type *base_type, *cv_type;
c906108c 17193
e7c27a73 17194 base_type = die_type (die, cu);
7e314c57
JK
17195
17196 /* The die_type call above may have already set the type for this DIE. */
17197 cv_type = get_die_type (die, cu);
17198 if (cv_type)
17199 return cv_type;
17200
cf363f18
MW
17201 /* In case the volatile qualifier is applied to an array type, the
17202 element type is so qualified, not the array type (section 6.7.3
17203 of C99). */
17204 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17205 return add_array_cv_type (die, cu, base_type, 0, 1);
17206
f792889a
DJ
17207 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17208 return set_die_type (die, cv_type, cu);
c906108c
SS
17209}
17210
06d66ee9
TT
17211/* Handle DW_TAG_restrict_type. */
17212
17213static struct type *
17214read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17215{
17216 struct type *base_type, *cv_type;
17217
17218 base_type = die_type (die, cu);
17219
17220 /* The die_type call above may have already set the type for this DIE. */
17221 cv_type = get_die_type (die, cu);
17222 if (cv_type)
17223 return cv_type;
17224
17225 cv_type = make_restrict_type (base_type);
17226 return set_die_type (die, cv_type, cu);
17227}
17228
a2c2acaf
MW
17229/* Handle DW_TAG_atomic_type. */
17230
17231static struct type *
17232read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17233{
17234 struct type *base_type, *cv_type;
17235
17236 base_type = die_type (die, cu);
17237
17238 /* The die_type call above may have already set the type for this DIE. */
17239 cv_type = get_die_type (die, cu);
17240 if (cv_type)
17241 return cv_type;
17242
17243 cv_type = make_atomic_type (base_type);
17244 return set_die_type (die, cv_type, cu);
17245}
17246
c906108c
SS
17247/* Extract all information from a DW_TAG_string_type DIE and add to
17248 the user defined type vector. It isn't really a user defined type,
17249 but it behaves like one, with other DIE's using an AT_user_def_type
17250 attribute to reference it. */
17251
f792889a 17252static struct type *
e7c27a73 17253read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17254{
518817b3 17255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17256 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17257 struct type *type, *range_type, *index_type, *char_type;
17258 struct attribute *attr;
17259 unsigned int length;
17260
e142c38c 17261 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17262 if (attr)
17263 {
17264 length = DW_UNSND (attr);
17265 }
17266 else
17267 {
0963b4bd 17268 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17269 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17270 if (attr)
17271 {
17272 length = DW_UNSND (attr);
17273 }
17274 else
17275 {
17276 length = 1;
17277 }
c906108c 17278 }
6ccb9162 17279
46bf5051 17280 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17281 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17282 char_type = language_string_char_type (cu->language_defn, gdbarch);
17283 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17284
f792889a 17285 return set_die_type (die, type, cu);
c906108c
SS
17286}
17287
4d804846
JB
17288/* Assuming that DIE corresponds to a function, returns nonzero
17289 if the function is prototyped. */
17290
17291static int
17292prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17293{
17294 struct attribute *attr;
17295
17296 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17297 if (attr && (DW_UNSND (attr) != 0))
17298 return 1;
17299
17300 /* The DWARF standard implies that the DW_AT_prototyped attribute
17301 is only meaninful for C, but the concept also extends to other
17302 languages that allow unprototyped functions (Eg: Objective C).
17303 For all other languages, assume that functions are always
17304 prototyped. */
17305 if (cu->language != language_c
17306 && cu->language != language_objc
17307 && cu->language != language_opencl)
17308 return 1;
17309
17310 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17311 prototyped and unprototyped functions; default to prototyped,
17312 since that is more common in modern code (and RealView warns
17313 about unprototyped functions). */
17314 if (producer_is_realview (cu->producer))
17315 return 1;
17316
17317 return 0;
17318}
17319
c906108c
SS
17320/* Handle DIES due to C code like:
17321
17322 struct foo
c5aa993b
JM
17323 {
17324 int (*funcp)(int a, long l);
17325 int b;
17326 };
c906108c 17327
0963b4bd 17328 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17329
f792889a 17330static struct type *
e7c27a73 17331read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17332{
518817b3 17333 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17334 struct type *type; /* Type that this function returns. */
17335 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17336 struct attribute *attr;
17337
e7c27a73 17338 type = die_type (die, cu);
7e314c57
JK
17339
17340 /* The die_type call above may have already set the type for this DIE. */
17341 ftype = get_die_type (die, cu);
17342 if (ftype)
17343 return ftype;
17344
0c8b41f1 17345 ftype = lookup_function_type (type);
c906108c 17346
4d804846 17347 if (prototyped_function_p (die, cu))
a6c727b2 17348 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17349
c055b101
CV
17350 /* Store the calling convention in the type if it's available in
17351 the subroutine die. Otherwise set the calling convention to
17352 the default value DW_CC_normal. */
17353 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17354 if (attr)
17355 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17356 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17357 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17358 else
17359 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17360
743649fd
MW
17361 /* Record whether the function returns normally to its caller or not
17362 if the DWARF producer set that information. */
17363 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17364 if (attr && (DW_UNSND (attr) != 0))
17365 TYPE_NO_RETURN (ftype) = 1;
17366
76c10ea2
GM
17367 /* We need to add the subroutine type to the die immediately so
17368 we don't infinitely recurse when dealing with parameters
0963b4bd 17369 declared as the same subroutine type. */
76c10ea2 17370 set_die_type (die, ftype, cu);
6e70227d 17371
639d11d3 17372 if (die->child != NULL)
c906108c 17373 {
bb5ed363 17374 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17375 struct die_info *child_die;
8072405b 17376 int nparams, iparams;
c906108c
SS
17377
17378 /* Count the number of parameters.
17379 FIXME: GDB currently ignores vararg functions, but knows about
17380 vararg member functions. */
8072405b 17381 nparams = 0;
639d11d3 17382 child_die = die->child;
c906108c
SS
17383 while (child_die && child_die->tag)
17384 {
17385 if (child_die->tag == DW_TAG_formal_parameter)
17386 nparams++;
17387 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17388 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17389 child_die = sibling_die (child_die);
17390 }
17391
17392 /* Allocate storage for parameters and fill them in. */
17393 TYPE_NFIELDS (ftype) = nparams;
17394 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17395 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17396
8072405b
JK
17397 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17398 even if we error out during the parameters reading below. */
17399 for (iparams = 0; iparams < nparams; iparams++)
17400 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17401
17402 iparams = 0;
639d11d3 17403 child_die = die->child;
c906108c
SS
17404 while (child_die && child_die->tag)
17405 {
17406 if (child_die->tag == DW_TAG_formal_parameter)
17407 {
3ce3b1ba
PA
17408 struct type *arg_type;
17409
17410 /* DWARF version 2 has no clean way to discern C++
17411 static and non-static member functions. G++ helps
17412 GDB by marking the first parameter for non-static
17413 member functions (which is the this pointer) as
17414 artificial. We pass this information to
17415 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17416
17417 DWARF version 3 added DW_AT_object_pointer, which GCC
17418 4.5 does not yet generate. */
e142c38c 17419 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17420 if (attr)
17421 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17422 else
9c37b5ae 17423 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17424 arg_type = die_type (child_die, cu);
17425
17426 /* RealView does not mark THIS as const, which the testsuite
17427 expects. GCC marks THIS as const in method definitions,
17428 but not in the class specifications (GCC PR 43053). */
17429 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17430 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17431 {
17432 int is_this = 0;
17433 struct dwarf2_cu *arg_cu = cu;
17434 const char *name = dwarf2_name (child_die, cu);
17435
17436 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17437 if (attr)
17438 {
17439 /* If the compiler emits this, use it. */
17440 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17441 is_this = 1;
17442 }
17443 else if (name && strcmp (name, "this") == 0)
17444 /* Function definitions will have the argument names. */
17445 is_this = 1;
17446 else if (name == NULL && iparams == 0)
17447 /* Declarations may not have the names, so like
17448 elsewhere in GDB, assume an artificial first
17449 argument is "this". */
17450 is_this = 1;
17451
17452 if (is_this)
17453 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17454 arg_type, 0);
17455 }
17456
17457 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17458 iparams++;
17459 }
17460 child_die = sibling_die (child_die);
17461 }
17462 }
17463
76c10ea2 17464 return ftype;
c906108c
SS
17465}
17466
f792889a 17467static struct type *
e7c27a73 17468read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17469{
518817b3 17470 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17471 const char *name = NULL;
3c8e0968 17472 struct type *this_type, *target_type;
c906108c 17473
94af9270 17474 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17475 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17476 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17477 set_die_type (die, this_type, cu);
3c8e0968
DE
17478 target_type = die_type (die, cu);
17479 if (target_type != this_type)
17480 TYPE_TARGET_TYPE (this_type) = target_type;
17481 else
17482 {
17483 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17484 spec and cause infinite loops in GDB. */
b98664d3 17485 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17486 "- DIE at %s [in module %s]"),
17487 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17488 TYPE_TARGET_TYPE (this_type) = NULL;
17489 }
f792889a 17490 return this_type;
c906108c
SS
17491}
17492
9b790ce7
UW
17493/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17494 (which may be different from NAME) to the architecture back-end to allow
17495 it to guess the correct format if necessary. */
17496
17497static struct type *
17498dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17499 const char *name_hint)
17500{
17501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17502 const struct floatformat **format;
17503 struct type *type;
17504
17505 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17506 if (format)
17507 type = init_float_type (objfile, bits, name, format);
17508 else
77b7c781 17509 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17510
17511 return type;
17512}
17513
eb77c9df
AB
17514/* Allocate an integer type of size BITS and name NAME. */
17515
17516static struct type *
17517dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17518 int bits, int unsigned_p, const char *name)
17519{
17520 struct type *type;
17521
17522 /* Versions of Intel's C Compiler generate an integer type called "void"
17523 instead of using DW_TAG_unspecified_type. This has been seen on
17524 at least versions 14, 17, and 18. */
35ee2dc2
AB
17525 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17526 && strcmp (name, "void") == 0)
eb77c9df
AB
17527 type = objfile_type (objfile)->builtin_void;
17528 else
17529 type = init_integer_type (objfile, bits, unsigned_p, name);
17530
17531 return type;
17532}
17533
8bdc1658
AB
17534/* Initialise and return a floating point type of size BITS suitable for
17535 use as a component of a complex number. The NAME_HINT is passed through
17536 when initialising the floating point type and is the name of the complex
17537 type.
17538
17539 As DWARF doesn't currently provide an explicit name for the components
17540 of a complex number, but it can be helpful to have these components
17541 named, we try to select a suitable name based on the size of the
17542 component. */
17543static struct type *
17544dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17545 struct objfile *objfile,
17546 int bits, const char *name_hint)
17547{
17548 gdbarch *gdbarch = get_objfile_arch (objfile);
17549 struct type *tt = nullptr;
17550
35add35e
AB
17551 /* Try to find a suitable floating point builtin type of size BITS.
17552 We're going to use the name of this type as the name for the complex
17553 target type that we are about to create. */
1db455a7 17554 switch (cu->language)
8bdc1658 17555 {
1db455a7
AB
17556 case language_fortran:
17557 switch (bits)
17558 {
17559 case 32:
17560 tt = builtin_f_type (gdbarch)->builtin_real;
17561 break;
17562 case 64:
17563 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17564 break;
17565 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17566 case 128:
17567 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17568 break;
17569 }
8bdc1658 17570 break;
1db455a7
AB
17571 default:
17572 switch (bits)
17573 {
17574 case 32:
17575 tt = builtin_type (gdbarch)->builtin_float;
17576 break;
17577 case 64:
17578 tt = builtin_type (gdbarch)->builtin_double;
17579 break;
17580 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17581 case 128:
17582 tt = builtin_type (gdbarch)->builtin_long_double;
17583 break;
17584 }
8bdc1658
AB
17585 break;
17586 }
17587
35add35e
AB
17588 /* If the type we found doesn't match the size we were looking for, then
17589 pretend we didn't find a type at all, the complex target type we
17590 create will then be nameless. */
a12e5744 17591 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17592 tt = nullptr;
17593
8bdc1658
AB
17594 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17595 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17596}
17597
c906108c
SS
17598/* Find a representation of a given base type and install
17599 it in the TYPE field of the die. */
17600
f792889a 17601static struct type *
e7c27a73 17602read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17603{
518817b3 17604 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17605 struct type *type;
17606 struct attribute *attr;
19f392bc 17607 int encoding = 0, bits = 0;
15d034d0 17608 const char *name;
c906108c 17609
e142c38c 17610 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17611 if (attr)
17612 {
17613 encoding = DW_UNSND (attr);
17614 }
e142c38c 17615 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17616 if (attr)
17617 {
19f392bc 17618 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17619 }
39cbfefa 17620 name = dwarf2_name (die, cu);
6ccb9162 17621 if (!name)
c906108c 17622 {
b98664d3 17623 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17624 }
6ccb9162
UW
17625
17626 switch (encoding)
c906108c 17627 {
6ccb9162
UW
17628 case DW_ATE_address:
17629 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17630 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17631 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17632 break;
17633 case DW_ATE_boolean:
19f392bc 17634 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17635 break;
17636 case DW_ATE_complex_float:
8bdc1658 17637 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17638 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17639 break;
17640 case DW_ATE_decimal_float:
19f392bc 17641 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17642 break;
17643 case DW_ATE_float:
9b790ce7 17644 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17645 break;
17646 case DW_ATE_signed:
eb77c9df 17647 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17648 break;
17649 case DW_ATE_unsigned:
3b2b8fea
TT
17650 if (cu->language == language_fortran
17651 && name
61012eef 17652 && startswith (name, "character("))
19f392bc
UW
17653 type = init_character_type (objfile, bits, 1, name);
17654 else
eb77c9df 17655 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17656 break;
17657 case DW_ATE_signed_char:
6e70227d 17658 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17659 || cu->language == language_pascal
17660 || cu->language == language_fortran)
19f392bc
UW
17661 type = init_character_type (objfile, bits, 0, name);
17662 else
eb77c9df 17663 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17664 break;
17665 case DW_ATE_unsigned_char:
868a0084 17666 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17667 || cu->language == language_pascal
c44af4eb
TT
17668 || cu->language == language_fortran
17669 || cu->language == language_rust)
19f392bc
UW
17670 type = init_character_type (objfile, bits, 1, name);
17671 else
eb77c9df 17672 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17673 break;
75079b2b 17674 case DW_ATE_UTF:
53e710ac
PA
17675 {
17676 gdbarch *arch = get_objfile_arch (objfile);
17677
17678 if (bits == 16)
17679 type = builtin_type (arch)->builtin_char16;
17680 else if (bits == 32)
17681 type = builtin_type (arch)->builtin_char32;
17682 else
17683 {
b98664d3 17684 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17685 bits);
eb77c9df 17686 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17687 }
17688 return set_die_type (die, type, cu);
17689 }
75079b2b
TT
17690 break;
17691
6ccb9162 17692 default:
b98664d3 17693 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17694 dwarf_type_encoding_name (encoding));
77b7c781 17695 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17696 break;
c906108c 17697 }
6ccb9162 17698
0114d602 17699 if (name && strcmp (name, "char") == 0)
876cecd0 17700 TYPE_NOSIGN (type) = 1;
0114d602 17701
2b4424c3
TT
17702 maybe_set_alignment (cu, die, type);
17703
f792889a 17704 return set_die_type (die, type, cu);
c906108c
SS
17705}
17706
80180f79
SA
17707/* Parse dwarf attribute if it's a block, reference or constant and put the
17708 resulting value of the attribute into struct bound_prop.
17709 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17710
17711static int
17712attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17713 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17714{
17715 struct dwarf2_property_baton *baton;
518817b3
SM
17716 struct obstack *obstack
17717 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17718
17719 if (attr == NULL || prop == NULL)
17720 return 0;
17721
17722 if (attr_form_is_block (attr))
17723 {
8d749320 17724 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17725 baton->referenced_type = NULL;
17726 baton->locexpr.per_cu = cu->per_cu;
17727 baton->locexpr.size = DW_BLOCK (attr)->size;
17728 baton->locexpr.data = DW_BLOCK (attr)->data;
17729 prop->data.baton = baton;
17730 prop->kind = PROP_LOCEXPR;
17731 gdb_assert (prop->data.baton != NULL);
17732 }
17733 else if (attr_form_is_ref (attr))
17734 {
17735 struct dwarf2_cu *target_cu = cu;
17736 struct die_info *target_die;
17737 struct attribute *target_attr;
17738
17739 target_die = follow_die_ref (die, attr, &target_cu);
17740 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17741 if (target_attr == NULL)
17742 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17743 target_cu);
80180f79
SA
17744 if (target_attr == NULL)
17745 return 0;
17746
df25ebbd 17747 switch (target_attr->name)
80180f79 17748 {
df25ebbd
JB
17749 case DW_AT_location:
17750 if (attr_form_is_section_offset (target_attr))
17751 {
8d749320 17752 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17753 baton->referenced_type = die_type (target_die, target_cu);
17754 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17755 prop->data.baton = baton;
17756 prop->kind = PROP_LOCLIST;
17757 gdb_assert (prop->data.baton != NULL);
17758 }
17759 else if (attr_form_is_block (target_attr))
17760 {
8d749320 17761 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17762 baton->referenced_type = die_type (target_die, target_cu);
17763 baton->locexpr.per_cu = cu->per_cu;
17764 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17765 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17766 prop->data.baton = baton;
17767 prop->kind = PROP_LOCEXPR;
17768 gdb_assert (prop->data.baton != NULL);
17769 }
17770 else
17771 {
17772 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17773 "dynamic property");
17774 return 0;
17775 }
17776 break;
17777 case DW_AT_data_member_location:
17778 {
17779 LONGEST offset;
17780
17781 if (!handle_data_member_location (target_die, target_cu,
17782 &offset))
17783 return 0;
17784
8d749320 17785 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17786 baton->referenced_type = read_type_die (target_die->parent,
17787 target_cu);
df25ebbd
JB
17788 baton->offset_info.offset = offset;
17789 baton->offset_info.type = die_type (target_die, target_cu);
17790 prop->data.baton = baton;
17791 prop->kind = PROP_ADDR_OFFSET;
17792 break;
17793 }
80180f79
SA
17794 }
17795 }
17796 else if (attr_form_is_constant (attr))
17797 {
17798 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17799 prop->kind = PROP_CONST;
17800 }
17801 else
17802 {
17803 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17804 dwarf2_name (die, cu));
17805 return 0;
17806 }
17807
17808 return 1;
17809}
17810
a02abb62
JB
17811/* Read the given DW_AT_subrange DIE. */
17812
f792889a 17813static struct type *
a02abb62
JB
17814read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17815{
4c9ad8c2 17816 struct type *base_type, *orig_base_type;
a02abb62
JB
17817 struct type *range_type;
17818 struct attribute *attr;
729efb13 17819 struct dynamic_prop low, high;
4fae6e18 17820 int low_default_is_valid;
c451ebe5 17821 int high_bound_is_count = 0;
15d034d0 17822 const char *name;
d359392f 17823 ULONGEST negative_mask;
e77813c8 17824
4c9ad8c2
TT
17825 orig_base_type = die_type (die, cu);
17826 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17827 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17828 creating the range type, but we use the result of check_typedef
17829 when examining properties of the type. */
17830 base_type = check_typedef (orig_base_type);
a02abb62 17831
7e314c57
JK
17832 /* The die_type call above may have already set the type for this DIE. */
17833 range_type = get_die_type (die, cu);
17834 if (range_type)
17835 return range_type;
17836
729efb13
SA
17837 low.kind = PROP_CONST;
17838 high.kind = PROP_CONST;
17839 high.data.const_val = 0;
17840
4fae6e18
JK
17841 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17842 omitting DW_AT_lower_bound. */
17843 switch (cu->language)
6e70227d 17844 {
4fae6e18
JK
17845 case language_c:
17846 case language_cplus:
729efb13 17847 low.data.const_val = 0;
4fae6e18
JK
17848 low_default_is_valid = 1;
17849 break;
17850 case language_fortran:
729efb13 17851 low.data.const_val = 1;
4fae6e18
JK
17852 low_default_is_valid = 1;
17853 break;
17854 case language_d:
4fae6e18 17855 case language_objc:
c44af4eb 17856 case language_rust:
729efb13 17857 low.data.const_val = 0;
4fae6e18
JK
17858 low_default_is_valid = (cu->header.version >= 4);
17859 break;
17860 case language_ada:
17861 case language_m2:
17862 case language_pascal:
729efb13 17863 low.data.const_val = 1;
4fae6e18
JK
17864 low_default_is_valid = (cu->header.version >= 4);
17865 break;
17866 default:
729efb13 17867 low.data.const_val = 0;
4fae6e18
JK
17868 low_default_is_valid = 0;
17869 break;
a02abb62
JB
17870 }
17871
e142c38c 17872 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17873 if (attr)
11c1ba78 17874 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18 17875 else if (!low_default_is_valid)
b98664d3 17876 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17877 "- DIE at %s [in module %s]"),
17878 sect_offset_str (die->sect_off),
518817b3 17879 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17880
506f5c41
TV
17881 struct attribute *attr_ub, *attr_count;
17882 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17883 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8 17884 {
506f5c41 17885 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17886 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17887 {
c451ebe5
SA
17888 /* If bounds are constant do the final calculation here. */
17889 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17890 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17891 else
17892 high_bound_is_count = 1;
c2ff108b 17893 }
506f5c41
TV
17894 else
17895 {
17896 if (attr_ub != NULL)
17897 complaint (_("Unresolved DW_AT_upper_bound "
17898 "- DIE at %s [in module %s]"),
17899 sect_offset_str (die->sect_off),
17900 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17901 if (attr_count != NULL)
17902 complaint (_("Unresolved DW_AT_count "
17903 "- DIE at %s [in module %s]"),
17904 sect_offset_str (die->sect_off),
17905 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17906 }
17907
e77813c8
PM
17908 }
17909
17910 /* Dwarf-2 specifications explicitly allows to create subrange types
17911 without specifying a base type.
17912 In that case, the base type must be set to the type of
17913 the lower bound, upper bound or count, in that order, if any of these
17914 three attributes references an object that has a type.
17915 If no base type is found, the Dwarf-2 specifications say that
17916 a signed integer type of size equal to the size of an address should
17917 be used.
17918 For the following C code: `extern char gdb_int [];'
17919 GCC produces an empty range DIE.
17920 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17921 high bound or count are not yet handled by this code. */
e77813c8
PM
17922 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17923 {
518817b3 17924 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17925 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17926 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17927 struct type *int_type = objfile_type (objfile)->builtin_int;
17928
17929 /* Test "int", "long int", and "long long int" objfile types,
17930 and select the first one having a size above or equal to the
17931 architecture address size. */
17932 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17933 base_type = int_type;
17934 else
17935 {
17936 int_type = objfile_type (objfile)->builtin_long;
17937 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17938 base_type = int_type;
17939 else
17940 {
17941 int_type = objfile_type (objfile)->builtin_long_long;
17942 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17943 base_type = int_type;
17944 }
17945 }
17946 }
a02abb62 17947
dbb9c2b1
JB
17948 /* Normally, the DWARF producers are expected to use a signed
17949 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17950 But this is unfortunately not always the case, as witnessed
17951 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17952 is used instead. To work around that ambiguity, we treat
17953 the bounds as signed, and thus sign-extend their values, when
17954 the base type is signed. */
6e70227d 17955 negative_mask =
d359392f 17956 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17957 if (low.kind == PROP_CONST
17958 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17959 low.data.const_val |= negative_mask;
17960 if (high.kind == PROP_CONST
17961 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17962 high.data.const_val |= negative_mask;
43bbcdc2 17963
729efb13 17964 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17965
c451ebe5
SA
17966 if (high_bound_is_count)
17967 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17968
c2ff108b
JK
17969 /* Ada expects an empty array on no boundary attributes. */
17970 if (attr == NULL && cu->language != language_ada)
729efb13 17971 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17972
39cbfefa
DJ
17973 name = dwarf2_name (die, cu);
17974 if (name)
17975 TYPE_NAME (range_type) = name;
6e70227d 17976
e142c38c 17977 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17978 if (attr)
17979 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17980
2b4424c3
TT
17981 maybe_set_alignment (cu, die, range_type);
17982
7e314c57
JK
17983 set_die_type (die, range_type, cu);
17984
17985 /* set_die_type should be already done. */
b4ba55a1
JB
17986 set_descriptive_type (range_type, die, cu);
17987
7e314c57 17988 return range_type;
a02abb62 17989}
6e70227d 17990
f792889a 17991static struct type *
81a17f79
JB
17992read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17993{
17994 struct type *type;
81a17f79 17995
518817b3
SM
17996 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17997 NULL);
0114d602 17998 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17999
74a2f8ff
JB
18000 /* In Ada, an unspecified type is typically used when the description
18001 of the type is defered to a different unit. When encountering
18002 such a type, we treat it as a stub, and try to resolve it later on,
18003 when needed. */
18004 if (cu->language == language_ada)
18005 TYPE_STUB (type) = 1;
18006
f792889a 18007 return set_die_type (die, type, cu);
81a17f79 18008}
a02abb62 18009
639d11d3
DC
18010/* Read a single die and all its descendents. Set the die's sibling
18011 field to NULL; set other fields in the die correctly, and set all
18012 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18013 location of the info_ptr after reading all of those dies. PARENT
18014 is the parent of the die in question. */
18015
18016static struct die_info *
dee91e82 18017read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18018 const gdb_byte *info_ptr,
18019 const gdb_byte **new_info_ptr,
dee91e82 18020 struct die_info *parent)
639d11d3
DC
18021{
18022 struct die_info *die;
d521ce57 18023 const gdb_byte *cur_ptr;
639d11d3
DC
18024 int has_children;
18025
bf6af496 18026 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18027 if (die == NULL)
18028 {
18029 *new_info_ptr = cur_ptr;
18030 return NULL;
18031 }
93311388 18032 store_in_ref_table (die, reader->cu);
639d11d3
DC
18033
18034 if (has_children)
bf6af496 18035 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18036 else
18037 {
18038 die->child = NULL;
18039 *new_info_ptr = cur_ptr;
18040 }
18041
18042 die->sibling = NULL;
18043 die->parent = parent;
18044 return die;
18045}
18046
18047/* Read a die, all of its descendents, and all of its siblings; set
18048 all of the fields of all of the dies correctly. Arguments are as
18049 in read_die_and_children. */
18050
18051static struct die_info *
bf6af496 18052read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18053 const gdb_byte *info_ptr,
18054 const gdb_byte **new_info_ptr,
bf6af496 18055 struct die_info *parent)
639d11d3
DC
18056{
18057 struct die_info *first_die, *last_sibling;
d521ce57 18058 const gdb_byte *cur_ptr;
639d11d3 18059
c906108c 18060 cur_ptr = info_ptr;
639d11d3
DC
18061 first_die = last_sibling = NULL;
18062
18063 while (1)
c906108c 18064 {
639d11d3 18065 struct die_info *die
dee91e82 18066 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18067
1d325ec1 18068 if (die == NULL)
c906108c 18069 {
639d11d3
DC
18070 *new_info_ptr = cur_ptr;
18071 return first_die;
c906108c 18072 }
1d325ec1
DJ
18073
18074 if (!first_die)
18075 first_die = die;
c906108c 18076 else
1d325ec1
DJ
18077 last_sibling->sibling = die;
18078
18079 last_sibling = die;
c906108c 18080 }
c906108c
SS
18081}
18082
bf6af496
DE
18083/* Read a die, all of its descendents, and all of its siblings; set
18084 all of the fields of all of the dies correctly. Arguments are as
18085 in read_die_and_children.
18086 This the main entry point for reading a DIE and all its children. */
18087
18088static struct die_info *
18089read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18090 const gdb_byte *info_ptr,
18091 const gdb_byte **new_info_ptr,
bf6af496
DE
18092 struct die_info *parent)
18093{
18094 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18095 new_info_ptr, parent);
18096
b4f54984 18097 if (dwarf_die_debug)
bf6af496
DE
18098 {
18099 fprintf_unfiltered (gdb_stdlog,
18100 "Read die from %s@0x%x of %s:\n",
a32a8923 18101 get_section_name (reader->die_section),
bf6af496
DE
18102 (unsigned) (info_ptr - reader->die_section->buffer),
18103 bfd_get_filename (reader->abfd));
b4f54984 18104 dump_die (die, dwarf_die_debug);
bf6af496
DE
18105 }
18106
18107 return die;
18108}
18109
3019eac3
DE
18110/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18111 attributes.
18112 The caller is responsible for filling in the extra attributes
18113 and updating (*DIEP)->num_attrs.
18114 Set DIEP to point to a newly allocated die with its information,
18115 except for its child, sibling, and parent fields.
18116 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18117
d521ce57 18118static const gdb_byte *
3019eac3 18119read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18120 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18121 int *has_children, int num_extra_attrs)
93311388 18122{
b64f50a1 18123 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18124 struct abbrev_info *abbrev;
18125 struct die_info *die;
18126 struct dwarf2_cu *cu = reader->cu;
18127 bfd *abfd = reader->abfd;
18128
9c541725 18129 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18130 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18131 info_ptr += bytes_read;
18132 if (!abbrev_number)
18133 {
18134 *diep = NULL;
18135 *has_children = 0;
18136 return info_ptr;
18137 }
18138
685af9cd 18139 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18140 if (!abbrev)
348e048f
DE
18141 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18142 abbrev_number,
18143 bfd_get_filename (abfd));
18144
3019eac3 18145 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18146 die->sect_off = sect_off;
93311388
DE
18147 die->tag = abbrev->tag;
18148 die->abbrev = abbrev_number;
18149
3019eac3
DE
18150 /* Make the result usable.
18151 The caller needs to update num_attrs after adding the extra
18152 attributes. */
93311388
DE
18153 die->num_attrs = abbrev->num_attrs;
18154
18155 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18156 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18157 info_ptr);
93311388
DE
18158
18159 *diep = die;
18160 *has_children = abbrev->has_children;
18161 return info_ptr;
18162}
18163
3019eac3
DE
18164/* Read a die and all its attributes.
18165 Set DIEP to point to a newly allocated die with its information,
18166 except for its child, sibling, and parent fields.
18167 Set HAS_CHILDREN to tell whether the die has children or not. */
18168
d521ce57 18169static const gdb_byte *
3019eac3 18170read_full_die (const struct die_reader_specs *reader,
d521ce57 18171 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18172 int *has_children)
18173{
d521ce57 18174 const gdb_byte *result;
bf6af496
DE
18175
18176 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18177
b4f54984 18178 if (dwarf_die_debug)
bf6af496
DE
18179 {
18180 fprintf_unfiltered (gdb_stdlog,
18181 "Read die from %s@0x%x of %s:\n",
a32a8923 18182 get_section_name (reader->die_section),
bf6af496
DE
18183 (unsigned) (info_ptr - reader->die_section->buffer),
18184 bfd_get_filename (reader->abfd));
b4f54984 18185 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18186 }
18187
18188 return result;
3019eac3 18189}
433df2d4
DE
18190\f
18191/* Abbreviation tables.
3019eac3 18192
433df2d4 18193 In DWARF version 2, the description of the debugging information is
c906108c
SS
18194 stored in a separate .debug_abbrev section. Before we read any
18195 dies from a section we read in all abbreviations and install them
433df2d4
DE
18196 in a hash table. */
18197
18198/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18199
685af9cd
TT
18200struct abbrev_info *
18201abbrev_table::alloc_abbrev ()
433df2d4
DE
18202{
18203 struct abbrev_info *abbrev;
18204
685af9cd 18205 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18206 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18207
433df2d4
DE
18208 return abbrev;
18209}
18210
18211/* Add an abbreviation to the table. */
c906108c 18212
685af9cd
TT
18213void
18214abbrev_table::add_abbrev (unsigned int abbrev_number,
18215 struct abbrev_info *abbrev)
433df2d4
DE
18216{
18217 unsigned int hash_number;
18218
18219 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18220 abbrev->next = m_abbrevs[hash_number];
18221 m_abbrevs[hash_number] = abbrev;
433df2d4 18222}
dee91e82 18223
433df2d4
DE
18224/* Look up an abbrev in the table.
18225 Returns NULL if the abbrev is not found. */
18226
685af9cd
TT
18227struct abbrev_info *
18228abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18229{
433df2d4
DE
18230 unsigned int hash_number;
18231 struct abbrev_info *abbrev;
18232
18233 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18234 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18235
18236 while (abbrev)
18237 {
18238 if (abbrev->number == abbrev_number)
18239 return abbrev;
18240 abbrev = abbrev->next;
18241 }
18242 return NULL;
18243}
18244
18245/* Read in an abbrev table. */
18246
685af9cd 18247static abbrev_table_up
ed2dc618
SM
18248abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18249 struct dwarf2_section_info *section,
9c541725 18250 sect_offset sect_off)
433df2d4
DE
18251{
18252 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18253 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18254 const gdb_byte *abbrev_ptr;
c906108c
SS
18255 struct abbrev_info *cur_abbrev;
18256 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18257 unsigned int abbrev_form;
f3dd6933
DJ
18258 struct attr_abbrev *cur_attrs;
18259 unsigned int allocated_attrs;
c906108c 18260
685af9cd 18261 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18262
433df2d4 18263 dwarf2_read_section (objfile, section);
9c541725 18264 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18265 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18266 abbrev_ptr += bytes_read;
18267
f3dd6933 18268 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18269 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18270
0963b4bd 18271 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18272 while (abbrev_number)
18273 {
685af9cd 18274 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18275
18276 /* read in abbrev header */
18277 cur_abbrev->number = abbrev_number;
aead7601
SM
18278 cur_abbrev->tag
18279 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18280 abbrev_ptr += bytes_read;
18281 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18282 abbrev_ptr += 1;
18283
18284 /* now read in declarations */
22d2f3ab 18285 for (;;)
c906108c 18286 {
43988095
JK
18287 LONGEST implicit_const;
18288
22d2f3ab
JK
18289 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18290 abbrev_ptr += bytes_read;
18291 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18292 abbrev_ptr += bytes_read;
43988095
JK
18293 if (abbrev_form == DW_FORM_implicit_const)
18294 {
18295 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18296 &bytes_read);
18297 abbrev_ptr += bytes_read;
18298 }
18299 else
18300 {
18301 /* Initialize it due to a false compiler warning. */
18302 implicit_const = -1;
18303 }
22d2f3ab
JK
18304
18305 if (abbrev_name == 0)
18306 break;
18307
f3dd6933 18308 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18309 {
f3dd6933
DJ
18310 allocated_attrs += ATTR_ALLOC_CHUNK;
18311 cur_attrs
224c3ddb 18312 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18313 }
ae038cb0 18314
aead7601
SM
18315 cur_attrs[cur_abbrev->num_attrs].name
18316 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18317 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18318 = (enum dwarf_form) abbrev_form;
43988095 18319 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18320 ++cur_abbrev->num_attrs;
c906108c
SS
18321 }
18322
8d749320
SM
18323 cur_abbrev->attrs =
18324 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18325 cur_abbrev->num_attrs);
f3dd6933
DJ
18326 memcpy (cur_abbrev->attrs, cur_attrs,
18327 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18328
685af9cd 18329 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18330
18331 /* Get next abbreviation.
18332 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18333 always properly terminated with an abbrev number of 0.
18334 Exit loop if we encounter an abbreviation which we have
18335 already read (which means we are about to read the abbreviations
18336 for the next compile unit) or if the end of the abbreviation
18337 table is reached. */
433df2d4 18338 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18339 break;
18340 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18341 abbrev_ptr += bytes_read;
685af9cd 18342 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18343 break;
18344 }
f3dd6933
DJ
18345
18346 xfree (cur_attrs);
433df2d4 18347 return abbrev_table;
c906108c
SS
18348}
18349
72bf9492
DJ
18350/* Returns nonzero if TAG represents a type that we might generate a partial
18351 symbol for. */
18352
18353static int
18354is_type_tag_for_partial (int tag)
18355{
18356 switch (tag)
18357 {
18358#if 0
18359 /* Some types that would be reasonable to generate partial symbols for,
18360 that we don't at present. */
18361 case DW_TAG_array_type:
18362 case DW_TAG_file_type:
18363 case DW_TAG_ptr_to_member_type:
18364 case DW_TAG_set_type:
18365 case DW_TAG_string_type:
18366 case DW_TAG_subroutine_type:
18367#endif
18368 case DW_TAG_base_type:
18369 case DW_TAG_class_type:
680b30c7 18370 case DW_TAG_interface_type:
72bf9492
DJ
18371 case DW_TAG_enumeration_type:
18372 case DW_TAG_structure_type:
18373 case DW_TAG_subrange_type:
18374 case DW_TAG_typedef:
18375 case DW_TAG_union_type:
18376 return 1;
18377 default:
18378 return 0;
18379 }
18380}
18381
18382/* Load all DIEs that are interesting for partial symbols into memory. */
18383
18384static struct partial_die_info *
dee91e82 18385load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18386 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18387{
dee91e82 18388 struct dwarf2_cu *cu = reader->cu;
518817b3 18389 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18390 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18391 unsigned int bytes_read;
5afb4e99 18392 unsigned int load_all = 0;
72bf9492
DJ
18393 int nesting_level = 1;
18394
18395 parent_die = NULL;
18396 last_die = NULL;
18397
7adf1e79
DE
18398 gdb_assert (cu->per_cu != NULL);
18399 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18400 load_all = 1;
18401
72bf9492
DJ
18402 cu->partial_dies
18403 = htab_create_alloc_ex (cu->header.length / 12,
18404 partial_die_hash,
18405 partial_die_eq,
18406 NULL,
18407 &cu->comp_unit_obstack,
18408 hashtab_obstack_allocate,
18409 dummy_obstack_deallocate);
18410
72bf9492
DJ
18411 while (1)
18412 {
685af9cd 18413 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18414
18415 /* A NULL abbrev means the end of a series of children. */
18416 if (abbrev == NULL)
18417 {
18418 if (--nesting_level == 0)
cd9983dd
YQ
18419 return first_die;
18420
72bf9492
DJ
18421 info_ptr += bytes_read;
18422 last_die = parent_die;
18423 parent_die = parent_die->die_parent;
18424 continue;
18425 }
18426
98bfdba5
PA
18427 /* Check for template arguments. We never save these; if
18428 they're seen, we just mark the parent, and go on our way. */
18429 if (parent_die != NULL
18430 && cu->language == language_cplus
18431 && (abbrev->tag == DW_TAG_template_type_param
18432 || abbrev->tag == DW_TAG_template_value_param))
18433 {
18434 parent_die->has_template_arguments = 1;
18435
18436 if (!load_all)
18437 {
18438 /* We don't need a partial DIE for the template argument. */
dee91e82 18439 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18440 continue;
18441 }
18442 }
18443
0d99eb77 18444 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18445 Skip their other children. */
18446 if (!load_all
18447 && cu->language == language_cplus
18448 && parent_die != NULL
18449 && parent_die->tag == DW_TAG_subprogram)
18450 {
dee91e82 18451 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18452 continue;
18453 }
18454
5afb4e99
DJ
18455 /* Check whether this DIE is interesting enough to save. Normally
18456 we would not be interested in members here, but there may be
18457 later variables referencing them via DW_AT_specification (for
18458 static members). */
18459 if (!load_all
18460 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18461 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18462 && abbrev->tag != DW_TAG_enumerator
18463 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18464 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18465 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18466 && abbrev->tag != DW_TAG_variable
5afb4e99 18467 && abbrev->tag != DW_TAG_namespace
f55ee35c 18468 && abbrev->tag != DW_TAG_module
95554aad 18469 && abbrev->tag != DW_TAG_member
74921315
KS
18470 && abbrev->tag != DW_TAG_imported_unit
18471 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18472 {
18473 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18474 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18475 continue;
18476 }
18477
6f06d47b
YQ
18478 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18479 abbrev);
cd9983dd 18480
48fbe735 18481 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18482
18483 /* This two-pass algorithm for processing partial symbols has a
18484 high cost in cache pressure. Thus, handle some simple cases
18485 here which cover the majority of C partial symbols. DIEs
18486 which neither have specification tags in them, nor could have
18487 specification tags elsewhere pointing at them, can simply be
18488 processed and discarded.
18489
18490 This segment is also optional; scan_partial_symbols and
18491 add_partial_symbol will handle these DIEs if we chain
18492 them in normally. When compilers which do not emit large
18493 quantities of duplicate debug information are more common,
18494 this code can probably be removed. */
18495
18496 /* Any complete simple types at the top level (pretty much all
18497 of them, for a language without namespaces), can be processed
18498 directly. */
18499 if (parent_die == NULL
cd9983dd
YQ
18500 && pdi.has_specification == 0
18501 && pdi.is_declaration == 0
18502 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18503 || pdi.tag == DW_TAG_base_type
18504 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18505 {
cd9983dd
YQ
18506 if (building_psymtab && pdi.name != NULL)
18507 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18508 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18509 psymbol_placement::STATIC,
1762568f 18510 0, cu->language, objfile);
cd9983dd 18511 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18512 continue;
18513 }
18514
d8228535
JK
18515 /* The exception for DW_TAG_typedef with has_children above is
18516 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18517 type_name_or_error will error on such types later.
d8228535
JK
18518
18519 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18520 it could not find the child DIEs referenced later, this is checked
18521 above. In correct DWARF DW_TAG_typedef should have no children. */
18522
cd9983dd 18523 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18524 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18525 "- DIE at %s [in module %s]"),
cd9983dd 18526 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18527
72bf9492
DJ
18528 /* If we're at the second level, and we're an enumerator, and
18529 our parent has no specification (meaning possibly lives in a
18530 namespace elsewhere), then we can add the partial symbol now
18531 instead of queueing it. */
cd9983dd 18532 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18533 && parent_die != NULL
18534 && parent_die->die_parent == NULL
18535 && parent_die->tag == DW_TAG_enumeration_type
18536 && parent_die->has_specification == 0)
18537 {
cd9983dd 18538 if (pdi.name == NULL)
b98664d3 18539 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18540 else if (building_psymtab)
cd9983dd 18541 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18542 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18543 cu->language == language_cplus
75aedd27
TT
18544 ? psymbol_placement::GLOBAL
18545 : psymbol_placement::STATIC,
1762568f 18546 0, cu->language, objfile);
72bf9492 18547
cd9983dd 18548 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18549 continue;
18550 }
18551
cd9983dd 18552 struct partial_die_info *part_die
6f06d47b 18553 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18554
72bf9492
DJ
18555 /* We'll save this DIE so link it in. */
18556 part_die->die_parent = parent_die;
18557 part_die->die_sibling = NULL;
18558 part_die->die_child = NULL;
18559
18560 if (last_die && last_die == parent_die)
18561 last_die->die_child = part_die;
18562 else if (last_die)
18563 last_die->die_sibling = part_die;
18564
18565 last_die = part_die;
18566
18567 if (first_die == NULL)
18568 first_die = part_die;
18569
18570 /* Maybe add the DIE to the hash table. Not all DIEs that we
18571 find interesting need to be in the hash table, because we
18572 also have the parent/sibling/child chains; only those that we
18573 might refer to by offset later during partial symbol reading.
18574
18575 For now this means things that might have be the target of a
18576 DW_AT_specification, DW_AT_abstract_origin, or
18577 DW_AT_extension. DW_AT_extension will refer only to
18578 namespaces; DW_AT_abstract_origin refers to functions (and
18579 many things under the function DIE, but we do not recurse
18580 into function DIEs during partial symbol reading) and
18581 possibly variables as well; DW_AT_specification refers to
18582 declarations. Declarations ought to have the DW_AT_declaration
18583 flag. It happens that GCC forgets to put it in sometimes, but
18584 only for functions, not for types.
18585
18586 Adding more things than necessary to the hash table is harmless
18587 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18588 wasted time in find_partial_die, when we reread the compilation
18589 unit with load_all_dies set. */
72bf9492 18590
5afb4e99 18591 if (load_all
72929c62 18592 || abbrev->tag == DW_TAG_constant
5afb4e99 18593 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18594 || abbrev->tag == DW_TAG_variable
18595 || abbrev->tag == DW_TAG_namespace
18596 || part_die->is_declaration)
18597 {
18598 void **slot;
18599
18600 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18601 to_underlying (part_die->sect_off),
18602 INSERT);
72bf9492
DJ
18603 *slot = part_die;
18604 }
18605
72bf9492 18606 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18607 we have no reason to follow the children of structures; for other
98bfdba5
PA
18608 languages we have to, so that we can get at method physnames
18609 to infer fully qualified class names, for DW_AT_specification,
18610 and for C++ template arguments. For C++, we also look one level
18611 inside functions to find template arguments (if the name of the
18612 function does not already contain the template arguments).
bc30ff58
JB
18613
18614 For Ada, we need to scan the children of subprograms and lexical
18615 blocks as well because Ada allows the definition of nested
18616 entities that could be interesting for the debugger, such as
18617 nested subprograms for instance. */
72bf9492 18618 if (last_die->has_children
5afb4e99
DJ
18619 && (load_all
18620 || last_die->tag == DW_TAG_namespace
f55ee35c 18621 || last_die->tag == DW_TAG_module
72bf9492 18622 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18623 || (cu->language == language_cplus
18624 && last_die->tag == DW_TAG_subprogram
18625 && (last_die->name == NULL
18626 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18627 || (cu->language != language_c
18628 && (last_die->tag == DW_TAG_class_type
680b30c7 18629 || last_die->tag == DW_TAG_interface_type
72bf9492 18630 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18631 || last_die->tag == DW_TAG_union_type))
18632 || (cu->language == language_ada
18633 && (last_die->tag == DW_TAG_subprogram
18634 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18635 {
18636 nesting_level++;
18637 parent_die = last_die;
18638 continue;
18639 }
18640
18641 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18642 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18643
18644 /* Back to the top, do it again. */
18645 }
18646}
18647
6f06d47b
YQ
18648partial_die_info::partial_die_info (sect_offset sect_off_,
18649 struct abbrev_info *abbrev)
18650 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18651{
18652}
18653
35cc7ed7
YQ
18654/* Read a minimal amount of information into the minimal die structure.
18655 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18656
48fbe735
YQ
18657const gdb_byte *
18658partial_die_info::read (const struct die_reader_specs *reader,
18659 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18660{
dee91e82 18661 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18662 struct dwarf2_per_objfile *dwarf2_per_objfile
18663 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18664 unsigned int i;
c5aa993b 18665 int has_low_pc_attr = 0;
c906108c 18666 int has_high_pc_attr = 0;
91da1414 18667 int high_pc_relative = 0;
c906108c 18668
fd0a254f 18669 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18670 {
48fbe735
YQ
18671 struct attribute attr;
18672
fd0a254f 18673 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18674
18675 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18676 partial symbol table. */
c906108c
SS
18677 switch (attr.name)
18678 {
18679 case DW_AT_name:
48fbe735 18680 switch (tag)
71c25dea
TT
18681 {
18682 case DW_TAG_compile_unit:
95554aad 18683 case DW_TAG_partial_unit:
348e048f 18684 case DW_TAG_type_unit:
71c25dea
TT
18685 /* Compilation units have a DW_AT_name that is a filename, not
18686 a source language identifier. */
18687 case DW_TAG_enumeration_type:
18688 case DW_TAG_enumerator:
18689 /* These tags always have simple identifiers already; no need
18690 to canonicalize them. */
48fbe735 18691 name = DW_STRING (&attr);
71c25dea
TT
18692 break;
18693 default:
48fbe735
YQ
18694 {
18695 struct objfile *objfile = dwarf2_per_objfile->objfile;
18696
18697 name
18698 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18699 &objfile->per_bfd->storage_obstack);
18700 }
71c25dea
TT
18701 break;
18702 }
c906108c 18703 break;
31ef98ae 18704 case DW_AT_linkage_name:
c906108c 18705 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18706 /* Note that both forms of linkage name might appear. We
18707 assume they will be the same, and we only store the last
18708 one we see. */
94af9270 18709 if (cu->language == language_ada)
48fbe735
YQ
18710 name = DW_STRING (&attr);
18711 linkage_name = DW_STRING (&attr);
c906108c
SS
18712 break;
18713 case DW_AT_low_pc:
18714 has_low_pc_attr = 1;
48fbe735 18715 lowpc = attr_value_as_address (&attr);
c906108c
SS
18716 break;
18717 case DW_AT_high_pc:
18718 has_high_pc_attr = 1;
48fbe735 18719 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18720 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18721 high_pc_relative = 1;
c906108c
SS
18722 break;
18723 case DW_AT_location:
0963b4bd 18724 /* Support the .debug_loc offsets. */
8e19ed76
PS
18725 if (attr_form_is_block (&attr))
18726 {
48fbe735 18727 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18728 }
3690dd37 18729 else if (attr_form_is_section_offset (&attr))
8e19ed76 18730 {
4d3c2250 18731 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18732 }
18733 else
18734 {
4d3c2250
KB
18735 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18736 "partial symbol information");
8e19ed76 18737 }
c906108c 18738 break;
c906108c 18739 case DW_AT_external:
48fbe735 18740 is_external = DW_UNSND (&attr);
c906108c
SS
18741 break;
18742 case DW_AT_declaration:
48fbe735 18743 is_declaration = DW_UNSND (&attr);
c906108c
SS
18744 break;
18745 case DW_AT_type:
48fbe735 18746 has_type = 1;
c906108c
SS
18747 break;
18748 case DW_AT_abstract_origin:
18749 case DW_AT_specification:
72bf9492 18750 case DW_AT_extension:
48fbe735
YQ
18751 has_specification = 1;
18752 spec_offset = dwarf2_get_ref_die_offset (&attr);
18753 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18754 || cu->per_cu->is_dwz);
c906108c
SS
18755 break;
18756 case DW_AT_sibling:
18757 /* Ignore absolute siblings, they might point outside of
18758 the current compile unit. */
18759 if (attr.form == DW_FORM_ref_addr)
b98664d3 18760 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18761 else
b9502d3f 18762 {
48fbe735 18763 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18764 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18765 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18766
18767 if (sibling_ptr < info_ptr)
b98664d3 18768 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18769 else if (sibling_ptr > reader->buffer_end)
18770 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18771 else
48fbe735 18772 sibling = sibling_ptr;
b9502d3f 18773 }
c906108c 18774 break;
fa4028e9 18775 case DW_AT_byte_size:
48fbe735 18776 has_byte_size = 1;
fa4028e9 18777 break;
ff908ebf 18778 case DW_AT_const_value:
48fbe735 18779 has_const_value = 1;
ff908ebf 18780 break;
68511cec
CES
18781 case DW_AT_calling_convention:
18782 /* DWARF doesn't provide a way to identify a program's source-level
18783 entry point. DW_AT_calling_convention attributes are only meant
18784 to describe functions' calling conventions.
18785
18786 However, because it's a necessary piece of information in
0c1b455e
TT
18787 Fortran, and before DWARF 4 DW_CC_program was the only
18788 piece of debugging information whose definition refers to
18789 a 'main program' at all, several compilers marked Fortran
18790 main programs with DW_CC_program --- even when those
18791 functions use the standard calling conventions.
18792
18793 Although DWARF now specifies a way to provide this
18794 information, we support this practice for backward
18795 compatibility. */
68511cec 18796 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18797 && cu->language == language_fortran)
48fbe735 18798 main_subprogram = 1;
68511cec 18799 break;
481860b3
GB
18800 case DW_AT_inline:
18801 if (DW_UNSND (&attr) == DW_INL_inlined
18802 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18803 may_be_inlined = 1;
481860b3 18804 break;
95554aad
TT
18805
18806 case DW_AT_import:
48fbe735 18807 if (tag == DW_TAG_imported_unit)
36586728 18808 {
48fbe735
YQ
18809 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18810 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18811 || cu->per_cu->is_dwz);
18812 }
95554aad
TT
18813 break;
18814
0c1b455e 18815 case DW_AT_main_subprogram:
48fbe735 18816 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18817 break;
18818
05caa1d2
TT
18819 case DW_AT_ranges:
18820 {
18821 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18822 but that requires a full DIE, so instead we just
18823 reimplement it. */
18824 int need_ranges_base = tag != DW_TAG_compile_unit;
18825 unsigned int ranges_offset = (DW_UNSND (&attr)
18826 + (need_ranges_base
18827 ? cu->ranges_base
18828 : 0));
18829
18830 /* Value of the DW_AT_ranges attribute is the offset in the
18831 .debug_ranges section. */
18832 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18833 nullptr))
18834 has_pc_info = 1;
18835 }
18836 break;
18837
c906108c
SS
18838 default:
18839 break;
18840 }
18841 }
18842
91da1414 18843 if (high_pc_relative)
48fbe735 18844 highpc += lowpc;
91da1414 18845
9373cf26
JK
18846 if (has_low_pc_attr && has_high_pc_attr)
18847 {
18848 /* When using the GNU linker, .gnu.linkonce. sections are used to
18849 eliminate duplicate copies of functions and vtables and such.
18850 The linker will arbitrarily choose one and discard the others.
18851 The AT_*_pc values for such functions refer to local labels in
18852 these sections. If the section from that file was discarded, the
18853 labels are not in the output, so the relocs get a value of 0.
18854 If this is a discarded function, mark the pc bounds as invalid,
18855 so that GDB will ignore it. */
48fbe735 18856 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18857 {
48fbe735 18858 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18860
b98664d3 18861 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18862 "for DIE at %s [in module %s]"),
48fbe735
YQ
18863 paddress (gdbarch, lowpc),
18864 sect_offset_str (sect_off),
9d8780f0 18865 objfile_name (objfile));
9373cf26
JK
18866 }
18867 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18868 else if (lowpc >= highpc)
9373cf26 18869 {
48fbe735 18870 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18871 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18872
b98664d3 18873 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18874 "for DIE at %s [in module %s]"),
48fbe735
YQ
18875 paddress (gdbarch, lowpc),
18876 paddress (gdbarch, highpc),
18877 sect_offset_str (sect_off),
9c541725 18878 objfile_name (objfile));
9373cf26
JK
18879 }
18880 else
48fbe735 18881 has_pc_info = 1;
9373cf26 18882 }
85cbf3d3 18883
c906108c
SS
18884 return info_ptr;
18885}
18886
72bf9492
DJ
18887/* Find a cached partial DIE at OFFSET in CU. */
18888
d590ff25
YQ
18889struct partial_die_info *
18890dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18891{
18892 struct partial_die_info *lookup_die = NULL;
6f06d47b 18893 struct partial_die_info part_die (sect_off);
72bf9492 18894
9a3c8263 18895 lookup_die = ((struct partial_die_info *)
d590ff25 18896 htab_find_with_hash (partial_dies, &part_die,
9c541725 18897 to_underlying (sect_off)));
72bf9492 18898
72bf9492
DJ
18899 return lookup_die;
18900}
18901
348e048f
DE
18902/* Find a partial DIE at OFFSET, which may or may not be in CU,
18903 except in the case of .debug_types DIEs which do not reference
18904 outside their CU (they do however referencing other types via
55f1336d 18905 DW_FORM_ref_sig8). */
72bf9492 18906
122cf0f2 18907static const struct cu_partial_die_info
9c541725 18908find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18909{
518817b3
SM
18910 struct dwarf2_per_objfile *dwarf2_per_objfile
18911 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18912 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18913 struct dwarf2_per_cu_data *per_cu = NULL;
18914 struct partial_die_info *pd = NULL;
72bf9492 18915
36586728 18916 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18917 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18918 {
d590ff25 18919 pd = cu->find_partial_die (sect_off);
5afb4e99 18920 if (pd != NULL)
fb816e8b 18921 return { cu, pd };
0d99eb77
DE
18922 /* We missed recording what we needed.
18923 Load all dies and try again. */
18924 per_cu = cu->per_cu;
5afb4e99 18925 }
0d99eb77
DE
18926 else
18927 {
18928 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18929 if (cu->per_cu->is_debug_types)
0d99eb77 18930 {
9d8780f0
SM
18931 error (_("Dwarf Error: Type Unit at offset %s contains"
18932 " external reference to offset %s [in module %s].\n"),
18933 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18934 bfd_get_filename (objfile->obfd));
18935 }
9c541725 18936 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18937 dwarf2_per_objfile);
72bf9492 18938
0d99eb77
DE
18939 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18940 load_partial_comp_unit (per_cu);
ae038cb0 18941
0d99eb77 18942 per_cu->cu->last_used = 0;
d590ff25 18943 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18944 }
5afb4e99 18945
dee91e82
DE
18946 /* If we didn't find it, and not all dies have been loaded,
18947 load them all and try again. */
18948
5afb4e99
DJ
18949 if (pd == NULL && per_cu->load_all_dies == 0)
18950 {
5afb4e99 18951 per_cu->load_all_dies = 1;
fd820528
DE
18952
18953 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18954 THIS_CU->cu may already be in use. So we can't just free it and
18955 replace its DIEs with the ones we read in. Instead, we leave those
18956 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18957 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18958 set. */
dee91e82 18959 load_partial_comp_unit (per_cu);
5afb4e99 18960
d590ff25 18961 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18962 }
18963
18964 if (pd == NULL)
18965 internal_error (__FILE__, __LINE__,
9d8780f0 18966 _("could not find partial DIE %s "
3e43a32a 18967 "in cache [from module %s]\n"),
9d8780f0 18968 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18969 return { per_cu->cu, pd };
72bf9492
DJ
18970}
18971
abc72ce4
DE
18972/* See if we can figure out if the class lives in a namespace. We do
18973 this by looking for a member function; its demangled name will
18974 contain namespace info, if there is any. */
18975
18976static void
18977guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18978 struct dwarf2_cu *cu)
18979{
18980 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18981 what template types look like, because the demangler
18982 frequently doesn't give the same name as the debug info. We
18983 could fix this by only using the demangled name to get the
18984 prefix (but see comment in read_structure_type). */
18985
18986 struct partial_die_info *real_pdi;
18987 struct partial_die_info *child_pdi;
18988
18989 /* If this DIE (this DIE's specification, if any) has a parent, then
18990 we should not do this. We'll prepend the parent's fully qualified
18991 name when we create the partial symbol. */
18992
18993 real_pdi = struct_pdi;
18994 while (real_pdi->has_specification)
fb816e8b 18995 {
122cf0f2
AB
18996 auto res = find_partial_die (real_pdi->spec_offset,
18997 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18998 real_pdi = res.pdi;
18999 cu = res.cu;
19000 }
abc72ce4
DE
19001
19002 if (real_pdi->die_parent != NULL)
19003 return;
19004
19005 for (child_pdi = struct_pdi->die_child;
19006 child_pdi != NULL;
19007 child_pdi = child_pdi->die_sibling)
19008 {
19009 if (child_pdi->tag == DW_TAG_subprogram
19010 && child_pdi->linkage_name != NULL)
19011 {
19012 char *actual_class_name
19013 = language_class_name_from_physname (cu->language_defn,
19014 child_pdi->linkage_name);
19015 if (actual_class_name != NULL)
19016 {
518817b3 19017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19018 struct_pdi->name
224c3ddb 19019 = ((const char *)
e3b94546 19020 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
19021 actual_class_name,
19022 strlen (actual_class_name)));
abc72ce4
DE
19023 xfree (actual_class_name);
19024 }
19025 break;
19026 }
19027 }
19028}
19029
52356b79
YQ
19030void
19031partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19032{
abc72ce4
DE
19033 /* Once we've fixed up a die, there's no point in doing so again.
19034 This also avoids a memory leak if we were to call
19035 guess_partial_die_structure_name multiple times. */
52356b79 19036 if (fixup_called)
abc72ce4
DE
19037 return;
19038
72bf9492
DJ
19039 /* If we found a reference attribute and the DIE has no name, try
19040 to find a name in the referred to DIE. */
19041
52356b79 19042 if (name == NULL && has_specification)
72bf9492
DJ
19043 {
19044 struct partial_die_info *spec_die;
72bf9492 19045
122cf0f2 19046 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19047 spec_die = res.pdi;
19048 cu = res.cu;
72bf9492 19049
52356b79 19050 spec_die->fixup (cu);
72bf9492
DJ
19051
19052 if (spec_die->name)
19053 {
52356b79 19054 name = spec_die->name;
72bf9492
DJ
19055
19056 /* Copy DW_AT_external attribute if it is set. */
19057 if (spec_die->is_external)
52356b79 19058 is_external = spec_die->is_external;
72bf9492
DJ
19059 }
19060 }
19061
19062 /* Set default names for some unnamed DIEs. */
72bf9492 19063
52356b79
YQ
19064 if (name == NULL && tag == DW_TAG_namespace)
19065 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19066
abc72ce4
DE
19067 /* If there is no parent die to provide a namespace, and there are
19068 children, see if we can determine the namespace from their linkage
122d1940 19069 name. */
abc72ce4 19070 if (cu->language == language_cplus
518817b3
SM
19071 && !VEC_empty (dwarf2_section_info_def,
19072 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
19073 && die_parent == NULL
19074 && has_children
19075 && (tag == DW_TAG_class_type
19076 || tag == DW_TAG_structure_type
19077 || tag == DW_TAG_union_type))
19078 guess_partial_die_structure_name (this, cu);
abc72ce4 19079
53832f31
TT
19080 /* GCC might emit a nameless struct or union that has a linkage
19081 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19082 if (name == NULL
19083 && (tag == DW_TAG_class_type
19084 || tag == DW_TAG_interface_type
19085 || tag == DW_TAG_structure_type
19086 || tag == DW_TAG_union_type)
19087 && linkage_name != NULL)
53832f31
TT
19088 {
19089 char *demangled;
19090
52356b79 19091 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19092 if (demangled)
19093 {
96408a79
SA
19094 const char *base;
19095
19096 /* Strip any leading namespaces/classes, keep only the base name.
19097 DW_AT_name for named DIEs does not contain the prefixes. */
19098 base = strrchr (demangled, ':');
19099 if (base && base > demangled && base[-1] == ':')
19100 base++;
19101 else
19102 base = demangled;
19103
518817b3 19104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 19105 name
224c3ddb 19106 = ((const char *)
e3b94546 19107 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 19108 base, strlen (base)));
53832f31
TT
19109 xfree (demangled);
19110 }
19111 }
19112
52356b79 19113 fixup_called = 1;
72bf9492
DJ
19114}
19115
a8329558 19116/* Read an attribute value described by an attribute form. */
c906108c 19117
d521ce57 19118static const gdb_byte *
dee91e82
DE
19119read_attribute_value (const struct die_reader_specs *reader,
19120 struct attribute *attr, unsigned form,
43988095 19121 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19122{
dee91e82 19123 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19124 struct dwarf2_per_objfile *dwarf2_per_objfile
19125 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19126 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19127 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19128 bfd *abfd = reader->abfd;
e7c27a73 19129 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19130 unsigned int bytes_read;
19131 struct dwarf_block *blk;
19132
aead7601 19133 attr->form = (enum dwarf_form) form;
a8329558 19134 switch (form)
c906108c 19135 {
c906108c 19136 case DW_FORM_ref_addr:
ae411497 19137 if (cu->header.version == 2)
4568ecf9 19138 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19139 else
4568ecf9
DE
19140 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19141 &cu->header, &bytes_read);
ae411497
TT
19142 info_ptr += bytes_read;
19143 break;
36586728
TT
19144 case DW_FORM_GNU_ref_alt:
19145 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19146 info_ptr += bytes_read;
19147 break;
ae411497 19148 case DW_FORM_addr:
e7c27a73 19149 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19150 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19151 info_ptr += bytes_read;
c906108c
SS
19152 break;
19153 case DW_FORM_block2:
7b5a2f43 19154 blk = dwarf_alloc_block (cu);
c906108c
SS
19155 blk->size = read_2_bytes (abfd, info_ptr);
19156 info_ptr += 2;
19157 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19158 info_ptr += blk->size;
19159 DW_BLOCK (attr) = blk;
19160 break;
19161 case DW_FORM_block4:
7b5a2f43 19162 blk = dwarf_alloc_block (cu);
c906108c
SS
19163 blk->size = read_4_bytes (abfd, info_ptr);
19164 info_ptr += 4;
19165 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19166 info_ptr += blk->size;
19167 DW_BLOCK (attr) = blk;
19168 break;
19169 case DW_FORM_data2:
19170 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19171 info_ptr += 2;
19172 break;
19173 case DW_FORM_data4:
19174 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19175 info_ptr += 4;
19176 break;
19177 case DW_FORM_data8:
19178 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19179 info_ptr += 8;
19180 break;
0224619f
JK
19181 case DW_FORM_data16:
19182 blk = dwarf_alloc_block (cu);
19183 blk->size = 16;
19184 blk->data = read_n_bytes (abfd, info_ptr, 16);
19185 info_ptr += 16;
19186 DW_BLOCK (attr) = blk;
19187 break;
2dc7f7b3
TT
19188 case DW_FORM_sec_offset:
19189 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19190 info_ptr += bytes_read;
19191 break;
c906108c 19192 case DW_FORM_string:
9b1c24c8 19193 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19194 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19195 info_ptr += bytes_read;
19196 break;
4bdf3d34 19197 case DW_FORM_strp:
36586728
TT
19198 if (!cu->per_cu->is_dwz)
19199 {
ed2dc618
SM
19200 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19201 abfd, info_ptr, cu_header,
36586728
TT
19202 &bytes_read);
19203 DW_STRING_IS_CANONICAL (attr) = 0;
19204 info_ptr += bytes_read;
19205 break;
19206 }
19207 /* FALLTHROUGH */
43988095
JK
19208 case DW_FORM_line_strp:
19209 if (!cu->per_cu->is_dwz)
19210 {
ed2dc618
SM
19211 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19212 abfd, info_ptr,
43988095
JK
19213 cu_header, &bytes_read);
19214 DW_STRING_IS_CANONICAL (attr) = 0;
19215 info_ptr += bytes_read;
19216 break;
19217 }
19218 /* FALLTHROUGH */
36586728
TT
19219 case DW_FORM_GNU_strp_alt:
19220 {
ed2dc618 19221 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19222 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19223 &bytes_read);
19224
ed2dc618
SM
19225 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19226 dwz, str_offset);
36586728
TT
19227 DW_STRING_IS_CANONICAL (attr) = 0;
19228 info_ptr += bytes_read;
19229 }
4bdf3d34 19230 break;
2dc7f7b3 19231 case DW_FORM_exprloc:
c906108c 19232 case DW_FORM_block:
7b5a2f43 19233 blk = dwarf_alloc_block (cu);
c906108c
SS
19234 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19235 info_ptr += bytes_read;
19236 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19237 info_ptr += blk->size;
19238 DW_BLOCK (attr) = blk;
19239 break;
19240 case DW_FORM_block1:
7b5a2f43 19241 blk = dwarf_alloc_block (cu);
c906108c
SS
19242 blk->size = read_1_byte (abfd, info_ptr);
19243 info_ptr += 1;
19244 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19245 info_ptr += blk->size;
19246 DW_BLOCK (attr) = blk;
19247 break;
19248 case DW_FORM_data1:
19249 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19250 info_ptr += 1;
19251 break;
19252 case DW_FORM_flag:
19253 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19254 info_ptr += 1;
19255 break;
2dc7f7b3
TT
19256 case DW_FORM_flag_present:
19257 DW_UNSND (attr) = 1;
19258 break;
c906108c
SS
19259 case DW_FORM_sdata:
19260 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19261 info_ptr += bytes_read;
19262 break;
19263 case DW_FORM_udata:
19264 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19265 info_ptr += bytes_read;
19266 break;
19267 case DW_FORM_ref1:
9c541725 19268 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19269 + read_1_byte (abfd, info_ptr));
c906108c
SS
19270 info_ptr += 1;
19271 break;
19272 case DW_FORM_ref2:
9c541725 19273 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19274 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19275 info_ptr += 2;
19276 break;
19277 case DW_FORM_ref4:
9c541725 19278 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19279 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19280 info_ptr += 4;
19281 break;
613e1657 19282 case DW_FORM_ref8:
9c541725 19283 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19284 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19285 info_ptr += 8;
19286 break;
55f1336d 19287 case DW_FORM_ref_sig8:
ac9ec31b 19288 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19289 info_ptr += 8;
19290 break;
c906108c 19291 case DW_FORM_ref_udata:
9c541725 19292 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19293 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19294 info_ptr += bytes_read;
19295 break;
c906108c 19296 case DW_FORM_indirect:
a8329558
KW
19297 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
43988095
JK
19299 if (form == DW_FORM_implicit_const)
19300 {
19301 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19302 info_ptr += bytes_read;
19303 }
19304 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19305 info_ptr);
19306 break;
19307 case DW_FORM_implicit_const:
19308 DW_SND (attr) = implicit_const;
a8329558 19309 break;
336d760d 19310 case DW_FORM_addrx:
3019eac3
DE
19311 case DW_FORM_GNU_addr_index:
19312 if (reader->dwo_file == NULL)
19313 {
19314 /* For now flag a hard error.
19315 Later we can turn this into a complaint. */
19316 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19317 dwarf_form_name (form),
19318 bfd_get_filename (abfd));
19319 }
19320 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19321 info_ptr += bytes_read;
19322 break;
cf532bd1 19323 case DW_FORM_strx:
15f18d14
AT
19324 case DW_FORM_strx1:
19325 case DW_FORM_strx2:
19326 case DW_FORM_strx3:
19327 case DW_FORM_strx4:
3019eac3
DE
19328 case DW_FORM_GNU_str_index:
19329 if (reader->dwo_file == NULL)
19330 {
19331 /* For now flag a hard error.
19332 Later we can turn this into a complaint if warranted. */
19333 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19334 dwarf_form_name (form),
19335 bfd_get_filename (abfd));
19336 }
19337 {
15f18d14
AT
19338 ULONGEST str_index;
19339 if (form == DW_FORM_strx1)
19340 {
19341 str_index = read_1_byte (abfd, info_ptr);
19342 info_ptr += 1;
19343 }
19344 else if (form == DW_FORM_strx2)
19345 {
19346 str_index = read_2_bytes (abfd, info_ptr);
19347 info_ptr += 2;
19348 }
19349 else if (form == DW_FORM_strx3)
19350 {
19351 str_index = read_3_bytes (abfd, info_ptr);
19352 info_ptr += 3;
19353 }
19354 else if (form == DW_FORM_strx4)
19355 {
19356 str_index = read_4_bytes (abfd, info_ptr);
19357 info_ptr += 4;
19358 }
19359 else
19360 {
19361 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19362 info_ptr += bytes_read;
19363 }
342587c4 19364 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19365 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19366 }
19367 break;
c906108c 19368 default:
8a3fe4f8 19369 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19370 dwarf_form_name (form),
19371 bfd_get_filename (abfd));
c906108c 19372 }
28e94949 19373
36586728 19374 /* Super hack. */
7771576e 19375 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19376 attr->form = DW_FORM_GNU_ref_alt;
19377
28e94949
JB
19378 /* We have seen instances where the compiler tried to emit a byte
19379 size attribute of -1 which ended up being encoded as an unsigned
19380 0xffffffff. Although 0xffffffff is technically a valid size value,
19381 an object of this size seems pretty unlikely so we can relatively
19382 safely treat these cases as if the size attribute was invalid and
19383 treat them as zero by default. */
19384 if (attr->name == DW_AT_byte_size
19385 && form == DW_FORM_data4
19386 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19387 {
19388 complaint
b98664d3 19389 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19390 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19391 DW_UNSND (attr) = 0;
19392 }
28e94949 19393
c906108c
SS
19394 return info_ptr;
19395}
19396
a8329558
KW
19397/* Read an attribute described by an abbreviated attribute. */
19398
d521ce57 19399static const gdb_byte *
dee91e82
DE
19400read_attribute (const struct die_reader_specs *reader,
19401 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19402 const gdb_byte *info_ptr)
a8329558
KW
19403{
19404 attr->name = abbrev->name;
43988095
JK
19405 return read_attribute_value (reader, attr, abbrev->form,
19406 abbrev->implicit_const, info_ptr);
a8329558
KW
19407}
19408
0963b4bd 19409/* Read dwarf information from a buffer. */
c906108c
SS
19410
19411static unsigned int
a1855c1d 19412read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19413{
fe1b8b76 19414 return bfd_get_8 (abfd, buf);
c906108c
SS
19415}
19416
19417static int
a1855c1d 19418read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19419{
fe1b8b76 19420 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19421}
19422
19423static unsigned int
a1855c1d 19424read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19425{
fe1b8b76 19426 return bfd_get_16 (abfd, buf);
c906108c
SS
19427}
19428
21ae7a4d 19429static int
a1855c1d 19430read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19431{
19432 return bfd_get_signed_16 (abfd, buf);
19433}
19434
15f18d14
AT
19435static unsigned int
19436read_3_bytes (bfd *abfd, const gdb_byte *buf)
19437{
19438 unsigned int result = 0;
19439 for (int i = 0; i < 3; ++i)
19440 {
19441 unsigned char byte = bfd_get_8 (abfd, buf);
19442 buf++;
19443 result |= ((unsigned int) byte << (i * 8));
19444 }
19445 return result;
19446}
19447
c906108c 19448static unsigned int
a1855c1d 19449read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19450{
fe1b8b76 19451 return bfd_get_32 (abfd, buf);
c906108c
SS
19452}
19453
21ae7a4d 19454static int
a1855c1d 19455read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19456{
19457 return bfd_get_signed_32 (abfd, buf);
19458}
19459
93311388 19460static ULONGEST
a1855c1d 19461read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19462{
fe1b8b76 19463 return bfd_get_64 (abfd, buf);
c906108c
SS
19464}
19465
19466static CORE_ADDR
d521ce57 19467read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19468 unsigned int *bytes_read)
c906108c 19469{
e7c27a73 19470 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19471 CORE_ADDR retval = 0;
19472
107d2387 19473 if (cu_header->signed_addr_p)
c906108c 19474 {
107d2387
AC
19475 switch (cu_header->addr_size)
19476 {
19477 case 2:
fe1b8b76 19478 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19479 break;
19480 case 4:
fe1b8b76 19481 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19482 break;
19483 case 8:
fe1b8b76 19484 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19485 break;
19486 default:
8e65ff28 19487 internal_error (__FILE__, __LINE__,
e2e0b3e5 19488 _("read_address: bad switch, signed [in module %s]"),
659b0389 19489 bfd_get_filename (abfd));
107d2387
AC
19490 }
19491 }
19492 else
19493 {
19494 switch (cu_header->addr_size)
19495 {
19496 case 2:
fe1b8b76 19497 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19498 break;
19499 case 4:
fe1b8b76 19500 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19501 break;
19502 case 8:
fe1b8b76 19503 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19504 break;
19505 default:
8e65ff28 19506 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19507 _("read_address: bad switch, "
19508 "unsigned [in module %s]"),
659b0389 19509 bfd_get_filename (abfd));
107d2387 19510 }
c906108c 19511 }
64367e0a 19512
107d2387
AC
19513 *bytes_read = cu_header->addr_size;
19514 return retval;
c906108c
SS
19515}
19516
f7ef9339 19517/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19518 specification allows the initial length to take up either 4 bytes
19519 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19520 bytes describe the length and all offsets will be 8 bytes in length
19521 instead of 4.
19522
f7ef9339
KB
19523 An older, non-standard 64-bit format is also handled by this
19524 function. The older format in question stores the initial length
19525 as an 8-byte quantity without an escape value. Lengths greater
19526 than 2^32 aren't very common which means that the initial 4 bytes
19527 is almost always zero. Since a length value of zero doesn't make
19528 sense for the 32-bit format, this initial zero can be considered to
19529 be an escape value which indicates the presence of the older 64-bit
19530 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19531 greater than 4GB. If it becomes necessary to handle lengths
19532 somewhat larger than 4GB, we could allow other small values (such
19533 as the non-sensical values of 1, 2, and 3) to also be used as
19534 escape values indicating the presence of the old format.
f7ef9339 19535
917c78fc
MK
19536 The value returned via bytes_read should be used to increment the
19537 relevant pointer after calling read_initial_length().
c764a876 19538
613e1657
KB
19539 [ Note: read_initial_length() and read_offset() are based on the
19540 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19541 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19542 from:
19543
f7ef9339 19544 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19545
613e1657
KB
19546 This document is only a draft and is subject to change. (So beware.)
19547
f7ef9339 19548 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19549 determined empirically by examining 64-bit ELF files produced by
19550 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19551
19552 - Kevin, July 16, 2002
613e1657
KB
19553 ] */
19554
19555static LONGEST
d521ce57 19556read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19557{
fe1b8b76 19558 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19559
dd373385 19560 if (length == 0xffffffff)
613e1657 19561 {
fe1b8b76 19562 length = bfd_get_64 (abfd, buf + 4);
613e1657 19563 *bytes_read = 12;
613e1657 19564 }
dd373385 19565 else if (length == 0)
f7ef9339 19566 {
dd373385 19567 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19568 length = bfd_get_64 (abfd, buf);
f7ef9339 19569 *bytes_read = 8;
f7ef9339 19570 }
613e1657
KB
19571 else
19572 {
19573 *bytes_read = 4;
613e1657
KB
19574 }
19575
c764a876
DE
19576 return length;
19577}
dd373385 19578
c764a876
DE
19579/* Cover function for read_initial_length.
19580 Returns the length of the object at BUF, and stores the size of the
19581 initial length in *BYTES_READ and stores the size that offsets will be in
19582 *OFFSET_SIZE.
19583 If the initial length size is not equivalent to that specified in
19584 CU_HEADER then issue a complaint.
19585 This is useful when reading non-comp-unit headers. */
dd373385 19586
c764a876 19587static LONGEST
d521ce57 19588read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19589 const struct comp_unit_head *cu_header,
19590 unsigned int *bytes_read,
19591 unsigned int *offset_size)
19592{
19593 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19594
19595 gdb_assert (cu_header->initial_length_size == 4
19596 || cu_header->initial_length_size == 8
19597 || cu_header->initial_length_size == 12);
19598
19599 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19600 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19601
c764a876 19602 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19603 return length;
613e1657
KB
19604}
19605
19606/* Read an offset from the data stream. The size of the offset is
917c78fc 19607 given by cu_header->offset_size. */
613e1657
KB
19608
19609static LONGEST
d521ce57
TT
19610read_offset (bfd *abfd, const gdb_byte *buf,
19611 const struct comp_unit_head *cu_header,
891d2f0b 19612 unsigned int *bytes_read)
c764a876
DE
19613{
19614 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19615
c764a876
DE
19616 *bytes_read = cu_header->offset_size;
19617 return offset;
19618}
19619
19620/* Read an offset from the data stream. */
19621
19622static LONGEST
d521ce57 19623read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19624{
19625 LONGEST retval = 0;
19626
c764a876 19627 switch (offset_size)
613e1657
KB
19628 {
19629 case 4:
fe1b8b76 19630 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19631 break;
19632 case 8:
fe1b8b76 19633 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19634 break;
19635 default:
8e65ff28 19636 internal_error (__FILE__, __LINE__,
c764a876 19637 _("read_offset_1: bad switch [in module %s]"),
659b0389 19638 bfd_get_filename (abfd));
613e1657
KB
19639 }
19640
917c78fc 19641 return retval;
613e1657
KB
19642}
19643
d521ce57
TT
19644static const gdb_byte *
19645read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19646{
19647 /* If the size of a host char is 8 bits, we can return a pointer
19648 to the buffer, otherwise we have to copy the data to a buffer
19649 allocated on the temporary obstack. */
4bdf3d34 19650 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19651 return buf;
c906108c
SS
19652}
19653
d521ce57
TT
19654static const char *
19655read_direct_string (bfd *abfd, const gdb_byte *buf,
19656 unsigned int *bytes_read_ptr)
c906108c
SS
19657{
19658 /* If the size of a host char is 8 bits, we can return a pointer
19659 to the string, otherwise we have to copy the string to a buffer
19660 allocated on the temporary obstack. */
4bdf3d34 19661 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19662 if (*buf == '\0')
19663 {
19664 *bytes_read_ptr = 1;
19665 return NULL;
19666 }
d521ce57
TT
19667 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19668 return (const char *) buf;
4bdf3d34
JJ
19669}
19670
43988095
JK
19671/* Return pointer to string at section SECT offset STR_OFFSET with error
19672 reporting strings FORM_NAME and SECT_NAME. */
19673
d521ce57 19674static const char *
ed2dc618
SM
19675read_indirect_string_at_offset_from (struct objfile *objfile,
19676 bfd *abfd, LONGEST str_offset,
43988095
JK
19677 struct dwarf2_section_info *sect,
19678 const char *form_name,
19679 const char *sect_name)
19680{
ed2dc618 19681 dwarf2_read_section (objfile, sect);
43988095
JK
19682 if (sect->buffer == NULL)
19683 error (_("%s used without %s section [in module %s]"),
19684 form_name, sect_name, bfd_get_filename (abfd));
19685 if (str_offset >= sect->size)
19686 error (_("%s pointing outside of %s section [in module %s]"),
19687 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19688 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19689 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19690 return NULL;
43988095
JK
19691 return (const char *) (sect->buffer + str_offset);
19692}
19693
19694/* Return pointer to string at .debug_str offset STR_OFFSET. */
19695
19696static const char *
ed2dc618
SM
19697read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19698 bfd *abfd, LONGEST str_offset)
43988095 19699{
ed2dc618
SM
19700 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19701 abfd, str_offset,
43988095
JK
19702 &dwarf2_per_objfile->str,
19703 "DW_FORM_strp", ".debug_str");
19704}
19705
19706/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19707
19708static const char *
ed2dc618
SM
19709read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19710 bfd *abfd, LONGEST str_offset)
43988095 19711{
ed2dc618
SM
19712 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19713 abfd, str_offset,
43988095
JK
19714 &dwarf2_per_objfile->line_str,
19715 "DW_FORM_line_strp",
19716 ".debug_line_str");
c906108c
SS
19717}
19718
36586728
TT
19719/* Read a string at offset STR_OFFSET in the .debug_str section from
19720 the .dwz file DWZ. Throw an error if the offset is too large. If
19721 the string consists of a single NUL byte, return NULL; otherwise
19722 return a pointer to the string. */
19723
d521ce57 19724static const char *
ed2dc618
SM
19725read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19726 LONGEST str_offset)
36586728 19727{
ed2dc618 19728 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19729
19730 if (dwz->str.buffer == NULL)
19731 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19732 "section [in module %s]"),
19733 bfd_get_filename (dwz->dwz_bfd));
19734 if (str_offset >= dwz->str.size)
19735 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19736 ".debug_str section [in module %s]"),
19737 bfd_get_filename (dwz->dwz_bfd));
19738 gdb_assert (HOST_CHAR_BIT == 8);
19739 if (dwz->str.buffer[str_offset] == '\0')
19740 return NULL;
d521ce57 19741 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19742}
19743
43988095
JK
19744/* Return pointer to string at .debug_str offset as read from BUF.
19745 BUF is assumed to be in a compilation unit described by CU_HEADER.
19746 Return *BYTES_READ_PTR count of bytes read from BUF. */
19747
d521ce57 19748static const char *
ed2dc618
SM
19749read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19750 const gdb_byte *buf,
cf2c3c16
TT
19751 const struct comp_unit_head *cu_header,
19752 unsigned int *bytes_read_ptr)
19753{
19754 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19755
ed2dc618 19756 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19757}
19758
43988095
JK
19759/* Return pointer to string at .debug_line_str offset as read from BUF.
19760 BUF is assumed to be in a compilation unit described by CU_HEADER.
19761 Return *BYTES_READ_PTR count of bytes read from BUF. */
19762
19763static const char *
ed2dc618
SM
19764read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19765 bfd *abfd, const gdb_byte *buf,
43988095
JK
19766 const struct comp_unit_head *cu_header,
19767 unsigned int *bytes_read_ptr)
19768{
19769 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19770
ed2dc618
SM
19771 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19772 str_offset);
43988095
JK
19773}
19774
19775ULONGEST
d521ce57 19776read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19777 unsigned int *bytes_read_ptr)
c906108c 19778{
12df843f 19779 ULONGEST result;
ce5d95e1 19780 unsigned int num_read;
870f88f7 19781 int shift;
c906108c
SS
19782 unsigned char byte;
19783
19784 result = 0;
19785 shift = 0;
19786 num_read = 0;
c906108c
SS
19787 while (1)
19788 {
fe1b8b76 19789 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19790 buf++;
19791 num_read++;
12df843f 19792 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19793 if ((byte & 128) == 0)
19794 {
19795 break;
19796 }
19797 shift += 7;
19798 }
19799 *bytes_read_ptr = num_read;
19800 return result;
19801}
19802
12df843f 19803static LONGEST
d521ce57
TT
19804read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19805 unsigned int *bytes_read_ptr)
c906108c 19806{
4dd1b460 19807 ULONGEST result;
870f88f7 19808 int shift, num_read;
c906108c
SS
19809 unsigned char byte;
19810
19811 result = 0;
19812 shift = 0;
c906108c 19813 num_read = 0;
c906108c
SS
19814 while (1)
19815 {
fe1b8b76 19816 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19817 buf++;
19818 num_read++;
4dd1b460 19819 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19820 shift += 7;
19821 if ((byte & 128) == 0)
19822 {
19823 break;
19824 }
19825 }
77e0b926 19826 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19827 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19828 *bytes_read_ptr = num_read;
19829 return result;
19830}
19831
3019eac3
DE
19832/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19833 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19834 ADDR_SIZE is the size of addresses from the CU header. */
19835
19836static CORE_ADDR
ed2dc618
SM
19837read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19838 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19839{
19840 struct objfile *objfile = dwarf2_per_objfile->objfile;
19841 bfd *abfd = objfile->obfd;
19842 const gdb_byte *info_ptr;
19843
19844 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19845 if (dwarf2_per_objfile->addr.buffer == NULL)
19846 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19847 objfile_name (objfile));
3019eac3
DE
19848 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19849 error (_("DW_FORM_addr_index pointing outside of "
19850 ".debug_addr section [in module %s]"),
4262abfb 19851 objfile_name (objfile));
3019eac3
DE
19852 info_ptr = (dwarf2_per_objfile->addr.buffer
19853 + addr_base + addr_index * addr_size);
19854 if (addr_size == 4)
19855 return bfd_get_32 (abfd, info_ptr);
19856 else
19857 return bfd_get_64 (abfd, info_ptr);
19858}
19859
19860/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19861
19862static CORE_ADDR
19863read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19864{
518817b3
SM
19865 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19866 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19867}
19868
19869/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19870
19871static CORE_ADDR
d521ce57 19872read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19873 unsigned int *bytes_read)
19874{
518817b3 19875 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19876 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19877
19878 return read_addr_index (cu, addr_index);
19879}
19880
19881/* Data structure to pass results from dwarf2_read_addr_index_reader
19882 back to dwarf2_read_addr_index. */
19883
19884struct dwarf2_read_addr_index_data
19885{
19886 ULONGEST addr_base;
19887 int addr_size;
19888};
19889
19890/* die_reader_func for dwarf2_read_addr_index. */
19891
19892static void
19893dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19894 const gdb_byte *info_ptr,
3019eac3
DE
19895 struct die_info *comp_unit_die,
19896 int has_children,
19897 void *data)
19898{
19899 struct dwarf2_cu *cu = reader->cu;
19900 struct dwarf2_read_addr_index_data *aidata =
19901 (struct dwarf2_read_addr_index_data *) data;
19902
19903 aidata->addr_base = cu->addr_base;
19904 aidata->addr_size = cu->header.addr_size;
19905}
19906
19907/* Given an index in .debug_addr, fetch the value.
19908 NOTE: This can be called during dwarf expression evaluation,
19909 long after the debug information has been read, and thus per_cu->cu
19910 may no longer exist. */
19911
19912CORE_ADDR
19913dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19914 unsigned int addr_index)
19915{
ed2dc618 19916 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19917 struct dwarf2_cu *cu = per_cu->cu;
19918 ULONGEST addr_base;
19919 int addr_size;
19920
3019eac3
DE
19921 /* We need addr_base and addr_size.
19922 If we don't have PER_CU->cu, we have to get it.
19923 Nasty, but the alternative is storing the needed info in PER_CU,
19924 which at this point doesn't seem justified: it's not clear how frequently
19925 it would get used and it would increase the size of every PER_CU.
19926 Entry points like dwarf2_per_cu_addr_size do a similar thing
19927 so we're not in uncharted territory here.
19928 Alas we need to be a bit more complicated as addr_base is contained
19929 in the DIE.
19930
19931 We don't need to read the entire CU(/TU).
19932 We just need the header and top level die.
a1b64ce1 19933
3019eac3 19934 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19935 For now we skip this optimization. */
3019eac3
DE
19936
19937 if (cu != NULL)
19938 {
19939 addr_base = cu->addr_base;
19940 addr_size = cu->header.addr_size;
19941 }
19942 else
19943 {
19944 struct dwarf2_read_addr_index_data aidata;
19945
a1b64ce1
DE
19946 /* Note: We can't use init_cutu_and_read_dies_simple here,
19947 we need addr_base. */
58f0c718 19948 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19949 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19950 addr_base = aidata.addr_base;
19951 addr_size = aidata.addr_size;
19952 }
19953
ed2dc618
SM
19954 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19955 addr_size);
3019eac3
DE
19956}
19957
cf532bd1 19958/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 19959 This is only used by the Fission support. */
3019eac3 19960
d521ce57 19961static const char *
342587c4 19962read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19963{
ed2dc618 19964 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19965 struct dwarf2_per_objfile *dwarf2_per_objfile
19966 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19967 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19968 const char *objf_name = objfile_name (objfile);
3019eac3 19969 bfd *abfd = objfile->obfd;
73869dc2
DE
19970 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19971 struct dwarf2_section_info *str_offsets_section =
19972 &reader->dwo_file->sections.str_offsets;
d521ce57 19973 const gdb_byte *info_ptr;
3019eac3 19974 ULONGEST str_offset;
cf532bd1 19975 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19976
73869dc2
DE
19977 dwarf2_read_section (objfile, str_section);
19978 dwarf2_read_section (objfile, str_offsets_section);
19979 if (str_section->buffer == NULL)
57d63ce2 19980 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19981 " in CU at offset %s [in module %s]"),
19982 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19983 if (str_offsets_section->buffer == NULL)
57d63ce2 19984 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19985 " in CU at offset %s [in module %s]"),
19986 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19987 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19988 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19989 " section in CU at offset %s [in module %s]"),
19990 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19991 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19992 + str_index * cu->header.offset_size);
19993 if (cu->header.offset_size == 4)
19994 str_offset = bfd_get_32 (abfd, info_ptr);
19995 else
19996 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19997 if (str_offset >= str_section->size)
57d63ce2 19998 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19999 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20000 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20001 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20002}
20003
3019eac3
DE
20004/* Return the length of an LEB128 number in BUF. */
20005
20006static int
20007leb128_size (const gdb_byte *buf)
20008{
20009 const gdb_byte *begin = buf;
20010 gdb_byte byte;
20011
20012 while (1)
20013 {
20014 byte = *buf++;
20015 if ((byte & 128) == 0)
20016 return buf - begin;
20017 }
20018}
20019
c906108c 20020static void
e142c38c 20021set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20022{
20023 switch (lang)
20024 {
20025 case DW_LANG_C89:
76bee0cc 20026 case DW_LANG_C99:
0cfd832f 20027 case DW_LANG_C11:
c906108c 20028 case DW_LANG_C:
d1be3247 20029 case DW_LANG_UPC:
e142c38c 20030 cu->language = language_c;
c906108c 20031 break;
9c37b5ae 20032 case DW_LANG_Java:
c906108c 20033 case DW_LANG_C_plus_plus:
0cfd832f
MW
20034 case DW_LANG_C_plus_plus_11:
20035 case DW_LANG_C_plus_plus_14:
e142c38c 20036 cu->language = language_cplus;
c906108c 20037 break;
6aecb9c2
JB
20038 case DW_LANG_D:
20039 cu->language = language_d;
20040 break;
c906108c
SS
20041 case DW_LANG_Fortran77:
20042 case DW_LANG_Fortran90:
b21b22e0 20043 case DW_LANG_Fortran95:
f7de9aab
MW
20044 case DW_LANG_Fortran03:
20045 case DW_LANG_Fortran08:
e142c38c 20046 cu->language = language_fortran;
c906108c 20047 break;
a766d390
DE
20048 case DW_LANG_Go:
20049 cu->language = language_go;
20050 break;
c906108c 20051 case DW_LANG_Mips_Assembler:
e142c38c 20052 cu->language = language_asm;
c906108c
SS
20053 break;
20054 case DW_LANG_Ada83:
8aaf0b47 20055 case DW_LANG_Ada95:
bc5f45f8
JB
20056 cu->language = language_ada;
20057 break;
72019c9c
GM
20058 case DW_LANG_Modula2:
20059 cu->language = language_m2;
20060 break;
fe8e67fd
PM
20061 case DW_LANG_Pascal83:
20062 cu->language = language_pascal;
20063 break;
22566fbd
DJ
20064 case DW_LANG_ObjC:
20065 cu->language = language_objc;
20066 break;
c44af4eb
TT
20067 case DW_LANG_Rust:
20068 case DW_LANG_Rust_old:
20069 cu->language = language_rust;
20070 break;
c906108c
SS
20071 case DW_LANG_Cobol74:
20072 case DW_LANG_Cobol85:
c906108c 20073 default:
e142c38c 20074 cu->language = language_minimal;
c906108c
SS
20075 break;
20076 }
e142c38c 20077 cu->language_defn = language_def (cu->language);
c906108c
SS
20078}
20079
20080/* Return the named attribute or NULL if not there. */
20081
20082static struct attribute *
e142c38c 20083dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20084{
a48e046c 20085 for (;;)
c906108c 20086 {
a48e046c
TT
20087 unsigned int i;
20088 struct attribute *spec = NULL;
20089
20090 for (i = 0; i < die->num_attrs; ++i)
20091 {
20092 if (die->attrs[i].name == name)
20093 return &die->attrs[i];
20094 if (die->attrs[i].name == DW_AT_specification
20095 || die->attrs[i].name == DW_AT_abstract_origin)
20096 spec = &die->attrs[i];
20097 }
20098
20099 if (!spec)
20100 break;
c906108c 20101
f2f0e013 20102 die = follow_die_ref (die, spec, &cu);
f2f0e013 20103 }
c5aa993b 20104
c906108c
SS
20105 return NULL;
20106}
20107
348e048f
DE
20108/* Return the named attribute or NULL if not there,
20109 but do not follow DW_AT_specification, etc.
20110 This is for use in contexts where we're reading .debug_types dies.
20111 Following DW_AT_specification, DW_AT_abstract_origin will take us
20112 back up the chain, and we want to go down. */
20113
20114static struct attribute *
45e58e77 20115dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20116{
20117 unsigned int i;
20118
20119 for (i = 0; i < die->num_attrs; ++i)
20120 if (die->attrs[i].name == name)
20121 return &die->attrs[i];
20122
20123 return NULL;
20124}
20125
7d45c7c3
KB
20126/* Return the string associated with a string-typed attribute, or NULL if it
20127 is either not found or is of an incorrect type. */
20128
20129static const char *
20130dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20131{
20132 struct attribute *attr;
20133 const char *str = NULL;
20134
20135 attr = dwarf2_attr (die, name, cu);
20136
20137 if (attr != NULL)
20138 {
43988095 20139 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20140 || attr->form == DW_FORM_string
cf532bd1 20141 || attr->form == DW_FORM_strx
b3340438 20142 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20143 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20144 str = DW_STRING (attr);
20145 else
b98664d3 20146 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20147 "DIE at %s in module %s"),
20148 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20150 }
20151
20152 return str;
20153}
20154
05cf31d1
JB
20155/* Return non-zero iff the attribute NAME is defined for the given DIE,
20156 and holds a non-zero value. This function should only be used for
2dc7f7b3 20157 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20158
20159static int
20160dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20161{
20162 struct attribute *attr = dwarf2_attr (die, name, cu);
20163
20164 return (attr && DW_UNSND (attr));
20165}
20166
3ca72b44 20167static int
e142c38c 20168die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20169{
05cf31d1
JB
20170 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20171 which value is non-zero. However, we have to be careful with
20172 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20173 (via dwarf2_flag_true_p) follows this attribute. So we may
20174 end up accidently finding a declaration attribute that belongs
20175 to a different DIE referenced by the specification attribute,
20176 even though the given DIE does not have a declaration attribute. */
20177 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20178 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20179}
20180
63d06c5c 20181/* Return the die giving the specification for DIE, if there is
f2f0e013 20182 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20183 containing the return value on output. If there is no
20184 specification, but there is an abstract origin, that is
20185 returned. */
63d06c5c
DC
20186
20187static struct die_info *
f2f0e013 20188die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20189{
f2f0e013
DJ
20190 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20191 *spec_cu);
63d06c5c 20192
edb3359d
DJ
20193 if (spec_attr == NULL)
20194 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20195
63d06c5c
DC
20196 if (spec_attr == NULL)
20197 return NULL;
20198 else
f2f0e013 20199 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20200}
c906108c 20201
527f3840
JK
20202/* Stub for free_line_header to match void * callback types. */
20203
20204static void
20205free_line_header_voidp (void *arg)
20206{
9a3c8263 20207 struct line_header *lh = (struct line_header *) arg;
527f3840 20208
fff8551c 20209 delete lh;
527f3840
JK
20210}
20211
fff8551c
PA
20212void
20213line_header::add_include_dir (const char *include_dir)
c906108c 20214{
27e0867f 20215 if (dwarf_line_debug >= 2)
fff8551c
PA
20216 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20217 include_dirs.size () + 1, include_dir);
27e0867f 20218
fff8551c 20219 include_dirs.push_back (include_dir);
debd256d 20220}
6e70227d 20221
fff8551c
PA
20222void
20223line_header::add_file_name (const char *name,
ecfb656c 20224 dir_index d_index,
fff8551c
PA
20225 unsigned int mod_time,
20226 unsigned int length)
debd256d 20227{
27e0867f
DE
20228 if (dwarf_line_debug >= 2)
20229 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20230 (unsigned) file_names.size () + 1, name);
27e0867f 20231
ecfb656c 20232 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20233}
6e70227d 20234
83769d0b 20235/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20236
20237static struct dwarf2_section_info *
20238get_debug_line_section (struct dwarf2_cu *cu)
20239{
20240 struct dwarf2_section_info *section;
518817b3
SM
20241 struct dwarf2_per_objfile *dwarf2_per_objfile
20242 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20243
20244 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20245 DWO file. */
20246 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20247 section = &cu->dwo_unit->dwo_file->sections.line;
20248 else if (cu->per_cu->is_dwz)
20249 {
ed2dc618 20250 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20251
20252 section = &dwz->line;
20253 }
20254 else
20255 section = &dwarf2_per_objfile->line;
20256
20257 return section;
20258}
20259
43988095
JK
20260/* Read directory or file name entry format, starting with byte of
20261 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20262 entries count and the entries themselves in the described entry
20263 format. */
20264
20265static void
ed2dc618
SM
20266read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20267 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20268 struct line_header *lh,
20269 const struct comp_unit_head *cu_header,
20270 void (*callback) (struct line_header *lh,
20271 const char *name,
ecfb656c 20272 dir_index d_index,
43988095
JK
20273 unsigned int mod_time,
20274 unsigned int length))
20275{
20276 gdb_byte format_count, formati;
20277 ULONGEST data_count, datai;
20278 const gdb_byte *buf = *bufp;
20279 const gdb_byte *format_header_data;
43988095
JK
20280 unsigned int bytes_read;
20281
20282 format_count = read_1_byte (abfd, buf);
20283 buf += 1;
20284 format_header_data = buf;
20285 for (formati = 0; formati < format_count; formati++)
20286 {
20287 read_unsigned_leb128 (abfd, buf, &bytes_read);
20288 buf += bytes_read;
20289 read_unsigned_leb128 (abfd, buf, &bytes_read);
20290 buf += bytes_read;
20291 }
20292
20293 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20294 buf += bytes_read;
20295 for (datai = 0; datai < data_count; datai++)
20296 {
20297 const gdb_byte *format = format_header_data;
20298 struct file_entry fe;
20299
43988095
JK
20300 for (formati = 0; formati < format_count; formati++)
20301 {
ecfb656c 20302 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20303 format += bytes_read;
43988095 20304
ecfb656c 20305 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20306 format += bytes_read;
ecfb656c
PA
20307
20308 gdb::optional<const char *> string;
20309 gdb::optional<unsigned int> uint;
20310
43988095
JK
20311 switch (form)
20312 {
20313 case DW_FORM_string:
ecfb656c 20314 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20315 buf += bytes_read;
20316 break;
20317
20318 case DW_FORM_line_strp:
ed2dc618
SM
20319 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20320 abfd, buf,
ecfb656c
PA
20321 cu_header,
20322 &bytes_read));
43988095
JK
20323 buf += bytes_read;
20324 break;
20325
20326 case DW_FORM_data1:
ecfb656c 20327 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20328 buf += 1;
20329 break;
20330
20331 case DW_FORM_data2:
ecfb656c 20332 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20333 buf += 2;
20334 break;
20335
20336 case DW_FORM_data4:
ecfb656c 20337 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20338 buf += 4;
20339 break;
20340
20341 case DW_FORM_data8:
ecfb656c 20342 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20343 buf += 8;
20344 break;
20345
20346 case DW_FORM_udata:
ecfb656c 20347 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20348 buf += bytes_read;
20349 break;
20350
20351 case DW_FORM_block:
20352 /* It is valid only for DW_LNCT_timestamp which is ignored by
20353 current GDB. */
20354 break;
20355 }
ecfb656c
PA
20356
20357 switch (content_type)
20358 {
20359 case DW_LNCT_path:
20360 if (string.has_value ())
20361 fe.name = *string;
20362 break;
20363 case DW_LNCT_directory_index:
20364 if (uint.has_value ())
20365 fe.d_index = (dir_index) *uint;
20366 break;
20367 case DW_LNCT_timestamp:
20368 if (uint.has_value ())
20369 fe.mod_time = *uint;
20370 break;
20371 case DW_LNCT_size:
20372 if (uint.has_value ())
20373 fe.length = *uint;
20374 break;
20375 case DW_LNCT_MD5:
20376 break;
20377 default:
b98664d3 20378 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20379 pulongest (content_type));
20380 }
43988095
JK
20381 }
20382
ecfb656c 20383 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20384 }
20385
20386 *bufp = buf;
20387}
20388
debd256d 20389/* Read the statement program header starting at OFFSET in
3019eac3 20390 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20391 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20392 Returns NULL if there is a problem reading the header, e.g., if it
20393 has a version we don't understand.
debd256d
JB
20394
20395 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20396 the returned object point into the dwarf line section buffer,
20397 and must not be freed. */
ae2de4f8 20398
fff8551c 20399static line_header_up
9c541725 20400dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20401{
d521ce57 20402 const gdb_byte *line_ptr;
c764a876 20403 unsigned int bytes_read, offset_size;
debd256d 20404 int i;
d521ce57 20405 const char *cur_dir, *cur_file;
3019eac3
DE
20406 struct dwarf2_section_info *section;
20407 bfd *abfd;
518817b3
SM
20408 struct dwarf2_per_objfile *dwarf2_per_objfile
20409 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20410
36586728 20411 section = get_debug_line_section (cu);
3019eac3
DE
20412 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20413 if (section->buffer == NULL)
debd256d 20414 {
3019eac3 20415 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20416 complaint (_("missing .debug_line.dwo section"));
3019eac3 20417 else
b98664d3 20418 complaint (_("missing .debug_line section"));
debd256d
JB
20419 return 0;
20420 }
20421
fceca515
DE
20422 /* We can't do this until we know the section is non-empty.
20423 Only then do we know we have such a section. */
a32a8923 20424 abfd = get_section_bfd_owner (section);
fceca515 20425
a738430d
MK
20426 /* Make sure that at least there's room for the total_length field.
20427 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20428 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20429 {
4d3c2250 20430 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20431 return 0;
20432 }
20433
fff8551c 20434 line_header_up lh (new line_header ());
debd256d 20435
9c541725 20436 lh->sect_off = sect_off;
527f3840
JK
20437 lh->offset_in_dwz = cu->per_cu->is_dwz;
20438
9c541725 20439 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20440
a738430d 20441 /* Read in the header. */
6e70227d 20442 lh->total_length =
c764a876
DE
20443 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20444 &bytes_read, &offset_size);
debd256d 20445 line_ptr += bytes_read;
3019eac3 20446 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20447 {
4d3c2250 20448 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20449 return 0;
20450 }
20451 lh->statement_program_end = line_ptr + lh->total_length;
20452 lh->version = read_2_bytes (abfd, line_ptr);
20453 line_ptr += 2;
43988095 20454 if (lh->version > 5)
cd366ee8
DE
20455 {
20456 /* This is a version we don't understand. The format could have
20457 changed in ways we don't handle properly so just punt. */
b98664d3 20458 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20459 return NULL;
20460 }
43988095
JK
20461 if (lh->version >= 5)
20462 {
20463 gdb_byte segment_selector_size;
20464
20465 /* Skip address size. */
20466 read_1_byte (abfd, line_ptr);
20467 line_ptr += 1;
20468
20469 segment_selector_size = read_1_byte (abfd, line_ptr);
20470 line_ptr += 1;
20471 if (segment_selector_size != 0)
20472 {
b98664d3 20473 complaint (_("unsupported segment selector size %u "
43988095
JK
20474 "in .debug_line section"),
20475 segment_selector_size);
20476 return NULL;
20477 }
20478 }
c764a876
DE
20479 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20480 line_ptr += offset_size;
debd256d
JB
20481 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20482 line_ptr += 1;
2dc7f7b3
TT
20483 if (lh->version >= 4)
20484 {
20485 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20486 line_ptr += 1;
20487 }
20488 else
20489 lh->maximum_ops_per_instruction = 1;
20490
20491 if (lh->maximum_ops_per_instruction == 0)
20492 {
20493 lh->maximum_ops_per_instruction = 1;
b98664d3 20494 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20495 "in `.debug_line' section"));
2dc7f7b3
TT
20496 }
20497
debd256d
JB
20498 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20499 line_ptr += 1;
20500 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20501 line_ptr += 1;
20502 lh->line_range = read_1_byte (abfd, line_ptr);
20503 line_ptr += 1;
20504 lh->opcode_base = read_1_byte (abfd, line_ptr);
20505 line_ptr += 1;
fff8551c 20506 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20507
20508 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20509 for (i = 1; i < lh->opcode_base; ++i)
20510 {
20511 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20512 line_ptr += 1;
20513 }
20514
43988095 20515 if (lh->version >= 5)
debd256d 20516 {
43988095 20517 /* Read directory table. */
ed2dc618
SM
20518 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20519 &cu->header,
b926417a 20520 [] (struct line_header *header, const char *name,
ecfb656c 20521 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20522 unsigned int length)
20523 {
b926417a 20524 header->add_include_dir (name);
fff8551c 20525 });
debd256d 20526
43988095 20527 /* Read file name table. */
ed2dc618
SM
20528 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20529 &cu->header,
b926417a 20530 [] (struct line_header *header, const char *name,
ecfb656c 20531 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20532 unsigned int length)
20533 {
b926417a 20534 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20535 });
43988095
JK
20536 }
20537 else
debd256d 20538 {
43988095
JK
20539 /* Read directory table. */
20540 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20541 {
20542 line_ptr += bytes_read;
fff8551c 20543 lh->add_include_dir (cur_dir);
43988095 20544 }
debd256d
JB
20545 line_ptr += bytes_read;
20546
43988095
JK
20547 /* Read file name table. */
20548 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20549 {
ecfb656c
PA
20550 unsigned int mod_time, length;
20551 dir_index d_index;
43988095
JK
20552
20553 line_ptr += bytes_read;
ecfb656c 20554 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20555 line_ptr += bytes_read;
20556 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20557 line_ptr += bytes_read;
20558 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20559 line_ptr += bytes_read;
20560
ecfb656c 20561 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20562 }
20563 line_ptr += bytes_read;
debd256d 20564 }
6e70227d 20565 lh->statement_program_start = line_ptr;
debd256d 20566
3019eac3 20567 if (line_ptr > (section->buffer + section->size))
b98664d3 20568 complaint (_("line number info header doesn't "
3e43a32a 20569 "fit in `.debug_line' section"));
debd256d 20570
debd256d
JB
20571 return lh;
20572}
c906108c 20573
c6da4cef
DE
20574/* Subroutine of dwarf_decode_lines to simplify it.
20575 Return the file name of the psymtab for included file FILE_INDEX
20576 in line header LH of PST.
20577 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20578 If space for the result is malloc'd, *NAME_HOLDER will be set.
20579 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20580
d521ce57 20581static const char *
c6da4cef
DE
20582psymtab_include_file_name (const struct line_header *lh, int file_index,
20583 const struct partial_symtab *pst,
c89b44cd
TT
20584 const char *comp_dir,
20585 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20586{
8c43009f 20587 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20588 const char *include_name = fe.name;
20589 const char *include_name_to_compare = include_name;
72b9f47f 20590 const char *pst_filename;
c6da4cef
DE
20591 int file_is_pst;
20592
8c43009f 20593 const char *dir_name = fe.include_dir (lh);
c6da4cef 20594
c89b44cd 20595 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20596 if (!IS_ABSOLUTE_PATH (include_name)
20597 && (dir_name != NULL || comp_dir != NULL))
20598 {
20599 /* Avoid creating a duplicate psymtab for PST.
20600 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20601 Before we do the comparison, however, we need to account
20602 for DIR_NAME and COMP_DIR.
20603 First prepend dir_name (if non-NULL). If we still don't
20604 have an absolute path prepend comp_dir (if non-NULL).
20605 However, the directory we record in the include-file's
20606 psymtab does not contain COMP_DIR (to match the
20607 corresponding symtab(s)).
20608
20609 Example:
20610
20611 bash$ cd /tmp
20612 bash$ gcc -g ./hello.c
20613 include_name = "hello.c"
20614 dir_name = "."
20615 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20616 DW_AT_name = "./hello.c"
20617
20618 */
c6da4cef
DE
20619
20620 if (dir_name != NULL)
20621 {
c89b44cd
TT
20622 name_holder->reset (concat (dir_name, SLASH_STRING,
20623 include_name, (char *) NULL));
20624 include_name = name_holder->get ();
c6da4cef 20625 include_name_to_compare = include_name;
c6da4cef
DE
20626 }
20627 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20628 {
c89b44cd
TT
20629 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20630 include_name, (char *) NULL));
20631 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20632 }
20633 }
20634
20635 pst_filename = pst->filename;
c89b44cd 20636 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20637 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20638 {
c89b44cd
TT
20639 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20640 pst_filename, (char *) NULL));
20641 pst_filename = copied_name.get ();
c6da4cef
DE
20642 }
20643
1e3fad37 20644 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20645
c6da4cef
DE
20646 if (file_is_pst)
20647 return NULL;
20648 return include_name;
20649}
20650
d9b3de22
DE
20651/* State machine to track the state of the line number program. */
20652
6f77053d 20653class lnp_state_machine
d9b3de22 20654{
6f77053d
PA
20655public:
20656 /* Initialize a machine state for the start of a line number
20657 program. */
804d2729
TT
20658 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20659 bool record_lines_p);
6f77053d 20660
8c43009f
PA
20661 file_entry *current_file ()
20662 {
20663 /* lh->file_names is 0-based, but the file name numbers in the
20664 statement program are 1-based. */
6f77053d
PA
20665 return m_line_header->file_name_at (m_file);
20666 }
20667
20668 /* Record the line in the state machine. END_SEQUENCE is true if
20669 we're processing the end of a sequence. */
20670 void record_line (bool end_sequence);
20671
7ab6656f
OJ
20672 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20673 nop-out rest of the lines in this sequence. */
6f77053d
PA
20674 void check_line_address (struct dwarf2_cu *cu,
20675 const gdb_byte *line_ptr,
7ab6656f 20676 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20677
20678 void handle_set_discriminator (unsigned int discriminator)
20679 {
20680 m_discriminator = discriminator;
20681 m_line_has_non_zero_discriminator |= discriminator != 0;
20682 }
20683
20684 /* Handle DW_LNE_set_address. */
20685 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20686 {
20687 m_op_index = 0;
20688 address += baseaddr;
20689 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20690 }
20691
20692 /* Handle DW_LNS_advance_pc. */
20693 void handle_advance_pc (CORE_ADDR adjust);
20694
20695 /* Handle a special opcode. */
20696 void handle_special_opcode (unsigned char op_code);
20697
20698 /* Handle DW_LNS_advance_line. */
20699 void handle_advance_line (int line_delta)
20700 {
20701 advance_line (line_delta);
20702 }
20703
20704 /* Handle DW_LNS_set_file. */
20705 void handle_set_file (file_name_index file);
20706
20707 /* Handle DW_LNS_negate_stmt. */
20708 void handle_negate_stmt ()
20709 {
20710 m_is_stmt = !m_is_stmt;
20711 }
20712
20713 /* Handle DW_LNS_const_add_pc. */
20714 void handle_const_add_pc ();
20715
20716 /* Handle DW_LNS_fixed_advance_pc. */
20717 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20718 {
20719 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20720 m_op_index = 0;
20721 }
20722
20723 /* Handle DW_LNS_copy. */
20724 void handle_copy ()
20725 {
20726 record_line (false);
20727 m_discriminator = 0;
20728 }
20729
20730 /* Handle DW_LNE_end_sequence. */
20731 void handle_end_sequence ()
20732 {
804d2729 20733 m_currently_recording_lines = true;
6f77053d
PA
20734 }
20735
20736private:
20737 /* Advance the line by LINE_DELTA. */
20738 void advance_line (int line_delta)
20739 {
20740 m_line += line_delta;
20741
20742 if (line_delta != 0)
20743 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20744 }
20745
804d2729
TT
20746 struct dwarf2_cu *m_cu;
20747
6f77053d
PA
20748 gdbarch *m_gdbarch;
20749
20750 /* True if we're recording lines.
20751 Otherwise we're building partial symtabs and are just interested in
20752 finding include files mentioned by the line number program. */
20753 bool m_record_lines_p;
20754
8c43009f 20755 /* The line number header. */
6f77053d 20756 line_header *m_line_header;
8c43009f 20757
6f77053d
PA
20758 /* These are part of the standard DWARF line number state machine,
20759 and initialized according to the DWARF spec. */
d9b3de22 20760
6f77053d 20761 unsigned char m_op_index = 0;
8c43009f 20762 /* The line table index (1-based) of the current file. */
6f77053d
PA
20763 file_name_index m_file = (file_name_index) 1;
20764 unsigned int m_line = 1;
20765
20766 /* These are initialized in the constructor. */
20767
20768 CORE_ADDR m_address;
20769 bool m_is_stmt;
20770 unsigned int m_discriminator;
d9b3de22
DE
20771
20772 /* Additional bits of state we need to track. */
20773
20774 /* The last file that we called dwarf2_start_subfile for.
20775 This is only used for TLLs. */
6f77053d 20776 unsigned int m_last_file = 0;
d9b3de22 20777 /* The last file a line number was recorded for. */
6f77053d 20778 struct subfile *m_last_subfile = NULL;
d9b3de22 20779
804d2729
TT
20780 /* When true, record the lines we decode. */
20781 bool m_currently_recording_lines = false;
d9b3de22
DE
20782
20783 /* The last line number that was recorded, used to coalesce
20784 consecutive entries for the same line. This can happen, for
20785 example, when discriminators are present. PR 17276. */
6f77053d
PA
20786 unsigned int m_last_line = 0;
20787 bool m_line_has_non_zero_discriminator = false;
8c43009f 20788};
d9b3de22 20789
6f77053d
PA
20790void
20791lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20792{
20793 CORE_ADDR addr_adj = (((m_op_index + adjust)
20794 / m_line_header->maximum_ops_per_instruction)
20795 * m_line_header->minimum_instruction_length);
20796 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20797 m_op_index = ((m_op_index + adjust)
20798 % m_line_header->maximum_ops_per_instruction);
20799}
d9b3de22 20800
6f77053d
PA
20801void
20802lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20803{
6f77053d
PA
20804 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20805 CORE_ADDR addr_adj = (((m_op_index
20806 + (adj_opcode / m_line_header->line_range))
20807 / m_line_header->maximum_ops_per_instruction)
20808 * m_line_header->minimum_instruction_length);
20809 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20810 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20811 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20812
6f77053d
PA
20813 int line_delta = (m_line_header->line_base
20814 + (adj_opcode % m_line_header->line_range));
20815 advance_line (line_delta);
20816 record_line (false);
20817 m_discriminator = 0;
20818}
d9b3de22 20819
6f77053d
PA
20820void
20821lnp_state_machine::handle_set_file (file_name_index file)
20822{
20823 m_file = file;
20824
20825 const file_entry *fe = current_file ();
20826 if (fe == NULL)
20827 dwarf2_debug_line_missing_file_complaint ();
20828 else if (m_record_lines_p)
20829 {
20830 const char *dir = fe->include_dir (m_line_header);
20831
c24bdb02 20832 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20833 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20834 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20835 }
20836}
20837
20838void
20839lnp_state_machine::handle_const_add_pc ()
20840{
20841 CORE_ADDR adjust
20842 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20843
20844 CORE_ADDR addr_adj
20845 = (((m_op_index + adjust)
20846 / m_line_header->maximum_ops_per_instruction)
20847 * m_line_header->minimum_instruction_length);
20848
20849 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20850 m_op_index = ((m_op_index + adjust)
20851 % m_line_header->maximum_ops_per_instruction);
20852}
d9b3de22 20853
a05a36a5
DE
20854/* Return non-zero if we should add LINE to the line number table.
20855 LINE is the line to add, LAST_LINE is the last line that was added,
20856 LAST_SUBFILE is the subfile for LAST_LINE.
20857 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20858 had a non-zero discriminator.
20859
20860 We have to be careful in the presence of discriminators.
20861 E.g., for this line:
20862
20863 for (i = 0; i < 100000; i++);
20864
20865 clang can emit four line number entries for that one line,
20866 each with a different discriminator.
20867 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20868
20869 However, we want gdb to coalesce all four entries into one.
20870 Otherwise the user could stepi into the middle of the line and
20871 gdb would get confused about whether the pc really was in the
20872 middle of the line.
20873
20874 Things are further complicated by the fact that two consecutive
20875 line number entries for the same line is a heuristic used by gcc
20876 to denote the end of the prologue. So we can't just discard duplicate
20877 entries, we have to be selective about it. The heuristic we use is
20878 that we only collapse consecutive entries for the same line if at least
20879 one of those entries has a non-zero discriminator. PR 17276.
20880
20881 Note: Addresses in the line number state machine can never go backwards
20882 within one sequence, thus this coalescing is ok. */
20883
20884static int
804d2729
TT
20885dwarf_record_line_p (struct dwarf2_cu *cu,
20886 unsigned int line, unsigned int last_line,
a05a36a5
DE
20887 int line_has_non_zero_discriminator,
20888 struct subfile *last_subfile)
20889{
c24bdb02 20890 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20891 return 1;
20892 if (line != last_line)
20893 return 1;
20894 /* Same line for the same file that we've seen already.
20895 As a last check, for pr 17276, only record the line if the line
20896 has never had a non-zero discriminator. */
20897 if (!line_has_non_zero_discriminator)
20898 return 1;
20899 return 0;
20900}
20901
804d2729
TT
20902/* Use the CU's builder to record line number LINE beginning at
20903 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20904
20905static void
d9b3de22
DE
20906dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20907 unsigned int line, CORE_ADDR address,
804d2729 20908 struct dwarf2_cu *cu)
252a6764
DE
20909{
20910 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20911
27e0867f
DE
20912 if (dwarf_line_debug)
20913 {
20914 fprintf_unfiltered (gdb_stdlog,
20915 "Recording line %u, file %s, address %s\n",
20916 line, lbasename (subfile->name),
20917 paddress (gdbarch, address));
20918 }
20919
804d2729 20920 if (cu != nullptr)
c24bdb02 20921 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20922}
20923
20924/* Subroutine of dwarf_decode_lines_1 to simplify it.
20925 Mark the end of a set of line number records.
d9b3de22 20926 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20927 If SUBFILE is NULL the request is ignored. */
20928
20929static void
20930dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20931 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20932{
27e0867f
DE
20933 if (subfile == NULL)
20934 return;
20935
20936 if (dwarf_line_debug)
20937 {
20938 fprintf_unfiltered (gdb_stdlog,
20939 "Finishing current line, file %s, address %s\n",
20940 lbasename (subfile->name),
20941 paddress (gdbarch, address));
20942 }
20943
804d2729 20944 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20945}
20946
6f77053d
PA
20947void
20948lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20949{
d9b3de22
DE
20950 if (dwarf_line_debug)
20951 {
20952 fprintf_unfiltered (gdb_stdlog,
20953 "Processing actual line %u: file %u,"
20954 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20955 m_line, to_underlying (m_file),
20956 paddress (m_gdbarch, m_address),
20957 m_is_stmt, m_discriminator);
d9b3de22
DE
20958 }
20959
6f77053d 20960 file_entry *fe = current_file ();
8c43009f
PA
20961
20962 if (fe == NULL)
d9b3de22
DE
20963 dwarf2_debug_line_missing_file_complaint ();
20964 /* For now we ignore lines not starting on an instruction boundary.
20965 But not when processing end_sequence for compatibility with the
20966 previous version of the code. */
6f77053d 20967 else if (m_op_index == 0 || end_sequence)
d9b3de22 20968 {
8c43009f 20969 fe->included_p = 1;
c258c396 20970 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 20971 {
c24bdb02 20972 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20973 || end_sequence)
d9b3de22 20974 {
804d2729
TT
20975 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20976 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20977 }
20978
20979 if (!end_sequence)
20980 {
804d2729 20981 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20982 m_line_has_non_zero_discriminator,
20983 m_last_subfile))
d9b3de22 20984 {
c24bdb02 20985 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20986 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20987 builder->get_current_subfile (),
6f77053d 20988 m_line, m_address,
804d2729 20989 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20990 }
c24bdb02 20991 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20992 m_last_line = m_line;
d9b3de22
DE
20993 }
20994 }
20995 }
20996}
20997
804d2729
TT
20998lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20999 line_header *lh, bool record_lines_p)
d9b3de22 21000{
804d2729 21001 m_cu = cu;
6f77053d
PA
21002 m_gdbarch = arch;
21003 m_record_lines_p = record_lines_p;
21004 m_line_header = lh;
d9b3de22 21005
804d2729 21006 m_currently_recording_lines = true;
d9b3de22 21007
d9b3de22
DE
21008 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21009 was a line entry for it so that the backend has a chance to adjust it
21010 and also record it in case it needs it. This is currently used by MIPS
21011 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21012 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21013 m_is_stmt = lh->default_is_stmt;
21014 m_discriminator = 0;
252a6764
DE
21015}
21016
6f77053d
PA
21017void
21018lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21019 const gdb_byte *line_ptr,
7ab6656f 21020 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21021{
7ab6656f
OJ
21022 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21023 the pc range of the CU. However, we restrict the test to only ADDRESS
21024 values of zero to preserve GDB's previous behaviour which is to handle
21025 the specific case of a function being GC'd by the linker. */
924c2928 21026
7ab6656f 21027 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21028 {
21029 /* This line table is for a function which has been
21030 GCd by the linker. Ignore it. PR gdb/12528 */
21031
518817b3 21032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21033 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21034
b98664d3 21035 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21036 line_offset, objfile_name (objfile));
804d2729
TT
21037 m_currently_recording_lines = false;
21038 /* Note: m_currently_recording_lines is left as false until we see
21039 DW_LNE_end_sequence. */
924c2928
DE
21040 }
21041}
21042
f3f5162e 21043/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21044 Process the line number information in LH.
21045 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21046 program in order to set included_p for every referenced header. */
debd256d 21047
c906108c 21048static void
43f3e411
DE
21049dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21050 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21051{
d521ce57
TT
21052 const gdb_byte *line_ptr, *extended_end;
21053 const gdb_byte *line_end;
a8c50c1f 21054 unsigned int bytes_read, extended_len;
699ca60a 21055 unsigned char op_code, extended_op;
e142c38c 21056 CORE_ADDR baseaddr;
518817b3 21057 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21058 bfd *abfd = objfile->obfd;
fbf65064 21059 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21060 /* True if we're recording line info (as opposed to building partial
21061 symtabs and just interested in finding include files mentioned by
21062 the line number program). */
21063 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21064
21065 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21066
debd256d
JB
21067 line_ptr = lh->statement_program_start;
21068 line_end = lh->statement_program_end;
c906108c
SS
21069
21070 /* Read the statement sequences until there's nothing left. */
21071 while (line_ptr < line_end)
21072 {
6f77053d
PA
21073 /* The DWARF line number program state machine. Reset the state
21074 machine at the start of each sequence. */
804d2729 21075 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21076 bool end_sequence = false;
d9b3de22 21077
8c43009f 21078 if (record_lines_p)
c906108c 21079 {
8c43009f
PA
21080 /* Start a subfile for the current file of the state
21081 machine. */
21082 const file_entry *fe = state_machine.current_file ();
21083
21084 if (fe != NULL)
804d2729 21085 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21086 }
21087
a738430d 21088 /* Decode the table. */
d9b3de22 21089 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21090 {
21091 op_code = read_1_byte (abfd, line_ptr);
21092 line_ptr += 1;
9aa1fe7e 21093
debd256d 21094 if (op_code >= lh->opcode_base)
6e70227d 21095 {
8e07a239 21096 /* Special opcode. */
6f77053d 21097 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21098 }
21099 else switch (op_code)
c906108c
SS
21100 {
21101 case DW_LNS_extended_op:
3e43a32a
MS
21102 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21103 &bytes_read);
473b7be6 21104 line_ptr += bytes_read;
a8c50c1f 21105 extended_end = line_ptr + extended_len;
c906108c
SS
21106 extended_op = read_1_byte (abfd, line_ptr);
21107 line_ptr += 1;
21108 switch (extended_op)
21109 {
21110 case DW_LNE_end_sequence:
6f77053d
PA
21111 state_machine.handle_end_sequence ();
21112 end_sequence = true;
c906108c
SS
21113 break;
21114 case DW_LNE_set_address:
d9b3de22
DE
21115 {
21116 CORE_ADDR address
21117 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21118 line_ptr += bytes_read;
6f77053d
PA
21119
21120 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21121 lowpc - baseaddr, address);
6f77053d 21122 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21123 }
c906108c
SS
21124 break;
21125 case DW_LNE_define_file:
debd256d 21126 {
d521ce57 21127 const char *cur_file;
ecfb656c
PA
21128 unsigned int mod_time, length;
21129 dir_index dindex;
6e70227d 21130
3e43a32a
MS
21131 cur_file = read_direct_string (abfd, line_ptr,
21132 &bytes_read);
debd256d 21133 line_ptr += bytes_read;
ecfb656c 21134 dindex = (dir_index)
debd256d
JB
21135 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21136 line_ptr += bytes_read;
21137 mod_time =
21138 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21139 line_ptr += bytes_read;
21140 length =
21141 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21142 line_ptr += bytes_read;
ecfb656c 21143 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21144 }
c906108c 21145 break;
d0c6ba3d 21146 case DW_LNE_set_discriminator:
6f77053d
PA
21147 {
21148 /* The discriminator is not interesting to the
21149 debugger; just ignore it. We still need to
21150 check its value though:
21151 if there are consecutive entries for the same
21152 (non-prologue) line we want to coalesce them.
21153 PR 17276. */
21154 unsigned int discr
21155 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21156 line_ptr += bytes_read;
21157
21158 state_machine.handle_set_discriminator (discr);
21159 }
d0c6ba3d 21160 break;
c906108c 21161 default:
b98664d3 21162 complaint (_("mangled .debug_line section"));
debd256d 21163 return;
c906108c 21164 }
a8c50c1f
DJ
21165 /* Make sure that we parsed the extended op correctly. If e.g.
21166 we expected a different address size than the producer used,
21167 we may have read the wrong number of bytes. */
21168 if (line_ptr != extended_end)
21169 {
b98664d3 21170 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21171 return;
21172 }
c906108c
SS
21173 break;
21174 case DW_LNS_copy:
6f77053d 21175 state_machine.handle_copy ();
c906108c
SS
21176 break;
21177 case DW_LNS_advance_pc:
2dc7f7b3
TT
21178 {
21179 CORE_ADDR adjust
21180 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21181 line_ptr += bytes_read;
6f77053d
PA
21182
21183 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21184 }
c906108c
SS
21185 break;
21186 case DW_LNS_advance_line:
a05a36a5
DE
21187 {
21188 int line_delta
21189 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21190 line_ptr += bytes_read;
6f77053d
PA
21191
21192 state_machine.handle_advance_line (line_delta);
a05a36a5 21193 }
c906108c
SS
21194 break;
21195 case DW_LNS_set_file:
d9b3de22 21196 {
6f77053d 21197 file_name_index file
ecfb656c
PA
21198 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21199 &bytes_read);
d9b3de22 21200 line_ptr += bytes_read;
8c43009f 21201
6f77053d 21202 state_machine.handle_set_file (file);
d9b3de22 21203 }
c906108c
SS
21204 break;
21205 case DW_LNS_set_column:
0ad93d4f 21206 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21207 line_ptr += bytes_read;
21208 break;
21209 case DW_LNS_negate_stmt:
6f77053d 21210 state_machine.handle_negate_stmt ();
c906108c
SS
21211 break;
21212 case DW_LNS_set_basic_block:
c906108c 21213 break;
c2c6d25f
JM
21214 /* Add to the address register of the state machine the
21215 address increment value corresponding to special opcode
a738430d
MK
21216 255. I.e., this value is scaled by the minimum
21217 instruction length since special opcode 255 would have
b021a221 21218 scaled the increment. */
c906108c 21219 case DW_LNS_const_add_pc:
6f77053d 21220 state_machine.handle_const_add_pc ();
c906108c
SS
21221 break;
21222 case DW_LNS_fixed_advance_pc:
3e29f34a 21223 {
6f77053d 21224 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21225 line_ptr += 2;
6f77053d
PA
21226
21227 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21228 }
c906108c 21229 break;
9aa1fe7e 21230 default:
a738430d
MK
21231 {
21232 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21233 int i;
a738430d 21234
debd256d 21235 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21236 {
21237 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21238 line_ptr += bytes_read;
21239 }
21240 }
c906108c
SS
21241 }
21242 }
d9b3de22
DE
21243
21244 if (!end_sequence)
21245 dwarf2_debug_line_missing_end_sequence_complaint ();
21246
21247 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21248 in which case we still finish recording the last line). */
6f77053d 21249 state_machine.record_line (true);
c906108c 21250 }
f3f5162e
DE
21251}
21252
21253/* Decode the Line Number Program (LNP) for the given line_header
21254 structure and CU. The actual information extracted and the type
21255 of structures created from the LNP depends on the value of PST.
21256
21257 1. If PST is NULL, then this procedure uses the data from the program
21258 to create all necessary symbol tables, and their linetables.
21259
21260 2. If PST is not NULL, this procedure reads the program to determine
21261 the list of files included by the unit represented by PST, and
21262 builds all the associated partial symbol tables.
21263
21264 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21265 It is used for relative paths in the line table.
21266 NOTE: When processing partial symtabs (pst != NULL),
21267 comp_dir == pst->dirname.
21268
21269 NOTE: It is important that psymtabs have the same file name (via strcmp)
21270 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21271 symtab we don't use it in the name of the psymtabs we create.
21272 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21273 A good testcase for this is mb-inline.exp.
21274
527f3840
JK
21275 LOWPC is the lowest address in CU (or 0 if not known).
21276
21277 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21278 for its PC<->lines mapping information. Otherwise only the filename
21279 table is read in. */
f3f5162e
DE
21280
21281static void
21282dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21283 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21284 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21285{
518817b3 21286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21287 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21288
527f3840
JK
21289 if (decode_mapping)
21290 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21291
21292 if (decode_for_pst_p)
21293 {
21294 int file_index;
21295
21296 /* Now that we're done scanning the Line Header Program, we can
21297 create the psymtab of each included file. */
fff8551c 21298 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21299 if (lh->file_names[file_index].included_p == 1)
21300 {
c89b44cd 21301 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21302 const char *include_name =
c89b44cd
TT
21303 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21304 &name_holder);
c6da4cef 21305 if (include_name != NULL)
aaa75496
JB
21306 dwarf2_create_include_psymtab (include_name, pst, objfile);
21307 }
21308 }
cb1df416
DJ
21309 else
21310 {
21311 /* Make sure a symtab is created for every file, even files
21312 which contain only variables (i.e. no code with associated
21313 line numbers). */
c24bdb02
KS
21314 buildsym_compunit *builder = cu->get_builder ();
21315 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21316 int i;
cb1df416 21317
fff8551c 21318 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21319 {
8c43009f 21320 file_entry &fe = lh->file_names[i];
9a619af0 21321
804d2729 21322 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21323
c24bdb02 21324 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21325 {
c24bdb02 21326 builder->get_current_subfile ()->symtab
804d2729 21327 = allocate_symtab (cust,
c24bdb02 21328 builder->get_current_subfile ()->name);
43f3e411 21329 }
c24bdb02 21330 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21331 }
21332 }
c906108c
SS
21333}
21334
21335/* Start a subfile for DWARF. FILENAME is the name of the file and
21336 DIRNAME the name of the source directory which contains FILENAME
4d663531 21337 or NULL if not known.
c906108c
SS
21338 This routine tries to keep line numbers from identical absolute and
21339 relative file names in a common subfile.
21340
21341 Using the `list' example from the GDB testsuite, which resides in
21342 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21343 of /srcdir/list0.c yields the following debugging information for list0.c:
21344
c5aa993b 21345 DW_AT_name: /srcdir/list0.c
4d663531 21346 DW_AT_comp_dir: /compdir
357e46e7 21347 files.files[0].name: list0.h
c5aa993b 21348 files.files[0].dir: /srcdir
357e46e7 21349 files.files[1].name: list0.c
c5aa993b 21350 files.files[1].dir: /srcdir
c906108c
SS
21351
21352 The line number information for list0.c has to end up in a single
4f1520fb
FR
21353 subfile, so that `break /srcdir/list0.c:1' works as expected.
21354 start_subfile will ensure that this happens provided that we pass the
21355 concatenation of files.files[1].dir and files.files[1].name as the
21356 subfile's name. */
c906108c
SS
21357
21358static void
804d2729
TT
21359dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21360 const char *dirname)
c906108c 21361{
d521ce57 21362 char *copy = NULL;
4f1520fb 21363
4d663531 21364 /* In order not to lose the line information directory,
4f1520fb
FR
21365 we concatenate it to the filename when it makes sense.
21366 Note that the Dwarf3 standard says (speaking of filenames in line
21367 information): ``The directory index is ignored for file names
21368 that represent full path names''. Thus ignoring dirname in the
21369 `else' branch below isn't an issue. */
c906108c 21370
d5166ae1 21371 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21372 {
21373 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21374 filename = copy;
21375 }
c906108c 21376
c24bdb02 21377 cu->get_builder ()->start_subfile (filename);
4f1520fb 21378
d521ce57
TT
21379 if (copy != NULL)
21380 xfree (copy);
c906108c
SS
21381}
21382
804d2729
TT
21383/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21384 buildsym_compunit constructor. */
f4dc4d17 21385
c24bdb02
KS
21386struct compunit_symtab *
21387dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21388 CORE_ADDR low_pc)
f4dc4d17 21389{
c24bdb02 21390 gdb_assert (m_builder == nullptr);
43f3e411 21391
c24bdb02
KS
21392 m_builder.reset (new struct buildsym_compunit
21393 (per_cu->dwarf2_per_objfile->objfile,
21394 name, comp_dir, language, low_pc));
93b8bea4 21395
c24bdb02 21396 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21397
c24bdb02
KS
21398 get_builder ()->record_debugformat ("DWARF 2");
21399 get_builder ()->record_producer (producer);
f4dc4d17 21400
c24bdb02 21401 processing_has_namespace_info = false;
43f3e411 21402
c24bdb02 21403 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21404}
21405
4c2df51b
DJ
21406static void
21407var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21408 struct dwarf2_cu *cu)
4c2df51b 21409{
518817b3 21410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21411 struct comp_unit_head *cu_header = &cu->header;
21412
4c2df51b
DJ
21413 /* NOTE drow/2003-01-30: There used to be a comment and some special
21414 code here to turn a symbol with DW_AT_external and a
21415 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21416 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21417 with some versions of binutils) where shared libraries could have
21418 relocations against symbols in their debug information - the
21419 minimal symbol would have the right address, but the debug info
21420 would not. It's no longer necessary, because we will explicitly
21421 apply relocations when we read in the debug information now. */
21422
21423 /* A DW_AT_location attribute with no contents indicates that a
21424 variable has been optimized away. */
21425 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21426 {
f1e6e072 21427 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21428 return;
21429 }
21430
21431 /* Handle one degenerate form of location expression specially, to
21432 preserve GDB's previous behavior when section offsets are
336d760d
AT
21433 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21434 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21435
21436 if (attr_form_is_block (attr)
3019eac3
DE
21437 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21438 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21439 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21440 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21441 && (DW_BLOCK (attr)->size
21442 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21443 {
891d2f0b 21444 unsigned int dummy;
4c2df51b 21445
3019eac3
DE
21446 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21447 SYMBOL_VALUE_ADDRESS (sym) =
21448 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21449 else
21450 SYMBOL_VALUE_ADDRESS (sym) =
21451 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21452 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21453 fixup_symbol_section (sym, objfile);
21454 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21455 SYMBOL_SECTION (sym));
4c2df51b
DJ
21456 return;
21457 }
21458
21459 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21460 expression evaluator, and use LOC_COMPUTED only when necessary
21461 (i.e. when the value of a register or memory location is
21462 referenced, or a thread-local block, etc.). Then again, it might
21463 not be worthwhile. I'm assuming that it isn't unless performance
21464 or memory numbers show me otherwise. */
21465
f1e6e072 21466 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21467
f1e6e072 21468 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21469 cu->has_loclist = true;
4c2df51b
DJ
21470}
21471
c906108c
SS
21472/* Given a pointer to a DWARF information entry, figure out if we need
21473 to make a symbol table entry for it, and if so, create a new entry
21474 and return a pointer to it.
21475 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21476 used the passed type.
21477 If SPACE is not NULL, use it to hold the new symbol. If it is
21478 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21479
21480static struct symbol *
5e2db402
TT
21481new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21482 struct symbol *space)
c906108c 21483{
518817b3
SM
21484 struct dwarf2_per_objfile *dwarf2_per_objfile
21485 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21486 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21487 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21488 struct symbol *sym = NULL;
15d034d0 21489 const char *name;
c906108c
SS
21490 struct attribute *attr = NULL;
21491 struct attribute *attr2 = NULL;
e142c38c 21492 CORE_ADDR baseaddr;
e37fd15a
SW
21493 struct pending **list_to_add = NULL;
21494
edb3359d 21495 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21496
21497 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21498
94af9270 21499 name = dwarf2_name (die, cu);
c906108c
SS
21500 if (name)
21501 {
94af9270 21502 const char *linkagename;
34eaf542 21503 int suppress_add = 0;
94af9270 21504
34eaf542
TT
21505 if (space)
21506 sym = space;
21507 else
e623cf5d 21508 sym = allocate_symbol (objfile);
c906108c 21509 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21510
21511 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21512 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21513 linkagename = dwarf2_physname (name, die, cu);
21514 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21515
f55ee35c
JK
21516 /* Fortran does not have mangling standard and the mangling does differ
21517 between gfortran, iFort etc. */
21518 if (cu->language == language_fortran
b250c185 21519 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21520 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21521 dwarf2_full_name (name, die, cu),
29df156d 21522 NULL);
f55ee35c 21523
c906108c 21524 /* Default assumptions.
c5aa993b 21525 Use the passed type or decode it from the die. */
176620f1 21526 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21527 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21528 if (type != NULL)
21529 SYMBOL_TYPE (sym) = type;
21530 else
e7c27a73 21531 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21532 attr = dwarf2_attr (die,
21533 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21534 cu);
c906108c
SS
21535 if (attr)
21536 {
21537 SYMBOL_LINE (sym) = DW_UNSND (attr);
21538 }
cb1df416 21539
edb3359d
DJ
21540 attr = dwarf2_attr (die,
21541 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21542 cu);
cb1df416
DJ
21543 if (attr)
21544 {
ecfb656c 21545 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21546 struct file_entry *fe;
9a619af0 21547
ecfb656c
PA
21548 if (cu->line_header != NULL)
21549 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21550 else
21551 fe = NULL;
21552
21553 if (fe == NULL)
b98664d3 21554 complaint (_("file index out of range"));
8c43009f
PA
21555 else
21556 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21557 }
21558
c906108c
SS
21559 switch (die->tag)
21560 {
21561 case DW_TAG_label:
e142c38c 21562 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21563 if (attr)
3e29f34a
MR
21564 {
21565 CORE_ADDR addr;
21566
21567 addr = attr_value_as_address (attr);
21568 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21569 SYMBOL_VALUE_ADDRESS (sym) = addr;
21570 }
0f5238ed
TT
21571 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21572 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21573 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21574 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21575 break;
21576 case DW_TAG_subprogram:
21577 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21578 finish_block. */
f1e6e072 21579 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21580 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21581 if ((attr2 && (DW_UNSND (attr2) != 0))
21582 || cu->language == language_ada)
c906108c 21583 {
2cfa0c8d
JB
21584 /* Subprograms marked external are stored as a global symbol.
21585 Ada subprograms, whether marked external or not, are always
21586 stored as a global symbol, because we want to be able to
21587 access them globally. For instance, we want to be able
21588 to break on a nested subprogram without having to
21589 specify the context. */
c24bdb02 21590 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21591 }
21592 else
21593 {
e37fd15a 21594 list_to_add = cu->list_in_scope;
c906108c
SS
21595 }
21596 break;
edb3359d
DJ
21597 case DW_TAG_inlined_subroutine:
21598 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21599 finish_block. */
f1e6e072 21600 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21601 SYMBOL_INLINED (sym) = 1;
481860b3 21602 list_to_add = cu->list_in_scope;
edb3359d 21603 break;
34eaf542
TT
21604 case DW_TAG_template_value_param:
21605 suppress_add = 1;
21606 /* Fall through. */
72929c62 21607 case DW_TAG_constant:
c906108c 21608 case DW_TAG_variable:
254e6b9e 21609 case DW_TAG_member:
0963b4bd
MS
21610 /* Compilation with minimal debug info may result in
21611 variables with missing type entries. Change the
21612 misleading `void' type to something sensible. */
c906108c 21613 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21614 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21615
e142c38c 21616 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21617 /* In the case of DW_TAG_member, we should only be called for
21618 static const members. */
21619 if (die->tag == DW_TAG_member)
21620 {
3863f96c
DE
21621 /* dwarf2_add_field uses die_is_declaration,
21622 so we do the same. */
254e6b9e
DE
21623 gdb_assert (die_is_declaration (die, cu));
21624 gdb_assert (attr);
21625 }
c906108c
SS
21626 if (attr)
21627 {
e7c27a73 21628 dwarf2_const_value (attr, sym, cu);
e142c38c 21629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21630 if (!suppress_add)
34eaf542
TT
21631 {
21632 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21633 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21634 else
e37fd15a 21635 list_to_add = cu->list_in_scope;
34eaf542 21636 }
c906108c
SS
21637 break;
21638 }
e142c38c 21639 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21640 if (attr)
21641 {
e7c27a73 21642 var_decode_location (attr, sym, cu);
e142c38c 21643 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21644
21645 /* Fortran explicitly imports any global symbols to the local
21646 scope by DW_TAG_common_block. */
21647 if (cu->language == language_fortran && die->parent
21648 && die->parent->tag == DW_TAG_common_block)
21649 attr2 = NULL;
21650
caac4577
JG
21651 if (SYMBOL_CLASS (sym) == LOC_STATIC
21652 && SYMBOL_VALUE_ADDRESS (sym) == 0
21653 && !dwarf2_per_objfile->has_section_at_zero)
21654 {
21655 /* When a static variable is eliminated by the linker,
21656 the corresponding debug information is not stripped
21657 out, but the variable address is set to null;
21658 do not add such variables into symbol table. */
21659 }
21660 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21661 {
f55ee35c
JK
21662 /* Workaround gfortran PR debug/40040 - it uses
21663 DW_AT_location for variables in -fPIC libraries which may
21664 get overriden by other libraries/executable and get
21665 a different address. Resolve it by the minimal symbol
21666 which may come from inferior's executable using copy
21667 relocation. Make this workaround only for gfortran as for
21668 other compilers GDB cannot guess the minimal symbol
21669 Fortran mangling kind. */
21670 if (cu->language == language_fortran && die->parent
21671 && die->parent->tag == DW_TAG_module
21672 && cu->producer
28586665 21673 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21674 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21675
1c809c68
TT
21676 /* A variable with DW_AT_external is never static,
21677 but it may be block-scoped. */
804d2729 21678 list_to_add
c24bdb02
KS
21679 = ((cu->list_in_scope
21680 == cu->get_builder ()->get_file_symbols ())
21681 ? cu->get_builder ()->get_global_symbols ()
804d2729 21682 : cu->list_in_scope);
1c809c68 21683 }
c906108c 21684 else
e37fd15a 21685 list_to_add = cu->list_in_scope;
c906108c
SS
21686 }
21687 else
21688 {
21689 /* We do not know the address of this symbol.
c5aa993b
JM
21690 If it is an external symbol and we have type information
21691 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21692 The address of the variable will then be determined from
21693 the minimal symbol table whenever the variable is
21694 referenced. */
e142c38c 21695 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21696
21697 /* Fortran explicitly imports any global symbols to the local
21698 scope by DW_TAG_common_block. */
21699 if (cu->language == language_fortran && die->parent
21700 && die->parent->tag == DW_TAG_common_block)
21701 {
21702 /* SYMBOL_CLASS doesn't matter here because
21703 read_common_block is going to reset it. */
21704 if (!suppress_add)
21705 list_to_add = cu->list_in_scope;
21706 }
21707 else if (attr2 && (DW_UNSND (attr2) != 0)
21708 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21709 {
0fe7935b
DJ
21710 /* A variable with DW_AT_external is never static, but it
21711 may be block-scoped. */
804d2729 21712 list_to_add
c24bdb02
KS
21713 = ((cu->list_in_scope
21714 == cu->get_builder ()->get_file_symbols ())
21715 ? cu->get_builder ()->get_global_symbols ()
804d2729 21716 : cu->list_in_scope);
0fe7935b 21717
f1e6e072 21718 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21719 }
442ddf59
JK
21720 else if (!die_is_declaration (die, cu))
21721 {
21722 /* Use the default LOC_OPTIMIZED_OUT class. */
21723 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21724 if (!suppress_add)
21725 list_to_add = cu->list_in_scope;
442ddf59 21726 }
c906108c
SS
21727 }
21728 break;
21729 case DW_TAG_formal_parameter:
a60f3166
TT
21730 {
21731 /* If we are inside a function, mark this as an argument. If
21732 not, we might be looking at an argument to an inlined function
21733 when we do not have enough information to show inlined frames;
21734 pretend it's a local variable in that case so that the user can
21735 still see it. */
804d2729 21736 struct context_stack *curr
c24bdb02 21737 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21738 if (curr != nullptr && curr->name != nullptr)
21739 SYMBOL_IS_ARGUMENT (sym) = 1;
21740 attr = dwarf2_attr (die, DW_AT_location, cu);
21741 if (attr)
21742 {
21743 var_decode_location (attr, sym, cu);
21744 }
21745 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21746 if (attr)
21747 {
21748 dwarf2_const_value (attr, sym, cu);
21749 }
f346a30d 21750
a60f3166
TT
21751 list_to_add = cu->list_in_scope;
21752 }
c906108c
SS
21753 break;
21754 case DW_TAG_unspecified_parameters:
21755 /* From varargs functions; gdb doesn't seem to have any
21756 interest in this information, so just ignore it for now.
21757 (FIXME?) */
21758 break;
34eaf542
TT
21759 case DW_TAG_template_type_param:
21760 suppress_add = 1;
21761 /* Fall through. */
c906108c 21762 case DW_TAG_class_type:
680b30c7 21763 case DW_TAG_interface_type:
c906108c
SS
21764 case DW_TAG_structure_type:
21765 case DW_TAG_union_type:
72019c9c 21766 case DW_TAG_set_type:
c906108c 21767 case DW_TAG_enumeration_type:
f1e6e072 21768 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21769 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21770
63d06c5c 21771 {
9c37b5ae 21772 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21773 really ever be static objects: otherwise, if you try
21774 to, say, break of a class's method and you're in a file
21775 which doesn't mention that class, it won't work unless
21776 the check for all static symbols in lookup_symbol_aux
21777 saves you. See the OtherFileClass tests in
21778 gdb.c++/namespace.exp. */
21779
e37fd15a 21780 if (!suppress_add)
34eaf542 21781 {
c24bdb02 21782 buildsym_compunit *builder = cu->get_builder ();
804d2729 21783 list_to_add
c24bdb02 21784 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21785 && cu->language == language_cplus
c24bdb02 21786 ? builder->get_global_symbols ()
804d2729 21787 : cu->list_in_scope);
63d06c5c 21788
64382290 21789 /* The semantics of C++ state that "struct foo {
9c37b5ae 21790 ... }" also defines a typedef for "foo". */
64382290 21791 if (cu->language == language_cplus
45280282 21792 || cu->language == language_ada
c44af4eb
TT
21793 || cu->language == language_d
21794 || cu->language == language_rust)
64382290
TT
21795 {
21796 /* The symbol's name is already allocated along
21797 with this objfile, so we don't need to
21798 duplicate it for the type. */
21799 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21800 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21801 }
63d06c5c
DC
21802 }
21803 }
c906108c
SS
21804 break;
21805 case DW_TAG_typedef:
f1e6e072 21806 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21807 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21808 list_to_add = cu->list_in_scope;
63d06c5c 21809 break;
c906108c 21810 case DW_TAG_base_type:
a02abb62 21811 case DW_TAG_subrange_type:
f1e6e072 21812 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21813 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21814 list_to_add = cu->list_in_scope;
c906108c
SS
21815 break;
21816 case DW_TAG_enumerator:
e142c38c 21817 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21818 if (attr)
21819 {
e7c27a73 21820 dwarf2_const_value (attr, sym, cu);
c906108c 21821 }
63d06c5c
DC
21822 {
21823 /* NOTE: carlton/2003-11-10: See comment above in the
21824 DW_TAG_class_type, etc. block. */
21825
804d2729 21826 list_to_add
c24bdb02 21827 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21828 && cu->language == language_cplus
c24bdb02 21829 ? cu->get_builder ()->get_global_symbols ()
804d2729 21830 : cu->list_in_scope);
63d06c5c 21831 }
c906108c 21832 break;
74921315 21833 case DW_TAG_imported_declaration:
5c4e30ca 21834 case DW_TAG_namespace:
f1e6e072 21835 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21836 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21837 break;
530e8392
KB
21838 case DW_TAG_module:
21839 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21840 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21841 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21842 break;
4357ac6c 21843 case DW_TAG_common_block:
f1e6e072 21844 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21845 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21846 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21847 break;
c906108c
SS
21848 default:
21849 /* Not a tag we recognize. Hopefully we aren't processing
21850 trash data, but since we must specifically ignore things
21851 we don't recognize, there is nothing else we should do at
0963b4bd 21852 this point. */
b98664d3 21853 complaint (_("unsupported tag: '%s'"),
4d3c2250 21854 dwarf_tag_name (die->tag));
c906108c
SS
21855 break;
21856 }
df8a16a1 21857
e37fd15a
SW
21858 if (suppress_add)
21859 {
21860 sym->hash_next = objfile->template_symbols;
21861 objfile->template_symbols = sym;
21862 list_to_add = NULL;
21863 }
21864
21865 if (list_to_add != NULL)
d3cb6808 21866 add_symbol_to_list (sym, list_to_add);
e37fd15a 21867
df8a16a1
DJ
21868 /* For the benefit of old versions of GCC, check for anonymous
21869 namespaces based on the demangled name. */
4d4ec4e5 21870 if (!cu->processing_has_namespace_info
94af9270 21871 && cu->language == language_cplus)
c24bdb02 21872 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21873 }
21874 return (sym);
21875}
21876
98bfdba5
PA
21877/* Given an attr with a DW_FORM_dataN value in host byte order,
21878 zero-extend it as appropriate for the symbol's type. The DWARF
21879 standard (v4) is not entirely clear about the meaning of using
21880 DW_FORM_dataN for a constant with a signed type, where the type is
21881 wider than the data. The conclusion of a discussion on the DWARF
21882 list was that this is unspecified. We choose to always zero-extend
21883 because that is the interpretation long in use by GCC. */
c906108c 21884
98bfdba5 21885static gdb_byte *
ff39bb5e 21886dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21887 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21888{
518817b3 21889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21890 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21891 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21892 LONGEST l = DW_UNSND (attr);
21893
21894 if (bits < sizeof (*value) * 8)
21895 {
21896 l &= ((LONGEST) 1 << bits) - 1;
21897 *value = l;
21898 }
21899 else if (bits == sizeof (*value) * 8)
21900 *value = l;
21901 else
21902 {
224c3ddb 21903 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21904 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21905 return bytes;
21906 }
21907
21908 return NULL;
21909}
21910
21911/* Read a constant value from an attribute. Either set *VALUE, or if
21912 the value does not fit in *VALUE, set *BYTES - either already
21913 allocated on the objfile obstack, or newly allocated on OBSTACK,
21914 or, set *BATON, if we translated the constant to a location
21915 expression. */
21916
21917static void
ff39bb5e 21918dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21919 const char *name, struct obstack *obstack,
21920 struct dwarf2_cu *cu,
d521ce57 21921 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21922 struct dwarf2_locexpr_baton **baton)
21923{
518817b3 21924 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21925 struct comp_unit_head *cu_header = &cu->header;
c906108c 21926 struct dwarf_block *blk;
98bfdba5
PA
21927 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21928 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21929
21930 *value = 0;
21931 *bytes = NULL;
21932 *baton = NULL;
c906108c
SS
21933
21934 switch (attr->form)
21935 {
21936 case DW_FORM_addr:
336d760d 21937 case DW_FORM_addrx:
3019eac3 21938 case DW_FORM_GNU_addr_index:
ac56253d 21939 {
ac56253d
TT
21940 gdb_byte *data;
21941
98bfdba5
PA
21942 if (TYPE_LENGTH (type) != cu_header->addr_size)
21943 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21944 cu_header->addr_size,
98bfdba5 21945 TYPE_LENGTH (type));
ac56253d
TT
21946 /* Symbols of this form are reasonably rare, so we just
21947 piggyback on the existing location code rather than writing
21948 a new implementation of symbol_computed_ops. */
8d749320 21949 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21950 (*baton)->per_cu = cu->per_cu;
21951 gdb_assert ((*baton)->per_cu);
ac56253d 21952
98bfdba5 21953 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21954 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21955 (*baton)->data = data;
ac56253d
TT
21956
21957 data[0] = DW_OP_addr;
21958 store_unsigned_integer (&data[1], cu_header->addr_size,
21959 byte_order, DW_ADDR (attr));
21960 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21961 }
c906108c 21962 break;
4ac36638 21963 case DW_FORM_string:
93b5768b 21964 case DW_FORM_strp:
cf532bd1 21965 case DW_FORM_strx:
3019eac3 21966 case DW_FORM_GNU_str_index:
36586728 21967 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21968 /* DW_STRING is already allocated on the objfile obstack, point
21969 directly to it. */
d521ce57 21970 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21971 break;
c906108c
SS
21972 case DW_FORM_block1:
21973 case DW_FORM_block2:
21974 case DW_FORM_block4:
21975 case DW_FORM_block:
2dc7f7b3 21976 case DW_FORM_exprloc:
0224619f 21977 case DW_FORM_data16:
c906108c 21978 blk = DW_BLOCK (attr);
98bfdba5
PA
21979 if (TYPE_LENGTH (type) != blk->size)
21980 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21981 TYPE_LENGTH (type));
21982 *bytes = blk->data;
c906108c 21983 break;
2df3850c
JM
21984
21985 /* The DW_AT_const_value attributes are supposed to carry the
21986 symbol's value "represented as it would be on the target
21987 architecture." By the time we get here, it's already been
21988 converted to host endianness, so we just need to sign- or
21989 zero-extend it as appropriate. */
21990 case DW_FORM_data1:
3aef2284 21991 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21992 break;
c906108c 21993 case DW_FORM_data2:
3aef2284 21994 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21995 break;
c906108c 21996 case DW_FORM_data4:
3aef2284 21997 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21998 break;
c906108c 21999 case DW_FORM_data8:
3aef2284 22000 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22001 break;
22002
c906108c 22003 case DW_FORM_sdata:
663c44ac 22004 case DW_FORM_implicit_const:
98bfdba5 22005 *value = DW_SND (attr);
2df3850c
JM
22006 break;
22007
c906108c 22008 case DW_FORM_udata:
98bfdba5 22009 *value = DW_UNSND (attr);
c906108c 22010 break;
2df3850c 22011
c906108c 22012 default:
b98664d3 22013 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22014 dwarf_form_name (attr->form));
98bfdba5 22015 *value = 0;
c906108c
SS
22016 break;
22017 }
22018}
22019
2df3850c 22020
98bfdba5
PA
22021/* Copy constant value from an attribute to a symbol. */
22022
2df3850c 22023static void
ff39bb5e 22024dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22025 struct dwarf2_cu *cu)
2df3850c 22026{
518817b3 22027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22028 LONGEST value;
d521ce57 22029 const gdb_byte *bytes;
98bfdba5 22030 struct dwarf2_locexpr_baton *baton;
2df3850c 22031
98bfdba5
PA
22032 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22033 SYMBOL_PRINT_NAME (sym),
22034 &objfile->objfile_obstack, cu,
22035 &value, &bytes, &baton);
2df3850c 22036
98bfdba5
PA
22037 if (baton != NULL)
22038 {
98bfdba5 22039 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22040 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22041 }
22042 else if (bytes != NULL)
22043 {
22044 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22045 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22046 }
22047 else
22048 {
22049 SYMBOL_VALUE (sym) = value;
f1e6e072 22050 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22051 }
2df3850c
JM
22052}
22053
c906108c
SS
22054/* Return the type of the die in question using its DW_AT_type attribute. */
22055
22056static struct type *
e7c27a73 22057die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22058{
c906108c 22059 struct attribute *type_attr;
c906108c 22060
e142c38c 22061 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22062 if (!type_attr)
22063 {
518817b3 22064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22065 /* A missing DW_AT_type represents a void type. */
518817b3 22066 return objfile_type (objfile)->builtin_void;
c906108c 22067 }
348e048f 22068
673bfd45 22069 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22070}
22071
b4ba55a1
JB
22072/* True iff CU's producer generates GNAT Ada auxiliary information
22073 that allows to find parallel types through that information instead
22074 of having to do expensive parallel lookups by type name. */
22075
22076static int
22077need_gnat_info (struct dwarf2_cu *cu)
22078{
de4cb04a
JB
22079 /* Assume that the Ada compiler was GNAT, which always produces
22080 the auxiliary information. */
22081 return (cu->language == language_ada);
b4ba55a1
JB
22082}
22083
b4ba55a1
JB
22084/* Return the auxiliary type of the die in question using its
22085 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22086 attribute is not present. */
22087
22088static struct type *
22089die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22090{
b4ba55a1 22091 struct attribute *type_attr;
b4ba55a1
JB
22092
22093 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22094 if (!type_attr)
22095 return NULL;
22096
673bfd45 22097 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22098}
22099
22100/* If DIE has a descriptive_type attribute, then set the TYPE's
22101 descriptive type accordingly. */
22102
22103static void
22104set_descriptive_type (struct type *type, struct die_info *die,
22105 struct dwarf2_cu *cu)
22106{
22107 struct type *descriptive_type = die_descriptive_type (die, cu);
22108
22109 if (descriptive_type)
22110 {
22111 ALLOCATE_GNAT_AUX_TYPE (type);
22112 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22113 }
22114}
22115
c906108c
SS
22116/* Return the containing type of the die in question using its
22117 DW_AT_containing_type attribute. */
22118
22119static struct type *
e7c27a73 22120die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22121{
c906108c 22122 struct attribute *type_attr;
518817b3 22123 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22124
e142c38c 22125 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22126 if (!type_attr)
22127 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22128 "[in module %s]"), objfile_name (objfile));
33ac96f0 22129
673bfd45 22130 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22131}
22132
ac9ec31b
DE
22133/* Return an error marker type to use for the ill formed type in DIE/CU. */
22134
22135static struct type *
22136build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22137{
518817b3
SM
22138 struct dwarf2_per_objfile *dwarf2_per_objfile
22139 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22140 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22141 char *saved;
ac9ec31b 22142
528e1572
SM
22143 std::string message
22144 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22145 objfile_name (objfile),
22146 sect_offset_str (cu->header.sect_off),
22147 sect_offset_str (die->sect_off));
224c3ddb 22148 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
528e1572 22149 message.c_str (), message.length ());
ac9ec31b 22150
19f392bc 22151 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22152}
22153
673bfd45 22154/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22155 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22156 DW_AT_containing_type.
673bfd45
DE
22157 If there is no type substitute an error marker. */
22158
c906108c 22159static struct type *
ff39bb5e 22160lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22161 struct dwarf2_cu *cu)
c906108c 22162{
518817b3
SM
22163 struct dwarf2_per_objfile *dwarf2_per_objfile
22164 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22165 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22166 struct type *this_type;
22167
ac9ec31b
DE
22168 gdb_assert (attr->name == DW_AT_type
22169 || attr->name == DW_AT_GNAT_descriptive_type
22170 || attr->name == DW_AT_containing_type);
22171
673bfd45
DE
22172 /* First see if we have it cached. */
22173
36586728
TT
22174 if (attr->form == DW_FORM_GNU_ref_alt)
22175 {
22176 struct dwarf2_per_cu_data *per_cu;
9c541725 22177 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22178
ed2dc618
SM
22179 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22180 dwarf2_per_objfile);
9c541725 22181 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22182 }
7771576e 22183 else if (attr_form_is_ref (attr))
673bfd45 22184 {
9c541725 22185 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22186
9c541725 22187 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22188 }
55f1336d 22189 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22190 {
ac9ec31b 22191 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22192
ac9ec31b 22193 return get_signatured_type (die, signature, cu);
673bfd45
DE
22194 }
22195 else
22196 {
b98664d3 22197 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22198 " at %s [in module %s]"),
22199 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22200 objfile_name (objfile));
ac9ec31b 22201 return build_error_marker_type (cu, die);
673bfd45
DE
22202 }
22203
22204 /* If not cached we need to read it in. */
22205
22206 if (this_type == NULL)
22207 {
ac9ec31b 22208 struct die_info *type_die = NULL;
673bfd45
DE
22209 struct dwarf2_cu *type_cu = cu;
22210
7771576e 22211 if (attr_form_is_ref (attr))
ac9ec31b
DE
22212 type_die = follow_die_ref (die, attr, &type_cu);
22213 if (type_die == NULL)
22214 return build_error_marker_type (cu, die);
22215 /* If we find the type now, it's probably because the type came
3019eac3
DE
22216 from an inter-CU reference and the type's CU got expanded before
22217 ours. */
ac9ec31b 22218 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22219 }
22220
22221 /* If we still don't have a type use an error marker. */
22222
22223 if (this_type == NULL)
ac9ec31b 22224 return build_error_marker_type (cu, die);
673bfd45 22225
f792889a 22226 return this_type;
c906108c
SS
22227}
22228
673bfd45
DE
22229/* Return the type in DIE, CU.
22230 Returns NULL for invalid types.
22231
02142a6c 22232 This first does a lookup in die_type_hash,
673bfd45
DE
22233 and only reads the die in if necessary.
22234
22235 NOTE: This can be called when reading in partial or full symbols. */
22236
f792889a 22237static struct type *
e7c27a73 22238read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22239{
f792889a
DJ
22240 struct type *this_type;
22241
22242 this_type = get_die_type (die, cu);
22243 if (this_type)
22244 return this_type;
22245
673bfd45
DE
22246 return read_type_die_1 (die, cu);
22247}
22248
22249/* Read the type in DIE, CU.
22250 Returns NULL for invalid types. */
22251
22252static struct type *
22253read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22254{
22255 struct type *this_type = NULL;
22256
c906108c
SS
22257 switch (die->tag)
22258 {
22259 case DW_TAG_class_type:
680b30c7 22260 case DW_TAG_interface_type:
c906108c
SS
22261 case DW_TAG_structure_type:
22262 case DW_TAG_union_type:
f792889a 22263 this_type = read_structure_type (die, cu);
c906108c
SS
22264 break;
22265 case DW_TAG_enumeration_type:
f792889a 22266 this_type = read_enumeration_type (die, cu);
c906108c
SS
22267 break;
22268 case DW_TAG_subprogram:
22269 case DW_TAG_subroutine_type:
edb3359d 22270 case DW_TAG_inlined_subroutine:
f792889a 22271 this_type = read_subroutine_type (die, cu);
c906108c
SS
22272 break;
22273 case DW_TAG_array_type:
f792889a 22274 this_type = read_array_type (die, cu);
c906108c 22275 break;
72019c9c 22276 case DW_TAG_set_type:
f792889a 22277 this_type = read_set_type (die, cu);
72019c9c 22278 break;
c906108c 22279 case DW_TAG_pointer_type:
f792889a 22280 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22281 break;
22282 case DW_TAG_ptr_to_member_type:
f792889a 22283 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22284 break;
22285 case DW_TAG_reference_type:
4297a3f0
AV
22286 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22287 break;
22288 case DW_TAG_rvalue_reference_type:
22289 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22290 break;
22291 case DW_TAG_const_type:
f792889a 22292 this_type = read_tag_const_type (die, cu);
c906108c
SS
22293 break;
22294 case DW_TAG_volatile_type:
f792889a 22295 this_type = read_tag_volatile_type (die, cu);
c906108c 22296 break;
06d66ee9
TT
22297 case DW_TAG_restrict_type:
22298 this_type = read_tag_restrict_type (die, cu);
22299 break;
c906108c 22300 case DW_TAG_string_type:
f792889a 22301 this_type = read_tag_string_type (die, cu);
c906108c
SS
22302 break;
22303 case DW_TAG_typedef:
f792889a 22304 this_type = read_typedef (die, cu);
c906108c 22305 break;
a02abb62 22306 case DW_TAG_subrange_type:
f792889a 22307 this_type = read_subrange_type (die, cu);
a02abb62 22308 break;
c906108c 22309 case DW_TAG_base_type:
f792889a 22310 this_type = read_base_type (die, cu);
c906108c 22311 break;
81a17f79 22312 case DW_TAG_unspecified_type:
f792889a 22313 this_type = read_unspecified_type (die, cu);
81a17f79 22314 break;
0114d602
DJ
22315 case DW_TAG_namespace:
22316 this_type = read_namespace_type (die, cu);
22317 break;
f55ee35c
JK
22318 case DW_TAG_module:
22319 this_type = read_module_type (die, cu);
22320 break;
a2c2acaf
MW
22321 case DW_TAG_atomic_type:
22322 this_type = read_tag_atomic_type (die, cu);
22323 break;
c906108c 22324 default:
b98664d3 22325 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22326 dwarf_tag_name (die->tag));
c906108c
SS
22327 break;
22328 }
63d06c5c 22329
f792889a 22330 return this_type;
63d06c5c
DC
22331}
22332
abc72ce4
DE
22333/* See if we can figure out if the class lives in a namespace. We do
22334 this by looking for a member function; its demangled name will
22335 contain namespace info, if there is any.
22336 Return the computed name or NULL.
22337 Space for the result is allocated on the objfile's obstack.
22338 This is the full-die version of guess_partial_die_structure_name.
22339 In this case we know DIE has no useful parent. */
22340
22341static char *
22342guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22343{
22344 struct die_info *spec_die;
22345 struct dwarf2_cu *spec_cu;
22346 struct die_info *child;
518817b3 22347 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22348
22349 spec_cu = cu;
22350 spec_die = die_specification (die, &spec_cu);
22351 if (spec_die != NULL)
22352 {
22353 die = spec_die;
22354 cu = spec_cu;
22355 }
22356
22357 for (child = die->child;
22358 child != NULL;
22359 child = child->sibling)
22360 {
22361 if (child->tag == DW_TAG_subprogram)
22362 {
73b9be8b 22363 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22364
7d45c7c3 22365 if (linkage_name != NULL)
abc72ce4
DE
22366 {
22367 char *actual_name
22368 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22369 linkage_name);
abc72ce4
DE
22370 char *name = NULL;
22371
22372 if (actual_name != NULL)
22373 {
15d034d0 22374 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22375
22376 if (die_name != NULL
22377 && strcmp (die_name, actual_name) != 0)
22378 {
22379 /* Strip off the class name from the full name.
22380 We want the prefix. */
22381 int die_name_len = strlen (die_name);
22382 int actual_name_len = strlen (actual_name);
22383
22384 /* Test for '::' as a sanity check. */
22385 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22386 && actual_name[actual_name_len
22387 - die_name_len - 1] == ':')
224c3ddb 22388 name = (char *) obstack_copy0 (
e3b94546 22389 &objfile->per_bfd->storage_obstack,
224c3ddb 22390 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22391 }
22392 }
22393 xfree (actual_name);
22394 return name;
22395 }
22396 }
22397 }
22398
22399 return NULL;
22400}
22401
96408a79
SA
22402/* GCC might emit a nameless typedef that has a linkage name. Determine the
22403 prefix part in such case. See
22404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22405
a121b7c1 22406static const char *
96408a79
SA
22407anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22408{
22409 struct attribute *attr;
e6a959d6 22410 const char *base;
96408a79
SA
22411
22412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22414 return NULL;
22415
7d45c7c3 22416 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22417 return NULL;
22418
73b9be8b 22419 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22420 if (attr == NULL || DW_STRING (attr) == NULL)
22421 return NULL;
22422
22423 /* dwarf2_name had to be already called. */
22424 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22425
22426 /* Strip the base name, keep any leading namespaces/classes. */
22427 base = strrchr (DW_STRING (attr), ':');
22428 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22429 return "";
22430
518817b3 22431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22432 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22433 DW_STRING (attr),
22434 &base[-1] - DW_STRING (attr));
96408a79
SA
22435}
22436
fdde2d81 22437/* Return the name of the namespace/class that DIE is defined within,
0114d602 22438 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22439
0114d602
DJ
22440 For example, if we're within the method foo() in the following
22441 code:
22442
22443 namespace N {
22444 class C {
22445 void foo () {
22446 }
22447 };
22448 }
22449
22450 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22451
0d5cff50 22452static const char *
e142c38c 22453determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22454{
518817b3
SM
22455 struct dwarf2_per_objfile *dwarf2_per_objfile
22456 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22457 struct die_info *parent, *spec_die;
22458 struct dwarf2_cu *spec_cu;
22459 struct type *parent_type;
a121b7c1 22460 const char *retval;
63d06c5c 22461
9c37b5ae 22462 if (cu->language != language_cplus
c44af4eb
TT
22463 && cu->language != language_fortran && cu->language != language_d
22464 && cu->language != language_rust)
0114d602
DJ
22465 return "";
22466
96408a79
SA
22467 retval = anonymous_struct_prefix (die, cu);
22468 if (retval)
22469 return retval;
22470
0114d602
DJ
22471 /* We have to be careful in the presence of DW_AT_specification.
22472 For example, with GCC 3.4, given the code
22473
22474 namespace N {
22475 void foo() {
22476 // Definition of N::foo.
22477 }
22478 }
22479
22480 then we'll have a tree of DIEs like this:
22481
22482 1: DW_TAG_compile_unit
22483 2: DW_TAG_namespace // N
22484 3: DW_TAG_subprogram // declaration of N::foo
22485 4: DW_TAG_subprogram // definition of N::foo
22486 DW_AT_specification // refers to die #3
22487
22488 Thus, when processing die #4, we have to pretend that we're in
22489 the context of its DW_AT_specification, namely the contex of die
22490 #3. */
22491 spec_cu = cu;
22492 spec_die = die_specification (die, &spec_cu);
22493 if (spec_die == NULL)
22494 parent = die->parent;
22495 else
63d06c5c 22496 {
0114d602
DJ
22497 parent = spec_die->parent;
22498 cu = spec_cu;
63d06c5c 22499 }
0114d602
DJ
22500
22501 if (parent == NULL)
22502 return "";
98bfdba5
PA
22503 else if (parent->building_fullname)
22504 {
22505 const char *name;
22506 const char *parent_name;
22507
22508 /* It has been seen on RealView 2.2 built binaries,
22509 DW_TAG_template_type_param types actually _defined_ as
22510 children of the parent class:
22511
22512 enum E {};
22513 template class <class Enum> Class{};
22514 Class<enum E> class_e;
22515
22516 1: DW_TAG_class_type (Class)
22517 2: DW_TAG_enumeration_type (E)
22518 3: DW_TAG_enumerator (enum1:0)
22519 3: DW_TAG_enumerator (enum2:1)
22520 ...
22521 2: DW_TAG_template_type_param
22522 DW_AT_type DW_FORM_ref_udata (E)
22523
22524 Besides being broken debug info, it can put GDB into an
22525 infinite loop. Consider:
22526
22527 When we're building the full name for Class<E>, we'll start
22528 at Class, and go look over its template type parameters,
22529 finding E. We'll then try to build the full name of E, and
22530 reach here. We're now trying to build the full name of E,
22531 and look over the parent DIE for containing scope. In the
22532 broken case, if we followed the parent DIE of E, we'd again
22533 find Class, and once again go look at its template type
22534 arguments, etc., etc. Simply don't consider such parent die
22535 as source-level parent of this die (it can't be, the language
22536 doesn't allow it), and break the loop here. */
22537 name = dwarf2_name (die, cu);
22538 parent_name = dwarf2_name (parent, cu);
b98664d3 22539 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22540 name ? name : "<unknown>",
22541 parent_name ? parent_name : "<unknown>");
22542 return "";
22543 }
63d06c5c 22544 else
0114d602
DJ
22545 switch (parent->tag)
22546 {
63d06c5c 22547 case DW_TAG_namespace:
0114d602 22548 parent_type = read_type_die (parent, cu);
acebe513
UW
22549 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22550 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22551 Work around this problem here. */
22552 if (cu->language == language_cplus
e86ca25f 22553 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22554 return "";
0114d602 22555 /* We give a name to even anonymous namespaces. */
e86ca25f 22556 return TYPE_NAME (parent_type);
63d06c5c 22557 case DW_TAG_class_type:
680b30c7 22558 case DW_TAG_interface_type:
63d06c5c 22559 case DW_TAG_structure_type:
0114d602 22560 case DW_TAG_union_type:
f55ee35c 22561 case DW_TAG_module:
0114d602 22562 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22563 if (TYPE_NAME (parent_type) != NULL)
22564 return TYPE_NAME (parent_type);
0114d602
DJ
22565 else
22566 /* An anonymous structure is only allowed non-static data
22567 members; no typedefs, no member functions, et cetera.
22568 So it does not need a prefix. */
22569 return "";
abc72ce4 22570 case DW_TAG_compile_unit:
95554aad 22571 case DW_TAG_partial_unit:
abc72ce4
DE
22572 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22573 if (cu->language == language_cplus
8b70b953 22574 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22575 && die->child != NULL
22576 && (die->tag == DW_TAG_class_type
22577 || die->tag == DW_TAG_structure_type
22578 || die->tag == DW_TAG_union_type))
22579 {
22580 char *name = guess_full_die_structure_name (die, cu);
22581 if (name != NULL)
22582 return name;
22583 }
22584 return "";
3d567982
TT
22585 case DW_TAG_enumeration_type:
22586 parent_type = read_type_die (parent, cu);
22587 if (TYPE_DECLARED_CLASS (parent_type))
22588 {
e86ca25f
TT
22589 if (TYPE_NAME (parent_type) != NULL)
22590 return TYPE_NAME (parent_type);
3d567982
TT
22591 return "";
22592 }
22593 /* Fall through. */
63d06c5c 22594 default:
8176b9b8 22595 return determine_prefix (parent, cu);
63d06c5c 22596 }
63d06c5c
DC
22597}
22598
3e43a32a
MS
22599/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22600 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22601 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22602 an obconcat, otherwise allocate storage for the result. The CU argument is
22603 used to determine the language and hence, the appropriate separator. */
987504bb 22604
f55ee35c 22605#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22606
22607static char *
f55ee35c
JK
22608typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22609 int physname, struct dwarf2_cu *cu)
63d06c5c 22610{
f55ee35c 22611 const char *lead = "";
5c315b68 22612 const char *sep;
63d06c5c 22613
3e43a32a
MS
22614 if (suffix == NULL || suffix[0] == '\0'
22615 || prefix == NULL || prefix[0] == '\0')
987504bb 22616 sep = "";
45280282
IB
22617 else if (cu->language == language_d)
22618 {
22619 /* For D, the 'main' function could be defined in any module, but it
22620 should never be prefixed. */
22621 if (strcmp (suffix, "D main") == 0)
22622 {
22623 prefix = "";
22624 sep = "";
22625 }
22626 else
22627 sep = ".";
22628 }
f55ee35c
JK
22629 else if (cu->language == language_fortran && physname)
22630 {
22631 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22632 DW_AT_MIPS_linkage_name is preferred and used instead. */
22633
22634 lead = "__";
22635 sep = "_MOD_";
22636 }
987504bb
JJ
22637 else
22638 sep = "::";
63d06c5c 22639
6dd47d34
DE
22640 if (prefix == NULL)
22641 prefix = "";
22642 if (suffix == NULL)
22643 suffix = "";
22644
987504bb
JJ
22645 if (obs == NULL)
22646 {
3e43a32a 22647 char *retval
224c3ddb
SM
22648 = ((char *)
22649 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22650
f55ee35c
JK
22651 strcpy (retval, lead);
22652 strcat (retval, prefix);
6dd47d34
DE
22653 strcat (retval, sep);
22654 strcat (retval, suffix);
63d06c5c
DC
22655 return retval;
22656 }
987504bb
JJ
22657 else
22658 {
22659 /* We have an obstack. */
f55ee35c 22660 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22661 }
63d06c5c
DC
22662}
22663
c906108c
SS
22664/* Return sibling of die, NULL if no sibling. */
22665
f9aca02d 22666static struct die_info *
fba45db2 22667sibling_die (struct die_info *die)
c906108c 22668{
639d11d3 22669 return die->sibling;
c906108c
SS
22670}
22671
71c25dea
TT
22672/* Get name of a die, return NULL if not found. */
22673
15d034d0
TT
22674static const char *
22675dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22676 struct obstack *obstack)
22677{
22678 if (name && cu->language == language_cplus)
22679 {
2f408ecb 22680 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22681
2f408ecb 22682 if (!canon_name.empty ())
71c25dea 22683 {
2f408ecb
PA
22684 if (canon_name != name)
22685 name = (const char *) obstack_copy0 (obstack,
22686 canon_name.c_str (),
22687 canon_name.length ());
71c25dea
TT
22688 }
22689 }
22690
22691 return name;
c906108c
SS
22692}
22693
96553a0c
DE
22694/* Get name of a die, return NULL if not found.
22695 Anonymous namespaces are converted to their magic string. */
9219021c 22696
15d034d0 22697static const char *
e142c38c 22698dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22699{
22700 struct attribute *attr;
518817b3 22701 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22702
e142c38c 22703 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22704 if ((!attr || !DW_STRING (attr))
96553a0c 22705 && die->tag != DW_TAG_namespace
53832f31
TT
22706 && die->tag != DW_TAG_class_type
22707 && die->tag != DW_TAG_interface_type
22708 && die->tag != DW_TAG_structure_type
22709 && die->tag != DW_TAG_union_type)
71c25dea
TT
22710 return NULL;
22711
22712 switch (die->tag)
22713 {
22714 case DW_TAG_compile_unit:
95554aad 22715 case DW_TAG_partial_unit:
71c25dea
TT
22716 /* Compilation units have a DW_AT_name that is a filename, not
22717 a source language identifier. */
22718 case DW_TAG_enumeration_type:
22719 case DW_TAG_enumerator:
22720 /* These tags always have simple identifiers already; no need
22721 to canonicalize them. */
22722 return DW_STRING (attr);
907af001 22723
96553a0c
DE
22724 case DW_TAG_namespace:
22725 if (attr != NULL && DW_STRING (attr) != NULL)
22726 return DW_STRING (attr);
22727 return CP_ANONYMOUS_NAMESPACE_STR;
22728
907af001
UW
22729 case DW_TAG_class_type:
22730 case DW_TAG_interface_type:
22731 case DW_TAG_structure_type:
22732 case DW_TAG_union_type:
22733 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22734 structures or unions. These were of the form "._%d" in GCC 4.1,
22735 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22736 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22737 if (attr && DW_STRING (attr)
61012eef
GB
22738 && (startswith (DW_STRING (attr), "._")
22739 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22740 return NULL;
53832f31
TT
22741
22742 /* GCC might emit a nameless typedef that has a linkage name. See
22743 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22744 if (!attr || DW_STRING (attr) == NULL)
22745 {
df5c6c50 22746 char *demangled = NULL;
53832f31 22747
73b9be8b 22748 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22749 if (attr == NULL || DW_STRING (attr) == NULL)
22750 return NULL;
22751
df5c6c50
JK
22752 /* Avoid demangling DW_STRING (attr) the second time on a second
22753 call for the same DIE. */
22754 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22755 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22756
22757 if (demangled)
22758 {
e6a959d6 22759 const char *base;
96408a79 22760
53832f31 22761 /* FIXME: we already did this for the partial symbol... */
34a68019 22762 DW_STRING (attr)
224c3ddb 22763 = ((const char *)
e3b94546 22764 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22765 demangled, strlen (demangled)));
53832f31
TT
22766 DW_STRING_IS_CANONICAL (attr) = 1;
22767 xfree (demangled);
96408a79
SA
22768
22769 /* Strip any leading namespaces/classes, keep only the base name.
22770 DW_AT_name for named DIEs does not contain the prefixes. */
22771 base = strrchr (DW_STRING (attr), ':');
22772 if (base && base > DW_STRING (attr) && base[-1] == ':')
22773 return &base[1];
22774 else
22775 return DW_STRING (attr);
53832f31
TT
22776 }
22777 }
907af001
UW
22778 break;
22779
71c25dea 22780 default:
907af001
UW
22781 break;
22782 }
22783
22784 if (!DW_STRING_IS_CANONICAL (attr))
22785 {
22786 DW_STRING (attr)
22787 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22788 &objfile->per_bfd->storage_obstack);
907af001 22789 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22790 }
907af001 22791 return DW_STRING (attr);
9219021c
DC
22792}
22793
22794/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22795 is none. *EXT_CU is the CU containing DIE on input, and the CU
22796 containing the return value on output. */
9219021c
DC
22797
22798static struct die_info *
f2f0e013 22799dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22800{
22801 struct attribute *attr;
9219021c 22802
f2f0e013 22803 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22804 if (attr == NULL)
22805 return NULL;
22806
f2f0e013 22807 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22808}
22809
fa9c3fa0
TT
22810/* A convenience function that returns an "unknown" DWARF name,
22811 including the value of V. STR is the name of the entity being
22812 printed, e.g., "TAG". */
22813
22814static const char *
22815dwarf_unknown (const char *str, unsigned v)
22816{
22817 char *cell = get_print_cell ();
22818 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22819 return cell;
22820}
22821
c906108c
SS
22822/* Convert a DIE tag into its string name. */
22823
f39c6ffd 22824static const char *
aa1ee363 22825dwarf_tag_name (unsigned tag)
c906108c 22826{
f39c6ffd
TT
22827 const char *name = get_DW_TAG_name (tag);
22828
22829 if (name == NULL)
fa9c3fa0 22830 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22831
22832 return name;
c906108c
SS
22833}
22834
22835/* Convert a DWARF attribute code into its string name. */
22836
f39c6ffd 22837static const char *
aa1ee363 22838dwarf_attr_name (unsigned attr)
c906108c 22839{
f39c6ffd
TT
22840 const char *name;
22841
c764a876 22842#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22843 if (attr == DW_AT_MIPS_fde)
22844 return "DW_AT_MIPS_fde";
22845#else
22846 if (attr == DW_AT_HP_block_index)
22847 return "DW_AT_HP_block_index";
c764a876 22848#endif
f39c6ffd
TT
22849
22850 name = get_DW_AT_name (attr);
22851
22852 if (name == NULL)
fa9c3fa0 22853 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22854
22855 return name;
c906108c
SS
22856}
22857
22858/* Convert a DWARF value form code into its string name. */
22859
f39c6ffd 22860static const char *
aa1ee363 22861dwarf_form_name (unsigned form)
c906108c 22862{
f39c6ffd
TT
22863 const char *name = get_DW_FORM_name (form);
22864
22865 if (name == NULL)
fa9c3fa0 22866 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22867
22868 return name;
c906108c
SS
22869}
22870
a121b7c1 22871static const char *
fba45db2 22872dwarf_bool_name (unsigned mybool)
c906108c
SS
22873{
22874 if (mybool)
22875 return "TRUE";
22876 else
22877 return "FALSE";
22878}
22879
22880/* Convert a DWARF type code into its string name. */
22881
f39c6ffd 22882static const char *
aa1ee363 22883dwarf_type_encoding_name (unsigned enc)
c906108c 22884{
f39c6ffd 22885 const char *name = get_DW_ATE_name (enc);
c906108c 22886
f39c6ffd 22887 if (name == NULL)
fa9c3fa0 22888 return dwarf_unknown ("ATE", enc);
c906108c 22889
f39c6ffd 22890 return name;
c906108c 22891}
c906108c 22892
f9aca02d 22893static void
d97bc12b 22894dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22895{
22896 unsigned int i;
22897
d97bc12b 22898 print_spaces (indent, f);
9d8780f0 22899 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22900 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22901 sect_offset_str (die->sect_off));
d97bc12b
DE
22902
22903 if (die->parent != NULL)
22904 {
22905 print_spaces (indent, f);
9d8780f0
SM
22906 fprintf_unfiltered (f, " parent at offset: %s\n",
22907 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22908 }
22909
22910 print_spaces (indent, f);
22911 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22912 dwarf_bool_name (die->child != NULL));
c906108c 22913
d97bc12b
DE
22914 print_spaces (indent, f);
22915 fprintf_unfiltered (f, " attributes:\n");
22916
c906108c
SS
22917 for (i = 0; i < die->num_attrs; ++i)
22918 {
d97bc12b
DE
22919 print_spaces (indent, f);
22920 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22921 dwarf_attr_name (die->attrs[i].name),
22922 dwarf_form_name (die->attrs[i].form));
d97bc12b 22923
c906108c
SS
22924 switch (die->attrs[i].form)
22925 {
c906108c 22926 case DW_FORM_addr:
336d760d 22927 case DW_FORM_addrx:
3019eac3 22928 case DW_FORM_GNU_addr_index:
d97bc12b 22929 fprintf_unfiltered (f, "address: ");
5af949e3 22930 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22931 break;
22932 case DW_FORM_block2:
22933 case DW_FORM_block4:
22934 case DW_FORM_block:
22935 case DW_FORM_block1:
56eb65bd
SP
22936 fprintf_unfiltered (f, "block: size %s",
22937 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22938 break;
2dc7f7b3 22939 case DW_FORM_exprloc:
56eb65bd
SP
22940 fprintf_unfiltered (f, "expression: size %s",
22941 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22942 break;
0224619f
JK
22943 case DW_FORM_data16:
22944 fprintf_unfiltered (f, "constant of 16 bytes");
22945 break;
4568ecf9
DE
22946 case DW_FORM_ref_addr:
22947 fprintf_unfiltered (f, "ref address: ");
22948 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22949 break;
36586728
TT
22950 case DW_FORM_GNU_ref_alt:
22951 fprintf_unfiltered (f, "alt ref address: ");
22952 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22953 break;
10b3939b
DJ
22954 case DW_FORM_ref1:
22955 case DW_FORM_ref2:
22956 case DW_FORM_ref4:
4568ecf9
DE
22957 case DW_FORM_ref8:
22958 case DW_FORM_ref_udata:
d97bc12b 22959 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22960 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22961 break;
c906108c
SS
22962 case DW_FORM_data1:
22963 case DW_FORM_data2:
22964 case DW_FORM_data4:
ce5d95e1 22965 case DW_FORM_data8:
c906108c
SS
22966 case DW_FORM_udata:
22967 case DW_FORM_sdata:
43bbcdc2
PH
22968 fprintf_unfiltered (f, "constant: %s",
22969 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22970 break;
2dc7f7b3
TT
22971 case DW_FORM_sec_offset:
22972 fprintf_unfiltered (f, "section offset: %s",
22973 pulongest (DW_UNSND (&die->attrs[i])));
22974 break;
55f1336d 22975 case DW_FORM_ref_sig8:
ac9ec31b
DE
22976 fprintf_unfiltered (f, "signature: %s",
22977 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22978 break;
c906108c 22979 case DW_FORM_string:
4bdf3d34 22980 case DW_FORM_strp:
43988095 22981 case DW_FORM_line_strp:
cf532bd1 22982 case DW_FORM_strx:
3019eac3 22983 case DW_FORM_GNU_str_index:
36586728 22984 case DW_FORM_GNU_strp_alt:
8285870a 22985 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22986 DW_STRING (&die->attrs[i])
8285870a
JK
22987 ? DW_STRING (&die->attrs[i]) : "",
22988 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22989 break;
22990 case DW_FORM_flag:
22991 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22992 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22993 else
d97bc12b 22994 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22995 break;
2dc7f7b3
TT
22996 case DW_FORM_flag_present:
22997 fprintf_unfiltered (f, "flag: TRUE");
22998 break;
a8329558 22999 case DW_FORM_indirect:
0963b4bd
MS
23000 /* The reader will have reduced the indirect form to
23001 the "base form" so this form should not occur. */
3e43a32a
MS
23002 fprintf_unfiltered (f,
23003 "unexpected attribute form: DW_FORM_indirect");
a8329558 23004 break;
663c44ac
JK
23005 case DW_FORM_implicit_const:
23006 fprintf_unfiltered (f, "constant: %s",
23007 plongest (DW_SND (&die->attrs[i])));
23008 break;
c906108c 23009 default:
d97bc12b 23010 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23011 die->attrs[i].form);
d97bc12b 23012 break;
c906108c 23013 }
d97bc12b 23014 fprintf_unfiltered (f, "\n");
c906108c
SS
23015 }
23016}
23017
f9aca02d 23018static void
d97bc12b 23019dump_die_for_error (struct die_info *die)
c906108c 23020{
d97bc12b
DE
23021 dump_die_shallow (gdb_stderr, 0, die);
23022}
23023
23024static void
23025dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23026{
23027 int indent = level * 4;
23028
23029 gdb_assert (die != NULL);
23030
23031 if (level >= max_level)
23032 return;
23033
23034 dump_die_shallow (f, indent, die);
23035
23036 if (die->child != NULL)
c906108c 23037 {
d97bc12b
DE
23038 print_spaces (indent, f);
23039 fprintf_unfiltered (f, " Children:");
23040 if (level + 1 < max_level)
23041 {
23042 fprintf_unfiltered (f, "\n");
23043 dump_die_1 (f, level + 1, max_level, die->child);
23044 }
23045 else
23046 {
3e43a32a
MS
23047 fprintf_unfiltered (f,
23048 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23049 }
23050 }
23051
23052 if (die->sibling != NULL && level > 0)
23053 {
23054 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23055 }
23056}
23057
d97bc12b
DE
23058/* This is called from the pdie macro in gdbinit.in.
23059 It's not static so gcc will keep a copy callable from gdb. */
23060
23061void
23062dump_die (struct die_info *die, int max_level)
23063{
23064 dump_die_1 (gdb_stdlog, 0, max_level, die);
23065}
23066
f9aca02d 23067static void
51545339 23068store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23069{
51545339 23070 void **slot;
c906108c 23071
9c541725
PA
23072 slot = htab_find_slot_with_hash (cu->die_hash, die,
23073 to_underlying (die->sect_off),
b64f50a1 23074 INSERT);
51545339
DJ
23075
23076 *slot = die;
c906108c
SS
23077}
23078
b64f50a1
JK
23079/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23080 required kind. */
23081
23082static sect_offset
ff39bb5e 23083dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23084{
7771576e 23085 if (attr_form_is_ref (attr))
9c541725 23086 return (sect_offset) DW_UNSND (attr);
93311388 23087
b98664d3 23088 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23089 dwarf_form_name (attr->form));
9c541725 23090 return {};
c906108c
SS
23091}
23092
43bbcdc2
PH
23093/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23094 * the value held by the attribute is not constant. */
a02abb62 23095
43bbcdc2 23096static LONGEST
ff39bb5e 23097dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23098{
663c44ac 23099 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23100 return DW_SND (attr);
23101 else if (attr->form == DW_FORM_udata
23102 || attr->form == DW_FORM_data1
23103 || attr->form == DW_FORM_data2
23104 || attr->form == DW_FORM_data4
23105 || attr->form == DW_FORM_data8)
23106 return DW_UNSND (attr);
23107 else
23108 {
0224619f 23109 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23110 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23111 dwarf_form_name (attr->form));
23112 return default_value;
23113 }
23114}
23115
348e048f
DE
23116/* Follow reference or signature attribute ATTR of SRC_DIE.
23117 On entry *REF_CU is the CU of SRC_DIE.
23118 On exit *REF_CU is the CU of the result. */
23119
23120static struct die_info *
ff39bb5e 23121follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23122 struct dwarf2_cu **ref_cu)
23123{
23124 struct die_info *die;
23125
7771576e 23126 if (attr_form_is_ref (attr))
348e048f 23127 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23128 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23129 die = follow_die_sig (src_die, attr, ref_cu);
23130 else
23131 {
23132 dump_die_for_error (src_die);
23133 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23134 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23135 }
23136
23137 return die;
03dd20cc
DJ
23138}
23139
5c631832 23140/* Follow reference OFFSET.
673bfd45
DE
23141 On entry *REF_CU is the CU of the source die referencing OFFSET.
23142 On exit *REF_CU is the CU of the result.
23143 Returns NULL if OFFSET is invalid. */
f504f079 23144
f9aca02d 23145static struct die_info *
9c541725 23146follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23147 struct dwarf2_cu **ref_cu)
c906108c 23148{
10b3939b 23149 struct die_info temp_die;
f2f0e013 23150 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23151 struct dwarf2_per_objfile *dwarf2_per_objfile
23152 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23153
348e048f
DE
23154 gdb_assert (cu->per_cu != NULL);
23155
98bfdba5
PA
23156 target_cu = cu;
23157
3019eac3 23158 if (cu->per_cu->is_debug_types)
348e048f
DE
23159 {
23160 /* .debug_types CUs cannot reference anything outside their CU.
23161 If they need to, they have to reference a signatured type via
55f1336d 23162 DW_FORM_ref_sig8. */
9c541725 23163 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23164 return NULL;
348e048f 23165 }
36586728 23166 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23167 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23168 {
23169 struct dwarf2_per_cu_data *per_cu;
9a619af0 23170
9c541725 23171 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23172 dwarf2_per_objfile);
03dd20cc
DJ
23173
23174 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23175 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23176 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23177
10b3939b
DJ
23178 target_cu = per_cu->cu;
23179 }
98bfdba5
PA
23180 else if (cu->dies == NULL)
23181 {
23182 /* We're loading full DIEs during partial symbol reading. */
23183 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23184 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23185 }
c906108c 23186
f2f0e013 23187 *ref_cu = target_cu;
9c541725 23188 temp_die.sect_off = sect_off;
c24bdb02
KS
23189
23190 if (target_cu != cu)
23191 target_cu->ancestor = cu;
23192
9a3c8263 23193 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23194 &temp_die,
23195 to_underlying (sect_off));
5c631832 23196}
10b3939b 23197
5c631832
JK
23198/* Follow reference attribute ATTR of SRC_DIE.
23199 On entry *REF_CU is the CU of SRC_DIE.
23200 On exit *REF_CU is the CU of the result. */
23201
23202static struct die_info *
ff39bb5e 23203follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23204 struct dwarf2_cu **ref_cu)
23205{
9c541725 23206 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23207 struct dwarf2_cu *cu = *ref_cu;
23208 struct die_info *die;
23209
9c541725 23210 die = follow_die_offset (sect_off,
36586728
TT
23211 (attr->form == DW_FORM_GNU_ref_alt
23212 || cu->per_cu->is_dwz),
23213 ref_cu);
5c631832 23214 if (!die)
9d8780f0
SM
23215 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23216 "at %s [in module %s]"),
23217 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23218 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23219
5c631832
JK
23220 return die;
23221}
23222
9c541725 23223/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23224 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23225 dwarf2_locexpr_baton->data has lifetime of
23226 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23227
23228struct dwarf2_locexpr_baton
9c541725 23229dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23230 struct dwarf2_per_cu_data *per_cu,
23231 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23232 void *baton, bool resolve_abstract_p)
5c631832 23233{
918dd910 23234 struct dwarf2_cu *cu;
5c631832
JK
23235 struct die_info *die;
23236 struct attribute *attr;
23237 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23238 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23239 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23240
918dd910 23241 if (per_cu->cu == NULL)
58f0c718 23242 load_cu (per_cu, false);
918dd910 23243 cu = per_cu->cu;
cc12ce38
DE
23244 if (cu == NULL)
23245 {
23246 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23247 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23248 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23249 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23250 }
918dd910 23251
9c541725 23252 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23253 if (!die)
9d8780f0
SM
23254 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23255 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23256
23257 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65
TV
23258 if (!attr && resolve_abstract_p
23259 && (dwarf2_per_objfile->abstract_to_concrete.find (die)
23260 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23261 {
23262 CORE_ADDR pc = (*get_frame_pc) (baton);
23263
23264 for (const auto &cand : dwarf2_per_objfile->abstract_to_concrete[die])
23265 {
23266 if (!cand->parent
23267 || cand->parent->tag != DW_TAG_subprogram)
23268 continue;
23269
23270 CORE_ADDR pc_low, pc_high;
23271 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23272 if (pc_low == ((CORE_ADDR) -1)
23273 || !(pc_low <= pc && pc < pc_high))
23274 continue;
23275
23276 die = cand;
23277 attr = dwarf2_attr (die, DW_AT_location, cu);
23278 break;
23279 }
23280 }
23281
5c631832
JK
23282 if (!attr)
23283 {
e103e986
JK
23284 /* DWARF: "If there is no such attribute, then there is no effect.".
23285 DATA is ignored if SIZE is 0. */
5c631832 23286
e103e986 23287 retval.data = NULL;
5c631832
JK
23288 retval.size = 0;
23289 }
8cf6f0b1
TT
23290 else if (attr_form_is_section_offset (attr))
23291 {
23292 struct dwarf2_loclist_baton loclist_baton;
23293 CORE_ADDR pc = (*get_frame_pc) (baton);
23294 size_t size;
23295
23296 fill_in_loclist_baton (cu, &loclist_baton, attr);
23297
23298 retval.data = dwarf2_find_location_expression (&loclist_baton,
23299 &size, pc);
23300 retval.size = size;
23301 }
5c631832
JK
23302 else
23303 {
23304 if (!attr_form_is_block (attr))
9d8780f0 23305 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23306 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23307 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23308
23309 retval.data = DW_BLOCK (attr)->data;
23310 retval.size = DW_BLOCK (attr)->size;
23311 }
23312 retval.per_cu = cu->per_cu;
918dd910 23313
ed2dc618 23314 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23315
5c631832 23316 return retval;
348e048f
DE
23317}
23318
8b9737bf
TT
23319/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23320 offset. */
23321
23322struct dwarf2_locexpr_baton
23323dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23324 struct dwarf2_per_cu_data *per_cu,
23325 CORE_ADDR (*get_frame_pc) (void *baton),
23326 void *baton)
23327{
9c541725 23328 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23329
9c541725 23330 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23331}
23332
b6807d98
TT
23333/* Write a constant of a given type as target-ordered bytes into
23334 OBSTACK. */
23335
23336static const gdb_byte *
23337write_constant_as_bytes (struct obstack *obstack,
23338 enum bfd_endian byte_order,
23339 struct type *type,
23340 ULONGEST value,
23341 LONGEST *len)
23342{
23343 gdb_byte *result;
23344
23345 *len = TYPE_LENGTH (type);
224c3ddb 23346 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23347 store_unsigned_integer (result, *len, byte_order, value);
23348
23349 return result;
23350}
23351
23352/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23353 pointer to the constant bytes and set LEN to the length of the
23354 data. If memory is needed, allocate it on OBSTACK. If the DIE
23355 does not have a DW_AT_const_value, return NULL. */
23356
23357const gdb_byte *
9c541725 23358dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23359 struct dwarf2_per_cu_data *per_cu,
23360 struct obstack *obstack,
23361 LONGEST *len)
23362{
23363 struct dwarf2_cu *cu;
23364 struct die_info *die;
23365 struct attribute *attr;
23366 const gdb_byte *result = NULL;
23367 struct type *type;
23368 LONGEST value;
23369 enum bfd_endian byte_order;
e3b94546 23370 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23371
b6807d98 23372 if (per_cu->cu == NULL)
58f0c718 23373 load_cu (per_cu, false);
b6807d98 23374 cu = per_cu->cu;
cc12ce38
DE
23375 if (cu == NULL)
23376 {
23377 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23378 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23379 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23380 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23381 }
b6807d98 23382
9c541725 23383 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23384 if (!die)
9d8780f0
SM
23385 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23386 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23387
23388 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23389 if (attr == NULL)
23390 return NULL;
23391
e3b94546 23392 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23393 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23394
23395 switch (attr->form)
23396 {
23397 case DW_FORM_addr:
336d760d 23398 case DW_FORM_addrx:
b6807d98
TT
23399 case DW_FORM_GNU_addr_index:
23400 {
23401 gdb_byte *tem;
23402
23403 *len = cu->header.addr_size;
224c3ddb 23404 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23405 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23406 result = tem;
23407 }
23408 break;
23409 case DW_FORM_string:
23410 case DW_FORM_strp:
cf532bd1 23411 case DW_FORM_strx:
b6807d98
TT
23412 case DW_FORM_GNU_str_index:
23413 case DW_FORM_GNU_strp_alt:
23414 /* DW_STRING is already allocated on the objfile obstack, point
23415 directly to it. */
23416 result = (const gdb_byte *) DW_STRING (attr);
23417 *len = strlen (DW_STRING (attr));
23418 break;
23419 case DW_FORM_block1:
23420 case DW_FORM_block2:
23421 case DW_FORM_block4:
23422 case DW_FORM_block:
23423 case DW_FORM_exprloc:
0224619f 23424 case DW_FORM_data16:
b6807d98
TT
23425 result = DW_BLOCK (attr)->data;
23426 *len = DW_BLOCK (attr)->size;
23427 break;
23428
23429 /* The DW_AT_const_value attributes are supposed to carry the
23430 symbol's value "represented as it would be on the target
23431 architecture." By the time we get here, it's already been
23432 converted to host endianness, so we just need to sign- or
23433 zero-extend it as appropriate. */
23434 case DW_FORM_data1:
23435 type = die_type (die, cu);
23436 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23437 if (result == NULL)
23438 result = write_constant_as_bytes (obstack, byte_order,
23439 type, value, len);
23440 break;
23441 case DW_FORM_data2:
23442 type = die_type (die, cu);
23443 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23444 if (result == NULL)
23445 result = write_constant_as_bytes (obstack, byte_order,
23446 type, value, len);
23447 break;
23448 case DW_FORM_data4:
23449 type = die_type (die, cu);
23450 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23451 if (result == NULL)
23452 result = write_constant_as_bytes (obstack, byte_order,
23453 type, value, len);
23454 break;
23455 case DW_FORM_data8:
23456 type = die_type (die, cu);
23457 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23458 if (result == NULL)
23459 result = write_constant_as_bytes (obstack, byte_order,
23460 type, value, len);
23461 break;
23462
23463 case DW_FORM_sdata:
663c44ac 23464 case DW_FORM_implicit_const:
b6807d98
TT
23465 type = die_type (die, cu);
23466 result = write_constant_as_bytes (obstack, byte_order,
23467 type, DW_SND (attr), len);
23468 break;
23469
23470 case DW_FORM_udata:
23471 type = die_type (die, cu);
23472 result = write_constant_as_bytes (obstack, byte_order,
23473 type, DW_UNSND (attr), len);
23474 break;
23475
23476 default:
b98664d3 23477 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23478 dwarf_form_name (attr->form));
23479 break;
23480 }
23481
23482 return result;
23483}
23484
7942e96e
AA
23485/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23486 valid type for this die is found. */
23487
23488struct type *
9c541725 23489dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23490 struct dwarf2_per_cu_data *per_cu)
23491{
23492 struct dwarf2_cu *cu;
23493 struct die_info *die;
23494
7942e96e 23495 if (per_cu->cu == NULL)
58f0c718 23496 load_cu (per_cu, false);
7942e96e
AA
23497 cu = per_cu->cu;
23498 if (!cu)
23499 return NULL;
23500
9c541725 23501 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23502 if (!die)
23503 return NULL;
23504
23505 return die_type (die, cu);
23506}
23507
8a9b8146
TT
23508/* Return the type of the DIE at DIE_OFFSET in the CU named by
23509 PER_CU. */
23510
23511struct type *
b64f50a1 23512dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23513 struct dwarf2_per_cu_data *per_cu)
23514{
9c541725 23515 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23516 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23517}
23518
ac9ec31b 23519/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23520 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23521 On exit *REF_CU is the CU of the result.
23522 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23523
23524static struct die_info *
ac9ec31b
DE
23525follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23526 struct dwarf2_cu **ref_cu)
348e048f 23527{
348e048f 23528 struct die_info temp_die;
c24bdb02 23529 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23530 struct die_info *die;
23531
ac9ec31b
DE
23532 /* While it might be nice to assert sig_type->type == NULL here,
23533 we can get here for DW_AT_imported_declaration where we need
23534 the DIE not the type. */
348e048f
DE
23535
23536 /* If necessary, add it to the queue and load its DIEs. */
23537
95554aad 23538 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23539 read_signatured_type (sig_type);
348e048f 23540
348e048f 23541 sig_cu = sig_type->per_cu.cu;
69d751e3 23542 gdb_assert (sig_cu != NULL);
9c541725
PA
23543 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23544 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23545 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23546 to_underlying (temp_die.sect_off));
348e048f
DE
23547 if (die)
23548 {
ed2dc618 23549 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23550 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23551
796a7ff8
DE
23552 /* For .gdb_index version 7 keep track of included TUs.
23553 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23554 if (dwarf2_per_objfile->index_table != NULL
23555 && dwarf2_per_objfile->index_table->version <= 7)
23556 {
23557 VEC_safe_push (dwarf2_per_cu_ptr,
23558 (*ref_cu)->per_cu->imported_symtabs,
23559 sig_cu->per_cu);
23560 }
23561
348e048f 23562 *ref_cu = sig_cu;
c24bdb02
KS
23563 if (sig_cu != cu)
23564 sig_cu->ancestor = cu;
23565
348e048f
DE
23566 return die;
23567 }
23568
ac9ec31b
DE
23569 return NULL;
23570}
23571
23572/* Follow signatured type referenced by ATTR in SRC_DIE.
23573 On entry *REF_CU is the CU of SRC_DIE.
23574 On exit *REF_CU is the CU of the result.
23575 The result is the DIE of the type.
23576 If the referenced type cannot be found an error is thrown. */
23577
23578static struct die_info *
ff39bb5e 23579follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23580 struct dwarf2_cu **ref_cu)
23581{
23582 ULONGEST signature = DW_SIGNATURE (attr);
23583 struct signatured_type *sig_type;
23584 struct die_info *die;
23585
23586 gdb_assert (attr->form == DW_FORM_ref_sig8);
23587
a2ce51a0 23588 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23589 /* sig_type will be NULL if the signatured type is missing from
23590 the debug info. */
23591 if (sig_type == NULL)
23592 {
23593 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23594 " from DIE at %s [in module %s]"),
23595 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23596 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23597 }
23598
23599 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23600 if (die == NULL)
23601 {
23602 dump_die_for_error (src_die);
23603 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23604 " from DIE at %s [in module %s]"),
23605 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23606 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23607 }
23608
23609 return die;
23610}
23611
23612/* Get the type specified by SIGNATURE referenced in DIE/CU,
23613 reading in and processing the type unit if necessary. */
23614
23615static struct type *
23616get_signatured_type (struct die_info *die, ULONGEST signature,
23617 struct dwarf2_cu *cu)
23618{
518817b3
SM
23619 struct dwarf2_per_objfile *dwarf2_per_objfile
23620 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23621 struct signatured_type *sig_type;
23622 struct dwarf2_cu *type_cu;
23623 struct die_info *type_die;
23624 struct type *type;
23625
a2ce51a0 23626 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23627 /* sig_type will be NULL if the signatured type is missing from
23628 the debug info. */
23629 if (sig_type == NULL)
23630 {
b98664d3 23631 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23632 " from DIE at %s [in module %s]"),
23633 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23634 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23635 return build_error_marker_type (cu, die);
23636 }
23637
23638 /* If we already know the type we're done. */
23639 if (sig_type->type != NULL)
23640 return sig_type->type;
23641
23642 type_cu = cu;
23643 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23644 if (type_die != NULL)
23645 {
23646 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23647 is created. This is important, for example, because for c++ classes
23648 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23649 type = read_type_die (type_die, type_cu);
23650 if (type == NULL)
23651 {
b98664d3 23652 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23653 " referenced from DIE at %s [in module %s]"),
23654 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23655 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23656 type = build_error_marker_type (cu, die);
23657 }
23658 }
23659 else
23660 {
b98664d3 23661 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23662 " from DIE at %s [in module %s]"),
23663 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23664 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23665 type = build_error_marker_type (cu, die);
23666 }
23667 sig_type->type = type;
23668
23669 return type;
23670}
23671
23672/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23673 reading in and processing the type unit if necessary. */
23674
23675static struct type *
ff39bb5e 23676get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23677 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23678{
23679 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23680 if (attr_form_is_ref (attr))
ac9ec31b
DE
23681 {
23682 struct dwarf2_cu *type_cu = cu;
23683 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23684
23685 return read_type_die (type_die, type_cu);
23686 }
23687 else if (attr->form == DW_FORM_ref_sig8)
23688 {
23689 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23690 }
23691 else
23692 {
518817b3
SM
23693 struct dwarf2_per_objfile *dwarf2_per_objfile
23694 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23695
b98664d3 23696 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23697 " at %s [in module %s]"),
23698 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23699 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23700 return build_error_marker_type (cu, die);
23701 }
348e048f
DE
23702}
23703
e5fe5e75 23704/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23705
23706static void
e5fe5e75 23707load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23708{
52dc124a 23709 struct signatured_type *sig_type;
348e048f 23710
f4dc4d17
DE
23711 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23712 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23713
6721b2ec
DE
23714 /* We have the per_cu, but we need the signatured_type.
23715 Fortunately this is an easy translation. */
23716 gdb_assert (per_cu->is_debug_types);
23717 sig_type = (struct signatured_type *) per_cu;
348e048f 23718
6721b2ec 23719 gdb_assert (per_cu->cu == NULL);
348e048f 23720
52dc124a 23721 read_signatured_type (sig_type);
348e048f 23722
6721b2ec 23723 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23724}
23725
dee91e82
DE
23726/* die_reader_func for read_signatured_type.
23727 This is identical to load_full_comp_unit_reader,
23728 but is kept separate for now. */
348e048f
DE
23729
23730static void
dee91e82 23731read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23732 const gdb_byte *info_ptr,
dee91e82
DE
23733 struct die_info *comp_unit_die,
23734 int has_children,
23735 void *data)
348e048f 23736{
dee91e82 23737 struct dwarf2_cu *cu = reader->cu;
348e048f 23738
dee91e82
DE
23739 gdb_assert (cu->die_hash == NULL);
23740 cu->die_hash =
23741 htab_create_alloc_ex (cu->header.length / 12,
23742 die_hash,
23743 die_eq,
23744 NULL,
23745 &cu->comp_unit_obstack,
23746 hashtab_obstack_allocate,
23747 dummy_obstack_deallocate);
348e048f 23748
dee91e82
DE
23749 if (has_children)
23750 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23751 &info_ptr, comp_unit_die);
23752 cu->dies = comp_unit_die;
23753 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23754
23755 /* We try not to read any attributes in this function, because not
9cdd5dbd 23756 all CUs needed for references have been loaded yet, and symbol
348e048f 23757 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23758 or we won't be able to build types correctly.
23759 Similarly, if we do not read the producer, we can not apply
23760 producer-specific interpretation. */
95554aad 23761 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23762}
348e048f 23763
3019eac3
DE
23764/* Read in a signatured type and build its CU and DIEs.
23765 If the type is a stub for the real type in a DWO file,
23766 read in the real type from the DWO file as well. */
dee91e82
DE
23767
23768static void
23769read_signatured_type (struct signatured_type *sig_type)
23770{
23771 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23772
3019eac3 23773 gdb_assert (per_cu->is_debug_types);
dee91e82 23774 gdb_assert (per_cu->cu == NULL);
348e048f 23775
58f0c718 23776 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23777 read_signatured_type_reader, NULL);
7ee85ab1 23778 sig_type->per_cu.tu_read = 1;
c906108c
SS
23779}
23780
c906108c
SS
23781/* Decode simple location descriptions.
23782 Given a pointer to a dwarf block that defines a location, compute
23783 the location and return the value.
23784
4cecd739
DJ
23785 NOTE drow/2003-11-18: This function is called in two situations
23786 now: for the address of static or global variables (partial symbols
23787 only) and for offsets into structures which are expected to be
23788 (more or less) constant. The partial symbol case should go away,
23789 and only the constant case should remain. That will let this
23790 function complain more accurately. A few special modes are allowed
23791 without complaint for global variables (for instance, global
23792 register values and thread-local values).
c906108c
SS
23793
23794 A location description containing no operations indicates that the
4cecd739 23795 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23796 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23797 callers will only want a very basic result and this can become a
21ae7a4d
JK
23798 complaint.
23799
23800 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23801
23802static CORE_ADDR
e7c27a73 23803decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23804{
518817b3 23805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23806 size_t i;
23807 size_t size = blk->size;
d521ce57 23808 const gdb_byte *data = blk->data;
21ae7a4d
JK
23809 CORE_ADDR stack[64];
23810 int stacki;
23811 unsigned int bytes_read, unsnd;
23812 gdb_byte op;
c906108c 23813
21ae7a4d
JK
23814 i = 0;
23815 stacki = 0;
23816 stack[stacki] = 0;
23817 stack[++stacki] = 0;
23818
23819 while (i < size)
23820 {
23821 op = data[i++];
23822 switch (op)
23823 {
23824 case DW_OP_lit0:
23825 case DW_OP_lit1:
23826 case DW_OP_lit2:
23827 case DW_OP_lit3:
23828 case DW_OP_lit4:
23829 case DW_OP_lit5:
23830 case DW_OP_lit6:
23831 case DW_OP_lit7:
23832 case DW_OP_lit8:
23833 case DW_OP_lit9:
23834 case DW_OP_lit10:
23835 case DW_OP_lit11:
23836 case DW_OP_lit12:
23837 case DW_OP_lit13:
23838 case DW_OP_lit14:
23839 case DW_OP_lit15:
23840 case DW_OP_lit16:
23841 case DW_OP_lit17:
23842 case DW_OP_lit18:
23843 case DW_OP_lit19:
23844 case DW_OP_lit20:
23845 case DW_OP_lit21:
23846 case DW_OP_lit22:
23847 case DW_OP_lit23:
23848 case DW_OP_lit24:
23849 case DW_OP_lit25:
23850 case DW_OP_lit26:
23851 case DW_OP_lit27:
23852 case DW_OP_lit28:
23853 case DW_OP_lit29:
23854 case DW_OP_lit30:
23855 case DW_OP_lit31:
23856 stack[++stacki] = op - DW_OP_lit0;
23857 break;
f1bea926 23858
21ae7a4d
JK
23859 case DW_OP_reg0:
23860 case DW_OP_reg1:
23861 case DW_OP_reg2:
23862 case DW_OP_reg3:
23863 case DW_OP_reg4:
23864 case DW_OP_reg5:
23865 case DW_OP_reg6:
23866 case DW_OP_reg7:
23867 case DW_OP_reg8:
23868 case DW_OP_reg9:
23869 case DW_OP_reg10:
23870 case DW_OP_reg11:
23871 case DW_OP_reg12:
23872 case DW_OP_reg13:
23873 case DW_OP_reg14:
23874 case DW_OP_reg15:
23875 case DW_OP_reg16:
23876 case DW_OP_reg17:
23877 case DW_OP_reg18:
23878 case DW_OP_reg19:
23879 case DW_OP_reg20:
23880 case DW_OP_reg21:
23881 case DW_OP_reg22:
23882 case DW_OP_reg23:
23883 case DW_OP_reg24:
23884 case DW_OP_reg25:
23885 case DW_OP_reg26:
23886 case DW_OP_reg27:
23887 case DW_OP_reg28:
23888 case DW_OP_reg29:
23889 case DW_OP_reg30:
23890 case DW_OP_reg31:
23891 stack[++stacki] = op - DW_OP_reg0;
23892 if (i < size)
23893 dwarf2_complex_location_expr_complaint ();
23894 break;
c906108c 23895
21ae7a4d
JK
23896 case DW_OP_regx:
23897 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23898 i += bytes_read;
23899 stack[++stacki] = unsnd;
23900 if (i < size)
23901 dwarf2_complex_location_expr_complaint ();
23902 break;
c906108c 23903
21ae7a4d
JK
23904 case DW_OP_addr:
23905 stack[++stacki] = read_address (objfile->obfd, &data[i],
23906 cu, &bytes_read);
23907 i += bytes_read;
23908 break;
d53d4ac5 23909
21ae7a4d
JK
23910 case DW_OP_const1u:
23911 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23912 i += 1;
23913 break;
23914
23915 case DW_OP_const1s:
23916 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23917 i += 1;
23918 break;
23919
23920 case DW_OP_const2u:
23921 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23922 i += 2;
23923 break;
23924
23925 case DW_OP_const2s:
23926 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23927 i += 2;
23928 break;
d53d4ac5 23929
21ae7a4d
JK
23930 case DW_OP_const4u:
23931 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23932 i += 4;
23933 break;
23934
23935 case DW_OP_const4s:
23936 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23937 i += 4;
23938 break;
23939
585861ea
JK
23940 case DW_OP_const8u:
23941 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23942 i += 8;
23943 break;
23944
21ae7a4d
JK
23945 case DW_OP_constu:
23946 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23947 &bytes_read);
23948 i += bytes_read;
23949 break;
23950
23951 case DW_OP_consts:
23952 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23953 i += bytes_read;
23954 break;
23955
23956 case DW_OP_dup:
23957 stack[stacki + 1] = stack[stacki];
23958 stacki++;
23959 break;
23960
23961 case DW_OP_plus:
23962 stack[stacki - 1] += stack[stacki];
23963 stacki--;
23964 break;
23965
23966 case DW_OP_plus_uconst:
23967 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23968 &bytes_read);
23969 i += bytes_read;
23970 break;
23971
23972 case DW_OP_minus:
23973 stack[stacki - 1] -= stack[stacki];
23974 stacki--;
23975 break;
23976
23977 case DW_OP_deref:
23978 /* If we're not the last op, then we definitely can't encode
23979 this using GDB's address_class enum. This is valid for partial
23980 global symbols, although the variable's address will be bogus
23981 in the psymtab. */
23982 if (i < size)
23983 dwarf2_complex_location_expr_complaint ();
23984 break;
23985
23986 case DW_OP_GNU_push_tls_address:
4aa4e28b 23987 case DW_OP_form_tls_address:
21ae7a4d
JK
23988 /* The top of the stack has the offset from the beginning
23989 of the thread control block at which the variable is located. */
23990 /* Nothing should follow this operator, so the top of stack would
23991 be returned. */
23992 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23993 address will be bogus in the psymtab. Make it always at least
23994 non-zero to not look as a variable garbage collected by linker
23995 which have DW_OP_addr 0. */
21ae7a4d
JK
23996 if (i < size)
23997 dwarf2_complex_location_expr_complaint ();
585861ea 23998 stack[stacki]++;
21ae7a4d
JK
23999 break;
24000
24001 case DW_OP_GNU_uninit:
24002 break;
24003
336d760d 24004 case DW_OP_addrx:
3019eac3 24005 case DW_OP_GNU_addr_index:
49f6c839 24006 case DW_OP_GNU_const_index:
3019eac3
DE
24007 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24008 &bytes_read);
24009 i += bytes_read;
24010 break;
24011
21ae7a4d
JK
24012 default:
24013 {
f39c6ffd 24014 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24015
24016 if (name)
b98664d3 24017 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24018 name);
24019 else
b98664d3 24020 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24021 op);
24022 }
24023
24024 return (stack[stacki]);
d53d4ac5 24025 }
3c6e0cb3 24026
21ae7a4d
JK
24027 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24028 outside of the allocated space. Also enforce minimum>0. */
24029 if (stacki >= ARRAY_SIZE (stack) - 1)
24030 {
b98664d3 24031 complaint (_("location description stack overflow"));
21ae7a4d
JK
24032 return 0;
24033 }
24034
24035 if (stacki <= 0)
24036 {
b98664d3 24037 complaint (_("location description stack underflow"));
21ae7a4d
JK
24038 return 0;
24039 }
24040 }
24041 return (stack[stacki]);
c906108c
SS
24042}
24043
24044/* memory allocation interface */
24045
c906108c 24046static struct dwarf_block *
7b5a2f43 24047dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24048{
8d749320 24049 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24050}
24051
c906108c 24052static struct die_info *
b60c80d6 24053dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24054{
24055 struct die_info *die;
b60c80d6
DJ
24056 size_t size = sizeof (struct die_info);
24057
24058 if (num_attrs > 1)
24059 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24060
b60c80d6 24061 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24062 memset (die, 0, sizeof (struct die_info));
24063 return (die);
24064}
2e276125
JB
24065
24066\f
24067/* Macro support. */
24068
233d95b5
JK
24069/* Return file name relative to the compilation directory of file number I in
24070 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24071 responsible for freeing it. */
233d95b5 24072
2e276125 24073static char *
233d95b5 24074file_file_name (int file, struct line_header *lh)
2e276125 24075{
6a83a1e6
EZ
24076 /* Is the file number a valid index into the line header's file name
24077 table? Remember that file numbers start with one, not zero. */
fff8551c 24078 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24079 {
8c43009f 24080 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24081
8c43009f
PA
24082 if (!IS_ABSOLUTE_PATH (fe.name))
24083 {
24084 const char *dir = fe.include_dir (lh);
24085 if (dir != NULL)
24086 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24087 }
24088 return xstrdup (fe.name);
6a83a1e6 24089 }
2e276125
JB
24090 else
24091 {
6a83a1e6
EZ
24092 /* The compiler produced a bogus file number. We can at least
24093 record the macro definitions made in the file, even if we
24094 won't be able to find the file by name. */
24095 char fake_name[80];
9a619af0 24096
8c042590
PM
24097 xsnprintf (fake_name, sizeof (fake_name),
24098 "<bad macro file number %d>", file);
2e276125 24099
b98664d3 24100 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24101 file);
2e276125 24102
6a83a1e6 24103 return xstrdup (fake_name);
2e276125
JB
24104 }
24105}
24106
233d95b5
JK
24107/* Return the full name of file number I in *LH's file name table.
24108 Use COMP_DIR as the name of the current directory of the
24109 compilation. The result is allocated using xmalloc; the caller is
24110 responsible for freeing it. */
24111static char *
24112file_full_name (int file, struct line_header *lh, const char *comp_dir)
24113{
24114 /* Is the file number a valid index into the line header's file name
24115 table? Remember that file numbers start with one, not zero. */
fff8551c 24116 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24117 {
24118 char *relative = file_file_name (file, lh);
24119
24120 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24121 return relative;
b36cec19
PA
24122 return reconcat (relative, comp_dir, SLASH_STRING,
24123 relative, (char *) NULL);
233d95b5
JK
24124 }
24125 else
24126 return file_file_name (file, lh);
24127}
24128
2e276125
JB
24129
24130static struct macro_source_file *
804d2729
TT
24131macro_start_file (struct dwarf2_cu *cu,
24132 int file, int line,
2e276125 24133 struct macro_source_file *current_file,
43f3e411 24134 struct line_header *lh)
2e276125 24135{
233d95b5
JK
24136 /* File name relative to the compilation directory of this source file. */
24137 char *file_name = file_file_name (file, lh);
2e276125 24138
2e276125 24139 if (! current_file)
abc9d0dc 24140 {
fc474241
DE
24141 /* Note: We don't create a macro table for this compilation unit
24142 at all until we actually get a filename. */
c24bdb02 24143 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24144
abc9d0dc
TT
24145 /* If we have no current file, then this must be the start_file
24146 directive for the compilation unit's main source file. */
fc474241
DE
24147 current_file = macro_set_main (macro_table, file_name);
24148 macro_define_special (macro_table);
abc9d0dc 24149 }
2e276125 24150 else
233d95b5 24151 current_file = macro_include (current_file, line, file_name);
2e276125 24152
233d95b5 24153 xfree (file_name);
6e70227d 24154
2e276125
JB
24155 return current_file;
24156}
24157
2e276125
JB
24158static const char *
24159consume_improper_spaces (const char *p, const char *body)
24160{
24161 if (*p == ' ')
24162 {
b98664d3 24163 complaint (_("macro definition contains spaces "
3e43a32a 24164 "in formal argument list:\n`%s'"),
4d3c2250 24165 body);
2e276125
JB
24166
24167 while (*p == ' ')
24168 p++;
24169 }
24170
24171 return p;
24172}
24173
24174
24175static void
24176parse_macro_definition (struct macro_source_file *file, int line,
24177 const char *body)
24178{
24179 const char *p;
24180
24181 /* The body string takes one of two forms. For object-like macro
24182 definitions, it should be:
24183
24184 <macro name> " " <definition>
24185
24186 For function-like macro definitions, it should be:
24187
24188 <macro name> "() " <definition>
24189 or
24190 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24191
24192 Spaces may appear only where explicitly indicated, and in the
24193 <definition>.
24194
24195 The Dwarf 2 spec says that an object-like macro's name is always
24196 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24197 the space when the macro's definition is the empty string.
2e276125
JB
24198
24199 The Dwarf 2 spec says that there should be no spaces between the
24200 formal arguments in a function-like macro's formal argument list,
24201 but versions of GCC around March 2002 include spaces after the
24202 commas. */
24203
24204
24205 /* Find the extent of the macro name. The macro name is terminated
24206 by either a space or null character (for an object-like macro) or
24207 an opening paren (for a function-like macro). */
24208 for (p = body; *p; p++)
24209 if (*p == ' ' || *p == '(')
24210 break;
24211
24212 if (*p == ' ' || *p == '\0')
24213 {
24214 /* It's an object-like macro. */
24215 int name_len = p - body;
3f8a7804 24216 char *name = savestring (body, name_len);
2e276125
JB
24217 const char *replacement;
24218
24219 if (*p == ' ')
24220 replacement = body + name_len + 1;
24221 else
24222 {
4d3c2250 24223 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24224 replacement = body + name_len;
24225 }
6e70227d 24226
2e276125
JB
24227 macro_define_object (file, line, name, replacement);
24228
24229 xfree (name);
24230 }
24231 else if (*p == '(')
24232 {
24233 /* It's a function-like macro. */
3f8a7804 24234 char *name = savestring (body, p - body);
2e276125
JB
24235 int argc = 0;
24236 int argv_size = 1;
8d749320 24237 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24238
24239 p++;
24240
24241 p = consume_improper_spaces (p, body);
24242
24243 /* Parse the formal argument list. */
24244 while (*p && *p != ')')
24245 {
24246 /* Find the extent of the current argument name. */
24247 const char *arg_start = p;
24248
24249 while (*p && *p != ',' && *p != ')' && *p != ' ')
24250 p++;
24251
24252 if (! *p || p == arg_start)
4d3c2250 24253 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24254 else
24255 {
24256 /* Make sure argv has room for the new argument. */
24257 if (argc >= argv_size)
24258 {
24259 argv_size *= 2;
224c3ddb 24260 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24261 }
24262
3f8a7804 24263 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24264 }
24265
24266 p = consume_improper_spaces (p, body);
24267
24268 /* Consume the comma, if present. */
24269 if (*p == ',')
24270 {
24271 p++;
24272
24273 p = consume_improper_spaces (p, body);
24274 }
24275 }
24276
24277 if (*p == ')')
24278 {
24279 p++;
24280
24281 if (*p == ' ')
24282 /* Perfectly formed definition, no complaints. */
24283 macro_define_function (file, line, name,
6e70227d 24284 argc, (const char **) argv,
2e276125
JB
24285 p + 1);
24286 else if (*p == '\0')
24287 {
24288 /* Complain, but do define it. */
4d3c2250 24289 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24290 macro_define_function (file, line, name,
6e70227d 24291 argc, (const char **) argv,
2e276125
JB
24292 p);
24293 }
24294 else
24295 /* Just complain. */
4d3c2250 24296 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24297 }
24298 else
24299 /* Just complain. */
4d3c2250 24300 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24301
24302 xfree (name);
24303 {
24304 int i;
24305
24306 for (i = 0; i < argc; i++)
24307 xfree (argv[i]);
24308 }
24309 xfree (argv);
24310 }
24311 else
4d3c2250 24312 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24313}
24314
cf2c3c16
TT
24315/* Skip some bytes from BYTES according to the form given in FORM.
24316 Returns the new pointer. */
2e276125 24317
d521ce57
TT
24318static const gdb_byte *
24319skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24320 enum dwarf_form form,
24321 unsigned int offset_size,
24322 struct dwarf2_section_info *section)
2e276125 24323{
cf2c3c16 24324 unsigned int bytes_read;
2e276125 24325
cf2c3c16 24326 switch (form)
2e276125 24327 {
cf2c3c16
TT
24328 case DW_FORM_data1:
24329 case DW_FORM_flag:
24330 ++bytes;
24331 break;
24332
24333 case DW_FORM_data2:
24334 bytes += 2;
24335 break;
24336
24337 case DW_FORM_data4:
24338 bytes += 4;
24339 break;
24340
24341 case DW_FORM_data8:
24342 bytes += 8;
24343 break;
24344
0224619f
JK
24345 case DW_FORM_data16:
24346 bytes += 16;
24347 break;
24348
cf2c3c16
TT
24349 case DW_FORM_string:
24350 read_direct_string (abfd, bytes, &bytes_read);
24351 bytes += bytes_read;
24352 break;
24353
24354 case DW_FORM_sec_offset:
24355 case DW_FORM_strp:
36586728 24356 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24357 bytes += offset_size;
24358 break;
24359
24360 case DW_FORM_block:
24361 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24362 bytes += bytes_read;
24363 break;
24364
24365 case DW_FORM_block1:
24366 bytes += 1 + read_1_byte (abfd, bytes);
24367 break;
24368 case DW_FORM_block2:
24369 bytes += 2 + read_2_bytes (abfd, bytes);
24370 break;
24371 case DW_FORM_block4:
24372 bytes += 4 + read_4_bytes (abfd, bytes);
24373 break;
24374
336d760d 24375 case DW_FORM_addrx:
cf2c3c16 24376 case DW_FORM_sdata:
cf532bd1 24377 case DW_FORM_strx:
cf2c3c16 24378 case DW_FORM_udata:
3019eac3
DE
24379 case DW_FORM_GNU_addr_index:
24380 case DW_FORM_GNU_str_index:
d521ce57 24381 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24382 if (bytes == NULL)
24383 {
24384 dwarf2_section_buffer_overflow_complaint (section);
24385 return NULL;
24386 }
cf2c3c16
TT
24387 break;
24388
663c44ac
JK
24389 case DW_FORM_implicit_const:
24390 break;
24391
cf2c3c16
TT
24392 default:
24393 {
b98664d3 24394 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24395 form, get_section_name (section));
cf2c3c16
TT
24396 return NULL;
24397 }
2e276125
JB
24398 }
24399
cf2c3c16
TT
24400 return bytes;
24401}
757a13d0 24402
cf2c3c16
TT
24403/* A helper for dwarf_decode_macros that handles skipping an unknown
24404 opcode. Returns an updated pointer to the macro data buffer; or,
24405 on error, issues a complaint and returns NULL. */
757a13d0 24406
d521ce57 24407static const gdb_byte *
cf2c3c16 24408skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24409 const gdb_byte **opcode_definitions,
24410 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24411 bfd *abfd,
24412 unsigned int offset_size,
24413 struct dwarf2_section_info *section)
24414{
24415 unsigned int bytes_read, i;
24416 unsigned long arg;
d521ce57 24417 const gdb_byte *defn;
2e276125 24418
cf2c3c16 24419 if (opcode_definitions[opcode] == NULL)
2e276125 24420 {
b98664d3 24421 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24422 opcode);
24423 return NULL;
24424 }
2e276125 24425
cf2c3c16
TT
24426 defn = opcode_definitions[opcode];
24427 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24428 defn += bytes_read;
2e276125 24429
cf2c3c16
TT
24430 for (i = 0; i < arg; ++i)
24431 {
aead7601
SM
24432 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24433 (enum dwarf_form) defn[i], offset_size,
f664829e 24434 section);
cf2c3c16
TT
24435 if (mac_ptr == NULL)
24436 {
24437 /* skip_form_bytes already issued the complaint. */
24438 return NULL;
24439 }
24440 }
757a13d0 24441
cf2c3c16
TT
24442 return mac_ptr;
24443}
757a13d0 24444
cf2c3c16
TT
24445/* A helper function which parses the header of a macro section.
24446 If the macro section is the extended (for now called "GNU") type,
24447 then this updates *OFFSET_SIZE. Returns a pointer to just after
24448 the header, or issues a complaint and returns NULL on error. */
757a13d0 24449
d521ce57
TT
24450static const gdb_byte *
24451dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24452 bfd *abfd,
d521ce57 24453 const gdb_byte *mac_ptr,
cf2c3c16
TT
24454 unsigned int *offset_size,
24455 int section_is_gnu)
24456{
24457 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24458
cf2c3c16
TT
24459 if (section_is_gnu)
24460 {
24461 unsigned int version, flags;
757a13d0 24462
cf2c3c16 24463 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24464 if (version != 4 && version != 5)
cf2c3c16 24465 {
b98664d3 24466 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24467 version);
24468 return NULL;
24469 }
24470 mac_ptr += 2;
757a13d0 24471
cf2c3c16
TT
24472 flags = read_1_byte (abfd, mac_ptr);
24473 ++mac_ptr;
24474 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24475
cf2c3c16
TT
24476 if ((flags & 2) != 0)
24477 /* We don't need the line table offset. */
24478 mac_ptr += *offset_size;
757a13d0 24479
cf2c3c16
TT
24480 /* Vendor opcode descriptions. */
24481 if ((flags & 4) != 0)
24482 {
24483 unsigned int i, count;
757a13d0 24484
cf2c3c16
TT
24485 count = read_1_byte (abfd, mac_ptr);
24486 ++mac_ptr;
24487 for (i = 0; i < count; ++i)
24488 {
24489 unsigned int opcode, bytes_read;
24490 unsigned long arg;
24491
24492 opcode = read_1_byte (abfd, mac_ptr);
24493 ++mac_ptr;
24494 opcode_definitions[opcode] = mac_ptr;
24495 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24496 mac_ptr += bytes_read;
24497 mac_ptr += arg;
24498 }
757a13d0 24499 }
cf2c3c16 24500 }
757a13d0 24501
cf2c3c16
TT
24502 return mac_ptr;
24503}
757a13d0 24504
cf2c3c16 24505/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24506 including DW_MACRO_import. */
cf2c3c16
TT
24507
24508static void
804d2729 24509dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24510 bfd *abfd,
d521ce57 24511 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24512 struct macro_source_file *current_file,
43f3e411 24513 struct line_header *lh,
cf2c3c16 24514 struct dwarf2_section_info *section,
36586728 24515 int section_is_gnu, int section_is_dwz,
cf2c3c16 24516 unsigned int offset_size,
8fc3fc34 24517 htab_t include_hash)
cf2c3c16 24518{
804d2729
TT
24519 struct dwarf2_per_objfile *dwarf2_per_objfile
24520 = cu->per_cu->dwarf2_per_objfile;
4d663531 24521 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24522 enum dwarf_macro_record_type macinfo_type;
24523 int at_commandline;
d521ce57 24524 const gdb_byte *opcode_definitions[256];
757a13d0 24525
cf2c3c16
TT
24526 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24527 &offset_size, section_is_gnu);
24528 if (mac_ptr == NULL)
24529 {
24530 /* We already issued a complaint. */
24531 return;
24532 }
757a13d0
JK
24533
24534 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24535 GDB is still reading the definitions from command line. First
24536 DW_MACINFO_start_file will need to be ignored as it was already executed
24537 to create CURRENT_FILE for the main source holding also the command line
24538 definitions. On first met DW_MACINFO_start_file this flag is reset to
24539 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24540
24541 at_commandline = 1;
24542
24543 do
24544 {
24545 /* Do we at least have room for a macinfo type byte? */
24546 if (mac_ptr >= mac_end)
24547 {
f664829e 24548 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24549 break;
24550 }
24551
aead7601 24552 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24553 mac_ptr++;
24554
cf2c3c16
TT
24555 /* Note that we rely on the fact that the corresponding GNU and
24556 DWARF constants are the same. */
132448f8
SM
24557 DIAGNOSTIC_PUSH
24558 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24559 switch (macinfo_type)
24560 {
24561 /* A zero macinfo type indicates the end of the macro
24562 information. */
24563 case 0:
24564 break;
2e276125 24565
0af92d60
JK
24566 case DW_MACRO_define:
24567 case DW_MACRO_undef:
24568 case DW_MACRO_define_strp:
24569 case DW_MACRO_undef_strp:
24570 case DW_MACRO_define_sup:
24571 case DW_MACRO_undef_sup:
2e276125 24572 {
891d2f0b 24573 unsigned int bytes_read;
2e276125 24574 int line;
d521ce57 24575 const char *body;
cf2c3c16 24576 int is_define;
2e276125 24577
cf2c3c16
TT
24578 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24579 mac_ptr += bytes_read;
24580
0af92d60
JK
24581 if (macinfo_type == DW_MACRO_define
24582 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24583 {
24584 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24585 mac_ptr += bytes_read;
24586 }
24587 else
24588 {
24589 LONGEST str_offset;
24590
24591 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24592 mac_ptr += offset_size;
2e276125 24593
0af92d60
JK
24594 if (macinfo_type == DW_MACRO_define_sup
24595 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24596 || section_is_dwz)
36586728 24597 {
ed2dc618
SM
24598 struct dwz_file *dwz
24599 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24600
ed2dc618
SM
24601 body = read_indirect_string_from_dwz (objfile,
24602 dwz, str_offset);
36586728
TT
24603 }
24604 else
ed2dc618
SM
24605 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24606 abfd, str_offset);
cf2c3c16
TT
24607 }
24608
0af92d60
JK
24609 is_define = (macinfo_type == DW_MACRO_define
24610 || macinfo_type == DW_MACRO_define_strp
24611 || macinfo_type == DW_MACRO_define_sup);
2e276125 24612 if (! current_file)
757a13d0
JK
24613 {
24614 /* DWARF violation as no main source is present. */
b98664d3 24615 complaint (_("debug info with no main source gives macro %s "
757a13d0 24616 "on line %d: %s"),
cf2c3c16
TT
24617 is_define ? _("definition") : _("undefinition"),
24618 line, body);
757a13d0
JK
24619 break;
24620 }
3e43a32a
MS
24621 if ((line == 0 && !at_commandline)
24622 || (line != 0 && at_commandline))
b98664d3 24623 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24624 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24625 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24626 line == 0 ? _("zero") : _("non-zero"), line, body);
24627
955b06fa 24628 if (body == NULL)
7bede828 24629 {
955b06fa
SDJ
24630 /* Fedora's rpm-build's "debugedit" binary
24631 corrupted .debug_macro sections.
24632
24633 For more info, see
24634 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24635 complaint (_("debug info gives %s invalid macro %s "
24636 "without body (corrupted?) at line %d "
24637 "on file %s"),
24638 at_commandline ? _("command-line") : _("in-file"),
24639 is_define ? _("definition") : _("undefinition"),
24640 line, current_file->filename);
7bede828 24641 }
955b06fa
SDJ
24642 else if (is_define)
24643 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24644 else
24645 {
0af92d60
JK
24646 gdb_assert (macinfo_type == DW_MACRO_undef
24647 || macinfo_type == DW_MACRO_undef_strp
24648 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24649 macro_undef (current_file, line, body);
24650 }
2e276125
JB
24651 }
24652 break;
24653
0af92d60 24654 case DW_MACRO_start_file:
2e276125 24655 {
891d2f0b 24656 unsigned int bytes_read;
2e276125
JB
24657 int line, file;
24658
24659 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24660 mac_ptr += bytes_read;
24661 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24662 mac_ptr += bytes_read;
24663
3e43a32a
MS
24664 if ((line == 0 && !at_commandline)
24665 || (line != 0 && at_commandline))
b98664d3 24666 complaint (_("debug info gives source %d included "
757a13d0
JK
24667 "from %s at %s line %d"),
24668 file, at_commandline ? _("command-line") : _("file"),
24669 line == 0 ? _("zero") : _("non-zero"), line);
24670
24671 if (at_commandline)
24672 {
0af92d60 24673 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24674 pass one. */
757a13d0
JK
24675 at_commandline = 0;
24676 }
24677 else
804d2729
TT
24678 current_file = macro_start_file (cu, file, line, current_file,
24679 lh);
2e276125
JB
24680 }
24681 break;
24682
0af92d60 24683 case DW_MACRO_end_file:
2e276125 24684 if (! current_file)
b98664d3 24685 complaint (_("macro debug info has an unmatched "
3e43a32a 24686 "`close_file' directive"));
2e276125
JB
24687 else
24688 {
24689 current_file = current_file->included_by;
24690 if (! current_file)
24691 {
cf2c3c16 24692 enum dwarf_macro_record_type next_type;
2e276125
JB
24693
24694 /* GCC circa March 2002 doesn't produce the zero
24695 type byte marking the end of the compilation
24696 unit. Complain if it's not there, but exit no
24697 matter what. */
24698
24699 /* Do we at least have room for a macinfo type byte? */
24700 if (mac_ptr >= mac_end)
24701 {
f664829e 24702 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24703 return;
24704 }
24705
24706 /* We don't increment mac_ptr here, so this is just
24707 a look-ahead. */
aead7601
SM
24708 next_type
24709 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24710 mac_ptr);
2e276125 24711 if (next_type != 0)
b98664d3 24712 complaint (_("no terminating 0-type entry for "
3e43a32a 24713 "macros in `.debug_macinfo' section"));
2e276125
JB
24714
24715 return;
24716 }
24717 }
24718 break;
24719
0af92d60
JK
24720 case DW_MACRO_import:
24721 case DW_MACRO_import_sup:
cf2c3c16
TT
24722 {
24723 LONGEST offset;
8fc3fc34 24724 void **slot;
a036ba48
TT
24725 bfd *include_bfd = abfd;
24726 struct dwarf2_section_info *include_section = section;
d521ce57 24727 const gdb_byte *include_mac_end = mac_end;
a036ba48 24728 int is_dwz = section_is_dwz;
d521ce57 24729 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24730
24731 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24732 mac_ptr += offset_size;
24733
0af92d60 24734 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24735 {
ed2dc618 24736 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24737
4d663531 24738 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24739
a036ba48 24740 include_section = &dwz->macro;
a32a8923 24741 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24742 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24743 is_dwz = 1;
24744 }
24745
24746 new_mac_ptr = include_section->buffer + offset;
24747 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24748
8fc3fc34
TT
24749 if (*slot != NULL)
24750 {
24751 /* This has actually happened; see
24752 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24753 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24754 ".debug_macro section"));
24755 }
24756 else
24757 {
d521ce57 24758 *slot = (void *) new_mac_ptr;
36586728 24759
804d2729 24760 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24761 include_mac_end, current_file, lh,
36586728 24762 section, section_is_gnu, is_dwz,
4d663531 24763 offset_size, include_hash);
8fc3fc34 24764
d521ce57 24765 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24766 }
cf2c3c16
TT
24767 }
24768 break;
24769
2e276125 24770 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24771 if (!section_is_gnu)
24772 {
24773 unsigned int bytes_read;
2e276125 24774
ac298888
TT
24775 /* This reads the constant, but since we don't recognize
24776 any vendor extensions, we ignore it. */
24777 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24778 mac_ptr += bytes_read;
24779 read_direct_string (abfd, mac_ptr, &bytes_read);
24780 mac_ptr += bytes_read;
2e276125 24781
cf2c3c16
TT
24782 /* We don't recognize any vendor extensions. */
24783 break;
24784 }
24785 /* FALLTHROUGH */
24786
24787 default:
24788 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24789 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24790 section);
24791 if (mac_ptr == NULL)
24792 return;
24793 break;
2e276125 24794 }
132448f8 24795 DIAGNOSTIC_POP
757a13d0 24796 } while (macinfo_type != 0);
2e276125 24797}
8e19ed76 24798
cf2c3c16 24799static void
09262596 24800dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24801 int section_is_gnu)
cf2c3c16 24802{
518817b3
SM
24803 struct dwarf2_per_objfile *dwarf2_per_objfile
24804 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24805 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24806 struct line_header *lh = cu->line_header;
24807 bfd *abfd;
d521ce57 24808 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24809 struct macro_source_file *current_file = 0;
24810 enum dwarf_macro_record_type macinfo_type;
24811 unsigned int offset_size = cu->header.offset_size;
d521ce57 24812 const gdb_byte *opcode_definitions[256];
8fc3fc34 24813 void **slot;
09262596
DE
24814 struct dwarf2_section_info *section;
24815 const char *section_name;
24816
24817 if (cu->dwo_unit != NULL)
24818 {
24819 if (section_is_gnu)
24820 {
24821 section = &cu->dwo_unit->dwo_file->sections.macro;
24822 section_name = ".debug_macro.dwo";
24823 }
24824 else
24825 {
24826 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24827 section_name = ".debug_macinfo.dwo";
24828 }
24829 }
24830 else
24831 {
24832 if (section_is_gnu)
24833 {
24834 section = &dwarf2_per_objfile->macro;
24835 section_name = ".debug_macro";
24836 }
24837 else
24838 {
24839 section = &dwarf2_per_objfile->macinfo;
24840 section_name = ".debug_macinfo";
24841 }
24842 }
cf2c3c16 24843
bb5ed363 24844 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24845 if (section->buffer == NULL)
24846 {
b98664d3 24847 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24848 return;
24849 }
a32a8923 24850 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24851
24852 /* First pass: Find the name of the base filename.
24853 This filename is needed in order to process all macros whose definition
24854 (or undefinition) comes from the command line. These macros are defined
24855 before the first DW_MACINFO_start_file entry, and yet still need to be
24856 associated to the base file.
24857
24858 To determine the base file name, we scan the macro definitions until we
24859 reach the first DW_MACINFO_start_file entry. We then initialize
24860 CURRENT_FILE accordingly so that any macro definition found before the
24861 first DW_MACINFO_start_file can still be associated to the base file. */
24862
24863 mac_ptr = section->buffer + offset;
24864 mac_end = section->buffer + section->size;
24865
24866 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24867 &offset_size, section_is_gnu);
24868 if (mac_ptr == NULL)
24869 {
24870 /* We already issued a complaint. */
24871 return;
24872 }
24873
24874 do
24875 {
24876 /* Do we at least have room for a macinfo type byte? */
24877 if (mac_ptr >= mac_end)
24878 {
24879 /* Complaint is printed during the second pass as GDB will probably
24880 stop the first pass earlier upon finding
24881 DW_MACINFO_start_file. */
24882 break;
24883 }
24884
aead7601 24885 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24886 mac_ptr++;
24887
24888 /* Note that we rely on the fact that the corresponding GNU and
24889 DWARF constants are the same. */
132448f8
SM
24890 DIAGNOSTIC_PUSH
24891 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24892 switch (macinfo_type)
24893 {
24894 /* A zero macinfo type indicates the end of the macro
24895 information. */
24896 case 0:
24897 break;
24898
0af92d60
JK
24899 case DW_MACRO_define:
24900 case DW_MACRO_undef:
cf2c3c16
TT
24901 /* Only skip the data by MAC_PTR. */
24902 {
24903 unsigned int bytes_read;
24904
24905 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24906 mac_ptr += bytes_read;
24907 read_direct_string (abfd, mac_ptr, &bytes_read);
24908 mac_ptr += bytes_read;
24909 }
24910 break;
24911
0af92d60 24912 case DW_MACRO_start_file:
cf2c3c16
TT
24913 {
24914 unsigned int bytes_read;
24915 int line, file;
24916
24917 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24918 mac_ptr += bytes_read;
24919 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24920 mac_ptr += bytes_read;
24921
804d2729 24922 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24923 }
24924 break;
24925
0af92d60 24926 case DW_MACRO_end_file:
cf2c3c16
TT
24927 /* No data to skip by MAC_PTR. */
24928 break;
24929
0af92d60
JK
24930 case DW_MACRO_define_strp:
24931 case DW_MACRO_undef_strp:
24932 case DW_MACRO_define_sup:
24933 case DW_MACRO_undef_sup:
cf2c3c16
TT
24934 {
24935 unsigned int bytes_read;
24936
24937 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24938 mac_ptr += bytes_read;
24939 mac_ptr += offset_size;
24940 }
24941 break;
24942
0af92d60
JK
24943 case DW_MACRO_import:
24944 case DW_MACRO_import_sup:
cf2c3c16 24945 /* Note that, according to the spec, a transparent include
0af92d60 24946 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24947 skip this opcode. */
24948 mac_ptr += offset_size;
24949 break;
24950
24951 case DW_MACINFO_vendor_ext:
24952 /* Only skip the data by MAC_PTR. */
24953 if (!section_is_gnu)
24954 {
24955 unsigned int bytes_read;
24956
24957 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24958 mac_ptr += bytes_read;
24959 read_direct_string (abfd, mac_ptr, &bytes_read);
24960 mac_ptr += bytes_read;
24961 }
24962 /* FALLTHROUGH */
24963
24964 default:
24965 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24966 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24967 section);
24968 if (mac_ptr == NULL)
24969 return;
24970 break;
24971 }
132448f8 24972 DIAGNOSTIC_POP
cf2c3c16
TT
24973 } while (macinfo_type != 0 && current_file == NULL);
24974
24975 /* Second pass: Process all entries.
24976
24977 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24978 command-line macro definitions/undefinitions. This flag is unset when we
24979 reach the first DW_MACINFO_start_file entry. */
24980
fc4007c9
TT
24981 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24982 htab_eq_pointer,
24983 NULL, xcalloc, xfree));
8fc3fc34 24984 mac_ptr = section->buffer + offset;
fc4007c9 24985 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24986 *slot = (void *) mac_ptr;
804d2729 24987 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24988 current_file, lh, section,
fc4007c9
TT
24989 section_is_gnu, 0, offset_size,
24990 include_hash.get ());
cf2c3c16
TT
24991}
24992
8e19ed76 24993/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24994 if so return true else false. */
380bca97 24995
8e19ed76 24996static int
6e5a29e1 24997attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24998{
24999 return (attr == NULL ? 0 :
25000 attr->form == DW_FORM_block1
25001 || attr->form == DW_FORM_block2
25002 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25003 || attr->form == DW_FORM_block
25004 || attr->form == DW_FORM_exprloc);
8e19ed76 25005}
4c2df51b 25006
c6a0999f
JB
25007/* Return non-zero if ATTR's value is a section offset --- classes
25008 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25009 You may use DW_UNSND (attr) to retrieve such offsets.
25010
25011 Section 7.5.4, "Attribute Encodings", explains that no attribute
25012 may have a value that belongs to more than one of these classes; it
25013 would be ambiguous if we did, because we use the same forms for all
25014 of them. */
380bca97 25015
3690dd37 25016static int
6e5a29e1 25017attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25018{
25019 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25020 || attr->form == DW_FORM_data8
25021 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25022}
25023
3690dd37
JB
25024/* Return non-zero if ATTR's value falls in the 'constant' class, or
25025 zero otherwise. When this function returns true, you can apply
25026 dwarf2_get_attr_constant_value to it.
25027
25028 However, note that for some attributes you must check
25029 attr_form_is_section_offset before using this test. DW_FORM_data4
25030 and DW_FORM_data8 are members of both the constant class, and of
25031 the classes that contain offsets into other debug sections
25032 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25033 that, if an attribute's can be either a constant or one of the
25034 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25035 taken as section offsets, not constants.
25036
25037 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25038 cannot handle that. */
380bca97 25039
3690dd37 25040static int
6e5a29e1 25041attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25042{
25043 switch (attr->form)
25044 {
25045 case DW_FORM_sdata:
25046 case DW_FORM_udata:
25047 case DW_FORM_data1:
25048 case DW_FORM_data2:
25049 case DW_FORM_data4:
25050 case DW_FORM_data8:
663c44ac 25051 case DW_FORM_implicit_const:
3690dd37
JB
25052 return 1;
25053 default:
25054 return 0;
25055 }
25056}
25057
7771576e
SA
25058
25059/* DW_ADDR is always stored already as sect_offset; despite for the forms
25060 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25061
25062static int
6e5a29e1 25063attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25064{
25065 switch (attr->form)
25066 {
25067 case DW_FORM_ref_addr:
25068 case DW_FORM_ref1:
25069 case DW_FORM_ref2:
25070 case DW_FORM_ref4:
25071 case DW_FORM_ref8:
25072 case DW_FORM_ref_udata:
25073 case DW_FORM_GNU_ref_alt:
25074 return 1;
25075 default:
25076 return 0;
25077 }
25078}
25079
3019eac3
DE
25080/* Return the .debug_loc section to use for CU.
25081 For DWO files use .debug_loc.dwo. */
25082
25083static struct dwarf2_section_info *
25084cu_debug_loc_section (struct dwarf2_cu *cu)
25085{
518817b3
SM
25086 struct dwarf2_per_objfile *dwarf2_per_objfile
25087 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25088
3019eac3 25089 if (cu->dwo_unit)
43988095
JK
25090 {
25091 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25092
25093 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25094 }
25095 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25096 : &dwarf2_per_objfile->loc);
3019eac3
DE
25097}
25098
8cf6f0b1
TT
25099/* A helper function that fills in a dwarf2_loclist_baton. */
25100
25101static void
25102fill_in_loclist_baton (struct dwarf2_cu *cu,
25103 struct dwarf2_loclist_baton *baton,
ff39bb5e 25104 const struct attribute *attr)
8cf6f0b1 25105{
518817b3
SM
25106 struct dwarf2_per_objfile *dwarf2_per_objfile
25107 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25108 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25109
25110 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25111
25112 baton->per_cu = cu->per_cu;
25113 gdb_assert (baton->per_cu);
25114 /* We don't know how long the location list is, but make sure we
25115 don't run off the edge of the section. */
3019eac3
DE
25116 baton->size = section->size - DW_UNSND (attr);
25117 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25118 baton->base_address = cu->base_address;
f664829e 25119 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25120}
25121
4c2df51b 25122static void
ff39bb5e 25123dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25124 struct dwarf2_cu *cu, int is_block)
4c2df51b 25125{
518817b3
SM
25126 struct dwarf2_per_objfile *dwarf2_per_objfile
25127 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25128 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25129 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25130
3690dd37 25131 if (attr_form_is_section_offset (attr)
3019eac3 25132 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25133 the section. If so, fall through to the complaint in the
25134 other branch. */
3019eac3 25135 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25136 {
0d53c4c4 25137 struct dwarf2_loclist_baton *baton;
4c2df51b 25138
8d749320 25139 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25140
8cf6f0b1 25141 fill_in_loclist_baton (cu, baton, attr);
be391dca 25142
d00adf39 25143 if (cu->base_known == 0)
b98664d3 25144 complaint (_("Location list used without "
3e43a32a 25145 "specifying the CU base address."));
4c2df51b 25146
f1e6e072
TT
25147 SYMBOL_ACLASS_INDEX (sym) = (is_block
25148 ? dwarf2_loclist_block_index
25149 : dwarf2_loclist_index);
0d53c4c4
DJ
25150 SYMBOL_LOCATION_BATON (sym) = baton;
25151 }
25152 else
25153 {
25154 struct dwarf2_locexpr_baton *baton;
25155
8d749320 25156 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25157 baton->per_cu = cu->per_cu;
25158 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25159
25160 if (attr_form_is_block (attr))
25161 {
25162 /* Note that we're just copying the block's data pointer
25163 here, not the actual data. We're still pointing into the
6502dd73
DJ
25164 info_buffer for SYM's objfile; right now we never release
25165 that buffer, but when we do clean up properly this may
25166 need to change. */
0d53c4c4
DJ
25167 baton->size = DW_BLOCK (attr)->size;
25168 baton->data = DW_BLOCK (attr)->data;
25169 }
25170 else
25171 {
25172 dwarf2_invalid_attrib_class_complaint ("location description",
25173 SYMBOL_NATURAL_NAME (sym));
25174 baton->size = 0;
0d53c4c4 25175 }
6e70227d 25176
f1e6e072
TT
25177 SYMBOL_ACLASS_INDEX (sym) = (is_block
25178 ? dwarf2_locexpr_block_index
25179 : dwarf2_locexpr_index);
0d53c4c4
DJ
25180 SYMBOL_LOCATION_BATON (sym) = baton;
25181 }
4c2df51b 25182}
6502dd73 25183
9aa1f1e3
TT
25184/* Return the OBJFILE associated with the compilation unit CU. If CU
25185 came from a separate debuginfo file, then the master objfile is
25186 returned. */
ae0d2f24
UW
25187
25188struct objfile *
25189dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25190{
e3b94546 25191 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25192
25193 /* Return the master objfile, so that we can report and look up the
25194 correct file containing this variable. */
25195 if (objfile->separate_debug_objfile_backlink)
25196 objfile = objfile->separate_debug_objfile_backlink;
25197
25198 return objfile;
25199}
25200
96408a79
SA
25201/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25202 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25203 CU_HEADERP first. */
25204
25205static const struct comp_unit_head *
25206per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25207 struct dwarf2_per_cu_data *per_cu)
25208{
d521ce57 25209 const gdb_byte *info_ptr;
96408a79
SA
25210
25211 if (per_cu->cu)
25212 return &per_cu->cu->header;
25213
9c541725 25214 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25215
25216 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25217 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25218 rcuh_kind::COMPILE);
96408a79
SA
25219
25220 return cu_headerp;
25221}
25222
ae0d2f24
UW
25223/* Return the address size given in the compilation unit header for CU. */
25224
98714339 25225int
ae0d2f24
UW
25226dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25227{
96408a79
SA
25228 struct comp_unit_head cu_header_local;
25229 const struct comp_unit_head *cu_headerp;
c471e790 25230
96408a79
SA
25231 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25232
25233 return cu_headerp->addr_size;
ae0d2f24
UW
25234}
25235
9eae7c52
TT
25236/* Return the offset size given in the compilation unit header for CU. */
25237
25238int
25239dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25240{
96408a79
SA
25241 struct comp_unit_head cu_header_local;
25242 const struct comp_unit_head *cu_headerp;
9c6c53f7 25243
96408a79
SA
25244 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25245
25246 return cu_headerp->offset_size;
25247}
25248
25249/* See its dwarf2loc.h declaration. */
25250
25251int
25252dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25253{
25254 struct comp_unit_head cu_header_local;
25255 const struct comp_unit_head *cu_headerp;
25256
25257 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25258
25259 if (cu_headerp->version == 2)
25260 return cu_headerp->addr_size;
25261 else
25262 return cu_headerp->offset_size;
181cebd4
JK
25263}
25264
9aa1f1e3
TT
25265/* Return the text offset of the CU. The returned offset comes from
25266 this CU's objfile. If this objfile came from a separate debuginfo
25267 file, then the offset may be different from the corresponding
25268 offset in the parent objfile. */
25269
25270CORE_ADDR
25271dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25272{
e3b94546 25273 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25274
25275 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25276}
25277
43988095
JK
25278/* Return DWARF version number of PER_CU. */
25279
25280short
25281dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25282{
25283 return per_cu->dwarf_version;
25284}
25285
348e048f
DE
25286/* Locate the .debug_info compilation unit from CU's objfile which contains
25287 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25288
25289static struct dwarf2_per_cu_data *
9c541725 25290dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25291 unsigned int offset_in_dwz,
ed2dc618 25292 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25293{
25294 struct dwarf2_per_cu_data *this_cu;
25295 int low, high;
25296
ae038cb0 25297 low = 0;
b76e467d 25298 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25299 while (high > low)
25300 {
36586728 25301 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25302 int mid = low + (high - low) / 2;
9a619af0 25303
36586728 25304 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25305 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25306 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25307 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25308 high = mid;
25309 else
25310 low = mid + 1;
25311 }
25312 gdb_assert (low == high);
36586728 25313 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25314 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25315 {
36586728 25316 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25317 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25318 "offset %s [in module %s]"),
25319 sect_offset_str (sect_off),
ed2dc618 25320 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25321
9c541725
PA
25322 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25323 <= sect_off);
ae038cb0
DJ
25324 return dwarf2_per_objfile->all_comp_units[low-1];
25325 }
25326 else
25327 {
b76e467d 25328 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25329 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25330 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25331 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25332 return this_cu;
25333 }
25334}
25335
23745b47 25336/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25337
fcd3b13d
SM
25338dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25339 : per_cu (per_cu_),
9068261f
AB
25340 mark (false),
25341 has_loclist (false),
25342 checked_producer (false),
25343 producer_is_gxx_lt_4_6 (false),
25344 producer_is_gcc_lt_4_3 (false),
eb77c9df 25345 producer_is_icc (false),
9068261f 25346 producer_is_icc_lt_14 (false),
c258c396 25347 producer_is_codewarrior (false),
9068261f 25348 processing_has_namespace_info (false)
93311388 25349{
fcd3b13d
SM
25350 per_cu->cu = this;
25351}
25352
25353/* Destroy a dwarf2_cu. */
25354
25355dwarf2_cu::~dwarf2_cu ()
25356{
25357 per_cu->cu = NULL;
9816fde3
JK
25358}
25359
25360/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25361
25362static void
95554aad
TT
25363prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25364 enum language pretend_language)
9816fde3
JK
25365{
25366 struct attribute *attr;
25367
25368 /* Set the language we're debugging. */
25369 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25370 if (attr)
25371 set_cu_language (DW_UNSND (attr), cu);
25372 else
9cded63f 25373 {
95554aad 25374 cu->language = pretend_language;
9cded63f
TT
25375 cu->language_defn = language_def (cu->language);
25376 }
dee91e82 25377
7d45c7c3 25378 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25379}
25380
ae038cb0
DJ
25381/* Increase the age counter on each cached compilation unit, and free
25382 any that are too old. */
25383
25384static void
ed2dc618 25385age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25386{
25387 struct dwarf2_per_cu_data *per_cu, **last_chain;
25388
25389 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25390 per_cu = dwarf2_per_objfile->read_in_chain;
25391 while (per_cu != NULL)
25392 {
25393 per_cu->cu->last_used ++;
b4f54984 25394 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25395 dwarf2_mark (per_cu->cu);
25396 per_cu = per_cu->cu->read_in_chain;
25397 }
25398
25399 per_cu = dwarf2_per_objfile->read_in_chain;
25400 last_chain = &dwarf2_per_objfile->read_in_chain;
25401 while (per_cu != NULL)
25402 {
25403 struct dwarf2_per_cu_data *next_cu;
25404
25405 next_cu = per_cu->cu->read_in_chain;
25406
25407 if (!per_cu->cu->mark)
25408 {
fcd3b13d 25409 delete per_cu->cu;
ae038cb0
DJ
25410 *last_chain = next_cu;
25411 }
25412 else
25413 last_chain = &per_cu->cu->read_in_chain;
25414
25415 per_cu = next_cu;
25416 }
25417}
25418
25419/* Remove a single compilation unit from the cache. */
25420
25421static void
dee91e82 25422free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25423{
25424 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25425 struct dwarf2_per_objfile *dwarf2_per_objfile
25426 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25427
25428 per_cu = dwarf2_per_objfile->read_in_chain;
25429 last_chain = &dwarf2_per_objfile->read_in_chain;
25430 while (per_cu != NULL)
25431 {
25432 struct dwarf2_per_cu_data *next_cu;
25433
25434 next_cu = per_cu->cu->read_in_chain;
25435
dee91e82 25436 if (per_cu == target_per_cu)
ae038cb0 25437 {
fcd3b13d 25438 delete per_cu->cu;
dee91e82 25439 per_cu->cu = NULL;
ae038cb0
DJ
25440 *last_chain = next_cu;
25441 break;
25442 }
25443 else
25444 last_chain = &per_cu->cu->read_in_chain;
25445
25446 per_cu = next_cu;
25447 }
25448}
25449
dee91e82
DE
25450/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25451 We store these in a hash table separate from the DIEs, and preserve them
25452 when the DIEs are flushed out of cache.
25453
25454 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25455 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25456 or the type may come from a DWO file. Furthermore, while it's more logical
25457 to use per_cu->section+offset, with Fission the section with the data is in
25458 the DWO file but we don't know that section at the point we need it.
25459 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25460 because we can enter the lookup routine, get_die_type_at_offset, from
25461 outside this file, and thus won't necessarily have PER_CU->cu.
25462 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25463
dee91e82 25464struct dwarf2_per_cu_offset_and_type
1c379e20 25465{
dee91e82 25466 const struct dwarf2_per_cu_data *per_cu;
9c541725 25467 sect_offset sect_off;
1c379e20
DJ
25468 struct type *type;
25469};
25470
dee91e82 25471/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25472
25473static hashval_t
dee91e82 25474per_cu_offset_and_type_hash (const void *item)
1c379e20 25475{
9a3c8263
SM
25476 const struct dwarf2_per_cu_offset_and_type *ofs
25477 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25478
9c541725 25479 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25480}
25481
dee91e82 25482/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25483
25484static int
dee91e82 25485per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25486{
9a3c8263
SM
25487 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25488 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25489 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25490 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25491
dee91e82 25492 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25493 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25494}
25495
25496/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25497 table if necessary. For convenience, return TYPE.
25498
25499 The DIEs reading must have careful ordering to:
25500 * Not cause infite loops trying to read in DIEs as a prerequisite for
25501 reading current DIE.
25502 * Not trying to dereference contents of still incompletely read in types
25503 while reading in other DIEs.
25504 * Enable referencing still incompletely read in types just by a pointer to
25505 the type without accessing its fields.
25506
25507 Therefore caller should follow these rules:
25508 * Try to fetch any prerequisite types we may need to build this DIE type
25509 before building the type and calling set_die_type.
e71ec853 25510 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25511 possible before fetching more types to complete the current type.
25512 * Make the type as complete as possible before fetching more types. */
1c379e20 25513
f792889a 25514static struct type *
1c379e20
DJ
25515set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25516{
518817b3
SM
25517 struct dwarf2_per_objfile *dwarf2_per_objfile
25518 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25519 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25520 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25521 struct attribute *attr;
25522 struct dynamic_prop prop;
1c379e20 25523
b4ba55a1
JB
25524 /* For Ada types, make sure that the gnat-specific data is always
25525 initialized (if not already set). There are a few types where
25526 we should not be doing so, because the type-specific area is
25527 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25528 where the type-specific area is used to store the floatformat).
25529 But this is not a problem, because the gnat-specific information
25530 is actually not needed for these types. */
25531 if (need_gnat_info (cu)
25532 && TYPE_CODE (type) != TYPE_CODE_FUNC
25533 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25534 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25535 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25536 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25537 && !HAVE_GNAT_AUX_INFO (type))
25538 INIT_GNAT_SPECIFIC (type);
25539
3f2f83dd
KB
25540 /* Read DW_AT_allocated and set in type. */
25541 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25542 if (attr_form_is_block (attr))
25543 {
25544 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25545 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25546 }
25547 else if (attr != NULL)
25548 {
b98664d3 25549 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25550 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25551 sect_offset_str (die->sect_off));
3f2f83dd
KB
25552 }
25553
25554 /* Read DW_AT_associated and set in type. */
25555 attr = dwarf2_attr (die, DW_AT_associated, cu);
25556 if (attr_form_is_block (attr))
25557 {
25558 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25559 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25560 }
25561 else if (attr != NULL)
25562 {
b98664d3 25563 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25564 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25565 sect_offset_str (die->sect_off));
3f2f83dd
KB
25566 }
25567
3cdcd0ce
JB
25568 /* Read DW_AT_data_location and set in type. */
25569 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25570 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25571 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25572
dee91e82 25573 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25574 {
dee91e82
DE
25575 dwarf2_per_objfile->die_type_hash =
25576 htab_create_alloc_ex (127,
25577 per_cu_offset_and_type_hash,
25578 per_cu_offset_and_type_eq,
25579 NULL,
25580 &objfile->objfile_obstack,
25581 hashtab_obstack_allocate,
25582 dummy_obstack_deallocate);
f792889a 25583 }
1c379e20 25584
dee91e82 25585 ofs.per_cu = cu->per_cu;
9c541725 25586 ofs.sect_off = die->sect_off;
1c379e20 25587 ofs.type = type;
dee91e82
DE
25588 slot = (struct dwarf2_per_cu_offset_and_type **)
25589 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25590 if (*slot)
b98664d3 25591 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25592 sect_offset_str (die->sect_off));
8d749320
SM
25593 *slot = XOBNEW (&objfile->objfile_obstack,
25594 struct dwarf2_per_cu_offset_and_type);
1c379e20 25595 **slot = ofs;
f792889a 25596 return type;
1c379e20
DJ
25597}
25598
9c541725 25599/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25600 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25601
25602static struct type *
9c541725 25603get_die_type_at_offset (sect_offset sect_off,
673bfd45 25604 struct dwarf2_per_cu_data *per_cu)
1c379e20 25605{
dee91e82 25606 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25607 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25608
dee91e82 25609 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25610 return NULL;
1c379e20 25611
dee91e82 25612 ofs.per_cu = per_cu;
9c541725 25613 ofs.sect_off = sect_off;
9a3c8263
SM
25614 slot = ((struct dwarf2_per_cu_offset_and_type *)
25615 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25616 if (slot)
25617 return slot->type;
25618 else
25619 return NULL;
25620}
25621
02142a6c 25622/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25623 or return NULL if DIE does not have a saved type. */
25624
25625static struct type *
25626get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25627{
9c541725 25628 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25629}
25630
10b3939b
DJ
25631/* Add a dependence relationship from CU to REF_PER_CU. */
25632
25633static void
25634dwarf2_add_dependence (struct dwarf2_cu *cu,
25635 struct dwarf2_per_cu_data *ref_per_cu)
25636{
25637 void **slot;
25638
25639 if (cu->dependencies == NULL)
25640 cu->dependencies
25641 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25642 NULL, &cu->comp_unit_obstack,
25643 hashtab_obstack_allocate,
25644 dummy_obstack_deallocate);
25645
25646 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25647 if (*slot == NULL)
25648 *slot = ref_per_cu;
25649}
1c379e20 25650
f504f079
DE
25651/* Subroutine of dwarf2_mark to pass to htab_traverse.
25652 Set the mark field in every compilation unit in the
ae038cb0
DJ
25653 cache that we must keep because we are keeping CU. */
25654
10b3939b
DJ
25655static int
25656dwarf2_mark_helper (void **slot, void *data)
25657{
25658 struct dwarf2_per_cu_data *per_cu;
25659
25660 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25661
25662 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25663 reading of the chain. As such dependencies remain valid it is not much
25664 useful to track and undo them during QUIT cleanups. */
25665 if (per_cu->cu == NULL)
25666 return 1;
25667
10b3939b
DJ
25668 if (per_cu->cu->mark)
25669 return 1;
9068261f 25670 per_cu->cu->mark = true;
10b3939b
DJ
25671
25672 if (per_cu->cu->dependencies != NULL)
25673 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25674
25675 return 1;
25676}
25677
f504f079
DE
25678/* Set the mark field in CU and in every other compilation unit in the
25679 cache that we must keep because we are keeping CU. */
25680
ae038cb0
DJ
25681static void
25682dwarf2_mark (struct dwarf2_cu *cu)
25683{
25684 if (cu->mark)
25685 return;
9068261f 25686 cu->mark = true;
10b3939b
DJ
25687 if (cu->dependencies != NULL)
25688 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25689}
25690
25691static void
25692dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25693{
25694 while (per_cu)
25695 {
9068261f 25696 per_cu->cu->mark = false;
ae038cb0
DJ
25697 per_cu = per_cu->cu->read_in_chain;
25698 }
72bf9492
DJ
25699}
25700
72bf9492
DJ
25701/* Trivial hash function for partial_die_info: the hash value of a DIE
25702 is its offset in .debug_info for this objfile. */
25703
25704static hashval_t
25705partial_die_hash (const void *item)
25706{
9a3c8263
SM
25707 const struct partial_die_info *part_die
25708 = (const struct partial_die_info *) item;
9a619af0 25709
9c541725 25710 return to_underlying (part_die->sect_off);
72bf9492
DJ
25711}
25712
25713/* Trivial comparison function for partial_die_info structures: two DIEs
25714 are equal if they have the same offset. */
25715
25716static int
25717partial_die_eq (const void *item_lhs, const void *item_rhs)
25718{
9a3c8263
SM
25719 const struct partial_die_info *part_die_lhs
25720 = (const struct partial_die_info *) item_lhs;
25721 const struct partial_die_info *part_die_rhs
25722 = (const struct partial_die_info *) item_rhs;
9a619af0 25723
9c541725 25724 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25725}
25726
3c3bb058
AB
25727struct cmd_list_element *set_dwarf_cmdlist;
25728struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25729
25730static void
981a3fb3 25731set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25732{
b4f54984 25733 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25734 gdb_stdout);
ae038cb0
DJ
25735}
25736
25737static void
981a3fb3 25738show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25739{
b4f54984 25740 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25741}
25742
cd4fb1b2 25743int dwarf_always_disassemble;
437afbb8 25744
437afbb8 25745static void
cd4fb1b2
SM
25746show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25747 struct cmd_list_element *c, const char *value)
9291a0cd 25748{
cd4fb1b2
SM
25749 fprintf_filtered (file,
25750 _("Whether to always disassemble "
25751 "DWARF expressions is %s.\n"),
25752 value);
9291a0cd
TT
25753}
25754
9291a0cd 25755static void
cd4fb1b2
SM
25756show_check_physname (struct ui_file *file, int from_tty,
25757 struct cmd_list_element *c, const char *value)
9291a0cd 25758{
cd4fb1b2
SM
25759 fprintf_filtered (file,
25760 _("Whether to check \"physname\" is %s.\n"),
25761 value);
9291a0cd
TT
25762}
25763
cd4fb1b2
SM
25764void
25765_initialize_dwarf2_read (void)
9291a0cd 25766{
cd4fb1b2
SM
25767 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25768Set DWARF specific variables.\n\
25769Configure DWARF variables such as the cache size"),
25770 &set_dwarf_cmdlist, "maintenance set dwarf ",
25771 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25772
cd4fb1b2
SM
25773 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25774Show DWARF specific variables\n\
25775Show DWARF variables such as the cache size"),
25776 &show_dwarf_cmdlist, "maintenance show dwarf ",
25777 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25778
cd4fb1b2
SM
25779 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25780 &dwarf_max_cache_age, _("\
25781Set the upper bound on the age of cached DWARF compilation units."), _("\
25782Show the upper bound on the age of cached DWARF compilation units."), _("\
25783A higher limit means that cached compilation units will be stored\n\
25784in memory longer, and more total memory will be used. Zero disables\n\
25785caching, which can slow down startup."),
25786 NULL,
25787 show_dwarf_max_cache_age,
25788 &set_dwarf_cmdlist,
25789 &show_dwarf_cmdlist);
156942c7 25790
cd4fb1b2
SM
25791 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25792 &dwarf_always_disassemble, _("\
25793Set whether `info address' always disassembles DWARF expressions."), _("\
25794Show whether `info address' always disassembles DWARF expressions."), _("\
25795When enabled, DWARF expressions are always printed in an assembly-like\n\
25796syntax. When disabled, expressions will be printed in a more\n\
25797conversational style, when possible."),
25798 NULL,
25799 show_dwarf_always_disassemble,
25800 &set_dwarf_cmdlist,
25801 &show_dwarf_cmdlist);
9291a0cd 25802
cd4fb1b2
SM
25803 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25804Set debugging of the DWARF reader."), _("\
25805Show debugging of the DWARF reader."), _("\
25806When enabled (non-zero), debugging messages are printed during DWARF\n\
25807reading and symtab expansion. A value of 1 (one) provides basic\n\
25808information. A value greater than 1 provides more verbose information."),
25809 NULL,
25810 NULL,
25811 &setdebuglist, &showdebuglist);
9291a0cd 25812
cd4fb1b2
SM
25813 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25814Set debugging of the DWARF DIE reader."), _("\
25815Show debugging of the DWARF DIE reader."), _("\
25816When enabled (non-zero), DIEs are dumped after they are read in.\n\
25817The value is the maximum depth to print."),
25818 NULL,
25819 NULL,
25820 &setdebuglist, &showdebuglist);
9291a0cd 25821
cd4fb1b2
SM
25822 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25823Set debugging of the dwarf line reader."), _("\
25824Show debugging of the dwarf line reader."), _("\
25825When enabled (non-zero), line number entries are dumped as they are read in.\n\
25826A value of 1 (one) provides basic information.\n\
25827A value greater than 1 provides more verbose information."),
25828 NULL,
25829 NULL,
25830 &setdebuglist, &showdebuglist);
437afbb8 25831
cd4fb1b2
SM
25832 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25833Set cross-checking of \"physname\" code against demangler."), _("\
25834Show cross-checking of \"physname\" code against demangler."), _("\
25835When enabled, GDB's internal \"physname\" code is checked against\n\
25836the demangler."),
25837 NULL, show_check_physname,
25838 &setdebuglist, &showdebuglist);
900e11f9 25839
e615022a
DE
25840 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25841 no_class, &use_deprecated_index_sections, _("\
25842Set whether to use deprecated gdb_index sections."), _("\
25843Show whether to use deprecated gdb_index sections."), _("\
25844When enabled, deprecated .gdb_index sections are used anyway.\n\
25845Normally they are ignored either because of a missing feature or\n\
25846performance issue.\n\
25847Warning: This option must be enabled before gdb reads the file."),
25848 NULL,
25849 NULL,
25850 &setlist, &showlist);
25851
f1e6e072
TT
25852 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25853 &dwarf2_locexpr_funcs);
25854 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25855 &dwarf2_loclist_funcs);
25856
25857 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25858 &dwarf2_block_frame_base_locexpr_funcs);
25859 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25860 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25861
25862#if GDB_SELF_TEST
25863 selftests::register_test ("dw2_expand_symtabs_matching",
25864 selftests::dw2_expand_symtabs_matching::run_test);
25865#endif
6502dd73 25866}
This page took 4.822826 seconds and 4 git commands to generate.