Automatic date update in version.in
[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"
268a13a5 61#include "gdbsupport/vec.h"
4de283e4 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"
268a13a5 71#include "gdbsupport/filestuff.h"
4de283e4 72#include "build-id.h"
d55e5aa6 73#include "namespace.h"
268a13a5
TT
74#include "gdbsupport/gdb_unlinker.h"
75#include "gdbsupport/function-view.h"
76#include "gdbsupport/gdb_optional.h"
77#include "gdbsupport/underlying.h"
78#include "gdbsupport/byte-vector.h"
79#include "gdbsupport/hash_enum.h"
4de283e4 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>
268a13a5 87#include "gdbsupport/selftest.h"
4de283e4
TT
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
268a13a5 92#include "gdbsupport/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
491144b5
CB
105/* When true, cross-check physname against demangler. */
106static bool check_physname = false;
900e11f9 107
491144b5
CB
108/* When true, do not reject deprecated .gdb_index sections. */
109static bool use_deprecated_index_sections = false;
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>
3b00ef10
TT
182 find_name_components_bounds (const lookup_name_info &ln_no_params,
183 enum language lang) const;
44ed8f3e
PA
184
185 /* Prevent deleting/destroying via a base class pointer. */
186protected:
187 ~mapped_index_base() = default;
188};
189
9291a0cd
TT
190/* A description of the mapped index. The file format is described in
191 a comment by the code that writes the index. */
fc898b42 192struct mapped_index final : public mapped_index_base
9291a0cd 193{
f00a2de2
PA
194 /* A slot/bucket in the symbol table hash. */
195 struct symbol_table_slot
196 {
197 const offset_type name;
198 const offset_type vec;
199 };
200
559a7a62 201 /* Index data format version. */
3063847f 202 int version = 0;
559a7a62 203
f00a2de2
PA
204 /* The address table data. */
205 gdb::array_view<const gdb_byte> address_table;
b11b1f88 206
3876f04e 207 /* The symbol table, implemented as a hash table. */
f00a2de2 208 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 209
9291a0cd 210 /* A pointer to the constant pool. */
3063847f 211 const char *constant_pool = nullptr;
3f563c84 212
44ed8f3e
PA
213 bool symbol_name_slot_invalid (offset_type idx) const override
214 {
215 const auto &bucket = this->symbol_table[idx];
9ab08412 216 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 217 }
5c58de74 218
3f563c84
PA
219 /* Convenience method to get at the name of the symbol at IDX in the
220 symbol table. */
44ed8f3e 221 const char *symbol_name_at (offset_type idx) const override
f00a2de2 222 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 223
44ed8f3e
PA
224 size_t symbol_name_count () const override
225 { return this->symbol_table.size (); }
9291a0cd
TT
226};
227
927aa2e7
JK
228/* A description of the mapped .debug_names.
229 Uninitialized map has CU_COUNT 0. */
fc898b42 230struct mapped_debug_names final : public mapped_index_base
927aa2e7 231{
ed2dc618
SM
232 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
233 : dwarf2_per_objfile (dwarf2_per_objfile_)
234 {}
235
236 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
237 bfd_endian dwarf5_byte_order;
238 bool dwarf5_is_dwarf64;
239 bool augmentation_is_gdb;
240 uint8_t offset_size;
241 uint32_t cu_count = 0;
242 uint32_t tu_count, bucket_count, name_count;
243 const gdb_byte *cu_table_reordered, *tu_table_reordered;
244 const uint32_t *bucket_table_reordered, *hash_table_reordered;
245 const gdb_byte *name_table_string_offs_reordered;
246 const gdb_byte *name_table_entry_offs_reordered;
247 const gdb_byte *entry_pool;
248
249 struct index_val
250 {
251 ULONGEST dwarf_tag;
252 struct attr
253 {
254 /* Attribute name DW_IDX_*. */
255 ULONGEST dw_idx;
256
257 /* Attribute form DW_FORM_*. */
258 ULONGEST form;
259
260 /* Value if FORM is DW_FORM_implicit_const. */
261 LONGEST implicit_const;
262 };
263 std::vector<attr> attr_vec;
264 };
265
266 std::unordered_map<ULONGEST, index_val> abbrev_map;
267
268 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
269
270 /* Implementation of the mapped_index_base virtual interface, for
271 the name_components cache. */
272
273 const char *symbol_name_at (offset_type idx) const override
274 { return namei_to_name (idx); }
275
276 size_t symbol_name_count () const override
277 { return this->name_count; }
927aa2e7
JK
278};
279
cd4fb1b2 280/* See dwarf2read.h. */
ed2dc618 281
cd4fb1b2 282dwarf2_per_objfile *
ed2dc618
SM
283get_dwarf2_per_objfile (struct objfile *objfile)
284{
5bfd760d 285 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 286}
c906108c 287
251d32d9 288/* Default names of the debugging sections. */
c906108c 289
233a11ab
CS
290/* Note that if the debugging section has been compressed, it might
291 have a name like .zdebug_info. */
292
9cdd5dbd
DE
293static const struct dwarf2_debug_sections dwarf2_elf_names =
294{
251d32d9
TG
295 { ".debug_info", ".zdebug_info" },
296 { ".debug_abbrev", ".zdebug_abbrev" },
297 { ".debug_line", ".zdebug_line" },
298 { ".debug_loc", ".zdebug_loc" },
43988095 299 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 300 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 301 { ".debug_macro", ".zdebug_macro" },
251d32d9 302 { ".debug_str", ".zdebug_str" },
43988095 303 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 304 { ".debug_ranges", ".zdebug_ranges" },
43988095 305 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 306 { ".debug_types", ".zdebug_types" },
3019eac3 307 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
308 { ".debug_frame", ".zdebug_frame" },
309 { ".eh_frame", NULL },
24d3216f 310 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
311 { ".debug_names", ".zdebug_names" },
312 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 313 23
251d32d9 314};
c906108c 315
80626a55 316/* List of DWO/DWP sections. */
3019eac3 317
80626a55 318static const struct dwop_section_names
3019eac3
DE
319{
320 struct dwarf2_section_names abbrev_dwo;
321 struct dwarf2_section_names info_dwo;
322 struct dwarf2_section_names line_dwo;
323 struct dwarf2_section_names loc_dwo;
43988095 324 struct dwarf2_section_names loclists_dwo;
09262596
DE
325 struct dwarf2_section_names macinfo_dwo;
326 struct dwarf2_section_names macro_dwo;
3019eac3
DE
327 struct dwarf2_section_names str_dwo;
328 struct dwarf2_section_names str_offsets_dwo;
329 struct dwarf2_section_names types_dwo;
80626a55
DE
330 struct dwarf2_section_names cu_index;
331 struct dwarf2_section_names tu_index;
3019eac3 332}
80626a55 333dwop_section_names =
3019eac3
DE
334{
335 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
336 { ".debug_info.dwo", ".zdebug_info.dwo" },
337 { ".debug_line.dwo", ".zdebug_line.dwo" },
338 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 339 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
340 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
341 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
342 { ".debug_str.dwo", ".zdebug_str.dwo" },
343 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
344 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
345 { ".debug_cu_index", ".zdebug_cu_index" },
346 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
347};
348
c906108c
SS
349/* local data types */
350
107d2387
AC
351/* The data in a compilation unit header, after target2host
352 translation, looks like this. */
c906108c 353struct comp_unit_head
a738430d 354{
c764a876 355 unsigned int length;
a738430d 356 short version;
a738430d
MK
357 unsigned char addr_size;
358 unsigned char signed_addr_p;
9c541725 359 sect_offset abbrev_sect_off;
57349743 360
a738430d
MK
361 /* Size of file offsets; either 4 or 8. */
362 unsigned int offset_size;
57349743 363
a738430d
MK
364 /* Size of the length field; either 4 or 12. */
365 unsigned int initial_length_size;
57349743 366
43988095
JK
367 enum dwarf_unit_type unit_type;
368
a738430d
MK
369 /* Offset to the first byte of this compilation unit header in the
370 .debug_info section, for resolving relative reference dies. */
9c541725 371 sect_offset sect_off;
57349743 372
d00adf39
DE
373 /* Offset to first die in this cu from the start of the cu.
374 This will be the first byte following the compilation unit header. */
9c541725 375 cu_offset first_die_cu_offset;
43988095 376
a084a2a6
AT
377
378 /* 64-bit signature of this unit. For type units, it denotes the signature of
379 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
380 Also used in DWARF 5, to denote the dwo id when the unit type is
381 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
382 ULONGEST signature;
383
384 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 385 cu_offset type_cu_offset_in_tu;
a738430d 386};
c906108c 387
3da10d80
KS
388/* Type used for delaying computation of method physnames.
389 See comments for compute_delayed_physnames. */
390struct delayed_method_info
391{
392 /* The type to which the method is attached, i.e., its parent class. */
393 struct type *type;
394
395 /* The index of the method in the type's function fieldlists. */
396 int fnfield_index;
397
398 /* The index of the method in the fieldlist. */
399 int index;
400
401 /* The name of the DIE. */
402 const char *name;
403
404 /* The DIE associated with this method. */
405 struct die_info *die;
406};
407
e7c27a73
DJ
408/* Internal state when decoding a particular compilation unit. */
409struct dwarf2_cu
410{
fcd3b13d
SM
411 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
412 ~dwarf2_cu ();
413
414 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
415
c24bdb02
KS
416 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
417 Create the set of symtabs used by this TU, or if this TU is sharing
418 symtabs with another TU and the symtabs have already been created
419 then restore those symtabs in the line header.
420 We don't need the pc/line-number mapping for type units. */
421 void setup_type_unit_groups (struct die_info *die);
422
423 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
424 buildsym_compunit constructor. */
425 struct compunit_symtab *start_symtab (const char *name,
426 const char *comp_dir,
427 CORE_ADDR low_pc);
428
429 /* Reset the builder. */
430 void reset_builder () { m_builder.reset (); }
431
d00adf39 432 /* The header of the compilation unit. */
fcd3b13d 433 struct comp_unit_head header {};
e142c38c 434
d00adf39 435 /* Base address of this compilation unit. */
fcd3b13d 436 CORE_ADDR base_address = 0;
d00adf39
DE
437
438 /* Non-zero if base_address has been set. */
fcd3b13d 439 int base_known = 0;
d00adf39 440
e142c38c 441 /* The language we are debugging. */
fcd3b13d
SM
442 enum language language = language_unknown;
443 const struct language_defn *language_defn = nullptr;
e142c38c 444
fcd3b13d 445 const char *producer = nullptr;
b0f35d58 446
c24bdb02 447private:
804d2729
TT
448 /* The symtab builder for this CU. This is only non-NULL when full
449 symbols are being read. */
c24bdb02 450 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 451
c24bdb02 452public:
e142c38c
DJ
453 /* The generic symbol table building routines have separate lists for
454 file scope symbols and all all other scopes (local scopes). So
455 we need to select the right one to pass to add_symbol_to_list().
456 We do it by keeping a pointer to the correct list in list_in_scope.
457
458 FIXME: The original dwarf code just treated the file scope as the
459 first local scope, and all other local scopes as nested local
460 scopes, and worked fine. Check to see if we really need to
461 distinguish these in buildsym.c. */
fcd3b13d 462 struct pending **list_in_scope = nullptr;
e142c38c 463
b64f50a1
JK
464 /* Hash table holding all the loaded partial DIEs
465 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 466 htab_t partial_dies = nullptr;
72bf9492
DJ
467
468 /* Storage for things with the same lifetime as this read-in compilation
469 unit, including partial DIEs. */
fcd3b13d 470 auto_obstack comp_unit_obstack;
72bf9492 471
ae038cb0
DJ
472 /* When multiple dwarf2_cu structures are living in memory, this field
473 chains them all together, so that they can be released efficiently.
474 We will probably also want a generation counter so that most-recently-used
475 compilation units are cached... */
fcd3b13d 476 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 477
69d751e3 478 /* Backlink to our per_cu entry. */
ae038cb0
DJ
479 struct dwarf2_per_cu_data *per_cu;
480
481 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 482 int last_used = 0;
ae038cb0 483
b64f50a1
JK
484 /* A hash table of DIE cu_offset for following references with
485 die_info->offset.sect_off as hash. */
fcd3b13d 486 htab_t die_hash = nullptr;
10b3939b
DJ
487
488 /* Full DIEs if read in. */
fcd3b13d 489 struct die_info *dies = nullptr;
10b3939b
DJ
490
491 /* A set of pointers to dwarf2_per_cu_data objects for compilation
492 units referenced by this one. Only set during full symbol processing;
493 partial symbol tables do not have dependencies. */
fcd3b13d 494 htab_t dependencies = nullptr;
10b3939b 495
cb1df416 496 /* Header data from the line table, during full symbol processing. */
fcd3b13d 497 struct line_header *line_header = nullptr;
4c8aa72d
PA
498 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
499 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
500 this is the DW_TAG_compile_unit die for this CU. We'll hold on
501 to the line header as long as this DIE is being processed. See
502 process_die_scope. */
fcd3b13d 503 die_info *line_header_die_owner = nullptr;
cb1df416 504
3da10d80
KS
505 /* A list of methods which need to have physnames computed
506 after all type information has been read. */
c89b44cd 507 std::vector<delayed_method_info> method_list;
3da10d80 508
96408a79 509 /* To be copied to symtab->call_site_htab. */
fcd3b13d 510 htab_t call_site_htab = nullptr;
96408a79 511
034e5797
DE
512 /* Non-NULL if this CU came from a DWO file.
513 There is an invariant here that is important to remember:
514 Except for attributes copied from the top level DIE in the "main"
515 (or "stub") file in preparation for reading the DWO file
516 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
517 Either there isn't a DWO file (in which case this is NULL and the point
518 is moot), or there is and either we're not going to read it (in which
519 case this is NULL) or there is and we are reading it (in which case this
520 is non-NULL). */
fcd3b13d 521 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
522
523 /* The DW_AT_addr_base attribute if present, zero otherwise
524 (zero is a valid value though).
1dbab08b 525 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 526 ULONGEST addr_base = 0;
3019eac3 527
2e3cf129
DE
528 /* The DW_AT_ranges_base attribute if present, zero otherwise
529 (zero is a valid value though).
1dbab08b 530 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 531 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
536 DW_AT_ranges_base *would* have to be applied, and we'd have to care
537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 538 ULONGEST ranges_base = 0;
2e3cf129 539
c9317f21
TT
540 /* When reading debug info generated by older versions of rustc, we
541 have to rewrite some union types to be struct types with a
542 variant part. This rewriting must be done after the CU is fully
543 read in, because otherwise at the point of rewriting some struct
544 type might not have been fully processed. So, we keep a list of
545 all such types here and process them after expansion. */
546 std::vector<struct type *> rust_unions;
547
ae038cb0 548 /* Mark used when releasing cached dies. */
9068261f 549 bool mark : 1;
ae038cb0 550
8be455d7
JK
551 /* This CU references .debug_loc. See the symtab->locations_valid field.
552 This test is imperfect as there may exist optimized debug code not using
553 any location list and still facing inlining issues if handled as
554 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 555 bool has_loclist : 1;
ba919b58 556
9068261f 557 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
558 if all the producer_is_* fields are valid. This information is cached
559 because profiling CU expansion showed excessive time spent in
560 producer_is_gxx_lt_4_6. */
9068261f
AB
561 bool checked_producer : 1;
562 bool producer_is_gxx_lt_4_6 : 1;
563 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 564 bool producer_is_icc : 1;
9068261f 565 bool producer_is_icc_lt_14 : 1;
c258c396 566 bool producer_is_codewarrior : 1;
4d4ec4e5 567
9068261f 568 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
569 debugging info for C++ namespaces. GCC 3.3.x did not produce
570 this information, but later versions do. */
571
9068261f 572 bool processing_has_namespace_info : 1;
d590ff25
YQ
573
574 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
575
576 /* If this CU was inherited by another CU (via specification,
577 abstract_origin, etc), this is the ancestor CU. */
578 dwarf2_cu *ancestor;
579
580 /* Get the buildsym_compunit for this CU. */
581 buildsym_compunit *get_builder ()
582 {
583 /* If this CU has a builder associated with it, use that. */
584 if (m_builder != nullptr)
585 return m_builder.get ();
586
587 /* Otherwise, search ancestors for a valid builder. */
588 if (ancestor != nullptr)
589 return ancestor->get_builder ();
590
591 return nullptr;
592 }
e7c27a73
DJ
593};
594
094b34ac
DE
595/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
596 This includes type_unit_group and quick_file_names. */
597
598struct stmt_list_hash
599{
600 /* The DWO unit this table is from or NULL if there is none. */
601 struct dwo_unit *dwo_unit;
602
603 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 604 sect_offset line_sect_off;
094b34ac
DE
605};
606
f4dc4d17
DE
607/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
608 an object of this type. */
609
610struct type_unit_group
611{
0186c6a7 612 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
613 To simplify things we create an artificial CU that "includes" all the
614 type units using this stmt_list so that the rest of the code still has
615 a "per_cu" handle on the symtab.
616 This PER_CU is recognized by having no section. */
8a0459fd 617#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
618 struct dwarf2_per_cu_data per_cu;
619
0186c6a7
DE
620 /* The TUs that share this DW_AT_stmt_list entry.
621 This is added to while parsing type units to build partial symtabs,
622 and is deleted afterwards and not used again. */
a8b3b8e9 623 std::vector<signatured_type *> *tus;
f4dc4d17 624
43f3e411 625 /* The compunit symtab.
094b34ac 626 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
627 so we create an essentially anonymous symtab as the compunit symtab. */
628 struct compunit_symtab *compunit_symtab;
f4dc4d17 629
094b34ac
DE
630 /* The data used to construct the hash key. */
631 struct stmt_list_hash hash;
f4dc4d17
DE
632
633 /* The number of symtabs from the line header.
634 The value here must match line_header.num_file_names. */
635 unsigned int num_symtabs;
636
637 /* The symbol tables for this TU (obtained from the files listed in
638 DW_AT_stmt_list).
639 WARNING: The order of entries here must match the order of entries
640 in the line header. After the first TU using this type_unit_group, the
641 line header for the subsequent TUs is recreated from this. This is done
642 because we need to use the same symtabs for each TU using the same
643 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
644 there's no guarantee the line header doesn't have duplicate entries. */
645 struct symtab **symtabs;
646};
647
73869dc2 648/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
649
650struct dwo_sections
651{
652 struct dwarf2_section_info abbrev;
3019eac3
DE
653 struct dwarf2_section_info line;
654 struct dwarf2_section_info loc;
43988095 655 struct dwarf2_section_info loclists;
09262596
DE
656 struct dwarf2_section_info macinfo;
657 struct dwarf2_section_info macro;
3019eac3
DE
658 struct dwarf2_section_info str;
659 struct dwarf2_section_info str_offsets;
80626a55
DE
660 /* In the case of a virtual DWO file, these two are unused. */
661 struct dwarf2_section_info info;
fd5866f6 662 std::vector<dwarf2_section_info> types;
3019eac3
DE
663};
664
c88ee1f0 665/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
666
667struct dwo_unit
668{
669 /* Backlink to the containing struct dwo_file. */
670 struct dwo_file *dwo_file;
671
672 /* The "id" that distinguishes this CU/TU.
673 .debug_info calls this "dwo_id", .debug_types calls this "signature".
674 Since signatures came first, we stick with it for consistency. */
675 ULONGEST signature;
676
677 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 678 struct dwarf2_section_info *section;
3019eac3 679
9c541725
PA
680 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
681 sect_offset sect_off;
3019eac3
DE
682 unsigned int length;
683
684 /* For types, offset in the type's DIE of the type defined by this TU. */
685 cu_offset type_offset_in_tu;
686};
687
73869dc2
DE
688/* include/dwarf2.h defines the DWP section codes.
689 It defines a max value but it doesn't define a min value, which we
690 use for error checking, so provide one. */
691
692enum dwp_v2_section_ids
693{
694 DW_SECT_MIN = 1
695};
696
80626a55 697/* Data for one DWO file.
57d63ce2
DE
698
699 This includes virtual DWO files (a virtual DWO file is a DWO file as it
700 appears in a DWP file). DWP files don't really have DWO files per se -
701 comdat folding of types "loses" the DWO file they came from, and from
702 a high level view DWP files appear to contain a mass of random types.
703 However, to maintain consistency with the non-DWP case we pretend DWP
704 files contain virtual DWO files, and we assign each TU with one virtual
705 DWO file (generally based on the line and abbrev section offsets -
706 a heuristic that seems to work in practice). */
3019eac3
DE
707
708struct dwo_file
709{
51ac9db5
SM
710 dwo_file () = default;
711 DISABLE_COPY_AND_ASSIGN (dwo_file);
712
0ac5b59e 713 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
714 For virtual DWO files the name is constructed from the section offsets
715 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
716 from related CU+TUs. */
51ac9db5 717 const char *dwo_name = nullptr;
0ac5b59e
DE
718
719 /* The DW_AT_comp_dir attribute. */
51ac9db5 720 const char *comp_dir = nullptr;
3019eac3 721
80626a55
DE
722 /* The bfd, when the file is open. Otherwise this is NULL.
723 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 724 gdb_bfd_ref_ptr dbfd;
3019eac3 725
73869dc2
DE
726 /* The sections that make up this DWO file.
727 Remember that for virtual DWO files in DWP V2, these are virtual
728 sections (for lack of a better name). */
51ac9db5 729 struct dwo_sections sections {};
3019eac3 730
33c5cd75
DB
731 /* The CUs in the file.
732 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
733 an extension to handle LLVM's Link Time Optimization output (where
734 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 735 htab_t cus {};
3019eac3
DE
736
737 /* Table of TUs in the file.
738 Each element is a struct dwo_unit. */
51ac9db5 739 htab_t tus {};
3019eac3
DE
740};
741
80626a55
DE
742/* These sections are what may appear in a DWP file. */
743
744struct dwp_sections
745{
73869dc2 746 /* These are used by both DWP version 1 and 2. */
80626a55
DE
747 struct dwarf2_section_info str;
748 struct dwarf2_section_info cu_index;
749 struct dwarf2_section_info tu_index;
73869dc2
DE
750
751 /* These are only used by DWP version 2 files.
752 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
753 sections are referenced by section number, and are not recorded here.
754 In DWP version 2 there is at most one copy of all these sections, each
755 section being (effectively) comprised of the concatenation of all of the
756 individual sections that exist in the version 1 format.
757 To keep the code simple we treat each of these concatenated pieces as a
758 section itself (a virtual section?). */
759 struct dwarf2_section_info abbrev;
760 struct dwarf2_section_info info;
761 struct dwarf2_section_info line;
762 struct dwarf2_section_info loc;
763 struct dwarf2_section_info macinfo;
764 struct dwarf2_section_info macro;
765 struct dwarf2_section_info str_offsets;
766 struct dwarf2_section_info types;
80626a55
DE
767};
768
73869dc2
DE
769/* These sections are what may appear in a virtual DWO file in DWP version 1.
770 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 771
73869dc2 772struct virtual_v1_dwo_sections
80626a55
DE
773{
774 struct dwarf2_section_info abbrev;
775 struct dwarf2_section_info line;
776 struct dwarf2_section_info loc;
777 struct dwarf2_section_info macinfo;
778 struct dwarf2_section_info macro;
779 struct dwarf2_section_info str_offsets;
780 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 781 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
782 struct dwarf2_section_info info_or_types;
783};
784
73869dc2
DE
785/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
786 In version 2, the sections of the DWO files are concatenated together
787 and stored in one section of that name. Thus each ELF section contains
788 several "virtual" sections. */
789
790struct virtual_v2_dwo_sections
791{
792 bfd_size_type abbrev_offset;
793 bfd_size_type abbrev_size;
794
795 bfd_size_type line_offset;
796 bfd_size_type line_size;
797
798 bfd_size_type loc_offset;
799 bfd_size_type loc_size;
800
801 bfd_size_type macinfo_offset;
802 bfd_size_type macinfo_size;
803
804 bfd_size_type macro_offset;
805 bfd_size_type macro_size;
806
807 bfd_size_type str_offsets_offset;
808 bfd_size_type str_offsets_size;
809
810 /* Each DWP hash table entry records one CU or one TU.
811 That is recorded here, and copied to dwo_unit.section. */
812 bfd_size_type info_or_types_offset;
813 bfd_size_type info_or_types_size;
814};
815
80626a55
DE
816/* Contents of DWP hash tables. */
817
818struct dwp_hash_table
819{
73869dc2 820 uint32_t version, nr_columns;
80626a55 821 uint32_t nr_units, nr_slots;
73869dc2
DE
822 const gdb_byte *hash_table, *unit_table;
823 union
824 {
825 struct
826 {
827 const gdb_byte *indices;
828 } v1;
829 struct
830 {
831 /* This is indexed by column number and gives the id of the section
832 in that column. */
833#define MAX_NR_V2_DWO_SECTIONS \
834 (1 /* .debug_info or .debug_types */ \
835 + 1 /* .debug_abbrev */ \
836 + 1 /* .debug_line */ \
837 + 1 /* .debug_loc */ \
838 + 1 /* .debug_str_offsets */ \
839 + 1 /* .debug_macro or .debug_macinfo */)
840 int section_ids[MAX_NR_V2_DWO_SECTIONS];
841 const gdb_byte *offsets;
842 const gdb_byte *sizes;
843 } v2;
844 } section_pool;
80626a55
DE
845};
846
847/* Data for one DWP file. */
848
849struct dwp_file
850{
400174b1
TT
851 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
852 : name (name_),
853 dbfd (std::move (abfd))
854 {
855 }
856
80626a55
DE
857 /* Name of the file. */
858 const char *name;
859
73869dc2 860 /* File format version. */
400174b1 861 int version = 0;
73869dc2 862
93417882 863 /* The bfd. */
400174b1 864 gdb_bfd_ref_ptr dbfd;
80626a55
DE
865
866 /* Section info for this file. */
400174b1 867 struct dwp_sections sections {};
80626a55 868
57d63ce2 869 /* Table of CUs in the file. */
400174b1 870 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
871
872 /* Table of TUs in the file. */
400174b1 873 const struct dwp_hash_table *tus = nullptr;
80626a55 874
19ac8c2e 875 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
876 htab_t loaded_cus {};
877 htab_t loaded_tus {};
80626a55 878
73869dc2
DE
879 /* Table to map ELF section numbers to their sections.
880 This is only needed for the DWP V1 file format. */
400174b1
TT
881 unsigned int num_sections = 0;
882 asection **elf_sections = nullptr;
80626a55
DE
883};
884
0963b4bd
MS
885/* Struct used to pass misc. parameters to read_die_and_children, et
886 al. which are used for both .debug_info and .debug_types dies.
887 All parameters here are unchanging for the life of the call. This
dee91e82 888 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
889
890struct die_reader_specs
891{
a32a8923 892 /* The bfd of die_section. */
93311388
DE
893 bfd* abfd;
894
895 /* The CU of the DIE we are parsing. */
896 struct dwarf2_cu *cu;
897
80626a55 898 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
899 struct dwo_file *dwo_file;
900
dee91e82 901 /* The section the die comes from.
3019eac3 902 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
903 struct dwarf2_section_info *die_section;
904
905 /* die_section->buffer. */
d521ce57 906 const gdb_byte *buffer;
f664829e
DE
907
908 /* The end of the buffer. */
909 const gdb_byte *buffer_end;
a2ce51a0
DE
910
911 /* The value of the DW_AT_comp_dir attribute. */
912 const char *comp_dir;
685af9cd
TT
913
914 /* The abbreviation table to use when reading the DIEs. */
915 struct abbrev_table *abbrev_table;
93311388
DE
916};
917
fd820528 918/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 919typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 920 const gdb_byte *info_ptr,
dee91e82
DE
921 struct die_info *comp_unit_die,
922 int has_children,
923 void *data);
924
ecfb656c
PA
925/* A 1-based directory index. This is a strong typedef to prevent
926 accidentally using a directory index as a 0-based index into an
927 array/vector. */
928enum class dir_index : unsigned int {};
929
930/* Likewise, a 1-based file name index. */
931enum class file_name_index : unsigned int {};
932
52059ffd
TT
933struct file_entry
934{
fff8551c
PA
935 file_entry () = default;
936
ecfb656c 937 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
938 unsigned int mod_time_, unsigned int length_)
939 : name (name_),
ecfb656c 940 d_index (d_index_),
fff8551c
PA
941 mod_time (mod_time_),
942 length (length_)
943 {}
944
ecfb656c
PA
945 /* Return the include directory at D_INDEX stored in LH. Returns
946 NULL if D_INDEX is out of bounds. */
8c43009f
PA
947 const char *include_dir (const line_header *lh) const;
948
fff8551c
PA
949 /* The file name. Note this is an observing pointer. The memory is
950 owned by debug_line_buffer. */
951 const char *name {};
952
8c43009f 953 /* The directory index (1-based). */
ecfb656c 954 dir_index d_index {};
fff8551c
PA
955
956 unsigned int mod_time {};
957
958 unsigned int length {};
959
960 /* True if referenced by the Line Number Program. */
961 bool included_p {};
962
83769d0b 963 /* The associated symbol table, if any. */
fff8551c 964 struct symtab *symtab {};
52059ffd
TT
965};
966
debd256d
JB
967/* The line number information for a compilation unit (found in the
968 .debug_line section) begins with a "statement program header",
969 which contains the following information. */
970struct line_header
971{
fff8551c
PA
972 line_header ()
973 : offset_in_dwz {}
974 {}
975
976 /* Add an entry to the include directory table. */
977 void add_include_dir (const char *include_dir);
978
979 /* Add an entry to the file name table. */
ecfb656c 980 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
981 unsigned int mod_time, unsigned int length);
982
ecfb656c 983 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 984 is out of bounds. */
ecfb656c 985 const char *include_dir_at (dir_index index) const
8c43009f 986 {
ecfb656c
PA
987 /* Convert directory index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= include_dirs.size ())
8c43009f 992 return NULL;
ecfb656c 993 return include_dirs[vec_index];
8c43009f
PA
994 }
995
ecfb656c 996 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 997 is out of bounds. */
ecfb656c 998 file_entry *file_name_at (file_name_index index)
8c43009f 999 {
ecfb656c
PA
1000 /* Convert file name index number (1-based) to vector index
1001 (0-based). */
1002 size_t vec_index = to_underlying (index) - 1;
1003
1004 if (vec_index >= file_names.size ())
fff8551c 1005 return NULL;
ecfb656c 1006 return &file_names[vec_index];
fff8551c
PA
1007 }
1008
527f3840 1009 /* Offset of line number information in .debug_line section. */
9c541725 1010 sect_offset sect_off {};
527f3840
JK
1011
1012 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1013 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1014
1015 unsigned int total_length {};
1016 unsigned short version {};
1017 unsigned int header_length {};
1018 unsigned char minimum_instruction_length {};
1019 unsigned char maximum_ops_per_instruction {};
1020 unsigned char default_is_stmt {};
1021 int line_base {};
1022 unsigned char line_range {};
1023 unsigned char opcode_base {};
debd256d
JB
1024
1025 /* standard_opcode_lengths[i] is the number of operands for the
1026 standard opcode whose value is i. This means that
1027 standard_opcode_lengths[0] is unused, and the last meaningful
1028 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1029 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1030
fff8551c
PA
1031 /* The include_directories table. Note these are observing
1032 pointers. The memory is owned by debug_line_buffer. */
1033 std::vector<const char *> include_dirs;
debd256d 1034
fff8551c
PA
1035 /* The file_names table. */
1036 std::vector<file_entry> file_names;
debd256d
JB
1037
1038 /* The start and end of the statement program following this
6502dd73 1039 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1040 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1041};
c906108c 1042
fff8551c
PA
1043typedef std::unique_ptr<line_header> line_header_up;
1044
8c43009f
PA
1045const char *
1046file_entry::include_dir (const line_header *lh) const
1047{
ecfb656c 1048 return lh->include_dir_at (d_index);
8c43009f
PA
1049}
1050
c906108c 1051/* When we construct a partial symbol table entry we only
0963b4bd 1052 need this much information. */
6f06d47b 1053struct partial_die_info : public allocate_on_obstack
c906108c 1054 {
6f06d47b
YQ
1055 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1056
1057 /* Disable assign but still keep copy ctor, which is needed
1058 load_partial_dies. */
1059 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1060
52356b79
YQ
1061 /* Adjust the partial die before generating a symbol for it. This
1062 function may set the is_external flag or change the DIE's
1063 name. */
1064 void fixup (struct dwarf2_cu *cu);
1065
48fbe735
YQ
1066 /* Read a minimal amount of information into the minimal die
1067 structure. */
1068 const gdb_byte *read (const struct die_reader_specs *reader,
1069 const struct abbrev_info &abbrev,
1070 const gdb_byte *info_ptr);
1071
72bf9492 1072 /* Offset of this DIE. */
6f06d47b 1073 const sect_offset sect_off;
72bf9492
DJ
1074
1075 /* DWARF-2 tag for this DIE. */
6f06d47b 1076 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1077
72bf9492 1078 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1079 const unsigned int has_children : 1;
1080
72bf9492
DJ
1081 unsigned int is_external : 1;
1082 unsigned int is_declaration : 1;
1083 unsigned int has_type : 1;
1084 unsigned int has_specification : 1;
1085 unsigned int has_pc_info : 1;
481860b3 1086 unsigned int may_be_inlined : 1;
72bf9492 1087
0c1b455e
TT
1088 /* This DIE has been marked DW_AT_main_subprogram. */
1089 unsigned int main_subprogram : 1;
1090
72bf9492
DJ
1091 /* Flag set if the SCOPE field of this structure has been
1092 computed. */
1093 unsigned int scope_set : 1;
1094
fa4028e9
JB
1095 /* Flag set if the DIE has a byte_size attribute. */
1096 unsigned int has_byte_size : 1;
1097
ff908ebf
AW
1098 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1099 unsigned int has_const_value : 1;
1100
98bfdba5
PA
1101 /* Flag set if any of the DIE's children are template arguments. */
1102 unsigned int has_template_arguments : 1;
1103
52356b79 1104 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1105 unsigned int fixup_called : 1;
1106
36586728
TT
1107 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1108 unsigned int is_dwz : 1;
1109
1110 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1111 unsigned int spec_is_dwz : 1;
1112
72bf9492 1113 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1114 sometimes a default name for unnamed DIEs. */
6f06d47b 1115 const char *name = nullptr;
72bf9492 1116
abc72ce4 1117 /* The linkage name, if present. */
6f06d47b 1118 const char *linkage_name = nullptr;
abc72ce4 1119
72bf9492
DJ
1120 /* The scope to prepend to our children. This is generally
1121 allocated on the comp_unit_obstack, so will disappear
1122 when this compilation unit leaves the cache. */
6f06d47b 1123 const char *scope = nullptr;
72bf9492 1124
95554aad
TT
1125 /* Some data associated with the partial DIE. The tag determines
1126 which field is live. */
1127 union
1128 {
1129 /* The location description associated with this DIE, if any. */
1130 struct dwarf_block *locdesc;
1131 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1132 sect_offset sect_off;
6f06d47b 1133 } d {};
72bf9492
DJ
1134
1135 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1136 CORE_ADDR lowpc = 0;
1137 CORE_ADDR highpc = 0;
72bf9492 1138
93311388 1139 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1140 DW_AT_sibling, if any. */
48fbe735
YQ
1141 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1142 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1143 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1144
1145 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1146 DW_AT_specification (or DW_AT_abstract_origin or
1147 DW_AT_extension). */
6f06d47b 1148 sect_offset spec_offset {};
72bf9492
DJ
1149
1150 /* Pointers to this DIE's parent, first child, and next sibling,
1151 if any. */
6f06d47b
YQ
1152 struct partial_die_info *die_parent = nullptr;
1153 struct partial_die_info *die_child = nullptr;
1154 struct partial_die_info *die_sibling = nullptr;
1155
1156 friend struct partial_die_info *
1157 dwarf2_cu::find_partial_die (sect_offset sect_off);
1158
1159 private:
1160 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1161 partial_die_info (sect_offset sect_off)
1162 : partial_die_info (sect_off, DW_TAG_padding, 0)
1163 {
1164 }
1165
1166 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1167 int has_children_)
1168 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1169 {
1170 is_external = 0;
1171 is_declaration = 0;
1172 has_type = 0;
1173 has_specification = 0;
1174 has_pc_info = 0;
1175 may_be_inlined = 0;
1176 main_subprogram = 0;
1177 scope_set = 0;
1178 has_byte_size = 0;
1179 has_const_value = 0;
1180 has_template_arguments = 0;
1181 fixup_called = 0;
1182 is_dwz = 0;
1183 spec_is_dwz = 0;
1184 }
c906108c
SS
1185 };
1186
0963b4bd 1187/* This data structure holds the information of an abbrev. */
c906108c
SS
1188struct abbrev_info
1189 {
1190 unsigned int number; /* number identifying abbrev */
1191 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1192 unsigned short has_children; /* boolean */
1193 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1194 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1195 struct abbrev_info *next; /* next in chain */
1196 };
1197
1198struct attr_abbrev
1199 {
9d25dd43
DE
1200 ENUM_BITFIELD(dwarf_attribute) name : 16;
1201 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1202
1203 /* It is valid only if FORM is DW_FORM_implicit_const. */
1204 LONGEST implicit_const;
c906108c
SS
1205 };
1206
433df2d4
DE
1207/* Size of abbrev_table.abbrev_hash_table. */
1208#define ABBREV_HASH_SIZE 121
1209
1210/* Top level data structure to contain an abbreviation table. */
1211
1212struct abbrev_table
1213{
685af9cd
TT
1214 explicit abbrev_table (sect_offset off)
1215 : sect_off (off)
1216 {
4a17f768 1217 m_abbrevs =
685af9cd 1218 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1219 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1220 }
1221
1222 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1223
1224 /* Allocate space for a struct abbrev_info object in
1225 ABBREV_TABLE. */
1226 struct abbrev_info *alloc_abbrev ();
1227
1228 /* Add an abbreviation to the table. */
1229 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1230
1231 /* Look up an abbrev in the table.
1232 Returns NULL if the abbrev is not found. */
1233
1234 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1235
1236
f4dc4d17
DE
1237 /* Where the abbrev table came from.
1238 This is used as a sanity check when the table is used. */
685af9cd 1239 const sect_offset sect_off;
433df2d4
DE
1240
1241 /* Storage for the abbrev table. */
685af9cd 1242 auto_obstack abbrev_obstack;
433df2d4 1243
4a17f768
YQ
1244private:
1245
433df2d4
DE
1246 /* Hash table of abbrevs.
1247 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1248 It could be statically allocated, but the previous code didn't so we
1249 don't either. */
4a17f768 1250 struct abbrev_info **m_abbrevs;
433df2d4
DE
1251};
1252
685af9cd
TT
1253typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1254
0963b4bd 1255/* Attributes have a name and a value. */
b60c80d6
DJ
1256struct attribute
1257 {
9d25dd43 1258 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1259 ENUM_BITFIELD(dwarf_form) form : 15;
1260
1261 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1262 field should be in u.str (existing only for DW_STRING) but it is kept
1263 here for better struct attribute alignment. */
1264 unsigned int string_is_canonical : 1;
1265
b60c80d6
DJ
1266 union
1267 {
15d034d0 1268 const char *str;
b60c80d6 1269 struct dwarf_block *blk;
43bbcdc2
PH
1270 ULONGEST unsnd;
1271 LONGEST snd;
b60c80d6 1272 CORE_ADDR addr;
ac9ec31b 1273 ULONGEST signature;
b60c80d6
DJ
1274 }
1275 u;
1276 };
1277
0963b4bd 1278/* This data structure holds a complete die structure. */
c906108c
SS
1279struct die_info
1280 {
76815b17
DE
1281 /* DWARF-2 tag for this DIE. */
1282 ENUM_BITFIELD(dwarf_tag) tag : 16;
1283
1284 /* Number of attributes */
98bfdba5
PA
1285 unsigned char num_attrs;
1286
1287 /* True if we're presently building the full type name for the
1288 type derived from this DIE. */
1289 unsigned char building_fullname : 1;
76815b17 1290
adde2bff
DE
1291 /* True if this die is in process. PR 16581. */
1292 unsigned char in_process : 1;
1293
76815b17
DE
1294 /* Abbrev number */
1295 unsigned int abbrev;
1296
93311388 1297 /* Offset in .debug_info or .debug_types section. */
9c541725 1298 sect_offset sect_off;
78ba4af6
JB
1299
1300 /* The dies in a compilation unit form an n-ary tree. PARENT
1301 points to this die's parent; CHILD points to the first child of
1302 this node; and all the children of a given node are chained
4950bc1c 1303 together via their SIBLING fields. */
639d11d3
DC
1304 struct die_info *child; /* Its first child, if any. */
1305 struct die_info *sibling; /* Its next sibling, if any. */
1306 struct die_info *parent; /* Its parent, if any. */
c906108c 1307
b60c80d6
DJ
1308 /* An array of attributes, with NUM_ATTRS elements. There may be
1309 zero, but it's not common and zero-sized arrays are not
1310 sufficiently portable C. */
1311 struct attribute attrs[1];
c906108c
SS
1312 };
1313
0963b4bd 1314/* Get at parts of an attribute structure. */
c906108c
SS
1315
1316#define DW_STRING(attr) ((attr)->u.str)
8285870a 1317#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1318#define DW_UNSND(attr) ((attr)->u.unsnd)
1319#define DW_BLOCK(attr) ((attr)->u.blk)
1320#define DW_SND(attr) ((attr)->u.snd)
1321#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1322#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1323
0963b4bd 1324/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1325struct dwarf_block
1326 {
56eb65bd 1327 size_t size;
1d6edc3c
JK
1328
1329 /* Valid only if SIZE is not zero. */
d521ce57 1330 const gdb_byte *data;
c906108c
SS
1331 };
1332
c906108c
SS
1333#ifndef ATTR_ALLOC_CHUNK
1334#define ATTR_ALLOC_CHUNK 4
1335#endif
1336
c906108c
SS
1337/* Allocate fields for structs, unions and enums in this size. */
1338#ifndef DW_FIELD_ALLOC_CHUNK
1339#define DW_FIELD_ALLOC_CHUNK 4
1340#endif
1341
c906108c
SS
1342/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1343 but this would require a corresponding change in unpack_field_as_long
1344 and friends. */
1345static int bits_per_byte = 8;
1346
2ddeaf8a
TT
1347/* When reading a variant or variant part, we track a bit more
1348 information about the field, and store it in an object of this
1349 type. */
1350
1351struct variant_field
1352{
1353 /* If we see a DW_TAG_variant, then this will be the discriminant
1354 value. */
1355 ULONGEST discriminant_value;
1356 /* If we see a DW_TAG_variant, then this will be set if this is the
1357 default branch. */
1358 bool default_branch;
1359 /* While reading a DW_TAG_variant_part, this will be set if this
1360 field is the discriminant. */
1361 bool is_discriminant;
1362};
1363
52059ffd
TT
1364struct nextfield
1365{
be2daae6
TT
1366 int accessibility = 0;
1367 int virtuality = 0;
2ddeaf8a 1368 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1369 struct variant_field variant {};
1370 struct field field {};
52059ffd
TT
1371};
1372
1373struct fnfieldlist
1374{
be2daae6
TT
1375 const char *name = nullptr;
1376 std::vector<struct fn_field> fnfields;
52059ffd
TT
1377};
1378
c906108c
SS
1379/* The routines that read and process dies for a C struct or C++ class
1380 pass lists of data member fields and lists of member function fields
1381 in an instance of a field_info structure, as defined below. */
1382struct field_info
c5aa993b 1383 {
0963b4bd 1384 /* List of data member and baseclasses fields. */
be2daae6
TT
1385 std::vector<struct nextfield> fields;
1386 std::vector<struct nextfield> baseclasses;
c906108c 1387
7d0ccb61 1388 /* Number of fields (including baseclasses). */
be2daae6 1389 int nfields = 0;
c906108c 1390
c5aa993b 1391 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1392 int non_public_fields = 0;
c906108c 1393
c5aa993b
JM
1394 /* Member function fieldlist array, contains name of possibly overloaded
1395 member function, number of overloaded member functions and a pointer
1396 to the head of the member function field chain. */
be2daae6 1397 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1398
1399 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1400 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1401 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1402
1403 /* Nested types defined by this class and the number of elements in this
1404 list. */
be2daae6 1405 std::vector<struct decl_field> nested_types_list;
c5aa993b 1406 };
c906108c 1407
10b3939b
DJ
1408/* One item on the queue of compilation units to read in full symbols
1409 for. */
1410struct dwarf2_queue_item
1411{
1412 struct dwarf2_per_cu_data *per_cu;
95554aad 1413 enum language pretend_language;
10b3939b
DJ
1414 struct dwarf2_queue_item *next;
1415};
1416
1417/* The current queue. */
1418static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1419
ae038cb0
DJ
1420/* Loaded secondary compilation units are kept in memory until they
1421 have not been referenced for the processing of this many
1422 compilation units. Set this to zero to disable caching. Cache
1423 sizes of up to at least twenty will improve startup time for
1424 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1425static int dwarf_max_cache_age = 5;
920d2a44 1426static void
b4f54984
DE
1427show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1428 struct cmd_list_element *c, const char *value)
920d2a44 1429{
3e43a32a 1430 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1431 "DWARF compilation units is %s.\n"),
920d2a44
AC
1432 value);
1433}
4390d890 1434\f
c906108c
SS
1435/* local function prototypes */
1436
a32a8923
DE
1437static const char *get_section_name (const struct dwarf2_section_info *);
1438
1439static const char *get_section_file_name (const struct dwarf2_section_info *);
1440
918dd910
JK
1441static void dwarf2_find_base_address (struct die_info *die,
1442 struct dwarf2_cu *cu);
1443
0018ea6f
DE
1444static struct partial_symtab *create_partial_symtab
1445 (struct dwarf2_per_cu_data *per_cu, const char *name);
1446
f1902523
JK
1447static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1448 const gdb_byte *info_ptr,
1449 struct die_info *type_unit_die,
1450 int has_children, void *data);
1451
ed2dc618
SM
1452static void dwarf2_build_psymtabs_hard
1453 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1454
72bf9492
DJ
1455static void scan_partial_symbols (struct partial_die_info *,
1456 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1457 int, struct dwarf2_cu *);
c906108c 1458
72bf9492
DJ
1459static void add_partial_symbol (struct partial_die_info *,
1460 struct dwarf2_cu *);
63d06c5c 1461
72bf9492
DJ
1462static void add_partial_namespace (struct partial_die_info *pdi,
1463 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1464 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1465
5d7cb8df 1466static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1467 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1468 struct dwarf2_cu *cu);
1469
72bf9492
DJ
1470static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1471 struct dwarf2_cu *cu);
91c24f0a 1472
bc30ff58
JB
1473static void add_partial_subprogram (struct partial_die_info *pdi,
1474 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1475 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1476
257e7a09
YQ
1477static void dwarf2_read_symtab (struct partial_symtab *,
1478 struct objfile *);
c906108c 1479
a14ed312 1480static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1481
685af9cd 1482static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1483 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1484 sect_offset);
433df2d4 1485
d521ce57 1486static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1487
dee91e82 1488static struct partial_die_info *load_partial_dies
d521ce57 1489 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1490
fb816e8b
TV
1491/* A pair of partial_die_info and compilation unit. */
1492struct cu_partial_die_info
1493{
1494 /* The compilation unit of the partial_die_info. */
1495 struct dwarf2_cu *cu;
1496 /* A partial_die_info. */
1497 struct partial_die_info *pdi;
122cf0f2
AB
1498
1499 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1500 : cu (cu),
1501 pdi (pdi)
1502 { /* Nothhing. */ }
1503
1504private:
1505 cu_partial_die_info () = delete;
fb816e8b
TV
1506};
1507
122cf0f2
AB
1508static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1509 struct dwarf2_cu *);
72bf9492 1510
d521ce57
TT
1511static const gdb_byte *read_attribute (const struct die_reader_specs *,
1512 struct attribute *, struct attr_abbrev *,
1513 const gdb_byte *);
a8329558 1514
a1855c1d 1515static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1516
a1855c1d 1517static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1518
a1855c1d 1519static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1520
15f18d14
AT
1521/* Read the next three bytes (little-endian order) as an unsigned integer. */
1522static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1523
a1855c1d 1524static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1525
a1855c1d 1526static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1527
d521ce57 1528static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1529 unsigned int *);
c906108c 1530
d521ce57 1531static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1532
1533static LONGEST read_checked_initial_length_and_offset
d521ce57 1534 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1535 unsigned int *, unsigned int *);
613e1657 1536
d521ce57
TT
1537static LONGEST read_offset (bfd *, const gdb_byte *,
1538 const struct comp_unit_head *,
c764a876
DE
1539 unsigned int *);
1540
d521ce57 1541static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1542
ed2dc618
SM
1543static sect_offset read_abbrev_offset
1544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1545 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1546
d521ce57 1547static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1548
d521ce57 1549static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1550
ed2dc618
SM
1551static const char *read_indirect_string
1552 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1553 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1554
ed2dc618
SM
1555static const char *read_indirect_line_string
1556 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1557 const struct comp_unit_head *, unsigned int *);
36586728 1558
ed2dc618
SM
1559static const char *read_indirect_string_at_offset
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1561 LONGEST str_offset);
927aa2e7 1562
ed2dc618
SM
1563static const char *read_indirect_string_from_dwz
1564 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1565
d521ce57 1566static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1567
d521ce57
TT
1568static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1569 const gdb_byte *,
3019eac3
DE
1570 unsigned int *);
1571
d521ce57 1572static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1573 ULONGEST str_index);
3019eac3 1574
e142c38c 1575static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1576
e142c38c
DJ
1577static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1578 struct dwarf2_cu *);
c906108c 1579
348e048f 1580static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1581 unsigned int);
348e048f 1582
7d45c7c3
KB
1583static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1584 struct dwarf2_cu *cu);
1585
a084a2a6
AT
1586static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1587
05cf31d1
JB
1588static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1589 struct dwarf2_cu *cu);
1590
e142c38c 1591static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1592
e142c38c 1593static struct die_info *die_specification (struct die_info *die,
f2f0e013 1594 struct dwarf2_cu **);
63d06c5c 1595
9c541725 1596static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1597 struct dwarf2_cu *cu);
debd256d 1598
f3f5162e 1599static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1600 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1601 CORE_ADDR, int decode_mapping);
c906108c 1602
804d2729
TT
1603static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1604 const char *);
c906108c 1605
a14ed312 1606static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1607 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1608
ff39bb5e 1609static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1610 struct dwarf2_cu *);
c906108c 1611
ff39bb5e 1612static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1613 struct type *type,
1614 const char *name,
1615 struct obstack *obstack,
12df843f 1616 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1617 const gdb_byte **bytes,
98bfdba5 1618 struct dwarf2_locexpr_baton **baton);
2df3850c 1619
e7c27a73 1620static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1621
b4ba55a1
JB
1622static int need_gnat_info (struct dwarf2_cu *);
1623
3e43a32a
MS
1624static struct type *die_descriptive_type (struct die_info *,
1625 struct dwarf2_cu *);
b4ba55a1
JB
1626
1627static void set_descriptive_type (struct type *, struct die_info *,
1628 struct dwarf2_cu *);
1629
e7c27a73
DJ
1630static struct type *die_containing_type (struct die_info *,
1631 struct dwarf2_cu *);
c906108c 1632
ff39bb5e 1633static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1634 struct dwarf2_cu *);
c906108c 1635
f792889a 1636static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1637
673bfd45
DE
1638static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1639
0d5cff50 1640static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1641
6e70227d 1642static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1643 const char *suffix, int physname,
1644 struct dwarf2_cu *cu);
63d06c5c 1645
e7c27a73 1646static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1647
348e048f
DE
1648static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1649
e7c27a73 1650static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1651
e7c27a73 1652static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1653
96408a79
SA
1654static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1655
71a3c369
TT
1656static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1657
ff013f42
JK
1658static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *, struct partial_symtab *);
1660
3a2b436a 1661/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1662 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1663enum pc_bounds_kind
1664{
e385593e 1665 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1666 PC_BOUNDS_NOT_PRESENT,
1667
e385593e
JK
1668 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1669 were present but they do not form a valid range of PC addresses. */
1670 PC_BOUNDS_INVALID,
1671
3a2b436a
JK
1672 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1673 PC_BOUNDS_RANGES,
1674
1675 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1676 PC_BOUNDS_HIGH_LOW,
1677};
1678
1679static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1680 CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *,
1682 struct partial_symtab *);
c906108c 1683
fae299cd
DC
1684static void get_scope_pc_bounds (struct die_info *,
1685 CORE_ADDR *, CORE_ADDR *,
1686 struct dwarf2_cu *);
1687
801e3a5b
JB
1688static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1689 CORE_ADDR, struct dwarf2_cu *);
1690
a14ed312 1691static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1692 struct dwarf2_cu *);
c906108c 1693
a14ed312 1694static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1695 struct type *, struct dwarf2_cu *);
c906108c 1696
a14ed312 1697static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1698 struct die_info *, struct type *,
e7c27a73 1699 struct dwarf2_cu *);
c906108c 1700
a14ed312 1701static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1702 struct type *,
1703 struct dwarf2_cu *);
c906108c 1704
134d01f1 1705static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1706
e7c27a73 1707static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1708
e7c27a73 1709static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1710
5d7cb8df
JK
1711static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1712
804d2729 1713static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1714
27aa8d6a
SW
1715static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1716
74921315
KS
1717static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1718
f55ee35c
JK
1719static struct type *read_module_type (struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
38d518c9 1722static const char *namespace_name (struct die_info *die,
e142c38c 1723 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1724
134d01f1 1725static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1726
e7c27a73 1727static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1728
6e70227d 1729static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1730 struct dwarf2_cu *);
1731
bf6af496 1732static struct die_info *read_die_and_siblings_1
d521ce57 1733 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1734 struct die_info *);
639d11d3 1735
dee91e82 1736static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1737 const gdb_byte *info_ptr,
1738 const gdb_byte **new_info_ptr,
639d11d3
DC
1739 struct die_info *parent);
1740
d521ce57
TT
1741static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1742 struct die_info **, const gdb_byte *,
1743 int *, int);
3019eac3 1744
d521ce57
TT
1745static const gdb_byte *read_full_die (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *);
93311388 1748
e7c27a73 1749static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1750
15d034d0
TT
1751static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1752 struct obstack *);
71c25dea 1753
15d034d0 1754static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1755
15d034d0 1756static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1757 struct die_info *die,
1758 struct dwarf2_cu *cu);
1759
ca69b9e6
DE
1760static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 struct dwarf2_cu *cu);
1762
e142c38c 1763static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1764 struct dwarf2_cu **);
9219021c 1765
f39c6ffd 1766static const char *dwarf_tag_name (unsigned int);
c906108c 1767
f39c6ffd 1768static const char *dwarf_attr_name (unsigned int);
c906108c 1769
a084a2a6
AT
1770static const char *dwarf_unit_type_name (int unit_type);
1771
f39c6ffd 1772static const char *dwarf_form_name (unsigned int);
c906108c 1773
a121b7c1 1774static const char *dwarf_bool_name (unsigned int);
c906108c 1775
f39c6ffd 1776static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1777
f9aca02d 1778static struct die_info *sibling_die (struct die_info *);
c906108c 1779
d97bc12b
DE
1780static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1781
1782static void dump_die_for_error (struct die_info *);
1783
1784static void dump_die_1 (struct ui_file *, int level, int max_level,
1785 struct die_info *);
c906108c 1786
d97bc12b 1787/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1788
51545339 1789static void store_in_ref_table (struct die_info *,
10b3939b 1790 struct dwarf2_cu *);
c906108c 1791
ff39bb5e 1792static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1793
ff39bb5e 1794static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1795
348e048f 1796static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1797 const struct attribute *,
348e048f
DE
1798 struct dwarf2_cu **);
1799
10b3939b 1800static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1801 const struct attribute *,
f2f0e013 1802 struct dwarf2_cu **);
c906108c 1803
348e048f 1804static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1805 const struct attribute *,
348e048f
DE
1806 struct dwarf2_cu **);
1807
ac9ec31b
DE
1808static struct type *get_signatured_type (struct die_info *, ULONGEST,
1809 struct dwarf2_cu *);
1810
1811static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1812 const struct attribute *,
ac9ec31b
DE
1813 struct dwarf2_cu *);
1814
e5fe5e75 1815static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1816
52dc124a 1817static void read_signatured_type (struct signatured_type *);
348e048f 1818
63e43d3a
PMR
1819static int attr_to_dynamic_prop (const struct attribute *attr,
1820 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1821 struct dynamic_prop *prop, struct type *type);
63e43d3a 1822
c906108c
SS
1823/* memory allocation interface */
1824
7b5a2f43 1825static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1826
b60c80d6 1827static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1828
43f3e411 1829static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1830
6e5a29e1 1831static int attr_form_is_block (const struct attribute *);
8e19ed76 1832
6e5a29e1 1833static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1834
6e5a29e1 1835static int attr_form_is_constant (const struct attribute *);
3690dd37 1836
6e5a29e1 1837static int attr_form_is_ref (const struct attribute *);
7771576e 1838
8cf6f0b1
TT
1839static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1840 struct dwarf2_loclist_baton *baton,
ff39bb5e 1841 const struct attribute *attr);
8cf6f0b1 1842
ff39bb5e 1843static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1844 struct symbol *sym,
f1e6e072
TT
1845 struct dwarf2_cu *cu,
1846 int is_block);
4c2df51b 1847
d521ce57
TT
1848static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1849 const gdb_byte *info_ptr,
1850 struct abbrev_info *abbrev);
4bb7a0a7 1851
72bf9492
DJ
1852static hashval_t partial_die_hash (const void *item);
1853
1854static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1855
ae038cb0 1856static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1857 (sect_offset sect_off, unsigned int offset_in_dwz,
1858 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1859
9816fde3 1860static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1861 struct die_info *comp_unit_die,
1862 enum language pretend_language);
93311388 1863
ed2dc618 1864static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1865
dee91e82 1866static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1867
f792889a
DJ
1868static struct type *set_die_type (struct die_info *, struct type *,
1869 struct dwarf2_cu *);
1c379e20 1870
ed2dc618 1871static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1872
ed2dc618 1873static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1874
58f0c718 1875static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1876 enum language);
10b3939b 1877
95554aad
TT
1878static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1879 enum language);
10b3939b 1880
f4dc4d17
DE
1881static void process_full_type_unit (struct dwarf2_per_cu_data *,
1882 enum language);
1883
10b3939b
DJ
1884static void dwarf2_add_dependence (struct dwarf2_cu *,
1885 struct dwarf2_per_cu_data *);
1886
ae038cb0
DJ
1887static void dwarf2_mark (struct dwarf2_cu *);
1888
1889static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1890
b64f50a1 1891static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1892 struct dwarf2_per_cu_data *);
673bfd45 1893
f792889a 1894static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1895
95554aad
TT
1896static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1897 enum language pretend_language);
1898
ed2dc618 1899static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1900
9a49df9d
AB
1901static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1902static struct type *dwarf2_per_cu_addr_sized_int_type
1903 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1904
b303c6f6
AB
1905/* Class, the destructor of which frees all allocated queue entries. This
1906 will only have work to do if an error was thrown while processing the
1907 dwarf. If no error was thrown then the queue entries should have all
1908 been processed, and freed, as we went along. */
1909
1910class dwarf2_queue_guard
1911{
1912public:
1913 dwarf2_queue_guard () = default;
1914
1915 /* Free any entries remaining on the queue. There should only be
1916 entries left if we hit an error while processing the dwarf. */
1917 ~dwarf2_queue_guard ()
1918 {
1919 struct dwarf2_queue_item *item, *last;
1920
1921 item = dwarf2_queue;
1922 while (item)
1923 {
1924 /* Anything still marked queued is likely to be in an
1925 inconsistent state, so discard it. */
1926 if (item->per_cu->queued)
1927 {
1928 if (item->per_cu->cu != NULL)
1929 free_one_cached_comp_unit (item->per_cu);
1930 item->per_cu->queued = 0;
1931 }
1932
1933 last = item;
1934 item = item->next;
1935 xfree (last);
1936 }
1937
1938 dwarf2_queue = dwarf2_queue_tail = NULL;
1939 }
1940};
1941
d721ba37
PA
1942/* The return type of find_file_and_directory. Note, the enclosed
1943 string pointers are only valid while this object is valid. */
1944
1945struct file_and_directory
1946{
1947 /* The filename. This is never NULL. */
1948 const char *name;
1949
1950 /* The compilation directory. NULL if not known. If we needed to
1951 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1952 points directly to the DW_AT_comp_dir string attribute owned by
1953 the obstack that owns the DIE. */
1954 const char *comp_dir;
1955
1956 /* If we needed to build a new string for comp_dir, this is what
1957 owns the storage. */
1958 std::string comp_dir_storage;
1959};
1960
1961static file_and_directory find_file_and_directory (struct die_info *die,
1962 struct dwarf2_cu *cu);
9291a0cd
TT
1963
1964static char *file_full_name (int file, struct line_header *lh,
1965 const char *comp_dir);
1966
43988095
JK
1967/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1968enum class rcuh_kind { COMPILE, TYPE };
1969
d521ce57 1970static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1971 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1972 struct comp_unit_head *header,
36586728 1973 struct dwarf2_section_info *section,
d521ce57 1974 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1975 rcuh_kind section_kind);
36586728 1976
fd820528 1977static void init_cutu_and_read_dies
f4dc4d17 1978 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1979 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1980 die_reader_func_ftype *die_reader_func, void *data);
1981
dee91e82
DE
1982static void init_cutu_and_read_dies_simple
1983 (struct dwarf2_per_cu_data *this_cu,
1984 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1985
673bfd45 1986static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1987
3019eac3
DE
1988static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1989
57d63ce2 1990static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1991 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1992 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1993 ULONGEST signature, int is_debug_types);
a2ce51a0 1994
ed2dc618
SM
1995static struct dwp_file *get_dwp_file
1996 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1997
3019eac3 1998static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1999 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2000
2001static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2002 (struct signatured_type *, const char *, const char *);
3019eac3 2003
89e63ee4
DE
2004static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2005
263db9a1
TT
2006/* A unique pointer to a dwo_file. */
2007
51ac9db5 2008typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2009
ed2dc618 2010static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2011
1b80a9fa 2012static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2013
2014static void free_line_header_voidp (void *arg);
4390d890
DE
2015\f
2016/* Various complaints about symbol reading that don't abort the process. */
2017
2018static void
2019dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2020{
b98664d3 2021 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2022}
2023
2024static void
2025dwarf2_debug_line_missing_file_complaint (void)
2026{
b98664d3 2027 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2028}
2029
2030static void
2031dwarf2_debug_line_missing_end_sequence_complaint (void)
2032{
b98664d3 2033 complaint (_(".debug_line section has line "
4390d890
DE
2034 "program sequence without an end"));
2035}
2036
2037static void
2038dwarf2_complex_location_expr_complaint (void)
2039{
b98664d3 2040 complaint (_("location expression too complex"));
4390d890
DE
2041}
2042
2043static void
2044dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2045 int arg3)
2046{
b98664d3 2047 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2048 arg1, arg2, arg3);
2049}
2050
2051static void
2052dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2053{
b98664d3 2054 complaint (_("debug info runs off end of %s section"
4390d890 2055 " [in module %s]"),
a32a8923
DE
2056 get_section_name (section),
2057 get_section_file_name (section));
4390d890 2058}
1b80a9fa 2059
4390d890
DE
2060static void
2061dwarf2_macro_malformed_definition_complaint (const char *arg1)
2062{
b98664d3 2063 complaint (_("macro debug info contains a "
4390d890
DE
2064 "malformed macro definition:\n`%s'"),
2065 arg1);
2066}
2067
2068static void
2069dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2070{
b98664d3 2071 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2072 arg1, arg2);
2073}
527f3840
JK
2074
2075/* Hash function for line_header_hash. */
2076
2077static hashval_t
2078line_header_hash (const struct line_header *ofs)
2079{
9c541725 2080 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2081}
2082
2083/* Hash function for htab_create_alloc_ex for line_header_hash. */
2084
2085static hashval_t
2086line_header_hash_voidp (const void *item)
2087{
9a3c8263 2088 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2089
2090 return line_header_hash (ofs);
2091}
2092
2093/* Equality function for line_header_hash. */
2094
2095static int
2096line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2097{
9a3c8263
SM
2098 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2099 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2100
9c541725 2101 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2102 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2103}
2104
4390d890 2105\f
9291a0cd 2106
31aa7e4e
JB
2107/* Read the given attribute value as an address, taking the attribute's
2108 form into account. */
2109
2110static CORE_ADDR
2111attr_value_as_address (struct attribute *attr)
2112{
2113 CORE_ADDR addr;
2114
336d760d
AT
2115 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2116 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2117 {
2118 /* Aside from a few clearly defined exceptions, attributes that
2119 contain an address must always be in DW_FORM_addr form.
2120 Unfortunately, some compilers happen to be violating this
2121 requirement by encoding addresses using other forms, such
2122 as DW_FORM_data4 for example. For those broken compilers,
2123 we try to do our best, without any guarantee of success,
2124 to interpret the address correctly. It would also be nice
2125 to generate a complaint, but that would require us to maintain
2126 a list of legitimate cases where a non-address form is allowed,
2127 as well as update callers to pass in at least the CU's DWARF
2128 version. This is more overhead than what we're willing to
2129 expand for a pretty rare case. */
2130 addr = DW_UNSND (attr);
2131 }
2132 else
2133 addr = DW_ADDR (attr);
2134
2135 return addr;
2136}
2137
330cdd98
PA
2138/* See declaration. */
2139
2140dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2141 const dwarf2_debug_sections *names,
2142 bool can_copy_)
2143 : objfile (objfile_),
2144 can_copy (can_copy_)
330cdd98
PA
2145{
2146 if (names == NULL)
2147 names = &dwarf2_elf_names;
2148
2149 bfd *obfd = objfile->obfd;
2150
2151 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2152 locate_sections (obfd, sec, *names);
2153}
2154
2155dwarf2_per_objfile::~dwarf2_per_objfile ()
2156{
2157 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2158 free_cached_comp_units ();
2159
2160 if (quick_file_names_table)
2161 htab_delete (quick_file_names_table);
2162
2163 if (line_header_hash)
2164 htab_delete (line_header_hash);
2165
b76e467d
SM
2166 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2167 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2168
b2bdb8cf
SM
2169 for (signatured_type *sig_type : all_type_units)
2170 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75 2171
330cdd98
PA
2172 /* Everything else should be on the objfile obstack. */
2173}
2174
2175/* See declaration. */
2176
2177void
2178dwarf2_per_objfile::free_cached_comp_units ()
2179{
2180 dwarf2_per_cu_data *per_cu = read_in_chain;
2181 dwarf2_per_cu_data **last_chain = &read_in_chain;
2182 while (per_cu != NULL)
2183 {
2184 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2185
fcd3b13d 2186 delete per_cu->cu;
330cdd98
PA
2187 *last_chain = next_cu;
2188 per_cu = next_cu;
2189 }
2190}
2191
11ed8cad
TT
2192/* A helper class that calls free_cached_comp_units on
2193 destruction. */
2194
2195class free_cached_comp_units
2196{
2197public:
2198
2199 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2200 : m_per_objfile (per_objfile)
2201 {
2202 }
2203
2204 ~free_cached_comp_units ()
2205 {
2206 m_per_objfile->free_cached_comp_units ();
2207 }
2208
2209 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2210
2211private:
2212
2213 dwarf2_per_objfile *m_per_objfile;
2214};
2215
c906108c 2216/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2217 information and return true if we have enough to do something.
2218 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2219 ELF names are used. CAN_COPY is true for formats where symbol
2220 interposition is possible and so symbol values must follow copy
2221 relocation rules. */
c906108c
SS
2222
2223int
251d32d9 2224dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2225 const struct dwarf2_debug_sections *names,
2226 bool can_copy)
c906108c 2227{
97cbe998
SDJ
2228 if (objfile->flags & OBJF_READNEVER)
2229 return 0;
2230
ed2dc618
SM
2231 struct dwarf2_per_objfile *dwarf2_per_objfile
2232 = get_dwarf2_per_objfile (objfile);
2233
2234 if (dwarf2_per_objfile == NULL)
5bfd760d 2235 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2236 names,
2237 can_copy);
5bfd760d 2238
73869dc2 2239 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2240 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2241 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2242 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2243}
2244
2245/* Return the containing section of virtual section SECTION. */
2246
2247static struct dwarf2_section_info *
2248get_containing_section (const struct dwarf2_section_info *section)
2249{
2250 gdb_assert (section->is_virtual);
2251 return section->s.containing_section;
c906108c
SS
2252}
2253
a32a8923
DE
2254/* Return the bfd owner of SECTION. */
2255
2256static struct bfd *
2257get_section_bfd_owner (const struct dwarf2_section_info *section)
2258{
73869dc2
DE
2259 if (section->is_virtual)
2260 {
2261 section = get_containing_section (section);
2262 gdb_assert (!section->is_virtual);
2263 }
049412e3 2264 return section->s.section->owner;
a32a8923
DE
2265}
2266
2267/* Return the bfd section of SECTION.
2268 Returns NULL if the section is not present. */
2269
2270static asection *
2271get_section_bfd_section (const struct dwarf2_section_info *section)
2272{
73869dc2
DE
2273 if (section->is_virtual)
2274 {
2275 section = get_containing_section (section);
2276 gdb_assert (!section->is_virtual);
2277 }
049412e3 2278 return section->s.section;
a32a8923
DE
2279}
2280
2281/* Return the name of SECTION. */
2282
2283static const char *
2284get_section_name (const struct dwarf2_section_info *section)
2285{
2286 asection *sectp = get_section_bfd_section (section);
2287
2288 gdb_assert (sectp != NULL);
fd361982 2289 return bfd_section_name (sectp);
a32a8923
DE
2290}
2291
2292/* Return the name of the file SECTION is in. */
2293
2294static const char *
2295get_section_file_name (const struct dwarf2_section_info *section)
2296{
2297 bfd *abfd = get_section_bfd_owner (section);
2298
2299 return bfd_get_filename (abfd);
2300}
2301
2302/* Return the id of SECTION.
2303 Returns 0 if SECTION doesn't exist. */
2304
2305static int
2306get_section_id (const struct dwarf2_section_info *section)
2307{
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 if (sectp == NULL)
2311 return 0;
2312 return sectp->id;
2313}
2314
2315/* Return the flags of SECTION.
73869dc2 2316 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2317
2318static int
2319get_section_flags (const struct dwarf2_section_info *section)
2320{
2321 asection *sectp = get_section_bfd_section (section);
2322
2323 gdb_assert (sectp != NULL);
fd361982 2324 return bfd_section_flags (sectp);
a32a8923
DE
2325}
2326
251d32d9
TG
2327/* When loading sections, we look either for uncompressed section or for
2328 compressed section names. */
233a11ab
CS
2329
2330static int
251d32d9
TG
2331section_is_p (const char *section_name,
2332 const struct dwarf2_section_names *names)
233a11ab 2333{
251d32d9
TG
2334 if (names->normal != NULL
2335 && strcmp (section_name, names->normal) == 0)
2336 return 1;
2337 if (names->compressed != NULL
2338 && strcmp (section_name, names->compressed) == 0)
2339 return 1;
2340 return 0;
233a11ab
CS
2341}
2342
330cdd98 2343/* See declaration. */
c906108c 2344
330cdd98
PA
2345void
2346dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2347 const dwarf2_debug_sections &names)
c906108c 2348{
fd361982 2349 flagword aflag = bfd_section_flags (sectp);
251d32d9 2350
dc7650b8
JK
2351 if ((aflag & SEC_HAS_CONTENTS) == 0)
2352 {
2353 }
330cdd98 2354 else if (section_is_p (sectp->name, &names.info))
c906108c 2355 {
330cdd98 2356 this->info.s.section = sectp;
fd361982 2357 this->info.size = bfd_section_size (sectp);
c906108c 2358 }
330cdd98 2359 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2360 {
330cdd98 2361 this->abbrev.s.section = sectp;
fd361982 2362 this->abbrev.size = bfd_section_size (sectp);
c906108c 2363 }
330cdd98 2364 else if (section_is_p (sectp->name, &names.line))
c906108c 2365 {
330cdd98 2366 this->line.s.section = sectp;
fd361982 2367 this->line.size = bfd_section_size (sectp);
c906108c 2368 }
330cdd98 2369 else if (section_is_p (sectp->name, &names.loc))
c906108c 2370 {
330cdd98 2371 this->loc.s.section = sectp;
fd361982 2372 this->loc.size = bfd_section_size (sectp);
c906108c 2373 }
330cdd98 2374 else if (section_is_p (sectp->name, &names.loclists))
43988095 2375 {
330cdd98 2376 this->loclists.s.section = sectp;
fd361982 2377 this->loclists.size = bfd_section_size (sectp);
43988095 2378 }
330cdd98 2379 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2380 {
330cdd98 2381 this->macinfo.s.section = sectp;
fd361982 2382 this->macinfo.size = bfd_section_size (sectp);
c906108c 2383 }
330cdd98 2384 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2385 {
330cdd98 2386 this->macro.s.section = sectp;
fd361982 2387 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2388 }
330cdd98 2389 else if (section_is_p (sectp->name, &names.str))
c906108c 2390 {
330cdd98 2391 this->str.s.section = sectp;
fd361982 2392 this->str.size = bfd_section_size (sectp);
c906108c 2393 }
330cdd98 2394 else if (section_is_p (sectp->name, &names.line_str))
43988095 2395 {
330cdd98 2396 this->line_str.s.section = sectp;
fd361982 2397 this->line_str.size = bfd_section_size (sectp);
43988095 2398 }
330cdd98 2399 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2400 {
330cdd98 2401 this->addr.s.section = sectp;
fd361982 2402 this->addr.size = bfd_section_size (sectp);
3019eac3 2403 }
330cdd98 2404 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2405 {
330cdd98 2406 this->frame.s.section = sectp;
fd361982 2407 this->frame.size = bfd_section_size (sectp);
b6af0555 2408 }
330cdd98 2409 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2410 {
330cdd98 2411 this->eh_frame.s.section = sectp;
fd361982 2412 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2413 }
330cdd98 2414 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2415 {
330cdd98 2416 this->ranges.s.section = sectp;
fd361982 2417 this->ranges.size = bfd_section_size (sectp);
af34e669 2418 }
330cdd98 2419 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2420 {
330cdd98 2421 this->rnglists.s.section = sectp;
fd361982 2422 this->rnglists.size = bfd_section_size (sectp);
43988095 2423 }
330cdd98 2424 else if (section_is_p (sectp->name, &names.types))
348e048f 2425 {
8b70b953
TT
2426 struct dwarf2_section_info type_section;
2427
2428 memset (&type_section, 0, sizeof (type_section));
049412e3 2429 type_section.s.section = sectp;
fd361982 2430 type_section.size = bfd_section_size (sectp);
8b70b953 2431
fd5866f6 2432 this->types.push_back (type_section);
348e048f 2433 }
330cdd98 2434 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2435 {
330cdd98 2436 this->gdb_index.s.section = sectp;
fd361982 2437 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2438 }
927aa2e7
JK
2439 else if (section_is_p (sectp->name, &names.debug_names))
2440 {
2441 this->debug_names.s.section = sectp;
fd361982 2442 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2443 }
2444 else if (section_is_p (sectp->name, &names.debug_aranges))
2445 {
2446 this->debug_aranges.s.section = sectp;
fd361982 2447 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2448 }
dce234bc 2449
fd361982
AM
2450 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2451 && bfd_section_vma (sectp) == 0)
330cdd98 2452 this->has_section_at_zero = true;
c906108c
SS
2453}
2454
fceca515
DE
2455/* A helper function that decides whether a section is empty,
2456 or not present. */
9e0ac564
TT
2457
2458static int
19ac8c2e 2459dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2460{
73869dc2
DE
2461 if (section->is_virtual)
2462 return section->size == 0;
049412e3 2463 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2464}
2465
cd4fb1b2 2466/* See dwarf2read.h. */
c906108c 2467
cd4fb1b2
SM
2468void
2469dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2470{
a32a8923 2471 asection *sectp;
3019eac3 2472 bfd *abfd;
dce234bc 2473 gdb_byte *buf, *retbuf;
c906108c 2474
be391dca
TT
2475 if (info->readin)
2476 return;
dce234bc 2477 info->buffer = NULL;
dc4ccb6f 2478 info->readin = true;
188dd5d6 2479
9e0ac564 2480 if (dwarf2_section_empty_p (info))
dce234bc 2481 return;
c906108c 2482
a32a8923 2483 sectp = get_section_bfd_section (info);
3019eac3 2484
73869dc2
DE
2485 /* If this is a virtual section we need to read in the real one first. */
2486 if (info->is_virtual)
2487 {
2488 struct dwarf2_section_info *containing_section =
2489 get_containing_section (info);
2490
2491 gdb_assert (sectp != NULL);
2492 if ((sectp->flags & SEC_RELOC) != 0)
2493 {
2494 error (_("Dwarf Error: DWP format V2 with relocations is not"
2495 " supported in section %s [in module %s]"),
2496 get_section_name (info), get_section_file_name (info));
2497 }
2498 dwarf2_read_section (objfile, containing_section);
2499 /* Other code should have already caught virtual sections that don't
2500 fit. */
2501 gdb_assert (info->virtual_offset + info->size
2502 <= containing_section->size);
2503 /* If the real section is empty or there was a problem reading the
2504 section we shouldn't get here. */
2505 gdb_assert (containing_section->buffer != NULL);
2506 info->buffer = containing_section->buffer + info->virtual_offset;
2507 return;
2508 }
2509
4bf44c1c
TT
2510 /* If the section has relocations, we must read it ourselves.
2511 Otherwise we attach it to the BFD. */
2512 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2513 {
d521ce57 2514 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2515 return;
dce234bc 2516 }
dce234bc 2517
224c3ddb 2518 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2519 info->buffer = buf;
dce234bc
PP
2520
2521 /* When debugging .o files, we may need to apply relocations; see
2522 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2523 We never compress sections in .o files, so we only need to
2524 try this when the section is not compressed. */
ac8035ab 2525 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2526 if (retbuf != NULL)
2527 {
2528 info->buffer = retbuf;
2529 return;
2530 }
2531
a32a8923
DE
2532 abfd = get_section_bfd_owner (info);
2533 gdb_assert (abfd != NULL);
2534
dce234bc
PP
2535 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2536 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2537 {
2538 error (_("Dwarf Error: Can't read DWARF data"
2539 " in section %s [in module %s]"),
fd361982 2540 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2541 }
dce234bc
PP
2542}
2543
9e0ac564
TT
2544/* A helper function that returns the size of a section in a safe way.
2545 If you are positive that the section has been read before using the
2546 size, then it is safe to refer to the dwarf2_section_info object's
2547 "size" field directly. In other cases, you must call this
2548 function, because for compressed sections the size field is not set
2549 correctly until the section has been read. */
2550
2551static bfd_size_type
2552dwarf2_section_size (struct objfile *objfile,
2553 struct dwarf2_section_info *info)
2554{
2555 if (!info->readin)
2556 dwarf2_read_section (objfile, info);
2557 return info->size;
2558}
2559
dce234bc 2560/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2561 SECTION_NAME. */
af34e669 2562
dce234bc 2563void
3017a003
TG
2564dwarf2_get_section_info (struct objfile *objfile,
2565 enum dwarf2_section_enum sect,
d521ce57 2566 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2567 bfd_size_type *sizep)
2568{
5bfd760d 2569 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2570 struct dwarf2_section_info *info;
a3b2a86b
TT
2571
2572 /* We may see an objfile without any DWARF, in which case we just
2573 return nothing. */
2574 if (data == NULL)
2575 {
2576 *sectp = NULL;
2577 *bufp = NULL;
2578 *sizep = 0;
2579 return;
2580 }
3017a003
TG
2581 switch (sect)
2582 {
2583 case DWARF2_DEBUG_FRAME:
2584 info = &data->frame;
2585 break;
2586 case DWARF2_EH_FRAME:
2587 info = &data->eh_frame;
2588 break;
2589 default:
2590 gdb_assert_not_reached ("unexpected section");
2591 }
dce234bc 2592
9e0ac564 2593 dwarf2_read_section (objfile, info);
dce234bc 2594
a32a8923 2595 *sectp = get_section_bfd_section (info);
dce234bc
PP
2596 *bufp = info->buffer;
2597 *sizep = info->size;
2598}
2599
36586728
TT
2600/* A helper function to find the sections for a .dwz file. */
2601
2602static void
2603locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2604{
9a3c8263 2605 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2606
2607 /* Note that we only support the standard ELF names, because .dwz
2608 is ELF-only (at the time of writing). */
2609 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2610 {
049412e3 2611 dwz_file->abbrev.s.section = sectp;
fd361982 2612 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2613 }
2614 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2615 {
049412e3 2616 dwz_file->info.s.section = sectp;
fd361982 2617 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2618 }
2619 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2620 {
049412e3 2621 dwz_file->str.s.section = sectp;
fd361982 2622 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2623 }
2624 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2625 {
049412e3 2626 dwz_file->line.s.section = sectp;
fd361982 2627 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2628 }
2629 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2630 {
049412e3 2631 dwz_file->macro.s.section = sectp;
fd361982 2632 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2633 }
2ec9a5e0
TT
2634 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2635 {
049412e3 2636 dwz_file->gdb_index.s.section = sectp;
fd361982 2637 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2638 }
927aa2e7
JK
2639 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2640 {
2641 dwz_file->debug_names.s.section = sectp;
fd361982 2642 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2643 }
36586728
TT
2644}
2645
c4973306 2646/* See dwarf2read.h. */
36586728 2647
c4973306 2648struct dwz_file *
ed2dc618 2649dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2650{
36586728 2651 const char *filename;
acd13123 2652 bfd_size_type buildid_len_arg;
dc294be5
TT
2653 size_t buildid_len;
2654 bfd_byte *buildid;
36586728
TT
2655
2656 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2657 return dwarf2_per_objfile->dwz_file.get ();
36586728 2658
4db1a1dc 2659 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2660 gdb::unique_xmalloc_ptr<char> data
2661 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2662 &buildid_len_arg, &buildid));
4db1a1dc
TT
2663 if (data == NULL)
2664 {
2665 if (bfd_get_error () == bfd_error_no_error)
2666 return NULL;
2667 error (_("could not read '.gnu_debugaltlink' section: %s"),
2668 bfd_errmsg (bfd_get_error ()));
2669 }
791afaa2
TT
2670
2671 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2672
acd13123
TT
2673 buildid_len = (size_t) buildid_len_arg;
2674
791afaa2 2675 filename = data.get ();
d721ba37
PA
2676
2677 std::string abs_storage;
36586728
TT
2678 if (!IS_ABSOLUTE_PATH (filename))
2679 {
14278e1f
TT
2680 gdb::unique_xmalloc_ptr<char> abs
2681 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2682
14278e1f 2683 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2684 filename = abs_storage.c_str ();
36586728
TT
2685 }
2686
dc294be5
TT
2687 /* First try the file name given in the section. If that doesn't
2688 work, try to use the build-id instead. */
192b62ce 2689 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2690 if (dwz_bfd != NULL)
36586728 2691 {
192b62ce 2692 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2693 dwz_bfd.reset (nullptr);
36586728
TT
2694 }
2695
dc294be5
TT
2696 if (dwz_bfd == NULL)
2697 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2698
2699 if (dwz_bfd == NULL)
2700 error (_("could not find '.gnu_debugaltlink' file for %s"),
2701 objfile_name (dwarf2_per_objfile->objfile));
2702
7ff8cb8c
TT
2703 std::unique_ptr<struct dwz_file> result
2704 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2705
7ff8cb8c
TT
2706 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2707 result.get ());
36586728 2708
7ff8cb8c
TT
2709 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2710 result->dwz_bfd.get ());
2711 dwarf2_per_objfile->dwz_file = std::move (result);
2712 return dwarf2_per_objfile->dwz_file.get ();
36586728 2713}
9291a0cd 2714\f
7b9f3c50
DE
2715/* DWARF quick_symbols_functions support. */
2716
2717/* TUs can share .debug_line entries, and there can be a lot more TUs than
2718 unique line tables, so we maintain a separate table of all .debug_line
2719 derived entries to support the sharing.
2720 All the quick functions need is the list of file names. We discard the
2721 line_header when we're done and don't need to record it here. */
2722struct quick_file_names
2723{
094b34ac
DE
2724 /* The data used to construct the hash key. */
2725 struct stmt_list_hash hash;
7b9f3c50
DE
2726
2727 /* The number of entries in file_names, real_names. */
2728 unsigned int num_file_names;
2729
2730 /* The file names from the line table, after being run through
2731 file_full_name. */
2732 const char **file_names;
2733
2734 /* The file names from the line table after being run through
2735 gdb_realpath. These are computed lazily. */
2736 const char **real_names;
2737};
2738
2739/* When using the index (and thus not using psymtabs), each CU has an
2740 object of this type. This is used to hold information needed by
2741 the various "quick" methods. */
2742struct dwarf2_per_cu_quick_data
2743{
2744 /* The file table. This can be NULL if there was no file table
2745 or it's currently not read in.
2746 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2747 struct quick_file_names *file_names;
2748
2749 /* The corresponding symbol table. This is NULL if symbols for this
2750 CU have not yet been read. */
43f3e411 2751 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2752
2753 /* A temporary mark bit used when iterating over all CUs in
2754 expand_symtabs_matching. */
2755 unsigned int mark : 1;
2756
2757 /* True if we've tried to read the file table and found there isn't one.
2758 There will be no point in trying to read it again next time. */
2759 unsigned int no_file_data : 1;
2760};
2761
094b34ac
DE
2762/* Utility hash function for a stmt_list_hash. */
2763
2764static hashval_t
2765hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2766{
2767 hashval_t v = 0;
2768
2769 if (stmt_list_hash->dwo_unit != NULL)
2770 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2771 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2772 return v;
2773}
2774
2775/* Utility equality function for a stmt_list_hash. */
2776
2777static int
2778eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2779 const struct stmt_list_hash *rhs)
2780{
2781 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2782 return 0;
2783 if (lhs->dwo_unit != NULL
2784 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2785 return 0;
2786
9c541725 2787 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2788}
2789
7b9f3c50
DE
2790/* Hash function for a quick_file_names. */
2791
2792static hashval_t
2793hash_file_name_entry (const void *e)
2794{
9a3c8263
SM
2795 const struct quick_file_names *file_data
2796 = (const struct quick_file_names *) e;
7b9f3c50 2797
094b34ac 2798 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2799}
2800
2801/* Equality function for a quick_file_names. */
2802
2803static int
2804eq_file_name_entry (const void *a, const void *b)
2805{
9a3c8263
SM
2806 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2807 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2808
094b34ac 2809 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2810}
2811
2812/* Delete function for a quick_file_names. */
2813
2814static void
2815delete_file_name_entry (void *e)
2816{
9a3c8263 2817 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2818 int i;
2819
2820 for (i = 0; i < file_data->num_file_names; ++i)
2821 {
2822 xfree ((void*) file_data->file_names[i]);
2823 if (file_data->real_names)
2824 xfree ((void*) file_data->real_names[i]);
2825 }
2826
2827 /* The space for the struct itself lives on objfile_obstack,
2828 so we don't free it here. */
2829}
2830
2831/* Create a quick_file_names hash table. */
2832
2833static htab_t
2834create_quick_file_names_table (unsigned int nr_initial_entries)
2835{
2836 return htab_create_alloc (nr_initial_entries,
2837 hash_file_name_entry, eq_file_name_entry,
2838 delete_file_name_entry, xcalloc, xfree);
2839}
9291a0cd 2840
918dd910
JK
2841/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2842 have to be created afterwards. You should call age_cached_comp_units after
2843 processing PER_CU->CU. dw2_setup must have been already called. */
2844
2845static void
58f0c718 2846load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2847{
3019eac3 2848 if (per_cu->is_debug_types)
e5fe5e75 2849 load_full_type_unit (per_cu);
918dd910 2850 else
58f0c718 2851 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2852
cc12ce38
DE
2853 if (per_cu->cu == NULL)
2854 return; /* Dummy CU. */
2dc860c0
DE
2855
2856 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2857}
2858
a0f42c21 2859/* Read in the symbols for PER_CU. */
2fdf6df6 2860
9291a0cd 2861static void
58f0c718 2862dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2863{
ed2dc618 2864 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2865
f4dc4d17
DE
2866 /* Skip type_unit_groups, reading the type units they contain
2867 is handled elsewhere. */
2868 if (IS_TYPE_UNIT_GROUP (per_cu))
2869 return;
2870
b303c6f6
AB
2871 /* The destructor of dwarf2_queue_guard frees any entries left on
2872 the queue. After this point we're guaranteed to leave this function
2873 with the dwarf queue empty. */
2874 dwarf2_queue_guard q_guard;
9291a0cd 2875
95554aad 2876 if (dwarf2_per_objfile->using_index
43f3e411 2877 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2878 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2879 {
2880 queue_comp_unit (per_cu, language_minimal);
58f0c718 2881 load_cu (per_cu, skip_partial);
89e63ee4
DE
2882
2883 /* If we just loaded a CU from a DWO, and we're working with an index
2884 that may badly handle TUs, load all the TUs in that DWO as well.
2885 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2886 if (!per_cu->is_debug_types
cc12ce38 2887 && per_cu->cu != NULL
89e63ee4
DE
2888 && per_cu->cu->dwo_unit != NULL
2889 && dwarf2_per_objfile->index_table != NULL
2890 && dwarf2_per_objfile->index_table->version <= 7
2891 /* DWP files aren't supported yet. */
ed2dc618 2892 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2893 queue_and_load_all_dwo_tus (per_cu);
95554aad 2894 }
9291a0cd 2895
ed2dc618 2896 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2897
2898 /* Age the cache, releasing compilation units that have not
2899 been used recently. */
ed2dc618 2900 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2901}
2902
2903/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2904 the objfile from which this CU came. Returns the resulting symbol
2905 table. */
2fdf6df6 2906
43f3e411 2907static struct compunit_symtab *
58f0c718 2908dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2909{
ed2dc618
SM
2910 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2911
95554aad 2912 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2913 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2914 {
11ed8cad 2915 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2916 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2917 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2918 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2919 }
f194fefb 2920
43f3e411 2921 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2922}
2923
ff4c9fec 2924/* See declaration. */
f4dc4d17 2925
ff4c9fec
SM
2926dwarf2_per_cu_data *
2927dwarf2_per_objfile::get_cutu (int index)
2928{
b76e467d 2929 if (index >= this->all_comp_units.size ())
ff4c9fec 2930 {
b76e467d 2931 index -= this->all_comp_units.size ();
b2bdb8cf 2932 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2933 return &this->all_type_units[index]->per_cu;
2934 }
f4dc4d17 2935
ff4c9fec
SM
2936 return this->all_comp_units[index];
2937}
f4dc4d17 2938
ff4c9fec 2939/* See declaration. */
2fdf6df6 2940
ff4c9fec
SM
2941dwarf2_per_cu_data *
2942dwarf2_per_objfile::get_cu (int index)
1fd400ff 2943{
b76e467d 2944 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2945
ff4c9fec 2946 return this->all_comp_units[index];
f4dc4d17
DE
2947}
2948
ff4c9fec 2949/* See declaration. */
f4dc4d17 2950
ff4c9fec
SM
2951signatured_type *
2952dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2953{
b2bdb8cf 2954 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2955
ff4c9fec 2956 return this->all_type_units[index];
1fd400ff
TT
2957}
2958
4b514bc8
JK
2959/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2960 objfile_obstack, and constructed with the specified field
2961 values. */
2962
2963static dwarf2_per_cu_data *
ed2dc618 2964create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2965 struct dwarf2_section_info *section,
2966 int is_dwz,
2967 sect_offset sect_off, ULONGEST length)
2968{
ed2dc618 2969 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2970 dwarf2_per_cu_data *the_cu
2971 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2972 struct dwarf2_per_cu_data);
2973 the_cu->sect_off = sect_off;
2974 the_cu->length = length;
e3b94546 2975 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2976 the_cu->section = section;
2977 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2978 struct dwarf2_per_cu_quick_data);
2979 the_cu->is_dwz = is_dwz;
2980 return the_cu;
2981}
2982
2ec9a5e0
TT
2983/* A helper for create_cus_from_index that handles a given list of
2984 CUs. */
2fdf6df6 2985
74a0d9f6 2986static void
12359b5e 2987create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2988 const gdb_byte *cu_list, offset_type n_elements,
2989 struct dwarf2_section_info *section,
b76e467d 2990 int is_dwz)
9291a0cd 2991{
12359b5e 2992 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2993 {
74a0d9f6 2994 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2995
2996 sect_offset sect_off
2997 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2998 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2999 cu_list += 2 * 8;
3000
b76e467d 3001 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3002 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3003 sect_off, length);
b76e467d 3004 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3005 }
9291a0cd
TT
3006}
3007
2ec9a5e0 3008/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3009 the CU objects for this objfile. */
2ec9a5e0 3010
74a0d9f6 3011static void
12359b5e 3012create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3013 const gdb_byte *cu_list, offset_type cu_list_elements,
3014 const gdb_byte *dwz_list, offset_type dwz_elements)
3015{
b76e467d
SM
3016 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3017 dwarf2_per_objfile->all_comp_units.reserve
3018 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3019
12359b5e 3020 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3021 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3022
3023 if (dwz_elements == 0)
74a0d9f6 3024 return;
2ec9a5e0 3025
12359b5e
SM
3026 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3027 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3028 &dwz->info, 1);
2ec9a5e0
TT
3029}
3030
1fd400ff 3031/* Create the signatured type hash table from the index. */
673bfd45 3032
74a0d9f6 3033static void
12359b5e
SM
3034create_signatured_type_table_from_index
3035 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3036 struct dwarf2_section_info *section,
3037 const gdb_byte *bytes,
3038 offset_type elements)
1fd400ff 3039{
12359b5e 3040 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3041
b2bdb8cf
SM
3042 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3043 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3044
12359b5e 3045 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3046
12359b5e 3047 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3048 {
52dc124a 3049 struct signatured_type *sig_type;
9c541725 3050 ULONGEST signature;
1fd400ff 3051 void **slot;
9c541725 3052 cu_offset type_offset_in_tu;
1fd400ff 3053
74a0d9f6 3054 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3055 sect_offset sect_off
3056 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3057 type_offset_in_tu
3058 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3059 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3060 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3061 bytes += 3 * 8;
3062
52dc124a 3063 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3064 struct signatured_type);
52dc124a 3065 sig_type->signature = signature;
9c541725 3066 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3067 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3068 sig_type->per_cu.section = section;
9c541725 3069 sig_type->per_cu.sect_off = sect_off;
e3b94546 3070 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3071 sig_type->per_cu.v.quick
1fd400ff
TT
3072 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3073 struct dwarf2_per_cu_quick_data);
3074
52dc124a
DE
3075 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3076 *slot = sig_type;
1fd400ff 3077
b2bdb8cf 3078 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3079 }
3080
673bfd45 3081 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3082}
3083
927aa2e7
JK
3084/* Create the signatured type hash table from .debug_names. */
3085
3086static void
3087create_signatured_type_table_from_debug_names
ed2dc618 3088 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3089 const mapped_debug_names &map,
3090 struct dwarf2_section_info *section,
3091 struct dwarf2_section_info *abbrev_section)
3092{
ed2dc618
SM
3093 struct objfile *objfile = dwarf2_per_objfile->objfile;
3094
927aa2e7
JK
3095 dwarf2_read_section (objfile, section);
3096 dwarf2_read_section (objfile, abbrev_section);
3097
b2bdb8cf
SM
3098 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3099 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3100
3101 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3102
3103 for (uint32_t i = 0; i < map.tu_count; ++i)
3104 {
3105 struct signatured_type *sig_type;
927aa2e7 3106 void **slot;
927aa2e7
JK
3107
3108 sect_offset sect_off
3109 = (sect_offset) (extract_unsigned_integer
3110 (map.tu_table_reordered + i * map.offset_size,
3111 map.offset_size,
3112 map.dwarf5_byte_order));
3113
3114 comp_unit_head cu_header;
ed2dc618
SM
3115 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3116 abbrev_section,
927aa2e7
JK
3117 section->buffer + to_underlying (sect_off),
3118 rcuh_kind::TYPE);
3119
3120 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3121 struct signatured_type);
3122 sig_type->signature = cu_header.signature;
3123 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3124 sig_type->per_cu.is_debug_types = 1;
3125 sig_type->per_cu.section = section;
3126 sig_type->per_cu.sect_off = sect_off;
e3b94546 3127 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3128 sig_type->per_cu.v.quick
3129 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3130 struct dwarf2_per_cu_quick_data);
3131
3132 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3133 *slot = sig_type;
3134
b2bdb8cf 3135 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3136 }
3137
3138 dwarf2_per_objfile->signatured_types = sig_types_hash;
3139}
3140
9291a0cd
TT
3141/* Read the address map data from the mapped index, and use it to
3142 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3143
9291a0cd 3144static void
ed2dc618
SM
3145create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3146 struct mapped_index *index)
9291a0cd 3147{
ed2dc618 3148 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3150 const gdb_byte *iter, *end;
9291a0cd 3151 struct addrmap *mutable_map;
9291a0cd
TT
3152 CORE_ADDR baseaddr;
3153
8268c778
PA
3154 auto_obstack temp_obstack;
3155
9291a0cd
TT
3156 mutable_map = addrmap_create_mutable (&temp_obstack);
3157
f00a2de2
PA
3158 iter = index->address_table.data ();
3159 end = iter + index->address_table.size ();
9291a0cd
TT
3160
3161 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3162
3163 while (iter < end)
3164 {
3165 ULONGEST hi, lo, cu_index;
3166 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3167 iter += 8;
3168 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3169 iter += 8;
3170 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3171 iter += 4;
f652bce2 3172
24a55014 3173 if (lo > hi)
f652bce2 3174 {
b98664d3 3175 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3176 hex_string (lo), hex_string (hi));
24a55014 3177 continue;
f652bce2 3178 }
24a55014 3179
b76e467d 3180 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3181 {
b98664d3 3182 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3183 (unsigned) cu_index);
24a55014 3184 continue;
f652bce2 3185 }
24a55014 3186
79748972
TT
3187 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3188 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3189 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3190 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3191 }
3192
d320c2b5 3193 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3194 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3195}
3196
927aa2e7
JK
3197/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3198 populate the objfile's psymtabs_addrmap. */
3199
3200static void
ed2dc618 3201create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3202 struct dwarf2_section_info *section)
3203{
ed2dc618 3204 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3205 bfd *abfd = objfile->obfd;
3206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3207 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3208 SECT_OFF_TEXT (objfile));
3209
3210 auto_obstack temp_obstack;
3211 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3212
3213 std::unordered_map<sect_offset,
3214 dwarf2_per_cu_data *,
3215 gdb::hash_enum<sect_offset>>
3216 debug_info_offset_to_per_cu;
b76e467d 3217 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3218 {
927aa2e7
JK
3219 const auto insertpair
3220 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3221 if (!insertpair.second)
3222 {
3223 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3224 "debug_info_offset %s, ignoring .debug_aranges."),
3225 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3226 return;
3227 }
3228 }
3229
3230 dwarf2_read_section (objfile, section);
3231
3232 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3233
3234 const gdb_byte *addr = section->buffer;
3235
3236 while (addr < section->buffer + section->size)
3237 {
3238 const gdb_byte *const entry_addr = addr;
3239 unsigned int bytes_read;
3240
3241 const LONGEST entry_length = read_initial_length (abfd, addr,
3242 &bytes_read);
3243 addr += bytes_read;
3244
3245 const gdb_byte *const entry_end = addr + entry_length;
3246 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3247 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3248 if (addr + entry_length > section->buffer + section->size)
3249 {
47e3f474 3250 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3251 "length %s exceeds section length %s, "
3252 "ignoring .debug_aranges."),
47e3f474
TV
3253 objfile_name (objfile),
3254 plongest (entry_addr - section->buffer),
927aa2e7
JK
3255 plongest (bytes_read + entry_length),
3256 pulongest (section->size));
3257 return;
3258 }
3259
3260 /* The version number. */
3261 const uint16_t version = read_2_bytes (abfd, addr);
3262 addr += 2;
3263 if (version != 2)
3264 {
47e3f474 3265 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3266 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3267 objfile_name (objfile),
3268 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3269 return;
3270 }
3271
3272 const uint64_t debug_info_offset
3273 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3274 addr += offset_size;
3275 const auto per_cu_it
3276 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3277 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3278 {
47e3f474 3279 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3280 "debug_info_offset %s does not exists, "
3281 "ignoring .debug_aranges."),
47e3f474
TV
3282 objfile_name (objfile),
3283 plongest (entry_addr - section->buffer),
927aa2e7
JK
3284 pulongest (debug_info_offset));
3285 return;
3286 }
3287 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3288
3289 const uint8_t address_size = *addr++;
3290 if (address_size < 1 || address_size > 8)
3291 {
47e3f474 3292 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3293 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3294 objfile_name (objfile),
3295 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3296 return;
3297 }
3298
3299 const uint8_t segment_selector_size = *addr++;
3300 if (segment_selector_size != 0)
3301 {
47e3f474 3302 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3303 "segment_selector_size %u is not supported, "
3304 "ignoring .debug_aranges."),
47e3f474
TV
3305 objfile_name (objfile),
3306 plongest (entry_addr - section->buffer),
927aa2e7
JK
3307 segment_selector_size);
3308 return;
3309 }
3310
3311 /* Must pad to an alignment boundary that is twice the address
3312 size. It is undocumented by the DWARF standard but GCC does
3313 use it. */
3314 for (size_t padding = ((-(addr - section->buffer))
3315 & (2 * address_size - 1));
3316 padding > 0; padding--)
3317 if (*addr++ != 0)
3318 {
47e3f474 3319 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3320 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3321 objfile_name (objfile),
3322 plongest (entry_addr - section->buffer));
927aa2e7
JK
3323 return;
3324 }
3325
3326 for (;;)
3327 {
3328 if (addr + 2 * address_size > entry_end)
3329 {
47e3f474 3330 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3331 "address list is not properly terminated, "
3332 "ignoring .debug_aranges."),
47e3f474
TV
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer));
927aa2e7
JK
3335 return;
3336 }
3337 ULONGEST start = extract_unsigned_integer (addr, address_size,
3338 dwarf5_byte_order);
3339 addr += address_size;
3340 ULONGEST length = extract_unsigned_integer (addr, address_size,
3341 dwarf5_byte_order);
3342 addr += address_size;
3343 if (start == 0 && length == 0)
3344 break;
3345 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3346 {
3347 /* Symbol was eliminated due to a COMDAT group. */
3348 continue;
3349 }
3350 ULONGEST end = start + length;
79748972
TT
3351 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3352 - baseaddr);
3353 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3354 - baseaddr);
927aa2e7
JK
3355 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3356 }
3357 }
3358
d320c2b5 3359 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3360 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3361}
3362
9291a0cd
TT
3363/* Find a slot in the mapped index INDEX for the object named NAME.
3364 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3365 constant pool and return true. If NAME cannot be found, return
3366 false. */
2fdf6df6 3367
109483d9 3368static bool
9291a0cd
TT
3369find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3370 offset_type **vec_out)
3371{
0cf03b49 3372 offset_type hash;
9291a0cd 3373 offset_type slot, step;
559a7a62 3374 int (*cmp) (const char *, const char *);
9291a0cd 3375
791afaa2 3376 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3377 if (current_language->la_language == language_cplus
45280282
IB
3378 || current_language->la_language == language_fortran
3379 || current_language->la_language == language_d)
0cf03b49
JK
3380 {
3381 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3382 not contain any. */
a8719064 3383
72998fb3 3384 if (strchr (name, '(') != NULL)
0cf03b49 3385 {
109483d9 3386 without_params = cp_remove_params (name);
0cf03b49 3387
72998fb3 3388 if (without_params != NULL)
791afaa2 3389 name = without_params.get ();
0cf03b49
JK
3390 }
3391 }
3392
559a7a62 3393 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3394 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3395 simulate our NAME being searched is also lowercased. */
3396 hash = mapped_index_string_hash ((index->version == 4
3397 && case_sensitivity == case_sensitive_off
3398 ? 5 : index->version),
3399 name);
3400
f00a2de2
PA
3401 slot = hash & (index->symbol_table.size () - 1);
3402 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3403 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3404
3405 for (;;)
3406 {
9291a0cd 3407 const char *str;
f00a2de2
PA
3408
3409 const auto &bucket = index->symbol_table[slot];
3410 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3411 return false;
9291a0cd 3412
f00a2de2 3413 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3414 if (!cmp (name, str))
9291a0cd
TT
3415 {
3416 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3417 + MAYBE_SWAP (bucket.vec));
109483d9 3418 return true;
9291a0cd
TT
3419 }
3420
f00a2de2 3421 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3422 }
3423}
3424
4485a1c1
SM
3425/* A helper function that reads the .gdb_index from BUFFER and fills
3426 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3427 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3428 ok to use deprecated sections.
3429
3430 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3431 out parameters that are filled in with information about the CU and
3432 TU lists in the section.
3433
4485a1c1 3434 Returns true if all went well, false otherwise. */
2fdf6df6 3435
d33bc52e 3436static bool
4485a1c1
SM
3437read_gdb_index_from_buffer (struct objfile *objfile,
3438 const char *filename,
3439 bool deprecated_ok,
3440 gdb::array_view<const gdb_byte> buffer,
3441 struct mapped_index *map,
3442 const gdb_byte **cu_list,
3443 offset_type *cu_list_elements,
3444 const gdb_byte **types_list,
3445 offset_type *types_list_elements)
3446{
3447 const gdb_byte *addr = &buffer[0];
82430852 3448
9291a0cd 3449 /* Version check. */
4485a1c1 3450 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3451 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3452 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3453 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3454 indices. */
831adc1f 3455 if (version < 4)
481860b3
GB
3456 {
3457 static int warning_printed = 0;
3458 if (!warning_printed)
3459 {
3460 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3461 filename);
481860b3
GB
3462 warning_printed = 1;
3463 }
3464 return 0;
3465 }
3466 /* Index version 4 uses a different hash function than index version
3467 5 and later.
3468
3469 Versions earlier than 6 did not emit psymbols for inlined
3470 functions. Using these files will cause GDB not to be able to
3471 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3472 indices unless the user has done
3473 "set use-deprecated-index-sections on". */
2ec9a5e0 3474 if (version < 6 && !deprecated_ok)
481860b3
GB
3475 {
3476 static int warning_printed = 0;
3477 if (!warning_printed)
3478 {
e615022a
DE
3479 warning (_("\
3480Skipping deprecated .gdb_index section in %s.\n\
3481Do \"set use-deprecated-index-sections on\" before the file is read\n\
3482to use the section anyway."),
2ec9a5e0 3483 filename);
481860b3
GB
3484 warning_printed = 1;
3485 }
3486 return 0;
3487 }
796a7ff8 3488 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3489 of the TU (for symbols coming from TUs),
3490 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3491 Plus gold-generated indices can have duplicate entries for global symbols,
3492 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3493 These are just performance bugs, and we can't distinguish gdb-generated
3494 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3495
481860b3 3496 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3497 longer backward compatible. */
796a7ff8 3498 if (version > 8)
594e8718 3499 return 0;
9291a0cd 3500
559a7a62 3501 map->version = version;
9291a0cd 3502
4485a1c1 3503 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3504
4485a1c1 3505 int i = 0;
2ec9a5e0
TT
3506 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3507 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3508 / 8);
1fd400ff
TT
3509 ++i;
3510
2ec9a5e0
TT
3511 *types_list = addr + MAYBE_SWAP (metadata[i]);
3512 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3513 - MAYBE_SWAP (metadata[i]))
3514 / 8);
987d643c 3515 ++i;
1fd400ff 3516
f00a2de2
PA
3517 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3518 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 map->address_table
3520 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3521 ++i;
3522
f00a2de2
PA
3523 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3524 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3525 map->symbol_table
3526 = gdb::array_view<mapped_index::symbol_table_slot>
3527 ((mapped_index::symbol_table_slot *) symbol_table,
3528 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3529
f00a2de2 3530 ++i;
f9d83a0b 3531 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3532
2ec9a5e0
TT
3533 return 1;
3534}
3535
4485a1c1
SM
3536/* Callback types for dwarf2_read_gdb_index. */
3537
3538typedef gdb::function_view
3539 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3540 get_gdb_index_contents_ftype;
3541typedef gdb::function_view
3542 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3543 get_gdb_index_contents_dwz_ftype;
3544
927aa2e7 3545/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3546 elements of all the CUs and return 1. Otherwise, return 0. */
3547
3548static int
4485a1c1
SM
3549dwarf2_read_gdb_index
3550 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3551 get_gdb_index_contents_ftype get_gdb_index_contents,
3552 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3553{
2ec9a5e0
TT
3554 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3555 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3556 struct dwz_file *dwz;
12359b5e 3557 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3558
4485a1c1
SM
3559 gdb::array_view<const gdb_byte> main_index_contents
3560 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3561
3562 if (main_index_contents.empty ())
3563 return 0;
3564
3063847f 3565 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3566 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3567 use_deprecated_index_sections,
3568 main_index_contents, map.get (), &cu_list,
3569 &cu_list_elements, &types_list,
3570 &types_list_elements))
2ec9a5e0
TT
3571 return 0;
3572
0fefef59 3573 /* Don't use the index if it's empty. */
3063847f 3574 if (map->symbol_table.empty ())
0fefef59
DE
3575 return 0;
3576
2ec9a5e0
TT
3577 /* If there is a .dwz file, read it so we can get its CU list as
3578 well. */
ed2dc618 3579 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3580 if (dwz != NULL)
2ec9a5e0 3581 {
2ec9a5e0
TT
3582 struct mapped_index dwz_map;
3583 const gdb_byte *dwz_types_ignore;
3584 offset_type dwz_types_elements_ignore;
3585
4485a1c1
SM
3586 gdb::array_view<const gdb_byte> dwz_index_content
3587 = get_gdb_index_contents_dwz (objfile, dwz);
3588
3589 if (dwz_index_content.empty ())
3590 return 0;
3591
3592 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3593 bfd_get_filename (dwz->dwz_bfd.get ()),
3594 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3595 &dwz_list, &dwz_list_elements,
3596 &dwz_types_ignore,
3597 &dwz_types_elements_ignore))
2ec9a5e0
TT
3598 {
3599 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3600 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3601 return 0;
3602 }
3603 }
3604
12359b5e
SM
3605 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3606 dwz_list, dwz_list_elements);
1fd400ff 3607
8b70b953
TT
3608 if (types_list_elements)
3609 {
8b70b953
TT
3610 /* We can only handle a single .debug_types when we have an
3611 index. */
fd5866f6 3612 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3613 return 0;
3614
fd5866f6 3615 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3616
12359b5e
SM
3617 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3618 types_list, types_list_elements);
8b70b953 3619 }
9291a0cd 3620
3063847f 3621 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3622
3063847f 3623 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3624 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3625 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3626 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3627
3628 return 1;
3629}
3630
dee91e82 3631/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3632
dee91e82
DE
3633static void
3634dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3635 const gdb_byte *info_ptr,
dee91e82
DE
3636 struct die_info *comp_unit_die,
3637 int has_children,
3638 void *data)
9291a0cd 3639{
dee91e82 3640 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3641 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3642 struct dwarf2_per_objfile *dwarf2_per_objfile
3643 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3644 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3645 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3646 struct attribute *attr;
dee91e82 3647 int i;
7b9f3c50
DE
3648 void **slot;
3649 struct quick_file_names *qfn;
9291a0cd 3650
0186c6a7
DE
3651 gdb_assert (! this_cu->is_debug_types);
3652
07261596
TT
3653 /* Our callers never want to match partial units -- instead they
3654 will match the enclosing full CU. */
3655 if (comp_unit_die->tag == DW_TAG_partial_unit)
3656 {
3657 this_cu->v.quick->no_file_data = 1;
3658 return;
3659 }
3660
0186c6a7 3661 lh_cu = this_cu;
7b9f3c50 3662 slot = NULL;
dee91e82 3663
fff8551c 3664 line_header_up lh;
9c541725 3665 sect_offset line_offset {};
fff8551c 3666
dee91e82 3667 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3668 if (attr)
3669 {
7b9f3c50
DE
3670 struct quick_file_names find_entry;
3671
9c541725 3672 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3673
3674 /* We may have already read in this line header (TU line header sharing).
3675 If we have we're done. */
094b34ac 3676 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3677 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3678 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3679 &find_entry, INSERT);
3680 if (*slot != NULL)
3681 {
9a3c8263 3682 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3683 return;
7b9f3c50
DE
3684 }
3685
3019eac3 3686 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3687 }
3688 if (lh == NULL)
3689 {
094b34ac 3690 lh_cu->v.quick->no_file_data = 1;
dee91e82 3691 return;
9291a0cd
TT
3692 }
3693
8d749320 3694 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3695 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3696 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3697 gdb_assert (slot != NULL);
3698 *slot = qfn;
9291a0cd 3699
d721ba37 3700 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3701
aa391654
TT
3702 int offset = 0;
3703 if (strcmp (fnd.name, "<unknown>") != 0)
3704 ++offset;
3705
3706 qfn->num_file_names = offset + lh->file_names.size ();
8d749320 3707 qfn->file_names =
aa391654
TT
3708 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3709 if (offset != 0)
3710 qfn->file_names[0] = xstrdup (fnd.name);
fff8551c 3711 for (i = 0; i < lh->file_names.size (); ++i)
aa391654 3712 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3713 qfn->real_names = NULL;
9291a0cd 3714
094b34ac 3715 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3716}
3717
3718/* A helper for the "quick" functions which attempts to read the line
3719 table for THIS_CU. */
3720
3721static struct quick_file_names *
e4a48d9d 3722dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3723{
0186c6a7
DE
3724 /* This should never be called for TUs. */
3725 gdb_assert (! this_cu->is_debug_types);
3726 /* Nor type unit groups. */
3727 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3728
dee91e82
DE
3729 if (this_cu->v.quick->file_names != NULL)
3730 return this_cu->v.quick->file_names;
3731 /* If we know there is no line data, no point in looking again. */
3732 if (this_cu->v.quick->no_file_data)
3733 return NULL;
3734
0186c6a7 3735 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3736
3737 if (this_cu->v.quick->no_file_data)
3738 return NULL;
3739 return this_cu->v.quick->file_names;
9291a0cd
TT
3740}
3741
3742/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3743 real path for a given file name from the line table. */
2fdf6df6 3744
9291a0cd 3745static const char *
7b9f3c50
DE
3746dw2_get_real_path (struct objfile *objfile,
3747 struct quick_file_names *qfn, int index)
9291a0cd 3748{
7b9f3c50
DE
3749 if (qfn->real_names == NULL)
3750 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3751 qfn->num_file_names, const char *);
9291a0cd 3752
7b9f3c50 3753 if (qfn->real_names[index] == NULL)
14278e1f 3754 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3755
7b9f3c50 3756 return qfn->real_names[index];
9291a0cd
TT
3757}
3758
3759static struct symtab *
3760dw2_find_last_source_symtab (struct objfile *objfile)
3761{
ed2dc618
SM
3762 struct dwarf2_per_objfile *dwarf2_per_objfile
3763 = get_dwarf2_per_objfile (objfile);
b76e467d 3764 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3765 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3766
43f3e411
DE
3767 if (cust == NULL)
3768 return NULL;
ed2dc618 3769
43f3e411 3770 return compunit_primary_filetab (cust);
9291a0cd
TT
3771}
3772
7b9f3c50
DE
3773/* Traversal function for dw2_forget_cached_source_info. */
3774
3775static int
3776dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3777{
7b9f3c50 3778 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3779
7b9f3c50 3780 if (file_data->real_names)
9291a0cd 3781 {
7b9f3c50 3782 int i;
9291a0cd 3783
7b9f3c50 3784 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3785 {
7b9f3c50
DE
3786 xfree ((void*) file_data->real_names[i]);
3787 file_data->real_names[i] = NULL;
9291a0cd
TT
3788 }
3789 }
7b9f3c50
DE
3790
3791 return 1;
3792}
3793
3794static void
3795dw2_forget_cached_source_info (struct objfile *objfile)
3796{
ed2dc618
SM
3797 struct dwarf2_per_objfile *dwarf2_per_objfile
3798 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3799
3800 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3801 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3802}
3803
f8eba3c6
TT
3804/* Helper function for dw2_map_symtabs_matching_filename that expands
3805 the symtabs and calls the iterator. */
3806
3807static int
3808dw2_map_expand_apply (struct objfile *objfile,
3809 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3810 const char *name, const char *real_path,
14bc53a8 3811 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3812{
43f3e411 3813 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3814
3815 /* Don't visit already-expanded CUs. */
43f3e411 3816 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3817 return 0;
3818
3819 /* This may expand more than one symtab, and we want to iterate over
3820 all of them. */
58f0c718 3821 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3822
14bc53a8
PA
3823 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3824 last_made, callback);
f8eba3c6
TT
3825}
3826
3827/* Implementation of the map_symtabs_matching_filename method. */
3828
14bc53a8
PA
3829static bool
3830dw2_map_symtabs_matching_filename
3831 (struct objfile *objfile, const char *name, const char *real_path,
3832 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3833{
c011a4f4 3834 const char *name_basename = lbasename (name);
ed2dc618
SM
3835 struct dwarf2_per_objfile *dwarf2_per_objfile
3836 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3837
848e3e78
DE
3838 /* The rule is CUs specify all the files, including those used by
3839 any TU, so there's no need to scan TUs here. */
f4dc4d17 3840
b76e467d 3841 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3842 {
3d7bb9d9 3843 /* We only need to look at symtabs not already expanded. */
43f3e411 3844 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3845 continue;
3846
b76e467d 3847 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3848 if (file_data == NULL)
9291a0cd
TT
3849 continue;
3850
b76e467d 3851 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3852 {
7b9f3c50 3853 const char *this_name = file_data->file_names[j];
da235a7c 3854 const char *this_real_name;
9291a0cd 3855
af529f8f 3856 if (compare_filenames_for_search (this_name, name))
9291a0cd 3857 {
f5b95b50 3858 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3859 callback))
3860 return true;
288e77a7 3861 continue;
4aac40c8 3862 }
9291a0cd 3863
c011a4f4
DE
3864 /* Before we invoke realpath, which can get expensive when many
3865 files are involved, do a quick comparison of the basenames. */
3866 if (! basenames_may_differ
3867 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3868 continue;
3869
da235a7c
JK
3870 this_real_name = dw2_get_real_path (objfile, file_data, j);
3871 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3872 {
da235a7c 3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3874 callback))
3875 return true;
288e77a7 3876 continue;
da235a7c 3877 }
9291a0cd 3878
da235a7c
JK
3879 if (real_path != NULL)
3880 {
af529f8f
JK
3881 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3882 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3883 if (this_real_name != NULL
af529f8f 3884 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3885 {
f5b95b50 3886 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3887 callback))
3888 return true;
288e77a7 3889 continue;
9291a0cd
TT
3890 }
3891 }
3892 }
3893 }
3894
14bc53a8 3895 return false;
9291a0cd
TT
3896}
3897
da51c347
DE
3898/* Struct used to manage iterating over all CUs looking for a symbol. */
3899
3900struct dw2_symtab_iterator
9291a0cd 3901{
ed2dc618
SM
3902 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3903 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3904 /* If set, only look for symbols that match that block. Valid values are
3905 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3906 gdb::optional<block_enum> block_index;
da51c347
DE
3907 /* The kind of symbol we're looking for. */
3908 domain_enum domain;
3909 /* The list of CUs from the index entry of the symbol,
3910 or NULL if not found. */
3911 offset_type *vec;
3912 /* The next element in VEC to look at. */
3913 int next;
3914 /* The number of elements in VEC, or zero if there is no match. */
3915 int length;
8943b874
DE
3916 /* Have we seen a global version of the symbol?
3917 If so we can ignore all further global instances.
3918 This is to work around gold/15646, inefficient gold-generated
3919 indices. */
3920 int global_seen;
da51c347 3921};
9291a0cd 3922
2b79f376 3923/* Initialize the index symtab iterator ITER. */
2fdf6df6 3924
9291a0cd 3925static void
da51c347 3926dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3927 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3928 gdb::optional<block_enum> block_index,
da51c347
DE
3929 domain_enum domain,
3930 const char *name)
3931{
ed2dc618 3932 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3933 iter->block_index = block_index;
3934 iter->domain = domain;
3935 iter->next = 0;
8943b874 3936 iter->global_seen = 0;
da51c347 3937
3063847f 3938 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3939
3940 /* index is NULL if OBJF_READNOW. */
3941 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3942 iter->length = MAYBE_SWAP (*iter->vec);
3943 else
3944 {
3945 iter->vec = NULL;
3946 iter->length = 0;
3947 }
3948}
3949
3950/* Return the next matching CU or NULL if there are no more. */
3951
3952static struct dwarf2_per_cu_data *
3953dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3954{
ed2dc618
SM
3955 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3956
da51c347
DE
3957 for ( ; iter->next < iter->length; ++iter->next)
3958 {
3959 offset_type cu_index_and_attrs =
3960 MAYBE_SWAP (iter->vec[iter->next + 1]);
3961 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
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 {
2b79f376
SM
3991 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3992
3993 if (iter->block_index.has_value ())
3994 {
3995 bool want_static = *iter->block_index == STATIC_BLOCK;
3996
3997 if (is_static != want_static)
3998 continue;
3999 }
4000
8943b874
DE
4001 /* Work around gold/15646. */
4002 if (!is_static && iter->global_seen)
4003 continue;
4004 if (!is_static)
4005 iter->global_seen = 1;
4006 }
da51c347
DE
4007
4008 /* Only check the symbol's kind if it has one. */
4009 if (attrs_valid)
4010 {
4011 switch (iter->domain)
4012 {
4013 case VAR_DOMAIN:
4014 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4015 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4016 /* Some types are also in VAR_DOMAIN. */
4017 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4018 continue;
4019 break;
4020 case STRUCT_DOMAIN:
4021 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4022 continue;
4023 break;
4024 case LABEL_DOMAIN:
4025 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4026 continue;
4027 break;
4028 default:
4029 break;
4030 }
4031 }
4032
4033 ++iter->next;
4034 return per_cu;
4035 }
4036
4037 return NULL;
4038}
4039
43f3e411 4040static struct compunit_symtab *
c7f839cb 4041dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4042 const char *name, domain_enum domain)
9291a0cd 4043{
43f3e411 4044 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4045 struct dwarf2_per_objfile *dwarf2_per_objfile
4046 = get_dwarf2_per_objfile (objfile);
9291a0cd 4047
b5ec771e
PA
4048 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4049
ed2dc618
SM
4050 struct dw2_symtab_iterator iter;
4051 struct dwarf2_per_cu_data *per_cu;
da51c347 4052
2b79f376 4053 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4054
ed2dc618
SM
4055 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4056 {
4057 struct symbol *sym, *with_opaque = NULL;
58f0c718 4058 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4059 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4060 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4061
ed2dc618
SM
4062 sym = block_find_symbol (block, name, domain,
4063 block_find_non_opaque_type_preferred,
4064 &with_opaque);
b2e2f908 4065
ed2dc618
SM
4066 /* Some caution must be observed with overloaded functions
4067 and methods, since the index will not contain any overload
4068 information (but NAME might contain it). */
da51c347 4069
ed2dc618
SM
4070 if (sym != NULL
4071 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4072 return stab;
4073 if (with_opaque != NULL
4074 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4075 stab_best = stab;
da51c347 4076
ed2dc618 4077 /* Keep looking through other CUs. */
9291a0cd 4078 }
9291a0cd 4079
da51c347 4080 return stab_best;
9291a0cd
TT
4081}
4082
4083static void
4084dw2_print_stats (struct objfile *objfile)
4085{
ed2dc618
SM
4086 struct dwarf2_per_objfile *dwarf2_per_objfile
4087 = get_dwarf2_per_objfile (objfile);
b76e467d 4088 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4089 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4090 int count = 0;
9291a0cd 4091
ed2dc618 4092 for (int i = 0; i < total; ++i)
9291a0cd 4093 {
ff4c9fec 4094 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4095
43f3e411 4096 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4097 ++count;
4098 }
e4a48d9d 4099 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4100 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4101}
4102
779bd270
DE
4103/* This dumps minimal information about the index.
4104 It is called via "mt print objfiles".
4105 One use is to verify .gdb_index has been loaded by the
4106 gdb.dwarf2/gdb-index.exp testcase. */
4107
9291a0cd
TT
4108static void
4109dw2_dump (struct objfile *objfile)
4110{
ed2dc618
SM
4111 struct dwarf2_per_objfile *dwarf2_per_objfile
4112 = get_dwarf2_per_objfile (objfile);
4113
779bd270
DE
4114 gdb_assert (dwarf2_per_objfile->using_index);
4115 printf_filtered (".gdb_index:");
4116 if (dwarf2_per_objfile->index_table != NULL)
4117 {
4118 printf_filtered (" version %d\n",
4119 dwarf2_per_objfile->index_table->version);
4120 }
4121 else
4122 printf_filtered (" faked for \"readnow\"\n");
4123 printf_filtered ("\n");
9291a0cd
TT
4124}
4125
9291a0cd
TT
4126static void
4127dw2_expand_symtabs_for_function (struct objfile *objfile,
4128 const char *func_name)
4129{
ed2dc618
SM
4130 struct dwarf2_per_objfile *dwarf2_per_objfile
4131 = get_dwarf2_per_objfile (objfile);
da51c347 4132
ed2dc618
SM
4133 struct dw2_symtab_iterator iter;
4134 struct dwarf2_per_cu_data *per_cu;
da51c347 4135
2b79f376 4136 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4137
ed2dc618 4138 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4139 dw2_instantiate_symtab (per_cu, false);
da51c347 4140
9291a0cd
TT
4141}
4142
4143static void
4144dw2_expand_all_symtabs (struct objfile *objfile)
4145{
ed2dc618
SM
4146 struct dwarf2_per_objfile *dwarf2_per_objfile
4147 = get_dwarf2_per_objfile (objfile);
b76e467d 4148 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4149 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4150
ed2dc618 4151 for (int i = 0; i < total_units; ++i)
9291a0cd 4152 {
ff4c9fec 4153 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4154
58f0c718
TT
4155 /* We don't want to directly expand a partial CU, because if we
4156 read it with the wrong language, then assertion failures can
4157 be triggered later on. See PR symtab/23010. So, tell
4158 dw2_instantiate_symtab to skip partial CUs -- any important
4159 partial CU will be read via DW_TAG_imported_unit anyway. */
4160 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4161 }
4162}
4163
4164static void
652a8996
JK
4165dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4166 const char *fullname)
9291a0cd 4167{
ed2dc618
SM
4168 struct dwarf2_per_objfile *dwarf2_per_objfile
4169 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4170
4171 /* We don't need to consider type units here.
4172 This is only called for examining code, e.g. expand_line_sal.
4173 There can be an order of magnitude (or more) more type units
4174 than comp units, and we avoid them if we can. */
4175
b76e467d 4176 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4177 {
3d7bb9d9 4178 /* We only need to look at symtabs not already expanded. */
43f3e411 4179 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4180 continue;
4181
b76e467d 4182 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4183 if (file_data == NULL)
9291a0cd
TT
4184 continue;
4185
b76e467d 4186 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4187 {
652a8996
JK
4188 const char *this_fullname = file_data->file_names[j];
4189
4190 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4191 {
58f0c718 4192 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4193 break;
4194 }
4195 }
4196 }
4197}
4198
9291a0cd 4199static void
199b4314
TT
4200dw2_map_matching_symbols
4201 (struct objfile *objfile,
b054970d 4202 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4203 int global,
4204 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4205 symbol_compare_ftype *ordered_compare)
9291a0cd 4206{
40658b94 4207 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4208 current language is Ada for a non-Ada objfile using GNU index. As Ada
4209 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4210}
4211
e1ef7d7a
PA
4212/* Starting from a search name, return the string that finds the upper
4213 bound of all strings that start with SEARCH_NAME in a sorted name
4214 list. Returns the empty string to indicate that the upper bound is
4215 the end of the list. */
4216
4217static std::string
4218make_sort_after_prefix_name (const char *search_name)
4219{
4220 /* When looking to complete "func", we find the upper bound of all
4221 symbols that start with "func" by looking for where we'd insert
4222 the closest string that would follow "func" in lexicographical
4223 order. Usually, that's "func"-with-last-character-incremented,
4224 i.e. "fund". Mind non-ASCII characters, though. Usually those
4225 will be UTF-8 multi-byte sequences, but we can't be certain.
4226 Especially mind the 0xff character, which is a valid character in
4227 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4228 rule out compilers allowing it in identifiers. Note that
4229 conveniently, strcmp/strcasecmp are specified to compare
4230 characters interpreted as unsigned char. So what we do is treat
4231 the whole string as a base 256 number composed of a sequence of
4232 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4233 to 0, and carries 1 to the following more-significant position.
4234 If the very first character in SEARCH_NAME ends up incremented
4235 and carries/overflows, then the upper bound is the end of the
4236 list. The string after the empty string is also the empty
4237 string.
4238
4239 Some examples of this operation:
4240
4241 SEARCH_NAME => "+1" RESULT
4242
4243 "abc" => "abd"
4244 "ab\xff" => "ac"
4245 "\xff" "a" "\xff" => "\xff" "b"
4246 "\xff" => ""
4247 "\xff\xff" => ""
4248 "" => ""
4249
4250 Then, with these symbols for example:
4251
4252 func
4253 func1
4254 fund
4255
4256 completing "func" looks for symbols between "func" and
4257 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4258 which finds "func" and "func1", but not "fund".
4259
4260 And with:
4261
4262 funcÿ (Latin1 'ÿ' [0xff])
4263 funcÿ1
4264 fund
4265
4266 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4267 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4268
4269 And with:
4270
4271 ÿÿ (Latin1 'ÿ' [0xff])
4272 ÿÿ1
4273
4274 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4275 the end of the list.
4276 */
4277 std::string after = search_name;
4278 while (!after.empty () && (unsigned char) after.back () == 0xff)
4279 after.pop_back ();
4280 if (!after.empty ())
4281 after.back () = (unsigned char) after.back () + 1;
4282 return after;
4283}
4284
5c58de74 4285/* See declaration. */
61d96d7e 4286
5c58de74
PA
4287std::pair<std::vector<name_component>::const_iterator,
4288 std::vector<name_component>::const_iterator>
44ed8f3e 4289mapped_index_base::find_name_components_bounds
3b00ef10 4290 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4291{
5c58de74
PA
4292 auto *name_cmp
4293 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4294
3b00ef10
TT
4295 const char *lang_name
4296 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4297
3f563c84
PA
4298 /* Comparison function object for lower_bound that matches against a
4299 given symbol name. */
4300 auto lookup_compare_lower = [&] (const name_component &elem,
4301 const char *name)
4302 {
5c58de74 4303 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4304 const char *elem_name = elem_qualified + elem.name_offset;
4305 return name_cmp (elem_name, name) < 0;
4306 };
4307
4308 /* Comparison function object for upper_bound that matches against a
4309 given symbol name. */
4310 auto lookup_compare_upper = [&] (const char *name,
4311 const name_component &elem)
4312 {
5c58de74 4313 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4314 const char *elem_name = elem_qualified + elem.name_offset;
4315 return name_cmp (name, elem_name) < 0;
4316 };
4317
5c58de74
PA
4318 auto begin = this->name_components.begin ();
4319 auto end = this->name_components.end ();
3f563c84
PA
4320
4321 /* Find the lower bound. */
4322 auto lower = [&] ()
4323 {
3b00ef10 4324 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4325 return begin;
4326 else
3b00ef10 4327 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4328 } ();
4329
4330 /* Find the upper bound. */
4331 auto upper = [&] ()
4332 {
5c58de74 4333 if (lookup_name_without_params.completion_mode ())
3f563c84 4334 {
e1ef7d7a
PA
4335 /* In completion mode, we want UPPER to point past all
4336 symbols names that have the same prefix. I.e., with
4337 these symbols, and completing "func":
4338
4339 function << lower bound
4340 function1
4341 other_function << upper bound
4342
4343 We find the upper bound by looking for the insertion
4344 point of "func"-with-last-character-incremented,
4345 i.e. "fund". */
3b00ef10 4346 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4347 if (after.empty ())
3f563c84 4348 return end;
e6b2f5ef
PA
4349 return std::lower_bound (lower, end, after.c_str (),
4350 lookup_compare_lower);
3f563c84
PA
4351 }
4352 else
3b00ef10 4353 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4354 } ();
4355
5c58de74
PA
4356 return {lower, upper};
4357}
4358
4359/* See declaration. */
4360
4361void
44ed8f3e 4362mapped_index_base::build_name_components ()
5c58de74
PA
4363{
4364 if (!this->name_components.empty ())
4365 return;
4366
4367 this->name_components_casing = case_sensitivity;
4368 auto *name_cmp
4369 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4370
4371 /* The code below only knows how to break apart components of C++
4372 symbol names (and other languages that use '::' as
3b00ef10 4373 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4374 auto count = this->symbol_name_count ();
4375 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4376 {
44ed8f3e 4377 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4378 continue;
4379
4380 const char *name = this->symbol_name_at (idx);
4381
4382 /* Add each name component to the name component table. */
4383 unsigned int previous_len = 0;
3b00ef10
TT
4384
4385 if (strstr (name, "::") != nullptr)
4386 {
4387 for (unsigned int current_len = cp_find_first_component (name);
4388 name[current_len] != '\0';
4389 current_len += cp_find_first_component (name + current_len))
4390 {
4391 gdb_assert (name[current_len] == ':');
4392 this->name_components.push_back ({previous_len, idx});
4393 /* Skip the '::'. */
4394 current_len += 2;
4395 previous_len = current_len;
4396 }
4397 }
4398 else
5c58de74 4399 {
3b00ef10
TT
4400 /* Handle the Ada encoded (aka mangled) form here. */
4401 for (const char *iter = strstr (name, "__");
4402 iter != nullptr;
4403 iter = strstr (iter, "__"))
4404 {
4405 this->name_components.push_back ({previous_len, idx});
4406 iter += 2;
4407 previous_len = iter - name;
4408 }
5c58de74 4409 }
3b00ef10 4410
5c58de74
PA
4411 this->name_components.push_back ({previous_len, idx});
4412 }
4413
4414 /* Sort name_components elements by name. */
4415 auto name_comp_compare = [&] (const name_component &left,
4416 const name_component &right)
4417 {
4418 const char *left_qualified = this->symbol_name_at (left.idx);
4419 const char *right_qualified = this->symbol_name_at (right.idx);
4420
4421 const char *left_name = left_qualified + left.name_offset;
4422 const char *right_name = right_qualified + right.name_offset;
4423
4424 return name_cmp (left_name, right_name) < 0;
4425 };
4426
4427 std::sort (this->name_components.begin (),
4428 this->name_components.end (),
4429 name_comp_compare);
4430}
4431
4432/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4433 mapped_index_base instead of the containing objfile. This is split
4434 to a separate function in order to be able to unit test the
4435 name_components matching using a mock mapped_index_base. For each
5c58de74 4436 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4437 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4438
4439static void
4440dw2_expand_symtabs_matching_symbol
44ed8f3e 4441 (mapped_index_base &index,
5c58de74
PA
4442 const lookup_name_info &lookup_name_in,
4443 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4444 enum search_domain kind,
3b00ef10 4445 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4446{
4447 lookup_name_info lookup_name_without_params
4448 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4449
4450 /* Build the symbol name component sorted vector, if we haven't
4451 yet. */
4452 index.build_name_components ();
4453
3f563c84
PA
4454 /* The same symbol may appear more than once in the range though.
4455 E.g., if we're looking for symbols that complete "w", and we have
4456 a symbol named "w1::w2", we'll find the two name components for
4457 that same symbol in the range. To be sure we only call the
4458 callback once per symbol, we first collect the symbol name
4459 indexes that matched in a temporary vector and ignore
4460 duplicates. */
4461 std::vector<offset_type> matches;
3f563c84 4462
3b00ef10
TT
4463 struct name_and_matcher
4464 {
4465 symbol_name_matcher_ftype *matcher;
4466 const std::string &name;
4467
4468 bool operator== (const name_and_matcher &other) const
3f563c84 4469 {
3b00ef10
TT
4470 return matcher == other.matcher && name == other.name;
4471 }
4472 };
4473
4474 /* A vector holding all the different symbol name matchers, for all
4475 languages. */
4476 std::vector<name_and_matcher> matchers;
4477
4478 for (int i = 0; i < nr_languages; i++)
4479 {
4480 enum language lang_e = (enum language) i;
4481
4482 const language_defn *lang = language_def (lang_e);
4483 symbol_name_matcher_ftype *name_matcher
4484 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4485
3b00ef10
TT
4486 name_and_matcher key {
4487 name_matcher,
4488 lookup_name_without_params.language_lookup_name (lang_e)
4489 };
4490
4491 /* Don't insert the same comparison routine more than once.
4492 Note that we do this linear walk. This is not a problem in
4493 practice because the number of supported languages is
4494 low. */
4495 if (std::find (matchers.begin (), matchers.end (), key)
4496 != matchers.end ())
9291a0cd 4497 continue;
3b00ef10
TT
4498 matchers.push_back (std::move (key));
4499
4500 auto bounds
4501 = index.find_name_components_bounds (lookup_name_without_params,
4502 lang_e);
4503
4504 /* Now for each symbol name in range, check to see if we have a name
4505 match, and if so, call the MATCH_CALLBACK callback. */
4506
4507 for (; bounds.first != bounds.second; ++bounds.first)
4508 {
4509 const char *qualified = index.symbol_name_at (bounds.first->idx);
4510
4511 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4512 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4513 continue;
9291a0cd 4514
3b00ef10
TT
4515 matches.push_back (bounds.first->idx);
4516 }
3f563c84
PA
4517 }
4518
4519 std::sort (matches.begin (), matches.end ());
4520
4521 /* Finally call the callback, once per match. */
4522 ULONGEST prev = -1;
4523 for (offset_type idx : matches)
4524 {
4525 if (prev != idx)
4526 {
3b00ef10
TT
4527 if (!match_callback (idx))
4528 break;
3f563c84
PA
4529 prev = idx;
4530 }
4531 }
4532
4533 /* Above we use a type wider than idx's for 'prev', since 0 and
4534 (offset_type)-1 are both possible values. */
4535 static_assert (sizeof (prev) > sizeof (offset_type), "");
4536}
4537
c62446b1
PA
4538#if GDB_SELF_TEST
4539
4540namespace selftests { namespace dw2_expand_symtabs_matching {
4541
a3c5fafd
PA
4542/* A mock .gdb_index/.debug_names-like name index table, enough to
4543 exercise dw2_expand_symtabs_matching_symbol, which works with the
4544 mapped_index_base interface. Builds an index from the symbol list
4545 passed as parameter to the constructor. */
4546class mock_mapped_index : public mapped_index_base
c62446b1
PA
4547{
4548public:
a3c5fafd
PA
4549 mock_mapped_index (gdb::array_view<const char *> symbols)
4550 : m_symbol_table (symbols)
c62446b1
PA
4551 {}
4552
a3c5fafd 4553 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4554
a3c5fafd 4555 /* Return the number of names in the symbol table. */
632e107b 4556 size_t symbol_name_count () const override
c62446b1 4557 {
a3c5fafd 4558 return m_symbol_table.size ();
c62446b1
PA
4559 }
4560
a3c5fafd 4561 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4562 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4563 {
4564 return m_symbol_table[idx];
4565 }
c62446b1 4566
a3c5fafd
PA
4567private:
4568 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4569};
4570
4571/* Convenience function that converts a NULL pointer to a "<null>"
4572 string, to pass to print routines. */
4573
4574static const char *
4575string_or_null (const char *str)
4576{
4577 return str != NULL ? str : "<null>";
4578}
4579
4580/* Check if a lookup_name_info built from
4581 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4582 index. EXPECTED_LIST is the list of expected matches, in expected
4583 matching order. If no match expected, then an empty list is
4584 specified. Returns true on success. On failure prints a warning
4585 indicating the file:line that failed, and returns false. */
4586
4587static bool
4588check_match (const char *file, int line,
4589 mock_mapped_index &mock_index,
4590 const char *name, symbol_name_match_type match_type,
4591 bool completion_mode,
4592 std::initializer_list<const char *> expected_list)
4593{
4594 lookup_name_info lookup_name (name, match_type, completion_mode);
4595
4596 bool matched = true;
4597
4598 auto mismatch = [&] (const char *expected_str,
4599 const char *got)
4600 {
4601 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4602 "expected=\"%s\", got=\"%s\"\n"),
4603 file, line,
4604 (match_type == symbol_name_match_type::FULL
4605 ? "FULL" : "WILD"),
4606 name, string_or_null (expected_str), string_or_null (got));
4607 matched = false;
4608 };
4609
4610 auto expected_it = expected_list.begin ();
4611 auto expected_end = expected_list.end ();
4612
a3c5fafd 4613 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4614 NULL, ALL_DOMAIN,
4615 [&] (offset_type idx)
4616 {
a3c5fafd 4617 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4618 const char *expected_str
4619 = expected_it == expected_end ? NULL : *expected_it++;
4620
4621 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4622 mismatch (expected_str, matched_name);
3b00ef10 4623 return true;
c62446b1
PA
4624 });
4625
4626 const char *expected_str
4627 = expected_it == expected_end ? NULL : *expected_it++;
4628 if (expected_str != NULL)
4629 mismatch (expected_str, NULL);
4630
4631 return matched;
4632}
4633
4634/* The symbols added to the mock mapped_index for testing (in
4635 canonical form). */
4636static const char *test_symbols[] = {
4637 "function",
4638 "std::bar",
4639 "std::zfunction",
4640 "std::zfunction2",
4641 "w1::w2",
4642 "ns::foo<char*>",
4643 "ns::foo<int>",
4644 "ns::foo<long>",
a20714ff
PA
4645 "ns2::tmpl<int>::foo2",
4646 "(anonymous namespace)::A::B::C",
c62446b1 4647
e1ef7d7a
PA
4648 /* These are used to check that the increment-last-char in the
4649 matching algorithm for completion doesn't match "t1_fund" when
4650 completing "t1_func". */
4651 "t1_func",
4652 "t1_func1",
4653 "t1_fund",
4654 "t1_fund1",
4655
4656 /* A UTF-8 name with multi-byte sequences to make sure that
4657 cp-name-parser understands this as a single identifier ("função"
4658 is "function" in PT). */
4659 u8"u8função",
4660
4661 /* \377 (0xff) is Latin1 'ÿ'. */
4662 "yfunc\377",
4663
4664 /* \377 (0xff) is Latin1 'ÿ'. */
4665 "\377",
4666 "\377\377123",
4667
c62446b1
PA
4668 /* A name with all sorts of complications. Starts with "z" to make
4669 it easier for the completion tests below. */
4670#define Z_SYM_NAME \
4671 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4672 "::tuple<(anonymous namespace)::ui*, " \
4673 "std::default_delete<(anonymous namespace)::ui>, void>"
4674
4675 Z_SYM_NAME
4676};
4677
a3c5fafd
PA
4678/* Returns true if the mapped_index_base::find_name_component_bounds
4679 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4680 in completion mode. */
5c58de74
PA
4681
4682static bool
a3c5fafd 4683check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4684 const char *search_name,
4685 gdb::array_view<const char *> expected_syms)
4686{
4687 lookup_name_info lookup_name (search_name,
4688 symbol_name_match_type::FULL, true);
4689
3b00ef10
TT
4690 auto bounds = index.find_name_components_bounds (lookup_name,
4691 language_cplus);
5c58de74
PA
4692
4693 size_t distance = std::distance (bounds.first, bounds.second);
4694 if (distance != expected_syms.size ())
4695 return false;
4696
4697 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4698 {
4699 auto nc_elem = bounds.first + exp_elem;
4700 const char *qualified = index.symbol_name_at (nc_elem->idx);
4701 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4702 return false;
4703 }
4704
4705 return true;
4706}
4707
4708/* Test the lower-level mapped_index::find_name_component_bounds
4709 method. */
4710
c62446b1 4711static void
5c58de74
PA
4712test_mapped_index_find_name_component_bounds ()
4713{
4714 mock_mapped_index mock_index (test_symbols);
4715
a3c5fafd 4716 mock_index.build_name_components ();
5c58de74
PA
4717
4718 /* Test the lower-level mapped_index::find_name_component_bounds
4719 method in completion mode. */
4720 {
4721 static const char *expected_syms[] = {
4722 "t1_func",
4723 "t1_func1",
5c58de74
PA
4724 };
4725
a3c5fafd 4726 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4727 "t1_func", expected_syms));
4728 }
4729
4730 /* Check that the increment-last-char in the name matching algorithm
4731 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4732 {
4733 static const char *expected_syms1[] = {
4734 "\377",
4735 "\377\377123",
4736 };
a3c5fafd 4737 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4738 "\377", expected_syms1));
4739
4740 static const char *expected_syms2[] = {
4741 "\377\377123",
4742 };
a3c5fafd 4743 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4744 "\377\377", expected_syms2));
4745 }
4746}
4747
4748/* Test dw2_expand_symtabs_matching_symbol. */
4749
4750static void
4751test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4752{
4753 mock_mapped_index mock_index (test_symbols);
4754
4755 /* We let all tests run until the end even if some fails, for debug
4756 convenience. */
4757 bool any_mismatch = false;
4758
4759 /* Create the expected symbols list (an initializer_list). Needed
4760 because lists have commas, and we need to pass them to CHECK,
4761 which is a macro. */
4762#define EXPECT(...) { __VA_ARGS__ }
4763
4764 /* Wrapper for check_match that passes down the current
4765 __FILE__/__LINE__. */
4766#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4767 any_mismatch |= !check_match (__FILE__, __LINE__, \
4768 mock_index, \
4769 NAME, MATCH_TYPE, COMPLETION_MODE, \
4770 EXPECTED_LIST)
4771
4772 /* Identity checks. */
4773 for (const char *sym : test_symbols)
4774 {
4775 /* Should be able to match all existing symbols. */
4776 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4777 EXPECT (sym));
4778
4779 /* Should be able to match all existing symbols with
4780 parameters. */
4781 std::string with_params = std::string (sym) + "(int)";
4782 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4783 EXPECT (sym));
4784
4785 /* Should be able to match all existing symbols with
4786 parameters and qualifiers. */
4787 with_params = std::string (sym) + " ( int ) const";
4788 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4789 EXPECT (sym));
4790
4791 /* This should really find sym, but cp-name-parser.y doesn't
4792 know about lvalue/rvalue qualifiers yet. */
4793 with_params = std::string (sym) + " ( int ) &&";
4794 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4795 {});
4796 }
4797
e1ef7d7a
PA
4798 /* Check that the name matching algorithm for completion doesn't get
4799 confused with Latin1 'ÿ' / 0xff. */
4800 {
4801 static const char str[] = "\377";
4802 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4803 EXPECT ("\377", "\377\377123"));
4804 }
4805
4806 /* Check that the increment-last-char in the matching algorithm for
4807 completion doesn't match "t1_fund" when completing "t1_func". */
4808 {
4809 static const char str[] = "t1_func";
4810 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4811 EXPECT ("t1_func", "t1_func1"));
4812 }
4813
c62446b1
PA
4814 /* Check that completion mode works at each prefix of the expected
4815 symbol name. */
4816 {
4817 static const char str[] = "function(int)";
4818 size_t len = strlen (str);
4819 std::string lookup;
4820
4821 for (size_t i = 1; i < len; i++)
4822 {
4823 lookup.assign (str, i);
4824 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4825 EXPECT ("function"));
4826 }
4827 }
4828
4829 /* While "w" is a prefix of both components, the match function
4830 should still only be called once. */
4831 {
4832 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4833 EXPECT ("w1::w2"));
a20714ff
PA
4834 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4835 EXPECT ("w1::w2"));
c62446b1
PA
4836 }
4837
4838 /* Same, with a "complicated" symbol. */
4839 {
4840 static const char str[] = Z_SYM_NAME;
4841 size_t len = strlen (str);
4842 std::string lookup;
4843
4844 for (size_t i = 1; i < len; i++)
4845 {
4846 lookup.assign (str, i);
4847 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4848 EXPECT (Z_SYM_NAME));
4849 }
4850 }
4851
4852 /* In FULL mode, an incomplete symbol doesn't match. */
4853 {
4854 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4855 {});
4856 }
4857
4858 /* A complete symbol with parameters matches any overload, since the
4859 index has no overload info. */
4860 {
4861 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4862 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4863 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4864 EXPECT ("std::zfunction", "std::zfunction2"));
4865 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4866 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4867 }
4868
4869 /* Check that whitespace is ignored appropriately. A symbol with a
4870 template argument list. */
4871 {
4872 static const char expected[] = "ns::foo<int>";
4873 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4874 EXPECT (expected));
a20714ff
PA
4875 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4876 EXPECT (expected));
c62446b1
PA
4877 }
4878
4879 /* Check that whitespace is ignored appropriately. A symbol with a
4880 template argument list that includes a pointer. */
4881 {
4882 static const char expected[] = "ns::foo<char*>";
4883 /* Try both completion and non-completion modes. */
4884 static const bool completion_mode[2] = {false, true};
4885 for (size_t i = 0; i < 2; i++)
4886 {
4887 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4888 completion_mode[i], EXPECT (expected));
a20714ff
PA
4889 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4890 completion_mode[i], EXPECT (expected));
c62446b1
PA
4891
4892 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4893 completion_mode[i], EXPECT (expected));
a20714ff
PA
4894 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4895 completion_mode[i], EXPECT (expected));
c62446b1
PA
4896 }
4897 }
4898
4899 {
4900 /* Check method qualifiers are ignored. */
4901 static const char expected[] = "ns::foo<char*>";
4902 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4903 symbol_name_match_type::FULL, true, EXPECT (expected));
4904 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4905 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4906 CHECK_MATCH ("foo < char * > ( int ) const",
4907 symbol_name_match_type::WILD, true, EXPECT (expected));
4908 CHECK_MATCH ("foo < char * > ( int ) &&",
4909 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4910 }
4911
4912 /* Test lookup names that don't match anything. */
4913 {
a20714ff
PA
4914 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4915 {});
4916
c62446b1
PA
4917 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4918 {});
4919 }
4920
a20714ff
PA
4921 /* Some wild matching tests, exercising "(anonymous namespace)",
4922 which should not be confused with a parameter list. */
4923 {
4924 static const char *syms[] = {
4925 "A::B::C",
4926 "B::C",
4927 "C",
4928 "A :: B :: C ( int )",
4929 "B :: C ( int )",
4930 "C ( int )",
4931 };
4932
4933 for (const char *s : syms)
4934 {
4935 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4936 EXPECT ("(anonymous namespace)::A::B::C"));
4937 }
4938 }
4939
4940 {
4941 static const char expected[] = "ns2::tmpl<int>::foo2";
4942 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4943 EXPECT (expected));
4944 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4945 EXPECT (expected));
4946 }
4947
c62446b1
PA
4948 SELF_CHECK (!any_mismatch);
4949
4950#undef EXPECT
4951#undef CHECK_MATCH
4952}
4953
5c58de74
PA
4954static void
4955run_test ()
4956{
4957 test_mapped_index_find_name_component_bounds ();
4958 test_dw2_expand_symtabs_matching_symbol ();
4959}
4960
c62446b1
PA
4961}} // namespace selftests::dw2_expand_symtabs_matching
4962
4963#endif /* GDB_SELF_TEST */
4964
4b514bc8
JK
4965/* If FILE_MATCHER is NULL or if PER_CU has
4966 dwarf2_per_cu_quick_data::MARK set (see
4967 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4968 EXPANSION_NOTIFY on it. */
4969
4970static void
4971dw2_expand_symtabs_matching_one
4972 (struct dwarf2_per_cu_data *per_cu,
4973 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4974 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4975{
4976 if (file_matcher == NULL || per_cu->v.quick->mark)
4977 {
4978 bool symtab_was_null
4979 = (per_cu->v.quick->compunit_symtab == NULL);
4980
58f0c718 4981 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4982
4983 if (expansion_notify != NULL
4984 && symtab_was_null
4985 && per_cu->v.quick->compunit_symtab != NULL)
4986 expansion_notify (per_cu->v.quick->compunit_symtab);
4987 }
4988}
4989
3f563c84
PA
4990/* Helper for dw2_expand_matching symtabs. Called on each symbol
4991 matched, to expand corresponding CUs that were marked. IDX is the
4992 index of the symbol name that matched. */
4993
4994static void
4995dw2_expand_marked_cus
ed2dc618 4996 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4997 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4998 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4999 search_domain kind)
5000{
3f563c84
PA
5001 offset_type *vec, vec_len, vec_idx;
5002 bool global_seen = false;
ed2dc618 5003 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5004
61920122 5005 vec = (offset_type *) (index.constant_pool
f00a2de2 5006 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5007 vec_len = MAYBE_SWAP (vec[0]);
5008 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5009 {
61920122
PA
5010 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5011 /* This value is only valid for index versions >= 7. */
5012 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5013 gdb_index_symbol_kind symbol_kind =
5014 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5015 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5016 /* Only check the symbol attributes if they're present.
5017 Indices prior to version 7 don't record them,
5018 and indices >= 7 may elide them for certain symbols
5019 (gold does this). */
5020 int attrs_valid =
5021 (index.version >= 7
5022 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5023
5024 /* Work around gold/15646. */
5025 if (attrs_valid)
9291a0cd 5026 {
61920122
PA
5027 if (!is_static && global_seen)
5028 continue;
5029 if (!is_static)
5030 global_seen = true;
5031 }
3190f0c6 5032
61920122
PA
5033 /* Only check the symbol's kind if it has one. */
5034 if (attrs_valid)
5035 {
5036 switch (kind)
8943b874 5037 {
61920122
PA
5038 case VARIABLES_DOMAIN:
5039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5040 continue;
5041 break;
5042 case FUNCTIONS_DOMAIN:
5043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5044 continue;
61920122
PA
5045 break;
5046 case TYPES_DOMAIN:
5047 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5048 continue;
5049 break;
5050 default:
5051 break;
8943b874 5052 }
61920122 5053 }
8943b874 5054
61920122 5055 /* Don't crash on bad data. */
b76e467d 5056 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5057 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5058 {
b98664d3 5059 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5060 " [in module %s]"),
5061 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5062 continue;
5063 }
5064
ff4c9fec 5065 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5066 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5067 expansion_notify);
61920122
PA
5068 }
5069}
5070
4b514bc8
JK
5071/* If FILE_MATCHER is non-NULL, set all the
5072 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5073 that match FILE_MATCHER. */
5074
61920122 5075static void
4b514bc8 5076dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5077 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5078 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5079{
4b514bc8 5080 if (file_matcher == NULL)
61920122
PA
5081 return;
5082
4b514bc8
JK
5083 objfile *const objfile = dwarf2_per_objfile->objfile;
5084
5085 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5086 htab_eq_pointer,
5087 NULL, xcalloc, xfree));
5088 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5089 htab_eq_pointer,
5090 NULL, xcalloc, xfree));
61920122 5091
4b514bc8
JK
5092 /* The rule is CUs specify all the files, including those used by
5093 any TU, so there's no need to scan TUs here. */
61920122 5094
b76e467d 5095 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5096 {
927aa2e7
JK
5097 QUIT;
5098
5099 per_cu->v.quick->mark = 0;
5100
5101 /* We only need to look at symtabs not already expanded. */
5102 if (per_cu->v.quick->compunit_symtab)
5103 continue;
5104
b76e467d 5105 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5106 if (file_data == NULL)
5107 continue;
5108
5109 if (htab_find (visited_not_found.get (), file_data) != NULL)
5110 continue;
5111 else if (htab_find (visited_found.get (), file_data) != NULL)
5112 {
5113 per_cu->v.quick->mark = 1;
5114 continue;
5115 }
5116
b76e467d 5117 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5118 {
5119 const char *this_real_name;
5120
5121 if (file_matcher (file_data->file_names[j], false))
5122 {
5123 per_cu->v.quick->mark = 1;
5124 break;
5125 }
5126
5127 /* Before we invoke realpath, which can get expensive when many
5128 files are involved, do a quick comparison of the basenames. */
5129 if (!basenames_may_differ
5130 && !file_matcher (lbasename (file_data->file_names[j]),
5131 true))
5132 continue;
5133
5134 this_real_name = dw2_get_real_path (objfile, file_data, j);
5135 if (file_matcher (this_real_name, false))
5136 {
5137 per_cu->v.quick->mark = 1;
5138 break;
5139 }
5140 }
5141
b76e467d
SM
5142 void **slot = htab_find_slot (per_cu->v.quick->mark
5143 ? visited_found.get ()
5144 : visited_not_found.get (),
5145 file_data, INSERT);
927aa2e7
JK
5146 *slot = file_data;
5147 }
5148}
5149
5150static void
5151dw2_expand_symtabs_matching
5152 (struct objfile *objfile,
5153 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5154 const lookup_name_info &lookup_name,
5155 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5156 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5157 enum search_domain kind)
5158{
ed2dc618
SM
5159 struct dwarf2_per_objfile *dwarf2_per_objfile
5160 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5161
5162 /* index_table is NULL if OBJF_READNOW. */
5163 if (!dwarf2_per_objfile->index_table)
5164 return;
5165
ed2dc618 5166 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5167
5168 mapped_index &index = *dwarf2_per_objfile->index_table;
5169
5170 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5171 symbol_matcher,
5172 kind, [&] (offset_type idx)
5173 {
ed2dc618 5174 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5175 expansion_notify, kind);
3b00ef10 5176 return true;
927aa2e7
JK
5177 });
5178}
5179
5180/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5181 symtab. */
5182
5183static struct compunit_symtab *
5184recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5185 CORE_ADDR pc)
5186{
5187 int i;
5188
5189 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5190 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5191 return cust;
5192
5193 if (cust->includes == NULL)
5194 return NULL;
5195
5196 for (i = 0; cust->includes[i]; ++i)
5197 {
5198 struct compunit_symtab *s = cust->includes[i];
5199
5200 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5201 if (s != NULL)
5202 return s;
5203 }
5204
5205 return NULL;
5206}
5207
5208static struct compunit_symtab *
5209dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5210 struct bound_minimal_symbol msymbol,
5211 CORE_ADDR pc,
5212 struct obj_section *section,
5213 int warn_if_readin)
5214{
5215 struct dwarf2_per_cu_data *data;
5216 struct compunit_symtab *result;
5217
d320c2b5 5218 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5219 return NULL;
5220
79748972
TT
5221 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5222 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5223 data = (struct dwarf2_per_cu_data *) addrmap_find
5224 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5225 if (!data)
5226 return NULL;
5227
5228 if (warn_if_readin && data->v.quick->compunit_symtab)
5229 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5230 paddress (get_objfile_arch (objfile), pc));
5231
5232 result
58f0c718
TT
5233 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5234 false),
927aa2e7
JK
5235 pc);
5236 gdb_assert (result != NULL);
5237 return result;
5238}
5239
5240static void
5241dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5242 void *data, int need_fullname)
5243{
ed2dc618
SM
5244 struct dwarf2_per_objfile *dwarf2_per_objfile
5245 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5246
5247 if (!dwarf2_per_objfile->filenames_cache)
5248 {
5249 dwarf2_per_objfile->filenames_cache.emplace ();
5250
5251 htab_up visited (htab_create_alloc (10,
5252 htab_hash_pointer, htab_eq_pointer,
5253 NULL, xcalloc, xfree));
5254
5255 /* The rule is CUs specify all the files, including those used
5256 by any TU, so there's no need to scan TUs here. We can
5257 ignore file names coming from already-expanded CUs. */
5258
b76e467d 5259 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5260 {
927aa2e7
JK
5261 if (per_cu->v.quick->compunit_symtab)
5262 {
5263 void **slot = htab_find_slot (visited.get (),
5264 per_cu->v.quick->file_names,
5265 INSERT);
5266
5267 *slot = per_cu->v.quick->file_names;
5268 }
5269 }
5270
b76e467d 5271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5272 {
927aa2e7
JK
5273 /* We only need to look at symtabs not already expanded. */
5274 if (per_cu->v.quick->compunit_symtab)
5275 continue;
5276
b76e467d 5277 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5278 if (file_data == NULL)
5279 continue;
5280
b76e467d 5281 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5282 if (*slot)
5283 {
5284 /* Already visited. */
5285 continue;
5286 }
5287 *slot = file_data;
5288
5289 for (int j = 0; j < file_data->num_file_names; ++j)
5290 {
5291 const char *filename = file_data->file_names[j];
5292 dwarf2_per_objfile->filenames_cache->seen (filename);
5293 }
5294 }
5295 }
5296
5297 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5298 {
5299 gdb::unique_xmalloc_ptr<char> this_real_name;
5300
5301 if (need_fullname)
5302 this_real_name = gdb_realpath (filename);
5303 (*fun) (filename, this_real_name.get (), data);
5304 });
5305}
5306
5307static int
5308dw2_has_symbols (struct objfile *objfile)
5309{
5310 return 1;
5311}
5312
5313const struct quick_symbol_functions dwarf2_gdb_index_functions =
5314{
5315 dw2_has_symbols,
5316 dw2_find_last_source_symtab,
5317 dw2_forget_cached_source_info,
5318 dw2_map_symtabs_matching_filename,
5319 dw2_lookup_symbol,
5320 dw2_print_stats,
5321 dw2_dump,
927aa2e7
JK
5322 dw2_expand_symtabs_for_function,
5323 dw2_expand_all_symtabs,
5324 dw2_expand_symtabs_with_fullname,
5325 dw2_map_matching_symbols,
5326 dw2_expand_symtabs_matching,
5327 dw2_find_pc_sect_compunit_symtab,
5328 NULL,
5329 dw2_map_symbol_filenames
5330};
5331
5332/* DWARF-5 debug_names reader. */
5333
5334/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5335static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5336
5337/* A helper function that reads the .debug_names section in SECTION
5338 and fills in MAP. FILENAME is the name of the file containing the
5339 section; it is used for error reporting.
5340
5341 Returns true if all went well, false otherwise. */
5342
5343static bool
5344read_debug_names_from_section (struct objfile *objfile,
5345 const char *filename,
5346 struct dwarf2_section_info *section,
5347 mapped_debug_names &map)
5348{
5349 if (dwarf2_section_empty_p (section))
5350 return false;
5351
5352 /* Older elfutils strip versions could keep the section in the main
5353 executable while splitting it for the separate debug info file. */
5354 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5355 return false;
5356
5357 dwarf2_read_section (objfile, section);
5358
5359 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5360
5361 const gdb_byte *addr = section->buffer;
5362
5363 bfd *const abfd = get_section_bfd_owner (section);
5364
5365 unsigned int bytes_read;
5366 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5367 addr += bytes_read;
5368
5369 map.dwarf5_is_dwarf64 = bytes_read != 4;
5370 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5371 if (bytes_read + length != section->size)
5372 {
5373 /* There may be multiple per-CU indices. */
5374 warning (_("Section .debug_names in %s length %s does not match "
5375 "section length %s, ignoring .debug_names."),
5376 filename, plongest (bytes_read + length),
5377 pulongest (section->size));
5378 return false;
5379 }
5380
5381 /* The version number. */
5382 uint16_t version = read_2_bytes (abfd, addr);
5383 addr += 2;
5384 if (version != 5)
5385 {
5386 warning (_("Section .debug_names in %s has unsupported version %d, "
5387 "ignoring .debug_names."),
5388 filename, version);
5389 return false;
5390 }
5391
5392 /* Padding. */
5393 uint16_t padding = read_2_bytes (abfd, addr);
5394 addr += 2;
5395 if (padding != 0)
5396 {
5397 warning (_("Section .debug_names in %s has unsupported padding %d, "
5398 "ignoring .debug_names."),
5399 filename, padding);
5400 return false;
5401 }
5402
5403 /* comp_unit_count - The number of CUs in the CU list. */
5404 map.cu_count = read_4_bytes (abfd, addr);
5405 addr += 4;
5406
5407 /* local_type_unit_count - The number of TUs in the local TU
5408 list. */
5409 map.tu_count = read_4_bytes (abfd, addr);
5410 addr += 4;
5411
5412 /* foreign_type_unit_count - The number of TUs in the foreign TU
5413 list. */
5414 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5415 addr += 4;
5416 if (foreign_tu_count != 0)
5417 {
5418 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5419 "ignoring .debug_names."),
5420 filename, static_cast<unsigned long> (foreign_tu_count));
5421 return false;
5422 }
5423
5424 /* bucket_count - The number of hash buckets in the hash lookup
5425 table. */
5426 map.bucket_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* name_count - The number of unique names in the index. */
5430 map.name_count = read_4_bytes (abfd, addr);
5431 addr += 4;
5432
5433 /* abbrev_table_size - The size in bytes of the abbreviations
5434 table. */
5435 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5436 addr += 4;
5437
5438 /* augmentation_string_size - The size in bytes of the augmentation
5439 string. This value is rounded up to a multiple of 4. */
5440 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5441 addr += 4;
5442 map.augmentation_is_gdb = ((augmentation_string_size
5443 == sizeof (dwarf5_augmentation))
5444 && memcmp (addr, dwarf5_augmentation,
5445 sizeof (dwarf5_augmentation)) == 0);
5446 augmentation_string_size += (-augmentation_string_size) & 3;
5447 addr += augmentation_string_size;
5448
5449 /* List of CUs */
5450 map.cu_table_reordered = addr;
5451 addr += map.cu_count * map.offset_size;
5452
5453 /* List of Local TUs */
5454 map.tu_table_reordered = addr;
5455 addr += map.tu_count * map.offset_size;
5456
5457 /* Hash Lookup Table */
5458 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5459 addr += map.bucket_count * 4;
5460 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5461 addr += map.name_count * 4;
5462
5463 /* Name Table */
5464 map.name_table_string_offs_reordered = addr;
5465 addr += map.name_count * map.offset_size;
5466 map.name_table_entry_offs_reordered = addr;
5467 addr += map.name_count * map.offset_size;
5468
5469 const gdb_byte *abbrev_table_start = addr;
5470 for (;;)
5471 {
927aa2e7
JK
5472 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5473 addr += bytes_read;
5474 if (index_num == 0)
5475 break;
5476
5477 const auto insertpair
5478 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5479 if (!insertpair.second)
5480 {
5481 warning (_("Section .debug_names in %s has duplicate index %s, "
5482 "ignoring .debug_names."),
5483 filename, pulongest (index_num));
5484 return false;
5485 }
5486 mapped_debug_names::index_val &indexval = insertpair.first->second;
5487 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5488 addr += bytes_read;
5489
5490 for (;;)
5491 {
5492 mapped_debug_names::index_val::attr attr;
5493 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5494 addr += bytes_read;
5495 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5496 addr += bytes_read;
5497 if (attr.form == DW_FORM_implicit_const)
5498 {
5499 attr.implicit_const = read_signed_leb128 (abfd, addr,
5500 &bytes_read);
5501 addr += bytes_read;
5502 }
5503 if (attr.dw_idx == 0 && attr.form == 0)
5504 break;
5505 indexval.attr_vec.push_back (std::move (attr));
5506 }
5507 }
5508 if (addr != abbrev_table_start + abbrev_table_size)
5509 {
5510 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5511 "of size %s vs. written as %u, ignoring .debug_names."),
5512 filename, plongest (addr - abbrev_table_start),
5513 abbrev_table_size);
927aa2e7
JK
5514 return false;
5515 }
5516 map.entry_pool = addr;
5517
5518 return true;
5519}
5520
5521/* A helper for create_cus_from_debug_names that handles the MAP's CU
5522 list. */
5523
5524static void
ed2dc618 5525create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5526 const mapped_debug_names &map,
5527 dwarf2_section_info &section,
b76e467d 5528 bool is_dwz)
927aa2e7
JK
5529{
5530 sect_offset sect_off_prev;
5531 for (uint32_t i = 0; i <= map.cu_count; ++i)
5532 {
5533 sect_offset sect_off_next;
5534 if (i < map.cu_count)
5535 {
5536 sect_off_next
5537 = (sect_offset) (extract_unsigned_integer
5538 (map.cu_table_reordered + i * map.offset_size,
5539 map.offset_size,
5540 map.dwarf5_byte_order));
5541 }
5542 else
5543 sect_off_next = (sect_offset) section.size;
5544 if (i >= 1)
5545 {
5546 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5547 dwarf2_per_cu_data *per_cu
ed2dc618 5548 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5549 sect_off_prev, length);
b76e467d 5550 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5551 }
5552 sect_off_prev = sect_off_next;
5553 }
5554}
5555
5556/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5557 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5558
5559static void
ed2dc618 5560create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5561 const mapped_debug_names &map,
5562 const mapped_debug_names &dwz_map)
5563{
b76e467d
SM
5564 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5565 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5566
ed2dc618
SM
5567 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5568 dwarf2_per_objfile->info,
b76e467d 5569 false /* is_dwz */);
927aa2e7
JK
5570
5571 if (dwz_map.cu_count == 0)
5572 return;
5573
ed2dc618
SM
5574 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5575 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5576 true /* is_dwz */);
927aa2e7
JK
5577}
5578
5579/* Read .debug_names. If everything went ok, initialize the "quick"
5580 elements of all the CUs and return true. Otherwise, return false. */
5581
5582static bool
ed2dc618 5583dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5584{
22ca247e
TT
5585 std::unique_ptr<mapped_debug_names> map
5586 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5587 mapped_debug_names dwz_map (dwarf2_per_objfile);
5588 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5589
5590 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5591 &dwarf2_per_objfile->debug_names,
22ca247e 5592 *map))
927aa2e7
JK
5593 return false;
5594
5595 /* Don't use the index if it's empty. */
22ca247e 5596 if (map->name_count == 0)
927aa2e7
JK
5597 return false;
5598
5599 /* If there is a .dwz file, read it so we can get its CU list as
5600 well. */
ed2dc618 5601 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5602 if (dwz != NULL)
5603 {
5604 if (!read_debug_names_from_section (objfile,
00f93c44 5605 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5606 &dwz->debug_names, dwz_map))
5607 {
5608 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5609 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5610 return false;
5611 }
5612 }
5613
22ca247e 5614 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5615
22ca247e 5616 if (map->tu_count != 0)
927aa2e7
JK
5617 {
5618 /* We can only handle a single .debug_types when we have an
5619 index. */
fd5866f6 5620 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5621 return false;
5622
fd5866f6 5623 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5624
5625 create_signatured_type_table_from_debug_names
22ca247e 5626 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5627 }
5628
ed2dc618
SM
5629 create_addrmap_from_aranges (dwarf2_per_objfile,
5630 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5631
22ca247e 5632 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5633 dwarf2_per_objfile->using_index = 1;
5634 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5635 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5636
5637 return true;
5638}
5639
927aa2e7
JK
5640/* Type used to manage iterating over all CUs looking for a symbol for
5641 .debug_names. */
5642
5643class dw2_debug_names_iterator
5644{
5645public:
927aa2e7 5646 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5647 gdb::optional<block_enum> block_index,
5648 domain_enum domain,
927aa2e7 5649 const char *name)
2b79f376 5650 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5651 m_addr (find_vec_in_debug_names (map, name))
5652 {}
5653
5654 dw2_debug_names_iterator (const mapped_debug_names &map,
5655 search_domain search, uint32_t namei)
5656 : m_map (map),
5657 m_search (search),
5658 m_addr (find_vec_in_debug_names (map, namei))
5659 {}
5660
3b00ef10
TT
5661 dw2_debug_names_iterator (const mapped_debug_names &map,
5662 block_enum block_index, domain_enum domain,
5663 uint32_t namei)
5664 : m_map (map), m_block_index (block_index), m_domain (domain),
5665 m_addr (find_vec_in_debug_names (map, namei))
5666 {}
5667
927aa2e7
JK
5668 /* Return the next matching CU or NULL if there are no more. */
5669 dwarf2_per_cu_data *next ();
5670
5671private:
5672 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5673 const char *name);
5674 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5675 uint32_t namei);
5676
5677 /* The internalized form of .debug_names. */
5678 const mapped_debug_names &m_map;
5679
2b79f376
SM
5680 /* If set, only look for symbols that match that block. Valid values are
5681 GLOBAL_BLOCK and STATIC_BLOCK. */
5682 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5683
5684 /* The kind of symbol we're looking for. */
5685 const domain_enum m_domain = UNDEF_DOMAIN;
5686 const search_domain m_search = ALL_DOMAIN;
5687
5688 /* The list of CUs from the index entry of the symbol, or NULL if
5689 not found. */
5690 const gdb_byte *m_addr;
5691};
5692
5693const char *
5694mapped_debug_names::namei_to_name (uint32_t namei) const
5695{
5696 const ULONGEST namei_string_offs
5697 = extract_unsigned_integer ((name_table_string_offs_reordered
5698 + namei * offset_size),
5699 offset_size,
5700 dwarf5_byte_order);
5701 return read_indirect_string_at_offset
ed2dc618 5702 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5703}
5704
5705/* Find a slot in .debug_names for the object named NAME. If NAME is
5706 found, return pointer to its pool data. If NAME cannot be found,
5707 return NULL. */
5708
5709const gdb_byte *
5710dw2_debug_names_iterator::find_vec_in_debug_names
5711 (const mapped_debug_names &map, const char *name)
5712{
5713 int (*cmp) (const char *, const char *);
5714
54ee4252 5715 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5716 if (current_language->la_language == language_cplus
5717 || current_language->la_language == language_fortran
5718 || current_language->la_language == language_d)
5719 {
5720 /* NAME is already canonical. Drop any qualifiers as
5721 .debug_names does not contain any. */
5722
5723 if (strchr (name, '(') != NULL)
5724 {
54ee4252 5725 without_params = cp_remove_params (name);
927aa2e7 5726 if (without_params != NULL)
54ee4252 5727 name = without_params.get ();
927aa2e7
JK
5728 }
5729 }
5730
5731 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5732
5733 const uint32_t full_hash = dwarf5_djb_hash (name);
5734 uint32_t namei
5735 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5736 (map.bucket_table_reordered
5737 + (full_hash % map.bucket_count)), 4,
5738 map.dwarf5_byte_order);
5739 if (namei == 0)
5740 return NULL;
5741 --namei;
5742 if (namei >= map.name_count)
5743 {
b98664d3 5744 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5745 "[in module %s]"),
5746 namei, map.name_count,
ed2dc618 5747 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5748 return NULL;
5749 }
5750
5751 for (;;)
5752 {
5753 const uint32_t namei_full_hash
5754 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5755 (map.hash_table_reordered + namei), 4,
5756 map.dwarf5_byte_order);
5757 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5758 return NULL;
5759
5760 if (full_hash == namei_full_hash)
5761 {
5762 const char *const namei_string = map.namei_to_name (namei);
5763
5764#if 0 /* An expensive sanity check. */
5765 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5766 {
b98664d3 5767 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5768 "[in module %s]"),
5769 namei, objfile_name (dwarf2_per_objfile->objfile));
5770 return NULL;
5771 }
5772#endif
5773
5774 if (cmp (namei_string, name) == 0)
5775 {
5776 const ULONGEST namei_entry_offs
5777 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5778 + namei * map.offset_size),
5779 map.offset_size, map.dwarf5_byte_order);
5780 return map.entry_pool + namei_entry_offs;
5781 }
5782 }
5783
5784 ++namei;
5785 if (namei >= map.name_count)
5786 return NULL;
5787 }
5788}
5789
5790const gdb_byte *
5791dw2_debug_names_iterator::find_vec_in_debug_names
5792 (const mapped_debug_names &map, uint32_t namei)
5793{
5794 if (namei >= map.name_count)
5795 {
b98664d3 5796 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5797 "[in module %s]"),
5798 namei, map.name_count,
ed2dc618 5799 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5800 return NULL;
5801 }
5802
5803 const ULONGEST namei_entry_offs
5804 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5805 + namei * map.offset_size),
5806 map.offset_size, map.dwarf5_byte_order);
5807 return map.entry_pool + namei_entry_offs;
5808}
5809
5810/* See dw2_debug_names_iterator. */
5811
5812dwarf2_per_cu_data *
5813dw2_debug_names_iterator::next ()
5814{
5815 if (m_addr == NULL)
5816 return NULL;
5817
ed2dc618
SM
5818 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5819 struct objfile *objfile = dwarf2_per_objfile->objfile;
5820 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5821
5822 again:
5823
5824 unsigned int bytes_read;
5825 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5826 m_addr += bytes_read;
5827 if (abbrev == 0)
5828 return NULL;
5829
5830 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5831 if (indexval_it == m_map.abbrev_map.cend ())
5832 {
b98664d3 5833 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5834 "[in module %s]"),
ed2dc618 5835 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5836 return NULL;
5837 }
5838 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5839 enum class symbol_linkage {
5840 unknown,
5841 static_,
5842 extern_,
23c13d42 5843 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5844 dwarf2_per_cu_data *per_cu = NULL;
5845 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5846 {
5847 ULONGEST ull;
5848 switch (attr.form)
5849 {
5850 case DW_FORM_implicit_const:
5851 ull = attr.implicit_const;
5852 break;
5853 case DW_FORM_flag_present:
5854 ull = 1;
5855 break;
5856 case DW_FORM_udata:
5857 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5858 m_addr += bytes_read;
5859 break;
5860 default:
b98664d3 5861 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5862 dwarf_form_name (attr.form),
ed2dc618 5863 objfile_name (objfile));
927aa2e7
JK
5864 return NULL;
5865 }
5866 switch (attr.dw_idx)
5867 {
5868 case DW_IDX_compile_unit:
5869 /* Don't crash on bad data. */
b76e467d 5870 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5871 {
b98664d3 5872 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5873 " [in module %s]"),
5874 pulongest (ull),
5875 objfile_name (dwarf2_per_objfile->objfile));
5876 continue;
5877 }
ff4c9fec 5878 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5879 break;
8af5c486
JK
5880 case DW_IDX_type_unit:
5881 /* Don't crash on bad data. */
b2bdb8cf 5882 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5883 {
b98664d3 5884 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5885 " [in module %s]"),
5886 pulongest (ull),
5887 objfile_name (dwarf2_per_objfile->objfile));
5888 continue;
5889 }
ff4c9fec 5890 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5891 break;
927aa2e7
JK
5892 case DW_IDX_GNU_internal:
5893 if (!m_map.augmentation_is_gdb)
5894 break;
23c13d42 5895 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5896 break;
5897 case DW_IDX_GNU_external:
5898 if (!m_map.augmentation_is_gdb)
5899 break;
23c13d42 5900 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5901 break;
5902 }
5903 }
5904
5905 /* Skip if already read in. */
5906 if (per_cu->v.quick->compunit_symtab)
5907 goto again;
5908
5909 /* Check static vs global. */
23c13d42 5910 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5911 {
2b79f376 5912 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5913 const bool symbol_is_static =
5914 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5915 if (want_static != symbol_is_static)
2b79f376 5916 goto again;
927aa2e7
JK
5917 }
5918
5919 /* Match dw2_symtab_iter_next, symbol_kind
5920 and debug_names::psymbol_tag. */
5921 switch (m_domain)
5922 {
5923 case VAR_DOMAIN:
5924 switch (indexval.dwarf_tag)
5925 {
5926 case DW_TAG_variable:
5927 case DW_TAG_subprogram:
5928 /* Some types are also in VAR_DOMAIN. */
5929 case DW_TAG_typedef:
5930 case DW_TAG_structure_type:
5931 break;
5932 default:
5933 goto again;
5934 }
5935 break;
5936 case STRUCT_DOMAIN:
5937 switch (indexval.dwarf_tag)
5938 {
5939 case DW_TAG_typedef:
5940 case DW_TAG_structure_type:
5941 break;
5942 default:
5943 goto again;
5944 }
5945 break;
5946 case LABEL_DOMAIN:
5947 switch (indexval.dwarf_tag)
5948 {
5949 case 0:
5950 case DW_TAG_variable:
5951 break;
5952 default:
5953 goto again;
5954 }
5955 break;
5956 default:
5957 break;
5958 }
5959
5960 /* Match dw2_expand_symtabs_matching, symbol_kind and
5961 debug_names::psymbol_tag. */
5962 switch (m_search)
4b514bc8 5963 {
927aa2e7
JK
5964 case VARIABLES_DOMAIN:
5965 switch (indexval.dwarf_tag)
4b514bc8 5966 {
927aa2e7
JK
5967 case DW_TAG_variable:
5968 break;
5969 default:
5970 goto again;
4b514bc8 5971 }
927aa2e7
JK
5972 break;
5973 case FUNCTIONS_DOMAIN:
5974 switch (indexval.dwarf_tag)
4b514bc8 5975 {
927aa2e7
JK
5976 case DW_TAG_subprogram:
5977 break;
5978 default:
5979 goto again;
4b514bc8 5980 }
927aa2e7
JK
5981 break;
5982 case TYPES_DOMAIN:
5983 switch (indexval.dwarf_tag)
5984 {
5985 case DW_TAG_typedef:
5986 case DW_TAG_structure_type:
5987 break;
5988 default:
5989 goto again;
5990 }
5991 break;
5992 default:
5993 break;
4b514bc8 5994 }
927aa2e7
JK
5995
5996 return per_cu;
4b514bc8 5997}
61920122 5998
927aa2e7 5999static struct compunit_symtab *
c7f839cb 6000dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6001 const char *name, domain_enum domain)
4b514bc8 6002{
ed2dc618
SM
6003 struct dwarf2_per_objfile *dwarf2_per_objfile
6004 = get_dwarf2_per_objfile (objfile);
61920122 6005
927aa2e7
JK
6006 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6007 if (!mapp)
61920122 6008 {
927aa2e7
JK
6009 /* index is NULL if OBJF_READNOW. */
6010 return NULL;
6011 }
6012 const auto &map = *mapp;
9291a0cd 6013
2b79f376 6014 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6015
927aa2e7
JK
6016 struct compunit_symtab *stab_best = NULL;
6017 struct dwarf2_per_cu_data *per_cu;
6018 while ((per_cu = iter.next ()) != NULL)
6019 {
6020 struct symbol *sym, *with_opaque = NULL;
58f0c718 6021 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6022 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6023 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6024
927aa2e7
JK
6025 sym = block_find_symbol (block, name, domain,
6026 block_find_non_opaque_type_preferred,
6027 &with_opaque);
9703b513 6028
927aa2e7
JK
6029 /* Some caution must be observed with overloaded functions and
6030 methods, since the index will not contain any overload
6031 information (but NAME might contain it). */
a3ec0bb1 6032
927aa2e7
JK
6033 if (sym != NULL
6034 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6035 return stab;
6036 if (with_opaque != NULL
6037 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6038 stab_best = stab;
9703b513 6039
927aa2e7 6040 /* Keep looking through other CUs. */
9703b513
TT
6041 }
6042
927aa2e7 6043 return stab_best;
9703b513
TT
6044}
6045
927aa2e7
JK
6046/* This dumps minimal information about .debug_names. It is called
6047 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6048 uses this to verify that .debug_names has been loaded. */
9291a0cd 6049
927aa2e7
JK
6050static void
6051dw2_debug_names_dump (struct objfile *objfile)
6052{
ed2dc618
SM
6053 struct dwarf2_per_objfile *dwarf2_per_objfile
6054 = get_dwarf2_per_objfile (objfile);
6055
927aa2e7
JK
6056 gdb_assert (dwarf2_per_objfile->using_index);
6057 printf_filtered (".debug_names:");
6058 if (dwarf2_per_objfile->debug_names_table)
6059 printf_filtered (" exists\n");
6060 else
6061 printf_filtered (" faked for \"readnow\"\n");
6062 printf_filtered ("\n");
9291a0cd
TT
6063}
6064
9291a0cd 6065static void
927aa2e7
JK
6066dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6067 const char *func_name)
9291a0cd 6068{
ed2dc618
SM
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6071
927aa2e7
JK
6072 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6073 if (dwarf2_per_objfile->debug_names_table)
24c79950 6074 {
927aa2e7 6075 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6076
2b79f376 6077 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6078
927aa2e7
JK
6079 struct dwarf2_per_cu_data *per_cu;
6080 while ((per_cu = iter.next ()) != NULL)
58f0c718 6081 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6082 }
6083}
24c79950 6084
3b00ef10
TT
6085static void
6086dw2_debug_names_map_matching_symbols
6087 (struct objfile *objfile,
6088 const lookup_name_info &name, domain_enum domain,
6089 int global,
6090 gdb::function_view<symbol_found_callback_ftype> callback,
6091 symbol_compare_ftype *ordered_compare)
6092{
6093 struct dwarf2_per_objfile *dwarf2_per_objfile
6094 = get_dwarf2_per_objfile (objfile);
6095
6096 /* debug_names_table is NULL if OBJF_READNOW. */
6097 if (!dwarf2_per_objfile->debug_names_table)
6098 return;
6099
6100 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6101 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6102
6103 const char *match_name = name.ada ().lookup_name ().c_str ();
6104 auto matcher = [&] (const char *symname)
6105 {
6106 if (ordered_compare == nullptr)
6107 return true;
6108 return ordered_compare (symname, match_name) == 0;
6109 };
6110
6111 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6112 [&] (offset_type namei)
6113 {
6114 /* The name was matched, now expand corresponding CUs that were
6115 marked. */
6116 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6117
6118 struct dwarf2_per_cu_data *per_cu;
6119 while ((per_cu = iter.next ()) != NULL)
6120 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6121 return true;
6122 });
6123
6124 /* It's a shame we couldn't do this inside the
6125 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6126 that have already been expanded. Instead, this loop matches what
6127 the psymtab code does. */
6128 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6129 {
6130 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6131 if (cust != nullptr)
6132 {
6133 const struct block *block
6134 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6135 if (!iterate_over_symbols_terminated (block, name,
6136 domain, callback))
6137 break;
6138 }
6139 }
6140}
6141
927aa2e7
JK
6142static void
6143dw2_debug_names_expand_symtabs_matching
6144 (struct objfile *objfile,
6145 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6146 const lookup_name_info &lookup_name,
6147 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6148 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6149 enum search_domain kind)
6150{
ed2dc618
SM
6151 struct dwarf2_per_objfile *dwarf2_per_objfile
6152 = get_dwarf2_per_objfile (objfile);
9291a0cd 6153
927aa2e7
JK
6154 /* debug_names_table is NULL if OBJF_READNOW. */
6155 if (!dwarf2_per_objfile->debug_names_table)
6156 return;
9291a0cd 6157
ed2dc618 6158 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6159
44ed8f3e 6160 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6161
44ed8f3e
PA
6162 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6163 symbol_matcher,
6164 kind, [&] (offset_type namei)
927aa2e7 6165 {
927aa2e7
JK
6166 /* The name was matched, now expand corresponding CUs that were
6167 marked. */
6168 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6169
927aa2e7
JK
6170 struct dwarf2_per_cu_data *per_cu;
6171 while ((per_cu = iter.next ()) != NULL)
6172 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6173 expansion_notify);
3b00ef10 6174 return true;
44ed8f3e 6175 });
9291a0cd
TT
6176}
6177
927aa2e7 6178const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6179{
6180 dw2_has_symbols,
6181 dw2_find_last_source_symtab,
6182 dw2_forget_cached_source_info,
f8eba3c6 6183 dw2_map_symtabs_matching_filename,
927aa2e7 6184 dw2_debug_names_lookup_symbol,
9291a0cd 6185 dw2_print_stats,
927aa2e7 6186 dw2_debug_names_dump,
927aa2e7 6187 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6188 dw2_expand_all_symtabs,
652a8996 6189 dw2_expand_symtabs_with_fullname,
3b00ef10 6190 dw2_debug_names_map_matching_symbols,
927aa2e7 6191 dw2_debug_names_expand_symtabs_matching,
43f3e411 6192 dw2_find_pc_sect_compunit_symtab,
71a3c369 6193 NULL,
9291a0cd
TT
6194 dw2_map_symbol_filenames
6195};
6196
4485a1c1
SM
6197/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6198 to either a dwarf2_per_objfile or dwz_file object. */
6199
6200template <typename T>
6201static gdb::array_view<const gdb_byte>
6202get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6203{
6204 dwarf2_section_info *section = &section_owner->gdb_index;
6205
6206 if (dwarf2_section_empty_p (section))
6207 return {};
6208
6209 /* Older elfutils strip versions could keep the section in the main
6210 executable while splitting it for the separate debug info file. */
6211 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6212 return {};
6213
6214 dwarf2_read_section (obj, section);
6215
8bebfcda
PA
6216 /* dwarf2_section_info::size is a bfd_size_type, while
6217 gdb::array_view works with size_t. On 32-bit hosts, with
6218 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6219 is 32-bit. So we need an explicit narrowing conversion here.
6220 This is fine, because it's impossible to allocate or mmap an
6221 array/buffer larger than what size_t can represent. */
6222 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6223}
6224
87d6a7aa
SM
6225/* Lookup the index cache for the contents of the index associated to
6226 DWARF2_OBJ. */
6227
6228static gdb::array_view<const gdb_byte>
6229get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6230{
6231 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6232 if (build_id == nullptr)
6233 return {};
6234
6235 return global_index_cache.lookup_gdb_index (build_id,
6236 &dwarf2_obj->index_cache_res);
6237}
6238
6239/* Same as the above, but for DWZ. */
6240
6241static gdb::array_view<const gdb_byte>
6242get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6243{
6244 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6245 if (build_id == nullptr)
6246 return {};
6247
6248 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6249}
6250
3c0aa29a 6251/* See symfile.h. */
9291a0cd 6252
3c0aa29a
PA
6253bool
6254dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6255{
ed2dc618
SM
6256 struct dwarf2_per_objfile *dwarf2_per_objfile
6257 = get_dwarf2_per_objfile (objfile);
6258
9291a0cd
TT
6259 /* If we're about to read full symbols, don't bother with the
6260 indices. In this case we also don't care if some other debug
6261 format is making psymtabs, because they are all about to be
6262 expanded anyway. */
6263 if ((objfile->flags & OBJF_READNOW))
6264 {
9291a0cd 6265 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6266 create_all_comp_units (dwarf2_per_objfile);
6267 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6268 dwarf2_per_objfile->quick_file_names_table
6269 = create_quick_file_names_table
6270 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6271
b76e467d 6272 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6273 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6274 {
ff4c9fec 6275 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6276
e254ef6a
DE
6277 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6278 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6279 }
6280
6281 /* Return 1 so that gdb sees the "quick" functions. However,
6282 these functions will be no-ops because we will have expanded
6283 all symtabs. */
3c0aa29a
PA
6284 *index_kind = dw_index_kind::GDB_INDEX;
6285 return true;
9291a0cd
TT
6286 }
6287
ed2dc618 6288 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6289 {
6290 *index_kind = dw_index_kind::DEBUG_NAMES;
6291 return true;
6292 }
927aa2e7 6293
4485a1c1
SM
6294 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6295 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6296 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6297 {
6298 *index_kind = dw_index_kind::GDB_INDEX;
6299 return true;
6300 }
9291a0cd 6301
87d6a7aa
SM
6302 /* ... otherwise, try to find the index in the index cache. */
6303 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6304 get_gdb_index_contents_from_cache,
6305 get_gdb_index_contents_from_cache_dwz))
6306 {
6307 global_index_cache.hit ();
6308 *index_kind = dw_index_kind::GDB_INDEX;
6309 return true;
6310 }
6311
6312 global_index_cache.miss ();
3c0aa29a 6313 return false;
9291a0cd
TT
6314}
6315
6316\f
6317
dce234bc
PP
6318/* Build a partial symbol table. */
6319
6320void
f29dff0a 6321dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6322{
ed2dc618
SM
6323 struct dwarf2_per_objfile *dwarf2_per_objfile
6324 = get_dwarf2_per_objfile (objfile);
c9bf0622 6325
6eee24ce 6326 init_psymbol_list (objfile, 1024);
c906108c 6327
a70b8144 6328 try
c9bf0622
TT
6329 {
6330 /* This isn't really ideal: all the data we allocate on the
6331 objfile's obstack is still uselessly kept around. However,
6332 freeing it seems unsafe. */
906768f9 6333 psymtab_discarder psymtabs (objfile);
ed2dc618 6334 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6335 psymtabs.keep ();
87d6a7aa
SM
6336
6337 /* (maybe) store an index in the cache. */
6338 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6339 }
230d2906 6340 catch (const gdb_exception_error &except)
492d29ea
PA
6341 {
6342 exception_print (gdb_stderr, except);
6343 }
c906108c 6344}
c906108c 6345
1ce1cefd
DE
6346/* Return the total length of the CU described by HEADER. */
6347
6348static unsigned int
6349get_cu_length (const struct comp_unit_head *header)
6350{
6351 return header->initial_length_size + header->length;
6352}
6353
9c541725 6354/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6355
9c541725
PA
6356static inline bool
6357offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6358{
9c541725
PA
6359 sect_offset bottom = cu_header->sect_off;
6360 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6361
9c541725 6362 return sect_off >= bottom && sect_off < top;
45452591
DE
6363}
6364
3b80fe9b
DE
6365/* Find the base address of the compilation unit for range lists and
6366 location lists. It will normally be specified by DW_AT_low_pc.
6367 In DWARF-3 draft 4, the base address could be overridden by
6368 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6369 compilation units with discontinuous ranges. */
6370
6371static void
6372dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6373{
6374 struct attribute *attr;
6375
6376 cu->base_known = 0;
6377 cu->base_address = 0;
6378
6379 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6380 if (attr)
6381 {
31aa7e4e 6382 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6383 cu->base_known = 1;
6384 }
6385 else
6386 {
6387 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6388 if (attr)
6389 {
31aa7e4e 6390 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6391 cu->base_known = 1;
6392 }
6393 }
6394}
6395
93311388 6396/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6397 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6398 NOTE: This leaves members offset, first_die_offset to be filled in
6399 by the caller. */
107d2387 6400
d521ce57 6401static const gdb_byte *
107d2387 6402read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6403 const gdb_byte *info_ptr,
6404 struct dwarf2_section_info *section,
6405 rcuh_kind section_kind)
107d2387
AC
6406{
6407 int signed_addr;
891d2f0b 6408 unsigned int bytes_read;
43988095
JK
6409 const char *filename = get_section_file_name (section);
6410 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6411
6412 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6413 cu_header->initial_length_size = bytes_read;
6414 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6415 info_ptr += bytes_read;
107d2387 6416 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6417 if (cu_header->version < 2 || cu_header->version > 5)
6418 error (_("Dwarf Error: wrong version in compilation unit header "
6419 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6420 cu_header->version, filename);
107d2387 6421 info_ptr += 2;
43988095
JK
6422 if (cu_header->version < 5)
6423 switch (section_kind)
6424 {
6425 case rcuh_kind::COMPILE:
6426 cu_header->unit_type = DW_UT_compile;
6427 break;
6428 case rcuh_kind::TYPE:
6429 cu_header->unit_type = DW_UT_type;
6430 break;
6431 default:
6432 internal_error (__FILE__, __LINE__,
6433 _("read_comp_unit_head: invalid section_kind"));
6434 }
6435 else
6436 {
6437 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6438 (read_1_byte (abfd, info_ptr));
6439 info_ptr += 1;
6440 switch (cu_header->unit_type)
6441 {
6442 case DW_UT_compile:
a084a2a6
AT
6443 case DW_UT_partial:
6444 case DW_UT_skeleton:
6445 case DW_UT_split_compile:
43988095
JK
6446 if (section_kind != rcuh_kind::COMPILE)
6447 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6448 "(is %s, should be %s) [in module %s]"),
6449 dwarf_unit_type_name (cu_header->unit_type),
6450 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6451 break;
6452 case DW_UT_type:
a084a2a6 6453 case DW_UT_split_type:
43988095
JK
6454 section_kind = rcuh_kind::TYPE;
6455 break;
6456 default:
6457 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6458 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6459 "[in module %s]"), cu_header->unit_type,
6460 dwarf_unit_type_name (DW_UT_compile),
6461 dwarf_unit_type_name (DW_UT_skeleton),
6462 dwarf_unit_type_name (DW_UT_split_compile),
6463 dwarf_unit_type_name (DW_UT_type),
6464 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6465 }
6466
6467 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6468 info_ptr += 1;
6469 }
9c541725
PA
6470 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6471 cu_header,
6472 &bytes_read);
613e1657 6473 info_ptr += bytes_read;
43988095
JK
6474 if (cu_header->version < 5)
6475 {
6476 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6477 info_ptr += 1;
6478 }
107d2387
AC
6479 signed_addr = bfd_get_sign_extend_vma (abfd);
6480 if (signed_addr < 0)
8e65ff28 6481 internal_error (__FILE__, __LINE__,
e2e0b3e5 6482 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6483 cu_header->signed_addr_p = signed_addr;
c764a876 6484
a084a2a6
AT
6485 bool header_has_signature = section_kind == rcuh_kind::TYPE
6486 || cu_header->unit_type == DW_UT_skeleton
6487 || cu_header->unit_type == DW_UT_split_compile;
43988095 6488
a084a2a6
AT
6489 if (header_has_signature)
6490 {
43988095
JK
6491 cu_header->signature = read_8_bytes (abfd, info_ptr);
6492 info_ptr += 8;
a084a2a6 6493 }
43988095 6494
a084a2a6
AT
6495 if (section_kind == rcuh_kind::TYPE)
6496 {
6497 LONGEST type_offset;
43988095
JK
6498 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6499 info_ptr += bytes_read;
9c541725
PA
6500 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6501 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6502 error (_("Dwarf Error: Too big type_offset in compilation unit "
6503 "header (is %s) [in module %s]"), plongest (type_offset),
6504 filename);
6505 }
6506
107d2387
AC
6507 return info_ptr;
6508}
6509
36586728
TT
6510/* Helper function that returns the proper abbrev section for
6511 THIS_CU. */
6512
6513static struct dwarf2_section_info *
6514get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6515{
6516 struct dwarf2_section_info *abbrev;
ed2dc618 6517 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6518
6519 if (this_cu->is_dwz)
ed2dc618 6520 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6521 else
6522 abbrev = &dwarf2_per_objfile->abbrev;
6523
6524 return abbrev;
6525}
6526
9ff913ba
DE
6527/* Subroutine of read_and_check_comp_unit_head and
6528 read_and_check_type_unit_head to simplify them.
6529 Perform various error checking on the header. */
6530
6531static void
ed2dc618
SM
6532error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6533 struct comp_unit_head *header,
4bdcc0c1
DE
6534 struct dwarf2_section_info *section,
6535 struct dwarf2_section_info *abbrev_section)
9ff913ba 6536{
a32a8923 6537 const char *filename = get_section_file_name (section);
9ff913ba 6538
9c541725 6539 if (to_underlying (header->abbrev_sect_off)
36586728 6540 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6541 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6542 "(offset %s + 6) [in module %s]"),
6543 sect_offset_str (header->abbrev_sect_off),
6544 sect_offset_str (header->sect_off),
9ff913ba
DE
6545 filename);
6546
9c541725 6547 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6548 avoid potential 32-bit overflow. */
9c541725 6549 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6550 > section->size)
9c541725 6551 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6552 "(offset %s + 0) [in module %s]"),
6553 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6554 filename);
6555}
6556
6557/* Read in a CU/TU header and perform some basic error checking.
6558 The contents of the header are stored in HEADER.
6559 The result is a pointer to the start of the first DIE. */
adabb602 6560
d521ce57 6561static const gdb_byte *
ed2dc618
SM
6562read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6563 struct comp_unit_head *header,
9ff913ba 6564 struct dwarf2_section_info *section,
4bdcc0c1 6565 struct dwarf2_section_info *abbrev_section,
d521ce57 6566 const gdb_byte *info_ptr,
43988095 6567 rcuh_kind section_kind)
72bf9492 6568{
d521ce57 6569 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6570
9c541725 6571 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6572
43988095 6573 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6574
9c541725 6575 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6576
ed2dc618
SM
6577 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6578 abbrev_section);
9ff913ba
DE
6579
6580 return info_ptr;
348e048f
DE
6581}
6582
f4dc4d17
DE
6583/* Fetch the abbreviation table offset from a comp or type unit header. */
6584
6585static sect_offset
ed2dc618
SM
6586read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6587 struct dwarf2_section_info *section,
9c541725 6588 sect_offset sect_off)
f4dc4d17 6589{
a32a8923 6590 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6591 const gdb_byte *info_ptr;
ac298888 6592 unsigned int initial_length_size, offset_size;
43988095 6593 uint16_t version;
f4dc4d17
DE
6594
6595 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6596 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6597 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6598 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6599 info_ptr += initial_length_size;
6600
6601 version = read_2_bytes (abfd, info_ptr);
6602 info_ptr += 2;
6603 if (version >= 5)
6604 {
6605 /* Skip unit type and address size. */
6606 info_ptr += 2;
6607 }
6608
9c541725 6609 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6610}
6611
aaa75496
JB
6612/* Allocate a new partial symtab for file named NAME and mark this new
6613 partial symtab as being an include of PST. */
6614
6615static void
d521ce57 6616dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6617 struct objfile *objfile)
6618{
6619 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6620
fbd9ab74
JK
6621 if (!IS_ABSOLUTE_PATH (subpst->filename))
6622 {
6623 /* It shares objfile->objfile_obstack. */
6624 subpst->dirname = pst->dirname;
6625 }
6626
a9342b62 6627 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6628 subpst->dependencies[0] = pst;
6629 subpst->number_of_dependencies = 1;
6630
aaa75496 6631 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6632
6633 /* No private part is necessary for include psymtabs. This property
6634 can be used to differentiate between such include psymtabs and
10b3939b 6635 the regular ones. */
58a9656e 6636 subpst->read_symtab_private = NULL;
aaa75496
JB
6637}
6638
6639/* Read the Line Number Program data and extract the list of files
6640 included by the source file represented by PST. Build an include
d85a05f0 6641 partial symtab for each of these included files. */
aaa75496
JB
6642
6643static void
6644dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6645 struct die_info *die,
6646 struct partial_symtab *pst)
aaa75496 6647{
fff8551c 6648 line_header_up lh;
d85a05f0 6649 struct attribute *attr;
aaa75496 6650
d85a05f0
DJ
6651 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6652 if (attr)
9c541725 6653 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6654 if (lh == NULL)
6655 return; /* No linetable, so no includes. */
6656
79748972
TT
6657 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6658 that we pass in the raw text_low here; that is ok because we're
6659 only decoding the line table to make include partial symtabs, and
6660 so the addresses aren't really used. */
4ae976d1 6661 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6662 pst->raw_text_low (), 1);
aaa75496
JB
6663}
6664
348e048f 6665static hashval_t
52dc124a 6666hash_signatured_type (const void *item)
348e048f 6667{
9a3c8263
SM
6668 const struct signatured_type *sig_type
6669 = (const struct signatured_type *) item;
9a619af0 6670
348e048f 6671 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6672 return sig_type->signature;
348e048f
DE
6673}
6674
6675static int
52dc124a 6676eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6677{
9a3c8263
SM
6678 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6679 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6680
348e048f
DE
6681 return lhs->signature == rhs->signature;
6682}
6683
1fd400ff
TT
6684/* Allocate a hash table for signatured types. */
6685
6686static htab_t
673bfd45 6687allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6688{
6689 return htab_create_alloc_ex (41,
52dc124a
DE
6690 hash_signatured_type,
6691 eq_signatured_type,
1fd400ff
TT
6692 NULL,
6693 &objfile->objfile_obstack,
6694 hashtab_obstack_allocate,
6695 dummy_obstack_deallocate);
6696}
6697
d467dd73 6698/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6699
6700static int
d467dd73 6701add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6702{
9a3c8263 6703 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6704 std::vector<signatured_type *> *all_type_units
6705 = (std::vector<signatured_type *> *) datum;
1fd400ff 6706
b2bdb8cf 6707 all_type_units->push_back (sigt);
1fd400ff
TT
6708
6709 return 1;
6710}
6711
78d4d2c5 6712/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6713 and fill them into TYPES_HTAB. It will process only type units,
6714 therefore DW_UT_type. */
c88ee1f0 6715
78d4d2c5 6716static void
ed2dc618
SM
6717create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6718 struct dwo_file *dwo_file,
43988095
JK
6719 dwarf2_section_info *section, htab_t &types_htab,
6720 rcuh_kind section_kind)
348e048f 6721{
3019eac3 6722 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6723 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6724 bfd *abfd;
6725 const gdb_byte *info_ptr, *end_ptr;
348e048f 6726
4bdcc0c1
DE
6727 abbrev_section = (dwo_file != NULL
6728 ? &dwo_file->sections.abbrev
6729 : &dwarf2_per_objfile->abbrev);
6730
b4f54984 6731 if (dwarf_read_debug)
43988095
JK
6732 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6733 get_section_name (section),
a32a8923 6734 get_section_file_name (abbrev_section));
09406207 6735
78d4d2c5
JK
6736 dwarf2_read_section (objfile, section);
6737 info_ptr = section->buffer;
348e048f 6738
78d4d2c5
JK
6739 if (info_ptr == NULL)
6740 return;
348e048f 6741
78d4d2c5
JK
6742 /* We can't set abfd until now because the section may be empty or
6743 not present, in which case the bfd is unknown. */
6744 abfd = get_section_bfd_owner (section);
348e048f 6745
78d4d2c5
JK
6746 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6747 because we don't need to read any dies: the signature is in the
6748 header. */
3019eac3 6749
78d4d2c5
JK
6750 end_ptr = info_ptr + section->size;
6751 while (info_ptr < end_ptr)
6752 {
78d4d2c5
JK
6753 struct signatured_type *sig_type;
6754 struct dwo_unit *dwo_tu;
6755 void **slot;
6756 const gdb_byte *ptr = info_ptr;
6757 struct comp_unit_head header;
6758 unsigned int length;
8b70b953 6759
9c541725 6760 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6761
a49dd8dd
JK
6762 /* Initialize it due to a false compiler warning. */
6763 header.signature = -1;
9c541725 6764 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6765
78d4d2c5
JK
6766 /* We need to read the type's signature in order to build the hash
6767 table, but we don't need anything else just yet. */
348e048f 6768
ed2dc618 6769 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6770 abbrev_section, ptr, section_kind);
348e048f 6771
78d4d2c5 6772 length = get_cu_length (&header);
6caca83c 6773
78d4d2c5
JK
6774 /* Skip dummy type units. */
6775 if (ptr >= info_ptr + length
43988095
JK
6776 || peek_abbrev_code (abfd, ptr) == 0
6777 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6778 {
6779 info_ptr += length;
6780 continue;
6781 }
dee91e82 6782
78d4d2c5
JK
6783 if (types_htab == NULL)
6784 {
6785 if (dwo_file)
6786 types_htab = allocate_dwo_unit_table (objfile);
6787 else
6788 types_htab = allocate_signatured_type_table (objfile);
6789 }
8b70b953 6790
78d4d2c5
JK
6791 if (dwo_file)
6792 {
6793 sig_type = NULL;
6794 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6795 struct dwo_unit);
6796 dwo_tu->dwo_file = dwo_file;
43988095 6797 dwo_tu->signature = header.signature;
9c541725 6798 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6799 dwo_tu->section = section;
9c541725 6800 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6801 dwo_tu->length = length;
6802 }
6803 else
6804 {
6805 /* N.B.: type_offset is not usable if this type uses a DWO file.
6806 The real type_offset is in the DWO file. */
6807 dwo_tu = NULL;
6808 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6809 struct signatured_type);
43988095 6810 sig_type->signature = header.signature;
9c541725 6811 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6812 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6813 sig_type->per_cu.is_debug_types = 1;
6814 sig_type->per_cu.section = section;
9c541725 6815 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6816 sig_type->per_cu.length = length;
6817 }
6818
6819 slot = htab_find_slot (types_htab,
6820 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6821 INSERT);
6822 gdb_assert (slot != NULL);
6823 if (*slot != NULL)
6824 {
9c541725 6825 sect_offset dup_sect_off;
0349ea22 6826
3019eac3
DE
6827 if (dwo_file)
6828 {
78d4d2c5
JK
6829 const struct dwo_unit *dup_tu
6830 = (const struct dwo_unit *) *slot;
6831
9c541725 6832 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6833 }
6834 else
6835 {
78d4d2c5
JK
6836 const struct signatured_type *dup_tu
6837 = (const struct signatured_type *) *slot;
6838
9c541725 6839 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6840 }
8b70b953 6841
b98664d3 6842 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6843 " the entry at offset %s, signature %s"),
6844 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6845 hex_string (header.signature));
78d4d2c5
JK
6846 }
6847 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6848
78d4d2c5 6849 if (dwarf_read_debug > 1)
9d8780f0
SM
6850 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6851 sect_offset_str (sect_off),
43988095 6852 hex_string (header.signature));
3019eac3 6853
78d4d2c5
JK
6854 info_ptr += length;
6855 }
6856}
3019eac3 6857
78d4d2c5
JK
6858/* Create the hash table of all entries in the .debug_types
6859 (or .debug_types.dwo) section(s).
6860 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6861 otherwise it is NULL.
b3c8eb43 6862
78d4d2c5 6863 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6864
78d4d2c5 6865 Note: This function processes DWO files only, not DWP files. */
348e048f 6866
78d4d2c5 6867static void
ed2dc618
SM
6868create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6869 struct dwo_file *dwo_file,
fd5866f6 6870 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6871 htab_t &types_htab)
6872{
fd5866f6
SM
6873 for (dwarf2_section_info &section : type_sections)
6874 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6875 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6876}
6877
6878/* Create the hash table of all entries in the .debug_types section,
6879 and initialize all_type_units.
6880 The result is zero if there is an error (e.g. missing .debug_types section),
6881 otherwise non-zero. */
6882
6883static int
ed2dc618 6884create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6885{
78d4d2c5 6886 htab_t types_htab = NULL;
3019eac3 6887
ed2dc618
SM
6888 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6889 &dwarf2_per_objfile->info, types_htab,
43988095 6890 rcuh_kind::COMPILE);
ed2dc618
SM
6891 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6892 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6893 if (types_htab == NULL)
6894 {
6895 dwarf2_per_objfile->signatured_types = NULL;
6896 return 0;
6897 }
6898
348e048f
DE
6899 dwarf2_per_objfile->signatured_types = types_htab;
6900
b2bdb8cf
SM
6901 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6902 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6903
6904 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6905 &dwarf2_per_objfile->all_type_units);
1fd400ff 6906
348e048f
DE
6907 return 1;
6908}
6909
6aa5f3a6
DE
6910/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6911 If SLOT is non-NULL, it is the entry to use in the hash table.
6912 Otherwise we find one. */
6913
6914static struct signatured_type *
ed2dc618
SM
6915add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6916 void **slot)
6aa5f3a6
DE
6917{
6918 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6919
b2bdb8cf
SM
6920 if (dwarf2_per_objfile->all_type_units.size ()
6921 == dwarf2_per_objfile->all_type_units.capacity ())
6922 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6923
b2bdb8cf
SM
6924 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6925 struct signatured_type);
6926
6927 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6928 sig_type->signature = sig;
6929 sig_type->per_cu.is_debug_types = 1;
6930 if (dwarf2_per_objfile->using_index)
6931 {
6932 sig_type->per_cu.v.quick =
6933 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6934 struct dwarf2_per_cu_quick_data);
6935 }
6936
6937 if (slot == NULL)
6938 {
6939 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6940 sig_type, INSERT);
6941 }
6942 gdb_assert (*slot == NULL);
6943 *slot = sig_type;
6944 /* The rest of sig_type must be filled in by the caller. */
6945 return sig_type;
6946}
6947
a2ce51a0
DE
6948/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6949 Fill in SIG_ENTRY with DWO_ENTRY. */
6950
6951static void
ed2dc618 6952fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6953 struct signatured_type *sig_entry,
6954 struct dwo_unit *dwo_entry)
6955{
7ee85ab1 6956 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6957 gdb_assert (! sig_entry->per_cu.queued);
6958 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6959 if (dwarf2_per_objfile->using_index)
6960 {
6961 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6962 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6963 }
6964 else
6965 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6966 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6967 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6968 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6969 gdb_assert (sig_entry->dwo_unit == NULL);
6970
6971 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6972 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6973 sig_entry->per_cu.length = dwo_entry->length;
6974 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6975 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6976 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6977 sig_entry->dwo_unit = dwo_entry;
6978}
6979
6980/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6981 If we haven't read the TU yet, create the signatured_type data structure
6982 for a TU to be read in directly from a DWO file, bypassing the stub.
6983 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6984 using .gdb_index, then when reading a CU we want to stay in the DWO file
6985 containing that CU. Otherwise we could end up reading several other DWO
6986 files (due to comdat folding) to process the transitive closure of all the
6987 mentioned TUs, and that can be slow. The current DWO file will have every
6988 type signature that it needs.
a2ce51a0
DE
6989 We only do this for .gdb_index because in the psymtab case we already have
6990 to read all the DWOs to build the type unit groups. */
6991
6992static struct signatured_type *
6993lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6994{
518817b3
SM
6995 struct dwarf2_per_objfile *dwarf2_per_objfile
6996 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6997 struct objfile *objfile = dwarf2_per_objfile->objfile;
6998 struct dwo_file *dwo_file;
6999 struct dwo_unit find_dwo_entry, *dwo_entry;
7000 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7001 void **slot;
a2ce51a0
DE
7002
7003 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7004
6aa5f3a6
DE
7005 /* If TU skeletons have been removed then we may not have read in any
7006 TUs yet. */
7007 if (dwarf2_per_objfile->signatured_types == NULL)
7008 {
7009 dwarf2_per_objfile->signatured_types
7010 = allocate_signatured_type_table (objfile);
7011 }
a2ce51a0
DE
7012
7013 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7014 Use the global signatured_types array to do our own comdat-folding
7015 of types. If this is the first time we're reading this TU, and
7016 the TU has an entry in .gdb_index, replace the recorded data from
7017 .gdb_index with this TU. */
a2ce51a0 7018
a2ce51a0 7019 find_sig_entry.signature = sig;
6aa5f3a6
DE
7020 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7021 &find_sig_entry, INSERT);
9a3c8263 7022 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7023
7024 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7025 read. Don't reassign the global entry to point to this DWO if that's
7026 the case. Also note that if the TU is already being read, it may not
7027 have come from a DWO, the program may be a mix of Fission-compiled
7028 code and non-Fission-compiled code. */
7029
7030 /* Have we already tried to read this TU?
7031 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7032 needn't exist in the global table yet). */
7033 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7034 return sig_entry;
7035
6aa5f3a6
DE
7036 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7037 dwo_unit of the TU itself. */
7038 dwo_file = cu->dwo_unit->dwo_file;
7039
a2ce51a0
DE
7040 /* Ok, this is the first time we're reading this TU. */
7041 if (dwo_file->tus == NULL)
7042 return NULL;
7043 find_dwo_entry.signature = sig;
9a3c8263 7044 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7045 if (dwo_entry == NULL)
7046 return NULL;
7047
6aa5f3a6
DE
7048 /* If the global table doesn't have an entry for this TU, add one. */
7049 if (sig_entry == NULL)
ed2dc618 7050 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7051
ed2dc618 7052 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7053 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7054 return sig_entry;
7055}
7056
a2ce51a0
DE
7057/* Subroutine of lookup_signatured_type.
7058 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7059 then try the DWP file. If the TU stub (skeleton) has been removed then
7060 it won't be in .gdb_index. */
a2ce51a0
DE
7061
7062static struct signatured_type *
7063lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7064{
518817b3
SM
7065 struct dwarf2_per_objfile *dwarf2_per_objfile
7066 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7067 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7068 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7069 struct dwo_unit *dwo_entry;
7070 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7071 void **slot;
a2ce51a0
DE
7072
7073 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7074 gdb_assert (dwp_file != NULL);
7075
6aa5f3a6
DE
7076 /* If TU skeletons have been removed then we may not have read in any
7077 TUs yet. */
7078 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7079 {
6aa5f3a6
DE
7080 dwarf2_per_objfile->signatured_types
7081 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7082 }
7083
6aa5f3a6
DE
7084 find_sig_entry.signature = sig;
7085 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7086 &find_sig_entry, INSERT);
9a3c8263 7087 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7088
7089 /* Have we already tried to read this TU?
7090 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7091 needn't exist in the global table yet). */
7092 if (sig_entry != NULL)
7093 return sig_entry;
7094
a2ce51a0
DE
7095 if (dwp_file->tus == NULL)
7096 return NULL;
ed2dc618 7097 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7098 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7099 if (dwo_entry == NULL)
7100 return NULL;
7101
ed2dc618
SM
7102 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7103 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7104
a2ce51a0
DE
7105 return sig_entry;
7106}
7107
380bca97 7108/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7109 Returns NULL if signature SIG is not present in the table.
7110 It is up to the caller to complain about this. */
348e048f
DE
7111
7112static struct signatured_type *
a2ce51a0 7113lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7114{
518817b3
SM
7115 struct dwarf2_per_objfile *dwarf2_per_objfile
7116 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7117
a2ce51a0
DE
7118 if (cu->dwo_unit
7119 && dwarf2_per_objfile->using_index)
7120 {
7121 /* We're in a DWO/DWP file, and we're using .gdb_index.
7122 These cases require special processing. */
ed2dc618 7123 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7124 return lookup_dwo_signatured_type (cu, sig);
7125 else
7126 return lookup_dwp_signatured_type (cu, sig);
7127 }
7128 else
7129 {
7130 struct signatured_type find_entry, *entry;
348e048f 7131
a2ce51a0
DE
7132 if (dwarf2_per_objfile->signatured_types == NULL)
7133 return NULL;
7134 find_entry.signature = sig;
9a3c8263
SM
7135 entry = ((struct signatured_type *)
7136 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7137 return entry;
7138 }
348e048f 7139}
42e7ad6c
DE
7140\f
7141/* Low level DIE reading support. */
348e048f 7142
d85a05f0
DJ
7143/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7144
7145static void
7146init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7147 struct dwarf2_cu *cu,
3019eac3 7148 struct dwarf2_section_info *section,
685af9cd
TT
7149 struct dwo_file *dwo_file,
7150 struct abbrev_table *abbrev_table)
d85a05f0 7151{
fceca515 7152 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7153 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7154 reader->cu = cu;
3019eac3 7155 reader->dwo_file = dwo_file;
dee91e82
DE
7156 reader->die_section = section;
7157 reader->buffer = section->buffer;
f664829e 7158 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7159 reader->comp_dir = NULL;
685af9cd 7160 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7161}
7162
b0c7bfa9
DE
7163/* Subroutine of init_cutu_and_read_dies to simplify it.
7164 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7165 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7166 already.
7167
7168 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7169 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7170 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7171 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7172 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7173 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7174 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7175 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7176 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7177 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7178 kept around for at least as long as *RESULT_READER.
7179
b0c7bfa9
DE
7180 The result is non-zero if a valid (non-dummy) DIE was found. */
7181
7182static int
7183read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7184 struct dwo_unit *dwo_unit,
b0c7bfa9 7185 struct die_info *stub_comp_unit_die,
a2ce51a0 7186 const char *stub_comp_dir,
b0c7bfa9 7187 struct die_reader_specs *result_reader,
d521ce57 7188 const gdb_byte **result_info_ptr,
b0c7bfa9 7189 struct die_info **result_comp_unit_die,
685af9cd
TT
7190 int *result_has_children,
7191 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7192{
ed2dc618 7193 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7194 struct objfile *objfile = dwarf2_per_objfile->objfile;
7195 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7196 bfd *abfd;
d521ce57 7197 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7198 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7199 int i,num_extra_attrs;
7200 struct dwarf2_section_info *dwo_abbrev_section;
7201 struct attribute *attr;
7202 struct die_info *comp_unit_die;
7203
b0aeadb3
DE
7204 /* At most one of these may be provided. */
7205 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7206
b0c7bfa9
DE
7207 /* These attributes aren't processed until later:
7208 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7209 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7210 referenced later. However, these attributes are found in the stub
7211 which we won't have later. In order to not impose this complication
7212 on the rest of the code, we read them here and copy them to the
7213 DWO CU/TU die. */
b0c7bfa9
DE
7214
7215 stmt_list = NULL;
7216 low_pc = NULL;
7217 high_pc = NULL;
7218 ranges = NULL;
7219 comp_dir = NULL;
7220
7221 if (stub_comp_unit_die != NULL)
7222 {
7223 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7224 DWO file. */
7225 if (! this_cu->is_debug_types)
7226 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7227 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7228 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7229 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7230 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7231
7232 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7233 We need the value before we can process DW_FORM_GNU_addr_index
7234 or DW_FORM_addrx. */
b0c7bfa9
DE
7235 cu->addr_base = 0;
7236 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7237 if (attr)
7238 cu->addr_base = DW_UNSND (attr);
7239
7240 /* There should be a DW_AT_ranges_base attribute here (if needed).
7241 We need the value before we can process DW_AT_ranges. */
7242 cu->ranges_base = 0;
7243 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7244 if (attr)
7245 cu->ranges_base = DW_UNSND (attr);
7246 }
a2ce51a0
DE
7247 else if (stub_comp_dir != NULL)
7248 {
7249 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7250 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7251 comp_dir->name = DW_AT_comp_dir;
7252 comp_dir->form = DW_FORM_string;
7253 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7254 DW_STRING (comp_dir) = stub_comp_dir;
7255 }
b0c7bfa9
DE
7256
7257 /* Set up for reading the DWO CU/TU. */
7258 cu->dwo_unit = dwo_unit;
685af9cd 7259 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7260 dwarf2_read_section (objfile, section);
a32a8923 7261 abfd = get_section_bfd_owner (section);
9c541725
PA
7262 begin_info_ptr = info_ptr = (section->buffer
7263 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7264 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7265
7266 if (this_cu->is_debug_types)
7267 {
b0c7bfa9
DE
7268 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7269
ed2dc618
SM
7270 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7271 &cu->header, section,
b0c7bfa9 7272 dwo_abbrev_section,
43988095 7273 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7274 /* This is not an assert because it can be caused by bad debug info. */
43988095 7275 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7276 {
7277 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7278 " TU at offset %s [in module %s]"),
a2ce51a0 7279 hex_string (sig_type->signature),
43988095 7280 hex_string (cu->header.signature),
9d8780f0 7281 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7282 bfd_get_filename (abfd));
7283 }
9c541725 7284 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7285 /* For DWOs coming from DWP files, we don't know the CU length
7286 nor the type's offset in the TU until now. */
7287 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7288 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7289
7290 /* Establish the type offset that can be used to lookup the type.
7291 For DWO files, we don't know it until now. */
9c541725
PA
7292 sig_type->type_offset_in_section
7293 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7294 }
7295 else
7296 {
ed2dc618
SM
7297 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7298 &cu->header, section,
b0c7bfa9 7299 dwo_abbrev_section,
43988095 7300 info_ptr, rcuh_kind::COMPILE);
9c541725 7301 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7302 /* For DWOs coming from DWP files, we don't know the CU length
7303 until now. */
7304 dwo_unit->length = get_cu_length (&cu->header);
7305 }
7306
685af9cd
TT
7307 *result_dwo_abbrev_table
7308 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7309 cu->header.abbrev_sect_off);
7310 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7311 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7312
7313 /* Read in the die, but leave space to copy over the attributes
7314 from the stub. This has the benefit of simplifying the rest of
7315 the code - all the work to maintain the illusion of a single
7316 DW_TAG_{compile,type}_unit DIE is done here. */
7317 num_extra_attrs = ((stmt_list != NULL)
7318 + (low_pc != NULL)
7319 + (high_pc != NULL)
7320 + (ranges != NULL)
7321 + (comp_dir != NULL));
7322 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7323 result_has_children, num_extra_attrs);
7324
7325 /* Copy over the attributes from the stub to the DIE we just read in. */
7326 comp_unit_die = *result_comp_unit_die;
7327 i = comp_unit_die->num_attrs;
7328 if (stmt_list != NULL)
7329 comp_unit_die->attrs[i++] = *stmt_list;
7330 if (low_pc != NULL)
7331 comp_unit_die->attrs[i++] = *low_pc;
7332 if (high_pc != NULL)
7333 comp_unit_die->attrs[i++] = *high_pc;
7334 if (ranges != NULL)
7335 comp_unit_die->attrs[i++] = *ranges;
7336 if (comp_dir != NULL)
7337 comp_unit_die->attrs[i++] = *comp_dir;
7338 comp_unit_die->num_attrs += num_extra_attrs;
7339
b4f54984 7340 if (dwarf_die_debug)
bf6af496
DE
7341 {
7342 fprintf_unfiltered (gdb_stdlog,
7343 "Read die from %s@0x%x of %s:\n",
a32a8923 7344 get_section_name (section),
bf6af496
DE
7345 (unsigned) (begin_info_ptr - section->buffer),
7346 bfd_get_filename (abfd));
b4f54984 7347 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7348 }
7349
a2ce51a0
DE
7350 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7351 TUs by skipping the stub and going directly to the entry in the DWO file.
7352 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7353 to get it via circuitous means. Blech. */
7354 if (comp_dir != NULL)
7355 result_reader->comp_dir = DW_STRING (comp_dir);
7356
b0c7bfa9
DE
7357 /* Skip dummy compilation units. */
7358 if (info_ptr >= begin_info_ptr + dwo_unit->length
7359 || peek_abbrev_code (abfd, info_ptr) == 0)
7360 return 0;
7361
7362 *result_info_ptr = info_ptr;
7363 return 1;
7364}
7365
a084a2a6
AT
7366/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7367 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7368 signature is part of the header. */
7369static gdb::optional<ULONGEST>
7370lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7371{
7372 if (cu->header.version >= 5)
7373 return cu->header.signature;
7374 struct attribute *attr;
7375 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7376 if (attr == nullptr)
7377 return gdb::optional<ULONGEST> ();
7378 return DW_UNSND (attr);
7379}
7380
b0c7bfa9
DE
7381/* Subroutine of init_cutu_and_read_dies to simplify it.
7382 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7383 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7384
7385static struct dwo_unit *
7386lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7387 struct die_info *comp_unit_die)
7388{
7389 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7390 struct dwo_unit *dwo_unit;
7391 const char *comp_dir, *dwo_name;
7392
a2ce51a0
DE
7393 gdb_assert (cu != NULL);
7394
b0c7bfa9 7395 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7396 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7397 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7398
7399 if (this_cu->is_debug_types)
7400 {
7401 struct signatured_type *sig_type;
7402
7403 /* Since this_cu is the first member of struct signatured_type,
7404 we can go from a pointer to one to a pointer to the other. */
7405 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7406 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7407 }
7408 else
7409 {
a084a2a6
AT
7410 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7411 if (!signature.has_value ())
b0c7bfa9
DE
7412 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7413 " [in module %s]"),
e3b94546 7414 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7415 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7416 *signature);
b0c7bfa9
DE
7417 }
7418
b0c7bfa9
DE
7419 return dwo_unit;
7420}
7421
a2ce51a0 7422/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7423 See it for a description of the parameters.
fcd3b13d 7424 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7425
7426static void
6aa5f3a6
DE
7427init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7428 int use_existing_cu, int keep,
a2ce51a0
DE
7429 die_reader_func_ftype *die_reader_func,
7430 void *data)
7431{
fcd3b13d 7432 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7433 struct signatured_type *sig_type;
a2ce51a0
DE
7434 struct die_reader_specs reader;
7435 const gdb_byte *info_ptr;
7436 struct die_info *comp_unit_die;
7437 int has_children;
ed2dc618 7438 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7439
7440 /* Verify we can do the following downcast, and that we have the
7441 data we need. */
7442 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7443 sig_type = (struct signatured_type *) this_cu;
7444 gdb_assert (sig_type->dwo_unit != NULL);
7445
6aa5f3a6
DE
7446 if (use_existing_cu && this_cu->cu != NULL)
7447 {
7448 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7449 /* There's no need to do the rereading_dwo_cu handling that
7450 init_cutu_and_read_dies does since we don't read the stub. */
7451 }
7452 else
7453 {
7454 /* If !use_existing_cu, this_cu->cu must be NULL. */
7455 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7456 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7457 }
7458
7459 /* A future optimization, if needed, would be to use an existing
7460 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7461 could share abbrev tables. */
a2ce51a0 7462
685af9cd
TT
7463 /* The abbreviation table used by READER, this must live at least as long as
7464 READER. */
7465 abbrev_table_up dwo_abbrev_table;
7466
a2ce51a0 7467 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7468 NULL /* stub_comp_unit_die */,
7469 sig_type->dwo_unit->dwo_file->comp_dir,
7470 &reader, &info_ptr,
685af9cd
TT
7471 &comp_unit_die, &has_children,
7472 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7473 {
7474 /* Dummy die. */
a2ce51a0
DE
7475 return;
7476 }
7477
7478 /* All the "real" work is done here. */
7479 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7480
6aa5f3a6 7481 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7482 but the alternative is making the latter more complex.
7483 This function is only for the special case of using DWO files directly:
7484 no point in overly complicating the general case just to handle this. */
fcd3b13d 7485 if (new_cu != NULL && keep)
a2ce51a0 7486 {
fcd3b13d
SM
7487 /* Link this CU into read_in_chain. */
7488 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7489 dwarf2_per_objfile->read_in_chain = this_cu;
7490 /* The chain owns it now. */
7491 new_cu.release ();
a2ce51a0 7492 }
a2ce51a0
DE
7493}
7494
fd820528 7495/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7496 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7497
f4dc4d17
DE
7498 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7499 Otherwise the table specified in the comp unit header is read in and used.
7500 This is an optimization for when we already have the abbrev table.
7501
dee91e82
DE
7502 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7503 Otherwise, a new CU is allocated with xmalloc.
7504
7505 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7506 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7507
7508 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7509 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7510
70221824 7511static void
fd820528 7512init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7513 struct abbrev_table *abbrev_table,
fd820528 7514 int use_existing_cu, int keep,
58f0c718 7515 bool skip_partial,
fd820528
DE
7516 die_reader_func_ftype *die_reader_func,
7517 void *data)
c906108c 7518{
ed2dc618 7519 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7520 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7521 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7522 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7523 struct dwarf2_cu *cu;
d521ce57 7524 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7525 struct die_reader_specs reader;
d85a05f0 7526 struct die_info *comp_unit_die;
dee91e82 7527 int has_children;
dee91e82 7528 struct signatured_type *sig_type = NULL;
4bdcc0c1 7529 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7530 /* Non-zero if CU currently points to a DWO file and we need to
7531 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7532 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7533 int rereading_dwo_cu = 0;
c906108c 7534
b4f54984 7535 if (dwarf_die_debug)
9d8780f0 7536 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7537 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7538 sect_offset_str (this_cu->sect_off));
09406207 7539
dee91e82
DE
7540 if (use_existing_cu)
7541 gdb_assert (keep);
23745b47 7542
a2ce51a0
DE
7543 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7544 file (instead of going through the stub), short-circuit all of this. */
7545 if (this_cu->reading_dwo_directly)
7546 {
7547 /* Narrow down the scope of possibilities to have to understand. */
7548 gdb_assert (this_cu->is_debug_types);
7549 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7550 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7551 die_reader_func, data);
a2ce51a0
DE
7552 return;
7553 }
7554
dee91e82
DE
7555 /* This is cheap if the section is already read in. */
7556 dwarf2_read_section (objfile, section);
7557
9c541725 7558 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7559
7560 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7561
fcd3b13d 7562 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7563 if (use_existing_cu && this_cu->cu != NULL)
7564 {
7565 cu = this_cu->cu;
42e7ad6c
DE
7566 /* If this CU is from a DWO file we need to start over, we need to
7567 refetch the attributes from the skeleton CU.
7568 This could be optimized by retrieving those attributes from when we
7569 were here the first time: the previous comp_unit_die was stored in
7570 comp_unit_obstack. But there's no data yet that we need this
7571 optimization. */
7572 if (cu->dwo_unit != NULL)
7573 rereading_dwo_cu = 1;
dee91e82
DE
7574 }
7575 else
7576 {
7577 /* If !use_existing_cu, this_cu->cu must be NULL. */
7578 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7579 new_cu.reset (new dwarf2_cu (this_cu));
7580 cu = new_cu.get ();
42e7ad6c 7581 }
dee91e82 7582
b0c7bfa9 7583 /* Get the header. */
9c541725 7584 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7585 {
7586 /* We already have the header, there's no need to read it in again. */
9c541725 7587 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7588 }
7589 else
7590 {
3019eac3 7591 if (this_cu->is_debug_types)
dee91e82 7592 {
ed2dc618
SM
7593 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7594 &cu->header, section,
4bdcc0c1 7595 abbrev_section, info_ptr,
43988095 7596 rcuh_kind::TYPE);
dee91e82 7597
42e7ad6c
DE
7598 /* Since per_cu is the first member of struct signatured_type,
7599 we can go from a pointer to one to a pointer to the other. */
7600 sig_type = (struct signatured_type *) this_cu;
43988095 7601 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7602 gdb_assert (sig_type->type_offset_in_tu
7603 == cu->header.type_cu_offset_in_tu);
7604 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7605
42e7ad6c
DE
7606 /* LENGTH has not been set yet for type units if we're
7607 using .gdb_index. */
1ce1cefd 7608 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7609
7610 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7611 sig_type->type_offset_in_section =
7612 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7613
7614 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7615 }
7616 else
7617 {
ed2dc618
SM
7618 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7619 &cu->header, section,
4bdcc0c1 7620 abbrev_section,
43988095
JK
7621 info_ptr,
7622 rcuh_kind::COMPILE);
dee91e82 7623
9c541725 7624 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7625 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7626 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7627 }
7628 }
10b3939b 7629
6caca83c 7630 /* Skip dummy compilation units. */
dee91e82 7631 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7632 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7633 return;
6caca83c 7634
433df2d4
DE
7635 /* If we don't have them yet, read the abbrevs for this compilation unit.
7636 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7637 done (own the table through ABBREV_TABLE_HOLDER). */
7638 abbrev_table_up abbrev_table_holder;
f4dc4d17 7639 if (abbrev_table != NULL)
685af9cd
TT
7640 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7641 else
f4dc4d17 7642 {
685af9cd
TT
7643 abbrev_table_holder
7644 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7645 cu->header.abbrev_sect_off);
7646 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7647 }
af703f96 7648
dee91e82 7649 /* Read the top level CU/TU die. */
685af9cd 7650 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7651 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7652
58f0c718
TT
7653 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7654 return;
7655
b0c7bfa9 7656 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7657 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7658 table from the DWO file and pass the ownership over to us. It will be
7659 referenced from READER, so we must make sure to free it after we're done
7660 with READER.
7661
b0c7bfa9
DE
7662 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7663 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7664 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7665 abbrev_table_up dwo_abbrev_table;
a084a2a6 7666 if (dwo_name != nullptr)
3019eac3 7667 {
3019eac3 7668 struct dwo_unit *dwo_unit;
b0c7bfa9 7669 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7670
7671 if (has_children)
6a506a2d 7672 {
b98664d3 7673 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7674 " has children (offset %s) [in module %s]"),
7675 sect_offset_str (this_cu->sect_off),
7676 bfd_get_filename (abfd));
6a506a2d 7677 }
b0c7bfa9 7678 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7679 if (dwo_unit != NULL)
3019eac3 7680 {
6a506a2d 7681 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7682 comp_unit_die, NULL,
6a506a2d 7683 &reader, &info_ptr,
685af9cd
TT
7684 &dwo_comp_unit_die, &has_children,
7685 &dwo_abbrev_table) == 0)
6a506a2d
DE
7686 {
7687 /* Dummy die. */
6a506a2d
DE
7688 return;
7689 }
7690 comp_unit_die = dwo_comp_unit_die;
7691 }
7692 else
7693 {
7694 /* Yikes, we couldn't find the rest of the DIE, we only have
7695 the stub. A complaint has already been logged. There's
7696 not much more we can do except pass on the stub DIE to
7697 die_reader_func. We don't want to throw an error on bad
7698 debug info. */
3019eac3
DE
7699 }
7700 }
7701
b0c7bfa9 7702 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7703 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7704
b0c7bfa9 7705 /* Done, clean up. */
fcd3b13d 7706 if (new_cu != NULL && keep)
348e048f 7707 {
fcd3b13d
SM
7708 /* Link this CU into read_in_chain. */
7709 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7710 dwarf2_per_objfile->read_in_chain = this_cu;
7711 /* The chain owns it now. */
7712 new_cu.release ();
348e048f 7713 }
dee91e82
DE
7714}
7715
33e80786
DE
7716/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7717 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7718 to have already done the lookup to find the DWO file).
dee91e82
DE
7719
7720 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7721 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7722
7723 We fill in THIS_CU->length.
7724
7725 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7726 linker) then DIE_READER_FUNC will not get called.
7727
7728 THIS_CU->cu is always freed when done.
3019eac3
DE
7729 This is done in order to not leave THIS_CU->cu in a state where we have
7730 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7731
7732static void
7733init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7734 struct dwo_file *dwo_file,
dee91e82
DE
7735 die_reader_func_ftype *die_reader_func,
7736 void *data)
7737{
ed2dc618 7738 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7739 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7740 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7741 bfd *abfd = get_section_bfd_owner (section);
33e80786 7742 struct dwarf2_section_info *abbrev_section;
d521ce57 7743 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7744 struct die_reader_specs reader;
dee91e82
DE
7745 struct die_info *comp_unit_die;
7746 int has_children;
7747
b4f54984 7748 if (dwarf_die_debug)
9d8780f0 7749 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7750 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7751 sect_offset_str (this_cu->sect_off));
09406207 7752
dee91e82
DE
7753 gdb_assert (this_cu->cu == NULL);
7754
33e80786
DE
7755 abbrev_section = (dwo_file != NULL
7756 ? &dwo_file->sections.abbrev
7757 : get_abbrev_section_for_cu (this_cu));
7758
dee91e82
DE
7759 /* This is cheap if the section is already read in. */
7760 dwarf2_read_section (objfile, section);
7761
fcd3b13d 7762 struct dwarf2_cu cu (this_cu);
dee91e82 7763
9c541725 7764 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7765 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7766 &cu.header, section,
4bdcc0c1 7767 abbrev_section, info_ptr,
43988095
JK
7768 (this_cu->is_debug_types
7769 ? rcuh_kind::TYPE
7770 : rcuh_kind::COMPILE));
dee91e82 7771
1ce1cefd 7772 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7773
7774 /* Skip dummy compilation units. */
7775 if (info_ptr >= begin_info_ptr + this_cu->length
7776 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7777 return;
72bf9492 7778
685af9cd
TT
7779 abbrev_table_up abbrev_table
7780 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7781 cu.header.abbrev_sect_off);
dee91e82 7782
685af9cd 7783 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7784 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7785
7786 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7787}
7788
3019eac3
DE
7789/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7790 does not lookup the specified DWO file.
7791 This cannot be used to read DWO files.
dee91e82
DE
7792
7793 THIS_CU->cu is always freed when done.
3019eac3
DE
7794 This is done in order to not leave THIS_CU->cu in a state where we have
7795 to care whether it refers to the "main" CU or the DWO CU.
7796 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7797
7798static void
7799init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7800 die_reader_func_ftype *die_reader_func,
7801 void *data)
7802{
33e80786 7803 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7804}
0018ea6f
DE
7805\f
7806/* Type Unit Groups.
dee91e82 7807
0018ea6f
DE
7808 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7809 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7810 so that all types coming from the same compilation (.o file) are grouped
7811 together. A future step could be to put the types in the same symtab as
7812 the CU the types ultimately came from. */
ff013f42 7813
f4dc4d17
DE
7814static hashval_t
7815hash_type_unit_group (const void *item)
7816{
9a3c8263
SM
7817 const struct type_unit_group *tu_group
7818 = (const struct type_unit_group *) item;
f4dc4d17 7819
094b34ac 7820 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7821}
348e048f
DE
7822
7823static int
f4dc4d17 7824eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7825{
9a3c8263
SM
7826 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7827 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7828
094b34ac 7829 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7830}
348e048f 7831
f4dc4d17
DE
7832/* Allocate a hash table for type unit groups. */
7833
7834static htab_t
ed2dc618 7835allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7836{
7837 return htab_create_alloc_ex (3,
7838 hash_type_unit_group,
7839 eq_type_unit_group,
7840 NULL,
ed2dc618 7841 &objfile->objfile_obstack,
f4dc4d17
DE
7842 hashtab_obstack_allocate,
7843 dummy_obstack_deallocate);
7844}
dee91e82 7845
f4dc4d17
DE
7846/* Type units that don't have DW_AT_stmt_list are grouped into their own
7847 partial symtabs. We combine several TUs per psymtab to not let the size
7848 of any one psymtab grow too big. */
7849#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7850#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7851
094b34ac 7852/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7853 Create the type_unit_group object used to hold one or more TUs. */
7854
7855static struct type_unit_group *
094b34ac 7856create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7857{
518817b3
SM
7858 struct dwarf2_per_objfile *dwarf2_per_objfile
7859 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7860 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7861 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7862 struct type_unit_group *tu_group;
f4dc4d17
DE
7863
7864 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7865 struct type_unit_group);
094b34ac 7866 per_cu = &tu_group->per_cu;
518817b3 7867 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7868
094b34ac
DE
7869 if (dwarf2_per_objfile->using_index)
7870 {
7871 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7872 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7873 }
7874 else
7875 {
9c541725 7876 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7877 struct partial_symtab *pst;
528e1572 7878 std::string name;
094b34ac
DE
7879
7880 /* Give the symtab a useful name for debug purposes. */
7881 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7882 name = string_printf ("<type_units_%d>",
7883 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7884 else
528e1572 7885 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7886
528e1572 7887 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7888 pst->anonymous = 1;
094b34ac 7889 }
f4dc4d17 7890
094b34ac 7891 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7892 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7893
7894 return tu_group;
7895}
7896
094b34ac
DE
7897/* Look up the type_unit_group for type unit CU, and create it if necessary.
7898 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7899
7900static struct type_unit_group *
ff39bb5e 7901get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7902{
518817b3
SM
7903 struct dwarf2_per_objfile *dwarf2_per_objfile
7904 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7905 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7906 struct type_unit_group *tu_group;
7907 void **slot;
7908 unsigned int line_offset;
7909 struct type_unit_group type_unit_group_for_lookup;
7910
7911 if (dwarf2_per_objfile->type_unit_groups == NULL)
7912 {
7913 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7914 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7915 }
7916
7917 /* Do we need to create a new group, or can we use an existing one? */
7918
7919 if (stmt_list)
7920 {
7921 line_offset = DW_UNSND (stmt_list);
7922 ++tu_stats->nr_symtab_sharers;
7923 }
7924 else
7925 {
7926 /* Ugh, no stmt_list. Rare, but we have to handle it.
7927 We can do various things here like create one group per TU or
7928 spread them over multiple groups to split up the expansion work.
7929 To avoid worst case scenarios (too many groups or too large groups)
7930 we, umm, group them in bunches. */
7931 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7932 | (tu_stats->nr_stmt_less_type_units
7933 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7934 ++tu_stats->nr_stmt_less_type_units;
7935 }
7936
094b34ac 7937 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7938 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7939 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7940 &type_unit_group_for_lookup, INSERT);
7941 if (*slot != NULL)
7942 {
9a3c8263 7943 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7944 gdb_assert (tu_group != NULL);
7945 }
7946 else
7947 {
9c541725 7948 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7949 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7950 *slot = tu_group;
7951 ++tu_stats->nr_symtabs;
7952 }
7953
7954 return tu_group;
7955}
0018ea6f
DE
7956\f
7957/* Partial symbol tables. */
7958
7959/* Create a psymtab named NAME and assign it to PER_CU.
7960
7961 The caller must fill in the following details:
7962 dirname, textlow, texthigh. */
7963
7964static struct partial_symtab *
7965create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7966{
e3b94546 7967 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7968 struct partial_symtab *pst;
7969
939652a5 7970 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7971
7972 pst->psymtabs_addrmap_supported = 1;
7973
7974 /* This is the glue that links PST into GDB's symbol API. */
7975 pst->read_symtab_private = per_cu;
7976 pst->read_symtab = dwarf2_read_symtab;
7977 per_cu->v.psymtab = pst;
7978
7979 return pst;
7980}
7981
b93601f3
TT
7982/* The DATA object passed to process_psymtab_comp_unit_reader has this
7983 type. */
7984
7985struct process_psymtab_comp_unit_data
7986{
7987 /* True if we are reading a DW_TAG_partial_unit. */
7988
7989 int want_partial_unit;
7990
7991 /* The "pretend" language that is used if the CU doesn't declare a
7992 language. */
7993
7994 enum language pretend_language;
7995};
7996
0018ea6f
DE
7997/* die_reader_func for process_psymtab_comp_unit. */
7998
7999static void
8000process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8001 const gdb_byte *info_ptr,
0018ea6f
DE
8002 struct die_info *comp_unit_die,
8003 int has_children,
8004 void *data)
8005{
8006 struct dwarf2_cu *cu = reader->cu;
518817b3 8007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8009 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8010 CORE_ADDR baseaddr;
8011 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8012 struct partial_symtab *pst;
3a2b436a 8013 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8014 const char *filename;
9a3c8263
SM
8015 struct process_psymtab_comp_unit_data *info
8016 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8017
b93601f3 8018 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8019 return;
8020
8021 gdb_assert (! per_cu->is_debug_types);
8022
b93601f3 8023 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8024
0018ea6f 8025 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8026 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8027 if (filename == NULL)
0018ea6f 8028 filename = "";
0018ea6f
DE
8029
8030 pst = create_partial_symtab (per_cu, filename);
8031
8032 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8033 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8034
8035 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8036
8037 dwarf2_find_base_address (comp_unit_die, cu);
8038
8039 /* Possibly set the default values of LOWPC and HIGHPC from
8040 `DW_AT_ranges'. */
3a2b436a
JK
8041 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8042 &best_highpc, cu, pst);
8043 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8044 {
8045 CORE_ADDR low
8046 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8047 - baseaddr);
8048 CORE_ADDR high
8049 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8050 - baseaddr - 1);
8051 /* Store the contiguous range if it is not empty; it can be
8052 empty for CUs with no code. */
d320c2b5
TT
8053 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8054 low, high, pst);
79748972 8055 }
0018ea6f
DE
8056
8057 /* Check if comp unit has_children.
8058 If so, read the rest of the partial symbols from this comp unit.
8059 If not, there's no more debug_info for this comp unit. */
8060 if (has_children)
8061 {
8062 struct partial_die_info *first_die;
8063 CORE_ADDR lowpc, highpc;
8064
8065 lowpc = ((CORE_ADDR) -1);
8066 highpc = ((CORE_ADDR) 0);
8067
8068 first_die = load_partial_dies (reader, info_ptr, 1);
8069
8070 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8071 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8072
8073 /* If we didn't find a lowpc, set it to highpc to avoid
8074 complaints from `maint check'. */
8075 if (lowpc == ((CORE_ADDR) -1))
8076 lowpc = highpc;
8077
8078 /* If the compilation unit didn't have an explicit address range,
8079 then use the information extracted from its child dies. */
e385593e 8080 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8081 {
8082 best_lowpc = lowpc;
8083 best_highpc = highpc;
8084 }
8085 }
4ae976d1 8086 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8087 best_lowpc + baseaddr)
8088 - baseaddr);
4ae976d1 8089 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8090 best_highpc + baseaddr)
8091 - baseaddr);
0018ea6f 8092
8763cede 8093 end_psymtab_common (objfile, pst);
0018ea6f
DE
8094
8095 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8096 {
8097 int i;
8098 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8099 struct dwarf2_per_cu_data *iter;
8100
8101 /* Fill in 'dependencies' here; we fill in 'users' in a
8102 post-pass. */
8103 pst->number_of_dependencies = len;
a9342b62
TT
8104 pst->dependencies
8105 = objfile->partial_symtabs->allocate_dependencies (len);
0018ea6f
DE
8106 for (i = 0;
8107 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8108 i, iter);
8109 ++i)
8110 pst->dependencies[i] = iter->v.psymtab;
8111
8112 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8113 }
8114
8115 /* Get the list of files included in the current compilation unit,
8116 and build a psymtab for each of them. */
8117 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8118
b4f54984 8119 if (dwarf_read_debug)
b926417a
TT
8120 fprintf_unfiltered (gdb_stdlog,
8121 "Psymtab for %s unit @%s: %s - %s"
8122 ", %d global, %d static syms\n",
8123 per_cu->is_debug_types ? "type" : "comp",
8124 sect_offset_str (per_cu->sect_off),
8125 paddress (gdbarch, pst->text_low (objfile)),
8126 paddress (gdbarch, pst->text_high (objfile)),
8127 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8128}
8129
8130/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8131 Process compilation unit THIS_CU for a psymtab. */
8132
8133static void
8134process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8135 int want_partial_unit,
8136 enum language pretend_language)
0018ea6f
DE
8137{
8138 /* If this compilation unit was already read in, free the
8139 cached copy in order to read it in again. This is
8140 necessary because we skipped some symbols when we first
8141 read in the compilation unit (see load_partial_dies).
8142 This problem could be avoided, but the benefit is unclear. */
8143 if (this_cu->cu != NULL)
8144 free_one_cached_comp_unit (this_cu);
8145
f1902523 8146 if (this_cu->is_debug_types)
58f0c718
TT
8147 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8148 build_type_psymtabs_reader, NULL);
f1902523
JK
8149 else
8150 {
8151 process_psymtab_comp_unit_data info;
8152 info.want_partial_unit = want_partial_unit;
8153 info.pretend_language = pretend_language;
58f0c718 8154 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8155 process_psymtab_comp_unit_reader, &info);
8156 }
0018ea6f
DE
8157
8158 /* Age out any secondary CUs. */
ed2dc618 8159 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8160}
f4dc4d17
DE
8161
8162/* Reader function for build_type_psymtabs. */
8163
8164static void
8165build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8166 const gdb_byte *info_ptr,
f4dc4d17
DE
8167 struct die_info *type_unit_die,
8168 int has_children,
8169 void *data)
8170{
ed2dc618 8171 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8172 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8173 struct objfile *objfile = dwarf2_per_objfile->objfile;
8174 struct dwarf2_cu *cu = reader->cu;
8175 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8176 struct signatured_type *sig_type;
f4dc4d17
DE
8177 struct type_unit_group *tu_group;
8178 struct attribute *attr;
8179 struct partial_die_info *first_die;
8180 CORE_ADDR lowpc, highpc;
8181 struct partial_symtab *pst;
8182
8183 gdb_assert (data == NULL);
0186c6a7
DE
8184 gdb_assert (per_cu->is_debug_types);
8185 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8186
8187 if (! has_children)
8188 return;
8189
8190 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8191 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8192
df07e2c7 8193 if (tu_group->tus == nullptr)
a8b3b8e9 8194 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8195 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8196
8197 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8198 pst = create_partial_symtab (per_cu, "");
8199 pst->anonymous = 1;
8200
8201 first_die = load_partial_dies (reader, info_ptr, 1);
8202
8203 lowpc = (CORE_ADDR) -1;
8204 highpc = (CORE_ADDR) 0;
8205 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8206
8763cede 8207 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8208}
8209
73051182
DE
8210/* Struct used to sort TUs by their abbreviation table offset. */
8211
8212struct tu_abbrev_offset
8213{
b2bdb8cf
SM
8214 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8215 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8216 {}
8217
8218 signatured_type *sig_type;
73051182
DE
8219 sect_offset abbrev_offset;
8220};
8221
484cf504 8222/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8223
484cf504
TT
8224static bool
8225sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8226 const struct tu_abbrev_offset &b)
73051182 8227{
484cf504 8228 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8229}
8230
8231/* Efficiently read all the type units.
8232 This does the bulk of the work for build_type_psymtabs.
8233
8234 The efficiency is because we sort TUs by the abbrev table they use and
8235 only read each abbrev table once. In one program there are 200K TUs
8236 sharing 8K abbrev tables.
8237
8238 The main purpose of this function is to support building the
8239 dwarf2_per_objfile->type_unit_groups table.
8240 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8241 can collapse the search space by grouping them by stmt_list.
8242 The savings can be significant, in the same program from above the 200K TUs
8243 share 8K stmt_list tables.
8244
8245 FUNC is expected to call get_type_unit_group, which will create the
8246 struct type_unit_group if necessary and add it to
8247 dwarf2_per_objfile->type_unit_groups. */
8248
8249static void
ed2dc618 8250build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8251{
73051182 8252 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8253 abbrev_table_up abbrev_table;
73051182 8254 sect_offset abbrev_offset;
73051182
DE
8255
8256 /* It's up to the caller to not call us multiple times. */
8257 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8258
b2bdb8cf 8259 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8260 return;
8261
8262 /* TUs typically share abbrev tables, and there can be way more TUs than
8263 abbrev tables. Sort by abbrev table to reduce the number of times we
8264 read each abbrev table in.
8265 Alternatives are to punt or to maintain a cache of abbrev tables.
8266 This is simpler and efficient enough for now.
8267
8268 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8269 symtab to use). Typically TUs with the same abbrev offset have the same
8270 stmt_list value too so in practice this should work well.
8271
8272 The basic algorithm here is:
8273
8274 sort TUs by abbrev table
8275 for each TU with same abbrev table:
8276 read abbrev table if first user
8277 read TU top level DIE
8278 [IWBN if DWO skeletons had DW_AT_stmt_list]
8279 call FUNC */
8280
b4f54984 8281 if (dwarf_read_debug)
73051182
DE
8282 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8283
8284 /* Sort in a separate table to maintain the order of all_type_units
8285 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8286 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8287 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8288
8289 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8290 sorted_by_abbrev.emplace_back
8291 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8292 sig_type->per_cu.section,
8293 sig_type->per_cu.sect_off));
73051182 8294
484cf504
TT
8295 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8296 sort_tu_by_abbrev_offset);
73051182 8297
9c541725 8298 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8299
b2bdb8cf 8300 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8301 {
73051182
DE
8302 /* Switch to the next abbrev table if necessary. */
8303 if (abbrev_table == NULL
b2bdb8cf 8304 || tu.abbrev_offset != abbrev_offset)
73051182 8305 {
b2bdb8cf 8306 abbrev_offset = tu.abbrev_offset;
73051182 8307 abbrev_table =
ed2dc618
SM
8308 abbrev_table_read_table (dwarf2_per_objfile,
8309 &dwarf2_per_objfile->abbrev,
73051182
DE
8310 abbrev_offset);
8311 ++tu_stats->nr_uniq_abbrev_tables;
8312 }
8313
b2bdb8cf 8314 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8315 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8316 }
6aa5f3a6 8317}
73051182 8318
6aa5f3a6
DE
8319/* Print collected type unit statistics. */
8320
8321static void
ed2dc618 8322print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8323{
8324 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8325
8326 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8327 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8328 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8329 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8330 tu_stats->nr_uniq_abbrev_tables);
8331 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8332 tu_stats->nr_symtabs);
8333 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8334 tu_stats->nr_symtab_sharers);
8335 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8336 tu_stats->nr_stmt_less_type_units);
8337 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8338 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8339}
8340
f4dc4d17
DE
8341/* Traversal function for build_type_psymtabs. */
8342
8343static int
8344build_type_psymtab_dependencies (void **slot, void *info)
8345{
ed2dc618
SM
8346 struct dwarf2_per_objfile *dwarf2_per_objfile
8347 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8348 struct objfile *objfile = dwarf2_per_objfile->objfile;
8349 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8350 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8351 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8352 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8353 int i;
8354
8355 gdb_assert (len > 0);
0186c6a7 8356 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8357
8358 pst->number_of_dependencies = len;
a9342b62 8359 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8360 for (i = 0; i < len; ++i)
f4dc4d17 8361 {
df07e2c7 8362 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8363 gdb_assert (iter->per_cu.is_debug_types);
8364 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8365 iter->type_unit_group = tu_group;
f4dc4d17
DE
8366 }
8367
df07e2c7
AB
8368 delete tu_group->tus;
8369 tu_group->tus = nullptr;
348e048f
DE
8370
8371 return 1;
8372}
8373
8374/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8375 Build partial symbol tables for the .debug_types comp-units. */
8376
8377static void
ed2dc618 8378build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8379{
ed2dc618 8380 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8381 return;
8382
ed2dc618 8383 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8384}
f4dc4d17 8385
6aa5f3a6
DE
8386/* Traversal function for process_skeletonless_type_unit.
8387 Read a TU in a DWO file and build partial symbols for it. */
8388
8389static int
8390process_skeletonless_type_unit (void **slot, void *info)
8391{
8392 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8393 struct dwarf2_per_objfile *dwarf2_per_objfile
8394 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8395 struct signatured_type find_entry, *entry;
8396
8397 /* If this TU doesn't exist in the global table, add it and read it in. */
8398
8399 if (dwarf2_per_objfile->signatured_types == NULL)
8400 {
8401 dwarf2_per_objfile->signatured_types
ed2dc618 8402 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8403 }
8404
8405 find_entry.signature = dwo_unit->signature;
8406 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8407 INSERT);
8408 /* If we've already seen this type there's nothing to do. What's happening
8409 is we're doing our own version of comdat-folding here. */
8410 if (*slot != NULL)
8411 return 1;
8412
8413 /* This does the job that create_all_type_units would have done for
8414 this TU. */
ed2dc618
SM
8415 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8416 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8417 *slot = entry;
8418
8419 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8420 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8421 build_type_psymtabs_reader, NULL);
8422
8423 return 1;
8424}
8425
8426/* Traversal function for process_skeletonless_type_units. */
8427
8428static int
8429process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8430{
8431 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8432
8433 if (dwo_file->tus != NULL)
8434 {
8435 htab_traverse_noresize (dwo_file->tus,
8436 process_skeletonless_type_unit, info);
8437 }
8438
8439 return 1;
8440}
8441
8442/* Scan all TUs of DWO files, verifying we've processed them.
8443 This is needed in case a TU was emitted without its skeleton.
8444 Note: This can't be done until we know what all the DWO files are. */
8445
8446static void
ed2dc618 8447process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8448{
8449 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8450 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8451 && dwarf2_per_objfile->dwo_files != NULL)
8452 {
51ac9db5 8453 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8454 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8455 dwarf2_per_objfile);
6aa5f3a6 8456 }
348e048f
DE
8457}
8458
ed2dc618 8459/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8460
8461static void
ed2dc618 8462set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8463{
b76e467d 8464 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8465 {
95554aad 8466 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8467
36586728
TT
8468 if (pst == NULL)
8469 continue;
8470
b76e467d 8471 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8472 {
8473 /* Set the 'user' field only if it is not already set. */
8474 if (pst->dependencies[j]->user == NULL)
8475 pst->dependencies[j]->user = pst;
8476 }
8477 }
8478}
8479
93311388
DE
8480/* Build the partial symbol table by doing a quick pass through the
8481 .debug_info and .debug_abbrev sections. */
72bf9492 8482
93311388 8483static void
ed2dc618 8484dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8485{
ed2dc618 8486 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8487
b4f54984 8488 if (dwarf_read_debug)
45cfd468
DE
8489 {
8490 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8491 objfile_name (objfile));
45cfd468
DE
8492 }
8493
98bfdba5
PA
8494 dwarf2_per_objfile->reading_partial_symbols = 1;
8495
be391dca 8496 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8497
93311388
DE
8498 /* Any cached compilation units will be linked by the per-objfile
8499 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8500 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8501
ed2dc618 8502 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8503
ed2dc618 8504 create_all_comp_units (dwarf2_per_objfile);
c906108c 8505
60606b2c
TT
8506 /* Create a temporary address map on a temporary obstack. We later
8507 copy this to the final obstack. */
8268c778 8508 auto_obstack temp_obstack;
791afaa2
TT
8509
8510 scoped_restore save_psymtabs_addrmap
d320c2b5 8511 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8512 addrmap_create_mutable (&temp_obstack));
72bf9492 8513
b76e467d
SM
8514 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8515 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8516
6aa5f3a6 8517 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8518 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8519
8520 /* Now that all TUs have been processed we can fill in the dependencies. */
8521 if (dwarf2_per_objfile->type_unit_groups != NULL)
8522 {
8523 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8524 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8525 }
8526
b4f54984 8527 if (dwarf_read_debug)
ed2dc618 8528 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8529
ed2dc618 8530 set_partial_user (dwarf2_per_objfile);
95554aad 8531
d320c2b5
TT
8532 objfile->partial_symtabs->psymtabs_addrmap
8533 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8534 objfile->partial_symtabs->obstack ());
791afaa2
TT
8535 /* At this point we want to keep the address map. */
8536 save_psymtabs_addrmap.release ();
ff013f42 8537
b4f54984 8538 if (dwarf_read_debug)
45cfd468 8539 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8540 objfile_name (objfile));
ae038cb0
DJ
8541}
8542
3019eac3 8543/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8544
8545static void
dee91e82 8546load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8547 const gdb_byte *info_ptr,
dee91e82
DE
8548 struct die_info *comp_unit_die,
8549 int has_children,
8550 void *data)
ae038cb0 8551{
dee91e82 8552 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8553
95554aad 8554 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8555
ae038cb0
DJ
8556 /* Check if comp unit has_children.
8557 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8558 If not, there's no more debug_info for this comp unit. */
d85a05f0 8559 if (has_children)
dee91e82
DE
8560 load_partial_dies (reader, info_ptr, 0);
8561}
98bfdba5 8562
dee91e82
DE
8563/* Load the partial DIEs for a secondary CU into memory.
8564 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8565
dee91e82
DE
8566static void
8567load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8568{
58f0c718 8569 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8570 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8571}
8572
ae038cb0 8573static void
ed2dc618 8574read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8575 struct dwarf2_section_info *section,
f1902523 8576 struct dwarf2_section_info *abbrev_section,
b76e467d 8577 unsigned int is_dwz)
ae038cb0 8578{
d521ce57 8579 const gdb_byte *info_ptr;
ed2dc618 8580 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8581
b4f54984 8582 if (dwarf_read_debug)
bf6af496 8583 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8584 get_section_name (section),
8585 get_section_file_name (section));
bf6af496 8586
36586728 8587 dwarf2_read_section (objfile, section);
ae038cb0 8588
36586728 8589 info_ptr = section->buffer;
6e70227d 8590
36586728 8591 while (info_ptr < section->buffer + section->size)
ae038cb0 8592 {
ae038cb0 8593 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8594
9c541725 8595 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8596
f1902523 8597 comp_unit_head cu_header;
ed2dc618
SM
8598 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8599 abbrev_section, info_ptr,
8600 rcuh_kind::COMPILE);
ae038cb0
DJ
8601
8602 /* Save the compilation unit for later lookup. */
f1902523
JK
8603 if (cu_header.unit_type != DW_UT_type)
8604 {
8605 this_cu = XOBNEW (&objfile->objfile_obstack,
8606 struct dwarf2_per_cu_data);
8607 memset (this_cu, 0, sizeof (*this_cu));
8608 }
8609 else
8610 {
8611 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8612 struct signatured_type);
8613 memset (sig_type, 0, sizeof (*sig_type));
8614 sig_type->signature = cu_header.signature;
8615 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8616 this_cu = &sig_type->per_cu;
8617 }
8618 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8619 this_cu->sect_off = sect_off;
f1902523 8620 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8621 this_cu->is_dwz = is_dwz;
e3b94546 8622 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8623 this_cu->section = section;
ae038cb0 8624
b76e467d 8625 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8626
8627 info_ptr = info_ptr + this_cu->length;
8628 }
36586728
TT
8629}
8630
8631/* Create a list of all compilation units in OBJFILE.
8632 This is only done for -readnow and building partial symtabs. */
8633
8634static void
ed2dc618 8635create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8636{
b76e467d 8637 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8638 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8639 &dwarf2_per_objfile->abbrev, 0);
36586728 8640
b76e467d 8641 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8642 if (dwz != NULL)
ed2dc618 8643 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8644 1);
c906108c
SS
8645}
8646
5734ee8b 8647/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8648 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8649 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8650 DW_AT_ranges). See the comments of add_partial_subprogram on how
8651 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8652
72bf9492
DJ
8653static void
8654scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8655 CORE_ADDR *highpc, int set_addrmap,
8656 struct dwarf2_cu *cu)
c906108c 8657{
72bf9492 8658 struct partial_die_info *pdi;
c906108c 8659
91c24f0a
DC
8660 /* Now, march along the PDI's, descending into ones which have
8661 interesting children but skipping the children of the other ones,
8662 until we reach the end of the compilation unit. */
c906108c 8663
72bf9492 8664 pdi = first_die;
91c24f0a 8665
72bf9492
DJ
8666 while (pdi != NULL)
8667 {
52356b79 8668 pdi->fixup (cu);
c906108c 8669
f55ee35c 8670 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8671 children, so we need to look at them. Ditto for anonymous
8672 enums. */
933c6fe4 8673
72bf9492 8674 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8675 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8676 || pdi->tag == DW_TAG_imported_unit
8677 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8678 {
72bf9492 8679 switch (pdi->tag)
c906108c
SS
8680 {
8681 case DW_TAG_subprogram:
b1dc1806 8682 case DW_TAG_inlined_subroutine:
cdc07690 8683 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8684 break;
72929c62 8685 case DW_TAG_constant:
c906108c
SS
8686 case DW_TAG_variable:
8687 case DW_TAG_typedef:
91c24f0a 8688 case DW_TAG_union_type:
72bf9492 8689 if (!pdi->is_declaration)
63d06c5c 8690 {
72bf9492 8691 add_partial_symbol (pdi, cu);
63d06c5c
DC
8692 }
8693 break;
c906108c 8694 case DW_TAG_class_type:
680b30c7 8695 case DW_TAG_interface_type:
c906108c 8696 case DW_TAG_structure_type:
72bf9492 8697 if (!pdi->is_declaration)
c906108c 8698 {
72bf9492 8699 add_partial_symbol (pdi, cu);
c906108c 8700 }
b7fee5a3
KS
8701 if ((cu->language == language_rust
8702 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8703 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8704 set_addrmap, cu);
c906108c 8705 break;
91c24f0a 8706 case DW_TAG_enumeration_type:
72bf9492
DJ
8707 if (!pdi->is_declaration)
8708 add_partial_enumeration (pdi, cu);
c906108c
SS
8709 break;
8710 case DW_TAG_base_type:
a02abb62 8711 case DW_TAG_subrange_type:
c906108c 8712 /* File scope base type definitions are added to the partial
c5aa993b 8713 symbol table. */
72bf9492 8714 add_partial_symbol (pdi, cu);
c906108c 8715 break;
d9fa45fe 8716 case DW_TAG_namespace:
cdc07690 8717 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8718 break;
5d7cb8df 8719 case DW_TAG_module:
cdc07690 8720 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8721 break;
95554aad
TT
8722 case DW_TAG_imported_unit:
8723 {
8724 struct dwarf2_per_cu_data *per_cu;
8725
f4dc4d17
DE
8726 /* For now we don't handle imported units in type units. */
8727 if (cu->per_cu->is_debug_types)
8728 {
8729 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8730 " supported in type units [in module %s]"),
518817b3 8731 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8732 }
8733
e3b94546
SM
8734 per_cu = dwarf2_find_containing_comp_unit
8735 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8736 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8737
8738 /* Go read the partial unit, if needed. */
8739 if (per_cu->v.psymtab == NULL)
b93601f3 8740 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8741
f4dc4d17 8742 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8743 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8744 }
8745 break;
74921315
KS
8746 case DW_TAG_imported_declaration:
8747 add_partial_symbol (pdi, cu);
8748 break;
c906108c
SS
8749 default:
8750 break;
8751 }
8752 }
8753
72bf9492
DJ
8754 /* If the die has a sibling, skip to the sibling. */
8755
8756 pdi = pdi->die_sibling;
8757 }
8758}
8759
8760/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8761
72bf9492 8762 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8763 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8764 Enumerators are an exception; they use the scope of their parent
8765 enumeration type, i.e. the name of the enumeration type is not
8766 prepended to the enumerator.
91c24f0a 8767
72bf9492
DJ
8768 There are two complexities. One is DW_AT_specification; in this
8769 case "parent" means the parent of the target of the specification,
8770 instead of the direct parent of the DIE. The other is compilers
8771 which do not emit DW_TAG_namespace; in this case we try to guess
8772 the fully qualified name of structure types from their members'
8773 linkage names. This must be done using the DIE's children rather
8774 than the children of any DW_AT_specification target. We only need
8775 to do this for structures at the top level, i.e. if the target of
8776 any DW_AT_specification (if any; otherwise the DIE itself) does not
8777 have a parent. */
8778
8779/* Compute the scope prefix associated with PDI's parent, in
8780 compilation unit CU. The result will be allocated on CU's
8781 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8782 field. NULL is returned if no prefix is necessary. */
15d034d0 8783static const char *
72bf9492
DJ
8784partial_die_parent_scope (struct partial_die_info *pdi,
8785 struct dwarf2_cu *cu)
8786{
15d034d0 8787 const char *grandparent_scope;
72bf9492 8788 struct partial_die_info *parent, *real_pdi;
91c24f0a 8789
72bf9492
DJ
8790 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8791 then this means the parent of the specification DIE. */
8792
8793 real_pdi = pdi;
72bf9492 8794 while (real_pdi->has_specification)
fb816e8b 8795 {
122cf0f2
AB
8796 auto res = find_partial_die (real_pdi->spec_offset,
8797 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8798 real_pdi = res.pdi;
8799 cu = res.cu;
8800 }
72bf9492
DJ
8801
8802 parent = real_pdi->die_parent;
8803 if (parent == NULL)
8804 return NULL;
8805
8806 if (parent->scope_set)
8807 return parent->scope;
8808
52356b79 8809 parent->fixup (cu);
72bf9492 8810
10b3939b 8811 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8812
acebe513
UW
8813 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8814 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8815 Work around this problem here. */
8816 if (cu->language == language_cplus
6e70227d 8817 && parent->tag == DW_TAG_namespace
acebe513
UW
8818 && strcmp (parent->name, "::") == 0
8819 && grandparent_scope == NULL)
8820 {
8821 parent->scope = NULL;
8822 parent->scope_set = 1;
8823 return NULL;
8824 }
8825
0a4b0913 8826 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8827 if (pdi->tag == DW_TAG_enumerator)
8828 /* Enumerators should not get the name of the enumeration as a prefix. */
8829 parent->scope = grandparent_scope;
8830 else if (parent->tag == DW_TAG_namespace
f55ee35c 8831 || parent->tag == DW_TAG_module
72bf9492
DJ
8832 || parent->tag == DW_TAG_structure_type
8833 || parent->tag == DW_TAG_class_type
680b30c7 8834 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8835 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8836 || parent->tag == DW_TAG_enumeration_type
8837 || (cu->language == language_fortran
8838 && parent->tag == DW_TAG_subprogram
8839 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8840 {
8841 if (grandparent_scope == NULL)
8842 parent->scope = parent->name;
8843 else
3e43a32a
MS
8844 parent->scope = typename_concat (&cu->comp_unit_obstack,
8845 grandparent_scope,
f55ee35c 8846 parent->name, 0, cu);
72bf9492 8847 }
72bf9492
DJ
8848 else
8849 {
8850 /* FIXME drow/2004-04-01: What should we be doing with
8851 function-local names? For partial symbols, we should probably be
8852 ignoring them. */
fa9c3fa0
TT
8853 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8854 dwarf_tag_name (parent->tag),
8855 sect_offset_str (pdi->sect_off));
72bf9492 8856 parent->scope = grandparent_scope;
c906108c
SS
8857 }
8858
72bf9492
DJ
8859 parent->scope_set = 1;
8860 return parent->scope;
8861}
8862
8863/* Return the fully scoped name associated with PDI, from compilation unit
8864 CU. The result will be allocated with malloc. */
4568ecf9 8865
72bf9492
DJ
8866static char *
8867partial_die_full_name (struct partial_die_info *pdi,
8868 struct dwarf2_cu *cu)
8869{
15d034d0 8870 const char *parent_scope;
72bf9492 8871
98bfdba5
PA
8872 /* If this is a template instantiation, we can not work out the
8873 template arguments from partial DIEs. So, unfortunately, we have
8874 to go through the full DIEs. At least any work we do building
8875 types here will be reused if full symbols are loaded later. */
8876 if (pdi->has_template_arguments)
8877 {
52356b79 8878 pdi->fixup (cu);
98bfdba5
PA
8879
8880 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8881 {
8882 struct die_info *die;
8883 struct attribute attr;
8884 struct dwarf2_cu *ref_cu = cu;
8885
b64f50a1 8886 /* DW_FORM_ref_addr is using section offset. */
b4069958 8887 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8888 attr.form = DW_FORM_ref_addr;
9c541725 8889 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8890 die = follow_die_ref (NULL, &attr, &ref_cu);
8891
8892 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8893 }
8894 }
8895
72bf9492
DJ
8896 parent_scope = partial_die_parent_scope (pdi, cu);
8897 if (parent_scope == NULL)
8898 return NULL;
8899 else
f55ee35c 8900 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8901}
8902
8903static void
72bf9492 8904add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8905{
518817b3
SM
8906 struct dwarf2_per_objfile *dwarf2_per_objfile
8907 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8908 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8909 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8910 CORE_ADDR addr = 0;
15d034d0 8911 const char *actual_name = NULL;
e142c38c 8912 CORE_ADDR baseaddr;
15d034d0 8913 char *built_actual_name;
e142c38c
DJ
8914
8915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8916
15d034d0
TT
8917 built_actual_name = partial_die_full_name (pdi, cu);
8918 if (built_actual_name != NULL)
8919 actual_name = built_actual_name;
63d06c5c 8920
72bf9492
DJ
8921 if (actual_name == NULL)
8922 actual_name = pdi->name;
8923
c906108c
SS
8924 switch (pdi->tag)
8925 {
b1dc1806 8926 case DW_TAG_inlined_subroutine:
c906108c 8927 case DW_TAG_subprogram:
79748972
TT
8928 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8929 - baseaddr);
0a4b0913
AB
8930 if (pdi->is_external
8931 || cu->language == language_ada
8932 || (cu->language == language_fortran
8933 && pdi->die_parent != NULL
8934 && pdi->die_parent->tag == DW_TAG_subprogram))
8935 {
8936 /* Normally, only "external" DIEs are part of the global scope.
8937 But in Ada and Fortran, we want to be able to access nested
8938 procedures globally. So all Ada and Fortran subprograms are
8939 stored in the global scope. */
f47fb265 8940 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8941 built_actual_name != NULL,
f47fb265 8942 VAR_DOMAIN, LOC_BLOCK,
79748972 8943 SECT_OFF_TEXT (objfile),
75aedd27 8944 psymbol_placement::GLOBAL,
79748972
TT
8945 addr,
8946 cu->language, objfile);
c906108c
SS
8947 }
8948 else
8949 {
f47fb265 8950 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8951 built_actual_name != NULL,
f47fb265 8952 VAR_DOMAIN, LOC_BLOCK,
79748972 8953 SECT_OFF_TEXT (objfile),
75aedd27 8954 psymbol_placement::STATIC,
1762568f 8955 addr, cu->language, objfile);
c906108c 8956 }
0c1b455e
TT
8957
8958 if (pdi->main_subprogram && actual_name != NULL)
8959 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8960 break;
72929c62 8961 case DW_TAG_constant:
75aedd27
TT
8962 add_psymbol_to_list (actual_name, strlen (actual_name),
8963 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8964 -1, (pdi->is_external
8965 ? psymbol_placement::GLOBAL
8966 : psymbol_placement::STATIC),
8967 0, cu->language, objfile);
72929c62 8968 break;
c906108c 8969 case DW_TAG_variable:
95554aad
TT
8970 if (pdi->d.locdesc)
8971 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8972
95554aad 8973 if (pdi->d.locdesc
caac4577
JG
8974 && addr == 0
8975 && !dwarf2_per_objfile->has_section_at_zero)
8976 {
8977 /* A global or static variable may also have been stripped
8978 out by the linker if unused, in which case its address
8979 will be nullified; do not add such variables into partial
8980 symbol table then. */
8981 }
8982 else if (pdi->is_external)
c906108c
SS
8983 {
8984 /* Global Variable.
8985 Don't enter into the minimal symbol tables as there is
8986 a minimal symbol table entry from the ELF symbols already.
8987 Enter into partial symbol table if it has a location
8988 descriptor or a type.
8989 If the location descriptor is missing, new_symbol will create
8990 a LOC_UNRESOLVED symbol, the address of the variable will then
8991 be determined from the minimal symbol table whenever the variable
8992 is referenced.
8993 The address for the partial symbol table entry is not
8994 used by GDB, but it comes in handy for debugging partial symbol
8995 table building. */
8996
95554aad 8997 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8998 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8999 built_actual_name != NULL,
f47fb265 9000 VAR_DOMAIN, LOC_STATIC,
79748972 9001 SECT_OFF_TEXT (objfile),
75aedd27 9002 psymbol_placement::GLOBAL,
79748972 9003 addr, cu->language, objfile);
c906108c
SS
9004 }
9005 else
9006 {
ff908ebf
AW
9007 int has_loc = pdi->d.locdesc != NULL;
9008
9009 /* Static Variable. Skip symbols whose value we cannot know (those
9010 without location descriptors or constant values). */
9011 if (!has_loc && !pdi->has_const_value)
decbce07 9012 {
15d034d0 9013 xfree (built_actual_name);
decbce07
MS
9014 return;
9015 }
ff908ebf 9016
f47fb265 9017 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9018 built_actual_name != NULL,
f47fb265 9019 VAR_DOMAIN, LOC_STATIC,
79748972 9020 SECT_OFF_TEXT (objfile),
75aedd27 9021 psymbol_placement::STATIC,
79748972 9022 has_loc ? addr : 0,
f47fb265 9023 cu->language, objfile);
c906108c
SS
9024 }
9025 break;
9026 case DW_TAG_typedef:
9027 case DW_TAG_base_type:
a02abb62 9028 case DW_TAG_subrange_type:
38d518c9 9029 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9030 built_actual_name != NULL,
79748972 9031 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9032 psymbol_placement::STATIC,
1762568f 9033 0, cu->language, objfile);
c906108c 9034 break;
74921315 9035 case DW_TAG_imported_declaration:
72bf9492
DJ
9036 case DW_TAG_namespace:
9037 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9038 built_actual_name != NULL,
79748972 9039 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9040 psymbol_placement::GLOBAL,
1762568f 9041 0, cu->language, objfile);
72bf9492 9042 break;
530e8392 9043 case DW_TAG_module:
a5fd13a9
BH
9044 /* With Fortran 77 there might be a "BLOCK DATA" module
9045 available without any name. If so, we skip the module as it
9046 doesn't bring any value. */
9047 if (actual_name != nullptr)
9048 add_psymbol_to_list (actual_name, strlen (actual_name),
9049 built_actual_name != NULL,
9050 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9051 psymbol_placement::GLOBAL,
9052 0, cu->language, objfile);
530e8392 9053 break;
c906108c 9054 case DW_TAG_class_type:
680b30c7 9055 case DW_TAG_interface_type:
c906108c
SS
9056 case DW_TAG_structure_type:
9057 case DW_TAG_union_type:
9058 case DW_TAG_enumeration_type:
fa4028e9
JB
9059 /* Skip external references. The DWARF standard says in the section
9060 about "Structure, Union, and Class Type Entries": "An incomplete
9061 structure, union or class type is represented by a structure,
9062 union or class entry that does not have a byte size attribute
9063 and that has a DW_AT_declaration attribute." */
9064 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9065 {
15d034d0 9066 xfree (built_actual_name);
decbce07
MS
9067 return;
9068 }
fa4028e9 9069
63d06c5c
DC
9070 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9071 static vs. global. */
38d518c9 9072 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9073 built_actual_name != NULL,
79748972 9074 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9075 cu->language == language_cplus
75aedd27
TT
9076 ? psymbol_placement::GLOBAL
9077 : psymbol_placement::STATIC,
1762568f 9078 0, cu->language, objfile);
c906108c 9079
c906108c
SS
9080 break;
9081 case DW_TAG_enumerator:
38d518c9 9082 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9083 built_actual_name != NULL,
79748972 9084 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9085 cu->language == language_cplus
75aedd27
TT
9086 ? psymbol_placement::GLOBAL
9087 : psymbol_placement::STATIC,
1762568f 9088 0, cu->language, objfile);
c906108c
SS
9089 break;
9090 default:
9091 break;
9092 }
5c4e30ca 9093
15d034d0 9094 xfree (built_actual_name);
c906108c
SS
9095}
9096
5c4e30ca
DC
9097/* Read a partial die corresponding to a namespace; also, add a symbol
9098 corresponding to that namespace to the symbol table. NAMESPACE is
9099 the name of the enclosing namespace. */
91c24f0a 9100
72bf9492
DJ
9101static void
9102add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9104 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9105{
72bf9492 9106 /* Add a symbol for the namespace. */
e7c27a73 9107
72bf9492 9108 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9109
9110 /* Now scan partial symbols in that namespace. */
9111
91c24f0a 9112 if (pdi->has_children)
cdc07690 9113 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9114}
9115
5d7cb8df
JK
9116/* Read a partial die corresponding to a Fortran module. */
9117
9118static void
9119add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9120 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9121{
530e8392
KB
9122 /* Add a symbol for the namespace. */
9123
9124 add_partial_symbol (pdi, cu);
9125
f55ee35c 9126 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9127
9128 if (pdi->has_children)
cdc07690 9129 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9130}
9131
b1dc1806
XR
9132/* Read a partial die corresponding to a subprogram or an inlined
9133 subprogram and create a partial symbol for that subprogram.
9134 When the CU language allows it, this routine also defines a partial
9135 symbol for each nested subprogram that this subprogram contains.
9136 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9137 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9138
cdc07690
YQ
9139 PDI may also be a lexical block, in which case we simply search
9140 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9141 Again, this is only performed when the CU language allows this
9142 type of definitions. */
9143
9144static void
9145add_partial_subprogram (struct partial_die_info *pdi,
9146 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9147 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9148{
b1dc1806 9149 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9150 {
9151 if (pdi->has_pc_info)
9152 {
9153 if (pdi->lowpc < *lowpc)
9154 *lowpc = pdi->lowpc;
9155 if (pdi->highpc > *highpc)
9156 *highpc = pdi->highpc;
cdc07690 9157 if (set_addrmap)
5734ee8b 9158 {
518817b3 9159 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9160 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9161 CORE_ADDR baseaddr;
b926417a
TT
9162 CORE_ADDR this_highpc;
9163 CORE_ADDR this_lowpc;
5734ee8b
DJ
9164
9165 baseaddr = ANOFFSET (objfile->section_offsets,
9166 SECT_OFF_TEXT (objfile));
b926417a
TT
9167 this_lowpc
9168 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9169 pdi->lowpc + baseaddr)
9170 - baseaddr);
9171 this_highpc
9172 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9173 pdi->highpc + baseaddr)
9174 - baseaddr);
d320c2b5 9175 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9176 this_lowpc, this_highpc - 1,
9291a0cd 9177 cu->per_cu->v.psymtab);
5734ee8b 9178 }
481860b3
GB
9179 }
9180
9181 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9182 {
bc30ff58 9183 if (!pdi->is_declaration)
e8d05480
JB
9184 /* Ignore subprogram DIEs that do not have a name, they are
9185 illegal. Do not emit a complaint at this point, we will
9186 do so when we convert this psymtab into a symtab. */
9187 if (pdi->name)
9188 add_partial_symbol (pdi, cu);
bc30ff58
JB
9189 }
9190 }
6e70227d 9191
bc30ff58
JB
9192 if (! pdi->has_children)
9193 return;
9194
0a4b0913 9195 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9196 {
9197 pdi = pdi->die_child;
9198 while (pdi != NULL)
9199 {
52356b79 9200 pdi->fixup (cu);
bc30ff58 9201 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9202 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9203 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9204 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9205 pdi = pdi->die_sibling;
9206 }
9207 }
9208}
9209
91c24f0a
DC
9210/* Read a partial die corresponding to an enumeration type. */
9211
72bf9492
DJ
9212static void
9213add_partial_enumeration (struct partial_die_info *enum_pdi,
9214 struct dwarf2_cu *cu)
91c24f0a 9215{
72bf9492 9216 struct partial_die_info *pdi;
91c24f0a
DC
9217
9218 if (enum_pdi->name != NULL)
72bf9492
DJ
9219 add_partial_symbol (enum_pdi, cu);
9220
9221 pdi = enum_pdi->die_child;
9222 while (pdi)
91c24f0a 9223 {
72bf9492 9224 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9225 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9226 else
72bf9492
DJ
9227 add_partial_symbol (pdi, cu);
9228 pdi = pdi->die_sibling;
91c24f0a 9229 }
91c24f0a
DC
9230}
9231
6caca83c
CC
9232/* Return the initial uleb128 in the die at INFO_PTR. */
9233
9234static unsigned int
d521ce57 9235peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9236{
9237 unsigned int bytes_read;
9238
9239 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9240}
9241
685af9cd
TT
9242/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9243 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9244
4bb7a0a7
DJ
9245 Return the corresponding abbrev, or NULL if the number is zero (indicating
9246 an empty DIE). In either case *BYTES_READ will be set to the length of
9247 the initial number. */
9248
9249static struct abbrev_info *
685af9cd
TT
9250peek_die_abbrev (const die_reader_specs &reader,
9251 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9252{
685af9cd 9253 dwarf2_cu *cu = reader.cu;
518817b3 9254 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9255 unsigned int abbrev_number
9256 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9257
9258 if (abbrev_number == 0)
9259 return NULL;
9260
685af9cd 9261 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9262 if (!abbrev)
9263 {
422b9917 9264 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9265 " at offset %s [in module %s]"),
422b9917 9266 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9267 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9268 }
9269
9270 return abbrev;
9271}
9272
93311388
DE
9273/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9274 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9275 DIE. Any children of the skipped DIEs will also be skipped. */
9276
d521ce57
TT
9277static const gdb_byte *
9278skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9279{
4bb7a0a7
DJ
9280 while (1)
9281 {
685af9cd
TT
9282 unsigned int bytes_read;
9283 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9284
4bb7a0a7
DJ
9285 if (abbrev == NULL)
9286 return info_ptr + bytes_read;
9287 else
dee91e82 9288 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9289 }
9290}
9291
93311388
DE
9292/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9293 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9294 abbrev corresponding to that skipped uleb128 should be passed in
9295 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9296 children. */
9297
d521ce57
TT
9298static const gdb_byte *
9299skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9300 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9301{
9302 unsigned int bytes_read;
9303 struct attribute attr;
dee91e82
DE
9304 bfd *abfd = reader->abfd;
9305 struct dwarf2_cu *cu = reader->cu;
d521ce57 9306 const gdb_byte *buffer = reader->buffer;
f664829e 9307 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9308 unsigned int form, i;
9309
9310 for (i = 0; i < abbrev->num_attrs; i++)
9311 {
9312 /* The only abbrev we care about is DW_AT_sibling. */
9313 if (abbrev->attrs[i].name == DW_AT_sibling)
9314 {
dee91e82 9315 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9316 if (attr.form == DW_FORM_ref_addr)
b98664d3 9317 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9318 else
b9502d3f 9319 {
9c541725
PA
9320 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9321 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9322
9323 if (sibling_ptr < info_ptr)
b98664d3 9324 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9325 else if (sibling_ptr > reader->buffer_end)
9326 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9327 else
9328 return sibling_ptr;
9329 }
4bb7a0a7
DJ
9330 }
9331
9332 /* If it isn't DW_AT_sibling, skip this attribute. */
9333 form = abbrev->attrs[i].form;
9334 skip_attribute:
9335 switch (form)
9336 {
4bb7a0a7 9337 case DW_FORM_ref_addr:
ae411497
TT
9338 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9339 and later it is offset sized. */
9340 if (cu->header.version == 2)
9341 info_ptr += cu->header.addr_size;
9342 else
9343 info_ptr += cu->header.offset_size;
9344 break;
36586728
TT
9345 case DW_FORM_GNU_ref_alt:
9346 info_ptr += cu->header.offset_size;
9347 break;
ae411497 9348 case DW_FORM_addr:
4bb7a0a7
DJ
9349 info_ptr += cu->header.addr_size;
9350 break;
9351 case DW_FORM_data1:
9352 case DW_FORM_ref1:
9353 case DW_FORM_flag:
8fe0f950 9354 case DW_FORM_strx1:
4bb7a0a7
DJ
9355 info_ptr += 1;
9356 break;
2dc7f7b3 9357 case DW_FORM_flag_present:
43988095 9358 case DW_FORM_implicit_const:
2dc7f7b3 9359 break;
4bb7a0a7
DJ
9360 case DW_FORM_data2:
9361 case DW_FORM_ref2:
8fe0f950 9362 case DW_FORM_strx2:
4bb7a0a7
DJ
9363 info_ptr += 2;
9364 break;
8fe0f950
AT
9365 case DW_FORM_strx3:
9366 info_ptr += 3;
9367 break;
4bb7a0a7
DJ
9368 case DW_FORM_data4:
9369 case DW_FORM_ref4:
8fe0f950 9370 case DW_FORM_strx4:
4bb7a0a7
DJ
9371 info_ptr += 4;
9372 break;
9373 case DW_FORM_data8:
9374 case DW_FORM_ref8:
55f1336d 9375 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9376 info_ptr += 8;
9377 break;
0224619f
JK
9378 case DW_FORM_data16:
9379 info_ptr += 16;
9380 break;
4bb7a0a7 9381 case DW_FORM_string:
9b1c24c8 9382 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9383 info_ptr += bytes_read;
9384 break;
2dc7f7b3 9385 case DW_FORM_sec_offset:
4bb7a0a7 9386 case DW_FORM_strp:
36586728 9387 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9388 info_ptr += cu->header.offset_size;
9389 break;
2dc7f7b3 9390 case DW_FORM_exprloc:
4bb7a0a7
DJ
9391 case DW_FORM_block:
9392 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9393 info_ptr += bytes_read;
9394 break;
9395 case DW_FORM_block1:
9396 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9397 break;
9398 case DW_FORM_block2:
9399 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9400 break;
9401 case DW_FORM_block4:
9402 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9403 break;
336d760d 9404 case DW_FORM_addrx:
cf532bd1 9405 case DW_FORM_strx:
4bb7a0a7
DJ
9406 case DW_FORM_sdata:
9407 case DW_FORM_udata:
9408 case DW_FORM_ref_udata:
3019eac3
DE
9409 case DW_FORM_GNU_addr_index:
9410 case DW_FORM_GNU_str_index:
d521ce57 9411 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9412 break;
9413 case DW_FORM_indirect:
9414 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9415 info_ptr += bytes_read;
9416 /* We need to continue parsing from here, so just go back to
9417 the top. */
9418 goto skip_attribute;
9419
9420 default:
3e43a32a
MS
9421 error (_("Dwarf Error: Cannot handle %s "
9422 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9423 dwarf_form_name (form),
9424 bfd_get_filename (abfd));
9425 }
9426 }
9427
9428 if (abbrev->has_children)
dee91e82 9429 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9430 else
9431 return info_ptr;
9432}
9433
93311388 9434/* Locate ORIG_PDI's sibling.
dee91e82 9435 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9436
d521ce57 9437static const gdb_byte *
dee91e82
DE
9438locate_pdi_sibling (const struct die_reader_specs *reader,
9439 struct partial_die_info *orig_pdi,
d521ce57 9440 const gdb_byte *info_ptr)
91c24f0a
DC
9441{
9442 /* Do we know the sibling already? */
72bf9492 9443
91c24f0a
DC
9444 if (orig_pdi->sibling)
9445 return orig_pdi->sibling;
9446
9447 /* Are there any children to deal with? */
9448
9449 if (!orig_pdi->has_children)
9450 return info_ptr;
9451
4bb7a0a7 9452 /* Skip the children the long way. */
91c24f0a 9453
dee91e82 9454 return skip_children (reader, info_ptr);
91c24f0a
DC
9455}
9456
257e7a09 9457/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9458 not NULL. */
c906108c
SS
9459
9460static void
257e7a09
YQ
9461dwarf2_read_symtab (struct partial_symtab *self,
9462 struct objfile *objfile)
c906108c 9463{
ed2dc618
SM
9464 struct dwarf2_per_objfile *dwarf2_per_objfile
9465 = get_dwarf2_per_objfile (objfile);
9466
257e7a09 9467 if (self->readin)
c906108c 9468 {
442e4d9c 9469 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9470 self->filename);
442e4d9c
YQ
9471 }
9472 else
9473 {
9474 if (info_verbose)
c906108c 9475 {
442e4d9c 9476 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9477 self->filename);
442e4d9c 9478 gdb_flush (gdb_stdout);
c906108c 9479 }
c906108c 9480
442e4d9c
YQ
9481 /* If this psymtab is constructed from a debug-only objfile, the
9482 has_section_at_zero flag will not necessarily be correct. We
9483 can get the correct value for this flag by looking at the data
9484 associated with the (presumably stripped) associated objfile. */
9485 if (objfile->separate_debug_objfile_backlink)
9486 {
9487 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9488 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9489
442e4d9c
YQ
9490 dwarf2_per_objfile->has_section_at_zero
9491 = dpo_backlink->has_section_at_zero;
9492 }
b2ab525c 9493
442e4d9c 9494 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9495
257e7a09 9496 psymtab_to_symtab_1 (self);
c906108c 9497
442e4d9c
YQ
9498 /* Finish up the debug error message. */
9499 if (info_verbose)
9500 printf_filtered (_("done.\n"));
c906108c 9501 }
95554aad 9502
ed2dc618 9503 process_cu_includes (dwarf2_per_objfile);
c906108c 9504}
9cdd5dbd
DE
9505\f
9506/* Reading in full CUs. */
c906108c 9507
10b3939b
DJ
9508/* Add PER_CU to the queue. */
9509
9510static void
95554aad
TT
9511queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9512 enum language pretend_language)
10b3939b
DJ
9513{
9514 struct dwarf2_queue_item *item;
9515
9516 per_cu->queued = 1;
8d749320 9517 item = XNEW (struct dwarf2_queue_item);
10b3939b 9518 item->per_cu = per_cu;
95554aad 9519 item->pretend_language = pretend_language;
10b3939b
DJ
9520 item->next = NULL;
9521
9522 if (dwarf2_queue == NULL)
9523 dwarf2_queue = item;
9524 else
9525 dwarf2_queue_tail->next = item;
9526
9527 dwarf2_queue_tail = item;
9528}
9529
89e63ee4
DE
9530/* If PER_CU is not yet queued, add it to the queue.
9531 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9532 dependency.
0907af0c 9533 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9534 meaning either PER_CU is already queued or it is already loaded.
9535
9536 N.B. There is an invariant here that if a CU is queued then it is loaded.
9537 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9538
9539static int
89e63ee4 9540maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9541 struct dwarf2_per_cu_data *per_cu,
9542 enum language pretend_language)
9543{
9544 /* We may arrive here during partial symbol reading, if we need full
9545 DIEs to process an unusual case (e.g. template arguments). Do
9546 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9547 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9548 {
9549 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9550 return 1;
9551 return 0;
9552 }
9553
9554 /* Mark the dependence relation so that we don't flush PER_CU
9555 too early. */
89e63ee4
DE
9556 if (dependent_cu != NULL)
9557 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9558
9559 /* If it's already on the queue, we have nothing to do. */
9560 if (per_cu->queued)
9561 return 0;
9562
9563 /* If the compilation unit is already loaded, just mark it as
9564 used. */
9565 if (per_cu->cu != NULL)
9566 {
9567 per_cu->cu->last_used = 0;
9568 return 0;
9569 }
9570
9571 /* Add it to the queue. */
9572 queue_comp_unit (per_cu, pretend_language);
9573
9574 return 1;
9575}
9576
10b3939b
DJ
9577/* Process the queue. */
9578
9579static void
ed2dc618 9580process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9581{
9582 struct dwarf2_queue_item *item, *next_item;
9583
b4f54984 9584 if (dwarf_read_debug)
45cfd468
DE
9585 {
9586 fprintf_unfiltered (gdb_stdlog,
9587 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9588 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9589 }
9590
03dd20cc
DJ
9591 /* The queue starts out with one item, but following a DIE reference
9592 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9593 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9594 {
cc12ce38
DE
9595 if ((dwarf2_per_objfile->using_index
9596 ? !item->per_cu->v.quick->compunit_symtab
9597 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9598 /* Skip dummy CUs. */
9599 && item->per_cu->cu != NULL)
f4dc4d17
DE
9600 {
9601 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9602 unsigned int debug_print_threshold;
247f5c4f 9603 char buf[100];
f4dc4d17 9604
247f5c4f 9605 if (per_cu->is_debug_types)
f4dc4d17 9606 {
247f5c4f
DE
9607 struct signatured_type *sig_type =
9608 (struct signatured_type *) per_cu;
9609
9d8780f0 9610 sprintf (buf, "TU %s at offset %s",
73be47f5 9611 hex_string (sig_type->signature),
9d8780f0 9612 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9613 /* There can be 100s of TUs.
9614 Only print them in verbose mode. */
9615 debug_print_threshold = 2;
f4dc4d17 9616 }
247f5c4f 9617 else
73be47f5 9618 {
9d8780f0
SM
9619 sprintf (buf, "CU at offset %s",
9620 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9621 debug_print_threshold = 1;
9622 }
247f5c4f 9623
b4f54984 9624 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9625 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9626
9627 if (per_cu->is_debug_types)
9628 process_full_type_unit (per_cu, item->pretend_language);
9629 else
9630 process_full_comp_unit (per_cu, item->pretend_language);
9631
b4f54984 9632 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9633 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9634 }
10b3939b
DJ
9635
9636 item->per_cu->queued = 0;
9637 next_item = item->next;
9638 xfree (item);
9639 }
9640
9641 dwarf2_queue_tail = NULL;
45cfd468 9642
b4f54984 9643 if (dwarf_read_debug)
45cfd468
DE
9644 {
9645 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9646 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9647 }
10b3939b
DJ
9648}
9649
10b3939b
DJ
9650/* Read in full symbols for PST, and anything it depends on. */
9651
c906108c 9652static void
fba45db2 9653psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9654{
10b3939b 9655 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9656 int i;
9657
95554aad
TT
9658 if (pst->readin)
9659 return;
9660
aaa75496 9661 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9662 if (!pst->dependencies[i]->readin
9663 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9664 {
9665 /* Inform about additional files that need to be read in. */
9666 if (info_verbose)
9667 {
a3f17187 9668 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9669 fputs_filtered (" ", gdb_stdout);
9670 wrap_here ("");
9671 fputs_filtered ("and ", gdb_stdout);
9672 wrap_here ("");
9673 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9674 wrap_here (""); /* Flush output. */
aaa75496
JB
9675 gdb_flush (gdb_stdout);
9676 }
9677 psymtab_to_symtab_1 (pst->dependencies[i]);
9678 }
9679
9a3c8263 9680 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9681
9682 if (per_cu == NULL)
aaa75496
JB
9683 {
9684 /* It's an include file, no symbols to read for it.
9685 Everything is in the parent symtab. */
9686 pst->readin = 1;
9687 return;
9688 }
c906108c 9689
58f0c718 9690 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9691}
9692
dee91e82
DE
9693/* Trivial hash function for die_info: the hash value of a DIE
9694 is its offset in .debug_info for this objfile. */
10b3939b 9695
dee91e82
DE
9696static hashval_t
9697die_hash (const void *item)
10b3939b 9698{
9a3c8263 9699 const struct die_info *die = (const struct die_info *) item;
6502dd73 9700
9c541725 9701 return to_underlying (die->sect_off);
dee91e82 9702}
63d06c5c 9703
dee91e82
DE
9704/* Trivial comparison function for die_info structures: two DIEs
9705 are equal if they have the same offset. */
98bfdba5 9706
dee91e82
DE
9707static int
9708die_eq (const void *item_lhs, const void *item_rhs)
9709{
9a3c8263
SM
9710 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9711 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9712
9c541725 9713 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9714}
c906108c 9715
dee91e82
DE
9716/* die_reader_func for load_full_comp_unit.
9717 This is identical to read_signatured_type_reader,
9718 but is kept separate for now. */
c906108c 9719
dee91e82
DE
9720static void
9721load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9722 const gdb_byte *info_ptr,
dee91e82
DE
9723 struct die_info *comp_unit_die,
9724 int has_children,
9725 void *data)
9726{
9727 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9728 enum language *language_ptr = (enum language *) data;
6caca83c 9729
dee91e82
DE
9730 gdb_assert (cu->die_hash == NULL);
9731 cu->die_hash =
9732 htab_create_alloc_ex (cu->header.length / 12,
9733 die_hash,
9734 die_eq,
9735 NULL,
9736 &cu->comp_unit_obstack,
9737 hashtab_obstack_allocate,
9738 dummy_obstack_deallocate);
e142c38c 9739
dee91e82
DE
9740 if (has_children)
9741 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9742 &info_ptr, comp_unit_die);
9743 cu->dies = comp_unit_die;
9744 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9745
9746 /* We try not to read any attributes in this function, because not
9cdd5dbd 9747 all CUs needed for references have been loaded yet, and symbol
10b3939b 9748 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9749 or we won't be able to build types correctly.
9750 Similarly, if we do not read the producer, we can not apply
9751 producer-specific interpretation. */
95554aad 9752 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9753}
10b3939b 9754
dee91e82 9755/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9756
dee91e82 9757static void
95554aad 9758load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9759 bool skip_partial,
95554aad 9760 enum language pretend_language)
dee91e82 9761{
3019eac3 9762 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9763
58f0c718 9764 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9765 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9766}
9767
3da10d80
KS
9768/* Add a DIE to the delayed physname list. */
9769
9770static void
9771add_to_method_list (struct type *type, int fnfield_index, int index,
9772 const char *name, struct die_info *die,
9773 struct dwarf2_cu *cu)
9774{
9775 struct delayed_method_info mi;
9776 mi.type = type;
9777 mi.fnfield_index = fnfield_index;
9778 mi.index = index;
9779 mi.name = name;
9780 mi.die = die;
c89b44cd 9781 cu->method_list.push_back (mi);
3da10d80
KS
9782}
9783
3693fdb3
PA
9784/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9785 "const" / "volatile". If so, decrements LEN by the length of the
9786 modifier and return true. Otherwise return false. */
9787
9788template<size_t N>
9789static bool
9790check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9791{
9792 size_t mod_len = sizeof (mod) - 1;
9793 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9794 {
9795 len -= mod_len;
9796 return true;
9797 }
9798 return false;
9799}
9800
3da10d80
KS
9801/* Compute the physnames of any methods on the CU's method list.
9802
9803 The computation of method physnames is delayed in order to avoid the
9804 (bad) condition that one of the method's formal parameters is of an as yet
9805 incomplete type. */
9806
9807static void
9808compute_delayed_physnames (struct dwarf2_cu *cu)
9809{
3693fdb3 9810 /* Only C++ delays computing physnames. */
c89b44cd 9811 if (cu->method_list.empty ())
3693fdb3
PA
9812 return;
9813 gdb_assert (cu->language == language_cplus);
9814
52941706 9815 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9816 {
1d06ead6 9817 const char *physname;
3da10d80 9818 struct fn_fieldlist *fn_flp
c89b44cd
TT
9819 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9820 physname = dwarf2_physname (mi.name, mi.die, cu);
9821 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9822 = physname ? physname : "";
3693fdb3
PA
9823
9824 /* Since there's no tag to indicate whether a method is a
9825 const/volatile overload, extract that information out of the
9826 demangled name. */
9827 if (physname != NULL)
9828 {
9829 size_t len = strlen (physname);
9830
9831 while (1)
9832 {
9833 if (physname[len] == ')') /* shortcut */
9834 break;
9835 else if (check_modifier (physname, len, " const"))
c89b44cd 9836 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9837 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9838 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9839 else
9840 break;
9841 }
9842 }
3da10d80 9843 }
c89b44cd
TT
9844
9845 /* The list is no longer needed. */
9846 cu->method_list.clear ();
3da10d80
KS
9847}
9848
a766d390
DE
9849/* Go objects should be embedded in a DW_TAG_module DIE,
9850 and it's not clear if/how imported objects will appear.
9851 To keep Go support simple until that's worked out,
9852 go back through what we've read and create something usable.
9853 We could do this while processing each DIE, and feels kinda cleaner,
9854 but that way is more invasive.
9855 This is to, for example, allow the user to type "p var" or "b main"
9856 without having to specify the package name, and allow lookups
9857 of module.object to work in contexts that use the expression
9858 parser. */
9859
9860static void
9861fixup_go_packaging (struct dwarf2_cu *cu)
9862{
9863 char *package_name = NULL;
9864 struct pending *list;
9865 int i;
9866
c24bdb02 9867 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9868 list != NULL;
9869 list = list->next)
a766d390
DE
9870 {
9871 for (i = 0; i < list->nsyms; ++i)
9872 {
9873 struct symbol *sym = list->symbol[i];
9874
9875 if (SYMBOL_LANGUAGE (sym) == language_go
9876 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9877 {
9878 char *this_package_name = go_symbol_package_name (sym);
9879
9880 if (this_package_name == NULL)
9881 continue;
9882 if (package_name == NULL)
9883 package_name = this_package_name;
9884 else
9885 {
518817b3
SM
9886 struct objfile *objfile
9887 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9888 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9889 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9890 (symbol_symtab (sym) != NULL
9891 ? symtab_to_filename_for_display
9892 (symbol_symtab (sym))
e3b94546 9893 : objfile_name (objfile)),
a766d390
DE
9894 this_package_name, package_name);
9895 xfree (this_package_name);
9896 }
9897 }
9898 }
9899 }
9900
9901 if (package_name != NULL)
9902 {
518817b3 9903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9904 const char *saved_package_name
021887d8 9905 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9906 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9907 saved_package_name);
a766d390
DE
9908 struct symbol *sym;
9909
e623cf5d 9910 sym = allocate_symbol (objfile);
f85f34ed 9911 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9912 SYMBOL_SET_NAMES (sym, saved_package_name,
9913 strlen (saved_package_name), 0, objfile);
a766d390
DE
9914 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9915 e.g., "main" finds the "main" module and not C's main(). */
9916 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9917 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9918 SYMBOL_TYPE (sym) = type;
9919
c24bdb02 9920 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9921
9922 xfree (package_name);
9923 }
9924}
9925
c9317f21
TT
9926/* Allocate a fully-qualified name consisting of the two parts on the
9927 obstack. */
9928
9929static const char *
9930rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9931{
9932 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9933}
9934
9935/* A helper that allocates a struct discriminant_info to attach to a
9936 union type. */
9937
9938static struct discriminant_info *
9939alloc_discriminant_info (struct type *type, int discriminant_index,
9940 int default_index)
9941{
9942 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9943 gdb_assert (discriminant_index == -1
9944 || (discriminant_index >= 0
9945 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9946 gdb_assert (default_index == -1
c7b15a66 9947 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9948
9949 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9950
9951 struct discriminant_info *disc
9952 = ((struct discriminant_info *)
9953 TYPE_ZALLOC (type,
9954 offsetof (struct discriminant_info, discriminants)
9955 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9956 disc->default_index = default_index;
9957 disc->discriminant_index = discriminant_index;
9958
9959 struct dynamic_prop prop;
9960 prop.kind = PROP_UNDEFINED;
9961 prop.data.baton = disc;
9962
9963 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9964
9965 return disc;
9966}
9967
9968/* Some versions of rustc emitted enums in an unusual way.
9969
9970 Ordinary enums were emitted as unions. The first element of each
9971 structure in the union was named "RUST$ENUM$DISR". This element
9972 held the discriminant.
9973
9974 These versions of Rust also implemented the "non-zero"
9975 optimization. When the enum had two values, and one is empty and
9976 the other holds a pointer that cannot be zero, the pointer is used
9977 as the discriminant, with a zero value meaning the empty variant.
9978 Here, the union's first member is of the form
9979 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9980 where the fieldnos are the indices of the fields that should be
9981 traversed in order to find the field (which may be several fields deep)
9982 and the variantname is the name of the variant of the case when the
9983 field is zero.
9984
9985 This function recognizes whether TYPE is of one of these forms,
9986 and, if so, smashes it to be a variant type. */
9987
9988static void
9989quirk_rust_enum (struct type *type, struct objfile *objfile)
9990{
9991 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9992
9993 /* We don't need to deal with empty enums. */
9994 if (TYPE_NFIELDS (type) == 0)
9995 return;
9996
9997#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9998 if (TYPE_NFIELDS (type) == 1
9999 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10000 {
10001 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10002
10003 /* Decode the field name to find the offset of the
10004 discriminant. */
10005 ULONGEST bit_offset = 0;
10006 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10007 while (name[0] >= '0' && name[0] <= '9')
10008 {
10009 char *tail;
10010 unsigned long index = strtoul (name, &tail, 10);
10011 name = tail;
10012 if (*name != '$'
10013 || index >= TYPE_NFIELDS (field_type)
10014 || (TYPE_FIELD_LOC_KIND (field_type, index)
10015 != FIELD_LOC_KIND_BITPOS))
10016 {
b98664d3 10017 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10018 "[in module %s]"),
10019 TYPE_FIELD_NAME (type, 0),
10020 objfile_name (objfile));
10021 return;
10022 }
10023 ++name;
10024
10025 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10026 field_type = TYPE_FIELD_TYPE (field_type, index);
10027 }
10028
10029 /* Make a union to hold the variants. */
10030 struct type *union_type = alloc_type (objfile);
10031 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10032 TYPE_NFIELDS (union_type) = 3;
10033 TYPE_FIELDS (union_type)
10034 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10035 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10036 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10037
10038 /* Put the discriminant must at index 0. */
10039 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10040 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10041 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10042 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10043
10044 /* The order of fields doesn't really matter, so put the real
10045 field at index 1 and the data-less field at index 2. */
10046 struct discriminant_info *disc
10047 = alloc_discriminant_info (union_type, 0, 1);
10048 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10049 TYPE_FIELD_NAME (union_type, 1)
10050 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10051 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10052 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10053 TYPE_FIELD_NAME (union_type, 1));
10054
10055 const char *dataless_name
10056 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10057 name);
10058 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10059 dataless_name);
10060 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10061 /* NAME points into the original discriminant name, which
10062 already has the correct lifetime. */
10063 TYPE_FIELD_NAME (union_type, 2) = name;
10064 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10065 disc->discriminants[2] = 0;
10066
10067 /* Smash this type to be a structure type. We have to do this
10068 because the type has already been recorded. */
10069 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10070 TYPE_NFIELDS (type) = 1;
10071 TYPE_FIELDS (type)
10072 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10073
10074 /* Install the variant part. */
10075 TYPE_FIELD_TYPE (type, 0) = union_type;
10076 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10077 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10078 }
10079 else if (TYPE_NFIELDS (type) == 1)
10080 {
10081 /* We assume that a union with a single field is a univariant
10082 enum. */
10083 /* Smash this type to be a structure type. We have to do this
10084 because the type has already been recorded. */
10085 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10086
10087 /* Make a union to hold the variants. */
10088 struct type *union_type = alloc_type (objfile);
10089 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10090 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10091 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10092 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10093 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10094
10095 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10096 const char *variant_name
10097 = rust_last_path_segment (TYPE_NAME (field_type));
10098 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10099 TYPE_NAME (field_type)
10100 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10101 TYPE_NAME (type), variant_name);
c9317f21
TT
10102
10103 /* Install the union in the outer struct type. */
10104 TYPE_NFIELDS (type) = 1;
10105 TYPE_FIELDS (type)
10106 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10107 TYPE_FIELD_TYPE (type, 0) = union_type;
10108 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10109 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10110
10111 alloc_discriminant_info (union_type, -1, 0);
10112 }
10113 else
10114 {
10115 struct type *disr_type = nullptr;
10116 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10117 {
10118 disr_type = TYPE_FIELD_TYPE (type, i);
10119
a037790e
TT
10120 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10121 {
10122 /* All fields of a true enum will be structs. */
10123 return;
10124 }
10125 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10126 {
10127 /* Could be data-less variant, so keep going. */
a037790e 10128 disr_type = nullptr;
c9317f21
TT
10129 }
10130 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10131 "RUST$ENUM$DISR") != 0)
10132 {
10133 /* Not a Rust enum. */
10134 return;
10135 }
10136 else
10137 {
10138 /* Found one. */
10139 break;
10140 }
10141 }
10142
10143 /* If we got here without a discriminant, then it's probably
10144 just a union. */
10145 if (disr_type == nullptr)
10146 return;
10147
10148 /* Smash this type to be a structure type. We have to do this
10149 because the type has already been recorded. */
10150 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10151
10152 /* Make a union to hold the variants. */
10153 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10154 struct type *union_type = alloc_type (objfile);
10155 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10156 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10157 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10158 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10159 TYPE_FIELDS (union_type)
10160 = (struct field *) TYPE_ZALLOC (union_type,
10161 (TYPE_NFIELDS (union_type)
10162 * sizeof (struct field)));
10163
10164 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10165 TYPE_NFIELDS (type) * sizeof (struct field));
10166
10167 /* Install the discriminant at index 0 in the union. */
10168 TYPE_FIELD (union_type, 0) = *disr_field;
10169 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10170 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10171
10172 /* Install the union in the outer struct type. */
10173 TYPE_FIELD_TYPE (type, 0) = union_type;
10174 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10175 TYPE_NFIELDS (type) = 1;
10176
10177 /* Set the size and offset of the union type. */
10178 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10179
10180 /* We need a way to find the correct discriminant given a
10181 variant name. For convenience we build a map here. */
10182 struct type *enum_type = FIELD_TYPE (*disr_field);
10183 std::unordered_map<std::string, ULONGEST> discriminant_map;
10184 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10185 {
10186 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10187 {
10188 const char *name
10189 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10190 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10191 }
10192 }
10193
10194 int n_fields = TYPE_NFIELDS (union_type);
10195 struct discriminant_info *disc
10196 = alloc_discriminant_info (union_type, 0, -1);
10197 /* Skip the discriminant here. */
10198 for (int i = 1; i < n_fields; ++i)
10199 {
10200 /* Find the final word in the name of this variant's type.
10201 That name can be used to look up the correct
10202 discriminant. */
10203 const char *variant_name
10204 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10205 i)));
10206
10207 auto iter = discriminant_map.find (variant_name);
10208 if (iter != discriminant_map.end ())
10209 disc->discriminants[i] = iter->second;
10210
bedda9ac 10211 /* Remove the discriminant field, if it exists. */
c9317f21 10212 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10213 if (TYPE_NFIELDS (sub_type) > 0)
10214 {
10215 --TYPE_NFIELDS (sub_type);
10216 ++TYPE_FIELDS (sub_type);
10217 }
c9317f21
TT
10218 TYPE_FIELD_NAME (union_type, i) = variant_name;
10219 TYPE_NAME (sub_type)
10220 = rust_fully_qualify (&objfile->objfile_obstack,
10221 TYPE_NAME (type), variant_name);
10222 }
10223 }
10224}
10225
10226/* Rewrite some Rust unions to be structures with variants parts. */
10227
10228static void
10229rust_union_quirks (struct dwarf2_cu *cu)
10230{
10231 gdb_assert (cu->language == language_rust);
52941706
SM
10232 for (type *type_ : cu->rust_unions)
10233 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10234 /* We don't need this any more. */
10235 cu->rust_unions.clear ();
c9317f21
TT
10236}
10237
95554aad
TT
10238/* Return the symtab for PER_CU. This works properly regardless of
10239 whether we're using the index or psymtabs. */
10240
43f3e411
DE
10241static struct compunit_symtab *
10242get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10243{
ed2dc618 10244 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10245 ? per_cu->v.quick->compunit_symtab
10246 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10247}
10248
10249/* A helper function for computing the list of all symbol tables
10250 included by PER_CU. */
10251
10252static void
4c39bc03 10253recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10254 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10255 struct dwarf2_per_cu_data *per_cu,
43f3e411 10256 struct compunit_symtab *immediate_parent)
95554aad
TT
10257{
10258 void **slot;
10259 int ix;
43f3e411 10260 struct compunit_symtab *cust;
95554aad
TT
10261 struct dwarf2_per_cu_data *iter;
10262
10263 slot = htab_find_slot (all_children, per_cu, INSERT);
10264 if (*slot != NULL)
10265 {
10266 /* This inclusion and its children have been processed. */
10267 return;
10268 }
10269
10270 *slot = per_cu;
10271 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10272 cust = get_compunit_symtab (per_cu);
10273 if (cust != NULL)
ec94af83
DE
10274 {
10275 /* If this is a type unit only add its symbol table if we haven't
10276 seen it yet (type unit per_cu's can share symtabs). */
10277 if (per_cu->is_debug_types)
10278 {
43f3e411 10279 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10280 if (*slot == NULL)
10281 {
43f3e411 10282 *slot = cust;
4c39bc03 10283 result->push_back (cust);
43f3e411
DE
10284 if (cust->user == NULL)
10285 cust->user = immediate_parent;
ec94af83
DE
10286 }
10287 }
10288 else
f9125b6c 10289 {
4c39bc03 10290 result->push_back (cust);
43f3e411
DE
10291 if (cust->user == NULL)
10292 cust->user = immediate_parent;
f9125b6c 10293 }
ec94af83 10294 }
95554aad
TT
10295
10296 for (ix = 0;
796a7ff8 10297 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10298 ++ix)
ec94af83
DE
10299 {
10300 recursively_compute_inclusions (result, all_children,
43f3e411 10301 all_type_symtabs, iter, cust);
ec94af83 10302 }
95554aad
TT
10303}
10304
43f3e411 10305/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10306 PER_CU. */
10307
10308static void
43f3e411 10309compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10310{
f4dc4d17
DE
10311 gdb_assert (! per_cu->is_debug_types);
10312
796a7ff8 10313 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10314 {
10315 int ix, len;
ec94af83 10316 struct dwarf2_per_cu_data *per_cu_iter;
4c39bc03 10317 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10318 htab_t all_children, all_type_symtabs;
43f3e411 10319 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10320
10321 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10322 if (cust == NULL)
95554aad
TT
10323 return;
10324
10325 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10326 NULL, xcalloc, xfree);
ec94af83
DE
10327 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10328 NULL, xcalloc, xfree);
95554aad
TT
10329
10330 for (ix = 0;
796a7ff8 10331 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10332 ix, per_cu_iter);
95554aad 10333 ++ix)
ec94af83
DE
10334 {
10335 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10336 all_type_symtabs, per_cu_iter,
43f3e411 10337 cust);
ec94af83 10338 }
95554aad 10339
ec94af83 10340 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10341 len = result_symtabs.size ();
43f3e411 10342 cust->includes
ed2dc618 10343 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10344 struct compunit_symtab *, len + 1);
4c39bc03
TT
10345 memcpy (cust->includes, result_symtabs.data (),
10346 len * sizeof (compunit_symtab *));
43f3e411 10347 cust->includes[len] = NULL;
95554aad 10348
95554aad 10349 htab_delete (all_children);
ec94af83 10350 htab_delete (all_type_symtabs);
95554aad
TT
10351 }
10352}
10353
10354/* Compute the 'includes' field for the symtabs of all the CUs we just
10355 read. */
10356
10357static void
ed2dc618 10358process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10359{
71b73764 10360 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10361 {
10362 if (! iter->is_debug_types)
43f3e411 10363 compute_compunit_symtab_includes (iter);
f4dc4d17 10364 }
95554aad 10365
c5d0225d 10366 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10367}
10368
9cdd5dbd 10369/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10370 already been loaded into memory. */
10371
10372static void
95554aad
TT
10373process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10374 enum language pretend_language)
10b3939b 10375{
10b3939b 10376 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10377 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10378 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10379 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10380 CORE_ADDR lowpc, highpc;
43f3e411 10381 struct compunit_symtab *cust;
10b3939b 10382 CORE_ADDR baseaddr;
4359dff1 10383 struct block *static_block;
3e29f34a 10384 CORE_ADDR addr;
10b3939b
DJ
10385
10386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10387
c89b44cd
TT
10388 /* Clear the list here in case something was left over. */
10389 cu->method_list.clear ();
10b3939b 10390
95554aad
TT
10391 cu->language = pretend_language;
10392 cu->language_defn = language_def (cu->language);
10393
c906108c 10394 /* Do line number decoding in read_file_scope () */
10b3939b 10395 process_die (cu->dies, cu);
c906108c 10396
a766d390
DE
10397 /* For now fudge the Go package. */
10398 if (cu->language == language_go)
10399 fixup_go_packaging (cu);
10400
5f48f8f3 10401 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10402 should be complete, and it should now be safe to compute all of the
10403 physnames. */
10404 compute_delayed_physnames (cu);
3da10d80 10405
c9317f21
TT
10406 if (cu->language == language_rust)
10407 rust_union_quirks (cu);
10408
fae299cd
DC
10409 /* Some compilers don't define a DW_AT_high_pc attribute for the
10410 compilation unit. If the DW_AT_high_pc is missing, synthesize
10411 it, by scanning the DIE's below the compilation unit. */
10b3939b 10412 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10413
3e29f34a 10414 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10415 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10416
10417 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10418 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10419 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10420 addrmap to help ensure it has an accurate map of pc values belonging to
10421 this comp unit. */
10422 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10423
c24bdb02 10424 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10425 SECT_OFF_TEXT (objfile),
10426 0);
c906108c 10427
43f3e411 10428 if (cust != NULL)
c906108c 10429 {
df15bd07 10430 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10431
8be455d7
JK
10432 /* Set symtab language to language from DW_AT_language. If the
10433 compilation is from a C file generated by language preprocessors, do
10434 not set the language if it was already deduced by start_subfile. */
43f3e411 10435 if (!(cu->language == language_c
40e3ad0e 10436 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10437 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10438
10439 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10440 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10441 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10442 there were bugs in prologue debug info, fixed later in GCC-4.5
10443 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10444
10445 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10446 needed, it would be wrong due to missing DW_AT_producer there.
10447
10448 Still one can confuse GDB by using non-standard GCC compilation
10449 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10450 */
ab260dad 10451 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10452 cust->locations_valid = 1;
e0d00bc7
JK
10453
10454 if (gcc_4_minor >= 5)
43f3e411 10455 cust->epilogue_unwind_valid = 1;
96408a79 10456
43f3e411 10457 cust->call_site_htab = cu->call_site_htab;
c906108c 10458 }
9291a0cd
TT
10459
10460 if (dwarf2_per_objfile->using_index)
43f3e411 10461 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10462 else
10463 {
10464 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10465 pst->compunit_symtab = cust;
9291a0cd
TT
10466 pst->readin = 1;
10467 }
c906108c 10468
95554aad 10469 /* Push it for inclusion processing later. */
c5d0225d 10470 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10471
10472 /* Not needed any more. */
c24bdb02 10473 cu->reset_builder ();
f4dc4d17 10474}
45cfd468 10475
f4dc4d17
DE
10476/* Generate full symbol information for type unit PER_CU, whose DIEs have
10477 already been loaded into memory. */
10478
10479static void
10480process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10481 enum language pretend_language)
10482{
10483 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10484 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10485 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10486 struct compunit_symtab *cust;
0186c6a7
DE
10487 struct signatured_type *sig_type;
10488
10489 gdb_assert (per_cu->is_debug_types);
10490 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10491
c89b44cd
TT
10492 /* Clear the list here in case something was left over. */
10493 cu->method_list.clear ();
f4dc4d17 10494
f4dc4d17
DE
10495 cu->language = pretend_language;
10496 cu->language_defn = language_def (cu->language);
10497
10498 /* The symbol tables are set up in read_type_unit_scope. */
10499 process_die (cu->dies, cu);
10500
10501 /* For now fudge the Go package. */
10502 if (cu->language == language_go)
10503 fixup_go_packaging (cu);
10504
5f48f8f3 10505 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10506 should be complete, and it should now be safe to compute all of the
10507 physnames. */
10508 compute_delayed_physnames (cu);
f4dc4d17 10509
c9317f21
TT
10510 if (cu->language == language_rust)
10511 rust_union_quirks (cu);
10512
f4dc4d17
DE
10513 /* TUs share symbol tables.
10514 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10515 of it with end_expandable_symtab. Otherwise, complete the addition of
10516 this TU's symbols to the existing symtab. */
43f3e411 10517 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10518 {
c24bdb02
KS
10519 buildsym_compunit *builder = cu->get_builder ();
10520 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10521 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10522
43f3e411 10523 if (cust != NULL)
f4dc4d17
DE
10524 {
10525 /* Set symtab language to language from DW_AT_language. If the
10526 compilation is from a C file generated by language preprocessors,
10527 do not set the language if it was already deduced by
10528 start_subfile. */
43f3e411
DE
10529 if (!(cu->language == language_c
10530 && COMPUNIT_FILETABS (cust)->language != language_c))
10531 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10532 }
10533 }
10534 else
10535 {
c24bdb02 10536 cu->get_builder ()->augment_type_symtab ();
43f3e411 10537 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10538 }
10539
10540 if (dwarf2_per_objfile->using_index)
43f3e411 10541 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10542 else
10543 {
10544 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10545 pst->compunit_symtab = cust;
f4dc4d17 10546 pst->readin = 1;
45cfd468 10547 }
804d2729
TT
10548
10549 /* Not needed any more. */
c24bdb02 10550 cu->reset_builder ();
c906108c
SS
10551}
10552
95554aad
TT
10553/* Process an imported unit DIE. */
10554
10555static void
10556process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10557{
10558 struct attribute *attr;
10559
f4dc4d17
DE
10560 /* For now we don't handle imported units in type units. */
10561 if (cu->per_cu->is_debug_types)
10562 {
10563 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10564 " supported in type units [in module %s]"),
518817b3 10565 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10566 }
10567
95554aad
TT
10568 attr = dwarf2_attr (die, DW_AT_import, cu);
10569 if (attr != NULL)
10570 {
9c541725
PA
10571 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10572 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10573 dwarf2_per_cu_data *per_cu
e3b94546 10574 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10575 cu->per_cu->dwarf2_per_objfile);
95554aad 10576
69d751e3 10577 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10578 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10579 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10580
796a7ff8 10581 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10582 per_cu);
10583 }
10584}
10585
4c8aa72d
PA
10586/* RAII object that represents a process_die scope: i.e.,
10587 starts/finishes processing a DIE. */
10588class process_die_scope
adde2bff 10589{
4c8aa72d
PA
10590public:
10591 process_die_scope (die_info *die, dwarf2_cu *cu)
10592 : m_die (die), m_cu (cu)
10593 {
10594 /* We should only be processing DIEs not already in process. */
10595 gdb_assert (!m_die->in_process);
10596 m_die->in_process = true;
10597 }
8c3cb9fa 10598
4c8aa72d
PA
10599 ~process_die_scope ()
10600 {
10601 m_die->in_process = false;
10602
10603 /* If we're done processing the DIE for the CU that owns the line
10604 header, we don't need the line header anymore. */
10605 if (m_cu->line_header_die_owner == m_die)
10606 {
10607 delete m_cu->line_header;
10608 m_cu->line_header = NULL;
10609 m_cu->line_header_die_owner = NULL;
10610 }
10611 }
10612
10613private:
10614 die_info *m_die;
10615 dwarf2_cu *m_cu;
10616};
adde2bff 10617
c906108c
SS
10618/* Process a die and its children. */
10619
10620static void
e7c27a73 10621process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10622{
4c8aa72d 10623 process_die_scope scope (die, cu);
adde2bff 10624
c906108c
SS
10625 switch (die->tag)
10626 {
10627 case DW_TAG_padding:
10628 break;
10629 case DW_TAG_compile_unit:
95554aad 10630 case DW_TAG_partial_unit:
e7c27a73 10631 read_file_scope (die, cu);
c906108c 10632 break;
348e048f
DE
10633 case DW_TAG_type_unit:
10634 read_type_unit_scope (die, cu);
10635 break;
c906108c 10636 case DW_TAG_subprogram:
0a4b0913
AB
10637 /* Nested subprograms in Fortran get a prefix. */
10638 if (cu->language == language_fortran
10639 && die->parent != NULL
10640 && die->parent->tag == DW_TAG_subprogram)
10641 cu->processing_has_namespace_info = true;
10642 /* Fall through. */
c906108c 10643 case DW_TAG_inlined_subroutine:
edb3359d 10644 read_func_scope (die, cu);
c906108c
SS
10645 break;
10646 case DW_TAG_lexical_block:
14898363
L
10647 case DW_TAG_try_block:
10648 case DW_TAG_catch_block:
e7c27a73 10649 read_lexical_block_scope (die, cu);
c906108c 10650 break;
216f72a1 10651 case DW_TAG_call_site:
96408a79
SA
10652 case DW_TAG_GNU_call_site:
10653 read_call_site_scope (die, cu);
10654 break;
c906108c 10655 case DW_TAG_class_type:
680b30c7 10656 case DW_TAG_interface_type:
c906108c
SS
10657 case DW_TAG_structure_type:
10658 case DW_TAG_union_type:
134d01f1 10659 process_structure_scope (die, cu);
c906108c
SS
10660 break;
10661 case DW_TAG_enumeration_type:
134d01f1 10662 process_enumeration_scope (die, cu);
c906108c 10663 break;
134d01f1 10664
f792889a
DJ
10665 /* These dies have a type, but processing them does not create
10666 a symbol or recurse to process the children. Therefore we can
10667 read them on-demand through read_type_die. */
c906108c 10668 case DW_TAG_subroutine_type:
72019c9c 10669 case DW_TAG_set_type:
c906108c 10670 case DW_TAG_array_type:
c906108c 10671 case DW_TAG_pointer_type:
c906108c 10672 case DW_TAG_ptr_to_member_type:
c906108c 10673 case DW_TAG_reference_type:
4297a3f0 10674 case DW_TAG_rvalue_reference_type:
c906108c 10675 case DW_TAG_string_type:
c906108c 10676 break;
134d01f1 10677
c906108c 10678 case DW_TAG_base_type:
a02abb62 10679 case DW_TAG_subrange_type:
cb249c71 10680 case DW_TAG_typedef:
134d01f1
DJ
10681 /* Add a typedef symbol for the type definition, if it has a
10682 DW_AT_name. */
f792889a 10683 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10684 break;
c906108c 10685 case DW_TAG_common_block:
e7c27a73 10686 read_common_block (die, cu);
c906108c
SS
10687 break;
10688 case DW_TAG_common_inclusion:
10689 break;
d9fa45fe 10690 case DW_TAG_namespace:
9068261f 10691 cu->processing_has_namespace_info = true;
e7c27a73 10692 read_namespace (die, cu);
d9fa45fe 10693 break;
5d7cb8df 10694 case DW_TAG_module:
9068261f 10695 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10696 read_module (die, cu);
10697 break;
d9fa45fe 10698 case DW_TAG_imported_declaration:
9068261f 10699 cu->processing_has_namespace_info = true;
74921315
KS
10700 if (read_namespace_alias (die, cu))
10701 break;
86a73007
TT
10702 /* The declaration is not a global namespace alias. */
10703 /* Fall through. */
d9fa45fe 10704 case DW_TAG_imported_module:
9068261f 10705 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10706 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10707 || cu->language != language_fortran))
b98664d3 10708 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10709 dwarf_tag_name (die->tag));
10710 read_import_statement (die, cu);
d9fa45fe 10711 break;
95554aad
TT
10712
10713 case DW_TAG_imported_unit:
10714 process_imported_unit_die (die, cu);
10715 break;
10716
71a3c369
TT
10717 case DW_TAG_variable:
10718 read_variable (die, cu);
10719 break;
10720
c906108c 10721 default:
e7c27a73 10722 new_symbol (die, NULL, cu);
c906108c
SS
10723 break;
10724 }
10725}
ca69b9e6
DE
10726\f
10727/* DWARF name computation. */
c906108c 10728
94af9270
KS
10729/* A helper function for dwarf2_compute_name which determines whether DIE
10730 needs to have the name of the scope prepended to the name listed in the
10731 die. */
10732
10733static int
10734die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10735{
1c809c68
TT
10736 struct attribute *attr;
10737
94af9270
KS
10738 switch (die->tag)
10739 {
10740 case DW_TAG_namespace:
10741 case DW_TAG_typedef:
10742 case DW_TAG_class_type:
10743 case DW_TAG_interface_type:
10744 case DW_TAG_structure_type:
10745 case DW_TAG_union_type:
10746 case DW_TAG_enumeration_type:
10747 case DW_TAG_enumerator:
10748 case DW_TAG_subprogram:
08a76f8a 10749 case DW_TAG_inlined_subroutine:
94af9270 10750 case DW_TAG_member:
74921315 10751 case DW_TAG_imported_declaration:
94af9270
KS
10752 return 1;
10753
10754 case DW_TAG_variable:
c2b0a229 10755 case DW_TAG_constant:
94af9270
KS
10756 /* We only need to prefix "globally" visible variables. These include
10757 any variable marked with DW_AT_external or any variable that
10758 lives in a namespace. [Variables in anonymous namespaces
10759 require prefixing, but they are not DW_AT_external.] */
10760
10761 if (dwarf2_attr (die, DW_AT_specification, cu))
10762 {
10763 struct dwarf2_cu *spec_cu = cu;
9a619af0 10764
94af9270
KS
10765 return die_needs_namespace (die_specification (die, &spec_cu),
10766 spec_cu);
10767 }
10768
1c809c68 10769 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10770 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10771 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10772 return 0;
10773 /* A variable in a lexical block of some kind does not need a
10774 namespace, even though in C++ such variables may be external
10775 and have a mangled name. */
10776 if (die->parent->tag == DW_TAG_lexical_block
10777 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10778 || die->parent->tag == DW_TAG_catch_block
10779 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10780 return 0;
10781 return 1;
94af9270
KS
10782
10783 default:
10784 return 0;
10785 }
10786}
10787
73b9be8b
KS
10788/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10789 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10790 defined for the given DIE. */
10791
10792static struct attribute *
10793dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10794{
10795 struct attribute *attr;
10796
10797 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10798 if (attr == NULL)
10799 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10800
10801 return attr;
10802}
10803
10804/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10805 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10806 defined for the given DIE. */
10807
10808static const char *
10809dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10810{
10811 const char *linkage_name;
10812
10813 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10814 if (linkage_name == NULL)
10815 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10816
10817 return linkage_name;
10818}
10819
94af9270 10820/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10821 compute the physname for the object, which include a method's:
9c37b5ae 10822 - formal parameters (C++),
a766d390 10823 - receiver type (Go),
a766d390
DE
10824
10825 The term "physname" is a bit confusing.
10826 For C++, for example, it is the demangled name.
10827 For Go, for example, it's the mangled name.
94af9270 10828
af6b7be1
JB
10829 For Ada, return the DIE's linkage name rather than the fully qualified
10830 name. PHYSNAME is ignored..
10831
94af9270
KS
10832 The result is allocated on the objfile_obstack and canonicalized. */
10833
10834static const char *
15d034d0
TT
10835dwarf2_compute_name (const char *name,
10836 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10837 int physname)
10838{
518817b3 10839 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10840
94af9270
KS
10841 if (name == NULL)
10842 name = dwarf2_name (die, cu);
10843
2ee7123e
DE
10844 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10845 but otherwise compute it by typename_concat inside GDB.
10846 FIXME: Actually this is not really true, or at least not always true.
10847 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10848 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10849 will set the demangled name to the result of dwarf2_full_name, and it is
10850 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10851 if (cu->language == language_ada
10852 || (cu->language == language_fortran && physname))
10853 {
10854 /* For Ada unit, we prefer the linkage name over the name, as
10855 the former contains the exported name, which the user expects
10856 to be able to reference. Ideally, we want the user to be able
10857 to reference this entity using either natural or linkage name,
10858 but we haven't started looking at this enhancement yet. */
73b9be8b 10859 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10860
2ee7123e
DE
10861 if (linkage_name != NULL)
10862 return linkage_name;
f55ee35c
JK
10863 }
10864
94af9270
KS
10865 /* These are the only languages we know how to qualify names in. */
10866 if (name != NULL
9c37b5ae 10867 && (cu->language == language_cplus
c44af4eb
TT
10868 || cu->language == language_fortran || cu->language == language_d
10869 || cu->language == language_rust))
94af9270
KS
10870 {
10871 if (die_needs_namespace (die, cu))
10872 {
0d5cff50 10873 const char *prefix;
34a68019 10874 const char *canonical_name = NULL;
94af9270 10875
d7e74731
PA
10876 string_file buf;
10877
94af9270 10878 prefix = determine_prefix (die, cu);
94af9270
KS
10879 if (*prefix != '\0')
10880 {
f55ee35c
JK
10881 char *prefixed_name = typename_concat (NULL, prefix, name,
10882 physname, cu);
9a619af0 10883
d7e74731 10884 buf.puts (prefixed_name);
94af9270
KS
10885 xfree (prefixed_name);
10886 }
10887 else
d7e74731 10888 buf.puts (name);
94af9270 10889
98bfdba5
PA
10890 /* Template parameters may be specified in the DIE's DW_AT_name, or
10891 as children with DW_TAG_template_type_param or
10892 DW_TAG_value_type_param. If the latter, add them to the name
10893 here. If the name already has template parameters, then
10894 skip this step; some versions of GCC emit both, and
10895 it is more efficient to use the pre-computed name.
10896
10897 Something to keep in mind about this process: it is very
10898 unlikely, or in some cases downright impossible, to produce
10899 something that will match the mangled name of a function.
10900 If the definition of the function has the same debug info,
10901 we should be able to match up with it anyway. But fallbacks
10902 using the minimal symbol, for instance to find a method
10903 implemented in a stripped copy of libstdc++, will not work.
10904 If we do not have debug info for the definition, we will have to
10905 match them up some other way.
10906
10907 When we do name matching there is a related problem with function
10908 templates; two instantiated function templates are allowed to
10909 differ only by their return types, which we do not add here. */
10910
10911 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10912 {
10913 struct attribute *attr;
10914 struct die_info *child;
10915 int first = 1;
10916
10917 die->building_fullname = 1;
10918
10919 for (child = die->child; child != NULL; child = child->sibling)
10920 {
10921 struct type *type;
12df843f 10922 LONGEST value;
d521ce57 10923 const gdb_byte *bytes;
98bfdba5
PA
10924 struct dwarf2_locexpr_baton *baton;
10925 struct value *v;
10926
10927 if (child->tag != DW_TAG_template_type_param
10928 && child->tag != DW_TAG_template_value_param)
10929 continue;
10930
10931 if (first)
10932 {
d7e74731 10933 buf.puts ("<");
98bfdba5
PA
10934 first = 0;
10935 }
10936 else
d7e74731 10937 buf.puts (", ");
98bfdba5
PA
10938
10939 attr = dwarf2_attr (child, DW_AT_type, cu);
10940 if (attr == NULL)
10941 {
b98664d3 10942 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10943 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10944 continue;
10945 }
10946 type = die_type (child, cu);
10947
10948 if (child->tag == DW_TAG_template_type_param)
10949 {
c1ec8cea
TT
10950 c_print_type (type, "", &buf, -1, 0, cu->language,
10951 &type_print_raw_options);
98bfdba5
PA
10952 continue;
10953 }
10954
10955 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10956 if (attr == NULL)
10957 {
b98664d3 10958 complaint (_("template parameter missing "
3e43a32a 10959 "DW_AT_const_value"));
d7e74731 10960 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10961 continue;
10962 }
10963
10964 dwarf2_const_value_attr (attr, type, name,
10965 &cu->comp_unit_obstack, cu,
10966 &value, &bytes, &baton);
10967
10968 if (TYPE_NOSIGN (type))
10969 /* GDB prints characters as NUMBER 'CHAR'. If that's
10970 changed, this can use value_print instead. */
d7e74731 10971 c_printchar (value, type, &buf);
98bfdba5
PA
10972 else
10973 {
10974 struct value_print_options opts;
10975
10976 if (baton != NULL)
10977 v = dwarf2_evaluate_loc_desc (type, NULL,
10978 baton->data,
10979 baton->size,
10980 baton->per_cu);
10981 else if (bytes != NULL)
10982 {
10983 v = allocate_value (type);
10984 memcpy (value_contents_writeable (v), bytes,
10985 TYPE_LENGTH (type));
10986 }
10987 else
10988 v = value_from_longest (type, value);
10989
3e43a32a
MS
10990 /* Specify decimal so that we do not depend on
10991 the radix. */
98bfdba5
PA
10992 get_formatted_print_options (&opts, 'd');
10993 opts.raw = 1;
d7e74731 10994 value_print (v, &buf, &opts);
98bfdba5 10995 release_value (v);
98bfdba5
PA
10996 }
10997 }
10998
10999 die->building_fullname = 0;
11000
11001 if (!first)
11002 {
11003 /* Close the argument list, with a space if necessary
11004 (nested templates). */
d7e74731
PA
11005 if (!buf.empty () && buf.string ().back () == '>')
11006 buf.puts (" >");
98bfdba5 11007 else
d7e74731 11008 buf.puts (">");
98bfdba5
PA
11009 }
11010 }
11011
9c37b5ae 11012 /* For C++ methods, append formal parameter type
94af9270 11013 information, if PHYSNAME. */
6e70227d 11014
94af9270 11015 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11016 && cu->language == language_cplus)
94af9270
KS
11017 {
11018 struct type *type = read_type_die (die, cu);
11019
d7e74731 11020 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11021 &type_print_raw_options);
94af9270 11022
9c37b5ae 11023 if (cu->language == language_cplus)
94af9270 11024 {
60430eff
DJ
11025 /* Assume that an artificial first parameter is
11026 "this", but do not crash if it is not. RealView
11027 marks unnamed (and thus unused) parameters as
11028 artificial; there is no way to differentiate
11029 the two cases. */
94af9270
KS
11030 if (TYPE_NFIELDS (type) > 0
11031 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11032 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11033 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11034 0))))
d7e74731 11035 buf.puts (" const");
94af9270
KS
11036 }
11037 }
11038
d7e74731 11039 const std::string &intermediate_name = buf.string ();
94af9270
KS
11040
11041 if (cu->language == language_cplus)
34a68019 11042 canonical_name
322a8516 11043 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11044 &objfile->per_bfd->storage_obstack);
11045
11046 /* If we only computed INTERMEDIATE_NAME, or if
11047 INTERMEDIATE_NAME is already canonical, then we need to
11048 copy it to the appropriate obstack. */
322a8516 11049 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11050 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11051 intermediate_name);
34a68019
TT
11052 else
11053 name = canonical_name;
94af9270
KS
11054 }
11055 }
11056
11057 return name;
11058}
11059
0114d602
DJ
11060/* Return the fully qualified name of DIE, based on its DW_AT_name.
11061 If scope qualifiers are appropriate they will be added. The result
34a68019 11062 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11063 not have a name. NAME may either be from a previous call to
11064 dwarf2_name or NULL.
11065
9c37b5ae 11066 The output string will be canonicalized (if C++). */
0114d602
DJ
11067
11068static const char *
15d034d0 11069dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11070{
94af9270
KS
11071 return dwarf2_compute_name (name, die, cu, 0);
11072}
0114d602 11073
94af9270
KS
11074/* Construct a physname for the given DIE in CU. NAME may either be
11075 from a previous call to dwarf2_name or NULL. The result will be
11076 allocated on the objfile_objstack or NULL if the DIE does not have a
11077 name.
0114d602 11078
9c37b5ae 11079 The output string will be canonicalized (if C++). */
0114d602 11080
94af9270 11081static const char *
15d034d0 11082dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11083{
518817b3 11084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11085 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11086 int need_copy = 1;
11087
11088 /* In this case dwarf2_compute_name is just a shortcut not building anything
11089 on its own. */
11090 if (!die_needs_namespace (die, cu))
11091 return dwarf2_compute_name (name, die, cu, 1);
11092
73b9be8b 11093 mangled = dw2_linkage_name (die, cu);
900e11f9 11094
e98c9e7c
TT
11095 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11096 See https://github.com/rust-lang/rust/issues/32925. */
11097 if (cu->language == language_rust && mangled != NULL
11098 && strchr (mangled, '{') != NULL)
11099 mangled = NULL;
11100
900e11f9
JK
11101 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11102 has computed. */
791afaa2 11103 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11104 if (mangled != NULL)
900e11f9 11105 {
900e11f9 11106
59cc4834
JB
11107 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11108 {
11109 /* Do nothing (do not demangle the symbol name). */
11110 }
11111 else if (cu->language == language_go)
a766d390 11112 {
5e2db402
TT
11113 /* This is a lie, but we already lie to the caller new_symbol.
11114 new_symbol assumes we return the mangled name.
a766d390 11115 This just undoes that lie until things are cleaned up. */
a766d390
DE
11116 }
11117 else
11118 {
0eb876f5
JB
11119 /* Use DMGL_RET_DROP for C++ template functions to suppress
11120 their return type. It is easier for GDB users to search
11121 for such functions as `name(params)' than `long name(params)'.
11122 In such case the minimal symbol names do not match the full
11123 symbol names but for template functions there is never a need
11124 to look up their definition from their declaration so
11125 the only disadvantage remains the minimal symbol variant
11126 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11127 demangled.reset (gdb_demangle (mangled,
11128 (DMGL_PARAMS | DMGL_ANSI
11129 | DMGL_RET_DROP)));
a766d390 11130 }
900e11f9 11131 if (demangled)
791afaa2 11132 canon = demangled.get ();
900e11f9
JK
11133 else
11134 {
11135 canon = mangled;
11136 need_copy = 0;
11137 }
11138 }
11139
11140 if (canon == NULL || check_physname)
11141 {
11142 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11143
11144 if (canon != NULL && strcmp (physname, canon) != 0)
11145 {
11146 /* It may not mean a bug in GDB. The compiler could also
11147 compute DW_AT_linkage_name incorrectly. But in such case
11148 GDB would need to be bug-to-bug compatible. */
11149
b98664d3 11150 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11151 "(from linkage <%s>) - DIE at %s [in module %s]"),
11152 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11153 objfile_name (objfile));
900e11f9
JK
11154
11155 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11156 is available here - over computed PHYSNAME. It is safer
11157 against both buggy GDB and buggy compilers. */
11158
11159 retval = canon;
11160 }
11161 else
11162 {
11163 retval = physname;
11164 need_copy = 0;
11165 }
11166 }
11167 else
11168 retval = canon;
11169
11170 if (need_copy)
021887d8 11171 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11172
900e11f9 11173 return retval;
0114d602
DJ
11174}
11175
74921315
KS
11176/* Inspect DIE in CU for a namespace alias. If one exists, record
11177 a new symbol for it.
11178
11179 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11180
11181static int
11182read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11183{
11184 struct attribute *attr;
11185
11186 /* If the die does not have a name, this is not a namespace
11187 alias. */
11188 attr = dwarf2_attr (die, DW_AT_name, cu);
11189 if (attr != NULL)
11190 {
11191 int num;
11192 struct die_info *d = die;
11193 struct dwarf2_cu *imported_cu = cu;
11194
11195 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11196 keep inspecting DIEs until we hit the underlying import. */
11197#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11198 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11199 {
11200 attr = dwarf2_attr (d, DW_AT_import, cu);
11201 if (attr == NULL)
11202 break;
11203
11204 d = follow_die_ref (d, attr, &imported_cu);
11205 if (d->tag != DW_TAG_imported_declaration)
11206 break;
11207 }
11208
11209 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11210 {
b98664d3 11211 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11212 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11213 return 0;
11214 }
11215
11216 if (attr != NULL)
11217 {
11218 struct type *type;
9c541725 11219 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11220
9c541725 11221 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11222 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11223 {
11224 /* This declaration is a global namespace alias. Add
11225 a symbol for it whose type is the aliased namespace. */
11226 new_symbol (die, type, cu);
11227 return 1;
11228 }
11229 }
11230 }
11231
11232 return 0;
11233}
11234
22cee43f 11235/* Return the using directives repository (global or local?) to use in the
804d2729 11236 current context for CU.
22cee43f
PMR
11237
11238 For Ada, imported declarations can materialize renamings, which *may* be
11239 global. However it is impossible (for now?) in DWARF to distinguish
11240 "external" imported declarations and "static" ones. As all imported
11241 declarations seem to be static in all other languages, make them all CU-wide
11242 global only in Ada. */
11243
11244static struct using_direct **
804d2729 11245using_directives (struct dwarf2_cu *cu)
22cee43f 11246{
c24bdb02
KS
11247 if (cu->language == language_ada
11248 && cu->get_builder ()->outermost_context_p ())
11249 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11250 else
c24bdb02 11251 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11252}
11253
27aa8d6a
SW
11254/* Read the import statement specified by the given die and record it. */
11255
11256static void
11257read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11258{
518817b3 11259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11260 struct attribute *import_attr;
32019081 11261 struct die_info *imported_die, *child_die;
de4affc9 11262 struct dwarf2_cu *imported_cu;
27aa8d6a 11263 const char *imported_name;
794684b6 11264 const char *imported_name_prefix;
13387711
SW
11265 const char *canonical_name;
11266 const char *import_alias;
11267 const char *imported_declaration = NULL;
794684b6 11268 const char *import_prefix;
eb1e02fd 11269 std::vector<const char *> excludes;
13387711 11270
27aa8d6a
SW
11271 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11272 if (import_attr == NULL)
11273 {
b98664d3 11274 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11275 dwarf_tag_name (die->tag));
11276 return;
11277 }
11278
de4affc9
CC
11279 imported_cu = cu;
11280 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11281 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11282 if (imported_name == NULL)
11283 {
11284 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11285
11286 The import in the following code:
11287 namespace A
11288 {
11289 typedef int B;
11290 }
11291
11292 int main ()
11293 {
11294 using A::B;
11295 B b;
11296 return b;
11297 }
11298
11299 ...
11300 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11301 <52> DW_AT_decl_file : 1
11302 <53> DW_AT_decl_line : 6
11303 <54> DW_AT_import : <0x75>
11304 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11305 <59> DW_AT_name : B
11306 <5b> DW_AT_decl_file : 1
11307 <5c> DW_AT_decl_line : 2
11308 <5d> DW_AT_type : <0x6e>
11309 ...
11310 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11311 <76> DW_AT_byte_size : 4
11312 <77> DW_AT_encoding : 5 (signed)
11313
11314 imports the wrong die ( 0x75 instead of 0x58 ).
11315 This case will be ignored until the gcc bug is fixed. */
11316 return;
11317 }
11318
82856980
SW
11319 /* Figure out the local name after import. */
11320 import_alias = dwarf2_name (die, cu);
27aa8d6a 11321
794684b6
SW
11322 /* Figure out where the statement is being imported to. */
11323 import_prefix = determine_prefix (die, cu);
11324
11325 /* Figure out what the scope of the imported die is and prepend it
11326 to the name of the imported die. */
de4affc9 11327 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11328
f55ee35c
JK
11329 if (imported_die->tag != DW_TAG_namespace
11330 && imported_die->tag != DW_TAG_module)
794684b6 11331 {
13387711
SW
11332 imported_declaration = imported_name;
11333 canonical_name = imported_name_prefix;
794684b6 11334 }
13387711 11335 else if (strlen (imported_name_prefix) > 0)
12aaed36 11336 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11337 imported_name_prefix,
11338 (cu->language == language_d ? "." : "::"),
11339 imported_name, (char *) NULL);
13387711
SW
11340 else
11341 canonical_name = imported_name;
794684b6 11342
32019081
JK
11343 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11344 for (child_die = die->child; child_die && child_die->tag;
11345 child_die = sibling_die (child_die))
11346 {
11347 /* DWARF-4: A Fortran use statement with a “rename list” may be
11348 represented by an imported module entry with an import attribute
11349 referring to the module and owned entries corresponding to those
11350 entities that are renamed as part of being imported. */
11351
11352 if (child_die->tag != DW_TAG_imported_declaration)
11353 {
b98664d3 11354 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11355 "- DIE at %s [in module %s]"),
11356 sect_offset_str (child_die->sect_off),
11357 objfile_name (objfile));
32019081
JK
11358 continue;
11359 }
11360
11361 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11362 if (import_attr == NULL)
11363 {
b98664d3 11364 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11365 dwarf_tag_name (child_die->tag));
11366 continue;
11367 }
11368
11369 imported_cu = cu;
11370 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11371 &imported_cu);
11372 imported_name = dwarf2_name (imported_die, imported_cu);
11373 if (imported_name == NULL)
11374 {
b98664d3 11375 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11376 "imported name - DIE at %s [in module %s]"),
11377 sect_offset_str (child_die->sect_off),
11378 objfile_name (objfile));
32019081
JK
11379 continue;
11380 }
11381
eb1e02fd 11382 excludes.push_back (imported_name);
32019081
JK
11383
11384 process_die (child_die, cu);
11385 }
11386
804d2729 11387 add_using_directive (using_directives (cu),
22cee43f
PMR
11388 import_prefix,
11389 canonical_name,
11390 import_alias,
11391 imported_declaration,
11392 excludes,
11393 0,
11394 &objfile->objfile_obstack);
27aa8d6a
SW
11395}
11396
5230b05a
WT
11397/* ICC<14 does not output the required DW_AT_declaration on incomplete
11398 types, but gives them a size of zero. Starting with version 14,
11399 ICC is compatible with GCC. */
11400
9068261f 11401static bool
5230b05a
WT
11402producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11403{
11404 if (!cu->checked_producer)
11405 check_producer (cu);
11406
11407 return cu->producer_is_icc_lt_14;
11408}
11409
eb77c9df
AB
11410/* ICC generates a DW_AT_type for C void functions. This was observed on
11411 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11412 which says that void functions should not have a DW_AT_type. */
11413
11414static bool
11415producer_is_icc (struct dwarf2_cu *cu)
11416{
11417 if (!cu->checked_producer)
11418 check_producer (cu);
11419
11420 return cu->producer_is_icc;
11421}
11422
1b80a9fa
JK
11423/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11424 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11425 this, it was first present in GCC release 4.3.0. */
11426
9068261f 11427static bool
1b80a9fa
JK
11428producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11429{
11430 if (!cu->checked_producer)
11431 check_producer (cu);
11432
11433 return cu->producer_is_gcc_lt_4_3;
11434}
11435
d721ba37
PA
11436static file_and_directory
11437find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11438{
d721ba37
PA
11439 file_and_directory res;
11440
9291a0cd
TT
11441 /* Find the filename. Do not use dwarf2_name here, since the filename
11442 is not a source language identifier. */
d721ba37
PA
11443 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11444 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11445
d721ba37
PA
11446 if (res.comp_dir == NULL
11447 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11448 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11449 {
d721ba37
PA
11450 res.comp_dir_storage = ldirname (res.name);
11451 if (!res.comp_dir_storage.empty ())
11452 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11453 }
d721ba37 11454 if (res.comp_dir != NULL)
9291a0cd
TT
11455 {
11456 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11457 directory, get rid of it. */
d721ba37 11458 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11459
d721ba37
PA
11460 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11461 res.comp_dir = cp + 1;
9291a0cd
TT
11462 }
11463
d721ba37
PA
11464 if (res.name == NULL)
11465 res.name = "<unknown>";
11466
11467 return res;
9291a0cd
TT
11468}
11469
f4dc4d17
DE
11470/* Handle DW_AT_stmt_list for a compilation unit.
11471 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11472 COMP_DIR is the compilation directory. LOWPC is passed to
11473 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11474
11475static void
11476handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11477 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11478{
518817b3
SM
11479 struct dwarf2_per_objfile *dwarf2_per_objfile
11480 = cu->per_cu->dwarf2_per_objfile;
527f3840 11481 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11482 struct attribute *attr;
527f3840
JK
11483 struct line_header line_header_local;
11484 hashval_t line_header_local_hash;
527f3840
JK
11485 void **slot;
11486 int decode_mapping;
2ab95328 11487
f4dc4d17
DE
11488 gdb_assert (! cu->per_cu->is_debug_types);
11489
2ab95328 11490 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11491 if (attr == NULL)
11492 return;
11493
9c541725 11494 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11495
11496 /* The line header hash table is only created if needed (it exists to
11497 prevent redundant reading of the line table for partial_units).
11498 If we're given a partial_unit, we'll need it. If we're given a
11499 compile_unit, then use the line header hash table if it's already
11500 created, but don't create one just yet. */
11501
11502 if (dwarf2_per_objfile->line_header_hash == NULL
11503 && die->tag == DW_TAG_partial_unit)
2ab95328 11504 {
527f3840
JK
11505 dwarf2_per_objfile->line_header_hash
11506 = htab_create_alloc_ex (127, line_header_hash_voidp,
11507 line_header_eq_voidp,
11508 free_line_header_voidp,
11509 &objfile->objfile_obstack,
11510 hashtab_obstack_allocate,
11511 dummy_obstack_deallocate);
11512 }
2ab95328 11513
9c541725 11514 line_header_local.sect_off = line_offset;
527f3840
JK
11515 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11516 line_header_local_hash = line_header_hash (&line_header_local);
11517 if (dwarf2_per_objfile->line_header_hash != NULL)
11518 {
11519 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11520 &line_header_local,
11521 line_header_local_hash, NO_INSERT);
11522
11523 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11524 is not present in *SLOT (since if there is something in *SLOT then
11525 it will be for a partial_unit). */
11526 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11527 {
527f3840 11528 gdb_assert (*slot != NULL);
9a3c8263 11529 cu->line_header = (struct line_header *) *slot;
527f3840 11530 return;
dee91e82 11531 }
2ab95328 11532 }
527f3840
JK
11533
11534 /* dwarf_decode_line_header does not yet provide sufficient information.
11535 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11536 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11537 if (lh == NULL)
527f3840 11538 return;
4c8aa72d
PA
11539
11540 cu->line_header = lh.release ();
11541 cu->line_header_die_owner = die;
527f3840
JK
11542
11543 if (dwarf2_per_objfile->line_header_hash == NULL)
11544 slot = NULL;
11545 else
11546 {
11547 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11548 &line_header_local,
11549 line_header_local_hash, INSERT);
11550 gdb_assert (slot != NULL);
11551 }
11552 if (slot != NULL && *slot == NULL)
11553 {
11554 /* This newly decoded line number information unit will be owned
11555 by line_header_hash hash table. */
11556 *slot = cu->line_header;
4c8aa72d 11557 cu->line_header_die_owner = NULL;
527f3840
JK
11558 }
11559 else
11560 {
11561 /* We cannot free any current entry in (*slot) as that struct line_header
11562 may be already used by multiple CUs. Create only temporary decoded
11563 line_header for this CU - it may happen at most once for each line
11564 number information unit. And if we're not using line_header_hash
11565 then this is what we want as well. */
11566 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11567 }
11568 decode_mapping = (die->tag != DW_TAG_partial_unit);
11569 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11570 decode_mapping);
fff8551c 11571
2ab95328
TT
11572}
11573
95554aad 11574/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11575
c906108c 11576static void
e7c27a73 11577read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11578{
518817b3
SM
11579 struct dwarf2_per_objfile *dwarf2_per_objfile
11580 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11581 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11582 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11583 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11584 CORE_ADDR highpc = ((CORE_ADDR) 0);
11585 struct attribute *attr;
c906108c 11586 struct die_info *child_die;
e142c38c 11587 CORE_ADDR baseaddr;
6e70227d 11588
380618d6 11589 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11591
fae299cd 11592 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11593
11594 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11595 from finish_block. */
2acceee2 11596 if (lowpc == ((CORE_ADDR) -1))
c906108c 11597 lowpc = highpc;
3e29f34a 11598 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11599
d721ba37 11600 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11601
f4b8a18d
KW
11602 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11603 standardised yet. As a workaround for the language detection we fall
11604 back to the DW_AT_producer string. */
11605 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11606 cu->language = language_opencl;
11607
3019eac3
DE
11608 /* Similar hack for Go. */
11609 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11610 set_cu_language (DW_LANG_Go, cu);
11611
c24bdb02 11612 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11613
11614 /* Decode line number information if present. We do this before
11615 processing child DIEs, so that the line header table is available
11616 for DW_AT_decl_file. */
d721ba37 11617 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11618
11619 /* Process all dies in compilation unit. */
11620 if (die->child != NULL)
11621 {
11622 child_die = die->child;
11623 while (child_die && child_die->tag)
11624 {
11625 process_die (child_die, cu);
11626 child_die = sibling_die (child_die);
11627 }
11628 }
11629
11630 /* Decode macro information, if present. Dwarf 2 macro information
11631 refers to information in the line number info statement program
11632 header, so we can only read it if we've read the header
11633 successfully. */
0af92d60
JK
11634 attr = dwarf2_attr (die, DW_AT_macros, cu);
11635 if (attr == NULL)
11636 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11637 if (attr && cu->line_header)
11638 {
11639 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11640 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11641
43f3e411 11642 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11643 }
11644 else
11645 {
11646 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11647 if (attr && cu->line_header)
11648 {
11649 unsigned int macro_offset = DW_UNSND (attr);
11650
43f3e411 11651 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11652 }
11653 }
3019eac3
DE
11654}
11655
c24bdb02
KS
11656void
11657dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11658{
f4dc4d17
DE
11659 struct type_unit_group *tu_group;
11660 int first_time;
3019eac3 11661 struct attribute *attr;
9c541725 11662 unsigned int i;
0186c6a7 11663 struct signatured_type *sig_type;
3019eac3 11664
f4dc4d17 11665 gdb_assert (per_cu->is_debug_types);
0186c6a7 11666 sig_type = (struct signatured_type *) per_cu;
3019eac3 11667
c24bdb02 11668 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11669
f4dc4d17 11670 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11671 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11672 if (sig_type->type_unit_group == NULL)
c24bdb02 11673 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11674 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11675
11676 /* If we've already processed this stmt_list there's no real need to
11677 do it again, we could fake it and just recreate the part we need
11678 (file name,index -> symtab mapping). If data shows this optimization
11679 is useful we can do it then. */
43f3e411 11680 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11681
11682 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11683 debug info. */
fff8551c 11684 line_header_up lh;
f4dc4d17 11685 if (attr != NULL)
3019eac3 11686 {
9c541725 11687 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11688 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11689 }
11690 if (lh == NULL)
11691 {
11692 if (first_time)
c24bdb02 11693 start_symtab ("", NULL, 0);
f4dc4d17
DE
11694 else
11695 {
11696 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11697 gdb_assert (m_builder == nullptr);
804d2729 11698 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11699 m_builder.reset (new struct buildsym_compunit
11700 (COMPUNIT_OBJFILE (cust), "",
11701 COMPUNIT_DIRNAME (cust),
11702 compunit_language (cust),
11703 0, cust));
f4dc4d17 11704 }
f4dc4d17 11705 return;
3019eac3
DE
11706 }
11707
c24bdb02
KS
11708 line_header = lh.release ();
11709 line_header_die_owner = die;
3019eac3 11710
f4dc4d17
DE
11711 if (first_time)
11712 {
c24bdb02 11713 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11714
1fd60fc0
DE
11715 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11716 still initializing it, and our caller (a few levels up)
11717 process_full_type_unit still needs to know if this is the first
11718 time. */
11719
c24bdb02 11720 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11721 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11722 line_header->file_names.size ());
3019eac3 11723
c24bdb02 11724 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11725 {
c24bdb02 11726 file_entry &fe = line_header->file_names[i];
3019eac3 11727
c24bdb02
KS
11728 dwarf2_start_subfile (this, fe.name,
11729 fe.include_dir (line_header));
11730 buildsym_compunit *b = get_builder ();
11731 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11732 {
4c8aa72d
PA
11733 /* NOTE: start_subfile will recognize when it's been
11734 passed a file it has already seen. So we can't
11735 assume there's a simple mapping from
11736 cu->line_header->file_names to subfiles, plus
11737 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11738 b->get_current_subfile ()->symtab
11739 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11740 }
11741
c24bdb02 11742 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11743 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11744 }
11745 }
11746 else
3019eac3 11747 {
c24bdb02 11748 gdb_assert (m_builder == nullptr);
804d2729 11749 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11750 m_builder.reset (new struct buildsym_compunit
11751 (COMPUNIT_OBJFILE (cust), "",
11752 COMPUNIT_DIRNAME (cust),
11753 compunit_language (cust),
11754 0, cust));
f4dc4d17 11755
c24bdb02 11756 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11757 {
c24bdb02 11758 file_entry &fe = line_header->file_names[i];
f4dc4d17 11759
4c8aa72d 11760 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11761 }
3019eac3
DE
11762 }
11763
f4dc4d17
DE
11764 /* The main symtab is allocated last. Type units don't have DW_AT_name
11765 so they don't have a "real" (so to speak) symtab anyway.
11766 There is later code that will assign the main symtab to all symbols
11767 that don't have one. We need to handle the case of a symbol with a
11768 missing symtab (DW_AT_decl_file) anyway. */
11769}
3019eac3 11770
f4dc4d17
DE
11771/* Process DW_TAG_type_unit.
11772 For TUs we want to skip the first top level sibling if it's not the
11773 actual type being defined by this TU. In this case the first top
11774 level sibling is there to provide context only. */
3019eac3 11775
f4dc4d17
DE
11776static void
11777read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11778{
11779 struct die_info *child_die;
3019eac3 11780
f4dc4d17
DE
11781 prepare_one_comp_unit (cu, die, language_minimal);
11782
11783 /* Initialize (or reinitialize) the machinery for building symtabs.
11784 We do this before processing child DIEs, so that the line header table
11785 is available for DW_AT_decl_file. */
c24bdb02 11786 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11787
11788 if (die->child != NULL)
11789 {
11790 child_die = die->child;
11791 while (child_die && child_die->tag)
11792 {
11793 process_die (child_die, cu);
11794 child_die = sibling_die (child_die);
11795 }
11796 }
3019eac3
DE
11797}
11798\f
80626a55
DE
11799/* DWO/DWP files.
11800
11801 http://gcc.gnu.org/wiki/DebugFission
11802 http://gcc.gnu.org/wiki/DebugFissionDWP
11803
11804 To simplify handling of both DWO files ("object" files with the DWARF info)
11805 and DWP files (a file with the DWOs packaged up into one file), we treat
11806 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11807
11808static hashval_t
11809hash_dwo_file (const void *item)
11810{
9a3c8263 11811 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11812 hashval_t hash;
3019eac3 11813
a2ce51a0
DE
11814 hash = htab_hash_string (dwo_file->dwo_name);
11815 if (dwo_file->comp_dir != NULL)
11816 hash += htab_hash_string (dwo_file->comp_dir);
11817 return hash;
3019eac3
DE
11818}
11819
11820static int
11821eq_dwo_file (const void *item_lhs, const void *item_rhs)
11822{
9a3c8263
SM
11823 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11824 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11825
a2ce51a0
DE
11826 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11827 return 0;
11828 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11829 return lhs->comp_dir == rhs->comp_dir;
11830 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11831}
11832
11833/* Allocate a hash table for DWO files. */
11834
51ac9db5 11835static htab_up
ed2dc618 11836allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11837{
51ac9db5
SM
11838 auto delete_dwo_file = [] (void *item)
11839 {
11840 struct dwo_file *dwo_file = (struct dwo_file *) item;
11841
11842 delete dwo_file;
11843 };
11844
11845 return htab_up (htab_create_alloc_ex (41,
11846 hash_dwo_file,
11847 eq_dwo_file,
11848 delete_dwo_file,
11849 &objfile->objfile_obstack,
11850 hashtab_obstack_allocate,
11851 dummy_obstack_deallocate));
3019eac3
DE
11852}
11853
80626a55
DE
11854/* Lookup DWO file DWO_NAME. */
11855
11856static void **
ed2dc618
SM
11857lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11858 const char *dwo_name,
11859 const char *comp_dir)
80626a55
DE
11860{
11861 struct dwo_file find_entry;
11862 void **slot;
11863
11864 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11865 dwarf2_per_objfile->dwo_files
11866 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11867
0ac5b59e
DE
11868 find_entry.dwo_name = dwo_name;
11869 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11870 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11871 INSERT);
80626a55
DE
11872
11873 return slot;
11874}
11875
3019eac3
DE
11876static hashval_t
11877hash_dwo_unit (const void *item)
11878{
9a3c8263 11879 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11880
11881 /* This drops the top 32 bits of the id, but is ok for a hash. */
11882 return dwo_unit->signature;
11883}
11884
11885static int
11886eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11887{
9a3c8263
SM
11888 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11889 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11890
11891 /* The signature is assumed to be unique within the DWO file.
11892 So while object file CU dwo_id's always have the value zero,
11893 that's OK, assuming each object file DWO file has only one CU,
11894 and that's the rule for now. */
11895 return lhs->signature == rhs->signature;
11896}
11897
11898/* Allocate a hash table for DWO CUs,TUs.
11899 There is one of these tables for each of CUs,TUs for each DWO file. */
11900
11901static htab_t
11902allocate_dwo_unit_table (struct objfile *objfile)
11903{
11904 /* Start out with a pretty small number.
11905 Generally DWO files contain only one CU and maybe some TUs. */
11906 return htab_create_alloc_ex (3,
11907 hash_dwo_unit,
11908 eq_dwo_unit,
11909 NULL,
11910 &objfile->objfile_obstack,
11911 hashtab_obstack_allocate,
11912 dummy_obstack_deallocate);
11913}
11914
80626a55 11915/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11916
19c3d4c9 11917struct create_dwo_cu_data
3019eac3
DE
11918{
11919 struct dwo_file *dwo_file;
19c3d4c9 11920 struct dwo_unit dwo_unit;
3019eac3
DE
11921};
11922
19c3d4c9 11923/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11924
11925static void
19c3d4c9
DE
11926create_dwo_cu_reader (const struct die_reader_specs *reader,
11927 const gdb_byte *info_ptr,
11928 struct die_info *comp_unit_die,
11929 int has_children,
11930 void *datap)
3019eac3
DE
11931{
11932 struct dwarf2_cu *cu = reader->cu;
9c541725 11933 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11934 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11935 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11936 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11937 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11938
a084a2a6
AT
11939 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11940 if (!signature.has_value ())
3019eac3 11941 {
b98664d3 11942 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11943 " its dwo_id [in module %s]"),
9d8780f0 11944 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11945 return;
11946 }
11947
3019eac3 11948 dwo_unit->dwo_file = dwo_file;
a084a2a6 11949 dwo_unit->signature = *signature;
8a0459fd 11950 dwo_unit->section = section;
9c541725 11951 dwo_unit->sect_off = sect_off;
3019eac3
DE
11952 dwo_unit->length = cu->per_cu->length;
11953
b4f54984 11954 if (dwarf_read_debug)
9d8780f0
SM
11955 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11956 sect_offset_str (sect_off),
9c541725 11957 hex_string (dwo_unit->signature));
3019eac3
DE
11958}
11959
33c5cd75 11960/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11961 Note: This function processes DWO files only, not DWP files. */
3019eac3 11962
33c5cd75 11963static void
ed2dc618
SM
11964create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11965 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11966 htab_t &cus_htab)
3019eac3
DE
11967{
11968 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11969 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11970
33c5cd75
DB
11971 dwarf2_read_section (objfile, &section);
11972 info_ptr = section.buffer;
3019eac3
DE
11973
11974 if (info_ptr == NULL)
33c5cd75 11975 return;
3019eac3 11976
b4f54984 11977 if (dwarf_read_debug)
19c3d4c9
DE
11978 {
11979 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11980 get_section_name (&section),
11981 get_section_file_name (&section));
19c3d4c9 11982 }
3019eac3 11983
33c5cd75 11984 end_ptr = info_ptr + section.size;
3019eac3
DE
11985 while (info_ptr < end_ptr)
11986 {
11987 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11988 struct create_dwo_cu_data create_dwo_cu_data;
11989 struct dwo_unit *dwo_unit;
11990 void **slot;
11991 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11992
19c3d4c9
DE
11993 memset (&create_dwo_cu_data.dwo_unit, 0,
11994 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11995 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11996 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11997 per_cu.is_debug_types = 0;
33c5cd75
DB
11998 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11999 per_cu.section = &section;
c5ed0576 12000 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12001
12002 init_cutu_and_read_dies_no_follow (
12003 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12004 info_ptr += per_cu.length;
12005
12006 // If the unit could not be parsed, skip it.
12007 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12008 continue;
3019eac3 12009
33c5cd75
DB
12010 if (cus_htab == NULL)
12011 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12012
33c5cd75
DB
12013 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12014 *dwo_unit = create_dwo_cu_data.dwo_unit;
12015 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12016 gdb_assert (slot != NULL);
12017 if (*slot != NULL)
19c3d4c9 12018 {
33c5cd75
DB
12019 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12020 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12021
b98664d3 12022 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12023 " the entry at offset %s, signature %s"),
12024 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12025 hex_string (dwo_unit->signature));
19c3d4c9 12026 }
33c5cd75 12027 *slot = (void *)dwo_unit;
3019eac3 12028 }
3019eac3
DE
12029}
12030
80626a55
DE
12031/* DWP file .debug_{cu,tu}_index section format:
12032 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12033
d2415c6c
DE
12034 DWP Version 1:
12035
80626a55
DE
12036 Both index sections have the same format, and serve to map a 64-bit
12037 signature to a set of section numbers. Each section begins with a header,
12038 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12039 indexes, and a pool of 32-bit section numbers. The index sections will be
12040 aligned at 8-byte boundaries in the file.
12041
d2415c6c
DE
12042 The index section header consists of:
12043
12044 V, 32 bit version number
12045 -, 32 bits unused
12046 N, 32 bit number of compilation units or type units in the index
12047 M, 32 bit number of slots in the hash table
80626a55 12048
d2415c6c 12049 Numbers are recorded using the byte order of the application binary.
80626a55 12050
d2415c6c
DE
12051 The hash table begins at offset 16 in the section, and consists of an array
12052 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12053 order of the application binary). Unused slots in the hash table are 0.
12054 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12055
d2415c6c
DE
12056 The parallel table begins immediately after the hash table
12057 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12058 array of 32-bit indexes (using the byte order of the application binary),
12059 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12060 table contains a 32-bit index into the pool of section numbers. For unused
12061 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12062
73869dc2
DE
12063 The pool of section numbers begins immediately following the hash table
12064 (at offset 16 + 12 * M from the beginning of the section). The pool of
12065 section numbers consists of an array of 32-bit words (using the byte order
12066 of the application binary). Each item in the array is indexed starting
12067 from 0. The hash table entry provides the index of the first section
12068 number in the set. Additional section numbers in the set follow, and the
12069 set is terminated by a 0 entry (section number 0 is not used in ELF).
12070
12071 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12072 section must be the first entry in the set, and the .debug_abbrev.dwo must
12073 be the second entry. Other members of the set may follow in any order.
12074
12075 ---
12076
12077 DWP Version 2:
12078
12079 DWP Version 2 combines all the .debug_info, etc. sections into one,
12080 and the entries in the index tables are now offsets into these sections.
12081 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12082 section.
12083
12084 Index Section Contents:
12085 Header
12086 Hash Table of Signatures dwp_hash_table.hash_table
12087 Parallel Table of Indices dwp_hash_table.unit_table
12088 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12089 Table of Section Sizes dwp_hash_table.v2.sizes
12090
12091 The index section header consists of:
12092
12093 V, 32 bit version number
12094 L, 32 bit number of columns in the table of section offsets
12095 N, 32 bit number of compilation units or type units in the index
12096 M, 32 bit number of slots in the hash table
12097
12098 Numbers are recorded using the byte order of the application binary.
12099
12100 The hash table has the same format as version 1.
12101 The parallel table of indices has the same format as version 1,
12102 except that the entries are origin-1 indices into the table of sections
12103 offsets and the table of section sizes.
12104
12105 The table of offsets begins immediately following the parallel table
12106 (at offset 16 + 12 * M from the beginning of the section). The table is
12107 a two-dimensional array of 32-bit words (using the byte order of the
12108 application binary), with L columns and N+1 rows, in row-major order.
12109 Each row in the array is indexed starting from 0. The first row provides
12110 a key to the remaining rows: each column in this row provides an identifier
12111 for a debug section, and the offsets in the same column of subsequent rows
12112 refer to that section. The section identifiers are:
12113
12114 DW_SECT_INFO 1 .debug_info.dwo
12115 DW_SECT_TYPES 2 .debug_types.dwo
12116 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12117 DW_SECT_LINE 4 .debug_line.dwo
12118 DW_SECT_LOC 5 .debug_loc.dwo
12119 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12120 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12121 DW_SECT_MACRO 8 .debug_macro.dwo
12122
12123 The offsets provided by the CU and TU index sections are the base offsets
12124 for the contributions made by each CU or TU to the corresponding section
12125 in the package file. Each CU and TU header contains an abbrev_offset
12126 field, used to find the abbreviations table for that CU or TU within the
12127 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12128 be interpreted as relative to the base offset given in the index section.
12129 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12130 should be interpreted as relative to the base offset for .debug_line.dwo,
12131 and offsets into other debug sections obtained from DWARF attributes should
12132 also be interpreted as relative to the corresponding base offset.
12133
12134 The table of sizes begins immediately following the table of offsets.
12135 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12136 with L columns and N rows, in row-major order. Each row in the array is
12137 indexed starting from 1 (row 0 is shared by the two tables).
12138
12139 ---
12140
12141 Hash table lookup is handled the same in version 1 and 2:
12142
12143 We assume that N and M will not exceed 2^32 - 1.
12144 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12145
d2415c6c
DE
12146 Given a 64-bit compilation unit signature or a type signature S, an entry
12147 in the hash table is located as follows:
80626a55 12148
d2415c6c
DE
12149 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12150 the low-order k bits all set to 1.
80626a55 12151
d2415c6c 12152 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12153
d2415c6c
DE
12154 3) If the hash table entry at index H matches the signature, use that
12155 entry. If the hash table entry at index H is unused (all zeroes),
12156 terminate the search: the signature is not present in the table.
80626a55 12157
d2415c6c 12158 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12159
d2415c6c 12160 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12161 to stop at an unused slot or find the match. */
80626a55
DE
12162
12163/* Create a hash table to map DWO IDs to their CU/TU entry in
12164 .debug_{info,types}.dwo in DWP_FILE.
12165 Returns NULL if there isn't one.
12166 Note: This function processes DWP files only, not DWO files. */
12167
12168static struct dwp_hash_table *
ed2dc618
SM
12169create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12170 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12171{
12172 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12173 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12174 const gdb_byte *index_ptr, *index_end;
80626a55 12175 struct dwarf2_section_info *index;
73869dc2 12176 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12177 struct dwp_hash_table *htab;
12178
12179 if (is_debug_types)
12180 index = &dwp_file->sections.tu_index;
12181 else
12182 index = &dwp_file->sections.cu_index;
12183
12184 if (dwarf2_section_empty_p (index))
12185 return NULL;
12186 dwarf2_read_section (objfile, index);
12187
12188 index_ptr = index->buffer;
12189 index_end = index_ptr + index->size;
12190
12191 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12192 index_ptr += 4;
12193 if (version == 2)
12194 nr_columns = read_4_bytes (dbfd, index_ptr);
12195 else
12196 nr_columns = 0;
12197 index_ptr += 4;
80626a55
DE
12198 nr_units = read_4_bytes (dbfd, index_ptr);
12199 index_ptr += 4;
12200 nr_slots = read_4_bytes (dbfd, index_ptr);
12201 index_ptr += 4;
12202
73869dc2 12203 if (version != 1 && version != 2)
80626a55 12204 {
21aa081e 12205 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12206 " [in module %s]"),
21aa081e 12207 pulongest (version), dwp_file->name);
80626a55
DE
12208 }
12209 if (nr_slots != (nr_slots & -nr_slots))
12210 {
21aa081e 12211 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12212 " is not power of 2 [in module %s]"),
21aa081e 12213 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12214 }
12215
12216 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12217 htab->version = version;
12218 htab->nr_columns = nr_columns;
80626a55
DE
12219 htab->nr_units = nr_units;
12220 htab->nr_slots = nr_slots;
12221 htab->hash_table = index_ptr;
12222 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12223
12224 /* Exit early if the table is empty. */
12225 if (nr_slots == 0 || nr_units == 0
12226 || (version == 2 && nr_columns == 0))
12227 {
12228 /* All must be zero. */
12229 if (nr_slots != 0 || nr_units != 0
12230 || (version == 2 && nr_columns != 0))
12231 {
b98664d3 12232 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12233 " all zero [in modules %s]"),
12234 dwp_file->name);
12235 }
12236 return htab;
12237 }
12238
12239 if (version == 1)
12240 {
12241 htab->section_pool.v1.indices =
12242 htab->unit_table + sizeof (uint32_t) * nr_slots;
12243 /* It's harder to decide whether the section is too small in v1.
12244 V1 is deprecated anyway so we punt. */
12245 }
12246 else
12247 {
12248 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12249 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12250 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12251 /* Reverse map for error checking. */
12252 int ids_seen[DW_SECT_MAX + 1];
12253 int i;
12254
12255 if (nr_columns < 2)
12256 {
12257 error (_("Dwarf Error: bad DWP hash table, too few columns"
12258 " in section table [in module %s]"),
12259 dwp_file->name);
12260 }
12261 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12262 {
12263 error (_("Dwarf Error: bad DWP hash table, too many columns"
12264 " in section table [in module %s]"),
12265 dwp_file->name);
12266 }
04fd5eed
GB
12267 memset (ids, 255, sizeof_ids);
12268 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12269 for (i = 0; i < nr_columns; ++i)
12270 {
12271 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12272
12273 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12274 {
12275 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12276 " in section table [in module %s]"),
12277 id, dwp_file->name);
12278 }
12279 if (ids_seen[id] != -1)
12280 {
12281 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12282 " id %d in section table [in module %s]"),
12283 id, dwp_file->name);
12284 }
12285 ids_seen[id] = i;
12286 ids[i] = id;
12287 }
12288 /* Must have exactly one info or types section. */
12289 if (((ids_seen[DW_SECT_INFO] != -1)
12290 + (ids_seen[DW_SECT_TYPES] != -1))
12291 != 1)
12292 {
12293 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12294 " DWO info/types section [in module %s]"),
12295 dwp_file->name);
12296 }
12297 /* Must have an abbrev section. */
12298 if (ids_seen[DW_SECT_ABBREV] == -1)
12299 {
12300 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12301 " section [in module %s]"),
12302 dwp_file->name);
12303 }
12304 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12305 htab->section_pool.v2.sizes =
12306 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12307 * nr_units * nr_columns);
12308 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12309 * nr_units * nr_columns))
12310 > index_end)
12311 {
12312 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12313 " [in module %s]"),
12314 dwp_file->name);
12315 }
12316 }
80626a55
DE
12317
12318 return htab;
12319}
12320
12321/* Update SECTIONS with the data from SECTP.
12322
12323 This function is like the other "locate" section routines that are
12324 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12325 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12326
12327 The result is non-zero for success, or zero if an error was found. */
12328
12329static int
73869dc2
DE
12330locate_v1_virtual_dwo_sections (asection *sectp,
12331 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12332{
12333 const struct dwop_section_names *names = &dwop_section_names;
12334
12335 if (section_is_p (sectp->name, &names->abbrev_dwo))
12336 {
12337 /* There can be only one. */
049412e3 12338 if (sections->abbrev.s.section != NULL)
80626a55 12339 return 0;
049412e3 12340 sections->abbrev.s.section = sectp;
fd361982 12341 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12342 }
12343 else if (section_is_p (sectp->name, &names->info_dwo)
12344 || section_is_p (sectp->name, &names->types_dwo))
12345 {
12346 /* There can be only one. */
049412e3 12347 if (sections->info_or_types.s.section != NULL)
80626a55 12348 return 0;
049412e3 12349 sections->info_or_types.s.section = sectp;
fd361982 12350 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12351 }
12352 else if (section_is_p (sectp->name, &names->line_dwo))
12353 {
12354 /* There can be only one. */
049412e3 12355 if (sections->line.s.section != NULL)
80626a55 12356 return 0;
049412e3 12357 sections->line.s.section = sectp;
fd361982 12358 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12359 }
12360 else if (section_is_p (sectp->name, &names->loc_dwo))
12361 {
12362 /* There can be only one. */
049412e3 12363 if (sections->loc.s.section != NULL)
80626a55 12364 return 0;
049412e3 12365 sections->loc.s.section = sectp;
fd361982 12366 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12367 }
12368 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12369 {
12370 /* There can be only one. */
049412e3 12371 if (sections->macinfo.s.section != NULL)
80626a55 12372 return 0;
049412e3 12373 sections->macinfo.s.section = sectp;
fd361982 12374 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12375 }
12376 else if (section_is_p (sectp->name, &names->macro_dwo))
12377 {
12378 /* There can be only one. */
049412e3 12379 if (sections->macro.s.section != NULL)
80626a55 12380 return 0;
049412e3 12381 sections->macro.s.section = sectp;
fd361982 12382 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12383 }
12384 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12385 {
12386 /* There can be only one. */
049412e3 12387 if (sections->str_offsets.s.section != NULL)
80626a55 12388 return 0;
049412e3 12389 sections->str_offsets.s.section = sectp;
fd361982 12390 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12391 }
12392 else
12393 {
12394 /* No other kind of section is valid. */
12395 return 0;
12396 }
12397
12398 return 1;
12399}
12400
73869dc2
DE
12401/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12402 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12403 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12404 This is for DWP version 1 files. */
80626a55
DE
12405
12406static struct dwo_unit *
ed2dc618
SM
12407create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12408 struct dwp_file *dwp_file,
73869dc2
DE
12409 uint32_t unit_index,
12410 const char *comp_dir,
12411 ULONGEST signature, int is_debug_types)
80626a55
DE
12412{
12413 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12414 const struct dwp_hash_table *dwp_htab =
12415 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12416 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12417 const char *kind = is_debug_types ? "TU" : "CU";
12418 struct dwo_file *dwo_file;
12419 struct dwo_unit *dwo_unit;
73869dc2 12420 struct virtual_v1_dwo_sections sections;
80626a55 12421 void **dwo_file_slot;
80626a55
DE
12422 int i;
12423
73869dc2
DE
12424 gdb_assert (dwp_file->version == 1);
12425
b4f54984 12426 if (dwarf_read_debug)
80626a55 12427 {
73869dc2 12428 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12429 kind,
73869dc2 12430 pulongest (unit_index), hex_string (signature),
80626a55
DE
12431 dwp_file->name);
12432 }
12433
19ac8c2e 12434 /* Fetch the sections of this DWO unit.
80626a55
DE
12435 Put a limit on the number of sections we look for so that bad data
12436 doesn't cause us to loop forever. */
12437
73869dc2 12438#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12439 (1 /* .debug_info or .debug_types */ \
12440 + 1 /* .debug_abbrev */ \
12441 + 1 /* .debug_line */ \
12442 + 1 /* .debug_loc */ \
12443 + 1 /* .debug_str_offsets */ \
19ac8c2e 12444 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12445 + 1 /* trailing zero */)
12446
12447 memset (&sections, 0, sizeof (sections));
80626a55 12448
73869dc2 12449 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12450 {
12451 asection *sectp;
12452 uint32_t section_nr =
12453 read_4_bytes (dbfd,
73869dc2
DE
12454 dwp_htab->section_pool.v1.indices
12455 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12456
12457 if (section_nr == 0)
12458 break;
12459 if (section_nr >= dwp_file->num_sections)
12460 {
12461 error (_("Dwarf Error: bad DWP hash table, section number too large"
12462 " [in module %s]"),
12463 dwp_file->name);
12464 }
12465
12466 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12467 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12468 {
12469 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12470 " [in module %s]"),
12471 dwp_file->name);
12472 }
12473 }
12474
12475 if (i < 2
a32a8923
DE
12476 || dwarf2_section_empty_p (&sections.info_or_types)
12477 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12478 {
12479 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12480 " [in module %s]"),
12481 dwp_file->name);
12482 }
73869dc2 12483 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12484 {
12485 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12486 " [in module %s]"),
12487 dwp_file->name);
12488 }
12489
12490 /* It's easier for the rest of the code if we fake a struct dwo_file and
12491 have dwo_unit "live" in that. At least for now.
12492
12493 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12494 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12495 file, we can combine them back into a virtual DWO file to save space
12496 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12497 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12498
791afaa2
TT
12499 std::string virtual_dwo_name =
12500 string_printf ("virtual-dwo/%d-%d-%d-%d",
12501 get_section_id (&sections.abbrev),
12502 get_section_id (&sections.line),
12503 get_section_id (&sections.loc),
12504 get_section_id (&sections.str_offsets));
80626a55 12505 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12506 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12507 virtual_dwo_name.c_str (),
12508 comp_dir);
80626a55
DE
12509 /* Create one if necessary. */
12510 if (*dwo_file_slot == NULL)
12511 {
b4f54984 12512 if (dwarf_read_debug)
80626a55
DE
12513 {
12514 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12515 virtual_dwo_name.c_str ());
80626a55 12516 }
51ac9db5 12517 dwo_file = new struct dwo_file;
efba19b0
TT
12518 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12519 virtual_dwo_name);
0ac5b59e 12520 dwo_file->comp_dir = comp_dir;
80626a55
DE
12521 dwo_file->sections.abbrev = sections.abbrev;
12522 dwo_file->sections.line = sections.line;
12523 dwo_file->sections.loc = sections.loc;
12524 dwo_file->sections.macinfo = sections.macinfo;
12525 dwo_file->sections.macro = sections.macro;
12526 dwo_file->sections.str_offsets = sections.str_offsets;
12527 /* The "str" section is global to the entire DWP file. */
12528 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12529 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12530 there's no need to record it in dwo_file.
12531 Also, we can't simply record type sections in dwo_file because
12532 we record a pointer into the vector in dwo_unit. As we collect more
12533 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12534 for it, invalidating all copies of pointers into the previous
12535 contents. */
80626a55
DE
12536 *dwo_file_slot = dwo_file;
12537 }
12538 else
12539 {
b4f54984 12540 if (dwarf_read_debug)
80626a55
DE
12541 {
12542 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12543 virtual_dwo_name.c_str ());
80626a55 12544 }
9a3c8263 12545 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12546 }
80626a55
DE
12547
12548 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12549 dwo_unit->dwo_file = dwo_file;
12550 dwo_unit->signature = signature;
8d749320
SM
12551 dwo_unit->section =
12552 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12553 *dwo_unit->section = sections.info_or_types;
57d63ce2 12554 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12555
12556 return dwo_unit;
12557}
12558
73869dc2
DE
12559/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12560 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12561 piece within that section used by a TU/CU, return a virtual section
12562 of just that piece. */
12563
12564static struct dwarf2_section_info
ed2dc618
SM
12565create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12566 struct dwarf2_section_info *section,
73869dc2
DE
12567 bfd_size_type offset, bfd_size_type size)
12568{
12569 struct dwarf2_section_info result;
12570 asection *sectp;
12571
12572 gdb_assert (section != NULL);
12573 gdb_assert (!section->is_virtual);
12574
12575 memset (&result, 0, sizeof (result));
12576 result.s.containing_section = section;
dc4ccb6f 12577 result.is_virtual = true;
73869dc2
DE
12578
12579 if (size == 0)
12580 return result;
12581
12582 sectp = get_section_bfd_section (section);
12583
12584 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12585 bounds of the real section. This is a pretty-rare event, so just
12586 flag an error (easier) instead of a warning and trying to cope. */
12587 if (sectp == NULL
fd361982 12588 || offset + size > bfd_section_size (sectp))
73869dc2 12589 {
73869dc2
DE
12590 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12591 " in section %s [in module %s]"),
fd361982 12592 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12593 objfile_name (dwarf2_per_objfile->objfile));
12594 }
12595
12596 result.virtual_offset = offset;
12597 result.size = size;
12598 return result;
12599}
12600
12601/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12602 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12603 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12604 This is for DWP version 2 files. */
12605
12606static struct dwo_unit *
ed2dc618
SM
12607create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12608 struct dwp_file *dwp_file,
73869dc2
DE
12609 uint32_t unit_index,
12610 const char *comp_dir,
12611 ULONGEST signature, int is_debug_types)
12612{
12613 struct objfile *objfile = dwarf2_per_objfile->objfile;
12614 const struct dwp_hash_table *dwp_htab =
12615 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12616 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12617 const char *kind = is_debug_types ? "TU" : "CU";
12618 struct dwo_file *dwo_file;
12619 struct dwo_unit *dwo_unit;
12620 struct virtual_v2_dwo_sections sections;
12621 void **dwo_file_slot;
73869dc2
DE
12622 int i;
12623
12624 gdb_assert (dwp_file->version == 2);
12625
b4f54984 12626 if (dwarf_read_debug)
73869dc2
DE
12627 {
12628 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12629 kind,
12630 pulongest (unit_index), hex_string (signature),
12631 dwp_file->name);
12632 }
12633
12634 /* Fetch the section offsets of this DWO unit. */
12635
12636 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12637
12638 for (i = 0; i < dwp_htab->nr_columns; ++i)
12639 {
12640 uint32_t offset = read_4_bytes (dbfd,
12641 dwp_htab->section_pool.v2.offsets
12642 + (((unit_index - 1) * dwp_htab->nr_columns
12643 + i)
12644 * sizeof (uint32_t)));
12645 uint32_t size = read_4_bytes (dbfd,
12646 dwp_htab->section_pool.v2.sizes
12647 + (((unit_index - 1) * dwp_htab->nr_columns
12648 + i)
12649 * sizeof (uint32_t)));
12650
12651 switch (dwp_htab->section_pool.v2.section_ids[i])
12652 {
12653 case DW_SECT_INFO:
12654 case DW_SECT_TYPES:
12655 sections.info_or_types_offset = offset;
12656 sections.info_or_types_size = size;
12657 break;
12658 case DW_SECT_ABBREV:
12659 sections.abbrev_offset = offset;
12660 sections.abbrev_size = size;
12661 break;
12662 case DW_SECT_LINE:
12663 sections.line_offset = offset;
12664 sections.line_size = size;
12665 break;
12666 case DW_SECT_LOC:
12667 sections.loc_offset = offset;
12668 sections.loc_size = size;
12669 break;
12670 case DW_SECT_STR_OFFSETS:
12671 sections.str_offsets_offset = offset;
12672 sections.str_offsets_size = size;
12673 break;
12674 case DW_SECT_MACINFO:
12675 sections.macinfo_offset = offset;
12676 sections.macinfo_size = size;
12677 break;
12678 case DW_SECT_MACRO:
12679 sections.macro_offset = offset;
12680 sections.macro_size = size;
12681 break;
12682 }
12683 }
12684
12685 /* It's easier for the rest of the code if we fake a struct dwo_file and
12686 have dwo_unit "live" in that. At least for now.
12687
12688 The DWP file can be made up of a random collection of CUs and TUs.
12689 However, for each CU + set of TUs that came from the same original DWO
12690 file, we can combine them back into a virtual DWO file to save space
12691 (fewer struct dwo_file objects to allocate). Remember that for really
12692 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12693
791afaa2
TT
12694 std::string virtual_dwo_name =
12695 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12696 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12697 (long) (sections.line_size ? sections.line_offset : 0),
12698 (long) (sections.loc_size ? sections.loc_offset : 0),
12699 (long) (sections.str_offsets_size
12700 ? sections.str_offsets_offset : 0));
73869dc2 12701 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12702 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12703 virtual_dwo_name.c_str (),
12704 comp_dir);
73869dc2
DE
12705 /* Create one if necessary. */
12706 if (*dwo_file_slot == NULL)
12707 {
b4f54984 12708 if (dwarf_read_debug)
73869dc2
DE
12709 {
12710 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12711 virtual_dwo_name.c_str ());
73869dc2 12712 }
51ac9db5 12713 dwo_file = new struct dwo_file;
efba19b0
TT
12714 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12715 virtual_dwo_name);
73869dc2
DE
12716 dwo_file->comp_dir = comp_dir;
12717 dwo_file->sections.abbrev =
ed2dc618 12718 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12719 sections.abbrev_offset, sections.abbrev_size);
12720 dwo_file->sections.line =
ed2dc618 12721 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12722 sections.line_offset, sections.line_size);
12723 dwo_file->sections.loc =
ed2dc618 12724 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12725 sections.loc_offset, sections.loc_size);
12726 dwo_file->sections.macinfo =
ed2dc618 12727 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12728 sections.macinfo_offset, sections.macinfo_size);
12729 dwo_file->sections.macro =
ed2dc618 12730 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12731 sections.macro_offset, sections.macro_size);
12732 dwo_file->sections.str_offsets =
ed2dc618
SM
12733 create_dwp_v2_section (dwarf2_per_objfile,
12734 &dwp_file->sections.str_offsets,
73869dc2
DE
12735 sections.str_offsets_offset,
12736 sections.str_offsets_size);
12737 /* The "str" section is global to the entire DWP file. */
12738 dwo_file->sections.str = dwp_file->sections.str;
12739 /* The info or types section is assigned below to dwo_unit,
12740 there's no need to record it in dwo_file.
12741 Also, we can't simply record type sections in dwo_file because
12742 we record a pointer into the vector in dwo_unit. As we collect more
12743 types we'll grow the vector and eventually have to reallocate space
12744 for it, invalidating all copies of pointers into the previous
12745 contents. */
12746 *dwo_file_slot = dwo_file;
12747 }
12748 else
12749 {
b4f54984 12750 if (dwarf_read_debug)
73869dc2
DE
12751 {
12752 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12753 virtual_dwo_name.c_str ());
73869dc2 12754 }
9a3c8263 12755 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12756 }
73869dc2
DE
12757
12758 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12759 dwo_unit->dwo_file = dwo_file;
12760 dwo_unit->signature = signature;
8d749320
SM
12761 dwo_unit->section =
12762 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12763 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12764 is_debug_types
73869dc2
DE
12765 ? &dwp_file->sections.types
12766 : &dwp_file->sections.info,
12767 sections.info_or_types_offset,
12768 sections.info_or_types_size);
12769 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12770
12771 return dwo_unit;
12772}
12773
57d63ce2
DE
12774/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12775 Returns NULL if the signature isn't found. */
80626a55
DE
12776
12777static struct dwo_unit *
ed2dc618
SM
12778lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12779 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12780 ULONGEST signature, int is_debug_types)
80626a55 12781{
57d63ce2
DE
12782 const struct dwp_hash_table *dwp_htab =
12783 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12784 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12785 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12786 uint32_t hash = signature & mask;
12787 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12788 unsigned int i;
12789 void **slot;
870f88f7 12790 struct dwo_unit find_dwo_cu;
80626a55
DE
12791
12792 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12793 find_dwo_cu.signature = signature;
19ac8c2e
DE
12794 slot = htab_find_slot (is_debug_types
12795 ? dwp_file->loaded_tus
12796 : dwp_file->loaded_cus,
12797 &find_dwo_cu, INSERT);
80626a55
DE
12798
12799 if (*slot != NULL)
9a3c8263 12800 return (struct dwo_unit *) *slot;
80626a55
DE
12801
12802 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12803 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12804 {
12805 ULONGEST signature_in_table;
12806
12807 signature_in_table =
57d63ce2 12808 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12809 if (signature_in_table == signature)
12810 {
57d63ce2
DE
12811 uint32_t unit_index =
12812 read_4_bytes (dbfd,
12813 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12814
73869dc2
DE
12815 if (dwp_file->version == 1)
12816 {
ed2dc618
SM
12817 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12818 dwp_file, unit_index,
73869dc2
DE
12819 comp_dir, signature,
12820 is_debug_types);
12821 }
12822 else
12823 {
ed2dc618
SM
12824 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12825 dwp_file, unit_index,
73869dc2
DE
12826 comp_dir, signature,
12827 is_debug_types);
12828 }
9a3c8263 12829 return (struct dwo_unit *) *slot;
80626a55
DE
12830 }
12831 if (signature_in_table == 0)
12832 return NULL;
12833 hash = (hash + hash2) & mask;
12834 }
12835
12836 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12837 " [in module %s]"),
12838 dwp_file->name);
12839}
12840
ab5088bf 12841/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12842 Open the file specified by FILE_NAME and hand it off to BFD for
12843 preliminary analysis. Return a newly initialized bfd *, which
12844 includes a canonicalized copy of FILE_NAME.
80626a55 12845 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12846 SEARCH_CWD is true if the current directory is to be searched.
12847 It will be searched before debug-file-directory.
13aaf454
DE
12848 If successful, the file is added to the bfd include table of the
12849 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12850 If unable to find/open the file, return NULL.
3019eac3
DE
12851 NOTE: This function is derived from symfile_bfd_open. */
12852
192b62ce 12853static gdb_bfd_ref_ptr
ed2dc618
SM
12854try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12855 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12856{
24b9144d 12857 int desc;
9c02c129
DE
12858 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12859 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12860 to debug_file_directory. */
e0cc99a6 12861 const char *search_path;
9c02c129
DE
12862 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12863
e0cc99a6 12864 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12865 if (search_cwd)
12866 {
12867 if (*debug_file_directory != '\0')
e0cc99a6
TT
12868 {
12869 search_path_holder.reset (concat (".", dirname_separator_string,
12870 debug_file_directory,
12871 (char *) NULL));
12872 search_path = search_path_holder.get ();
12873 }
6ac97d4c 12874 else
e0cc99a6 12875 search_path = ".";
6ac97d4c 12876 }
9c02c129 12877 else
e0cc99a6 12878 search_path = debug_file_directory;
3019eac3 12879
24b9144d 12880 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12881 if (is_dwp)
12882 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12883
12884 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12885 desc = openp (search_path, flags, file_name,
3019eac3
DE
12886 O_RDONLY | O_BINARY, &absolute_name);
12887 if (desc < 0)
12888 return NULL;
12889
e0cc99a6
TT
12890 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12891 gnutarget, desc));
9c02c129
DE
12892 if (sym_bfd == NULL)
12893 return NULL;
192b62ce 12894 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12895
192b62ce
TT
12896 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12897 return NULL;
3019eac3 12898
13aaf454
DE
12899 /* Success. Record the bfd as having been included by the objfile's bfd.
12900 This is important because things like demangled_names_hash lives in the
12901 objfile's per_bfd space and may have references to things like symbol
12902 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12903 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12904
3019eac3
DE
12905 return sym_bfd;
12906}
12907
ab5088bf 12908/* Try to open DWO file FILE_NAME.
3019eac3
DE
12909 COMP_DIR is the DW_AT_comp_dir attribute.
12910 The result is the bfd handle of the file.
12911 If there is a problem finding or opening the file, return NULL.
12912 Upon success, the canonicalized path of the file is stored in the bfd,
12913 same as symfile_bfd_open. */
12914
192b62ce 12915static gdb_bfd_ref_ptr
ed2dc618
SM
12916open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12917 const char *file_name, const char *comp_dir)
3019eac3 12918{
80626a55 12919 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12920 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12921 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12922
12923 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12924
12925 if (comp_dir != NULL)
12926 {
b36cec19
PA
12927 char *path_to_try = concat (comp_dir, SLASH_STRING,
12928 file_name, (char *) NULL);
3019eac3
DE
12929
12930 /* NOTE: If comp_dir is a relative path, this will also try the
12931 search path, which seems useful. */
ed2dc618
SM
12932 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12933 path_to_try,
12934 0 /*is_dwp*/,
192b62ce 12935 1 /*search_cwd*/));
3019eac3
DE
12936 xfree (path_to_try);
12937 if (abfd != NULL)
12938 return abfd;
12939 }
12940
12941 /* That didn't work, try debug-file-directory, which, despite its name,
12942 is a list of paths. */
12943
12944 if (*debug_file_directory == '\0')
12945 return NULL;
12946
ed2dc618
SM
12947 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12948 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12949}
12950
80626a55
DE
12951/* This function is mapped across the sections and remembers the offset and
12952 size of each of the DWO debugging sections we are interested in. */
12953
12954static void
12955dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12956{
9a3c8263 12957 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12958 const struct dwop_section_names *names = &dwop_section_names;
12959
12960 if (section_is_p (sectp->name, &names->abbrev_dwo))
12961 {
049412e3 12962 dwo_sections->abbrev.s.section = sectp;
fd361982 12963 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12964 }
12965 else if (section_is_p (sectp->name, &names->info_dwo))
12966 {
049412e3 12967 dwo_sections->info.s.section = sectp;
fd361982 12968 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12969 }
12970 else if (section_is_p (sectp->name, &names->line_dwo))
12971 {
049412e3 12972 dwo_sections->line.s.section = sectp;
fd361982 12973 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12974 }
12975 else if (section_is_p (sectp->name, &names->loc_dwo))
12976 {
049412e3 12977 dwo_sections->loc.s.section = sectp;
fd361982 12978 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12979 }
12980 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12981 {
049412e3 12982 dwo_sections->macinfo.s.section = sectp;
fd361982 12983 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12984 }
12985 else if (section_is_p (sectp->name, &names->macro_dwo))
12986 {
049412e3 12987 dwo_sections->macro.s.section = sectp;
fd361982 12988 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12989 }
12990 else if (section_is_p (sectp->name, &names->str_dwo))
12991 {
049412e3 12992 dwo_sections->str.s.section = sectp;
fd361982 12993 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12994 }
12995 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12996 {
049412e3 12997 dwo_sections->str_offsets.s.section = sectp;
fd361982 12998 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12999 }
13000 else if (section_is_p (sectp->name, &names->types_dwo))
13001 {
13002 struct dwarf2_section_info type_section;
13003
13004 memset (&type_section, 0, sizeof (type_section));
049412e3 13005 type_section.s.section = sectp;
fd361982 13006 type_section.size = bfd_section_size (sectp);
fd5866f6 13007 dwo_sections->types.push_back (type_section);
80626a55
DE
13008 }
13009}
13010
ab5088bf 13011/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13012 by PER_CU. This is for the non-DWP case.
80626a55 13013 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13014
13015static struct dwo_file *
0ac5b59e
DE
13016open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13017 const char *dwo_name, const char *comp_dir)
3019eac3 13018{
ed2dc618 13019 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13020
fb1eb2f9 13021 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13022 if (dbfd == NULL)
13023 {
b4f54984 13024 if (dwarf_read_debug)
80626a55
DE
13025 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13026 return NULL;
13027 }
263db9a1 13028
51ac9db5 13029 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13030 dwo_file->dwo_name = dwo_name;
13031 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13032 dwo_file->dbfd = std::move (dbfd);
3019eac3 13033
fb1eb2f9 13034 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13035 &dwo_file->sections);
3019eac3 13036
ed2dc618
SM
13037 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13038 dwo_file->cus);
3019eac3 13039
263db9a1 13040 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13041 dwo_file->sections.types, dwo_file->tus);
3019eac3 13042
b4f54984 13043 if (dwarf_read_debug)
80626a55
DE
13044 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13045
263db9a1 13046 return dwo_file.release ();
3019eac3
DE
13047}
13048
80626a55 13049/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13050 size of each of the DWP debugging sections common to version 1 and 2 that
13051 we are interested in. */
3019eac3 13052
80626a55 13053static void
73869dc2
DE
13054dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13055 void *dwp_file_ptr)
3019eac3 13056{
9a3c8263 13057 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13058 const struct dwop_section_names *names = &dwop_section_names;
13059 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13060
80626a55 13061 /* Record the ELF section number for later lookup: this is what the
73869dc2 13062 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13063 gdb_assert (elf_section_nr < dwp_file->num_sections);
13064 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13065
80626a55
DE
13066 /* Look for specific sections that we need. */
13067 if (section_is_p (sectp->name, &names->str_dwo))
13068 {
049412e3 13069 dwp_file->sections.str.s.section = sectp;
fd361982 13070 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13071 }
13072 else if (section_is_p (sectp->name, &names->cu_index))
13073 {
049412e3 13074 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13075 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13076 }
13077 else if (section_is_p (sectp->name, &names->tu_index))
13078 {
049412e3 13079 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13080 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13081 }
13082}
3019eac3 13083
73869dc2
DE
13084/* This function is mapped across the sections and remembers the offset and
13085 size of each of the DWP version 2 debugging sections that we are interested
13086 in. This is split into a separate function because we don't know if we
13087 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13088
13089static void
13090dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13091{
9a3c8263 13092 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13093 const struct dwop_section_names *names = &dwop_section_names;
13094 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13095
13096 /* Record the ELF section number for later lookup: this is what the
13097 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13098 gdb_assert (elf_section_nr < dwp_file->num_sections);
13099 dwp_file->elf_sections[elf_section_nr] = sectp;
13100
13101 /* Look for specific sections that we need. */
13102 if (section_is_p (sectp->name, &names->abbrev_dwo))
13103 {
049412e3 13104 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13105 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13106 }
13107 else if (section_is_p (sectp->name, &names->info_dwo))
13108 {
049412e3 13109 dwp_file->sections.info.s.section = sectp;
fd361982 13110 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13111 }
13112 else if (section_is_p (sectp->name, &names->line_dwo))
13113 {
049412e3 13114 dwp_file->sections.line.s.section = sectp;
fd361982 13115 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13116 }
13117 else if (section_is_p (sectp->name, &names->loc_dwo))
13118 {
049412e3 13119 dwp_file->sections.loc.s.section = sectp;
fd361982 13120 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13121 }
13122 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13123 {
049412e3 13124 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13125 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13126 }
13127 else if (section_is_p (sectp->name, &names->macro_dwo))
13128 {
049412e3 13129 dwp_file->sections.macro.s.section = sectp;
fd361982 13130 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13131 }
13132 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13133 {
049412e3 13134 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13135 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13136 }
13137 else if (section_is_p (sectp->name, &names->types_dwo))
13138 {
049412e3 13139 dwp_file->sections.types.s.section = sectp;
fd361982 13140 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13141 }
13142}
13143
80626a55 13144/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13145
80626a55
DE
13146static hashval_t
13147hash_dwp_loaded_cutus (const void *item)
13148{
9a3c8263 13149 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13150
80626a55
DE
13151 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13152 return dwo_unit->signature;
3019eac3
DE
13153}
13154
80626a55 13155/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13156
80626a55
DE
13157static int
13158eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13159{
9a3c8263
SM
13160 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13161 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13162
80626a55
DE
13163 return dua->signature == dub->signature;
13164}
3019eac3 13165
80626a55 13166/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13167
80626a55
DE
13168static htab_t
13169allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13170{
13171 return htab_create_alloc_ex (3,
13172 hash_dwp_loaded_cutus,
13173 eq_dwp_loaded_cutus,
13174 NULL,
13175 &objfile->objfile_obstack,
13176 hashtab_obstack_allocate,
13177 dummy_obstack_deallocate);
13178}
3019eac3 13179
ab5088bf
DE
13180/* Try to open DWP file FILE_NAME.
13181 The result is the bfd handle of the file.
13182 If there is a problem finding or opening the file, return NULL.
13183 Upon success, the canonicalized path of the file is stored in the bfd,
13184 same as symfile_bfd_open. */
13185
192b62ce 13186static gdb_bfd_ref_ptr
ed2dc618
SM
13187open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13188 const char *file_name)
ab5088bf 13189{
ed2dc618
SM
13190 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13191 1 /*is_dwp*/,
192b62ce 13192 1 /*search_cwd*/));
6ac97d4c
DE
13193 if (abfd != NULL)
13194 return abfd;
13195
13196 /* Work around upstream bug 15652.
13197 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13198 [Whether that's a "bug" is debatable, but it is getting in our way.]
13199 We have no real idea where the dwp file is, because gdb's realpath-ing
13200 of the executable's path may have discarded the needed info.
13201 [IWBN if the dwp file name was recorded in the executable, akin to
13202 .gnu_debuglink, but that doesn't exist yet.]
13203 Strip the directory from FILE_NAME and search again. */
13204 if (*debug_file_directory != '\0')
13205 {
13206 /* Don't implicitly search the current directory here.
13207 If the user wants to search "." to handle this case,
13208 it must be added to debug-file-directory. */
ed2dc618
SM
13209 return try_open_dwop_file (dwarf2_per_objfile,
13210 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13211 0 /*search_cwd*/);
13212 }
13213
13214 return NULL;
ab5088bf
DE
13215}
13216
80626a55
DE
13217/* Initialize the use of the DWP file for the current objfile.
13218 By convention the name of the DWP file is ${objfile}.dwp.
13219 The result is NULL if it can't be found. */
a766d390 13220
400174b1 13221static std::unique_ptr<struct dwp_file>
ed2dc618 13222open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13223{
13224 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13225
82bf32bc
JK
13226 /* Try to find first .dwp for the binary file before any symbolic links
13227 resolving. */
6c447423
DE
13228
13229 /* If the objfile is a debug file, find the name of the real binary
13230 file and get the name of dwp file from there. */
d721ba37 13231 std::string dwp_name;
6c447423
DE
13232 if (objfile->separate_debug_objfile_backlink != NULL)
13233 {
13234 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13235 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13236
d721ba37 13237 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13238 }
13239 else
d721ba37
PA
13240 dwp_name = objfile->original_name;
13241
13242 dwp_name += ".dwp";
80626a55 13243
ed2dc618 13244 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13245 if (dbfd == NULL
13246 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13247 {
13248 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13249 dwp_name = objfile_name (objfile);
13250 dwp_name += ".dwp";
ed2dc618 13251 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13252 }
13253
80626a55
DE
13254 if (dbfd == NULL)
13255 {
b4f54984 13256 if (dwarf_read_debug)
d721ba37 13257 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13258 return std::unique_ptr<dwp_file> ();
3019eac3 13259 }
400174b1
TT
13260
13261 const char *name = bfd_get_filename (dbfd.get ());
13262 std::unique_ptr<struct dwp_file> dwp_file
13263 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13264
0a0f4c01 13265 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13266 dwp_file->elf_sections =
13267 OBSTACK_CALLOC (&objfile->objfile_obstack,
13268 dwp_file->num_sections, asection *);
13269
400174b1
TT
13270 bfd_map_over_sections (dwp_file->dbfd.get (),
13271 dwarf2_locate_common_dwp_sections,
13272 dwp_file.get ());
80626a55 13273
400174b1
TT
13274 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13275 0);
80626a55 13276
400174b1
TT
13277 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13278 1);
80626a55 13279
73869dc2 13280 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13281 if (dwp_file->cus && dwp_file->tus
13282 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13283 {
13284 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13285 pretty bizarre. We use pulongest here because that's the established
4d65956b 13286 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13287 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13288 " TU version %s [in DWP file %s]"),
13289 pulongest (dwp_file->cus->version),
d721ba37 13290 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13291 }
08302ed2
DE
13292
13293 if (dwp_file->cus)
13294 dwp_file->version = dwp_file->cus->version;
13295 else if (dwp_file->tus)
13296 dwp_file->version = dwp_file->tus->version;
13297 else
13298 dwp_file->version = 2;
73869dc2
DE
13299
13300 if (dwp_file->version == 2)
400174b1
TT
13301 bfd_map_over_sections (dwp_file->dbfd.get (),
13302 dwarf2_locate_v2_dwp_sections,
13303 dwp_file.get ());
73869dc2 13304
19ac8c2e
DE
13305 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13306 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13307
b4f54984 13308 if (dwarf_read_debug)
80626a55
DE
13309 {
13310 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13311 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13312 " %s CUs, %s TUs\n",
13313 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13314 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13315 }
13316
13317 return dwp_file;
3019eac3 13318}
c906108c 13319
ab5088bf
DE
13320/* Wrapper around open_and_init_dwp_file, only open it once. */
13321
13322static struct dwp_file *
ed2dc618 13323get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13324{
13325 if (! dwarf2_per_objfile->dwp_checked)
13326 {
ed2dc618
SM
13327 dwarf2_per_objfile->dwp_file
13328 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13329 dwarf2_per_objfile->dwp_checked = 1;
13330 }
400174b1 13331 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13332}
13333
80626a55
DE
13334/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13335 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13336 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13337 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13338 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13339
13340 This is called, for example, when wanting to read a variable with a
13341 complex location. Therefore we don't want to do file i/o for every call.
13342 Therefore we don't want to look for a DWO file on every call.
13343 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13344 then we check if we've already seen DWO_NAME, and only THEN do we check
13345 for a DWO file.
13346
1c658ad5 13347 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13348 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13349
3019eac3 13350static struct dwo_unit *
80626a55
DE
13351lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13352 const char *dwo_name, const char *comp_dir,
13353 ULONGEST signature, int is_debug_types)
3019eac3 13354{
ed2dc618 13355 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13356 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13357 const char *kind = is_debug_types ? "TU" : "CU";
13358 void **dwo_file_slot;
3019eac3 13359 struct dwo_file *dwo_file;
80626a55 13360 struct dwp_file *dwp_file;
cb1df416 13361
6a506a2d
DE
13362 /* First see if there's a DWP file.
13363 If we have a DWP file but didn't find the DWO inside it, don't
13364 look for the original DWO file. It makes gdb behave differently
13365 depending on whether one is debugging in the build tree. */
cf2c3c16 13366
ed2dc618 13367 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13368 if (dwp_file != NULL)
cf2c3c16 13369 {
80626a55
DE
13370 const struct dwp_hash_table *dwp_htab =
13371 is_debug_types ? dwp_file->tus : dwp_file->cus;
13372
13373 if (dwp_htab != NULL)
13374 {
13375 struct dwo_unit *dwo_cutu =
ed2dc618 13376 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13377 signature, is_debug_types);
80626a55
DE
13378
13379 if (dwo_cutu != NULL)
13380 {
b4f54984 13381 if (dwarf_read_debug)
80626a55
DE
13382 {
13383 fprintf_unfiltered (gdb_stdlog,
13384 "Virtual DWO %s %s found: @%s\n",
13385 kind, hex_string (signature),
13386 host_address_to_string (dwo_cutu));
13387 }
13388 return dwo_cutu;
13389 }
13390 }
13391 }
6a506a2d 13392 else
80626a55 13393 {
6a506a2d 13394 /* No DWP file, look for the DWO file. */
80626a55 13395
ed2dc618
SM
13396 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13397 dwo_name, comp_dir);
6a506a2d 13398 if (*dwo_file_slot == NULL)
80626a55 13399 {
6a506a2d
DE
13400 /* Read in the file and build a table of the CUs/TUs it contains. */
13401 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13402 }
6a506a2d 13403 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13404 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13405
6a506a2d 13406 if (dwo_file != NULL)
19c3d4c9 13407 {
6a506a2d
DE
13408 struct dwo_unit *dwo_cutu = NULL;
13409
13410 if (is_debug_types && dwo_file->tus)
13411 {
13412 struct dwo_unit find_dwo_cutu;
13413
13414 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13415 find_dwo_cutu.signature = signature;
9a3c8263
SM
13416 dwo_cutu
13417 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13418 }
33c5cd75 13419 else if (!is_debug_types && dwo_file->cus)
80626a55 13420 {
33c5cd75
DB
13421 struct dwo_unit find_dwo_cutu;
13422
13423 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13424 find_dwo_cutu.signature = signature;
13425 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13426 &find_dwo_cutu);
6a506a2d
DE
13427 }
13428
13429 if (dwo_cutu != NULL)
13430 {
b4f54984 13431 if (dwarf_read_debug)
6a506a2d
DE
13432 {
13433 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13434 kind, dwo_name, hex_string (signature),
13435 host_address_to_string (dwo_cutu));
13436 }
13437 return dwo_cutu;
80626a55
DE
13438 }
13439 }
2e276125 13440 }
9cdd5dbd 13441
80626a55
DE
13442 /* We didn't find it. This could mean a dwo_id mismatch, or
13443 someone deleted the DWO/DWP file, or the search path isn't set up
13444 correctly to find the file. */
13445
b4f54984 13446 if (dwarf_read_debug)
80626a55
DE
13447 {
13448 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13449 kind, dwo_name, hex_string (signature));
13450 }
3019eac3 13451
6656a72d
DE
13452 /* This is a warning and not a complaint because it can be caused by
13453 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13454 {
13455 /* Print the name of the DWP file if we looked there, helps the user
13456 better diagnose the problem. */
791afaa2 13457 std::string dwp_text;
43942612
DE
13458
13459 if (dwp_file != NULL)
791afaa2
TT
13460 dwp_text = string_printf (" [in DWP file %s]",
13461 lbasename (dwp_file->name));
43942612 13462
9d8780f0 13463 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13464 " [in module %s]"),
13465 kind, dwo_name, hex_string (signature),
791afaa2 13466 dwp_text.c_str (),
43942612 13467 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13468 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13469 }
3019eac3 13470 return NULL;
5fb290d7
DJ
13471}
13472
80626a55
DE
13473/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13474 See lookup_dwo_cutu_unit for details. */
13475
13476static struct dwo_unit *
13477lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13478 const char *dwo_name, const char *comp_dir,
13479 ULONGEST signature)
13480{
13481 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13482}
13483
13484/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13485 See lookup_dwo_cutu_unit for details. */
13486
13487static struct dwo_unit *
13488lookup_dwo_type_unit (struct signatured_type *this_tu,
13489 const char *dwo_name, const char *comp_dir)
13490{
13491 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13492}
13493
89e63ee4
DE
13494/* Traversal function for queue_and_load_all_dwo_tus. */
13495
13496static int
13497queue_and_load_dwo_tu (void **slot, void *info)
13498{
13499 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13500 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13501 ULONGEST signature = dwo_unit->signature;
13502 struct signatured_type *sig_type =
13503 lookup_dwo_signatured_type (per_cu->cu, signature);
13504
13505 if (sig_type != NULL)
13506 {
13507 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13508
13509 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13510 a real dependency of PER_CU on SIG_TYPE. That is detected later
13511 while processing PER_CU. */
13512 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13513 load_full_type_unit (sig_cu);
13514 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13515 }
13516
13517 return 1;
13518}
13519
13520/* Queue all TUs contained in the DWO of PER_CU to be read in.
13521 The DWO may have the only definition of the type, though it may not be
13522 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13523 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13524
13525static void
13526queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13527{
13528 struct dwo_unit *dwo_unit;
13529 struct dwo_file *dwo_file;
13530
13531 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13532 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13533 gdb_assert (per_cu->cu != NULL);
13534
13535 dwo_unit = per_cu->cu->dwo_unit;
13536 gdb_assert (dwo_unit != NULL);
13537
13538 dwo_file = dwo_unit->dwo_file;
13539 if (dwo_file->tus != NULL)
13540 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13541}
13542
3019eac3 13543/* Read in various DIEs. */
348e048f 13544
d389af10 13545/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13546 Inherit only the children of the DW_AT_abstract_origin DIE not being
13547 already referenced by DW_AT_abstract_origin from the children of the
13548 current DIE. */
d389af10
JK
13549
13550static void
13551inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13552{
13553 struct die_info *child_die;
791afaa2 13554 sect_offset *offsetp;
d389af10
JK
13555 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13556 struct die_info *origin_die;
13557 /* Iterator of the ORIGIN_DIE children. */
13558 struct die_info *origin_child_die;
d389af10 13559 struct attribute *attr;
cd02d79d
PA
13560 struct dwarf2_cu *origin_cu;
13561 struct pending **origin_previous_list_in_scope;
d389af10
JK
13562
13563 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13564 if (!attr)
13565 return;
13566
cd02d79d
PA
13567 /* Note that following die references may follow to a die in a
13568 different cu. */
13569
13570 origin_cu = cu;
13571 origin_die = follow_die_ref (die, attr, &origin_cu);
13572
13573 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13574 symbols in. */
13575 origin_previous_list_in_scope = origin_cu->list_in_scope;
13576 origin_cu->list_in_scope = cu->list_in_scope;
13577
edb3359d
DJ
13578 if (die->tag != origin_die->tag
13579 && !(die->tag == DW_TAG_inlined_subroutine
13580 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13581 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13582 sect_offset_str (die->sect_off),
13583 sect_offset_str (origin_die->sect_off));
d389af10 13584
791afaa2 13585 std::vector<sect_offset> offsets;
d389af10 13586
3ea89b92
PMR
13587 for (child_die = die->child;
13588 child_die && child_die->tag;
13589 child_die = sibling_die (child_die))
13590 {
13591 struct die_info *child_origin_die;
13592 struct dwarf2_cu *child_origin_cu;
13593
13594 /* We are trying to process concrete instance entries:
216f72a1 13595 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13596 it's not relevant to our analysis here. i.e. detecting DIEs that are
13597 present in the abstract instance but not referenced in the concrete
13598 one. */
216f72a1
JK
13599 if (child_die->tag == DW_TAG_call_site
13600 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13601 continue;
13602
c38f313d
DJ
13603 /* For each CHILD_DIE, find the corresponding child of
13604 ORIGIN_DIE. If there is more than one layer of
13605 DW_AT_abstract_origin, follow them all; there shouldn't be,
13606 but GCC versions at least through 4.4 generate this (GCC PR
13607 40573). */
3ea89b92
PMR
13608 child_origin_die = child_die;
13609 child_origin_cu = cu;
c38f313d
DJ
13610 while (1)
13611 {
cd02d79d
PA
13612 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13613 child_origin_cu);
c38f313d
DJ
13614 if (attr == NULL)
13615 break;
cd02d79d
PA
13616 child_origin_die = follow_die_ref (child_origin_die, attr,
13617 &child_origin_cu);
c38f313d
DJ
13618 }
13619
d389af10
JK
13620 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13621 counterpart may exist. */
c38f313d 13622 if (child_origin_die != child_die)
d389af10 13623 {
edb3359d
DJ
13624 if (child_die->tag != child_origin_die->tag
13625 && !(child_die->tag == DW_TAG_inlined_subroutine
13626 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13627 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13628 "different tags"),
9d8780f0
SM
13629 sect_offset_str (child_die->sect_off),
13630 sect_offset_str (child_origin_die->sect_off));
c38f313d 13631 if (child_origin_die->parent != origin_die)
b98664d3 13632 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13633 "different parents"),
9d8780f0
SM
13634 sect_offset_str (child_die->sect_off),
13635 sect_offset_str (child_origin_die->sect_off));
c38f313d 13636 else
791afaa2 13637 offsets.push_back (child_origin_die->sect_off);
d389af10 13638 }
d389af10 13639 }
791afaa2
TT
13640 std::sort (offsets.begin (), offsets.end ());
13641 sect_offset *offsets_end = offsets.data () + offsets.size ();
13642 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13643 if (offsetp[-1] == *offsetp)
b98664d3 13644 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13645 "to DIE %s as their abstract origin"),
13646 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13647
791afaa2 13648 offsetp = offsets.data ();
d389af10
JK
13649 origin_child_die = origin_die->child;
13650 while (origin_child_die && origin_child_die->tag)
13651 {
13652 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13653 while (offsetp < offsets_end
9c541725 13654 && *offsetp < origin_child_die->sect_off)
d389af10 13655 offsetp++;
b64f50a1 13656 if (offsetp >= offsets_end
9c541725 13657 || *offsetp > origin_child_die->sect_off)
d389af10 13658 {
adde2bff
DE
13659 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13660 Check whether we're already processing ORIGIN_CHILD_DIE.
13661 This can happen with mutually referenced abstract_origins.
13662 PR 16581. */
13663 if (!origin_child_die->in_process)
13664 process_die (origin_child_die, origin_cu);
d389af10
JK
13665 }
13666 origin_child_die = sibling_die (origin_child_die);
13667 }
cd02d79d 13668 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13669}
13670
c906108c 13671static void
e7c27a73 13672read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13673{
518817b3 13674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13675 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13676 struct context_stack *newobj;
c906108c
SS
13677 CORE_ADDR lowpc;
13678 CORE_ADDR highpc;
13679 struct die_info *child_die;
edb3359d 13680 struct attribute *attr, *call_line, *call_file;
15d034d0 13681 const char *name;
e142c38c 13682 CORE_ADDR baseaddr;
801e3a5b 13683 struct block *block;
edb3359d 13684 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13685 std::vector<struct symbol *> template_args;
34eaf542 13686 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13687
13688 if (inlined_func)
13689 {
13690 /* If we do not have call site information, we can't show the
13691 caller of this inlined function. That's too confusing, so
13692 only use the scope for local variables. */
13693 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13694 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13695 if (call_line == NULL || call_file == NULL)
13696 {
13697 read_lexical_block_scope (die, cu);
13698 return;
13699 }
13700 }
c906108c 13701
e142c38c
DJ
13702 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13703
94af9270 13704 name = dwarf2_name (die, cu);
c906108c 13705
e8d05480
JB
13706 /* Ignore functions with missing or empty names. These are actually
13707 illegal according to the DWARF standard. */
13708 if (name == NULL)
13709 {
b98664d3 13710 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13711 sect_offset_str (die->sect_off));
e8d05480
JB
13712 return;
13713 }
13714
13715 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13716 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13717 <= PC_BOUNDS_INVALID)
e8d05480 13718 {
ae4d0c03
PM
13719 attr = dwarf2_attr (die, DW_AT_external, cu);
13720 if (!attr || !DW_UNSND (attr))
b98664d3 13721 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13722 "for subprogram DIE at %s"),
13723 sect_offset_str (die->sect_off));
e8d05480
JB
13724 return;
13725 }
c906108c 13726
3e29f34a
MR
13727 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13728 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13729
34eaf542
TT
13730 /* If we have any template arguments, then we must allocate a
13731 different sort of symbol. */
13732 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13733 {
13734 if (child_die->tag == DW_TAG_template_type_param
13735 || child_die->tag == DW_TAG_template_value_param)
13736 {
e623cf5d 13737 templ_func = allocate_template_symbol (objfile);
cf724bc9 13738 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13739 break;
13740 }
13741 }
13742
c24bdb02 13743 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13744 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13745 (struct symbol *) templ_func);
4c2df51b 13746
81873cc8
TV
13747 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13748 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13749 cu->language);
13750
4cecd739
DJ
13751 /* If there is a location expression for DW_AT_frame_base, record
13752 it. */
e142c38c 13753 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13754 if (attr)
fe978cb0 13755 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13756
63e43d3a
PMR
13757 /* If there is a location for the static link, record it. */
13758 newobj->static_link = NULL;
13759 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13760 if (attr)
13761 {
224c3ddb
SM
13762 newobj->static_link
13763 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13764 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13765 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13766 }
13767
c24bdb02 13768 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13769
639d11d3 13770 if (die->child != NULL)
c906108c 13771 {
639d11d3 13772 child_die = die->child;
c906108c
SS
13773 while (child_die && child_die->tag)
13774 {
34eaf542
TT
13775 if (child_die->tag == DW_TAG_template_type_param
13776 || child_die->tag == DW_TAG_template_value_param)
13777 {
13778 struct symbol *arg = new_symbol (child_die, NULL, cu);
13779
f1078f66 13780 if (arg != NULL)
2f4732b0 13781 template_args.push_back (arg);
34eaf542
TT
13782 }
13783 else
13784 process_die (child_die, cu);
c906108c
SS
13785 child_die = sibling_die (child_die);
13786 }
13787 }
13788
d389af10
JK
13789 inherit_abstract_dies (die, cu);
13790
4a811a97
UW
13791 /* If we have a DW_AT_specification, we might need to import using
13792 directives from the context of the specification DIE. See the
13793 comment in determine_prefix. */
13794 if (cu->language == language_cplus
13795 && dwarf2_attr (die, DW_AT_specification, cu))
13796 {
13797 struct dwarf2_cu *spec_cu = cu;
13798 struct die_info *spec_die = die_specification (die, &spec_cu);
13799
13800 while (spec_die)
13801 {
13802 child_die = spec_die->child;
13803 while (child_die && child_die->tag)
13804 {
13805 if (child_die->tag == DW_TAG_imported_module)
13806 process_die (child_die, spec_cu);
13807 child_die = sibling_die (child_die);
13808 }
13809
13810 /* In some cases, GCC generates specification DIEs that
13811 themselves contain DW_AT_specification attributes. */
13812 spec_die = die_specification (spec_die, &spec_cu);
13813 }
13814 }
13815
c24bdb02 13816 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13817 /* Make a block for the local symbols within. */
c24bdb02 13818 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13819 cstk.static_link, lowpc, highpc);
801e3a5b 13820
df8a16a1 13821 /* For C++, set the block's scope. */
45280282
IB
13822 if ((cu->language == language_cplus
13823 || cu->language == language_fortran
c44af4eb
TT
13824 || cu->language == language_d
13825 || cu->language == language_rust)
4d4ec4e5 13826 && cu->processing_has_namespace_info)
195a3f6c
TT
13827 block_set_scope (block, determine_prefix (die, cu),
13828 &objfile->objfile_obstack);
df8a16a1 13829
801e3a5b
JB
13830 /* If we have address ranges, record them. */
13831 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13832
a60f3166 13833 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13834
34eaf542 13835 /* Attach template arguments to function. */
2f4732b0 13836 if (!template_args.empty ())
34eaf542
TT
13837 {
13838 gdb_assert (templ_func != NULL);
13839
2f4732b0 13840 templ_func->n_template_arguments = template_args.size ();
34eaf542 13841 templ_func->template_arguments
8d749320
SM
13842 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13843 templ_func->n_template_arguments);
34eaf542 13844 memcpy (templ_func->template_arguments,
2f4732b0 13845 template_args.data (),
34eaf542 13846 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13847
13848 /* Make sure that the symtab is set on the new symbols. Even
13849 though they don't appear in this symtab directly, other parts
13850 of gdb assume that symbols do, and this is reasonably
13851 true. */
8634679f 13852 for (symbol *sym : template_args)
3e1d3d8c 13853 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13854 }
13855
208d8187
JB
13856 /* In C++, we can have functions nested inside functions (e.g., when
13857 a function declares a class that has methods). This means that
13858 when we finish processing a function scope, we may need to go
13859 back to building a containing block's symbol lists. */
c24bdb02
KS
13860 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13861 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13862
921e78cf
JB
13863 /* If we've finished processing a top-level function, subsequent
13864 symbols go in the file symbol list. */
c24bdb02
KS
13865 if (cu->get_builder ()->outermost_context_p ())
13866 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13867}
13868
13869/* Process all the DIES contained within a lexical block scope. Start
13870 a new scope, process the dies, and then close the scope. */
13871
13872static void
e7c27a73 13873read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13874{
518817b3 13875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13876 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13877 CORE_ADDR lowpc, highpc;
13878 struct die_info *child_die;
e142c38c
DJ
13879 CORE_ADDR baseaddr;
13880
13881 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13882
13883 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13884 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13885 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13886 be nasty. Might be easier to properly extend generic blocks to
af34e669 13887 describe ranges. */
e385593e
JK
13888 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13889 {
13890 case PC_BOUNDS_NOT_PRESENT:
13891 /* DW_TAG_lexical_block has no attributes, process its children as if
13892 there was no wrapping by that DW_TAG_lexical_block.
13893 GCC does no longer produces such DWARF since GCC r224161. */
13894 for (child_die = die->child;
13895 child_die != NULL && child_die->tag;
13896 child_die = sibling_die (child_die))
13897 process_die (child_die, cu);
13898 return;
13899 case PC_BOUNDS_INVALID:
13900 return;
13901 }
3e29f34a
MR
13902 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13903 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13904
c24bdb02 13905 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13906 if (die->child != NULL)
c906108c 13907 {
639d11d3 13908 child_die = die->child;
c906108c
SS
13909 while (child_die && child_die->tag)
13910 {
e7c27a73 13911 process_die (child_die, cu);
c906108c
SS
13912 child_die = sibling_die (child_die);
13913 }
13914 }
3ea89b92 13915 inherit_abstract_dies (die, cu);
c24bdb02 13916 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13917
c24bdb02
KS
13918 if (*cu->get_builder ()->get_local_symbols () != NULL
13919 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13920 {
801e3a5b 13921 struct block *block
c24bdb02 13922 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13923 cstk.start_addr, highpc);
801e3a5b
JB
13924
13925 /* Note that recording ranges after traversing children, as we
13926 do here, means that recording a parent's ranges entails
13927 walking across all its children's ranges as they appear in
13928 the address map, which is quadratic behavior.
13929
13930 It would be nicer to record the parent's ranges before
13931 traversing its children, simply overriding whatever you find
13932 there. But since we don't even decide whether to create a
13933 block until after we've traversed its children, that's hard
13934 to do. */
13935 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13936 }
c24bdb02
KS
13937 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13938 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13939}
13940
216f72a1 13941/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13942
13943static void
13944read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13945{
518817b3 13946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13947 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13948 CORE_ADDR pc, baseaddr;
13949 struct attribute *attr;
13950 struct call_site *call_site, call_site_local;
13951 void **slot;
13952 int nparams;
13953 struct die_info *child_die;
13954
13955 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13956
216f72a1
JK
13957 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13958 if (attr == NULL)
13959 {
13960 /* This was a pre-DWARF-5 GNU extension alias
13961 for DW_AT_call_return_pc. */
13962 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13963 }
96408a79
SA
13964 if (!attr)
13965 {
b98664d3 13966 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13967 "DIE %s [in module %s]"),
13968 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13969 return;
13970 }
31aa7e4e 13971 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13972 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13973
13974 if (cu->call_site_htab == NULL)
13975 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13976 NULL, &objfile->objfile_obstack,
13977 hashtab_obstack_allocate, NULL);
13978 call_site_local.pc = pc;
13979 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13980 if (*slot != NULL)
13981 {
b98664d3 13982 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13983 "DIE %s [in module %s]"),
13984 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13985 objfile_name (objfile));
96408a79
SA
13986 return;
13987 }
13988
13989 /* Count parameters at the caller. */
13990
13991 nparams = 0;
13992 for (child_die = die->child; child_die && child_die->tag;
13993 child_die = sibling_die (child_die))
13994 {
216f72a1
JK
13995 if (child_die->tag != DW_TAG_call_site_parameter
13996 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13997 {
b98664d3 13998 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13999 "DW_TAG_call_site child DIE %s [in module %s]"),
14000 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14001 objfile_name (objfile));
96408a79
SA
14002 continue;
14003 }
14004
14005 nparams++;
14006 }
14007
224c3ddb
SM
14008 call_site
14009 = ((struct call_site *)
14010 obstack_alloc (&objfile->objfile_obstack,
14011 sizeof (*call_site)
14012 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14013 *slot = call_site;
14014 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14015 call_site->pc = pc;
14016
216f72a1
JK
14017 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14018 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14019 {
14020 struct die_info *func_die;
14021
14022 /* Skip also over DW_TAG_inlined_subroutine. */
14023 for (func_die = die->parent;
14024 func_die && func_die->tag != DW_TAG_subprogram
14025 && func_die->tag != DW_TAG_subroutine_type;
14026 func_die = func_die->parent);
14027
216f72a1
JK
14028 /* DW_AT_call_all_calls is a superset
14029 of DW_AT_call_all_tail_calls. */
96408a79 14030 if (func_die
216f72a1 14031 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14032 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14033 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14034 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14035 {
14036 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14037 not complete. But keep CALL_SITE for look ups via call_site_htab,
14038 both the initial caller containing the real return address PC and
14039 the final callee containing the current PC of a chain of tail
14040 calls do not need to have the tail call list complete. But any
14041 function candidate for a virtual tail call frame searched via
14042 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14043 determined unambiguously. */
14044 }
14045 else
14046 {
14047 struct type *func_type = NULL;
14048
14049 if (func_die)
14050 func_type = get_die_type (func_die, cu);
14051 if (func_type != NULL)
14052 {
14053 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14054
14055 /* Enlist this call site to the function. */
14056 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14057 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14058 }
14059 else
b98664d3 14060 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14061 "DIE %s [in module %s]"),
14062 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14063 }
14064 }
14065
216f72a1
JK
14066 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14067 if (attr == NULL)
14068 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14069 if (attr == NULL)
14070 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14071 if (attr == NULL)
216f72a1
JK
14072 {
14073 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14074 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14075 }
96408a79
SA
14076 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14077 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14078 /* Keep NULL DWARF_BLOCK. */;
14079 else if (attr_form_is_block (attr))
14080 {
14081 struct dwarf2_locexpr_baton *dlbaton;
14082
8d749320 14083 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14084 dlbaton->data = DW_BLOCK (attr)->data;
14085 dlbaton->size = DW_BLOCK (attr)->size;
14086 dlbaton->per_cu = cu->per_cu;
14087
14088 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14089 }
7771576e 14090 else if (attr_form_is_ref (attr))
96408a79 14091 {
96408a79
SA
14092 struct dwarf2_cu *target_cu = cu;
14093 struct die_info *target_die;
14094
ac9ec31b 14095 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14096 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14097 if (die_is_declaration (target_die, target_cu))
14098 {
7d45c7c3 14099 const char *target_physname;
9112db09
JK
14100
14101 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14102 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14103 if (target_physname == NULL)
9112db09 14104 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14105 if (target_physname == NULL)
b98664d3 14106 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14107 "physname, for referencing DIE %s [in module %s]"),
14108 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14109 else
7d455152 14110 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14111 }
14112 else
14113 {
14114 CORE_ADDR lowpc;
14115
14116 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14117 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14118 <= PC_BOUNDS_INVALID)
b98664d3 14119 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14120 "low pc, for referencing DIE %s [in module %s]"),
14121 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14122 else
3e29f34a
MR
14123 {
14124 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14125 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14126 }
96408a79
SA
14127 }
14128 }
14129 else
b98664d3 14130 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14131 "block nor reference, for DIE %s [in module %s]"),
14132 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14133
14134 call_site->per_cu = cu->per_cu;
14135
14136 for (child_die = die->child;
14137 child_die && child_die->tag;
14138 child_die = sibling_die (child_die))
14139 {
96408a79 14140 struct call_site_parameter *parameter;
1788b2d3 14141 struct attribute *loc, *origin;
96408a79 14142
216f72a1
JK
14143 if (child_die->tag != DW_TAG_call_site_parameter
14144 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14145 {
14146 /* Already printed the complaint above. */
14147 continue;
14148 }
14149
14150 gdb_assert (call_site->parameter_count < nparams);
14151 parameter = &call_site->parameter[call_site->parameter_count];
14152
1788b2d3
JK
14153 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14154 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14155 register is contained in DW_AT_call_value. */
96408a79 14156
24c5c679 14157 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14158 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14159 if (origin == NULL)
14160 {
14161 /* This was a pre-DWARF-5 GNU extension alias
14162 for DW_AT_call_parameter. */
14163 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14164 }
7771576e 14165 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14166 {
1788b2d3 14167 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14168
14169 sect_offset sect_off
14170 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14171 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14172 {
14173 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14174 binding can be done only inside one CU. Such referenced DIE
14175 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14176 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14177 "DW_TAG_call_site child DIE %s [in module %s]"),
14178 sect_offset_str (child_die->sect_off),
9c541725 14179 objfile_name (objfile));
d76b7dbc
JK
14180 continue;
14181 }
9c541725
PA
14182 parameter->u.param_cu_off
14183 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14184 }
14185 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14186 {
b98664d3 14187 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14188 "DW_TAG_call_site child DIE %s [in module %s]"),
14189 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14190 continue;
14191 }
24c5c679 14192 else
96408a79 14193 {
24c5c679
JK
14194 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14195 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14196 if (parameter->u.dwarf_reg != -1)
14197 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14198 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14199 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14200 &parameter->u.fb_offset))
14201 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14202 else
14203 {
b98664d3 14204 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14205 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14206 "DW_TAG_call_site child DIE %s "
24c5c679 14207 "[in module %s]"),
9d8780f0 14208 sect_offset_str (child_die->sect_off),
9c541725 14209 objfile_name (objfile));
24c5c679
JK
14210 continue;
14211 }
96408a79
SA
14212 }
14213
216f72a1
JK
14214 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14215 if (attr == NULL)
14216 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14217 if (!attr_form_is_block (attr))
14218 {
b98664d3 14219 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14220 "DW_TAG_call_site child DIE %s [in module %s]"),
14221 sect_offset_str (child_die->sect_off),
9c541725 14222 objfile_name (objfile));
96408a79
SA
14223 continue;
14224 }
14225 parameter->value = DW_BLOCK (attr)->data;
14226 parameter->value_size = DW_BLOCK (attr)->size;
14227
14228 /* Parameters are not pre-cleared by memset above. */
14229 parameter->data_value = NULL;
14230 parameter->data_value_size = 0;
14231 call_site->parameter_count++;
14232
216f72a1
JK
14233 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14234 if (attr == NULL)
14235 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14236 if (attr)
14237 {
14238 if (!attr_form_is_block (attr))
b98664d3 14239 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14240 "DW_TAG_call_site child DIE %s [in module %s]"),
14241 sect_offset_str (child_die->sect_off),
9c541725 14242 objfile_name (objfile));
96408a79
SA
14243 else
14244 {
14245 parameter->data_value = DW_BLOCK (attr)->data;
14246 parameter->data_value_size = DW_BLOCK (attr)->size;
14247 }
14248 }
14249 }
14250}
14251
71a3c369
TT
14252/* Helper function for read_variable. If DIE represents a virtual
14253 table, then return the type of the concrete object that is
14254 associated with the virtual table. Otherwise, return NULL. */
14255
14256static struct type *
14257rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14258{
14259 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14260 if (attr == NULL)
14261 return NULL;
14262
14263 /* Find the type DIE. */
14264 struct die_info *type_die = NULL;
14265 struct dwarf2_cu *type_cu = cu;
14266
14267 if (attr_form_is_ref (attr))
14268 type_die = follow_die_ref (die, attr, &type_cu);
14269 if (type_die == NULL)
14270 return NULL;
14271
14272 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14273 return NULL;
14274 return die_containing_type (type_die, type_cu);
14275}
14276
14277/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14278
14279static void
14280read_variable (struct die_info *die, struct dwarf2_cu *cu)
14281{
14282 struct rust_vtable_symbol *storage = NULL;
14283
14284 if (cu->language == language_rust)
14285 {
14286 struct type *containing_type = rust_containing_type (die, cu);
14287
14288 if (containing_type != NULL)
14289 {
518817b3 14290 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14291
14292 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14293 struct rust_vtable_symbol);
14294 initialize_objfile_symbol (storage);
14295 storage->concrete_type = containing_type;
cf724bc9 14296 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14297 }
14298 }
14299
e4a62c65
TV
14300 struct symbol *res = new_symbol (die, NULL, cu, storage);
14301 struct attribute *abstract_origin
14302 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14303 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14304 if (res == NULL && loc && abstract_origin)
14305 {
14306 /* We have a variable without a name, but with a location and an abstract
14307 origin. This may be a concrete instance of an abstract variable
14308 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14309 later. */
14310 struct dwarf2_cu *origin_cu = cu;
14311 struct die_info *origin_die
14312 = follow_die_ref (die, abstract_origin, &origin_cu);
14313 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14314 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14315 }
71a3c369
TT
14316}
14317
43988095
JK
14318/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14319 reading .debug_rnglists.
14320 Callback's type should be:
14321 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14322 Return true if the attributes are present and valid, otherwise,
14323 return false. */
14324
14325template <typename Callback>
14326static bool
14327dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14328 Callback &&callback)
14329{
ed2dc618 14330 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14331 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14332 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14333 bfd *obfd = objfile->obfd;
43988095
JK
14334 /* Base address selection entry. */
14335 CORE_ADDR base;
14336 int found_base;
43988095 14337 const gdb_byte *buffer;
43988095
JK
14338 CORE_ADDR baseaddr;
14339 bool overflow = false;
14340
14341 found_base = cu->base_known;
14342 base = cu->base_address;
14343
14344 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14345 if (offset >= dwarf2_per_objfile->rnglists.size)
14346 {
b98664d3 14347 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14348 offset);
14349 return false;
14350 }
14351 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14352
14353 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14354
14355 while (1)
14356 {
7814882a
JK
14357 /* Initialize it due to a false compiler warning. */
14358 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14359 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14360 + dwarf2_per_objfile->rnglists.size);
14361 unsigned int bytes_read;
14362
14363 if (buffer == buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14369 switch (rlet)
14370 {
14371 case DW_RLE_end_of_list:
14372 break;
14373 case DW_RLE_base_address:
14374 if (buffer + cu->header.addr_size > buf_end)
14375 {
14376 overflow = true;
14377 break;
14378 }
14379 base = read_address (obfd, buffer, cu, &bytes_read);
14380 found_base = 1;
14381 buffer += bytes_read;
14382 break;
14383 case DW_RLE_start_length:
14384 if (buffer + cu->header.addr_size > buf_end)
14385 {
14386 overflow = true;
14387 break;
14388 }
14389 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14390 buffer += bytes_read;
14391 range_end = (range_beginning
14392 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14393 buffer += bytes_read;
14394 if (buffer > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 break;
14400 case DW_RLE_offset_pair:
14401 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14402 buffer += bytes_read;
14403 if (buffer > buf_end)
14404 {
14405 overflow = true;
14406 break;
14407 }
14408 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14409 buffer += bytes_read;
14410 if (buffer > buf_end)
14411 {
14412 overflow = true;
14413 break;
14414 }
14415 break;
14416 case DW_RLE_start_end:
14417 if (buffer + 2 * cu->header.addr_size > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14423 buffer += bytes_read;
14424 range_end = read_address (obfd, buffer, cu, &bytes_read);
14425 buffer += bytes_read;
14426 break;
14427 default:
b98664d3 14428 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14429 return false;
14430 }
14431 if (rlet == DW_RLE_end_of_list || overflow)
14432 break;
14433 if (rlet == DW_RLE_base_address)
14434 continue;
14435
14436 if (!found_base)
14437 {
14438 /* We have no valid base address for the ranges
14439 data. */
b98664d3 14440 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14441 return false;
14442 }
14443
14444 if (range_beginning > range_end)
14445 {
14446 /* Inverted range entries are invalid. */
b98664d3 14447 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14448 return false;
14449 }
14450
14451 /* Empty range entries have no effect. */
14452 if (range_beginning == range_end)
14453 continue;
14454
14455 range_beginning += base;
14456 range_end += base;
14457
14458 /* A not-uncommon case of bad debug info.
14459 Don't pollute the addrmap with bad data. */
14460 if (range_beginning + baseaddr == 0
14461 && !dwarf2_per_objfile->has_section_at_zero)
14462 {
b98664d3 14463 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14464 " [in module %s]"), objfile_name (objfile));
14465 continue;
14466 }
14467
14468 callback (range_beginning, range_end);
14469 }
14470
14471 if (overflow)
14472 {
b98664d3 14473 complaint (_("Offset %d is not terminated "
43988095
JK
14474 "for DW_AT_ranges attribute"),
14475 offset);
14476 return false;
14477 }
14478
14479 return true;
14480}
14481
14482/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14483 Callback's type should be:
14484 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14485 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14486
43988095 14487template <typename Callback>
43039443 14488static int
5f46c5a5 14489dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14490 Callback &&callback)
43039443 14491{
ed2dc618 14492 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14493 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14494 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14495 struct comp_unit_head *cu_header = &cu->header;
14496 bfd *obfd = objfile->obfd;
14497 unsigned int addr_size = cu_header->addr_size;
14498 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14499 /* Base address selection entry. */
14500 CORE_ADDR base;
14501 int found_base;
14502 unsigned int dummy;
d521ce57 14503 const gdb_byte *buffer;
ff013f42 14504 CORE_ADDR baseaddr;
43039443 14505
43988095
JK
14506 if (cu_header->version >= 5)
14507 return dwarf2_rnglists_process (offset, cu, callback);
14508
d00adf39
DE
14509 found_base = cu->base_known;
14510 base = cu->base_address;
43039443 14511
be391dca 14512 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14513 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14514 {
b98664d3 14515 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14516 offset);
14517 return 0;
14518 }
dce234bc 14519 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14520
e7030f15 14521 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14522
43039443
JK
14523 while (1)
14524 {
14525 CORE_ADDR range_beginning, range_end;
14526
14527 range_beginning = read_address (obfd, buffer, cu, &dummy);
14528 buffer += addr_size;
14529 range_end = read_address (obfd, buffer, cu, &dummy);
14530 buffer += addr_size;
14531 offset += 2 * addr_size;
14532
14533 /* An end of list marker is a pair of zero addresses. */
14534 if (range_beginning == 0 && range_end == 0)
14535 /* Found the end of list entry. */
14536 break;
14537
14538 /* Each base address selection entry is a pair of 2 values.
14539 The first is the largest possible address, the second is
14540 the base address. Check for a base address here. */
14541 if ((range_beginning & mask) == mask)
14542 {
28d2bfb9
AB
14543 /* If we found the largest possible address, then we already
14544 have the base address in range_end. */
14545 base = range_end;
43039443
JK
14546 found_base = 1;
14547 continue;
14548 }
14549
14550 if (!found_base)
14551 {
14552 /* We have no valid base address for the ranges
14553 data. */
b98664d3 14554 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14555 return 0;
14556 }
14557
9277c30c
UW
14558 if (range_beginning > range_end)
14559 {
14560 /* Inverted range entries are invalid. */
b98664d3 14561 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14562 return 0;
14563 }
14564
14565 /* Empty range entries have no effect. */
14566 if (range_beginning == range_end)
14567 continue;
14568
43039443
JK
14569 range_beginning += base;
14570 range_end += base;
14571
01093045
DE
14572 /* A not-uncommon case of bad debug info.
14573 Don't pollute the addrmap with bad data. */
14574 if (range_beginning + baseaddr == 0
14575 && !dwarf2_per_objfile->has_section_at_zero)
14576 {
b98664d3 14577 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14578 " [in module %s]"), objfile_name (objfile));
01093045
DE
14579 continue;
14580 }
14581
5f46c5a5
JK
14582 callback (range_beginning, range_end);
14583 }
14584
14585 return 1;
14586}
14587
14588/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14589 Return 1 if the attributes are present and valid, otherwise, return 0.
14590 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14591
14592static int
14593dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14594 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14595 struct partial_symtab *ranges_pst)
14596{
518817b3 14597 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14598 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14599 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14600 SECT_OFF_TEXT (objfile));
14601 int low_set = 0;
14602 CORE_ADDR low = 0;
14603 CORE_ADDR high = 0;
14604 int retval;
14605
14606 retval = dwarf2_ranges_process (offset, cu,
14607 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14608 {
9277c30c 14609 if (ranges_pst != NULL)
3e29f34a
MR
14610 {
14611 CORE_ADDR lowpc;
14612 CORE_ADDR highpc;
14613
79748972
TT
14614 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14615 range_beginning + baseaddr)
14616 - baseaddr);
14617 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14618 range_end + baseaddr)
14619 - baseaddr);
d320c2b5
TT
14620 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14621 lowpc, highpc - 1, ranges_pst);
3e29f34a 14622 }
ff013f42 14623
43039443
JK
14624 /* FIXME: This is recording everything as a low-high
14625 segment of consecutive addresses. We should have a
14626 data structure for discontiguous block ranges
14627 instead. */
14628 if (! low_set)
14629 {
14630 low = range_beginning;
14631 high = range_end;
14632 low_set = 1;
14633 }
14634 else
14635 {
14636 if (range_beginning < low)
14637 low = range_beginning;
14638 if (range_end > high)
14639 high = range_end;
14640 }
5f46c5a5
JK
14641 });
14642 if (!retval)
14643 return 0;
43039443
JK
14644
14645 if (! low_set)
14646 /* If the first entry is an end-of-list marker, the range
14647 describes an empty scope, i.e. no instructions. */
14648 return 0;
14649
14650 if (low_return)
14651 *low_return = low;
14652 if (high_return)
14653 *high_return = high;
14654 return 1;
14655}
14656
3a2b436a
JK
14657/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14658 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14659 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14660
3a2b436a 14661static enum pc_bounds_kind
af34e669 14662dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14663 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14664 struct partial_symtab *pst)
c906108c 14665{
518817b3
SM
14666 struct dwarf2_per_objfile *dwarf2_per_objfile
14667 = cu->per_cu->dwarf2_per_objfile;
c906108c 14668 struct attribute *attr;
91da1414 14669 struct attribute *attr_high;
af34e669
DJ
14670 CORE_ADDR low = 0;
14671 CORE_ADDR high = 0;
e385593e 14672 enum pc_bounds_kind ret;
c906108c 14673
91da1414
MW
14674 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14675 if (attr_high)
af34e669 14676 {
e142c38c 14677 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14678 if (attr)
91da1414 14679 {
31aa7e4e
JB
14680 low = attr_value_as_address (attr);
14681 high = attr_value_as_address (attr_high);
14682 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14683 high += low;
91da1414 14684 }
af34e669
DJ
14685 else
14686 /* Found high w/o low attribute. */
e385593e 14687 return PC_BOUNDS_INVALID;
af34e669
DJ
14688
14689 /* Found consecutive range of addresses. */
3a2b436a 14690 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14691 }
c906108c 14692 else
af34e669 14693 {
e142c38c 14694 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14695 if (attr != NULL)
14696 {
ab435259
DE
14697 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14698 We take advantage of the fact that DW_AT_ranges does not appear
14699 in DW_TAG_compile_unit of DWO files. */
14700 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14701 unsigned int ranges_offset = (DW_UNSND (attr)
14702 + (need_ranges_base
14703 ? cu->ranges_base
14704 : 0));
2e3cf129 14705
af34e669 14706 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14707 .debug_ranges section. */
2e3cf129 14708 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14709 return PC_BOUNDS_INVALID;
43039443 14710 /* Found discontinuous range of addresses. */
3a2b436a 14711 ret = PC_BOUNDS_RANGES;
af34e669 14712 }
e385593e
JK
14713 else
14714 return PC_BOUNDS_NOT_PRESENT;
af34e669 14715 }
c906108c 14716
48fbe735 14717 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14718 if (high <= low)
e385593e 14719 return PC_BOUNDS_INVALID;
c906108c
SS
14720
14721 /* When using the GNU linker, .gnu.linkonce. sections are used to
14722 eliminate duplicate copies of functions and vtables and such.
14723 The linker will arbitrarily choose one and discard the others.
14724 The AT_*_pc values for such functions refer to local labels in
14725 these sections. If the section from that file was discarded, the
14726 labels are not in the output, so the relocs get a value of 0.
14727 If this is a discarded function, mark the pc bounds as invalid,
14728 so that GDB will ignore it. */
72dca2f5 14729 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14730 return PC_BOUNDS_INVALID;
c906108c
SS
14731
14732 *lowpc = low;
96408a79
SA
14733 if (highpc)
14734 *highpc = high;
af34e669 14735 return ret;
c906108c
SS
14736}
14737
b084d499
JB
14738/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14739 its low and high PC addresses. Do nothing if these addresses could not
14740 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14741 and HIGHPC to the high address if greater than HIGHPC. */
14742
14743static void
14744dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14745 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14746 struct dwarf2_cu *cu)
14747{
14748 CORE_ADDR low, high;
14749 struct die_info *child = die->child;
14750
e385593e 14751 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14752 {
325fac50
PA
14753 *lowpc = std::min (*lowpc, low);
14754 *highpc = std::max (*highpc, high);
b084d499
JB
14755 }
14756
14757 /* If the language does not allow nested subprograms (either inside
14758 subprograms or lexical blocks), we're done. */
14759 if (cu->language != language_ada)
14760 return;
6e70227d 14761
b084d499
JB
14762 /* Check all the children of the given DIE. If it contains nested
14763 subprograms, then check their pc bounds. Likewise, we need to
14764 check lexical blocks as well, as they may also contain subprogram
14765 definitions. */
14766 while (child && child->tag)
14767 {
14768 if (child->tag == DW_TAG_subprogram
14769 || child->tag == DW_TAG_lexical_block)
14770 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14771 child = sibling_die (child);
14772 }
14773}
14774
fae299cd
DC
14775/* Get the low and high pc's represented by the scope DIE, and store
14776 them in *LOWPC and *HIGHPC. If the correct values can't be
14777 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14778
14779static void
14780get_scope_pc_bounds (struct die_info *die,
14781 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14782 struct dwarf2_cu *cu)
14783{
14784 CORE_ADDR best_low = (CORE_ADDR) -1;
14785 CORE_ADDR best_high = (CORE_ADDR) 0;
14786 CORE_ADDR current_low, current_high;
14787
3a2b436a 14788 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14789 >= PC_BOUNDS_RANGES)
fae299cd
DC
14790 {
14791 best_low = current_low;
14792 best_high = current_high;
14793 }
14794 else
14795 {
14796 struct die_info *child = die->child;
14797
14798 while (child && child->tag)
14799 {
14800 switch (child->tag) {
14801 case DW_TAG_subprogram:
b084d499 14802 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14803 break;
14804 case DW_TAG_namespace:
f55ee35c 14805 case DW_TAG_module:
fae299cd
DC
14806 /* FIXME: carlton/2004-01-16: Should we do this for
14807 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14808 that current GCC's always emit the DIEs corresponding
14809 to definitions of methods of classes as children of a
14810 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14811 the DIEs giving the declarations, which could be
14812 anywhere). But I don't see any reason why the
14813 standards says that they have to be there. */
14814 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14815
14816 if (current_low != ((CORE_ADDR) -1))
14817 {
325fac50
PA
14818 best_low = std::min (best_low, current_low);
14819 best_high = std::max (best_high, current_high);
fae299cd
DC
14820 }
14821 break;
14822 default:
0963b4bd 14823 /* Ignore. */
fae299cd
DC
14824 break;
14825 }
14826
14827 child = sibling_die (child);
14828 }
14829 }
14830
14831 *lowpc = best_low;
14832 *highpc = best_high;
14833}
14834
801e3a5b
JB
14835/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14836 in DIE. */
380bca97 14837
801e3a5b
JB
14838static void
14839dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14840 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14841{
518817b3 14842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14843 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14844 struct attribute *attr;
91da1414 14845 struct attribute *attr_high;
801e3a5b 14846
91da1414
MW
14847 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14848 if (attr_high)
801e3a5b 14849 {
801e3a5b
JB
14850 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14851 if (attr)
14852 {
31aa7e4e
JB
14853 CORE_ADDR low = attr_value_as_address (attr);
14854 CORE_ADDR high = attr_value_as_address (attr_high);
14855
14856 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14857 high += low;
9a619af0 14858
3e29f34a
MR
14859 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14860 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14861 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14862 }
14863 }
14864
14865 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14866 if (attr)
14867 {
ab435259
DE
14868 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14869 We take advantage of the fact that DW_AT_ranges does not appear
14870 in DW_TAG_compile_unit of DWO files. */
14871 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14872
14873 /* The value of the DW_AT_ranges attribute is the offset of the
14874 address range list in the .debug_ranges section. */
ab435259
DE
14875 unsigned long offset = (DW_UNSND (attr)
14876 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14877
2d5f09ec 14878 std::vector<blockrange> blockvec;
5f46c5a5
JK
14879 dwarf2_ranges_process (offset, cu,
14880 [&] (CORE_ADDR start, CORE_ADDR end)
14881 {
58fdfd2c
JK
14882 start += baseaddr;
14883 end += baseaddr;
5f46c5a5
JK
14884 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14885 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14886 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14887 blockvec.emplace_back (start, end);
5f46c5a5 14888 });
2d5f09ec
KB
14889
14890 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14891 }
14892}
14893
685b1105
JK
14894/* Check whether the producer field indicates either of GCC < 4.6, or the
14895 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14896
685b1105
JK
14897static void
14898check_producer (struct dwarf2_cu *cu)
60d5a603 14899{
38360086 14900 int major, minor;
60d5a603
JK
14901
14902 if (cu->producer == NULL)
14903 {
14904 /* For unknown compilers expect their behavior is DWARF version
14905 compliant.
14906
14907 GCC started to support .debug_types sections by -gdwarf-4 since
14908 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14909 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14910 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14911 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14912 }
b1ffba5a 14913 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14914 {
38360086
MW
14915 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14916 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14917 }
5230b05a 14918 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14919 {
14920 cu->producer_is_icc = true;
14921 cu->producer_is_icc_lt_14 = major < 14;
14922 }
c258c396
JD
14923 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14924 cu->producer_is_codewarrior = true;
685b1105
JK
14925 else
14926 {
14927 /* For other non-GCC compilers, expect their behavior is DWARF version
14928 compliant. */
60d5a603
JK
14929 }
14930
9068261f 14931 cu->checked_producer = true;
685b1105 14932}
ba919b58 14933
685b1105
JK
14934/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14935 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14936 during 4.6.0 experimental. */
14937
9068261f 14938static bool
685b1105
JK
14939producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14940{
14941 if (!cu->checked_producer)
14942 check_producer (cu);
14943
14944 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14945}
14946
c258c396
JD
14947
14948/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14949 with incorrect is_stmt attributes. */
14950
14951static bool
14952producer_is_codewarrior (struct dwarf2_cu *cu)
14953{
14954 if (!cu->checked_producer)
14955 check_producer (cu);
14956
14957 return cu->producer_is_codewarrior;
14958}
14959
60d5a603
JK
14960/* Return the default accessibility type if it is not overriden by
14961 DW_AT_accessibility. */
14962
14963static enum dwarf_access_attribute
14964dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14965{
14966 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14967 {
14968 /* The default DWARF 2 accessibility for members is public, the default
14969 accessibility for inheritance is private. */
14970
14971 if (die->tag != DW_TAG_inheritance)
14972 return DW_ACCESS_public;
14973 else
14974 return DW_ACCESS_private;
14975 }
14976 else
14977 {
14978 /* DWARF 3+ defines the default accessibility a different way. The same
14979 rules apply now for DW_TAG_inheritance as for the members and it only
14980 depends on the container kind. */
14981
14982 if (die->parent->tag == DW_TAG_class_type)
14983 return DW_ACCESS_private;
14984 else
14985 return DW_ACCESS_public;
14986 }
14987}
14988
74ac6d43
TT
14989/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14990 offset. If the attribute was not found return 0, otherwise return
14991 1. If it was found but could not properly be handled, set *OFFSET
14992 to 0. */
14993
14994static int
14995handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14996 LONGEST *offset)
14997{
14998 struct attribute *attr;
14999
15000 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15001 if (attr != NULL)
15002 {
15003 *offset = 0;
15004
15005 /* Note that we do not check for a section offset first here.
15006 This is because DW_AT_data_member_location is new in DWARF 4,
15007 so if we see it, we can assume that a constant form is really
15008 a constant and not a section offset. */
15009 if (attr_form_is_constant (attr))
15010 *offset = dwarf2_get_attr_constant_value (attr, 0);
15011 else if (attr_form_is_section_offset (attr))
15012 dwarf2_complex_location_expr_complaint ();
15013 else if (attr_form_is_block (attr))
15014 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15015 else
15016 dwarf2_complex_location_expr_complaint ();
15017
15018 return 1;
15019 }
15020
15021 return 0;
15022}
15023
c906108c
SS
15024/* Add an aggregate field to the field list. */
15025
15026static void
107d2387 15027dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15028 struct dwarf2_cu *cu)
6e70227d 15029{
518817b3 15030 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15031 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15032 struct nextfield *new_field;
15033 struct attribute *attr;
15034 struct field *fp;
15d034d0 15035 const char *fieldname = "";
c906108c 15036
7d0ccb61
DJ
15037 if (die->tag == DW_TAG_inheritance)
15038 {
be2daae6
TT
15039 fip->baseclasses.emplace_back ();
15040 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15041 }
15042 else
15043 {
be2daae6
TT
15044 fip->fields.emplace_back ();
15045 new_field = &fip->fields.back ();
7d0ccb61 15046 }
be2daae6 15047
c906108c
SS
15048 fip->nfields++;
15049
e142c38c 15050 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15051 if (attr)
15052 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15053 else
15054 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15055 if (new_field->accessibility != DW_ACCESS_public)
15056 fip->non_public_fields = 1;
60d5a603 15057
e142c38c 15058 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15059 if (attr)
15060 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15061 else
15062 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15063
15064 fp = &new_field->field;
a9a9bd0f 15065
e142c38c 15066 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15067 {
74ac6d43
TT
15068 LONGEST offset;
15069
a9a9bd0f 15070 /* Data member other than a C++ static data member. */
6e70227d 15071
c906108c 15072 /* Get type of field. */
e7c27a73 15073 fp->type = die_type (die, cu);
c906108c 15074
d6a843b5 15075 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15076
c906108c 15077 /* Get bit size of field (zero if none). */
e142c38c 15078 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15079 if (attr)
15080 {
15081 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15082 }
15083 else
15084 {
15085 FIELD_BITSIZE (*fp) = 0;
15086 }
15087
15088 /* Get bit offset of field. */
74ac6d43
TT
15089 if (handle_data_member_location (die, cu, &offset))
15090 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15092 if (attr)
15093 {
5e2b427d 15094 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15095 {
15096 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15097 additional bit offset from the MSB of the containing
15098 anonymous object to the MSB of the field. We don't
15099 have to do anything special since we don't need to
15100 know the size of the anonymous object. */
f41f5e61 15101 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15102 }
15103 else
15104 {
15105 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15106 MSB of the anonymous object, subtract off the number of
15107 bits from the MSB of the field to the MSB of the
15108 object, and then subtract off the number of bits of
15109 the field itself. The result is the bit offset of
15110 the LSB of the field. */
c906108c
SS
15111 int anonymous_size;
15112 int bit_offset = DW_UNSND (attr);
15113
e142c38c 15114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15115 if (attr)
15116 {
15117 /* The size of the anonymous object containing
15118 the bit field is explicit, so use the
15119 indicated size (in bytes). */
15120 anonymous_size = DW_UNSND (attr);
15121 }
15122 else
15123 {
15124 /* The size of the anonymous object containing
15125 the bit field must be inferred from the type
15126 attribute of the data member containing the
15127 bit field. */
15128 anonymous_size = TYPE_LENGTH (fp->type);
15129 }
f41f5e61
PA
15130 SET_FIELD_BITPOS (*fp,
15131 (FIELD_BITPOS (*fp)
15132 + anonymous_size * bits_per_byte
15133 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15134 }
15135 }
da5b30da
AA
15136 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15137 if (attr != NULL)
15138 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15139 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15140
15141 /* Get name of field. */
39cbfefa
DJ
15142 fieldname = dwarf2_name (die, cu);
15143 if (fieldname == NULL)
15144 fieldname = "";
d8151005
DJ
15145
15146 /* The name is already allocated along with this objfile, so we don't
15147 need to duplicate it for the type. */
15148 fp->name = fieldname;
c906108c
SS
15149
15150 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15151 pointer or virtual base class pointer) to private. */
e142c38c 15152 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15153 {
d48cc9dd 15154 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15155 new_field->accessibility = DW_ACCESS_private;
15156 fip->non_public_fields = 1;
15157 }
15158 }
a9a9bd0f 15159 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15160 {
a9a9bd0f
DC
15161 /* C++ static member. */
15162
15163 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15164 is a declaration, but all versions of G++ as of this writing
15165 (so through at least 3.2.1) incorrectly generate
15166 DW_TAG_variable tags. */
6e70227d 15167
ff355380 15168 const char *physname;
c906108c 15169
a9a9bd0f 15170 /* Get name of field. */
39cbfefa
DJ
15171 fieldname = dwarf2_name (die, cu);
15172 if (fieldname == NULL)
c906108c
SS
15173 return;
15174
254e6b9e 15175 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15176 if (attr
15177 /* Only create a symbol if this is an external value.
15178 new_symbol checks this and puts the value in the global symbol
15179 table, which we want. If it is not external, new_symbol
15180 will try to put the value in cu->list_in_scope which is wrong. */
15181 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15182 {
15183 /* A static const member, not much different than an enum as far as
15184 we're concerned, except that we can support more types. */
15185 new_symbol (die, NULL, cu);
15186 }
15187
2df3850c 15188 /* Get physical name. */
ff355380 15189 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15190
d8151005
DJ
15191 /* The name is already allocated along with this objfile, so we don't
15192 need to duplicate it for the type. */
15193 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15194 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15195 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15196 }
15197 else if (die->tag == DW_TAG_inheritance)
15198 {
74ac6d43 15199 LONGEST offset;
d4b96c9a 15200
74ac6d43
TT
15201 /* C++ base class field. */
15202 if (handle_data_member_location (die, cu, &offset))
15203 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15204 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15205 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15206 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15207 }
2ddeaf8a
TT
15208 else if (die->tag == DW_TAG_variant_part)
15209 {
15210 /* process_structure_scope will treat this DIE as a union. */
15211 process_structure_scope (die, cu);
15212
15213 /* The variant part is relative to the start of the enclosing
15214 structure. */
15215 SET_FIELD_BITPOS (*fp, 0);
15216 fp->type = get_die_type (die, cu);
15217 fp->artificial = 1;
15218 fp->name = "<<variant>>";
c8c81635
TT
15219
15220 /* Normally a DW_TAG_variant_part won't have a size, but our
15221 representation requires one, so set it to the maximum of the
15222 child sizes. */
15223 if (TYPE_LENGTH (fp->type) == 0)
15224 {
15225 unsigned max = 0;
15226 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15227 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15228 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15229 TYPE_LENGTH (fp->type) = max;
15230 }
2ddeaf8a
TT
15231 }
15232 else
15233 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15234}
15235
883fd55a
KS
15236/* Can the type given by DIE define another type? */
15237
15238static bool
15239type_can_define_types (const struct die_info *die)
15240{
15241 switch (die->tag)
15242 {
15243 case DW_TAG_typedef:
15244 case DW_TAG_class_type:
15245 case DW_TAG_structure_type:
15246 case DW_TAG_union_type:
15247 case DW_TAG_enumeration_type:
15248 return true;
15249
15250 default:
15251 return false;
15252 }
15253}
15254
15255/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15256
15257static void
883fd55a
KS
15258dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15259 struct dwarf2_cu *cu)
6e70227d 15260{
be2daae6
TT
15261 struct decl_field fp;
15262 memset (&fp, 0, sizeof (fp));
98751a41 15263
883fd55a 15264 gdb_assert (type_can_define_types (die));
98751a41 15265
883fd55a 15266 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15267 fp.name = dwarf2_name (die, cu);
15268 fp.type = read_type_die (die, cu);
98751a41 15269
c191a687
KS
15270 /* Save accessibility. */
15271 enum dwarf_access_attribute accessibility;
15272 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15273 if (attr != NULL)
15274 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15275 else
15276 accessibility = dwarf2_default_access_attribute (die, cu);
15277 switch (accessibility)
15278 {
15279 case DW_ACCESS_public:
15280 /* The assumed value if neither private nor protected. */
15281 break;
15282 case DW_ACCESS_private:
be2daae6 15283 fp.is_private = 1;
c191a687
KS
15284 break;
15285 case DW_ACCESS_protected:
be2daae6 15286 fp.is_protected = 1;
c191a687
KS
15287 break;
15288 default:
b98664d3 15289 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15290 }
15291
883fd55a 15292 if (die->tag == DW_TAG_typedef)
be2daae6 15293 fip->typedef_field_list.push_back (fp);
883fd55a 15294 else
be2daae6 15295 fip->nested_types_list.push_back (fp);
98751a41
JK
15296}
15297
c906108c
SS
15298/* Create the vector of fields, and attach it to the type. */
15299
15300static void
fba45db2 15301dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15302 struct dwarf2_cu *cu)
c906108c
SS
15303{
15304 int nfields = fip->nfields;
15305
15306 /* Record the field count, allocate space for the array of fields,
15307 and create blank accessibility bitfields if necessary. */
15308 TYPE_NFIELDS (type) = nfields;
15309 TYPE_FIELDS (type) = (struct field *)
be2daae6 15310 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15311
b4ba55a1 15312 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15313 {
15314 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15315
15316 TYPE_FIELD_PRIVATE_BITS (type) =
15317 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15318 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15319
15320 TYPE_FIELD_PROTECTED_BITS (type) =
15321 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15322 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15323
774b6a14
TT
15324 TYPE_FIELD_IGNORE_BITS (type) =
15325 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15326 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15327 }
15328
15329 /* If the type has baseclasses, allocate and clear a bit vector for
15330 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15331 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15332 {
be2daae6 15333 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15334 unsigned char *pointer;
c906108c
SS
15335
15336 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15337 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15338 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15339 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15340 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15341 }
15342
2ddeaf8a
TT
15343 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15344 {
15345 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15346
be2daae6 15347 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15348 {
be2daae6
TT
15349 struct nextfield &field = fip->fields[index];
15350
15351 if (field.variant.is_discriminant)
2ddeaf8a 15352 di->discriminant_index = index;
be2daae6 15353 else if (field.variant.default_branch)
2ddeaf8a
TT
15354 di->default_index = index;
15355 else
be2daae6 15356 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15357 }
15358 }
15359
be2daae6
TT
15360 /* Copy the saved-up fields into the field vector. */
15361 for (int i = 0; i < nfields; ++i)
c906108c 15362 {
be2daae6
TT
15363 struct nextfield &field
15364 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15365 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15366
be2daae6
TT
15367 TYPE_FIELD (type, i) = field.field;
15368 switch (field.accessibility)
c906108c 15369 {
c5aa993b 15370 case DW_ACCESS_private:
b4ba55a1 15371 if (cu->language != language_ada)
be2daae6 15372 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15373 break;
c906108c 15374
c5aa993b 15375 case DW_ACCESS_protected:
b4ba55a1 15376 if (cu->language != language_ada)
be2daae6 15377 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15378 break;
c906108c 15379
c5aa993b
JM
15380 case DW_ACCESS_public:
15381 break;
c906108c 15382
c5aa993b
JM
15383 default:
15384 /* Unknown accessibility. Complain and treat it as public. */
15385 {
b98664d3 15386 complaint (_("unsupported accessibility %d"),
be2daae6 15387 field.accessibility);
c5aa993b
JM
15388 }
15389 break;
c906108c 15390 }
be2daae6 15391 if (i < fip->baseclasses.size ())
c906108c 15392 {
be2daae6 15393 switch (field.virtuality)
c906108c 15394 {
c5aa993b
JM
15395 case DW_VIRTUALITY_virtual:
15396 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15397 if (cu->language == language_ada)
a73c6dcd 15398 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15399 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15400 break;
c906108c
SS
15401 }
15402 }
c906108c
SS
15403 }
15404}
15405
7d27a96d
TT
15406/* Return true if this member function is a constructor, false
15407 otherwise. */
15408
15409static int
15410dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15411{
15412 const char *fieldname;
fe978cb0 15413 const char *type_name;
7d27a96d
TT
15414 int len;
15415
15416 if (die->parent == NULL)
15417 return 0;
15418
15419 if (die->parent->tag != DW_TAG_structure_type
15420 && die->parent->tag != DW_TAG_union_type
15421 && die->parent->tag != DW_TAG_class_type)
15422 return 0;
15423
15424 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15425 type_name = dwarf2_name (die->parent, cu);
15426 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15427 return 0;
15428
15429 len = strlen (fieldname);
fe978cb0
PA
15430 return (strncmp (fieldname, type_name, len) == 0
15431 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15432}
15433
c906108c
SS
15434/* Add a member function to the proper fieldlist. */
15435
15436static void
107d2387 15437dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15438 struct type *type, struct dwarf2_cu *cu)
c906108c 15439{
518817b3 15440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15441 struct attribute *attr;
c906108c 15442 int i;
be2daae6 15443 struct fnfieldlist *flp = nullptr;
c906108c 15444 struct fn_field *fnp;
15d034d0 15445 const char *fieldname;
f792889a 15446 struct type *this_type;
60d5a603 15447 enum dwarf_access_attribute accessibility;
c906108c 15448
b4ba55a1 15449 if (cu->language == language_ada)
a73c6dcd 15450 error (_("unexpected member function in Ada type"));
b4ba55a1 15451
2df3850c 15452 /* Get name of member function. */
39cbfefa
DJ
15453 fieldname = dwarf2_name (die, cu);
15454 if (fieldname == NULL)
2df3850c 15455 return;
c906108c 15456
c906108c 15457 /* Look up member function name in fieldlist. */
be2daae6 15458 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15459 {
27bfe10e 15460 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15461 {
15462 flp = &fip->fnfieldlists[i];
15463 break;
15464 }
c906108c
SS
15465 }
15466
be2daae6
TT
15467 /* Create a new fnfieldlist if necessary. */
15468 if (flp == nullptr)
c906108c 15469 {
be2daae6
TT
15470 fip->fnfieldlists.emplace_back ();
15471 flp = &fip->fnfieldlists.back ();
c906108c 15472 flp->name = fieldname;
be2daae6 15473 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15474 }
15475
be2daae6
TT
15476 /* Create a new member function field and add it to the vector of
15477 fnfieldlists. */
15478 flp->fnfields.emplace_back ();
15479 fnp = &flp->fnfields.back ();
3da10d80
KS
15480
15481 /* Delay processing of the physname until later. */
9c37b5ae 15482 if (cu->language == language_cplus)
be2daae6
TT
15483 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15484 die, cu);
3da10d80
KS
15485 else
15486 {
1d06ead6 15487 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15488 fnp->physname = physname ? physname : "";
15489 }
15490
c906108c 15491 fnp->type = alloc_type (objfile);
f792889a
DJ
15492 this_type = read_type_die (die, cu);
15493 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15494 {
f792889a 15495 int nparams = TYPE_NFIELDS (this_type);
c906108c 15496
f792889a 15497 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15498 of the method itself (TYPE_CODE_METHOD). */
15499 smash_to_method_type (fnp->type, type,
f792889a
DJ
15500 TYPE_TARGET_TYPE (this_type),
15501 TYPE_FIELDS (this_type),
15502 TYPE_NFIELDS (this_type),
15503 TYPE_VARARGS (this_type));
c906108c
SS
15504
15505 /* Handle static member functions.
c5aa993b 15506 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15507 member functions. G++ helps GDB by marking the first
15508 parameter for non-static member functions (which is the this
15509 pointer) as artificial. We obtain this information from
15510 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15511 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15512 fnp->voffset = VOFFSET_STATIC;
15513 }
15514 else
b98664d3 15515 complaint (_("member function type missing for '%s'"),
3da10d80 15516 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15517
15518 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15519 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15520 fnp->fcontext = die_containing_type (die, cu);
c906108c 15521
3e43a32a
MS
15522 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15523 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15524
15525 /* Get accessibility. */
e142c38c 15526 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15527 if (attr)
aead7601 15528 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15529 else
15530 accessibility = dwarf2_default_access_attribute (die, cu);
15531 switch (accessibility)
c906108c 15532 {
60d5a603
JK
15533 case DW_ACCESS_private:
15534 fnp->is_private = 1;
15535 break;
15536 case DW_ACCESS_protected:
15537 fnp->is_protected = 1;
15538 break;
c906108c
SS
15539 }
15540
b02dede2 15541 /* Check for artificial methods. */
e142c38c 15542 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15543 if (attr && DW_UNSND (attr) != 0)
15544 fnp->is_artificial = 1;
15545
7d27a96d
TT
15546 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15547
0d564a31 15548 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15549 function. For older versions of GCC, this is an offset in the
15550 appropriate virtual table, as specified by DW_AT_containing_type.
15551 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15552 to the object address. */
15553
e142c38c 15554 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15555 if (attr)
8e19ed76 15556 {
aec5aa8b 15557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15558 {
aec5aa8b
TT
15559 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15560 {
15561 /* Old-style GCC. */
15562 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15563 }
15564 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15565 || (DW_BLOCK (attr)->size > 1
15566 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15567 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15568 {
aec5aa8b
TT
15569 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15570 if ((fnp->voffset % cu->header.addr_size) != 0)
15571 dwarf2_complex_location_expr_complaint ();
15572 else
15573 fnp->voffset /= cu->header.addr_size;
15574 fnp->voffset += 2;
15575 }
15576 else
15577 dwarf2_complex_location_expr_complaint ();
15578
15579 if (!fnp->fcontext)
7e993ebf
KS
15580 {
15581 /* If there is no `this' field and no DW_AT_containing_type,
15582 we cannot actually find a base class context for the
15583 vtable! */
15584 if (TYPE_NFIELDS (this_type) == 0
15585 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15586 {
b98664d3 15587 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15588 "function \"%s\" (offset %s)"),
15589 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15590 }
15591 else
15592 {
15593 fnp->fcontext
15594 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15595 }
15596 }
aec5aa8b 15597 }
3690dd37 15598 else if (attr_form_is_section_offset (attr))
8e19ed76 15599 {
4d3c2250 15600 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15601 }
15602 else
15603 {
4d3c2250
KB
15604 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15605 fieldname);
8e19ed76 15606 }
0d564a31 15607 }
d48cc9dd
DJ
15608 else
15609 {
15610 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15611 if (attr && DW_UNSND (attr))
15612 {
15613 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15614 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15615 "but the vtable offset is not specified"),
9d8780f0 15616 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15618 TYPE_CPLUS_DYNAMIC (type) = 1;
15619 }
15620 }
c906108c
SS
15621}
15622
15623/* Create the vector of member function fields, and attach it to the type. */
15624
15625static void
fba45db2 15626dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15627 struct dwarf2_cu *cu)
c906108c 15628{
b4ba55a1 15629 if (cu->language == language_ada)
a73c6dcd 15630 error (_("unexpected member functions in Ada type"));
b4ba55a1 15631
c906108c
SS
15632 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15633 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15634 TYPE_ALLOC (type,
15635 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15636
be2daae6 15637 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15638 {
be2daae6 15639 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15640 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15641
be2daae6
TT
15642 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15643 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15644 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15645 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15646
15647 for (int k = 0; k < nf.fnfields.size (); ++k)
15648 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15649 }
15650
be2daae6 15651 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15652}
15653
1168df01
JB
15654/* Returns non-zero if NAME is the name of a vtable member in CU's
15655 language, zero otherwise. */
15656static int
15657is_vtable_name (const char *name, struct dwarf2_cu *cu)
15658{
15659 static const char vptr[] = "_vptr";
15660
9c37b5ae
TT
15661 /* Look for the C++ form of the vtable. */
15662 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15663 return 1;
15664
15665 return 0;
15666}
15667
c0dd20ea 15668/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15669 functions, with the ABI-specified layout. If TYPE describes
15670 such a structure, smash it into a member function type.
61049d3b
DJ
15671
15672 GCC shouldn't do this; it should just output pointer to member DIEs.
15673 This is GCC PR debug/28767. */
c0dd20ea 15674
0b92b5bb
TT
15675static void
15676quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15677{
09e2d7c7 15678 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15679
15680 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15681 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15682 return;
c0dd20ea
DJ
15683
15684 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15685 if (TYPE_FIELD_NAME (type, 0) == NULL
15686 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15687 || TYPE_FIELD_NAME (type, 1) == NULL
15688 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15689 return;
c0dd20ea
DJ
15690
15691 /* Find the type of the method. */
0b92b5bb 15692 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15693 if (pfn_type == NULL
15694 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15695 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15696 return;
c0dd20ea
DJ
15697
15698 /* Look for the "this" argument. */
15699 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15700 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15701 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15702 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15703 return;
c0dd20ea 15704
09e2d7c7 15705 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15706 new_type = alloc_type (objfile);
09e2d7c7 15707 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15708 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15709 TYPE_VARARGS (pfn_type));
0b92b5bb 15710 smash_to_methodptr_type (type, new_type);
c0dd20ea 15711}
1168df01 15712
2b4424c3
TT
15713/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15714 appropriate error checking and issuing complaints if there is a
15715 problem. */
15716
15717static ULONGEST
15718get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15719{
15720 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15721
15722 if (attr == nullptr)
15723 return 0;
15724
15725 if (!attr_form_is_constant (attr))
15726 {
b98664d3 15727 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15728 " - DIE at %s [in module %s]"),
15729 sect_offset_str (die->sect_off),
15730 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15731 return 0;
15732 }
15733
15734 ULONGEST align;
15735 if (attr->form == DW_FORM_sdata)
15736 {
15737 LONGEST val = DW_SND (attr);
15738 if (val < 0)
15739 {
b98664d3 15740 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15741 " - DIE at %s [in module %s]"),
15742 sect_offset_str (die->sect_off),
15743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15744 return 0;
15745 }
15746 align = val;
15747 }
15748 else
15749 align = DW_UNSND (attr);
15750
15751 if (align == 0)
15752 {
b98664d3 15753 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15754 " - DIE at %s [in module %s]"),
15755 sect_offset_str (die->sect_off),
15756 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15757 return 0;
15758 }
15759 if ((align & (align - 1)) != 0)
15760 {
b98664d3 15761 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15762 " - DIE at %s [in module %s]"),
15763 sect_offset_str (die->sect_off),
15764 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15765 return 0;
15766 }
15767
15768 return align;
15769}
15770
15771/* If the DIE has a DW_AT_alignment attribute, use its value to set
15772 the alignment for TYPE. */
15773
15774static void
15775maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15776 struct type *type)
15777{
15778 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15779 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15780 " - DIE at %s [in module %s]"),
15781 sect_offset_str (die->sect_off),
15782 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15783}
685b1105 15784
c906108c 15785/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15786 (definition) to create a type for the structure or union. Fill in
15787 the type's name and general properties; the members will not be
83655187
DE
15788 processed until process_structure_scope. A symbol table entry for
15789 the type will also not be done until process_structure_scope (assuming
15790 the type has a name).
c906108c 15791
c767944b
DJ
15792 NOTE: we need to call these functions regardless of whether or not the
15793 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15794 structure or union. This gets the type entered into our set of
83655187 15795 user defined types. */
c906108c 15796
f792889a 15797static struct type *
134d01f1 15798read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15799{
518817b3 15800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15801 struct type *type;
15802 struct attribute *attr;
15d034d0 15803 const char *name;
c906108c 15804
348e048f
DE
15805 /* If the definition of this type lives in .debug_types, read that type.
15806 Don't follow DW_AT_specification though, that will take us back up
15807 the chain and we want to go down. */
45e58e77 15808 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15809 if (attr)
15810 {
ac9ec31b 15811 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15812
ac9ec31b 15813 /* The type's CU may not be the same as CU.
02142a6c 15814 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15815 return set_die_type (die, type, cu);
15816 }
15817
c0dd20ea 15818 type = alloc_type (objfile);
c906108c 15819 INIT_CPLUS_SPECIFIC (type);
93311388 15820
39cbfefa
DJ
15821 name = dwarf2_name (die, cu);
15822 if (name != NULL)
c906108c 15823 {
987504bb 15824 if (cu->language == language_cplus
c44af4eb
TT
15825 || cu->language == language_d
15826 || cu->language == language_rust)
63d06c5c 15827 {
15d034d0 15828 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15829
15830 /* dwarf2_full_name might have already finished building the DIE's
15831 type. If so, there is no need to continue. */
15832 if (get_die_type (die, cu) != NULL)
15833 return get_die_type (die, cu);
15834
e86ca25f 15835 TYPE_NAME (type) = full_name;
63d06c5c
DC
15836 }
15837 else
15838 {
d8151005
DJ
15839 /* The name is already allocated along with this objfile, so
15840 we don't need to duplicate it for the type. */
e86ca25f 15841 TYPE_NAME (type) = name;
63d06c5c 15842 }
c906108c
SS
15843 }
15844
15845 if (die->tag == DW_TAG_structure_type)
15846 {
15847 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15848 }
15849 else if (die->tag == DW_TAG_union_type)
15850 {
15851 TYPE_CODE (type) = TYPE_CODE_UNION;
15852 }
2ddeaf8a
TT
15853 else if (die->tag == DW_TAG_variant_part)
15854 {
15855 TYPE_CODE (type) = TYPE_CODE_UNION;
15856 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15857 }
c906108c
SS
15858 else
15859 {
4753d33b 15860 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15861 }
15862
0cc2414c
TT
15863 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15864 TYPE_DECLARED_CLASS (type) = 1;
15865
e142c38c 15866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15867 if (attr)
15868 {
155bfbd3
JB
15869 if (attr_form_is_constant (attr))
15870 TYPE_LENGTH (type) = DW_UNSND (attr);
15871 else
15872 {
15873 /* For the moment, dynamic type sizes are not supported
15874 by GDB's struct type. The actual size is determined
15875 on-demand when resolving the type of a given object,
15876 so set the type's length to zero for now. Otherwise,
15877 we record an expression as the length, and that expression
15878 could lead to a very large value, which could eventually
15879 lead to us trying to allocate that much memory when creating
15880 a value of that type. */
15881 TYPE_LENGTH (type) = 0;
15882 }
c906108c
SS
15883 }
15884 else
15885 {
15886 TYPE_LENGTH (type) = 0;
15887 }
15888
2b4424c3
TT
15889 maybe_set_alignment (cu, die, type);
15890
5230b05a 15891 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15892 {
5230b05a
WT
15893 /* ICC<14 does not output the required DW_AT_declaration on
15894 incomplete types, but gives them a size of zero. */
422b1cb0 15895 TYPE_STUB (type) = 1;
685b1105
JK
15896 }
15897 else
15898 TYPE_STUB_SUPPORTED (type) = 1;
15899
dc718098 15900 if (die_is_declaration (die, cu))
876cecd0 15901 TYPE_STUB (type) = 1;
a6c727b2
DJ
15902 else if (attr == NULL && die->child == NULL
15903 && producer_is_realview (cu->producer))
15904 /* RealView does not output the required DW_AT_declaration
15905 on incomplete types. */
15906 TYPE_STUB (type) = 1;
dc718098 15907
c906108c
SS
15908 /* We need to add the type field to the die immediately so we don't
15909 infinitely recurse when dealing with pointers to the structure
0963b4bd 15910 type within the structure itself. */
1c379e20 15911 set_die_type (die, type, cu);
c906108c 15912
7e314c57
JK
15913 /* set_die_type should be already done. */
15914 set_descriptive_type (type, die, cu);
15915
c767944b
DJ
15916 return type;
15917}
15918
2ddeaf8a
TT
15919/* A helper for process_structure_scope that handles a single member
15920 DIE. */
15921
15922static void
15923handle_struct_member_die (struct die_info *child_die, struct type *type,
15924 struct field_info *fi,
15925 std::vector<struct symbol *> *template_args,
15926 struct dwarf2_cu *cu)
15927{
15928 if (child_die->tag == DW_TAG_member
15929 || child_die->tag == DW_TAG_variable
15930 || child_die->tag == DW_TAG_variant_part)
15931 {
15932 /* NOTE: carlton/2002-11-05: A C++ static data member
15933 should be a DW_TAG_member that is a declaration, but
15934 all versions of G++ as of this writing (so through at
15935 least 3.2.1) incorrectly generate DW_TAG_variable
15936 tags for them instead. */
15937 dwarf2_add_field (fi, child_die, cu);
15938 }
15939 else if (child_die->tag == DW_TAG_subprogram)
15940 {
15941 /* Rust doesn't have member functions in the C++ sense.
15942 However, it does emit ordinary functions as children
15943 of a struct DIE. */
15944 if (cu->language == language_rust)
15945 read_func_scope (child_die, cu);
15946 else
15947 {
15948 /* C++ member function. */
15949 dwarf2_add_member_fn (fi, child_die, type, cu);
15950 }
15951 }
15952 else if (child_die->tag == DW_TAG_inheritance)
15953 {
15954 /* C++ base class field. */
15955 dwarf2_add_field (fi, child_die, cu);
15956 }
15957 else if (type_can_define_types (child_die))
15958 dwarf2_add_type_defn (fi, child_die, cu);
15959 else if (child_die->tag == DW_TAG_template_type_param
15960 || child_die->tag == DW_TAG_template_value_param)
15961 {
15962 struct symbol *arg = new_symbol (child_die, NULL, cu);
15963
15964 if (arg != NULL)
15965 template_args->push_back (arg);
15966 }
15967 else if (child_die->tag == DW_TAG_variant)
15968 {
15969 /* In a variant we want to get the discriminant and also add a
15970 field for our sole member child. */
15971 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15972
bde09ab7 15973 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15974 variant_child != NULL;
15975 variant_child = sibling_die (variant_child))
15976 {
15977 if (variant_child->tag == DW_TAG_member)
15978 {
15979 handle_struct_member_die (variant_child, type, fi,
15980 template_args, cu);
15981 /* Only handle the one. */
15982 break;
15983 }
15984 }
15985
15986 /* We don't handle this but we might as well report it if we see
15987 it. */
15988 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15989 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15990 " - DIE at %s [in module %s]"),
15991 sect_offset_str (child_die->sect_off),
15992 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15993
15994 /* The first field was just added, so we can stash the
15995 discriminant there. */
be2daae6 15996 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15997 if (discr == NULL)
be2daae6 15998 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15999 else
be2daae6 16000 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16001 }
16002}
16003
c767944b
DJ
16004/* Finish creating a structure or union type, including filling in
16005 its members and creating a symbol for it. */
16006
16007static void
16008process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16009{
518817b3 16010 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16011 struct die_info *child_die;
c767944b
DJ
16012 struct type *type;
16013
16014 type = get_die_type (die, cu);
16015 if (type == NULL)
16016 type = read_structure_type (die, cu);
16017
2ddeaf8a
TT
16018 /* When reading a DW_TAG_variant_part, we need to notice when we
16019 read the discriminant member, so we can record it later in the
16020 discriminant_info. */
16021 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16022 sect_offset discr_offset;
3e1d3d8c 16023 bool has_template_parameters = false;
2ddeaf8a
TT
16024
16025 if (is_variant_part)
16026 {
16027 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16028 if (discr == NULL)
16029 {
16030 /* Maybe it's a univariant form, an extension we support.
16031 In this case arrange not to check the offset. */
16032 is_variant_part = false;
16033 }
16034 else if (attr_form_is_ref (discr))
16035 {
16036 struct dwarf2_cu *target_cu = cu;
16037 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16038
16039 discr_offset = target_die->sect_off;
16040 }
16041 else
16042 {
b98664d3 16043 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16044 " - DIE at %s [in module %s]"),
16045 sect_offset_str (die->sect_off),
16046 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16047 is_variant_part = false;
16048 }
16049 }
16050
e142c38c 16051 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16052 {
16053 struct field_info fi;
2f4732b0 16054 std::vector<struct symbol *> template_args;
c906108c 16055
639d11d3 16056 child_die = die->child;
c906108c
SS
16057
16058 while (child_die && child_die->tag)
16059 {
2ddeaf8a 16060 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16061
2ddeaf8a 16062 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16063 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16064
c906108c
SS
16065 child_die = sibling_die (child_die);
16066 }
16067
34eaf542 16068 /* Attach template arguments to type. */
2f4732b0 16069 if (!template_args.empty ())
34eaf542 16070 {
3e1d3d8c 16071 has_template_parameters = true;
34eaf542 16072 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16073 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16074 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16075 = XOBNEWVEC (&objfile->objfile_obstack,
16076 struct symbol *,
16077 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16078 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16079 template_args.data (),
34eaf542
TT
16080 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16081 * sizeof (struct symbol *)));
34eaf542
TT
16082 }
16083
c906108c
SS
16084 /* Attach fields and member functions to the type. */
16085 if (fi.nfields)
e7c27a73 16086 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16087 if (!fi.fnfieldlists.empty ())
c906108c 16088 {
e7c27a73 16089 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16090
c5aa993b 16091 /* Get the type which refers to the base class (possibly this
c906108c 16092 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16093 class from the DW_AT_containing_type attribute. This use of
16094 DW_AT_containing_type is a GNU extension. */
c906108c 16095
e142c38c 16096 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16097 {
e7c27a73 16098 struct type *t = die_containing_type (die, cu);
c906108c 16099
ae6ae975 16100 set_type_vptr_basetype (type, t);
c906108c
SS
16101 if (type == t)
16102 {
c906108c
SS
16103 int i;
16104
16105 /* Our own class provides vtbl ptr. */
16106 for (i = TYPE_NFIELDS (t) - 1;
16107 i >= TYPE_N_BASECLASSES (t);
16108 --i)
16109 {
0d5cff50 16110 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16111
1168df01 16112 if (is_vtable_name (fieldname, cu))
c906108c 16113 {
ae6ae975 16114 set_type_vptr_fieldno (type, i);
c906108c
SS
16115 break;
16116 }
16117 }
16118
16119 /* Complain if virtual function table field not found. */
16120 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16121 complaint (_("virtual function table pointer "
3e43a32a 16122 "not found when defining class '%s'"),
e86ca25f 16123 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16124 }
16125 else
16126 {
ae6ae975 16127 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16128 }
16129 }
f6235d4c 16130 else if (cu->producer
61012eef 16131 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16132 {
16133 /* The IBM XLC compiler does not provide direct indication
16134 of the containing type, but the vtable pointer is
16135 always named __vfp. */
16136
16137 int i;
16138
16139 for (i = TYPE_NFIELDS (type) - 1;
16140 i >= TYPE_N_BASECLASSES (type);
16141 --i)
16142 {
16143 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16144 {
ae6ae975
DE
16145 set_type_vptr_fieldno (type, i);
16146 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16147 break;
16148 }
16149 }
16150 }
c906108c 16151 }
98751a41
JK
16152
16153 /* Copy fi.typedef_field_list linked list elements content into the
16154 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16155 if (!fi.typedef_field_list.empty ())
98751a41 16156 {
be2daae6 16157 int count = fi.typedef_field_list.size ();
98751a41 16158
a0d7a4ff 16159 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16160 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16161 = ((struct decl_field *)
be2daae6
TT
16162 TYPE_ALLOC (type,
16163 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16164 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16165
be2daae6
TT
16166 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16167 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16168 }
c767944b 16169
883fd55a
KS
16170 /* Copy fi.nested_types_list linked list elements content into the
16171 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16172 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16173 {
be2daae6 16174 int count = fi.nested_types_list.size ();
883fd55a
KS
16175
16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16177 TYPE_NESTED_TYPES_ARRAY (type)
16178 = ((struct decl_field *)
be2daae6
TT
16179 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16180 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16181
be2daae6
TT
16182 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16183 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16184 }
c906108c 16185 }
63d06c5c 16186
bb5ed363 16187 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16188 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16189 cu->rust_unions.push_back (type);
0b92b5bb 16190
90aeadfc
DC
16191 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16192 snapshots) has been known to create a die giving a declaration
16193 for a class that has, as a child, a die giving a definition for a
16194 nested class. So we have to process our children even if the
16195 current die is a declaration. Normally, of course, a declaration
16196 won't have any children at all. */
134d01f1 16197
ca040673
DE
16198 child_die = die->child;
16199
90aeadfc
DC
16200 while (child_die != NULL && child_die->tag)
16201 {
16202 if (child_die->tag == DW_TAG_member
16203 || child_die->tag == DW_TAG_variable
34eaf542
TT
16204 || child_die->tag == DW_TAG_inheritance
16205 || child_die->tag == DW_TAG_template_value_param
16206 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16207 {
90aeadfc 16208 /* Do nothing. */
134d01f1 16209 }
90aeadfc
DC
16210 else
16211 process_die (child_die, cu);
134d01f1 16212
90aeadfc 16213 child_die = sibling_die (child_die);
134d01f1
DJ
16214 }
16215
fa4028e9
JB
16216 /* Do not consider external references. According to the DWARF standard,
16217 these DIEs are identified by the fact that they have no byte_size
16218 attribute, and a declaration attribute. */
16219 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16220 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16221 {
16222 struct symbol *sym = new_symbol (die, type, cu);
16223
16224 if (has_template_parameters)
16225 {
a776957c
TT
16226 struct symtab *symtab;
16227 if (sym != nullptr)
16228 symtab = symbol_symtab (sym);
16229 else if (cu->line_header != nullptr)
16230 {
16231 /* Any related symtab will do. */
16232 symtab
16233 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16234 }
16235 else
16236 {
16237 symtab = nullptr;
16238 complaint (_("could not find suitable "
16239 "symtab for template parameter"
16240 " - DIE at %s [in module %s]"),
16241 sect_offset_str (die->sect_off),
16242 objfile_name (objfile));
16243 }
16244
16245 if (symtab != nullptr)
16246 {
16247 /* Make sure that the symtab is set on the new symbols.
16248 Even though they don't appear in this symtab directly,
16249 other parts of gdb assume that symbols do, and this is
16250 reasonably true. */
16251 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16252 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16253 }
3e1d3d8c
TT
16254 }
16255 }
134d01f1
DJ
16256}
16257
55426c9d
JB
16258/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16259 update TYPE using some information only available in DIE's children. */
16260
16261static void
16262update_enumeration_type_from_children (struct die_info *die,
16263 struct type *type,
16264 struct dwarf2_cu *cu)
16265{
60f7655a 16266 struct die_info *child_die;
55426c9d
JB
16267 int unsigned_enum = 1;
16268 int flag_enum = 1;
16269 ULONGEST mask = 0;
55426c9d 16270
8268c778 16271 auto_obstack obstack;
55426c9d 16272
60f7655a
DE
16273 for (child_die = die->child;
16274 child_die != NULL && child_die->tag;
16275 child_die = sibling_die (child_die))
55426c9d
JB
16276 {
16277 struct attribute *attr;
16278 LONGEST value;
16279 const gdb_byte *bytes;
16280 struct dwarf2_locexpr_baton *baton;
16281 const char *name;
60f7655a 16282
55426c9d
JB
16283 if (child_die->tag != DW_TAG_enumerator)
16284 continue;
16285
16286 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16287 if (attr == NULL)
16288 continue;
16289
16290 name = dwarf2_name (child_die, cu);
16291 if (name == NULL)
16292 name = "<anonymous enumerator>";
16293
16294 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16295 &value, &bytes, &baton);
16296 if (value < 0)
16297 {
16298 unsigned_enum = 0;
16299 flag_enum = 0;
16300 }
16301 else if ((mask & value) != 0)
16302 flag_enum = 0;
16303 else
16304 mask |= value;
16305
16306 /* If we already know that the enum type is neither unsigned, nor
16307 a flag type, no need to look at the rest of the enumerates. */
16308 if (!unsigned_enum && !flag_enum)
16309 break;
55426c9d
JB
16310 }
16311
16312 if (unsigned_enum)
16313 TYPE_UNSIGNED (type) = 1;
16314 if (flag_enum)
16315 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16316}
16317
134d01f1
DJ
16318/* Given a DW_AT_enumeration_type die, set its type. We do not
16319 complete the type's fields yet, or create any symbols. */
c906108c 16320
f792889a 16321static struct type *
134d01f1 16322read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16323{
518817b3 16324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16325 struct type *type;
c906108c 16326 struct attribute *attr;
0114d602 16327 const char *name;
134d01f1 16328
348e048f
DE
16329 /* If the definition of this type lives in .debug_types, read that type.
16330 Don't follow DW_AT_specification though, that will take us back up
16331 the chain and we want to go down. */
45e58e77 16332 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16333 if (attr)
16334 {
ac9ec31b 16335 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16336
ac9ec31b 16337 /* The type's CU may not be the same as CU.
02142a6c 16338 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16339 return set_die_type (die, type, cu);
16340 }
16341
c906108c
SS
16342 type = alloc_type (objfile);
16343
16344 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16345 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16346 if (name != NULL)
e86ca25f 16347 TYPE_NAME (type) = name;
c906108c 16348
0626fc76
TT
16349 attr = dwarf2_attr (die, DW_AT_type, cu);
16350 if (attr != NULL)
16351 {
16352 struct type *underlying_type = die_type (die, cu);
16353
16354 TYPE_TARGET_TYPE (type) = underlying_type;
16355 }
16356
e142c38c 16357 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16358 if (attr)
16359 {
16360 TYPE_LENGTH (type) = DW_UNSND (attr);
16361 }
16362 else
16363 {
16364 TYPE_LENGTH (type) = 0;
16365 }
16366
2b4424c3
TT
16367 maybe_set_alignment (cu, die, type);
16368
137033e9
JB
16369 /* The enumeration DIE can be incomplete. In Ada, any type can be
16370 declared as private in the package spec, and then defined only
16371 inside the package body. Such types are known as Taft Amendment
16372 Types. When another package uses such a type, an incomplete DIE
16373 may be generated by the compiler. */
02eb380e 16374 if (die_is_declaration (die, cu))
876cecd0 16375 TYPE_STUB (type) = 1;
02eb380e 16376
0626fc76
TT
16377 /* Finish the creation of this type by using the enum's children.
16378 We must call this even when the underlying type has been provided
16379 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16380 update_enumeration_type_from_children (die, type, cu);
16381
0626fc76
TT
16382 /* If this type has an underlying type that is not a stub, then we
16383 may use its attributes. We always use the "unsigned" attribute
16384 in this situation, because ordinarily we guess whether the type
16385 is unsigned -- but the guess can be wrong and the underlying type
16386 can tell us the reality. However, we defer to a local size
16387 attribute if one exists, because this lets the compiler override
16388 the underlying type if needed. */
16389 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16390 {
16391 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16392 if (TYPE_LENGTH (type) == 0)
16393 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16394 if (TYPE_RAW_ALIGN (type) == 0
16395 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16396 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16397 }
16398
3d567982
TT
16399 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16400
f792889a 16401 return set_die_type (die, type, cu);
134d01f1
DJ
16402}
16403
16404/* Given a pointer to a die which begins an enumeration, process all
16405 the dies that define the members of the enumeration, and create the
16406 symbol for the enumeration type.
16407
16408 NOTE: We reverse the order of the element list. */
16409
16410static void
16411process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16412{
f792889a 16413 struct type *this_type;
134d01f1 16414
f792889a
DJ
16415 this_type = get_die_type (die, cu);
16416 if (this_type == NULL)
16417 this_type = read_enumeration_type (die, cu);
9dc481d3 16418
639d11d3 16419 if (die->child != NULL)
c906108c 16420 {
9dc481d3
DE
16421 struct die_info *child_die;
16422 struct symbol *sym;
16423 struct field *fields = NULL;
16424 int num_fields = 0;
15d034d0 16425 const char *name;
9dc481d3 16426
639d11d3 16427 child_die = die->child;
c906108c
SS
16428 while (child_die && child_die->tag)
16429 {
16430 if (child_die->tag != DW_TAG_enumerator)
16431 {
e7c27a73 16432 process_die (child_die, cu);
c906108c
SS
16433 }
16434 else
16435 {
39cbfefa
DJ
16436 name = dwarf2_name (child_die, cu);
16437 if (name)
c906108c 16438 {
f792889a 16439 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16440
16441 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16442 {
16443 fields = (struct field *)
16444 xrealloc (fields,
16445 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16446 * sizeof (struct field));
c906108c
SS
16447 }
16448
3567439c 16449 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16450 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16451 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16452 FIELD_BITSIZE (fields[num_fields]) = 0;
16453
16454 num_fields++;
16455 }
16456 }
16457
16458 child_die = sibling_die (child_die);
16459 }
16460
16461 if (num_fields)
16462 {
f792889a
DJ
16463 TYPE_NFIELDS (this_type) = num_fields;
16464 TYPE_FIELDS (this_type) = (struct field *)
16465 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16466 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16467 sizeof (struct field) * num_fields);
b8c9b27d 16468 xfree (fields);
c906108c 16469 }
c906108c 16470 }
134d01f1 16471
6c83ed52
TT
16472 /* If we are reading an enum from a .debug_types unit, and the enum
16473 is a declaration, and the enum is not the signatured type in the
16474 unit, then we do not want to add a symbol for it. Adding a
16475 symbol would in some cases obscure the true definition of the
16476 enum, giving users an incomplete type when the definition is
16477 actually available. Note that we do not want to do this for all
16478 enums which are just declarations, because C++0x allows forward
16479 enum declarations. */
3019eac3 16480 if (cu->per_cu->is_debug_types
6c83ed52
TT
16481 && die_is_declaration (die, cu))
16482 {
52dc124a 16483 struct signatured_type *sig_type;
6c83ed52 16484
c0f78cd4 16485 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16486 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16487 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16488 return;
16489 }
16490
f792889a 16491 new_symbol (die, this_type, cu);
c906108c
SS
16492}
16493
16494/* Extract all information from a DW_TAG_array_type DIE and put it in
16495 the DIE's type field. For now, this only handles one dimensional
16496 arrays. */
16497
f792889a 16498static struct type *
e7c27a73 16499read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16500{
518817b3 16501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16502 struct die_info *child_die;
7e314c57 16503 struct type *type;
c906108c 16504 struct type *element_type, *range_type, *index_type;
c906108c 16505 struct attribute *attr;
15d034d0 16506 const char *name;
a405673c 16507 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16508 unsigned int bit_stride = 0;
c906108c 16509
e7c27a73 16510 element_type = die_type (die, cu);
c906108c 16511
7e314c57
JK
16512 /* The die_type call above may have already set the type for this DIE. */
16513 type = get_die_type (die, cu);
16514 if (type)
16515 return type;
16516
dc53a7ad
JB
16517 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16518 if (attr != NULL)
a405673c
JB
16519 {
16520 int stride_ok;
9a49df9d
AB
16521 struct type *prop_type
16522 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16523
16524 byte_stride_prop
16525 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16526 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16527 prop_type);
a405673c
JB
16528 if (!stride_ok)
16529 {
b98664d3 16530 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16531 " - DIE at %s [in module %s]"),
16532 sect_offset_str (die->sect_off),
518817b3 16533 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16534 /* Ignore this attribute. We will likely not be able to print
16535 arrays of this type correctly, but there is little we can do
16536 to help if we cannot read the attribute's value. */
16537 byte_stride_prop = NULL;
16538 }
16539 }
dc53a7ad
JB
16540
16541 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16542 if (attr != NULL)
16543 bit_stride = DW_UNSND (attr);
16544
c906108c
SS
16545 /* Irix 6.2 native cc creates array types without children for
16546 arrays with unspecified length. */
639d11d3 16547 if (die->child == NULL)
c906108c 16548 {
46bf5051 16549 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16550 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16551 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16552 byte_stride_prop, bit_stride);
f792889a 16553 return set_die_type (die, type, cu);
c906108c
SS
16554 }
16555
791afaa2 16556 std::vector<struct type *> range_types;
639d11d3 16557 child_die = die->child;
c906108c
SS
16558 while (child_die && child_die->tag)
16559 {
16560 if (child_die->tag == DW_TAG_subrange_type)
16561 {
f792889a 16562 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16563
f792889a 16564 if (child_type != NULL)
a02abb62 16565 {
0963b4bd
MS
16566 /* The range type was succesfully read. Save it for the
16567 array type creation. */
791afaa2 16568 range_types.push_back (child_type);
a02abb62 16569 }
c906108c
SS
16570 }
16571 child_die = sibling_die (child_die);
16572 }
16573
16574 /* Dwarf2 dimensions are output from left to right, create the
16575 necessary array types in backwards order. */
7ca2d3a3 16576
c906108c 16577 type = element_type;
7ca2d3a3
DL
16578
16579 if (read_array_order (die, cu) == DW_ORD_col_major)
16580 {
16581 int i = 0;
9a619af0 16582
791afaa2 16583 while (i < range_types.size ())
dc53a7ad 16584 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16585 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16586 }
16587 else
16588 {
791afaa2 16589 size_t ndim = range_types.size ();
7ca2d3a3 16590 while (ndim-- > 0)
dc53a7ad 16591 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16592 byte_stride_prop, bit_stride);
7ca2d3a3 16593 }
c906108c 16594
f5f8a009
EZ
16595 /* Understand Dwarf2 support for vector types (like they occur on
16596 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16597 array type. This is not part of the Dwarf2/3 standard yet, but a
16598 custom vendor extension. The main difference between a regular
16599 array and the vector variant is that vectors are passed by value
16600 to functions. */
e142c38c 16601 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16602 if (attr)
ea37ba09 16603 make_vector_type (type);
f5f8a009 16604
dbc98a8b
KW
16605 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16606 implementation may choose to implement triple vectors using this
16607 attribute. */
16608 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16609 if (attr)
16610 {
16611 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16612 TYPE_LENGTH (type) = DW_UNSND (attr);
16613 else
b98664d3 16614 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16615 "than the total size of elements"));
dbc98a8b
KW
16616 }
16617
39cbfefa
DJ
16618 name = dwarf2_name (die, cu);
16619 if (name)
16620 TYPE_NAME (type) = name;
6e70227d 16621
2b4424c3
TT
16622 maybe_set_alignment (cu, die, type);
16623
0963b4bd 16624 /* Install the type in the die. */
7e314c57
JK
16625 set_die_type (die, type, cu);
16626
16627 /* set_die_type should be already done. */
b4ba55a1
JB
16628 set_descriptive_type (type, die, cu);
16629
7e314c57 16630 return type;
c906108c
SS
16631}
16632
7ca2d3a3 16633static enum dwarf_array_dim_ordering
6e70227d 16634read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16635{
16636 struct attribute *attr;
16637
16638 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16639
aead7601
SM
16640 if (attr)
16641 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16642
0963b4bd
MS
16643 /* GNU F77 is a special case, as at 08/2004 array type info is the
16644 opposite order to the dwarf2 specification, but data is still
16645 laid out as per normal fortran.
7ca2d3a3 16646
0963b4bd
MS
16647 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16648 version checking. */
7ca2d3a3 16649
905e0470
PM
16650 if (cu->language == language_fortran
16651 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16652 {
16653 return DW_ORD_row_major;
16654 }
16655
6e70227d 16656 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16657 {
16658 case array_column_major:
16659 return DW_ORD_col_major;
16660 case array_row_major:
16661 default:
16662 return DW_ORD_row_major;
16663 };
16664}
16665
72019c9c 16666/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16667 the DIE's type field. */
72019c9c 16668
f792889a 16669static struct type *
72019c9c
GM
16670read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16671{
7e314c57
JK
16672 struct type *domain_type, *set_type;
16673 struct attribute *attr;
f792889a 16674
7e314c57
JK
16675 domain_type = die_type (die, cu);
16676
16677 /* The die_type call above may have already set the type for this DIE. */
16678 set_type = get_die_type (die, cu);
16679 if (set_type)
16680 return set_type;
16681
16682 set_type = create_set_type (NULL, domain_type);
16683
16684 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16685 if (attr)
16686 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16687
2b4424c3
TT
16688 maybe_set_alignment (cu, die, set_type);
16689
f792889a 16690 return set_die_type (die, set_type, cu);
72019c9c 16691}
7ca2d3a3 16692
0971de02
TT
16693/* A helper for read_common_block that creates a locexpr baton.
16694 SYM is the symbol which we are marking as computed.
16695 COMMON_DIE is the DIE for the common block.
16696 COMMON_LOC is the location expression attribute for the common
16697 block itself.
16698 MEMBER_LOC is the location expression attribute for the particular
16699 member of the common block that we are processing.
16700 CU is the CU from which the above come. */
16701
16702static void
16703mark_common_block_symbol_computed (struct symbol *sym,
16704 struct die_info *common_die,
16705 struct attribute *common_loc,
16706 struct attribute *member_loc,
16707 struct dwarf2_cu *cu)
16708{
518817b3
SM
16709 struct dwarf2_per_objfile *dwarf2_per_objfile
16710 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16711 struct objfile *objfile = dwarf2_per_objfile->objfile;
16712 struct dwarf2_locexpr_baton *baton;
16713 gdb_byte *ptr;
16714 unsigned int cu_off;
16715 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16716 LONGEST offset = 0;
16717
16718 gdb_assert (common_loc && member_loc);
16719 gdb_assert (attr_form_is_block (common_loc));
16720 gdb_assert (attr_form_is_block (member_loc)
16721 || attr_form_is_constant (member_loc));
16722
8d749320 16723 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16724 baton->per_cu = cu->per_cu;
16725 gdb_assert (baton->per_cu);
16726
16727 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16728
16729 if (attr_form_is_constant (member_loc))
16730 {
16731 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16732 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16733 }
16734 else
16735 baton->size += DW_BLOCK (member_loc)->size;
16736
224c3ddb 16737 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16738 baton->data = ptr;
16739
16740 *ptr++ = DW_OP_call4;
9c541725 16741 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16742 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16743 ptr += 4;
16744
16745 if (attr_form_is_constant (member_loc))
16746 {
16747 *ptr++ = DW_OP_addr;
16748 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16749 ptr += cu->header.addr_size;
16750 }
16751 else
16752 {
16753 /* We have to copy the data here, because DW_OP_call4 will only
16754 use a DW_AT_location attribute. */
16755 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16756 ptr += DW_BLOCK (member_loc)->size;
16757 }
16758
16759 *ptr++ = DW_OP_plus;
16760 gdb_assert (ptr - baton->data == baton->size);
16761
0971de02 16762 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16763 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16764}
16765
4357ac6c
TT
16766/* Create appropriate locally-scoped variables for all the
16767 DW_TAG_common_block entries. Also create a struct common_block
16768 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16769 is used to sepate the common blocks name namespace from regular
16770 variable names. */
c906108c
SS
16771
16772static void
e7c27a73 16773read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16774{
0971de02
TT
16775 struct attribute *attr;
16776
16777 attr = dwarf2_attr (die, DW_AT_location, cu);
16778 if (attr)
16779 {
16780 /* Support the .debug_loc offsets. */
16781 if (attr_form_is_block (attr))
16782 {
16783 /* Ok. */
16784 }
16785 else if (attr_form_is_section_offset (attr))
16786 {
16787 dwarf2_complex_location_expr_complaint ();
16788 attr = NULL;
16789 }
16790 else
16791 {
16792 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16793 "common block member");
16794 attr = NULL;
16795 }
16796 }
16797
639d11d3 16798 if (die->child != NULL)
c906108c 16799 {
518817b3 16800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16801 struct die_info *child_die;
16802 size_t n_entries = 0, size;
16803 struct common_block *common_block;
16804 struct symbol *sym;
74ac6d43 16805
4357ac6c
TT
16806 for (child_die = die->child;
16807 child_die && child_die->tag;
16808 child_die = sibling_die (child_die))
16809 ++n_entries;
16810
16811 size = (sizeof (struct common_block)
16812 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16813 common_block
16814 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16815 size);
4357ac6c
TT
16816 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16817 common_block->n_entries = 0;
16818
16819 for (child_die = die->child;
16820 child_die && child_die->tag;
16821 child_die = sibling_die (child_die))
16822 {
16823 /* Create the symbol in the DW_TAG_common_block block in the current
16824 symbol scope. */
e7c27a73 16825 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16826 if (sym != NULL)
16827 {
16828 struct attribute *member_loc;
16829
16830 common_block->contents[common_block->n_entries++] = sym;
16831
16832 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16833 cu);
16834 if (member_loc)
16835 {
16836 /* GDB has handled this for a long time, but it is
16837 not specified by DWARF. It seems to have been
16838 emitted by gfortran at least as recently as:
16839 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16840 complaint (_("Variable in common block has "
0971de02 16841 "DW_AT_data_member_location "
9d8780f0
SM
16842 "- DIE at %s [in module %s]"),
16843 sect_offset_str (child_die->sect_off),
518817b3 16844 objfile_name (objfile));
0971de02
TT
16845
16846 if (attr_form_is_section_offset (member_loc))
16847 dwarf2_complex_location_expr_complaint ();
16848 else if (attr_form_is_constant (member_loc)
16849 || attr_form_is_block (member_loc))
16850 {
16851 if (attr)
16852 mark_common_block_symbol_computed (sym, die, attr,
16853 member_loc, cu);
16854 }
16855 else
16856 dwarf2_complex_location_expr_complaint ();
16857 }
16858 }
c906108c 16859 }
4357ac6c
TT
16860
16861 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16862 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16863 }
16864}
16865
0114d602 16866/* Create a type for a C++ namespace. */
d9fa45fe 16867
0114d602
DJ
16868static struct type *
16869read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16870{
518817b3 16871 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16872 const char *previous_prefix, *name;
9219021c 16873 int is_anonymous;
0114d602
DJ
16874 struct type *type;
16875
16876 /* For extensions, reuse the type of the original namespace. */
16877 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16878 {
16879 struct die_info *ext_die;
16880 struct dwarf2_cu *ext_cu = cu;
9a619af0 16881
0114d602
DJ
16882 ext_die = dwarf2_extension (die, &ext_cu);
16883 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16884
16885 /* EXT_CU may not be the same as CU.
02142a6c 16886 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16887 return set_die_type (die, type, cu);
16888 }
9219021c 16889
e142c38c 16890 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16891
16892 /* Now build the name of the current namespace. */
16893
0114d602
DJ
16894 previous_prefix = determine_prefix (die, cu);
16895 if (previous_prefix[0] != '\0')
16896 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16897 previous_prefix, name, 0, cu);
0114d602
DJ
16898
16899 /* Create the type. */
19f392bc 16900 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16901
60531b24 16902 return set_die_type (die, type, cu);
0114d602
DJ
16903}
16904
22cee43f 16905/* Read a namespace scope. */
0114d602
DJ
16906
16907static void
16908read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16909{
518817b3 16910 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16911 int is_anonymous;
9219021c 16912
5c4e30ca
DC
16913 /* Add a symbol associated to this if we haven't seen the namespace
16914 before. Also, add a using directive if it's an anonymous
16915 namespace. */
9219021c 16916
f2f0e013 16917 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16918 {
16919 struct type *type;
16920
0114d602 16921 type = read_type_die (die, cu);
e7c27a73 16922 new_symbol (die, type, cu);
5c4e30ca 16923
e8e80198 16924 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16925 if (is_anonymous)
0114d602
DJ
16926 {
16927 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16928
eb1e02fd 16929 std::vector<const char *> excludes;
804d2729 16930 add_using_directive (using_directives (cu),
22cee43f 16931 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16932 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16933 }
5c4e30ca 16934 }
9219021c 16935
639d11d3 16936 if (die->child != NULL)
d9fa45fe 16937 {
639d11d3 16938 struct die_info *child_die = die->child;
6e70227d 16939
d9fa45fe
DC
16940 while (child_die && child_die->tag)
16941 {
e7c27a73 16942 process_die (child_die, cu);
d9fa45fe
DC
16943 child_die = sibling_die (child_die);
16944 }
16945 }
38d518c9
EZ
16946}
16947
f55ee35c
JK
16948/* Read a Fortran module as type. This DIE can be only a declaration used for
16949 imported module. Still we need that type as local Fortran "use ... only"
16950 declaration imports depend on the created type in determine_prefix. */
16951
16952static struct type *
16953read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16954{
518817b3 16955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16956 const char *module_name;
f55ee35c
JK
16957 struct type *type;
16958
16959 module_name = dwarf2_name (die, cu);
19f392bc 16960 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16961
f55ee35c
JK
16962 return set_die_type (die, type, cu);
16963}
16964
5d7cb8df
JK
16965/* Read a Fortran module. */
16966
16967static void
16968read_module (struct die_info *die, struct dwarf2_cu *cu)
16969{
16970 struct die_info *child_die = die->child;
530e8392
KB
16971 struct type *type;
16972
16973 type = read_type_die (die, cu);
16974 new_symbol (die, type, cu);
5d7cb8df 16975
5d7cb8df
JK
16976 while (child_die && child_die->tag)
16977 {
16978 process_die (child_die, cu);
16979 child_die = sibling_die (child_die);
16980 }
16981}
16982
38d518c9
EZ
16983/* Return the name of the namespace represented by DIE. Set
16984 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16985 namespace. */
16986
16987static const char *
e142c38c 16988namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16989{
16990 struct die_info *current_die;
16991 const char *name = NULL;
16992
16993 /* Loop through the extensions until we find a name. */
16994
16995 for (current_die = die;
16996 current_die != NULL;
f2f0e013 16997 current_die = dwarf2_extension (die, &cu))
38d518c9 16998 {
96553a0c
DE
16999 /* We don't use dwarf2_name here so that we can detect the absence
17000 of a name -> anonymous namespace. */
7d45c7c3 17001 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17002
38d518c9
EZ
17003 if (name != NULL)
17004 break;
17005 }
17006
17007 /* Is it an anonymous namespace? */
17008
17009 *is_anonymous = (name == NULL);
17010 if (*is_anonymous)
2b1dbab0 17011 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17012
17013 return name;
d9fa45fe
DC
17014}
17015
c906108c
SS
17016/* Extract all information from a DW_TAG_pointer_type DIE and add to
17017 the user defined type vector. */
17018
f792889a 17019static struct type *
e7c27a73 17020read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17021{
518817b3
SM
17022 struct gdbarch *gdbarch
17023 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17024 struct comp_unit_head *cu_header = &cu->header;
c906108c 17025 struct type *type;
8b2dbe47
KB
17026 struct attribute *attr_byte_size;
17027 struct attribute *attr_address_class;
17028 int byte_size, addr_class;
7e314c57
JK
17029 struct type *target_type;
17030
17031 target_type = die_type (die, cu);
c906108c 17032
7e314c57
JK
17033 /* The die_type call above may have already set the type for this DIE. */
17034 type = get_die_type (die, cu);
17035 if (type)
17036 return type;
17037
17038 type = lookup_pointer_type (target_type);
8b2dbe47 17039
e142c38c 17040 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17041 if (attr_byte_size)
17042 byte_size = DW_UNSND (attr_byte_size);
c906108c 17043 else
8b2dbe47
KB
17044 byte_size = cu_header->addr_size;
17045
e142c38c 17046 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17047 if (attr_address_class)
17048 addr_class = DW_UNSND (attr_address_class);
17049 else
17050 addr_class = DW_ADDR_none;
17051
2b4424c3
TT
17052 ULONGEST alignment = get_alignment (cu, die);
17053
17054 /* If the pointer size, alignment, or address class is different
17055 than the default, create a type variant marked as such and set
17056 the length accordingly. */
17057 if (TYPE_LENGTH (type) != byte_size
17058 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17059 && alignment != TYPE_RAW_ALIGN (type))
17060 || addr_class != DW_ADDR_none)
c906108c 17061 {
5e2b427d 17062 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17063 {
17064 int type_flags;
17065
849957d9 17066 type_flags = gdbarch_address_class_type_flags
5e2b427d 17067 (gdbarch, byte_size, addr_class);
876cecd0
TT
17068 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17069 == 0);
8b2dbe47
KB
17070 type = make_type_with_address_space (type, type_flags);
17071 }
17072 else if (TYPE_LENGTH (type) != byte_size)
17073 {
b98664d3 17074 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17075 }
2b4424c3
TT
17076 else if (TYPE_RAW_ALIGN (type) != alignment)
17077 {
b98664d3 17078 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17079 " - DIE at %s [in module %s]"),
17080 sect_offset_str (die->sect_off),
17081 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17082 }
6e70227d 17083 else
9a619af0
MS
17084 {
17085 /* Should we also complain about unhandled address classes? */
17086 }
c906108c 17087 }
8b2dbe47
KB
17088
17089 TYPE_LENGTH (type) = byte_size;
2b4424c3 17090 set_type_align (type, alignment);
f792889a 17091 return set_die_type (die, type, cu);
c906108c
SS
17092}
17093
17094/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17095 the user defined type vector. */
17096
f792889a 17097static struct type *
e7c27a73 17098read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17099{
17100 struct type *type;
17101 struct type *to_type;
17102 struct type *domain;
17103
e7c27a73
DJ
17104 to_type = die_type (die, cu);
17105 domain = die_containing_type (die, cu);
0d5de010 17106
7e314c57
JK
17107 /* The calls above may have already set the type for this DIE. */
17108 type = get_die_type (die, cu);
17109 if (type)
17110 return type;
17111
0d5de010
DJ
17112 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17113 type = lookup_methodptr_type (to_type);
7078baeb
TT
17114 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17115 {
518817b3
SM
17116 struct type *new_type
17117 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17118
17119 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17120 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17121 TYPE_VARARGS (to_type));
17122 type = lookup_methodptr_type (new_type);
17123 }
0d5de010
DJ
17124 else
17125 type = lookup_memberptr_type (to_type, domain);
c906108c 17126
f792889a 17127 return set_die_type (die, type, cu);
c906108c
SS
17128}
17129
4297a3f0 17130/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17131 the user defined type vector. */
17132
f792889a 17133static struct type *
4297a3f0
AV
17134read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17135 enum type_code refcode)
c906108c 17136{
e7c27a73 17137 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17138 struct type *type, *target_type;
c906108c
SS
17139 struct attribute *attr;
17140
4297a3f0
AV
17141 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17142
7e314c57
JK
17143 target_type = die_type (die, cu);
17144
17145 /* The die_type call above may have already set the type for this DIE. */
17146 type = get_die_type (die, cu);
17147 if (type)
17148 return type;
17149
4297a3f0 17150 type = lookup_reference_type (target_type, refcode);
e142c38c 17151 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17152 if (attr)
17153 {
17154 TYPE_LENGTH (type) = DW_UNSND (attr);
17155 }
17156 else
17157 {
107d2387 17158 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17159 }
2b4424c3 17160 maybe_set_alignment (cu, die, type);
f792889a 17161 return set_die_type (die, type, cu);
c906108c
SS
17162}
17163
cf363f18
MW
17164/* Add the given cv-qualifiers to the element type of the array. GCC
17165 outputs DWARF type qualifiers that apply to an array, not the
17166 element type. But GDB relies on the array element type to carry
17167 the cv-qualifiers. This mimics section 6.7.3 of the C99
17168 specification. */
17169
17170static struct type *
17171add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17172 struct type *base_type, int cnst, int voltl)
17173{
17174 struct type *el_type, *inner_array;
17175
17176 base_type = copy_type (base_type);
17177 inner_array = base_type;
17178
17179 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17180 {
17181 TYPE_TARGET_TYPE (inner_array) =
17182 copy_type (TYPE_TARGET_TYPE (inner_array));
17183 inner_array = TYPE_TARGET_TYPE (inner_array);
17184 }
17185
17186 el_type = TYPE_TARGET_TYPE (inner_array);
17187 cnst |= TYPE_CONST (el_type);
17188 voltl |= TYPE_VOLATILE (el_type);
17189 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17190
17191 return set_die_type (die, base_type, cu);
17192}
17193
f792889a 17194static struct type *
e7c27a73 17195read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17196{
f792889a 17197 struct type *base_type, *cv_type;
c906108c 17198
e7c27a73 17199 base_type = die_type (die, cu);
7e314c57
JK
17200
17201 /* The die_type call above may have already set the type for this DIE. */
17202 cv_type = get_die_type (die, cu);
17203 if (cv_type)
17204 return cv_type;
17205
2f608a3a
KW
17206 /* In case the const qualifier is applied to an array type, the element type
17207 is so qualified, not the array type (section 6.7.3 of C99). */
17208 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17209 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17210
f792889a
DJ
17211 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17212 return set_die_type (die, cv_type, cu);
c906108c
SS
17213}
17214
f792889a 17215static struct type *
e7c27a73 17216read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17217{
f792889a 17218 struct type *base_type, *cv_type;
c906108c 17219
e7c27a73 17220 base_type = die_type (die, cu);
7e314c57
JK
17221
17222 /* The die_type call above may have already set the type for this DIE. */
17223 cv_type = get_die_type (die, cu);
17224 if (cv_type)
17225 return cv_type;
17226
cf363f18
MW
17227 /* In case the volatile qualifier is applied to an array type, the
17228 element type is so qualified, not the array type (section 6.7.3
17229 of C99). */
17230 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17231 return add_array_cv_type (die, cu, base_type, 0, 1);
17232
f792889a
DJ
17233 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17234 return set_die_type (die, cv_type, cu);
c906108c
SS
17235}
17236
06d66ee9
TT
17237/* Handle DW_TAG_restrict_type. */
17238
17239static struct type *
17240read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17241{
17242 struct type *base_type, *cv_type;
17243
17244 base_type = die_type (die, cu);
17245
17246 /* The die_type call above may have already set the type for this DIE. */
17247 cv_type = get_die_type (die, cu);
17248 if (cv_type)
17249 return cv_type;
17250
17251 cv_type = make_restrict_type (base_type);
17252 return set_die_type (die, cv_type, cu);
17253}
17254
a2c2acaf
MW
17255/* Handle DW_TAG_atomic_type. */
17256
17257static struct type *
17258read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17259{
17260 struct type *base_type, *cv_type;
17261
17262 base_type = die_type (die, cu);
17263
17264 /* The die_type call above may have already set the type for this DIE. */
17265 cv_type = get_die_type (die, cu);
17266 if (cv_type)
17267 return cv_type;
17268
17269 cv_type = make_atomic_type (base_type);
17270 return set_die_type (die, cv_type, cu);
17271}
17272
c906108c
SS
17273/* Extract all information from a DW_TAG_string_type DIE and add to
17274 the user defined type vector. It isn't really a user defined type,
17275 but it behaves like one, with other DIE's using an AT_user_def_type
17276 attribute to reference it. */
17277
f792889a 17278static struct type *
e7c27a73 17279read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17280{
518817b3 17281 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17282 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17283 struct type *type, *range_type, *index_type, *char_type;
17284 struct attribute *attr;
17285 unsigned int length;
17286
e142c38c 17287 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17288 if (attr)
17289 {
17290 length = DW_UNSND (attr);
17291 }
17292 else
17293 {
0963b4bd 17294 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17295 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17296 if (attr)
17297 {
17298 length = DW_UNSND (attr);
17299 }
17300 else
17301 {
17302 length = 1;
17303 }
c906108c 17304 }
6ccb9162 17305
46bf5051 17306 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17307 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17308 char_type = language_string_char_type (cu->language_defn, gdbarch);
17309 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17310
f792889a 17311 return set_die_type (die, type, cu);
c906108c
SS
17312}
17313
4d804846
JB
17314/* Assuming that DIE corresponds to a function, returns nonzero
17315 if the function is prototyped. */
17316
17317static int
17318prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17319{
17320 struct attribute *attr;
17321
17322 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17323 if (attr && (DW_UNSND (attr) != 0))
17324 return 1;
17325
17326 /* The DWARF standard implies that the DW_AT_prototyped attribute
17327 is only meaninful for C, but the concept also extends to other
17328 languages that allow unprototyped functions (Eg: Objective C).
17329 For all other languages, assume that functions are always
17330 prototyped. */
17331 if (cu->language != language_c
17332 && cu->language != language_objc
17333 && cu->language != language_opencl)
17334 return 1;
17335
17336 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17337 prototyped and unprototyped functions; default to prototyped,
17338 since that is more common in modern code (and RealView warns
17339 about unprototyped functions). */
17340 if (producer_is_realview (cu->producer))
17341 return 1;
17342
17343 return 0;
17344}
17345
c906108c
SS
17346/* Handle DIES due to C code like:
17347
17348 struct foo
c5aa993b
JM
17349 {
17350 int (*funcp)(int a, long l);
17351 int b;
17352 };
c906108c 17353
0963b4bd 17354 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17355
f792889a 17356static struct type *
e7c27a73 17357read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17358{
518817b3 17359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17360 struct type *type; /* Type that this function returns. */
17361 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17362 struct attribute *attr;
17363
e7c27a73 17364 type = die_type (die, cu);
7e314c57
JK
17365
17366 /* The die_type call above may have already set the type for this DIE. */
17367 ftype = get_die_type (die, cu);
17368 if (ftype)
17369 return ftype;
17370
0c8b41f1 17371 ftype = lookup_function_type (type);
c906108c 17372
4d804846 17373 if (prototyped_function_p (die, cu))
a6c727b2 17374 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17375
c055b101
CV
17376 /* Store the calling convention in the type if it's available in
17377 the subroutine die. Otherwise set the calling convention to
17378 the default value DW_CC_normal. */
17379 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17380 if (attr)
17381 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17382 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17383 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17384 else
17385 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17386
743649fd
MW
17387 /* Record whether the function returns normally to its caller or not
17388 if the DWARF producer set that information. */
17389 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17390 if (attr && (DW_UNSND (attr) != 0))
17391 TYPE_NO_RETURN (ftype) = 1;
17392
76c10ea2
GM
17393 /* We need to add the subroutine type to the die immediately so
17394 we don't infinitely recurse when dealing with parameters
0963b4bd 17395 declared as the same subroutine type. */
76c10ea2 17396 set_die_type (die, ftype, cu);
6e70227d 17397
639d11d3 17398 if (die->child != NULL)
c906108c 17399 {
bb5ed363 17400 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17401 struct die_info *child_die;
8072405b 17402 int nparams, iparams;
c906108c
SS
17403
17404 /* Count the number of parameters.
17405 FIXME: GDB currently ignores vararg functions, but knows about
17406 vararg member functions. */
8072405b 17407 nparams = 0;
639d11d3 17408 child_die = die->child;
c906108c
SS
17409 while (child_die && child_die->tag)
17410 {
17411 if (child_die->tag == DW_TAG_formal_parameter)
17412 nparams++;
17413 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17414 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17415 child_die = sibling_die (child_die);
17416 }
17417
17418 /* Allocate storage for parameters and fill them in. */
17419 TYPE_NFIELDS (ftype) = nparams;
17420 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17421 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17422
8072405b
JK
17423 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17424 even if we error out during the parameters reading below. */
17425 for (iparams = 0; iparams < nparams; iparams++)
17426 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17427
17428 iparams = 0;
639d11d3 17429 child_die = die->child;
c906108c
SS
17430 while (child_die && child_die->tag)
17431 {
17432 if (child_die->tag == DW_TAG_formal_parameter)
17433 {
3ce3b1ba
PA
17434 struct type *arg_type;
17435
17436 /* DWARF version 2 has no clean way to discern C++
17437 static and non-static member functions. G++ helps
17438 GDB by marking the first parameter for non-static
17439 member functions (which is the this pointer) as
17440 artificial. We pass this information to
17441 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17442
17443 DWARF version 3 added DW_AT_object_pointer, which GCC
17444 4.5 does not yet generate. */
e142c38c 17445 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17446 if (attr)
17447 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17448 else
9c37b5ae 17449 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17450 arg_type = die_type (child_die, cu);
17451
17452 /* RealView does not mark THIS as const, which the testsuite
17453 expects. GCC marks THIS as const in method definitions,
17454 but not in the class specifications (GCC PR 43053). */
17455 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17456 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17457 {
17458 int is_this = 0;
17459 struct dwarf2_cu *arg_cu = cu;
17460 const char *name = dwarf2_name (child_die, cu);
17461
17462 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17463 if (attr)
17464 {
17465 /* If the compiler emits this, use it. */
17466 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17467 is_this = 1;
17468 }
17469 else if (name && strcmp (name, "this") == 0)
17470 /* Function definitions will have the argument names. */
17471 is_this = 1;
17472 else if (name == NULL && iparams == 0)
17473 /* Declarations may not have the names, so like
17474 elsewhere in GDB, assume an artificial first
17475 argument is "this". */
17476 is_this = 1;
17477
17478 if (is_this)
17479 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17480 arg_type, 0);
17481 }
17482
17483 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17484 iparams++;
17485 }
17486 child_die = sibling_die (child_die);
17487 }
17488 }
17489
76c10ea2 17490 return ftype;
c906108c
SS
17491}
17492
f792889a 17493static struct type *
e7c27a73 17494read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17495{
518817b3 17496 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17497 const char *name = NULL;
3c8e0968 17498 struct type *this_type, *target_type;
c906108c 17499
94af9270 17500 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17501 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17502 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17503 set_die_type (die, this_type, cu);
3c8e0968
DE
17504 target_type = die_type (die, cu);
17505 if (target_type != this_type)
17506 TYPE_TARGET_TYPE (this_type) = target_type;
17507 else
17508 {
17509 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17510 spec and cause infinite loops in GDB. */
b98664d3 17511 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17512 "- DIE at %s [in module %s]"),
17513 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17514 TYPE_TARGET_TYPE (this_type) = NULL;
17515 }
f792889a 17516 return this_type;
c906108c
SS
17517}
17518
9b790ce7
UW
17519/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17520 (which may be different from NAME) to the architecture back-end to allow
17521 it to guess the correct format if necessary. */
17522
17523static struct type *
17524dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17525 const char *name_hint)
17526{
17527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17528 const struct floatformat **format;
17529 struct type *type;
17530
17531 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17532 if (format)
17533 type = init_float_type (objfile, bits, name, format);
17534 else
77b7c781 17535 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17536
17537 return type;
17538}
17539
eb77c9df
AB
17540/* Allocate an integer type of size BITS and name NAME. */
17541
17542static struct type *
17543dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17544 int bits, int unsigned_p, const char *name)
17545{
17546 struct type *type;
17547
17548 /* Versions of Intel's C Compiler generate an integer type called "void"
17549 instead of using DW_TAG_unspecified_type. This has been seen on
17550 at least versions 14, 17, and 18. */
35ee2dc2
AB
17551 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17552 && strcmp (name, "void") == 0)
eb77c9df
AB
17553 type = objfile_type (objfile)->builtin_void;
17554 else
17555 type = init_integer_type (objfile, bits, unsigned_p, name);
17556
17557 return type;
17558}
17559
8bdc1658
AB
17560/* Initialise and return a floating point type of size BITS suitable for
17561 use as a component of a complex number. The NAME_HINT is passed through
17562 when initialising the floating point type and is the name of the complex
17563 type.
17564
17565 As DWARF doesn't currently provide an explicit name for the components
17566 of a complex number, but it can be helpful to have these components
17567 named, we try to select a suitable name based on the size of the
17568 component. */
17569static struct type *
17570dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17571 struct objfile *objfile,
17572 int bits, const char *name_hint)
17573{
17574 gdbarch *gdbarch = get_objfile_arch (objfile);
17575 struct type *tt = nullptr;
17576
35add35e
AB
17577 /* Try to find a suitable floating point builtin type of size BITS.
17578 We're going to use the name of this type as the name for the complex
17579 target type that we are about to create. */
1db455a7 17580 switch (cu->language)
8bdc1658 17581 {
1db455a7
AB
17582 case language_fortran:
17583 switch (bits)
17584 {
17585 case 32:
17586 tt = builtin_f_type (gdbarch)->builtin_real;
17587 break;
17588 case 64:
17589 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17590 break;
17591 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17592 case 128:
17593 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17594 break;
17595 }
8bdc1658 17596 break;
1db455a7
AB
17597 default:
17598 switch (bits)
17599 {
17600 case 32:
17601 tt = builtin_type (gdbarch)->builtin_float;
17602 break;
17603 case 64:
17604 tt = builtin_type (gdbarch)->builtin_double;
17605 break;
17606 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17607 case 128:
17608 tt = builtin_type (gdbarch)->builtin_long_double;
17609 break;
17610 }
8bdc1658
AB
17611 break;
17612 }
17613
35add35e
AB
17614 /* If the type we found doesn't match the size we were looking for, then
17615 pretend we didn't find a type at all, the complex target type we
17616 create will then be nameless. */
a12e5744 17617 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17618 tt = nullptr;
17619
8bdc1658
AB
17620 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17621 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17622}
17623
c906108c
SS
17624/* Find a representation of a given base type and install
17625 it in the TYPE field of the die. */
17626
f792889a 17627static struct type *
e7c27a73 17628read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17629{
518817b3 17630 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17631 struct type *type;
17632 struct attribute *attr;
19f392bc 17633 int encoding = 0, bits = 0;
15d034d0 17634 const char *name;
c906108c 17635
e142c38c 17636 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17637 if (attr)
17638 {
17639 encoding = DW_UNSND (attr);
17640 }
e142c38c 17641 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17642 if (attr)
17643 {
19f392bc 17644 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17645 }
39cbfefa 17646 name = dwarf2_name (die, cu);
6ccb9162 17647 if (!name)
c906108c 17648 {
b98664d3 17649 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17650 }
6ccb9162
UW
17651
17652 switch (encoding)
c906108c 17653 {
6ccb9162
UW
17654 case DW_ATE_address:
17655 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17656 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17657 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17658 break;
17659 case DW_ATE_boolean:
19f392bc 17660 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17661 break;
17662 case DW_ATE_complex_float:
8bdc1658 17663 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17664 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17665 break;
17666 case DW_ATE_decimal_float:
19f392bc 17667 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17668 break;
17669 case DW_ATE_float:
9b790ce7 17670 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17671 break;
17672 case DW_ATE_signed:
eb77c9df 17673 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17674 break;
17675 case DW_ATE_unsigned:
3b2b8fea
TT
17676 if (cu->language == language_fortran
17677 && name
61012eef 17678 && startswith (name, "character("))
19f392bc
UW
17679 type = init_character_type (objfile, bits, 1, name);
17680 else
eb77c9df 17681 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17682 break;
17683 case DW_ATE_signed_char:
6e70227d 17684 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17685 || cu->language == language_pascal
17686 || cu->language == language_fortran)
19f392bc
UW
17687 type = init_character_type (objfile, bits, 0, name);
17688 else
eb77c9df 17689 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17690 break;
17691 case DW_ATE_unsigned_char:
868a0084 17692 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17693 || cu->language == language_pascal
c44af4eb
TT
17694 || cu->language == language_fortran
17695 || cu->language == language_rust)
19f392bc
UW
17696 type = init_character_type (objfile, bits, 1, name);
17697 else
eb77c9df 17698 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17699 break;
75079b2b 17700 case DW_ATE_UTF:
53e710ac
PA
17701 {
17702 gdbarch *arch = get_objfile_arch (objfile);
17703
17704 if (bits == 16)
17705 type = builtin_type (arch)->builtin_char16;
17706 else if (bits == 32)
17707 type = builtin_type (arch)->builtin_char32;
17708 else
17709 {
b98664d3 17710 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17711 bits);
eb77c9df 17712 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17713 }
17714 return set_die_type (die, type, cu);
17715 }
75079b2b
TT
17716 break;
17717
6ccb9162 17718 default:
b98664d3 17719 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17720 dwarf_type_encoding_name (encoding));
77b7c781 17721 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17722 break;
c906108c 17723 }
6ccb9162 17724
0114d602 17725 if (name && strcmp (name, "char") == 0)
876cecd0 17726 TYPE_NOSIGN (type) = 1;
0114d602 17727
2b4424c3
TT
17728 maybe_set_alignment (cu, die, type);
17729
f792889a 17730 return set_die_type (die, type, cu);
c906108c
SS
17731}
17732
80180f79
SA
17733/* Parse dwarf attribute if it's a block, reference or constant and put the
17734 resulting value of the attribute into struct bound_prop.
17735 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17736
17737static int
17738attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17739 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17740 struct type *default_type)
80180f79
SA
17741{
17742 struct dwarf2_property_baton *baton;
518817b3
SM
17743 struct obstack *obstack
17744 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17745
9a49df9d
AB
17746 gdb_assert (default_type != NULL);
17747
80180f79
SA
17748 if (attr == NULL || prop == NULL)
17749 return 0;
17750
17751 if (attr_form_is_block (attr))
17752 {
8d749320 17753 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17754 baton->property_type = default_type;
80180f79
SA
17755 baton->locexpr.per_cu = cu->per_cu;
17756 baton->locexpr.size = DW_BLOCK (attr)->size;
17757 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17758 baton->locexpr.is_reference = false;
80180f79
SA
17759 prop->data.baton = baton;
17760 prop->kind = PROP_LOCEXPR;
17761 gdb_assert (prop->data.baton != NULL);
17762 }
17763 else if (attr_form_is_ref (attr))
17764 {
17765 struct dwarf2_cu *target_cu = cu;
17766 struct die_info *target_die;
17767 struct attribute *target_attr;
17768
17769 target_die = follow_die_ref (die, attr, &target_cu);
17770 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17771 if (target_attr == NULL)
17772 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17773 target_cu);
80180f79
SA
17774 if (target_attr == NULL)
17775 return 0;
17776
df25ebbd 17777 switch (target_attr->name)
80180f79 17778 {
df25ebbd
JB
17779 case DW_AT_location:
17780 if (attr_form_is_section_offset (target_attr))
17781 {
8d749320 17782 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17783 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17784 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17785 prop->data.baton = baton;
17786 prop->kind = PROP_LOCLIST;
17787 gdb_assert (prop->data.baton != NULL);
17788 }
17789 else if (attr_form_is_block (target_attr))
17790 {
8d749320 17791 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17792 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17793 baton->locexpr.per_cu = cu->per_cu;
17794 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17795 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17796 baton->locexpr.is_reference = true;
df25ebbd
JB
17797 prop->data.baton = baton;
17798 prop->kind = PROP_LOCEXPR;
17799 gdb_assert (prop->data.baton != NULL);
17800 }
17801 else
17802 {
17803 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17804 "dynamic property");
17805 return 0;
17806 }
17807 break;
17808 case DW_AT_data_member_location:
17809 {
17810 LONGEST offset;
17811
17812 if (!handle_data_member_location (target_die, target_cu,
17813 &offset))
17814 return 0;
17815
8d749320 17816 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17817 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17818 target_cu);
df25ebbd
JB
17819 baton->offset_info.offset = offset;
17820 baton->offset_info.type = die_type (target_die, target_cu);
17821 prop->data.baton = baton;
17822 prop->kind = PROP_ADDR_OFFSET;
17823 break;
17824 }
80180f79
SA
17825 }
17826 }
17827 else if (attr_form_is_constant (attr))
17828 {
17829 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17830 prop->kind = PROP_CONST;
17831 }
17832 else
17833 {
17834 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17835 dwarf2_name (die, cu));
17836 return 0;
17837 }
17838
17839 return 1;
17840}
17841
9a49df9d
AB
17842/* Find an integer type the same size as the address size given in the
17843 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17844 is unsigned or not. */
17845
17846static struct type *
17847dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17848 bool unsigned_p)
17849{
17850 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17851 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17852 struct type *int_type;
17853
17854 /* Helper macro to examine the various builtin types. */
17855#define TRY_TYPE(F) \
17856 int_type = (unsigned_p \
17857 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17858 : objfile_type (objfile)->builtin_ ## F); \
17859 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17860 return int_type
17861
17862 TRY_TYPE (char);
17863 TRY_TYPE (short);
17864 TRY_TYPE (int);
17865 TRY_TYPE (long);
17866 TRY_TYPE (long_long);
17867
17868#undef TRY_TYPE
17869
17870 gdb_assert_not_reached ("unable to find suitable integer type");
17871}
17872
b86352cf
AB
17873/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17874 present (which is valid) then compute the default type based on the
17875 compilation units address size. */
17876
17877static struct type *
17878read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17879{
17880 struct type *index_type = die_type (die, cu);
17881
17882 /* Dwarf-2 specifications explicitly allows to create subrange types
17883 without specifying a base type.
17884 In that case, the base type must be set to the type of
17885 the lower bound, upper bound or count, in that order, if any of these
17886 three attributes references an object that has a type.
17887 If no base type is found, the Dwarf-2 specifications say that
17888 a signed integer type of size equal to the size of an address should
17889 be used.
17890 For the following C code: `extern char gdb_int [];'
17891 GCC produces an empty range DIE.
17892 FIXME: muller/2010-05-28: Possible references to object for low bound,
17893 high bound or count are not yet handled by this code. */
17894 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17895 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17896
17897 return index_type;
17898}
17899
a02abb62
JB
17900/* Read the given DW_AT_subrange DIE. */
17901
f792889a 17902static struct type *
a02abb62
JB
17903read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17904{
4c9ad8c2 17905 struct type *base_type, *orig_base_type;
a02abb62
JB
17906 struct type *range_type;
17907 struct attribute *attr;
729efb13 17908 struct dynamic_prop low, high;
4fae6e18 17909 int low_default_is_valid;
c451ebe5 17910 int high_bound_is_count = 0;
15d034d0 17911 const char *name;
d359392f 17912 ULONGEST negative_mask;
e77813c8 17913
b86352cf
AB
17914 orig_base_type = read_subrange_index_type (die, cu);
17915
4c9ad8c2
TT
17916 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17917 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17918 creating the range type, but we use the result of check_typedef
17919 when examining properties of the type. */
17920 base_type = check_typedef (orig_base_type);
a02abb62 17921
7e314c57
JK
17922 /* The die_type call above may have already set the type for this DIE. */
17923 range_type = get_die_type (die, cu);
17924 if (range_type)
17925 return range_type;
17926
729efb13
SA
17927 low.kind = PROP_CONST;
17928 high.kind = PROP_CONST;
17929 high.data.const_val = 0;
17930
4fae6e18
JK
17931 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17932 omitting DW_AT_lower_bound. */
17933 switch (cu->language)
6e70227d 17934 {
4fae6e18
JK
17935 case language_c:
17936 case language_cplus:
729efb13 17937 low.data.const_val = 0;
4fae6e18
JK
17938 low_default_is_valid = 1;
17939 break;
17940 case language_fortran:
729efb13 17941 low.data.const_val = 1;
4fae6e18
JK
17942 low_default_is_valid = 1;
17943 break;
17944 case language_d:
4fae6e18 17945 case language_objc:
c44af4eb 17946 case language_rust:
729efb13 17947 low.data.const_val = 0;
4fae6e18
JK
17948 low_default_is_valid = (cu->header.version >= 4);
17949 break;
17950 case language_ada:
17951 case language_m2:
17952 case language_pascal:
729efb13 17953 low.data.const_val = 1;
4fae6e18
JK
17954 low_default_is_valid = (cu->header.version >= 4);
17955 break;
17956 default:
729efb13 17957 low.data.const_val = 0;
4fae6e18
JK
17958 low_default_is_valid = 0;
17959 break;
a02abb62
JB
17960 }
17961
e142c38c 17962 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17963 if (attr)
9a49df9d 17964 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17965 else if (!low_default_is_valid)
b98664d3 17966 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17967 "- DIE at %s [in module %s]"),
17968 sect_offset_str (die->sect_off),
518817b3 17969 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17970
506f5c41
TV
17971 struct attribute *attr_ub, *attr_count;
17972 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17973 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17974 {
506f5c41 17975 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17976 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17977 {
c451ebe5
SA
17978 /* If bounds are constant do the final calculation here. */
17979 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17980 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17981 else
17982 high_bound_is_count = 1;
c2ff108b 17983 }
506f5c41
TV
17984 else
17985 {
17986 if (attr_ub != NULL)
17987 complaint (_("Unresolved DW_AT_upper_bound "
17988 "- DIE at %s [in module %s]"),
17989 sect_offset_str (die->sect_off),
17990 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17991 if (attr_count != NULL)
17992 complaint (_("Unresolved DW_AT_count "
17993 "- DIE at %s [in module %s]"),
17994 sect_offset_str (die->sect_off),
17995 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17996 }
e77813c8 17997 }
a02abb62 17998
4e962e74
TT
17999 LONGEST bias = 0;
18000 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18001 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18002 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18003
dbb9c2b1
JB
18004 /* Normally, the DWARF producers are expected to use a signed
18005 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18006 But this is unfortunately not always the case, as witnessed
18007 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18008 is used instead. To work around that ambiguity, we treat
18009 the bounds as signed, and thus sign-extend their values, when
18010 the base type is signed. */
6e70227d 18011 negative_mask =
d359392f 18012 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18013 if (low.kind == PROP_CONST
18014 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18015 low.data.const_val |= negative_mask;
18016 if (high.kind == PROP_CONST
18017 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18018 high.data.const_val |= negative_mask;
43bbcdc2 18019
4e962e74 18020 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18021
c451ebe5
SA
18022 if (high_bound_is_count)
18023 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18024
c2ff108b
JK
18025 /* Ada expects an empty array on no boundary attributes. */
18026 if (attr == NULL && cu->language != language_ada)
729efb13 18027 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18028
39cbfefa
DJ
18029 name = dwarf2_name (die, cu);
18030 if (name)
18031 TYPE_NAME (range_type) = name;
6e70227d 18032
e142c38c 18033 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18034 if (attr)
18035 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18036
2b4424c3
TT
18037 maybe_set_alignment (cu, die, range_type);
18038
7e314c57
JK
18039 set_die_type (die, range_type, cu);
18040
18041 /* set_die_type should be already done. */
b4ba55a1
JB
18042 set_descriptive_type (range_type, die, cu);
18043
7e314c57 18044 return range_type;
a02abb62 18045}
6e70227d 18046
f792889a 18047static struct type *
81a17f79
JB
18048read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18049{
18050 struct type *type;
81a17f79 18051
518817b3
SM
18052 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18053 NULL);
0114d602 18054 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18055
74a2f8ff
JB
18056 /* In Ada, an unspecified type is typically used when the description
18057 of the type is defered to a different unit. When encountering
18058 such a type, we treat it as a stub, and try to resolve it later on,
18059 when needed. */
18060 if (cu->language == language_ada)
18061 TYPE_STUB (type) = 1;
18062
f792889a 18063 return set_die_type (die, type, cu);
81a17f79 18064}
a02abb62 18065
639d11d3
DC
18066/* Read a single die and all its descendents. Set the die's sibling
18067 field to NULL; set other fields in the die correctly, and set all
18068 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18069 location of the info_ptr after reading all of those dies. PARENT
18070 is the parent of the die in question. */
18071
18072static struct die_info *
dee91e82 18073read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18074 const gdb_byte *info_ptr,
18075 const gdb_byte **new_info_ptr,
dee91e82 18076 struct die_info *parent)
639d11d3
DC
18077{
18078 struct die_info *die;
d521ce57 18079 const gdb_byte *cur_ptr;
639d11d3
DC
18080 int has_children;
18081
bf6af496 18082 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18083 if (die == NULL)
18084 {
18085 *new_info_ptr = cur_ptr;
18086 return NULL;
18087 }
93311388 18088 store_in_ref_table (die, reader->cu);
639d11d3
DC
18089
18090 if (has_children)
bf6af496 18091 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18092 else
18093 {
18094 die->child = NULL;
18095 *new_info_ptr = cur_ptr;
18096 }
18097
18098 die->sibling = NULL;
18099 die->parent = parent;
18100 return die;
18101}
18102
18103/* Read a die, all of its descendents, and all of its siblings; set
18104 all of the fields of all of the dies correctly. Arguments are as
18105 in read_die_and_children. */
18106
18107static struct die_info *
bf6af496 18108read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18109 const gdb_byte *info_ptr,
18110 const gdb_byte **new_info_ptr,
bf6af496 18111 struct die_info *parent)
639d11d3
DC
18112{
18113 struct die_info *first_die, *last_sibling;
d521ce57 18114 const gdb_byte *cur_ptr;
639d11d3 18115
c906108c 18116 cur_ptr = info_ptr;
639d11d3
DC
18117 first_die = last_sibling = NULL;
18118
18119 while (1)
c906108c 18120 {
639d11d3 18121 struct die_info *die
dee91e82 18122 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18123
1d325ec1 18124 if (die == NULL)
c906108c 18125 {
639d11d3
DC
18126 *new_info_ptr = cur_ptr;
18127 return first_die;
c906108c 18128 }
1d325ec1
DJ
18129
18130 if (!first_die)
18131 first_die = die;
c906108c 18132 else
1d325ec1
DJ
18133 last_sibling->sibling = die;
18134
18135 last_sibling = die;
c906108c 18136 }
c906108c
SS
18137}
18138
bf6af496
DE
18139/* Read a die, all of its descendents, and all of its siblings; set
18140 all of the fields of all of the dies correctly. Arguments are as
18141 in read_die_and_children.
18142 This the main entry point for reading a DIE and all its children. */
18143
18144static struct die_info *
18145read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18146 const gdb_byte *info_ptr,
18147 const gdb_byte **new_info_ptr,
bf6af496
DE
18148 struct die_info *parent)
18149{
18150 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18151 new_info_ptr, parent);
18152
b4f54984 18153 if (dwarf_die_debug)
bf6af496
DE
18154 {
18155 fprintf_unfiltered (gdb_stdlog,
18156 "Read die from %s@0x%x of %s:\n",
a32a8923 18157 get_section_name (reader->die_section),
bf6af496
DE
18158 (unsigned) (info_ptr - reader->die_section->buffer),
18159 bfd_get_filename (reader->abfd));
b4f54984 18160 dump_die (die, dwarf_die_debug);
bf6af496
DE
18161 }
18162
18163 return die;
18164}
18165
3019eac3
DE
18166/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18167 attributes.
18168 The caller is responsible for filling in the extra attributes
18169 and updating (*DIEP)->num_attrs.
18170 Set DIEP to point to a newly allocated die with its information,
18171 except for its child, sibling, and parent fields.
18172 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18173
d521ce57 18174static const gdb_byte *
3019eac3 18175read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18176 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18177 int *has_children, int num_extra_attrs)
93311388 18178{
b64f50a1 18179 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18180 struct abbrev_info *abbrev;
18181 struct die_info *die;
18182 struct dwarf2_cu *cu = reader->cu;
18183 bfd *abfd = reader->abfd;
18184
9c541725 18185 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18186 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18187 info_ptr += bytes_read;
18188 if (!abbrev_number)
18189 {
18190 *diep = NULL;
18191 *has_children = 0;
18192 return info_ptr;
18193 }
18194
685af9cd 18195 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18196 if (!abbrev)
348e048f
DE
18197 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18198 abbrev_number,
18199 bfd_get_filename (abfd));
18200
3019eac3 18201 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18202 die->sect_off = sect_off;
93311388
DE
18203 die->tag = abbrev->tag;
18204 die->abbrev = abbrev_number;
18205
3019eac3
DE
18206 /* Make the result usable.
18207 The caller needs to update num_attrs after adding the extra
18208 attributes. */
93311388
DE
18209 die->num_attrs = abbrev->num_attrs;
18210
18211 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18212 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18213 info_ptr);
93311388
DE
18214
18215 *diep = die;
18216 *has_children = abbrev->has_children;
18217 return info_ptr;
18218}
18219
3019eac3
DE
18220/* Read a die and all its attributes.
18221 Set DIEP to point to a newly allocated die with its information,
18222 except for its child, sibling, and parent fields.
18223 Set HAS_CHILDREN to tell whether the die has children or not. */
18224
d521ce57 18225static const gdb_byte *
3019eac3 18226read_full_die (const struct die_reader_specs *reader,
d521ce57 18227 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18228 int *has_children)
18229{
d521ce57 18230 const gdb_byte *result;
bf6af496
DE
18231
18232 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18233
b4f54984 18234 if (dwarf_die_debug)
bf6af496
DE
18235 {
18236 fprintf_unfiltered (gdb_stdlog,
18237 "Read die from %s@0x%x of %s:\n",
a32a8923 18238 get_section_name (reader->die_section),
bf6af496
DE
18239 (unsigned) (info_ptr - reader->die_section->buffer),
18240 bfd_get_filename (reader->abfd));
b4f54984 18241 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18242 }
18243
18244 return result;
3019eac3 18245}
433df2d4
DE
18246\f
18247/* Abbreviation tables.
3019eac3 18248
433df2d4 18249 In DWARF version 2, the description of the debugging information is
c906108c
SS
18250 stored in a separate .debug_abbrev section. Before we read any
18251 dies from a section we read in all abbreviations and install them
433df2d4
DE
18252 in a hash table. */
18253
18254/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18255
685af9cd
TT
18256struct abbrev_info *
18257abbrev_table::alloc_abbrev ()
433df2d4
DE
18258{
18259 struct abbrev_info *abbrev;
18260
685af9cd 18261 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18262 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18263
433df2d4
DE
18264 return abbrev;
18265}
18266
18267/* Add an abbreviation to the table. */
c906108c 18268
685af9cd
TT
18269void
18270abbrev_table::add_abbrev (unsigned int abbrev_number,
18271 struct abbrev_info *abbrev)
433df2d4
DE
18272{
18273 unsigned int hash_number;
18274
18275 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18276 abbrev->next = m_abbrevs[hash_number];
18277 m_abbrevs[hash_number] = abbrev;
433df2d4 18278}
dee91e82 18279
433df2d4
DE
18280/* Look up an abbrev in the table.
18281 Returns NULL if the abbrev is not found. */
18282
685af9cd
TT
18283struct abbrev_info *
18284abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18285{
433df2d4
DE
18286 unsigned int hash_number;
18287 struct abbrev_info *abbrev;
18288
18289 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18290 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18291
18292 while (abbrev)
18293 {
18294 if (abbrev->number == abbrev_number)
18295 return abbrev;
18296 abbrev = abbrev->next;
18297 }
18298 return NULL;
18299}
18300
18301/* Read in an abbrev table. */
18302
685af9cd 18303static abbrev_table_up
ed2dc618
SM
18304abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18305 struct dwarf2_section_info *section,
9c541725 18306 sect_offset sect_off)
433df2d4
DE
18307{
18308 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18309 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18310 const gdb_byte *abbrev_ptr;
c906108c
SS
18311 struct abbrev_info *cur_abbrev;
18312 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18313 unsigned int abbrev_form;
f3dd6933
DJ
18314 struct attr_abbrev *cur_attrs;
18315 unsigned int allocated_attrs;
c906108c 18316
685af9cd 18317 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18318
433df2d4 18319 dwarf2_read_section (objfile, section);
9c541725 18320 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18321 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18322 abbrev_ptr += bytes_read;
18323
f3dd6933 18324 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18325 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18326
0963b4bd 18327 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18328 while (abbrev_number)
18329 {
685af9cd 18330 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18331
18332 /* read in abbrev header */
18333 cur_abbrev->number = abbrev_number;
aead7601
SM
18334 cur_abbrev->tag
18335 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18336 abbrev_ptr += bytes_read;
18337 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18338 abbrev_ptr += 1;
18339
18340 /* now read in declarations */
22d2f3ab 18341 for (;;)
c906108c 18342 {
43988095
JK
18343 LONGEST implicit_const;
18344
22d2f3ab
JK
18345 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18346 abbrev_ptr += bytes_read;
18347 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18348 abbrev_ptr += bytes_read;
43988095
JK
18349 if (abbrev_form == DW_FORM_implicit_const)
18350 {
18351 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18352 &bytes_read);
18353 abbrev_ptr += bytes_read;
18354 }
18355 else
18356 {
18357 /* Initialize it due to a false compiler warning. */
18358 implicit_const = -1;
18359 }
22d2f3ab
JK
18360
18361 if (abbrev_name == 0)
18362 break;
18363
f3dd6933 18364 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18365 {
f3dd6933
DJ
18366 allocated_attrs += ATTR_ALLOC_CHUNK;
18367 cur_attrs
224c3ddb 18368 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18369 }
ae038cb0 18370
aead7601
SM
18371 cur_attrs[cur_abbrev->num_attrs].name
18372 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18373 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18374 = (enum dwarf_form) abbrev_form;
43988095 18375 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18376 ++cur_abbrev->num_attrs;
c906108c
SS
18377 }
18378
8d749320
SM
18379 cur_abbrev->attrs =
18380 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18381 cur_abbrev->num_attrs);
f3dd6933
DJ
18382 memcpy (cur_abbrev->attrs, cur_attrs,
18383 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18384
685af9cd 18385 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18386
18387 /* Get next abbreviation.
18388 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18389 always properly terminated with an abbrev number of 0.
18390 Exit loop if we encounter an abbreviation which we have
18391 already read (which means we are about to read the abbreviations
18392 for the next compile unit) or if the end of the abbreviation
18393 table is reached. */
433df2d4 18394 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18395 break;
18396 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18397 abbrev_ptr += bytes_read;
685af9cd 18398 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18399 break;
18400 }
f3dd6933
DJ
18401
18402 xfree (cur_attrs);
433df2d4 18403 return abbrev_table;
c906108c
SS
18404}
18405
72bf9492
DJ
18406/* Returns nonzero if TAG represents a type that we might generate a partial
18407 symbol for. */
18408
18409static int
18410is_type_tag_for_partial (int tag)
18411{
18412 switch (tag)
18413 {
18414#if 0
18415 /* Some types that would be reasonable to generate partial symbols for,
18416 that we don't at present. */
18417 case DW_TAG_array_type:
18418 case DW_TAG_file_type:
18419 case DW_TAG_ptr_to_member_type:
18420 case DW_TAG_set_type:
18421 case DW_TAG_string_type:
18422 case DW_TAG_subroutine_type:
18423#endif
18424 case DW_TAG_base_type:
18425 case DW_TAG_class_type:
680b30c7 18426 case DW_TAG_interface_type:
72bf9492
DJ
18427 case DW_TAG_enumeration_type:
18428 case DW_TAG_structure_type:
18429 case DW_TAG_subrange_type:
18430 case DW_TAG_typedef:
18431 case DW_TAG_union_type:
18432 return 1;
18433 default:
18434 return 0;
18435 }
18436}
18437
18438/* Load all DIEs that are interesting for partial symbols into memory. */
18439
18440static struct partial_die_info *
dee91e82 18441load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18442 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18443{
dee91e82 18444 struct dwarf2_cu *cu = reader->cu;
518817b3 18445 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18446 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18447 unsigned int bytes_read;
5afb4e99 18448 unsigned int load_all = 0;
72bf9492
DJ
18449 int nesting_level = 1;
18450
18451 parent_die = NULL;
18452 last_die = NULL;
18453
7adf1e79
DE
18454 gdb_assert (cu->per_cu != NULL);
18455 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18456 load_all = 1;
18457
72bf9492
DJ
18458 cu->partial_dies
18459 = htab_create_alloc_ex (cu->header.length / 12,
18460 partial_die_hash,
18461 partial_die_eq,
18462 NULL,
18463 &cu->comp_unit_obstack,
18464 hashtab_obstack_allocate,
18465 dummy_obstack_deallocate);
18466
72bf9492
DJ
18467 while (1)
18468 {
685af9cd 18469 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18470
18471 /* A NULL abbrev means the end of a series of children. */
18472 if (abbrev == NULL)
18473 {
18474 if (--nesting_level == 0)
cd9983dd
YQ
18475 return first_die;
18476
72bf9492
DJ
18477 info_ptr += bytes_read;
18478 last_die = parent_die;
18479 parent_die = parent_die->die_parent;
18480 continue;
18481 }
18482
98bfdba5
PA
18483 /* Check for template arguments. We never save these; if
18484 they're seen, we just mark the parent, and go on our way. */
18485 if (parent_die != NULL
18486 && cu->language == language_cplus
18487 && (abbrev->tag == DW_TAG_template_type_param
18488 || abbrev->tag == DW_TAG_template_value_param))
18489 {
18490 parent_die->has_template_arguments = 1;
18491
18492 if (!load_all)
18493 {
18494 /* We don't need a partial DIE for the template argument. */
dee91e82 18495 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18496 continue;
18497 }
18498 }
18499
0d99eb77 18500 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18501 Skip their other children. */
18502 if (!load_all
18503 && cu->language == language_cplus
18504 && parent_die != NULL
18505 && parent_die->tag == DW_TAG_subprogram)
18506 {
dee91e82 18507 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18508 continue;
18509 }
18510
5afb4e99
DJ
18511 /* Check whether this DIE is interesting enough to save. Normally
18512 we would not be interested in members here, but there may be
18513 later variables referencing them via DW_AT_specification (for
18514 static members). */
18515 if (!load_all
18516 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18517 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18518 && abbrev->tag != DW_TAG_enumerator
18519 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18520 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18521 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18522 && abbrev->tag != DW_TAG_variable
5afb4e99 18523 && abbrev->tag != DW_TAG_namespace
f55ee35c 18524 && abbrev->tag != DW_TAG_module
95554aad 18525 && abbrev->tag != DW_TAG_member
74921315
KS
18526 && abbrev->tag != DW_TAG_imported_unit
18527 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18528 {
18529 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18530 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18531 continue;
18532 }
18533
6f06d47b
YQ
18534 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18535 abbrev);
cd9983dd 18536
48fbe735 18537 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18538
18539 /* This two-pass algorithm for processing partial symbols has a
18540 high cost in cache pressure. Thus, handle some simple cases
18541 here which cover the majority of C partial symbols. DIEs
18542 which neither have specification tags in them, nor could have
18543 specification tags elsewhere pointing at them, can simply be
18544 processed and discarded.
18545
18546 This segment is also optional; scan_partial_symbols and
18547 add_partial_symbol will handle these DIEs if we chain
18548 them in normally. When compilers which do not emit large
18549 quantities of duplicate debug information are more common,
18550 this code can probably be removed. */
18551
18552 /* Any complete simple types at the top level (pretty much all
18553 of them, for a language without namespaces), can be processed
18554 directly. */
18555 if (parent_die == NULL
cd9983dd
YQ
18556 && pdi.has_specification == 0
18557 && pdi.is_declaration == 0
18558 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18559 || pdi.tag == DW_TAG_base_type
18560 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18561 {
cd9983dd 18562 if (building_psymtab && pdi.name != NULL)
67547d89 18563 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18564 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18565 psymbol_placement::STATIC,
1762568f 18566 0, cu->language, objfile);
cd9983dd 18567 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18568 continue;
18569 }
18570
d8228535
JK
18571 /* The exception for DW_TAG_typedef with has_children above is
18572 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18573 type_name_or_error will error on such types later.
d8228535
JK
18574
18575 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18576 it could not find the child DIEs referenced later, this is checked
18577 above. In correct DWARF DW_TAG_typedef should have no children. */
18578
cd9983dd 18579 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18580 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18581 "- DIE at %s [in module %s]"),
cd9983dd 18582 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18583
72bf9492
DJ
18584 /* If we're at the second level, and we're an enumerator, and
18585 our parent has no specification (meaning possibly lives in a
18586 namespace elsewhere), then we can add the partial symbol now
18587 instead of queueing it. */
cd9983dd 18588 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18589 && parent_die != NULL
18590 && parent_die->die_parent == NULL
18591 && parent_die->tag == DW_TAG_enumeration_type
18592 && parent_die->has_specification == 0)
18593 {
cd9983dd 18594 if (pdi.name == NULL)
b98664d3 18595 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18596 else if (building_psymtab)
67547d89 18597 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18598 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18599 cu->language == language_cplus
75aedd27
TT
18600 ? psymbol_placement::GLOBAL
18601 : psymbol_placement::STATIC,
1762568f 18602 0, cu->language, objfile);
72bf9492 18603
cd9983dd 18604 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18605 continue;
18606 }
18607
cd9983dd 18608 struct partial_die_info *part_die
6f06d47b 18609 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18610
72bf9492
DJ
18611 /* We'll save this DIE so link it in. */
18612 part_die->die_parent = parent_die;
18613 part_die->die_sibling = NULL;
18614 part_die->die_child = NULL;
18615
18616 if (last_die && last_die == parent_die)
18617 last_die->die_child = part_die;
18618 else if (last_die)
18619 last_die->die_sibling = part_die;
18620
18621 last_die = part_die;
18622
18623 if (first_die == NULL)
18624 first_die = part_die;
18625
18626 /* Maybe add the DIE to the hash table. Not all DIEs that we
18627 find interesting need to be in the hash table, because we
18628 also have the parent/sibling/child chains; only those that we
18629 might refer to by offset later during partial symbol reading.
18630
18631 For now this means things that might have be the target of a
18632 DW_AT_specification, DW_AT_abstract_origin, or
18633 DW_AT_extension. DW_AT_extension will refer only to
18634 namespaces; DW_AT_abstract_origin refers to functions (and
18635 many things under the function DIE, but we do not recurse
18636 into function DIEs during partial symbol reading) and
18637 possibly variables as well; DW_AT_specification refers to
18638 declarations. Declarations ought to have the DW_AT_declaration
18639 flag. It happens that GCC forgets to put it in sometimes, but
18640 only for functions, not for types.
18641
18642 Adding more things than necessary to the hash table is harmless
18643 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18644 wasted time in find_partial_die, when we reread the compilation
18645 unit with load_all_dies set. */
72bf9492 18646
5afb4e99 18647 if (load_all
72929c62 18648 || abbrev->tag == DW_TAG_constant
5afb4e99 18649 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18650 || abbrev->tag == DW_TAG_variable
18651 || abbrev->tag == DW_TAG_namespace
18652 || part_die->is_declaration)
18653 {
18654 void **slot;
18655
18656 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18657 to_underlying (part_die->sect_off),
18658 INSERT);
72bf9492
DJ
18659 *slot = part_die;
18660 }
18661
72bf9492 18662 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18663 we have no reason to follow the children of structures; for other
98bfdba5
PA
18664 languages we have to, so that we can get at method physnames
18665 to infer fully qualified class names, for DW_AT_specification,
18666 and for C++ template arguments. For C++, we also look one level
18667 inside functions to find template arguments (if the name of the
18668 function does not already contain the template arguments).
bc30ff58 18669
0a4b0913
AB
18670 For Ada and Fortran, we need to scan the children of subprograms
18671 and lexical blocks as well because these languages allow the
18672 definition of nested entities that could be interesting for the
18673 debugger, such as nested subprograms for instance. */
72bf9492 18674 if (last_die->has_children
5afb4e99
DJ
18675 && (load_all
18676 || last_die->tag == DW_TAG_namespace
f55ee35c 18677 || last_die->tag == DW_TAG_module
72bf9492 18678 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18679 || (cu->language == language_cplus
18680 && last_die->tag == DW_TAG_subprogram
18681 && (last_die->name == NULL
18682 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18683 || (cu->language != language_c
18684 && (last_die->tag == DW_TAG_class_type
680b30c7 18685 || last_die->tag == DW_TAG_interface_type
72bf9492 18686 || last_die->tag == DW_TAG_structure_type
bc30ff58 18687 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18688 || ((cu->language == language_ada
18689 || cu->language == language_fortran)
bc30ff58
JB
18690 && (last_die->tag == DW_TAG_subprogram
18691 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18692 {
18693 nesting_level++;
18694 parent_die = last_die;
18695 continue;
18696 }
18697
18698 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18699 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18700
18701 /* Back to the top, do it again. */
18702 }
18703}
18704
6f06d47b
YQ
18705partial_die_info::partial_die_info (sect_offset sect_off_,
18706 struct abbrev_info *abbrev)
18707 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18708{
18709}
18710
35cc7ed7
YQ
18711/* Read a minimal amount of information into the minimal die structure.
18712 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18713
48fbe735
YQ
18714const gdb_byte *
18715partial_die_info::read (const struct die_reader_specs *reader,
18716 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18717{
dee91e82 18718 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18719 struct dwarf2_per_objfile *dwarf2_per_objfile
18720 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18721 unsigned int i;
c5aa993b 18722 int has_low_pc_attr = 0;
c906108c 18723 int has_high_pc_attr = 0;
91da1414 18724 int high_pc_relative = 0;
c906108c 18725
fd0a254f 18726 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18727 {
48fbe735
YQ
18728 struct attribute attr;
18729
fd0a254f 18730 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18731
18732 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18733 partial symbol table. */
c906108c
SS
18734 switch (attr.name)
18735 {
18736 case DW_AT_name:
48fbe735 18737 switch (tag)
71c25dea
TT
18738 {
18739 case DW_TAG_compile_unit:
95554aad 18740 case DW_TAG_partial_unit:
348e048f 18741 case DW_TAG_type_unit:
71c25dea
TT
18742 /* Compilation units have a DW_AT_name that is a filename, not
18743 a source language identifier. */
18744 case DW_TAG_enumeration_type:
18745 case DW_TAG_enumerator:
18746 /* These tags always have simple identifiers already; no need
18747 to canonicalize them. */
48fbe735 18748 name = DW_STRING (&attr);
71c25dea
TT
18749 break;
18750 default:
48fbe735
YQ
18751 {
18752 struct objfile *objfile = dwarf2_per_objfile->objfile;
18753
18754 name
18755 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18756 &objfile->per_bfd->storage_obstack);
18757 }
71c25dea
TT
18758 break;
18759 }
c906108c 18760 break;
31ef98ae 18761 case DW_AT_linkage_name:
c906108c 18762 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18763 /* Note that both forms of linkage name might appear. We
18764 assume they will be the same, and we only store the last
18765 one we see. */
48fbe735 18766 linkage_name = DW_STRING (&attr);
c906108c
SS
18767 break;
18768 case DW_AT_low_pc:
18769 has_low_pc_attr = 1;
48fbe735 18770 lowpc = attr_value_as_address (&attr);
c906108c
SS
18771 break;
18772 case DW_AT_high_pc:
18773 has_high_pc_attr = 1;
48fbe735 18774 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18775 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18776 high_pc_relative = 1;
c906108c
SS
18777 break;
18778 case DW_AT_location:
0963b4bd 18779 /* Support the .debug_loc offsets. */
8e19ed76
PS
18780 if (attr_form_is_block (&attr))
18781 {
48fbe735 18782 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18783 }
3690dd37 18784 else if (attr_form_is_section_offset (&attr))
8e19ed76 18785 {
4d3c2250 18786 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18787 }
18788 else
18789 {
4d3c2250
KB
18790 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18791 "partial symbol information");
8e19ed76 18792 }
c906108c 18793 break;
c906108c 18794 case DW_AT_external:
48fbe735 18795 is_external = DW_UNSND (&attr);
c906108c
SS
18796 break;
18797 case DW_AT_declaration:
48fbe735 18798 is_declaration = DW_UNSND (&attr);
c906108c
SS
18799 break;
18800 case DW_AT_type:
48fbe735 18801 has_type = 1;
c906108c
SS
18802 break;
18803 case DW_AT_abstract_origin:
18804 case DW_AT_specification:
72bf9492 18805 case DW_AT_extension:
48fbe735
YQ
18806 has_specification = 1;
18807 spec_offset = dwarf2_get_ref_die_offset (&attr);
18808 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18809 || cu->per_cu->is_dwz);
c906108c
SS
18810 break;
18811 case DW_AT_sibling:
18812 /* Ignore absolute siblings, they might point outside of
18813 the current compile unit. */
18814 if (attr.form == DW_FORM_ref_addr)
b98664d3 18815 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18816 else
b9502d3f 18817 {
48fbe735 18818 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18819 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18820 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18821
18822 if (sibling_ptr < info_ptr)
b98664d3 18823 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18824 else if (sibling_ptr > reader->buffer_end)
18825 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18826 else
48fbe735 18827 sibling = sibling_ptr;
b9502d3f 18828 }
c906108c 18829 break;
fa4028e9 18830 case DW_AT_byte_size:
48fbe735 18831 has_byte_size = 1;
fa4028e9 18832 break;
ff908ebf 18833 case DW_AT_const_value:
48fbe735 18834 has_const_value = 1;
ff908ebf 18835 break;
68511cec
CES
18836 case DW_AT_calling_convention:
18837 /* DWARF doesn't provide a way to identify a program's source-level
18838 entry point. DW_AT_calling_convention attributes are only meant
18839 to describe functions' calling conventions.
18840
18841 However, because it's a necessary piece of information in
0c1b455e
TT
18842 Fortran, and before DWARF 4 DW_CC_program was the only
18843 piece of debugging information whose definition refers to
18844 a 'main program' at all, several compilers marked Fortran
18845 main programs with DW_CC_program --- even when those
18846 functions use the standard calling conventions.
18847
18848 Although DWARF now specifies a way to provide this
18849 information, we support this practice for backward
18850 compatibility. */
68511cec 18851 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18852 && cu->language == language_fortran)
48fbe735 18853 main_subprogram = 1;
68511cec 18854 break;
481860b3
GB
18855 case DW_AT_inline:
18856 if (DW_UNSND (&attr) == DW_INL_inlined
18857 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18858 may_be_inlined = 1;
481860b3 18859 break;
95554aad
TT
18860
18861 case DW_AT_import:
48fbe735 18862 if (tag == DW_TAG_imported_unit)
36586728 18863 {
48fbe735
YQ
18864 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18865 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18866 || cu->per_cu->is_dwz);
18867 }
95554aad
TT
18868 break;
18869
0c1b455e 18870 case DW_AT_main_subprogram:
48fbe735 18871 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18872 break;
18873
05caa1d2
TT
18874 case DW_AT_ranges:
18875 {
18876 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18877 but that requires a full DIE, so instead we just
18878 reimplement it. */
18879 int need_ranges_base = tag != DW_TAG_compile_unit;
18880 unsigned int ranges_offset = (DW_UNSND (&attr)
18881 + (need_ranges_base
18882 ? cu->ranges_base
18883 : 0));
18884
18885 /* Value of the DW_AT_ranges attribute is the offset in the
18886 .debug_ranges section. */
18887 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18888 nullptr))
18889 has_pc_info = 1;
18890 }
18891 break;
18892
c906108c
SS
18893 default:
18894 break;
18895 }
18896 }
18897
10d06d82
TT
18898 /* For Ada, if both the name and the linkage name appear, we prefer
18899 the latter. This lets "catch exception" work better, regardless
18900 of the order in which the name and linkage name were emitted.
18901 Really, though, this is just a workaround for the fact that gdb
18902 doesn't store both the name and the linkage name. */
18903 if (cu->language == language_ada && linkage_name != nullptr)
18904 name = linkage_name;
18905
91da1414 18906 if (high_pc_relative)
48fbe735 18907 highpc += lowpc;
91da1414 18908
9373cf26
JK
18909 if (has_low_pc_attr && has_high_pc_attr)
18910 {
18911 /* When using the GNU linker, .gnu.linkonce. sections are used to
18912 eliminate duplicate copies of functions and vtables and such.
18913 The linker will arbitrarily choose one and discard the others.
18914 The AT_*_pc values for such functions refer to local labels in
18915 these sections. If the section from that file was discarded, the
18916 labels are not in the output, so the relocs get a value of 0.
18917 If this is a discarded function, mark the pc bounds as invalid,
18918 so that GDB will ignore it. */
48fbe735 18919 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18920 {
48fbe735 18921 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18922 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18923
b98664d3 18924 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18925 "for DIE at %s [in module %s]"),
48fbe735
YQ
18926 paddress (gdbarch, lowpc),
18927 sect_offset_str (sect_off),
9d8780f0 18928 objfile_name (objfile));
9373cf26
JK
18929 }
18930 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18931 else if (lowpc >= highpc)
9373cf26 18932 {
48fbe735 18933 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18934 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18935
b98664d3 18936 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18937 "for DIE at %s [in module %s]"),
48fbe735
YQ
18938 paddress (gdbarch, lowpc),
18939 paddress (gdbarch, highpc),
18940 sect_offset_str (sect_off),
9c541725 18941 objfile_name (objfile));
9373cf26
JK
18942 }
18943 else
48fbe735 18944 has_pc_info = 1;
9373cf26 18945 }
85cbf3d3 18946
c906108c
SS
18947 return info_ptr;
18948}
18949
72bf9492
DJ
18950/* Find a cached partial DIE at OFFSET in CU. */
18951
d590ff25
YQ
18952struct partial_die_info *
18953dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18954{
18955 struct partial_die_info *lookup_die = NULL;
6f06d47b 18956 struct partial_die_info part_die (sect_off);
72bf9492 18957
9a3c8263 18958 lookup_die = ((struct partial_die_info *)
d590ff25 18959 htab_find_with_hash (partial_dies, &part_die,
9c541725 18960 to_underlying (sect_off)));
72bf9492 18961
72bf9492
DJ
18962 return lookup_die;
18963}
18964
348e048f
DE
18965/* Find a partial DIE at OFFSET, which may or may not be in CU,
18966 except in the case of .debug_types DIEs which do not reference
18967 outside their CU (they do however referencing other types via
55f1336d 18968 DW_FORM_ref_sig8). */
72bf9492 18969
122cf0f2 18970static const struct cu_partial_die_info
9c541725 18971find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18972{
518817b3
SM
18973 struct dwarf2_per_objfile *dwarf2_per_objfile
18974 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18975 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18976 struct dwarf2_per_cu_data *per_cu = NULL;
18977 struct partial_die_info *pd = NULL;
72bf9492 18978
36586728 18979 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18980 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18981 {
d590ff25 18982 pd = cu->find_partial_die (sect_off);
5afb4e99 18983 if (pd != NULL)
fb816e8b 18984 return { cu, pd };
0d99eb77
DE
18985 /* We missed recording what we needed.
18986 Load all dies and try again. */
18987 per_cu = cu->per_cu;
5afb4e99 18988 }
0d99eb77
DE
18989 else
18990 {
18991 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18992 if (cu->per_cu->is_debug_types)
0d99eb77 18993 {
9d8780f0
SM
18994 error (_("Dwarf Error: Type Unit at offset %s contains"
18995 " external reference to offset %s [in module %s].\n"),
18996 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18997 bfd_get_filename (objfile->obfd));
18998 }
9c541725 18999 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19000 dwarf2_per_objfile);
72bf9492 19001
0d99eb77
DE
19002 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19003 load_partial_comp_unit (per_cu);
ae038cb0 19004
0d99eb77 19005 per_cu->cu->last_used = 0;
d590ff25 19006 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19007 }
5afb4e99 19008
dee91e82
DE
19009 /* If we didn't find it, and not all dies have been loaded,
19010 load them all and try again. */
19011
5afb4e99
DJ
19012 if (pd == NULL && per_cu->load_all_dies == 0)
19013 {
5afb4e99 19014 per_cu->load_all_dies = 1;
fd820528
DE
19015
19016 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19017 THIS_CU->cu may already be in use. So we can't just free it and
19018 replace its DIEs with the ones we read in. Instead, we leave those
19019 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19020 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19021 set. */
dee91e82 19022 load_partial_comp_unit (per_cu);
5afb4e99 19023
d590ff25 19024 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19025 }
19026
19027 if (pd == NULL)
19028 internal_error (__FILE__, __LINE__,
9d8780f0 19029 _("could not find partial DIE %s "
3e43a32a 19030 "in cache [from module %s]\n"),
9d8780f0 19031 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19032 return { per_cu->cu, pd };
72bf9492
DJ
19033}
19034
abc72ce4
DE
19035/* See if we can figure out if the class lives in a namespace. We do
19036 this by looking for a member function; its demangled name will
19037 contain namespace info, if there is any. */
19038
19039static void
19040guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19041 struct dwarf2_cu *cu)
19042{
19043 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19044 what template types look like, because the demangler
19045 frequently doesn't give the same name as the debug info. We
19046 could fix this by only using the demangled name to get the
19047 prefix (but see comment in read_structure_type). */
19048
19049 struct partial_die_info *real_pdi;
19050 struct partial_die_info *child_pdi;
19051
19052 /* If this DIE (this DIE's specification, if any) has a parent, then
19053 we should not do this. We'll prepend the parent's fully qualified
19054 name when we create the partial symbol. */
19055
19056 real_pdi = struct_pdi;
19057 while (real_pdi->has_specification)
fb816e8b 19058 {
122cf0f2
AB
19059 auto res = find_partial_die (real_pdi->spec_offset,
19060 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19061 real_pdi = res.pdi;
19062 cu = res.cu;
19063 }
abc72ce4
DE
19064
19065 if (real_pdi->die_parent != NULL)
19066 return;
19067
19068 for (child_pdi = struct_pdi->die_child;
19069 child_pdi != NULL;
19070 child_pdi = child_pdi->die_sibling)
19071 {
19072 if (child_pdi->tag == DW_TAG_subprogram
19073 && child_pdi->linkage_name != NULL)
19074 {
19075 char *actual_class_name
19076 = language_class_name_from_physname (cu->language_defn,
19077 child_pdi->linkage_name);
19078 if (actual_class_name != NULL)
19079 {
518817b3 19080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19081 struct_pdi->name
021887d8
TT
19082 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19083 actual_class_name);
abc72ce4
DE
19084 xfree (actual_class_name);
19085 }
19086 break;
19087 }
19088 }
19089}
19090
52356b79
YQ
19091void
19092partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19093{
abc72ce4
DE
19094 /* Once we've fixed up a die, there's no point in doing so again.
19095 This also avoids a memory leak if we were to call
19096 guess_partial_die_structure_name multiple times. */
52356b79 19097 if (fixup_called)
abc72ce4
DE
19098 return;
19099
72bf9492
DJ
19100 /* If we found a reference attribute and the DIE has no name, try
19101 to find a name in the referred to DIE. */
19102
52356b79 19103 if (name == NULL && has_specification)
72bf9492
DJ
19104 {
19105 struct partial_die_info *spec_die;
72bf9492 19106
122cf0f2 19107 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19108 spec_die = res.pdi;
19109 cu = res.cu;
72bf9492 19110
52356b79 19111 spec_die->fixup (cu);
72bf9492
DJ
19112
19113 if (spec_die->name)
19114 {
52356b79 19115 name = spec_die->name;
72bf9492
DJ
19116
19117 /* Copy DW_AT_external attribute if it is set. */
19118 if (spec_die->is_external)
52356b79 19119 is_external = spec_die->is_external;
72bf9492
DJ
19120 }
19121 }
19122
19123 /* Set default names for some unnamed DIEs. */
72bf9492 19124
52356b79
YQ
19125 if (name == NULL && tag == DW_TAG_namespace)
19126 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19127
abc72ce4
DE
19128 /* If there is no parent die to provide a namespace, and there are
19129 children, see if we can determine the namespace from their linkage
122d1940 19130 name. */
abc72ce4 19131 if (cu->language == language_cplus
fd5866f6 19132 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19133 && die_parent == NULL
19134 && has_children
19135 && (tag == DW_TAG_class_type
19136 || tag == DW_TAG_structure_type
19137 || tag == DW_TAG_union_type))
19138 guess_partial_die_structure_name (this, cu);
abc72ce4 19139
53832f31
TT
19140 /* GCC might emit a nameless struct or union that has a linkage
19141 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19142 if (name == NULL
19143 && (tag == DW_TAG_class_type
19144 || tag == DW_TAG_interface_type
19145 || tag == DW_TAG_structure_type
19146 || tag == DW_TAG_union_type)
19147 && linkage_name != NULL)
53832f31
TT
19148 {
19149 char *demangled;
19150
52356b79 19151 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19152 if (demangled)
19153 {
96408a79
SA
19154 const char *base;
19155
19156 /* Strip any leading namespaces/classes, keep only the base name.
19157 DW_AT_name for named DIEs does not contain the prefixes. */
19158 base = strrchr (demangled, ':');
19159 if (base && base > demangled && base[-1] == ':')
19160 base++;
19161 else
19162 base = demangled;
19163
518817b3 19164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19165 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19166 xfree (demangled);
19167 }
19168 }
19169
52356b79 19170 fixup_called = 1;
72bf9492
DJ
19171}
19172
a8329558 19173/* Read an attribute value described by an attribute form. */
c906108c 19174
d521ce57 19175static const gdb_byte *
dee91e82
DE
19176read_attribute_value (const struct die_reader_specs *reader,
19177 struct attribute *attr, unsigned form,
43988095 19178 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19179{
dee91e82 19180 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19181 struct dwarf2_per_objfile *dwarf2_per_objfile
19182 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19183 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19184 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19185 bfd *abfd = reader->abfd;
e7c27a73 19186 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19187 unsigned int bytes_read;
19188 struct dwarf_block *blk;
19189
aead7601 19190 attr->form = (enum dwarf_form) form;
a8329558 19191 switch (form)
c906108c 19192 {
c906108c 19193 case DW_FORM_ref_addr:
ae411497 19194 if (cu->header.version == 2)
4568ecf9 19195 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19196 else
4568ecf9
DE
19197 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19198 &cu->header, &bytes_read);
ae411497
TT
19199 info_ptr += bytes_read;
19200 break;
36586728
TT
19201 case DW_FORM_GNU_ref_alt:
19202 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19203 info_ptr += bytes_read;
19204 break;
ae411497 19205 case DW_FORM_addr:
e7c27a73 19206 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19207 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19208 info_ptr += bytes_read;
c906108c
SS
19209 break;
19210 case DW_FORM_block2:
7b5a2f43 19211 blk = dwarf_alloc_block (cu);
c906108c
SS
19212 blk->size = read_2_bytes (abfd, info_ptr);
19213 info_ptr += 2;
19214 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19215 info_ptr += blk->size;
19216 DW_BLOCK (attr) = blk;
19217 break;
19218 case DW_FORM_block4:
7b5a2f43 19219 blk = dwarf_alloc_block (cu);
c906108c
SS
19220 blk->size = read_4_bytes (abfd, info_ptr);
19221 info_ptr += 4;
19222 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19223 info_ptr += blk->size;
19224 DW_BLOCK (attr) = blk;
19225 break;
19226 case DW_FORM_data2:
19227 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19228 info_ptr += 2;
19229 break;
19230 case DW_FORM_data4:
19231 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19232 info_ptr += 4;
19233 break;
19234 case DW_FORM_data8:
19235 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19236 info_ptr += 8;
19237 break;
0224619f
JK
19238 case DW_FORM_data16:
19239 blk = dwarf_alloc_block (cu);
19240 blk->size = 16;
19241 blk->data = read_n_bytes (abfd, info_ptr, 16);
19242 info_ptr += 16;
19243 DW_BLOCK (attr) = blk;
19244 break;
2dc7f7b3
TT
19245 case DW_FORM_sec_offset:
19246 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19247 info_ptr += bytes_read;
19248 break;
c906108c 19249 case DW_FORM_string:
9b1c24c8 19250 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19251 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19252 info_ptr += bytes_read;
19253 break;
4bdf3d34 19254 case DW_FORM_strp:
36586728
TT
19255 if (!cu->per_cu->is_dwz)
19256 {
ed2dc618
SM
19257 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19258 abfd, info_ptr, cu_header,
36586728
TT
19259 &bytes_read);
19260 DW_STRING_IS_CANONICAL (attr) = 0;
19261 info_ptr += bytes_read;
19262 break;
19263 }
19264 /* FALLTHROUGH */
43988095
JK
19265 case DW_FORM_line_strp:
19266 if (!cu->per_cu->is_dwz)
19267 {
ed2dc618
SM
19268 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19269 abfd, info_ptr,
43988095
JK
19270 cu_header, &bytes_read);
19271 DW_STRING_IS_CANONICAL (attr) = 0;
19272 info_ptr += bytes_read;
19273 break;
19274 }
19275 /* FALLTHROUGH */
36586728
TT
19276 case DW_FORM_GNU_strp_alt:
19277 {
ed2dc618 19278 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19279 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19280 &bytes_read);
19281
ed2dc618
SM
19282 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19283 dwz, str_offset);
36586728
TT
19284 DW_STRING_IS_CANONICAL (attr) = 0;
19285 info_ptr += bytes_read;
19286 }
4bdf3d34 19287 break;
2dc7f7b3 19288 case DW_FORM_exprloc:
c906108c 19289 case DW_FORM_block:
7b5a2f43 19290 blk = dwarf_alloc_block (cu);
c906108c
SS
19291 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19292 info_ptr += bytes_read;
19293 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19294 info_ptr += blk->size;
19295 DW_BLOCK (attr) = blk;
19296 break;
19297 case DW_FORM_block1:
7b5a2f43 19298 blk = dwarf_alloc_block (cu);
c906108c
SS
19299 blk->size = read_1_byte (abfd, info_ptr);
19300 info_ptr += 1;
19301 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19302 info_ptr += blk->size;
19303 DW_BLOCK (attr) = blk;
19304 break;
19305 case DW_FORM_data1:
19306 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19307 info_ptr += 1;
19308 break;
19309 case DW_FORM_flag:
19310 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19311 info_ptr += 1;
19312 break;
2dc7f7b3
TT
19313 case DW_FORM_flag_present:
19314 DW_UNSND (attr) = 1;
19315 break;
c906108c
SS
19316 case DW_FORM_sdata:
19317 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19318 info_ptr += bytes_read;
19319 break;
19320 case DW_FORM_udata:
19321 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19322 info_ptr += bytes_read;
19323 break;
19324 case DW_FORM_ref1:
9c541725 19325 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19326 + read_1_byte (abfd, info_ptr));
c906108c
SS
19327 info_ptr += 1;
19328 break;
19329 case DW_FORM_ref2:
9c541725 19330 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19331 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19332 info_ptr += 2;
19333 break;
19334 case DW_FORM_ref4:
9c541725 19335 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19336 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19337 info_ptr += 4;
19338 break;
613e1657 19339 case DW_FORM_ref8:
9c541725 19340 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19341 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19342 info_ptr += 8;
19343 break;
55f1336d 19344 case DW_FORM_ref_sig8:
ac9ec31b 19345 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19346 info_ptr += 8;
19347 break;
c906108c 19348 case DW_FORM_ref_udata:
9c541725 19349 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19350 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19351 info_ptr += bytes_read;
19352 break;
c906108c 19353 case DW_FORM_indirect:
a8329558
KW
19354 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19355 info_ptr += bytes_read;
43988095
JK
19356 if (form == DW_FORM_implicit_const)
19357 {
19358 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19359 info_ptr += bytes_read;
19360 }
19361 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19362 info_ptr);
19363 break;
19364 case DW_FORM_implicit_const:
19365 DW_SND (attr) = implicit_const;
a8329558 19366 break;
336d760d 19367 case DW_FORM_addrx:
3019eac3
DE
19368 case DW_FORM_GNU_addr_index:
19369 if (reader->dwo_file == NULL)
19370 {
19371 /* For now flag a hard error.
19372 Later we can turn this into a complaint. */
19373 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19374 dwarf_form_name (form),
19375 bfd_get_filename (abfd));
19376 }
19377 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19378 info_ptr += bytes_read;
19379 break;
cf532bd1 19380 case DW_FORM_strx:
15f18d14
AT
19381 case DW_FORM_strx1:
19382 case DW_FORM_strx2:
19383 case DW_FORM_strx3:
19384 case DW_FORM_strx4:
3019eac3
DE
19385 case DW_FORM_GNU_str_index:
19386 if (reader->dwo_file == NULL)
19387 {
19388 /* For now flag a hard error.
19389 Later we can turn this into a complaint if warranted. */
19390 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19391 dwarf_form_name (form),
19392 bfd_get_filename (abfd));
19393 }
19394 {
15f18d14
AT
19395 ULONGEST str_index;
19396 if (form == DW_FORM_strx1)
19397 {
19398 str_index = read_1_byte (abfd, info_ptr);
19399 info_ptr += 1;
19400 }
19401 else if (form == DW_FORM_strx2)
19402 {
19403 str_index = read_2_bytes (abfd, info_ptr);
19404 info_ptr += 2;
19405 }
19406 else if (form == DW_FORM_strx3)
19407 {
19408 str_index = read_3_bytes (abfd, info_ptr);
19409 info_ptr += 3;
19410 }
19411 else if (form == DW_FORM_strx4)
19412 {
19413 str_index = read_4_bytes (abfd, info_ptr);
19414 info_ptr += 4;
19415 }
19416 else
19417 {
19418 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19419 info_ptr += bytes_read;
19420 }
342587c4 19421 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19422 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19423 }
19424 break;
c906108c 19425 default:
8a3fe4f8 19426 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19427 dwarf_form_name (form),
19428 bfd_get_filename (abfd));
c906108c 19429 }
28e94949 19430
36586728 19431 /* Super hack. */
7771576e 19432 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19433 attr->form = DW_FORM_GNU_ref_alt;
19434
28e94949
JB
19435 /* We have seen instances where the compiler tried to emit a byte
19436 size attribute of -1 which ended up being encoded as an unsigned
19437 0xffffffff. Although 0xffffffff is technically a valid size value,
19438 an object of this size seems pretty unlikely so we can relatively
19439 safely treat these cases as if the size attribute was invalid and
19440 treat them as zero by default. */
19441 if (attr->name == DW_AT_byte_size
19442 && form == DW_FORM_data4
19443 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19444 {
19445 complaint
b98664d3 19446 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19447 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19448 DW_UNSND (attr) = 0;
19449 }
28e94949 19450
c906108c
SS
19451 return info_ptr;
19452}
19453
a8329558
KW
19454/* Read an attribute described by an abbreviated attribute. */
19455
d521ce57 19456static const gdb_byte *
dee91e82
DE
19457read_attribute (const struct die_reader_specs *reader,
19458 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19459 const gdb_byte *info_ptr)
a8329558
KW
19460{
19461 attr->name = abbrev->name;
43988095
JK
19462 return read_attribute_value (reader, attr, abbrev->form,
19463 abbrev->implicit_const, info_ptr);
a8329558
KW
19464}
19465
0963b4bd 19466/* Read dwarf information from a buffer. */
c906108c
SS
19467
19468static unsigned int
a1855c1d 19469read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19470{
fe1b8b76 19471 return bfd_get_8 (abfd, buf);
c906108c
SS
19472}
19473
19474static int
a1855c1d 19475read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19476{
fe1b8b76 19477 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19478}
19479
19480static unsigned int
a1855c1d 19481read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19482{
fe1b8b76 19483 return bfd_get_16 (abfd, buf);
c906108c
SS
19484}
19485
21ae7a4d 19486static int
a1855c1d 19487read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19488{
19489 return bfd_get_signed_16 (abfd, buf);
19490}
19491
15f18d14
AT
19492static unsigned int
19493read_3_bytes (bfd *abfd, const gdb_byte *buf)
19494{
19495 unsigned int result = 0;
19496 for (int i = 0; i < 3; ++i)
19497 {
19498 unsigned char byte = bfd_get_8 (abfd, buf);
19499 buf++;
19500 result |= ((unsigned int) byte << (i * 8));
19501 }
19502 return result;
19503}
19504
c906108c 19505static unsigned int
a1855c1d 19506read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19507{
fe1b8b76 19508 return bfd_get_32 (abfd, buf);
c906108c
SS
19509}
19510
21ae7a4d 19511static int
a1855c1d 19512read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19513{
19514 return bfd_get_signed_32 (abfd, buf);
19515}
19516
93311388 19517static ULONGEST
a1855c1d 19518read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19519{
fe1b8b76 19520 return bfd_get_64 (abfd, buf);
c906108c
SS
19521}
19522
19523static CORE_ADDR
d521ce57 19524read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19525 unsigned int *bytes_read)
c906108c 19526{
e7c27a73 19527 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19528 CORE_ADDR retval = 0;
19529
107d2387 19530 if (cu_header->signed_addr_p)
c906108c 19531 {
107d2387
AC
19532 switch (cu_header->addr_size)
19533 {
19534 case 2:
fe1b8b76 19535 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19536 break;
19537 case 4:
fe1b8b76 19538 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19539 break;
19540 case 8:
fe1b8b76 19541 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19542 break;
19543 default:
8e65ff28 19544 internal_error (__FILE__, __LINE__,
e2e0b3e5 19545 _("read_address: bad switch, signed [in module %s]"),
659b0389 19546 bfd_get_filename (abfd));
107d2387
AC
19547 }
19548 }
19549 else
19550 {
19551 switch (cu_header->addr_size)
19552 {
19553 case 2:
fe1b8b76 19554 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19555 break;
19556 case 4:
fe1b8b76 19557 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19558 break;
19559 case 8:
fe1b8b76 19560 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19561 break;
19562 default:
8e65ff28 19563 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19564 _("read_address: bad switch, "
19565 "unsigned [in module %s]"),
659b0389 19566 bfd_get_filename (abfd));
107d2387 19567 }
c906108c 19568 }
64367e0a 19569
107d2387
AC
19570 *bytes_read = cu_header->addr_size;
19571 return retval;
c906108c
SS
19572}
19573
f7ef9339 19574/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19575 specification allows the initial length to take up either 4 bytes
19576 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19577 bytes describe the length and all offsets will be 8 bytes in length
19578 instead of 4.
19579
f7ef9339
KB
19580 An older, non-standard 64-bit format is also handled by this
19581 function. The older format in question stores the initial length
19582 as an 8-byte quantity without an escape value. Lengths greater
19583 than 2^32 aren't very common which means that the initial 4 bytes
19584 is almost always zero. Since a length value of zero doesn't make
19585 sense for the 32-bit format, this initial zero can be considered to
19586 be an escape value which indicates the presence of the older 64-bit
19587 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19588 greater than 4GB. If it becomes necessary to handle lengths
19589 somewhat larger than 4GB, we could allow other small values (such
19590 as the non-sensical values of 1, 2, and 3) to also be used as
19591 escape values indicating the presence of the old format.
f7ef9339 19592
917c78fc
MK
19593 The value returned via bytes_read should be used to increment the
19594 relevant pointer after calling read_initial_length().
c764a876 19595
613e1657
KB
19596 [ Note: read_initial_length() and read_offset() are based on the
19597 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19598 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19599 from:
19600
f7ef9339 19601 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19602
613e1657
KB
19603 This document is only a draft and is subject to change. (So beware.)
19604
f7ef9339 19605 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19606 determined empirically by examining 64-bit ELF files produced by
19607 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19608
19609 - Kevin, July 16, 2002
613e1657
KB
19610 ] */
19611
19612static LONGEST
d521ce57 19613read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19614{
fe1b8b76 19615 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19616
dd373385 19617 if (length == 0xffffffff)
613e1657 19618 {
fe1b8b76 19619 length = bfd_get_64 (abfd, buf + 4);
613e1657 19620 *bytes_read = 12;
613e1657 19621 }
dd373385 19622 else if (length == 0)
f7ef9339 19623 {
dd373385 19624 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19625 length = bfd_get_64 (abfd, buf);
f7ef9339 19626 *bytes_read = 8;
f7ef9339 19627 }
613e1657
KB
19628 else
19629 {
19630 *bytes_read = 4;
613e1657
KB
19631 }
19632
c764a876
DE
19633 return length;
19634}
dd373385 19635
c764a876
DE
19636/* Cover function for read_initial_length.
19637 Returns the length of the object at BUF, and stores the size of the
19638 initial length in *BYTES_READ and stores the size that offsets will be in
19639 *OFFSET_SIZE.
19640 If the initial length size is not equivalent to that specified in
19641 CU_HEADER then issue a complaint.
19642 This is useful when reading non-comp-unit headers. */
dd373385 19643
c764a876 19644static LONGEST
d521ce57 19645read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19646 const struct comp_unit_head *cu_header,
19647 unsigned int *bytes_read,
19648 unsigned int *offset_size)
19649{
19650 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19651
19652 gdb_assert (cu_header->initial_length_size == 4
19653 || cu_header->initial_length_size == 8
19654 || cu_header->initial_length_size == 12);
19655
19656 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19657 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19658
c764a876 19659 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19660 return length;
613e1657
KB
19661}
19662
19663/* Read an offset from the data stream. The size of the offset is
917c78fc 19664 given by cu_header->offset_size. */
613e1657
KB
19665
19666static LONGEST
d521ce57
TT
19667read_offset (bfd *abfd, const gdb_byte *buf,
19668 const struct comp_unit_head *cu_header,
891d2f0b 19669 unsigned int *bytes_read)
c764a876
DE
19670{
19671 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19672
c764a876
DE
19673 *bytes_read = cu_header->offset_size;
19674 return offset;
19675}
19676
19677/* Read an offset from the data stream. */
19678
19679static LONGEST
d521ce57 19680read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19681{
19682 LONGEST retval = 0;
19683
c764a876 19684 switch (offset_size)
613e1657
KB
19685 {
19686 case 4:
fe1b8b76 19687 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19688 break;
19689 case 8:
fe1b8b76 19690 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19691 break;
19692 default:
8e65ff28 19693 internal_error (__FILE__, __LINE__,
c764a876 19694 _("read_offset_1: bad switch [in module %s]"),
659b0389 19695 bfd_get_filename (abfd));
613e1657
KB
19696 }
19697
917c78fc 19698 return retval;
613e1657
KB
19699}
19700
d521ce57
TT
19701static const gdb_byte *
19702read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19703{
19704 /* If the size of a host char is 8 bits, we can return a pointer
19705 to the buffer, otherwise we have to copy the data to a buffer
19706 allocated on the temporary obstack. */
4bdf3d34 19707 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19708 return buf;
c906108c
SS
19709}
19710
d521ce57
TT
19711static const char *
19712read_direct_string (bfd *abfd, const gdb_byte *buf,
19713 unsigned int *bytes_read_ptr)
c906108c
SS
19714{
19715 /* If the size of a host char is 8 bits, we can return a pointer
19716 to the string, otherwise we have to copy the string to a buffer
19717 allocated on the temporary obstack. */
4bdf3d34 19718 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19719 if (*buf == '\0')
19720 {
19721 *bytes_read_ptr = 1;
19722 return NULL;
19723 }
d521ce57
TT
19724 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19725 return (const char *) buf;
4bdf3d34
JJ
19726}
19727
43988095
JK
19728/* Return pointer to string at section SECT offset STR_OFFSET with error
19729 reporting strings FORM_NAME and SECT_NAME. */
19730
d521ce57 19731static const char *
ed2dc618
SM
19732read_indirect_string_at_offset_from (struct objfile *objfile,
19733 bfd *abfd, LONGEST str_offset,
43988095
JK
19734 struct dwarf2_section_info *sect,
19735 const char *form_name,
19736 const char *sect_name)
19737{
ed2dc618 19738 dwarf2_read_section (objfile, sect);
43988095
JK
19739 if (sect->buffer == NULL)
19740 error (_("%s used without %s section [in module %s]"),
19741 form_name, sect_name, bfd_get_filename (abfd));
19742 if (str_offset >= sect->size)
19743 error (_("%s pointing outside of %s section [in module %s]"),
19744 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19745 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19746 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19747 return NULL;
43988095
JK
19748 return (const char *) (sect->buffer + str_offset);
19749}
19750
19751/* Return pointer to string at .debug_str offset STR_OFFSET. */
19752
19753static const char *
ed2dc618
SM
19754read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19755 bfd *abfd, LONGEST str_offset)
43988095 19756{
ed2dc618
SM
19757 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19758 abfd, str_offset,
43988095
JK
19759 &dwarf2_per_objfile->str,
19760 "DW_FORM_strp", ".debug_str");
19761}
19762
19763/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19764
19765static const char *
ed2dc618
SM
19766read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19767 bfd *abfd, LONGEST str_offset)
43988095 19768{
ed2dc618
SM
19769 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19770 abfd, str_offset,
43988095
JK
19771 &dwarf2_per_objfile->line_str,
19772 "DW_FORM_line_strp",
19773 ".debug_line_str");
c906108c
SS
19774}
19775
36586728
TT
19776/* Read a string at offset STR_OFFSET in the .debug_str section from
19777 the .dwz file DWZ. Throw an error if the offset is too large. If
19778 the string consists of a single NUL byte, return NULL; otherwise
19779 return a pointer to the string. */
19780
d521ce57 19781static const char *
ed2dc618
SM
19782read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19783 LONGEST str_offset)
36586728 19784{
ed2dc618 19785 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19786
19787 if (dwz->str.buffer == NULL)
19788 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19789 "section [in module %s]"),
00f93c44 19790 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19791 if (str_offset >= dwz->str.size)
19792 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19793 ".debug_str section [in module %s]"),
00f93c44 19794 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19795 gdb_assert (HOST_CHAR_BIT == 8);
19796 if (dwz->str.buffer[str_offset] == '\0')
19797 return NULL;
d521ce57 19798 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19799}
19800
43988095
JK
19801/* Return pointer to string at .debug_str offset as read from BUF.
19802 BUF is assumed to be in a compilation unit described by CU_HEADER.
19803 Return *BYTES_READ_PTR count of bytes read from BUF. */
19804
d521ce57 19805static const char *
ed2dc618
SM
19806read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19807 const gdb_byte *buf,
cf2c3c16
TT
19808 const struct comp_unit_head *cu_header,
19809 unsigned int *bytes_read_ptr)
19810{
19811 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19812
ed2dc618 19813 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19814}
19815
43988095
JK
19816/* Return pointer to string at .debug_line_str offset as read from BUF.
19817 BUF is assumed to be in a compilation unit described by CU_HEADER.
19818 Return *BYTES_READ_PTR count of bytes read from BUF. */
19819
19820static const char *
ed2dc618
SM
19821read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19822 bfd *abfd, const gdb_byte *buf,
43988095
JK
19823 const struct comp_unit_head *cu_header,
19824 unsigned int *bytes_read_ptr)
19825{
19826 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19827
ed2dc618
SM
19828 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19829 str_offset);
43988095
JK
19830}
19831
19832ULONGEST
d521ce57 19833read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19834 unsigned int *bytes_read_ptr)
c906108c 19835{
12df843f 19836 ULONGEST result;
ce5d95e1 19837 unsigned int num_read;
870f88f7 19838 int shift;
c906108c
SS
19839 unsigned char byte;
19840
19841 result = 0;
19842 shift = 0;
19843 num_read = 0;
c906108c
SS
19844 while (1)
19845 {
fe1b8b76 19846 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19847 buf++;
19848 num_read++;
12df843f 19849 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19850 if ((byte & 128) == 0)
19851 {
19852 break;
19853 }
19854 shift += 7;
19855 }
19856 *bytes_read_ptr = num_read;
19857 return result;
19858}
19859
12df843f 19860static LONGEST
d521ce57
TT
19861read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19862 unsigned int *bytes_read_ptr)
c906108c 19863{
4dd1b460 19864 ULONGEST result;
870f88f7 19865 int shift, num_read;
c906108c
SS
19866 unsigned char byte;
19867
19868 result = 0;
19869 shift = 0;
c906108c 19870 num_read = 0;
c906108c
SS
19871 while (1)
19872 {
fe1b8b76 19873 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19874 buf++;
19875 num_read++;
4dd1b460 19876 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19877 shift += 7;
19878 if ((byte & 128) == 0)
19879 {
19880 break;
19881 }
19882 }
77e0b926 19883 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19884 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19885 *bytes_read_ptr = num_read;
19886 return result;
19887}
19888
3019eac3
DE
19889/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19890 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19891 ADDR_SIZE is the size of addresses from the CU header. */
19892
19893static CORE_ADDR
ed2dc618
SM
19894read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19895 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19896{
19897 struct objfile *objfile = dwarf2_per_objfile->objfile;
19898 bfd *abfd = objfile->obfd;
19899 const gdb_byte *info_ptr;
19900
19901 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19902 if (dwarf2_per_objfile->addr.buffer == NULL)
19903 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19904 objfile_name (objfile));
3019eac3
DE
19905 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19906 error (_("DW_FORM_addr_index pointing outside of "
19907 ".debug_addr section [in module %s]"),
4262abfb 19908 objfile_name (objfile));
3019eac3
DE
19909 info_ptr = (dwarf2_per_objfile->addr.buffer
19910 + addr_base + addr_index * addr_size);
19911 if (addr_size == 4)
19912 return bfd_get_32 (abfd, info_ptr);
19913 else
19914 return bfd_get_64 (abfd, info_ptr);
19915}
19916
19917/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19918
19919static CORE_ADDR
19920read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19921{
518817b3
SM
19922 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19923 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19924}
19925
19926/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19927
19928static CORE_ADDR
d521ce57 19929read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19930 unsigned int *bytes_read)
19931{
518817b3 19932 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19933 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19934
19935 return read_addr_index (cu, addr_index);
19936}
19937
19938/* Data structure to pass results from dwarf2_read_addr_index_reader
19939 back to dwarf2_read_addr_index. */
19940
19941struct dwarf2_read_addr_index_data
19942{
19943 ULONGEST addr_base;
19944 int addr_size;
19945};
19946
19947/* die_reader_func for dwarf2_read_addr_index. */
19948
19949static void
19950dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19951 const gdb_byte *info_ptr,
3019eac3
DE
19952 struct die_info *comp_unit_die,
19953 int has_children,
19954 void *data)
19955{
19956 struct dwarf2_cu *cu = reader->cu;
19957 struct dwarf2_read_addr_index_data *aidata =
19958 (struct dwarf2_read_addr_index_data *) data;
19959
19960 aidata->addr_base = cu->addr_base;
19961 aidata->addr_size = cu->header.addr_size;
19962}
19963
19964/* Given an index in .debug_addr, fetch the value.
19965 NOTE: This can be called during dwarf expression evaluation,
19966 long after the debug information has been read, and thus per_cu->cu
19967 may no longer exist. */
19968
19969CORE_ADDR
19970dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19971 unsigned int addr_index)
19972{
ed2dc618 19973 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19974 struct dwarf2_cu *cu = per_cu->cu;
19975 ULONGEST addr_base;
19976 int addr_size;
19977
3019eac3
DE
19978 /* We need addr_base and addr_size.
19979 If we don't have PER_CU->cu, we have to get it.
19980 Nasty, but the alternative is storing the needed info in PER_CU,
19981 which at this point doesn't seem justified: it's not clear how frequently
19982 it would get used and it would increase the size of every PER_CU.
19983 Entry points like dwarf2_per_cu_addr_size do a similar thing
19984 so we're not in uncharted territory here.
19985 Alas we need to be a bit more complicated as addr_base is contained
19986 in the DIE.
19987
19988 We don't need to read the entire CU(/TU).
19989 We just need the header and top level die.
a1b64ce1 19990
3019eac3 19991 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19992 For now we skip this optimization. */
3019eac3
DE
19993
19994 if (cu != NULL)
19995 {
19996 addr_base = cu->addr_base;
19997 addr_size = cu->header.addr_size;
19998 }
19999 else
20000 {
20001 struct dwarf2_read_addr_index_data aidata;
20002
a1b64ce1
DE
20003 /* Note: We can't use init_cutu_and_read_dies_simple here,
20004 we need addr_base. */
58f0c718 20005 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20006 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20007 addr_base = aidata.addr_base;
20008 addr_size = aidata.addr_size;
20009 }
20010
ed2dc618
SM
20011 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20012 addr_size);
3019eac3
DE
20013}
20014
cf532bd1 20015/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20016 This is only used by the Fission support. */
3019eac3 20017
d521ce57 20018static const char *
342587c4 20019read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20020{
ed2dc618 20021 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20022 struct dwarf2_per_objfile *dwarf2_per_objfile
20023 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20024 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20025 const char *objf_name = objfile_name (objfile);
3019eac3 20026 bfd *abfd = objfile->obfd;
73869dc2
DE
20027 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20028 struct dwarf2_section_info *str_offsets_section =
20029 &reader->dwo_file->sections.str_offsets;
d521ce57 20030 const gdb_byte *info_ptr;
3019eac3 20031 ULONGEST str_offset;
cf532bd1 20032 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20033
73869dc2
DE
20034 dwarf2_read_section (objfile, str_section);
20035 dwarf2_read_section (objfile, str_offsets_section);
20036 if (str_section->buffer == NULL)
57d63ce2 20037 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20038 " in CU at offset %s [in module %s]"),
20039 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20040 if (str_offsets_section->buffer == NULL)
57d63ce2 20041 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20042 " in CU at offset %s [in module %s]"),
20043 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20044 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20045 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20046 " section in CU at offset %s [in module %s]"),
20047 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20048 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20049 + str_index * cu->header.offset_size);
20050 if (cu->header.offset_size == 4)
20051 str_offset = bfd_get_32 (abfd, info_ptr);
20052 else
20053 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20054 if (str_offset >= str_section->size)
57d63ce2 20055 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20056 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20057 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20058 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20059}
20060
3019eac3
DE
20061/* Return the length of an LEB128 number in BUF. */
20062
20063static int
20064leb128_size (const gdb_byte *buf)
20065{
20066 const gdb_byte *begin = buf;
20067 gdb_byte byte;
20068
20069 while (1)
20070 {
20071 byte = *buf++;
20072 if ((byte & 128) == 0)
20073 return buf - begin;
20074 }
20075}
20076
c906108c 20077static void
e142c38c 20078set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20079{
20080 switch (lang)
20081 {
20082 case DW_LANG_C89:
76bee0cc 20083 case DW_LANG_C99:
0cfd832f 20084 case DW_LANG_C11:
c906108c 20085 case DW_LANG_C:
d1be3247 20086 case DW_LANG_UPC:
e142c38c 20087 cu->language = language_c;
c906108c 20088 break;
9c37b5ae 20089 case DW_LANG_Java:
c906108c 20090 case DW_LANG_C_plus_plus:
0cfd832f
MW
20091 case DW_LANG_C_plus_plus_11:
20092 case DW_LANG_C_plus_plus_14:
e142c38c 20093 cu->language = language_cplus;
c906108c 20094 break;
6aecb9c2
JB
20095 case DW_LANG_D:
20096 cu->language = language_d;
20097 break;
c906108c
SS
20098 case DW_LANG_Fortran77:
20099 case DW_LANG_Fortran90:
b21b22e0 20100 case DW_LANG_Fortran95:
f7de9aab
MW
20101 case DW_LANG_Fortran03:
20102 case DW_LANG_Fortran08:
e142c38c 20103 cu->language = language_fortran;
c906108c 20104 break;
a766d390
DE
20105 case DW_LANG_Go:
20106 cu->language = language_go;
20107 break;
c906108c 20108 case DW_LANG_Mips_Assembler:
e142c38c 20109 cu->language = language_asm;
c906108c
SS
20110 break;
20111 case DW_LANG_Ada83:
8aaf0b47 20112 case DW_LANG_Ada95:
bc5f45f8
JB
20113 cu->language = language_ada;
20114 break;
72019c9c
GM
20115 case DW_LANG_Modula2:
20116 cu->language = language_m2;
20117 break;
fe8e67fd
PM
20118 case DW_LANG_Pascal83:
20119 cu->language = language_pascal;
20120 break;
22566fbd
DJ
20121 case DW_LANG_ObjC:
20122 cu->language = language_objc;
20123 break;
c44af4eb
TT
20124 case DW_LANG_Rust:
20125 case DW_LANG_Rust_old:
20126 cu->language = language_rust;
20127 break;
c906108c
SS
20128 case DW_LANG_Cobol74:
20129 case DW_LANG_Cobol85:
c906108c 20130 default:
e142c38c 20131 cu->language = language_minimal;
c906108c
SS
20132 break;
20133 }
e142c38c 20134 cu->language_defn = language_def (cu->language);
c906108c
SS
20135}
20136
20137/* Return the named attribute or NULL if not there. */
20138
20139static struct attribute *
e142c38c 20140dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20141{
a48e046c 20142 for (;;)
c906108c 20143 {
a48e046c
TT
20144 unsigned int i;
20145 struct attribute *spec = NULL;
20146
20147 for (i = 0; i < die->num_attrs; ++i)
20148 {
20149 if (die->attrs[i].name == name)
20150 return &die->attrs[i];
20151 if (die->attrs[i].name == DW_AT_specification
20152 || die->attrs[i].name == DW_AT_abstract_origin)
20153 spec = &die->attrs[i];
20154 }
20155
20156 if (!spec)
20157 break;
c906108c 20158
f2f0e013 20159 die = follow_die_ref (die, spec, &cu);
f2f0e013 20160 }
c5aa993b 20161
c906108c
SS
20162 return NULL;
20163}
20164
348e048f
DE
20165/* Return the named attribute or NULL if not there,
20166 but do not follow DW_AT_specification, etc.
20167 This is for use in contexts where we're reading .debug_types dies.
20168 Following DW_AT_specification, DW_AT_abstract_origin will take us
20169 back up the chain, and we want to go down. */
20170
20171static struct attribute *
45e58e77 20172dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20173{
20174 unsigned int i;
20175
20176 for (i = 0; i < die->num_attrs; ++i)
20177 if (die->attrs[i].name == name)
20178 return &die->attrs[i];
20179
20180 return NULL;
20181}
20182
7d45c7c3
KB
20183/* Return the string associated with a string-typed attribute, or NULL if it
20184 is either not found or is of an incorrect type. */
20185
20186static const char *
20187dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20188{
20189 struct attribute *attr;
20190 const char *str = NULL;
20191
20192 attr = dwarf2_attr (die, name, cu);
20193
20194 if (attr != NULL)
20195 {
43988095 20196 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20197 || attr->form == DW_FORM_string
cf532bd1 20198 || attr->form == DW_FORM_strx
8fe0f950
AT
20199 || attr->form == DW_FORM_strx1
20200 || attr->form == DW_FORM_strx2
20201 || attr->form == DW_FORM_strx3
20202 || attr->form == DW_FORM_strx4
b3340438 20203 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20204 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20205 str = DW_STRING (attr);
20206 else
b98664d3 20207 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20208 "DIE at %s in module %s"),
20209 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20210 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20211 }
20212
20213 return str;
20214}
20215
a084a2a6
AT
20216/* Return the dwo name or NULL if not present. If present, it is in either
20217 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20218static const char *
20219dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20220{
20221 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20222 if (dwo_name == nullptr)
20223 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20224 return dwo_name;
20225}
20226
05cf31d1
JB
20227/* Return non-zero iff the attribute NAME is defined for the given DIE,
20228 and holds a non-zero value. This function should only be used for
2dc7f7b3 20229 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20230
20231static int
20232dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20233{
20234 struct attribute *attr = dwarf2_attr (die, name, cu);
20235
20236 return (attr && DW_UNSND (attr));
20237}
20238
3ca72b44 20239static int
e142c38c 20240die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20241{
05cf31d1
JB
20242 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20243 which value is non-zero. However, we have to be careful with
20244 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20245 (via dwarf2_flag_true_p) follows this attribute. So we may
20246 end up accidently finding a declaration attribute that belongs
20247 to a different DIE referenced by the specification attribute,
20248 even though the given DIE does not have a declaration attribute. */
20249 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20250 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20251}
20252
63d06c5c 20253/* Return the die giving the specification for DIE, if there is
f2f0e013 20254 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20255 containing the return value on output. If there is no
20256 specification, but there is an abstract origin, that is
20257 returned. */
63d06c5c
DC
20258
20259static struct die_info *
f2f0e013 20260die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20261{
f2f0e013
DJ
20262 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20263 *spec_cu);
63d06c5c 20264
edb3359d
DJ
20265 if (spec_attr == NULL)
20266 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20267
63d06c5c
DC
20268 if (spec_attr == NULL)
20269 return NULL;
20270 else
f2f0e013 20271 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20272}
c906108c 20273
527f3840
JK
20274/* Stub for free_line_header to match void * callback types. */
20275
20276static void
20277free_line_header_voidp (void *arg)
20278{
9a3c8263 20279 struct line_header *lh = (struct line_header *) arg;
527f3840 20280
fff8551c 20281 delete lh;
527f3840
JK
20282}
20283
fff8551c
PA
20284void
20285line_header::add_include_dir (const char *include_dir)
c906108c 20286{
27e0867f 20287 if (dwarf_line_debug >= 2)
fff8551c
PA
20288 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20289 include_dirs.size () + 1, include_dir);
27e0867f 20290
fff8551c 20291 include_dirs.push_back (include_dir);
debd256d 20292}
6e70227d 20293
fff8551c
PA
20294void
20295line_header::add_file_name (const char *name,
ecfb656c 20296 dir_index d_index,
fff8551c
PA
20297 unsigned int mod_time,
20298 unsigned int length)
debd256d 20299{
27e0867f
DE
20300 if (dwarf_line_debug >= 2)
20301 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20302 (unsigned) file_names.size () + 1, name);
27e0867f 20303
ecfb656c 20304 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20305}
6e70227d 20306
83769d0b 20307/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20308
20309static struct dwarf2_section_info *
20310get_debug_line_section (struct dwarf2_cu *cu)
20311{
20312 struct dwarf2_section_info *section;
518817b3
SM
20313 struct dwarf2_per_objfile *dwarf2_per_objfile
20314 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20315
20316 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20317 DWO file. */
20318 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20319 section = &cu->dwo_unit->dwo_file->sections.line;
20320 else if (cu->per_cu->is_dwz)
20321 {
ed2dc618 20322 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20323
20324 section = &dwz->line;
20325 }
20326 else
20327 section = &dwarf2_per_objfile->line;
20328
20329 return section;
20330}
20331
43988095
JK
20332/* Read directory or file name entry format, starting with byte of
20333 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20334 entries count and the entries themselves in the described entry
20335 format. */
20336
20337static void
ed2dc618
SM
20338read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20339 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20340 struct line_header *lh,
20341 const struct comp_unit_head *cu_header,
20342 void (*callback) (struct line_header *lh,
20343 const char *name,
ecfb656c 20344 dir_index d_index,
43988095
JK
20345 unsigned int mod_time,
20346 unsigned int length))
20347{
20348 gdb_byte format_count, formati;
20349 ULONGEST data_count, datai;
20350 const gdb_byte *buf = *bufp;
20351 const gdb_byte *format_header_data;
43988095
JK
20352 unsigned int bytes_read;
20353
20354 format_count = read_1_byte (abfd, buf);
20355 buf += 1;
20356 format_header_data = buf;
20357 for (formati = 0; formati < format_count; formati++)
20358 {
20359 read_unsigned_leb128 (abfd, buf, &bytes_read);
20360 buf += bytes_read;
20361 read_unsigned_leb128 (abfd, buf, &bytes_read);
20362 buf += bytes_read;
20363 }
20364
20365 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20366 buf += bytes_read;
20367 for (datai = 0; datai < data_count; datai++)
20368 {
20369 const gdb_byte *format = format_header_data;
20370 struct file_entry fe;
20371
43988095
JK
20372 for (formati = 0; formati < format_count; formati++)
20373 {
ecfb656c 20374 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20375 format += bytes_read;
43988095 20376
ecfb656c 20377 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20378 format += bytes_read;
ecfb656c
PA
20379
20380 gdb::optional<const char *> string;
20381 gdb::optional<unsigned int> uint;
20382
43988095
JK
20383 switch (form)
20384 {
20385 case DW_FORM_string:
ecfb656c 20386 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20387 buf += bytes_read;
20388 break;
20389
20390 case DW_FORM_line_strp:
ed2dc618
SM
20391 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20392 abfd, buf,
ecfb656c
PA
20393 cu_header,
20394 &bytes_read));
43988095
JK
20395 buf += bytes_read;
20396 break;
20397
20398 case DW_FORM_data1:
ecfb656c 20399 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20400 buf += 1;
20401 break;
20402
20403 case DW_FORM_data2:
ecfb656c 20404 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20405 buf += 2;
20406 break;
20407
20408 case DW_FORM_data4:
ecfb656c 20409 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20410 buf += 4;
20411 break;
20412
20413 case DW_FORM_data8:
ecfb656c 20414 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20415 buf += 8;
20416 break;
20417
20418 case DW_FORM_udata:
ecfb656c 20419 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20420 buf += bytes_read;
20421 break;
20422
20423 case DW_FORM_block:
20424 /* It is valid only for DW_LNCT_timestamp which is ignored by
20425 current GDB. */
20426 break;
20427 }
ecfb656c
PA
20428
20429 switch (content_type)
20430 {
20431 case DW_LNCT_path:
20432 if (string.has_value ())
20433 fe.name = *string;
20434 break;
20435 case DW_LNCT_directory_index:
20436 if (uint.has_value ())
20437 fe.d_index = (dir_index) *uint;
20438 break;
20439 case DW_LNCT_timestamp:
20440 if (uint.has_value ())
20441 fe.mod_time = *uint;
20442 break;
20443 case DW_LNCT_size:
20444 if (uint.has_value ())
20445 fe.length = *uint;
20446 break;
20447 case DW_LNCT_MD5:
20448 break;
20449 default:
b98664d3 20450 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20451 pulongest (content_type));
20452 }
43988095
JK
20453 }
20454
ecfb656c 20455 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20456 }
20457
20458 *bufp = buf;
20459}
20460
debd256d 20461/* Read the statement program header starting at OFFSET in
3019eac3 20462 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20463 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20464 Returns NULL if there is a problem reading the header, e.g., if it
20465 has a version we don't understand.
debd256d
JB
20466
20467 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20468 the returned object point into the dwarf line section buffer,
20469 and must not be freed. */
ae2de4f8 20470
fff8551c 20471static line_header_up
9c541725 20472dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20473{
d521ce57 20474 const gdb_byte *line_ptr;
c764a876 20475 unsigned int bytes_read, offset_size;
debd256d 20476 int i;
d521ce57 20477 const char *cur_dir, *cur_file;
3019eac3
DE
20478 struct dwarf2_section_info *section;
20479 bfd *abfd;
518817b3
SM
20480 struct dwarf2_per_objfile *dwarf2_per_objfile
20481 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20482
36586728 20483 section = get_debug_line_section (cu);
3019eac3
DE
20484 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20485 if (section->buffer == NULL)
debd256d 20486 {
3019eac3 20487 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20488 complaint (_("missing .debug_line.dwo section"));
3019eac3 20489 else
b98664d3 20490 complaint (_("missing .debug_line section"));
debd256d
JB
20491 return 0;
20492 }
20493
fceca515
DE
20494 /* We can't do this until we know the section is non-empty.
20495 Only then do we know we have such a section. */
a32a8923 20496 abfd = get_section_bfd_owner (section);
fceca515 20497
a738430d
MK
20498 /* Make sure that at least there's room for the total_length field.
20499 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20500 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20501 {
4d3c2250 20502 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20503 return 0;
20504 }
20505
fff8551c 20506 line_header_up lh (new line_header ());
debd256d 20507
9c541725 20508 lh->sect_off = sect_off;
527f3840
JK
20509 lh->offset_in_dwz = cu->per_cu->is_dwz;
20510
9c541725 20511 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20512
a738430d 20513 /* Read in the header. */
6e70227d 20514 lh->total_length =
c764a876
DE
20515 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20516 &bytes_read, &offset_size);
debd256d 20517 line_ptr += bytes_read;
3019eac3 20518 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20519 {
4d3c2250 20520 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20521 return 0;
20522 }
20523 lh->statement_program_end = line_ptr + lh->total_length;
20524 lh->version = read_2_bytes (abfd, line_ptr);
20525 line_ptr += 2;
43988095 20526 if (lh->version > 5)
cd366ee8
DE
20527 {
20528 /* This is a version we don't understand. The format could have
20529 changed in ways we don't handle properly so just punt. */
b98664d3 20530 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20531 return NULL;
20532 }
43988095
JK
20533 if (lh->version >= 5)
20534 {
20535 gdb_byte segment_selector_size;
20536
20537 /* Skip address size. */
20538 read_1_byte (abfd, line_ptr);
20539 line_ptr += 1;
20540
20541 segment_selector_size = read_1_byte (abfd, line_ptr);
20542 line_ptr += 1;
20543 if (segment_selector_size != 0)
20544 {
b98664d3 20545 complaint (_("unsupported segment selector size %u "
43988095
JK
20546 "in .debug_line section"),
20547 segment_selector_size);
20548 return NULL;
20549 }
20550 }
c764a876
DE
20551 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20552 line_ptr += offset_size;
debd256d
JB
20553 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20554 line_ptr += 1;
2dc7f7b3
TT
20555 if (lh->version >= 4)
20556 {
20557 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20558 line_ptr += 1;
20559 }
20560 else
20561 lh->maximum_ops_per_instruction = 1;
20562
20563 if (lh->maximum_ops_per_instruction == 0)
20564 {
20565 lh->maximum_ops_per_instruction = 1;
b98664d3 20566 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20567 "in `.debug_line' section"));
2dc7f7b3
TT
20568 }
20569
debd256d
JB
20570 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20571 line_ptr += 1;
20572 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20573 line_ptr += 1;
20574 lh->line_range = read_1_byte (abfd, line_ptr);
20575 line_ptr += 1;
20576 lh->opcode_base = read_1_byte (abfd, line_ptr);
20577 line_ptr += 1;
fff8551c 20578 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20579
20580 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20581 for (i = 1; i < lh->opcode_base; ++i)
20582 {
20583 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20584 line_ptr += 1;
20585 }
20586
43988095 20587 if (lh->version >= 5)
debd256d 20588 {
43988095 20589 /* Read directory table. */
ed2dc618
SM
20590 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20591 &cu->header,
b926417a 20592 [] (struct line_header *header, const char *name,
ecfb656c 20593 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20594 unsigned int length)
20595 {
b926417a 20596 header->add_include_dir (name);
fff8551c 20597 });
debd256d 20598
43988095 20599 /* Read file name table. */
ed2dc618
SM
20600 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20601 &cu->header,
b926417a 20602 [] (struct line_header *header, const char *name,
ecfb656c 20603 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20604 unsigned int length)
20605 {
b926417a 20606 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20607 });
43988095
JK
20608 }
20609 else
debd256d 20610 {
43988095
JK
20611 /* Read directory table. */
20612 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20613 {
20614 line_ptr += bytes_read;
fff8551c 20615 lh->add_include_dir (cur_dir);
43988095 20616 }
debd256d
JB
20617 line_ptr += bytes_read;
20618
43988095
JK
20619 /* Read file name table. */
20620 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20621 {
ecfb656c
PA
20622 unsigned int mod_time, length;
20623 dir_index d_index;
43988095
JK
20624
20625 line_ptr += bytes_read;
ecfb656c 20626 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20627 line_ptr += bytes_read;
20628 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20629 line_ptr += bytes_read;
20630 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20631 line_ptr += bytes_read;
20632
ecfb656c 20633 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20634 }
20635 line_ptr += bytes_read;
debd256d 20636 }
6e70227d 20637 lh->statement_program_start = line_ptr;
debd256d 20638
3019eac3 20639 if (line_ptr > (section->buffer + section->size))
b98664d3 20640 complaint (_("line number info header doesn't "
3e43a32a 20641 "fit in `.debug_line' section"));
debd256d 20642
debd256d
JB
20643 return lh;
20644}
c906108c 20645
c6da4cef
DE
20646/* Subroutine of dwarf_decode_lines to simplify it.
20647 Return the file name of the psymtab for included file FILE_INDEX
20648 in line header LH of PST.
20649 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20650 If space for the result is malloc'd, *NAME_HOLDER will be set.
20651 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20652
d521ce57 20653static const char *
c6da4cef
DE
20654psymtab_include_file_name (const struct line_header *lh, int file_index,
20655 const struct partial_symtab *pst,
c89b44cd
TT
20656 const char *comp_dir,
20657 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20658{
8c43009f 20659 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20660 const char *include_name = fe.name;
20661 const char *include_name_to_compare = include_name;
72b9f47f 20662 const char *pst_filename;
c6da4cef
DE
20663 int file_is_pst;
20664
8c43009f 20665 const char *dir_name = fe.include_dir (lh);
c6da4cef 20666
c89b44cd 20667 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20668 if (!IS_ABSOLUTE_PATH (include_name)
20669 && (dir_name != NULL || comp_dir != NULL))
20670 {
20671 /* Avoid creating a duplicate psymtab for PST.
20672 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20673 Before we do the comparison, however, we need to account
20674 for DIR_NAME and COMP_DIR.
20675 First prepend dir_name (if non-NULL). If we still don't
20676 have an absolute path prepend comp_dir (if non-NULL).
20677 However, the directory we record in the include-file's
20678 psymtab does not contain COMP_DIR (to match the
20679 corresponding symtab(s)).
20680
20681 Example:
20682
20683 bash$ cd /tmp
20684 bash$ gcc -g ./hello.c
20685 include_name = "hello.c"
20686 dir_name = "."
20687 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20688 DW_AT_name = "./hello.c"
20689
20690 */
c6da4cef
DE
20691
20692 if (dir_name != NULL)
20693 {
c89b44cd
TT
20694 name_holder->reset (concat (dir_name, SLASH_STRING,
20695 include_name, (char *) NULL));
20696 include_name = name_holder->get ();
c6da4cef 20697 include_name_to_compare = include_name;
c6da4cef
DE
20698 }
20699 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20700 {
c89b44cd
TT
20701 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20702 include_name, (char *) NULL));
20703 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20704 }
20705 }
20706
20707 pst_filename = pst->filename;
c89b44cd 20708 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20709 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20710 {
c89b44cd
TT
20711 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20712 pst_filename, (char *) NULL));
20713 pst_filename = copied_name.get ();
c6da4cef
DE
20714 }
20715
1e3fad37 20716 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20717
c6da4cef
DE
20718 if (file_is_pst)
20719 return NULL;
20720 return include_name;
20721}
20722
d9b3de22
DE
20723/* State machine to track the state of the line number program. */
20724
6f77053d 20725class lnp_state_machine
d9b3de22 20726{
6f77053d
PA
20727public:
20728 /* Initialize a machine state for the start of a line number
20729 program. */
804d2729
TT
20730 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20731 bool record_lines_p);
6f77053d 20732
8c43009f
PA
20733 file_entry *current_file ()
20734 {
20735 /* lh->file_names is 0-based, but the file name numbers in the
20736 statement program are 1-based. */
6f77053d
PA
20737 return m_line_header->file_name_at (m_file);
20738 }
20739
20740 /* Record the line in the state machine. END_SEQUENCE is true if
20741 we're processing the end of a sequence. */
20742 void record_line (bool end_sequence);
20743
7ab6656f
OJ
20744 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20745 nop-out rest of the lines in this sequence. */
6f77053d
PA
20746 void check_line_address (struct dwarf2_cu *cu,
20747 const gdb_byte *line_ptr,
7ab6656f 20748 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20749
20750 void handle_set_discriminator (unsigned int discriminator)
20751 {
20752 m_discriminator = discriminator;
20753 m_line_has_non_zero_discriminator |= discriminator != 0;
20754 }
20755
20756 /* Handle DW_LNE_set_address. */
20757 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20758 {
20759 m_op_index = 0;
20760 address += baseaddr;
20761 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20762 }
20763
20764 /* Handle DW_LNS_advance_pc. */
20765 void handle_advance_pc (CORE_ADDR adjust);
20766
20767 /* Handle a special opcode. */
20768 void handle_special_opcode (unsigned char op_code);
20769
20770 /* Handle DW_LNS_advance_line. */
20771 void handle_advance_line (int line_delta)
20772 {
20773 advance_line (line_delta);
20774 }
20775
20776 /* Handle DW_LNS_set_file. */
20777 void handle_set_file (file_name_index file);
20778
20779 /* Handle DW_LNS_negate_stmt. */
20780 void handle_negate_stmt ()
20781 {
20782 m_is_stmt = !m_is_stmt;
20783 }
20784
20785 /* Handle DW_LNS_const_add_pc. */
20786 void handle_const_add_pc ();
20787
20788 /* Handle DW_LNS_fixed_advance_pc. */
20789 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20790 {
20791 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20792 m_op_index = 0;
20793 }
20794
20795 /* Handle DW_LNS_copy. */
20796 void handle_copy ()
20797 {
20798 record_line (false);
20799 m_discriminator = 0;
20800 }
20801
20802 /* Handle DW_LNE_end_sequence. */
20803 void handle_end_sequence ()
20804 {
804d2729 20805 m_currently_recording_lines = true;
6f77053d
PA
20806 }
20807
20808private:
20809 /* Advance the line by LINE_DELTA. */
20810 void advance_line (int line_delta)
20811 {
20812 m_line += line_delta;
20813
20814 if (line_delta != 0)
20815 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20816 }
20817
804d2729
TT
20818 struct dwarf2_cu *m_cu;
20819
6f77053d
PA
20820 gdbarch *m_gdbarch;
20821
20822 /* True if we're recording lines.
20823 Otherwise we're building partial symtabs and are just interested in
20824 finding include files mentioned by the line number program. */
20825 bool m_record_lines_p;
20826
8c43009f 20827 /* The line number header. */
6f77053d 20828 line_header *m_line_header;
8c43009f 20829
6f77053d
PA
20830 /* These are part of the standard DWARF line number state machine,
20831 and initialized according to the DWARF spec. */
d9b3de22 20832
6f77053d 20833 unsigned char m_op_index = 0;
8c43009f 20834 /* The line table index (1-based) of the current file. */
6f77053d
PA
20835 file_name_index m_file = (file_name_index) 1;
20836 unsigned int m_line = 1;
20837
20838 /* These are initialized in the constructor. */
20839
20840 CORE_ADDR m_address;
20841 bool m_is_stmt;
20842 unsigned int m_discriminator;
d9b3de22
DE
20843
20844 /* Additional bits of state we need to track. */
20845
20846 /* The last file that we called dwarf2_start_subfile for.
20847 This is only used for TLLs. */
6f77053d 20848 unsigned int m_last_file = 0;
d9b3de22 20849 /* The last file a line number was recorded for. */
6f77053d 20850 struct subfile *m_last_subfile = NULL;
d9b3de22 20851
804d2729
TT
20852 /* When true, record the lines we decode. */
20853 bool m_currently_recording_lines = false;
d9b3de22
DE
20854
20855 /* The last line number that was recorded, used to coalesce
20856 consecutive entries for the same line. This can happen, for
20857 example, when discriminators are present. PR 17276. */
6f77053d
PA
20858 unsigned int m_last_line = 0;
20859 bool m_line_has_non_zero_discriminator = false;
8c43009f 20860};
d9b3de22 20861
6f77053d
PA
20862void
20863lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20864{
20865 CORE_ADDR addr_adj = (((m_op_index + adjust)
20866 / m_line_header->maximum_ops_per_instruction)
20867 * m_line_header->minimum_instruction_length);
20868 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20869 m_op_index = ((m_op_index + adjust)
20870 % m_line_header->maximum_ops_per_instruction);
20871}
d9b3de22 20872
6f77053d
PA
20873void
20874lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20875{
6f77053d
PA
20876 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20877 CORE_ADDR addr_adj = (((m_op_index
20878 + (adj_opcode / m_line_header->line_range))
20879 / m_line_header->maximum_ops_per_instruction)
20880 * m_line_header->minimum_instruction_length);
20881 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20882 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20883 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20884
6f77053d
PA
20885 int line_delta = (m_line_header->line_base
20886 + (adj_opcode % m_line_header->line_range));
20887 advance_line (line_delta);
20888 record_line (false);
20889 m_discriminator = 0;
20890}
d9b3de22 20891
6f77053d
PA
20892void
20893lnp_state_machine::handle_set_file (file_name_index file)
20894{
20895 m_file = file;
20896
20897 const file_entry *fe = current_file ();
20898 if (fe == NULL)
20899 dwarf2_debug_line_missing_file_complaint ();
20900 else if (m_record_lines_p)
20901 {
20902 const char *dir = fe->include_dir (m_line_header);
20903
c24bdb02 20904 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20905 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20906 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20907 }
20908}
20909
20910void
20911lnp_state_machine::handle_const_add_pc ()
20912{
20913 CORE_ADDR adjust
20914 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20915
20916 CORE_ADDR addr_adj
20917 = (((m_op_index + adjust)
20918 / m_line_header->maximum_ops_per_instruction)
20919 * m_line_header->minimum_instruction_length);
20920
20921 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20922 m_op_index = ((m_op_index + adjust)
20923 % m_line_header->maximum_ops_per_instruction);
20924}
d9b3de22 20925
a05a36a5
DE
20926/* Return non-zero if we should add LINE to the line number table.
20927 LINE is the line to add, LAST_LINE is the last line that was added,
20928 LAST_SUBFILE is the subfile for LAST_LINE.
20929 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20930 had a non-zero discriminator.
20931
20932 We have to be careful in the presence of discriminators.
20933 E.g., for this line:
20934
20935 for (i = 0; i < 100000; i++);
20936
20937 clang can emit four line number entries for that one line,
20938 each with a different discriminator.
20939 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20940
20941 However, we want gdb to coalesce all four entries into one.
20942 Otherwise the user could stepi into the middle of the line and
20943 gdb would get confused about whether the pc really was in the
20944 middle of the line.
20945
20946 Things are further complicated by the fact that two consecutive
20947 line number entries for the same line is a heuristic used by gcc
20948 to denote the end of the prologue. So we can't just discard duplicate
20949 entries, we have to be selective about it. The heuristic we use is
20950 that we only collapse consecutive entries for the same line if at least
20951 one of those entries has a non-zero discriminator. PR 17276.
20952
20953 Note: Addresses in the line number state machine can never go backwards
20954 within one sequence, thus this coalescing is ok. */
20955
20956static int
804d2729
TT
20957dwarf_record_line_p (struct dwarf2_cu *cu,
20958 unsigned int line, unsigned int last_line,
a05a36a5
DE
20959 int line_has_non_zero_discriminator,
20960 struct subfile *last_subfile)
20961{
c24bdb02 20962 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20963 return 1;
20964 if (line != last_line)
20965 return 1;
20966 /* Same line for the same file that we've seen already.
20967 As a last check, for pr 17276, only record the line if the line
20968 has never had a non-zero discriminator. */
20969 if (!line_has_non_zero_discriminator)
20970 return 1;
20971 return 0;
20972}
20973
804d2729
TT
20974/* Use the CU's builder to record line number LINE beginning at
20975 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20976
20977static void
d9b3de22
DE
20978dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20979 unsigned int line, CORE_ADDR address,
804d2729 20980 struct dwarf2_cu *cu)
252a6764
DE
20981{
20982 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20983
27e0867f
DE
20984 if (dwarf_line_debug)
20985 {
20986 fprintf_unfiltered (gdb_stdlog,
20987 "Recording line %u, file %s, address %s\n",
20988 line, lbasename (subfile->name),
20989 paddress (gdbarch, address));
20990 }
20991
804d2729 20992 if (cu != nullptr)
c24bdb02 20993 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20994}
20995
20996/* Subroutine of dwarf_decode_lines_1 to simplify it.
20997 Mark the end of a set of line number records.
d9b3de22 20998 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20999 If SUBFILE is NULL the request is ignored. */
21000
21001static void
21002dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21003 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21004{
27e0867f
DE
21005 if (subfile == NULL)
21006 return;
21007
21008 if (dwarf_line_debug)
21009 {
21010 fprintf_unfiltered (gdb_stdlog,
21011 "Finishing current line, file %s, address %s\n",
21012 lbasename (subfile->name),
21013 paddress (gdbarch, address));
21014 }
21015
804d2729 21016 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21017}
21018
6f77053d
PA
21019void
21020lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21021{
d9b3de22
DE
21022 if (dwarf_line_debug)
21023 {
21024 fprintf_unfiltered (gdb_stdlog,
21025 "Processing actual line %u: file %u,"
21026 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21027 m_line, to_underlying (m_file),
21028 paddress (m_gdbarch, m_address),
21029 m_is_stmt, m_discriminator);
d9b3de22
DE
21030 }
21031
6f77053d 21032 file_entry *fe = current_file ();
8c43009f
PA
21033
21034 if (fe == NULL)
d9b3de22
DE
21035 dwarf2_debug_line_missing_file_complaint ();
21036 /* For now we ignore lines not starting on an instruction boundary.
21037 But not when processing end_sequence for compatibility with the
21038 previous version of the code. */
6f77053d 21039 else if (m_op_index == 0 || end_sequence)
d9b3de22 21040 {
8c43009f 21041 fe->included_p = 1;
c258c396 21042 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21043 {
c24bdb02 21044 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21045 || end_sequence)
d9b3de22 21046 {
804d2729
TT
21047 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21048 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21049 }
21050
21051 if (!end_sequence)
21052 {
804d2729 21053 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21054 m_line_has_non_zero_discriminator,
21055 m_last_subfile))
d9b3de22 21056 {
c24bdb02 21057 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21058 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21059 builder->get_current_subfile (),
6f77053d 21060 m_line, m_address,
804d2729 21061 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21062 }
c24bdb02 21063 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21064 m_last_line = m_line;
d9b3de22
DE
21065 }
21066 }
21067 }
21068}
21069
804d2729
TT
21070lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21071 line_header *lh, bool record_lines_p)
d9b3de22 21072{
804d2729 21073 m_cu = cu;
6f77053d
PA
21074 m_gdbarch = arch;
21075 m_record_lines_p = record_lines_p;
21076 m_line_header = lh;
d9b3de22 21077
804d2729 21078 m_currently_recording_lines = true;
d9b3de22 21079
d9b3de22
DE
21080 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21081 was a line entry for it so that the backend has a chance to adjust it
21082 and also record it in case it needs it. This is currently used by MIPS
21083 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21084 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21085 m_is_stmt = lh->default_is_stmt;
21086 m_discriminator = 0;
252a6764
DE
21087}
21088
6f77053d
PA
21089void
21090lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21091 const gdb_byte *line_ptr,
7ab6656f 21092 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21093{
7ab6656f
OJ
21094 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21095 the pc range of the CU. However, we restrict the test to only ADDRESS
21096 values of zero to preserve GDB's previous behaviour which is to handle
21097 the specific case of a function being GC'd by the linker. */
924c2928 21098
7ab6656f 21099 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21100 {
21101 /* This line table is for a function which has been
21102 GCd by the linker. Ignore it. PR gdb/12528 */
21103
518817b3 21104 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21105 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21106
b98664d3 21107 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21108 line_offset, objfile_name (objfile));
804d2729
TT
21109 m_currently_recording_lines = false;
21110 /* Note: m_currently_recording_lines is left as false until we see
21111 DW_LNE_end_sequence. */
924c2928
DE
21112 }
21113}
21114
f3f5162e 21115/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21116 Process the line number information in LH.
21117 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21118 program in order to set included_p for every referenced header. */
debd256d 21119
c906108c 21120static void
43f3e411
DE
21121dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21122 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21123{
d521ce57
TT
21124 const gdb_byte *line_ptr, *extended_end;
21125 const gdb_byte *line_end;
a8c50c1f 21126 unsigned int bytes_read, extended_len;
699ca60a 21127 unsigned char op_code, extended_op;
e142c38c 21128 CORE_ADDR baseaddr;
518817b3 21129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21130 bfd *abfd = objfile->obfd;
fbf65064 21131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21132 /* True if we're recording line info (as opposed to building partial
21133 symtabs and just interested in finding include files mentioned by
21134 the line number program). */
21135 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21136
21137 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21138
debd256d
JB
21139 line_ptr = lh->statement_program_start;
21140 line_end = lh->statement_program_end;
c906108c
SS
21141
21142 /* Read the statement sequences until there's nothing left. */
21143 while (line_ptr < line_end)
21144 {
6f77053d
PA
21145 /* The DWARF line number program state machine. Reset the state
21146 machine at the start of each sequence. */
804d2729 21147 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21148 bool end_sequence = false;
d9b3de22 21149
8c43009f 21150 if (record_lines_p)
c906108c 21151 {
8c43009f
PA
21152 /* Start a subfile for the current file of the state
21153 machine. */
21154 const file_entry *fe = state_machine.current_file ();
21155
21156 if (fe != NULL)
804d2729 21157 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21158 }
21159
a738430d 21160 /* Decode the table. */
d9b3de22 21161 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21162 {
21163 op_code = read_1_byte (abfd, line_ptr);
21164 line_ptr += 1;
9aa1fe7e 21165
debd256d 21166 if (op_code >= lh->opcode_base)
6e70227d 21167 {
8e07a239 21168 /* Special opcode. */
6f77053d 21169 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21170 }
21171 else switch (op_code)
c906108c
SS
21172 {
21173 case DW_LNS_extended_op:
3e43a32a
MS
21174 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21175 &bytes_read);
473b7be6 21176 line_ptr += bytes_read;
a8c50c1f 21177 extended_end = line_ptr + extended_len;
c906108c
SS
21178 extended_op = read_1_byte (abfd, line_ptr);
21179 line_ptr += 1;
21180 switch (extended_op)
21181 {
21182 case DW_LNE_end_sequence:
6f77053d
PA
21183 state_machine.handle_end_sequence ();
21184 end_sequence = true;
c906108c
SS
21185 break;
21186 case DW_LNE_set_address:
d9b3de22
DE
21187 {
21188 CORE_ADDR address
21189 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21190 line_ptr += bytes_read;
6f77053d
PA
21191
21192 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21193 lowpc - baseaddr, address);
6f77053d 21194 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21195 }
c906108c
SS
21196 break;
21197 case DW_LNE_define_file:
debd256d 21198 {
d521ce57 21199 const char *cur_file;
ecfb656c
PA
21200 unsigned int mod_time, length;
21201 dir_index dindex;
6e70227d 21202
3e43a32a
MS
21203 cur_file = read_direct_string (abfd, line_ptr,
21204 &bytes_read);
debd256d 21205 line_ptr += bytes_read;
ecfb656c 21206 dindex = (dir_index)
debd256d
JB
21207 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21208 line_ptr += bytes_read;
21209 mod_time =
21210 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21211 line_ptr += bytes_read;
21212 length =
21213 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21214 line_ptr += bytes_read;
ecfb656c 21215 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21216 }
c906108c 21217 break;
d0c6ba3d 21218 case DW_LNE_set_discriminator:
6f77053d
PA
21219 {
21220 /* The discriminator is not interesting to the
21221 debugger; just ignore it. We still need to
21222 check its value though:
21223 if there are consecutive entries for the same
21224 (non-prologue) line we want to coalesce them.
21225 PR 17276. */
21226 unsigned int discr
21227 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21228 line_ptr += bytes_read;
21229
21230 state_machine.handle_set_discriminator (discr);
21231 }
d0c6ba3d 21232 break;
c906108c 21233 default:
b98664d3 21234 complaint (_("mangled .debug_line section"));
debd256d 21235 return;
c906108c 21236 }
a8c50c1f
DJ
21237 /* Make sure that we parsed the extended op correctly. If e.g.
21238 we expected a different address size than the producer used,
21239 we may have read the wrong number of bytes. */
21240 if (line_ptr != extended_end)
21241 {
b98664d3 21242 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21243 return;
21244 }
c906108c
SS
21245 break;
21246 case DW_LNS_copy:
6f77053d 21247 state_machine.handle_copy ();
c906108c
SS
21248 break;
21249 case DW_LNS_advance_pc:
2dc7f7b3
TT
21250 {
21251 CORE_ADDR adjust
21252 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21253 line_ptr += bytes_read;
6f77053d
PA
21254
21255 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21256 }
c906108c
SS
21257 break;
21258 case DW_LNS_advance_line:
a05a36a5
DE
21259 {
21260 int line_delta
21261 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21262 line_ptr += bytes_read;
6f77053d
PA
21263
21264 state_machine.handle_advance_line (line_delta);
a05a36a5 21265 }
c906108c
SS
21266 break;
21267 case DW_LNS_set_file:
d9b3de22 21268 {
6f77053d 21269 file_name_index file
ecfb656c
PA
21270 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21271 &bytes_read);
d9b3de22 21272 line_ptr += bytes_read;
8c43009f 21273
6f77053d 21274 state_machine.handle_set_file (file);
d9b3de22 21275 }
c906108c
SS
21276 break;
21277 case DW_LNS_set_column:
0ad93d4f 21278 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21279 line_ptr += bytes_read;
21280 break;
21281 case DW_LNS_negate_stmt:
6f77053d 21282 state_machine.handle_negate_stmt ();
c906108c
SS
21283 break;
21284 case DW_LNS_set_basic_block:
c906108c 21285 break;
c2c6d25f
JM
21286 /* Add to the address register of the state machine the
21287 address increment value corresponding to special opcode
a738430d
MK
21288 255. I.e., this value is scaled by the minimum
21289 instruction length since special opcode 255 would have
b021a221 21290 scaled the increment. */
c906108c 21291 case DW_LNS_const_add_pc:
6f77053d 21292 state_machine.handle_const_add_pc ();
c906108c
SS
21293 break;
21294 case DW_LNS_fixed_advance_pc:
3e29f34a 21295 {
6f77053d 21296 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21297 line_ptr += 2;
6f77053d
PA
21298
21299 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21300 }
c906108c 21301 break;
9aa1fe7e 21302 default:
a738430d
MK
21303 {
21304 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21305 int i;
a738430d 21306
debd256d 21307 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21308 {
21309 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21310 line_ptr += bytes_read;
21311 }
21312 }
c906108c
SS
21313 }
21314 }
d9b3de22
DE
21315
21316 if (!end_sequence)
21317 dwarf2_debug_line_missing_end_sequence_complaint ();
21318
21319 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21320 in which case we still finish recording the last line). */
6f77053d 21321 state_machine.record_line (true);
c906108c 21322 }
f3f5162e
DE
21323}
21324
21325/* Decode the Line Number Program (LNP) for the given line_header
21326 structure and CU. The actual information extracted and the type
21327 of structures created from the LNP depends on the value of PST.
21328
21329 1. If PST is NULL, then this procedure uses the data from the program
21330 to create all necessary symbol tables, and their linetables.
21331
21332 2. If PST is not NULL, this procedure reads the program to determine
21333 the list of files included by the unit represented by PST, and
21334 builds all the associated partial symbol tables.
21335
21336 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21337 It is used for relative paths in the line table.
21338 NOTE: When processing partial symtabs (pst != NULL),
21339 comp_dir == pst->dirname.
21340
21341 NOTE: It is important that psymtabs have the same file name (via strcmp)
21342 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21343 symtab we don't use it in the name of the psymtabs we create.
21344 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21345 A good testcase for this is mb-inline.exp.
21346
527f3840
JK
21347 LOWPC is the lowest address in CU (or 0 if not known).
21348
21349 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21350 for its PC<->lines mapping information. Otherwise only the filename
21351 table is read in. */
f3f5162e
DE
21352
21353static void
21354dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21355 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21356 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21357{
518817b3 21358 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21359 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21360
527f3840
JK
21361 if (decode_mapping)
21362 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21363
21364 if (decode_for_pst_p)
21365 {
21366 int file_index;
21367
21368 /* Now that we're done scanning the Line Header Program, we can
21369 create the psymtab of each included file. */
fff8551c 21370 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21371 if (lh->file_names[file_index].included_p == 1)
21372 {
c89b44cd 21373 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21374 const char *include_name =
c89b44cd
TT
21375 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21376 &name_holder);
c6da4cef 21377 if (include_name != NULL)
aaa75496
JB
21378 dwarf2_create_include_psymtab (include_name, pst, objfile);
21379 }
21380 }
cb1df416
DJ
21381 else
21382 {
21383 /* Make sure a symtab is created for every file, even files
21384 which contain only variables (i.e. no code with associated
21385 line numbers). */
c24bdb02
KS
21386 buildsym_compunit *builder = cu->get_builder ();
21387 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21388 int i;
cb1df416 21389
fff8551c 21390 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21391 {
8c43009f 21392 file_entry &fe = lh->file_names[i];
9a619af0 21393
804d2729 21394 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21395
c24bdb02 21396 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21397 {
c24bdb02 21398 builder->get_current_subfile ()->symtab
804d2729 21399 = allocate_symtab (cust,
c24bdb02 21400 builder->get_current_subfile ()->name);
43f3e411 21401 }
c24bdb02 21402 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21403 }
21404 }
c906108c
SS
21405}
21406
21407/* Start a subfile for DWARF. FILENAME is the name of the file and
21408 DIRNAME the name of the source directory which contains FILENAME
4d663531 21409 or NULL if not known.
c906108c
SS
21410 This routine tries to keep line numbers from identical absolute and
21411 relative file names in a common subfile.
21412
21413 Using the `list' example from the GDB testsuite, which resides in
21414 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21415 of /srcdir/list0.c yields the following debugging information for list0.c:
21416
c5aa993b 21417 DW_AT_name: /srcdir/list0.c
4d663531 21418 DW_AT_comp_dir: /compdir
357e46e7 21419 files.files[0].name: list0.h
c5aa993b 21420 files.files[0].dir: /srcdir
357e46e7 21421 files.files[1].name: list0.c
c5aa993b 21422 files.files[1].dir: /srcdir
c906108c
SS
21423
21424 The line number information for list0.c has to end up in a single
4f1520fb
FR
21425 subfile, so that `break /srcdir/list0.c:1' works as expected.
21426 start_subfile will ensure that this happens provided that we pass the
21427 concatenation of files.files[1].dir and files.files[1].name as the
21428 subfile's name. */
c906108c
SS
21429
21430static void
804d2729
TT
21431dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21432 const char *dirname)
c906108c 21433{
d521ce57 21434 char *copy = NULL;
4f1520fb 21435
4d663531 21436 /* In order not to lose the line information directory,
4f1520fb
FR
21437 we concatenate it to the filename when it makes sense.
21438 Note that the Dwarf3 standard says (speaking of filenames in line
21439 information): ``The directory index is ignored for file names
21440 that represent full path names''. Thus ignoring dirname in the
21441 `else' branch below isn't an issue. */
c906108c 21442
d5166ae1 21443 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21444 {
21445 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21446 filename = copy;
21447 }
c906108c 21448
c24bdb02 21449 cu->get_builder ()->start_subfile (filename);
4f1520fb 21450
d521ce57
TT
21451 if (copy != NULL)
21452 xfree (copy);
c906108c
SS
21453}
21454
804d2729
TT
21455/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21456 buildsym_compunit constructor. */
f4dc4d17 21457
c24bdb02
KS
21458struct compunit_symtab *
21459dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21460 CORE_ADDR low_pc)
f4dc4d17 21461{
c24bdb02 21462 gdb_assert (m_builder == nullptr);
43f3e411 21463
c24bdb02
KS
21464 m_builder.reset (new struct buildsym_compunit
21465 (per_cu->dwarf2_per_objfile->objfile,
21466 name, comp_dir, language, low_pc));
93b8bea4 21467
c24bdb02 21468 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21469
c24bdb02
KS
21470 get_builder ()->record_debugformat ("DWARF 2");
21471 get_builder ()->record_producer (producer);
f4dc4d17 21472
c24bdb02 21473 processing_has_namespace_info = false;
43f3e411 21474
c24bdb02 21475 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21476}
21477
4c2df51b
DJ
21478static void
21479var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21480 struct dwarf2_cu *cu)
4c2df51b 21481{
518817b3 21482 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21483 struct comp_unit_head *cu_header = &cu->header;
21484
4c2df51b
DJ
21485 /* NOTE drow/2003-01-30: There used to be a comment and some special
21486 code here to turn a symbol with DW_AT_external and a
21487 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21488 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21489 with some versions of binutils) where shared libraries could have
21490 relocations against symbols in their debug information - the
21491 minimal symbol would have the right address, but the debug info
21492 would not. It's no longer necessary, because we will explicitly
21493 apply relocations when we read in the debug information now. */
21494
21495 /* A DW_AT_location attribute with no contents indicates that a
21496 variable has been optimized away. */
21497 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21498 {
f1e6e072 21499 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21500 return;
21501 }
21502
21503 /* Handle one degenerate form of location expression specially, to
21504 preserve GDB's previous behavior when section offsets are
336d760d
AT
21505 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21506 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21507
21508 if (attr_form_is_block (attr)
3019eac3
DE
21509 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21510 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21511 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21512 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21513 && (DW_BLOCK (attr)->size
21514 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21515 {
891d2f0b 21516 unsigned int dummy;
4c2df51b 21517
3019eac3 21518 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21519 SET_SYMBOL_VALUE_ADDRESS (sym,
21520 read_address (objfile->obfd,
21521 DW_BLOCK (attr)->data + 1,
21522 cu, &dummy));
3019eac3 21523 else
38583298
TT
21524 SET_SYMBOL_VALUE_ADDRESS
21525 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21526 &dummy));
f1e6e072 21527 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21528 fixup_symbol_section (sym, objfile);
38583298
TT
21529 SET_SYMBOL_VALUE_ADDRESS (sym,
21530 SYMBOL_VALUE_ADDRESS (sym)
21531 + ANOFFSET (objfile->section_offsets,
21532 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21533 return;
21534 }
21535
21536 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21537 expression evaluator, and use LOC_COMPUTED only when necessary
21538 (i.e. when the value of a register or memory location is
21539 referenced, or a thread-local block, etc.). Then again, it might
21540 not be worthwhile. I'm assuming that it isn't unless performance
21541 or memory numbers show me otherwise. */
21542
f1e6e072 21543 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21544
f1e6e072 21545 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21546 cu->has_loclist = true;
4c2df51b
DJ
21547}
21548
c906108c
SS
21549/* Given a pointer to a DWARF information entry, figure out if we need
21550 to make a symbol table entry for it, and if so, create a new entry
21551 and return a pointer to it.
21552 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21553 used the passed type.
21554 If SPACE is not NULL, use it to hold the new symbol. If it is
21555 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21556
21557static struct symbol *
5e2db402
TT
21558new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21559 struct symbol *space)
c906108c 21560{
518817b3
SM
21561 struct dwarf2_per_objfile *dwarf2_per_objfile
21562 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21563 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21564 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21565 struct symbol *sym = NULL;
15d034d0 21566 const char *name;
c906108c
SS
21567 struct attribute *attr = NULL;
21568 struct attribute *attr2 = NULL;
e142c38c 21569 CORE_ADDR baseaddr;
e37fd15a
SW
21570 struct pending **list_to_add = NULL;
21571
edb3359d 21572 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21573
21574 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21575
94af9270 21576 name = dwarf2_name (die, cu);
c906108c
SS
21577 if (name)
21578 {
94af9270 21579 const char *linkagename;
34eaf542 21580 int suppress_add = 0;
94af9270 21581
34eaf542
TT
21582 if (space)
21583 sym = space;
21584 else
e623cf5d 21585 sym = allocate_symbol (objfile);
c906108c 21586 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21587
21588 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21589 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21590 linkagename = dwarf2_physname (name, die, cu);
21591 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21592
f55ee35c
JK
21593 /* Fortran does not have mangling standard and the mangling does differ
21594 between gfortran, iFort etc. */
21595 if (cu->language == language_fortran
b250c185 21596 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21597 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21598 dwarf2_full_name (name, die, cu),
29df156d 21599 NULL);
f55ee35c 21600
c906108c 21601 /* Default assumptions.
c5aa993b 21602 Use the passed type or decode it from the die. */
176620f1 21603 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21604 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21605 if (type != NULL)
21606 SYMBOL_TYPE (sym) = type;
21607 else
e7c27a73 21608 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21609 attr = dwarf2_attr (die,
21610 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21611 cu);
c906108c
SS
21612 if (attr)
21613 {
21614 SYMBOL_LINE (sym) = DW_UNSND (attr);
21615 }
cb1df416 21616
edb3359d
DJ
21617 attr = dwarf2_attr (die,
21618 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21619 cu);
cb1df416
DJ
21620 if (attr)
21621 {
ecfb656c 21622 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21623 struct file_entry *fe;
9a619af0 21624
ecfb656c
PA
21625 if (cu->line_header != NULL)
21626 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21627 else
21628 fe = NULL;
21629
21630 if (fe == NULL)
b98664d3 21631 complaint (_("file index out of range"));
8c43009f
PA
21632 else
21633 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21634 }
21635
c906108c
SS
21636 switch (die->tag)
21637 {
21638 case DW_TAG_label:
e142c38c 21639 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21640 if (attr)
3e29f34a
MR
21641 {
21642 CORE_ADDR addr;
21643
21644 addr = attr_value_as_address (attr);
21645 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21646 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21647 }
0f5238ed
TT
21648 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21649 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21650 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21651 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21652 break;
21653 case DW_TAG_subprogram:
21654 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21655 finish_block. */
f1e6e072 21656 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21657 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21658 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21659 || cu->language == language_ada
21660 || cu->language == language_fortran)
c906108c 21661 {
2cfa0c8d 21662 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21663 Ada and Fortran subprograms, whether marked external or
21664 not, are always stored as a global symbol, because we want
21665 to be able to access them globally. For instance, we want
21666 to be able to break on a nested subprogram without having
21667 to specify the context. */
c24bdb02 21668 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21669 }
21670 else
21671 {
e37fd15a 21672 list_to_add = cu->list_in_scope;
c906108c
SS
21673 }
21674 break;
edb3359d
DJ
21675 case DW_TAG_inlined_subroutine:
21676 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21677 finish_block. */
f1e6e072 21678 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21679 SYMBOL_INLINED (sym) = 1;
481860b3 21680 list_to_add = cu->list_in_scope;
edb3359d 21681 break;
34eaf542
TT
21682 case DW_TAG_template_value_param:
21683 suppress_add = 1;
21684 /* Fall through. */
72929c62 21685 case DW_TAG_constant:
c906108c 21686 case DW_TAG_variable:
254e6b9e 21687 case DW_TAG_member:
0963b4bd
MS
21688 /* Compilation with minimal debug info may result in
21689 variables with missing type entries. Change the
21690 misleading `void' type to something sensible. */
c906108c 21691 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21692 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21693
e142c38c 21694 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21695 /* In the case of DW_TAG_member, we should only be called for
21696 static const members. */
21697 if (die->tag == DW_TAG_member)
21698 {
3863f96c
DE
21699 /* dwarf2_add_field uses die_is_declaration,
21700 so we do the same. */
254e6b9e
DE
21701 gdb_assert (die_is_declaration (die, cu));
21702 gdb_assert (attr);
21703 }
c906108c
SS
21704 if (attr)
21705 {
e7c27a73 21706 dwarf2_const_value (attr, sym, cu);
e142c38c 21707 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21708 if (!suppress_add)
34eaf542
TT
21709 {
21710 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21711 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21712 else
e37fd15a 21713 list_to_add = cu->list_in_scope;
34eaf542 21714 }
c906108c
SS
21715 break;
21716 }
e142c38c 21717 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21718 if (attr)
21719 {
e7c27a73 21720 var_decode_location (attr, sym, cu);
e142c38c 21721 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21722
21723 /* Fortran explicitly imports any global symbols to the local
21724 scope by DW_TAG_common_block. */
21725 if (cu->language == language_fortran && die->parent
21726 && die->parent->tag == DW_TAG_common_block)
21727 attr2 = NULL;
21728
caac4577
JG
21729 if (SYMBOL_CLASS (sym) == LOC_STATIC
21730 && SYMBOL_VALUE_ADDRESS (sym) == 0
21731 && !dwarf2_per_objfile->has_section_at_zero)
21732 {
21733 /* When a static variable is eliminated by the linker,
21734 the corresponding debug information is not stripped
21735 out, but the variable address is set to null;
21736 do not add such variables into symbol table. */
21737 }
21738 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21739 {
4b610737
TT
21740 if (SYMBOL_CLASS (sym) == LOC_STATIC
21741 && (objfile->flags & OBJF_MAINLINE) == 0
21742 && dwarf2_per_objfile->can_copy)
21743 {
21744 /* A global static variable might be subject to
21745 copy relocation. We first check for a local
21746 minsym, though, because maybe the symbol was
21747 marked hidden, in which case this would not
21748 apply. */
21749 bound_minimal_symbol found
21750 = (lookup_minimal_symbol_linkage
21751 (SYMBOL_LINKAGE_NAME (sym), objfile));
21752 if (found.minsym != nullptr)
21753 sym->maybe_copied = 1;
21754 }
f55ee35c 21755
1c809c68
TT
21756 /* A variable with DW_AT_external is never static,
21757 but it may be block-scoped. */
804d2729 21758 list_to_add
c24bdb02
KS
21759 = ((cu->list_in_scope
21760 == cu->get_builder ()->get_file_symbols ())
21761 ? cu->get_builder ()->get_global_symbols ()
804d2729 21762 : cu->list_in_scope);
1c809c68 21763 }
c906108c 21764 else
e37fd15a 21765 list_to_add = cu->list_in_scope;
c906108c
SS
21766 }
21767 else
21768 {
21769 /* We do not know the address of this symbol.
c5aa993b
JM
21770 If it is an external symbol and we have type information
21771 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21772 The address of the variable will then be determined from
21773 the minimal symbol table whenever the variable is
21774 referenced. */
e142c38c 21775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21776
21777 /* Fortran explicitly imports any global symbols to the local
21778 scope by DW_TAG_common_block. */
21779 if (cu->language == language_fortran && die->parent
21780 && die->parent->tag == DW_TAG_common_block)
21781 {
21782 /* SYMBOL_CLASS doesn't matter here because
21783 read_common_block is going to reset it. */
21784 if (!suppress_add)
21785 list_to_add = cu->list_in_scope;
21786 }
21787 else if (attr2 && (DW_UNSND (attr2) != 0)
21788 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21789 {
0fe7935b
DJ
21790 /* A variable with DW_AT_external is never static, but it
21791 may be block-scoped. */
804d2729 21792 list_to_add
c24bdb02
KS
21793 = ((cu->list_in_scope
21794 == cu->get_builder ()->get_file_symbols ())
21795 ? cu->get_builder ()->get_global_symbols ()
804d2729 21796 : cu->list_in_scope);
0fe7935b 21797
f1e6e072 21798 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21799 }
442ddf59
JK
21800 else if (!die_is_declaration (die, cu))
21801 {
21802 /* Use the default LOC_OPTIMIZED_OUT class. */
21803 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21804 if (!suppress_add)
21805 list_to_add = cu->list_in_scope;
442ddf59 21806 }
c906108c
SS
21807 }
21808 break;
21809 case DW_TAG_formal_parameter:
a60f3166
TT
21810 {
21811 /* If we are inside a function, mark this as an argument. If
21812 not, we might be looking at an argument to an inlined function
21813 when we do not have enough information to show inlined frames;
21814 pretend it's a local variable in that case so that the user can
21815 still see it. */
804d2729 21816 struct context_stack *curr
c24bdb02 21817 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21818 if (curr != nullptr && curr->name != nullptr)
21819 SYMBOL_IS_ARGUMENT (sym) = 1;
21820 attr = dwarf2_attr (die, DW_AT_location, cu);
21821 if (attr)
21822 {
21823 var_decode_location (attr, sym, cu);
21824 }
21825 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21826 if (attr)
21827 {
21828 dwarf2_const_value (attr, sym, cu);
21829 }
f346a30d 21830
a60f3166
TT
21831 list_to_add = cu->list_in_scope;
21832 }
c906108c
SS
21833 break;
21834 case DW_TAG_unspecified_parameters:
21835 /* From varargs functions; gdb doesn't seem to have any
21836 interest in this information, so just ignore it for now.
21837 (FIXME?) */
21838 break;
34eaf542
TT
21839 case DW_TAG_template_type_param:
21840 suppress_add = 1;
21841 /* Fall through. */
c906108c 21842 case DW_TAG_class_type:
680b30c7 21843 case DW_TAG_interface_type:
c906108c
SS
21844 case DW_TAG_structure_type:
21845 case DW_TAG_union_type:
72019c9c 21846 case DW_TAG_set_type:
c906108c 21847 case DW_TAG_enumeration_type:
f1e6e072 21848 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21849 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21850
63d06c5c 21851 {
9c37b5ae 21852 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21853 really ever be static objects: otherwise, if you try
21854 to, say, break of a class's method and you're in a file
21855 which doesn't mention that class, it won't work unless
21856 the check for all static symbols in lookup_symbol_aux
21857 saves you. See the OtherFileClass tests in
21858 gdb.c++/namespace.exp. */
21859
e37fd15a 21860 if (!suppress_add)
34eaf542 21861 {
c24bdb02 21862 buildsym_compunit *builder = cu->get_builder ();
804d2729 21863 list_to_add
c24bdb02 21864 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21865 && cu->language == language_cplus
c24bdb02 21866 ? builder->get_global_symbols ()
804d2729 21867 : cu->list_in_scope);
63d06c5c 21868
64382290 21869 /* The semantics of C++ state that "struct foo {
9c37b5ae 21870 ... }" also defines a typedef for "foo". */
64382290 21871 if (cu->language == language_cplus
45280282 21872 || cu->language == language_ada
c44af4eb
TT
21873 || cu->language == language_d
21874 || cu->language == language_rust)
64382290
TT
21875 {
21876 /* The symbol's name is already allocated along
21877 with this objfile, so we don't need to
21878 duplicate it for the type. */
21879 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21880 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21881 }
63d06c5c
DC
21882 }
21883 }
c906108c
SS
21884 break;
21885 case DW_TAG_typedef:
f1e6e072 21886 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21887 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21888 list_to_add = cu->list_in_scope;
63d06c5c 21889 break;
c906108c 21890 case DW_TAG_base_type:
a02abb62 21891 case DW_TAG_subrange_type:
f1e6e072 21892 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21893 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21894 list_to_add = cu->list_in_scope;
c906108c
SS
21895 break;
21896 case DW_TAG_enumerator:
e142c38c 21897 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21898 if (attr)
21899 {
e7c27a73 21900 dwarf2_const_value (attr, sym, cu);
c906108c 21901 }
63d06c5c
DC
21902 {
21903 /* NOTE: carlton/2003-11-10: See comment above in the
21904 DW_TAG_class_type, etc. block. */
21905
804d2729 21906 list_to_add
c24bdb02 21907 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21908 && cu->language == language_cplus
c24bdb02 21909 ? cu->get_builder ()->get_global_symbols ()
804d2729 21910 : cu->list_in_scope);
63d06c5c 21911 }
c906108c 21912 break;
74921315 21913 case DW_TAG_imported_declaration:
5c4e30ca 21914 case DW_TAG_namespace:
f1e6e072 21915 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21916 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21917 break;
530e8392
KB
21918 case DW_TAG_module:
21919 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21920 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21921 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21922 break;
4357ac6c 21923 case DW_TAG_common_block:
f1e6e072 21924 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21925 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21926 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21927 break;
c906108c
SS
21928 default:
21929 /* Not a tag we recognize. Hopefully we aren't processing
21930 trash data, but since we must specifically ignore things
21931 we don't recognize, there is nothing else we should do at
0963b4bd 21932 this point. */
b98664d3 21933 complaint (_("unsupported tag: '%s'"),
4d3c2250 21934 dwarf_tag_name (die->tag));
c906108c
SS
21935 break;
21936 }
df8a16a1 21937
e37fd15a
SW
21938 if (suppress_add)
21939 {
21940 sym->hash_next = objfile->template_symbols;
21941 objfile->template_symbols = sym;
21942 list_to_add = NULL;
21943 }
21944
21945 if (list_to_add != NULL)
d3cb6808 21946 add_symbol_to_list (sym, list_to_add);
e37fd15a 21947
df8a16a1
DJ
21948 /* For the benefit of old versions of GCC, check for anonymous
21949 namespaces based on the demangled name. */
4d4ec4e5 21950 if (!cu->processing_has_namespace_info
94af9270 21951 && cu->language == language_cplus)
c24bdb02 21952 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21953 }
21954 return (sym);
21955}
21956
98bfdba5
PA
21957/* Given an attr with a DW_FORM_dataN value in host byte order,
21958 zero-extend it as appropriate for the symbol's type. The DWARF
21959 standard (v4) is not entirely clear about the meaning of using
21960 DW_FORM_dataN for a constant with a signed type, where the type is
21961 wider than the data. The conclusion of a discussion on the DWARF
21962 list was that this is unspecified. We choose to always zero-extend
21963 because that is the interpretation long in use by GCC. */
c906108c 21964
98bfdba5 21965static gdb_byte *
ff39bb5e 21966dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21967 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21968{
518817b3 21969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21970 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21971 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21972 LONGEST l = DW_UNSND (attr);
21973
21974 if (bits < sizeof (*value) * 8)
21975 {
21976 l &= ((LONGEST) 1 << bits) - 1;
21977 *value = l;
21978 }
21979 else if (bits == sizeof (*value) * 8)
21980 *value = l;
21981 else
21982 {
224c3ddb 21983 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21984 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21985 return bytes;
21986 }
21987
21988 return NULL;
21989}
21990
21991/* Read a constant value from an attribute. Either set *VALUE, or if
21992 the value does not fit in *VALUE, set *BYTES - either already
21993 allocated on the objfile obstack, or newly allocated on OBSTACK,
21994 or, set *BATON, if we translated the constant to a location
21995 expression. */
21996
21997static void
ff39bb5e 21998dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21999 const char *name, struct obstack *obstack,
22000 struct dwarf2_cu *cu,
d521ce57 22001 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22002 struct dwarf2_locexpr_baton **baton)
22003{
518817b3 22004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22005 struct comp_unit_head *cu_header = &cu->header;
c906108c 22006 struct dwarf_block *blk;
98bfdba5
PA
22007 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22008 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22009
22010 *value = 0;
22011 *bytes = NULL;
22012 *baton = NULL;
c906108c
SS
22013
22014 switch (attr->form)
22015 {
22016 case DW_FORM_addr:
336d760d 22017 case DW_FORM_addrx:
3019eac3 22018 case DW_FORM_GNU_addr_index:
ac56253d 22019 {
ac56253d
TT
22020 gdb_byte *data;
22021
98bfdba5
PA
22022 if (TYPE_LENGTH (type) != cu_header->addr_size)
22023 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22024 cu_header->addr_size,
98bfdba5 22025 TYPE_LENGTH (type));
ac56253d
TT
22026 /* Symbols of this form are reasonably rare, so we just
22027 piggyback on the existing location code rather than writing
22028 a new implementation of symbol_computed_ops. */
8d749320 22029 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22030 (*baton)->per_cu = cu->per_cu;
22031 gdb_assert ((*baton)->per_cu);
ac56253d 22032
98bfdba5 22033 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22034 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22035 (*baton)->data = data;
ac56253d
TT
22036
22037 data[0] = DW_OP_addr;
22038 store_unsigned_integer (&data[1], cu_header->addr_size,
22039 byte_order, DW_ADDR (attr));
22040 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22041 }
c906108c 22042 break;
4ac36638 22043 case DW_FORM_string:
93b5768b 22044 case DW_FORM_strp:
cf532bd1 22045 case DW_FORM_strx:
3019eac3 22046 case DW_FORM_GNU_str_index:
36586728 22047 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22048 /* DW_STRING is already allocated on the objfile obstack, point
22049 directly to it. */
d521ce57 22050 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22051 break;
c906108c
SS
22052 case DW_FORM_block1:
22053 case DW_FORM_block2:
22054 case DW_FORM_block4:
22055 case DW_FORM_block:
2dc7f7b3 22056 case DW_FORM_exprloc:
0224619f 22057 case DW_FORM_data16:
c906108c 22058 blk = DW_BLOCK (attr);
98bfdba5
PA
22059 if (TYPE_LENGTH (type) != blk->size)
22060 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22061 TYPE_LENGTH (type));
22062 *bytes = blk->data;
c906108c 22063 break;
2df3850c
JM
22064
22065 /* The DW_AT_const_value attributes are supposed to carry the
22066 symbol's value "represented as it would be on the target
22067 architecture." By the time we get here, it's already been
22068 converted to host endianness, so we just need to sign- or
22069 zero-extend it as appropriate. */
22070 case DW_FORM_data1:
3aef2284 22071 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22072 break;
c906108c 22073 case DW_FORM_data2:
3aef2284 22074 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22075 break;
c906108c 22076 case DW_FORM_data4:
3aef2284 22077 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22078 break;
c906108c 22079 case DW_FORM_data8:
3aef2284 22080 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22081 break;
22082
c906108c 22083 case DW_FORM_sdata:
663c44ac 22084 case DW_FORM_implicit_const:
98bfdba5 22085 *value = DW_SND (attr);
2df3850c
JM
22086 break;
22087
c906108c 22088 case DW_FORM_udata:
98bfdba5 22089 *value = DW_UNSND (attr);
c906108c 22090 break;
2df3850c 22091
c906108c 22092 default:
b98664d3 22093 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22094 dwarf_form_name (attr->form));
98bfdba5 22095 *value = 0;
c906108c
SS
22096 break;
22097 }
22098}
22099
2df3850c 22100
98bfdba5
PA
22101/* Copy constant value from an attribute to a symbol. */
22102
2df3850c 22103static void
ff39bb5e 22104dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22105 struct dwarf2_cu *cu)
2df3850c 22106{
518817b3 22107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22108 LONGEST value;
d521ce57 22109 const gdb_byte *bytes;
98bfdba5 22110 struct dwarf2_locexpr_baton *baton;
2df3850c 22111
98bfdba5
PA
22112 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22113 SYMBOL_PRINT_NAME (sym),
22114 &objfile->objfile_obstack, cu,
22115 &value, &bytes, &baton);
2df3850c 22116
98bfdba5
PA
22117 if (baton != NULL)
22118 {
98bfdba5 22119 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22120 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22121 }
22122 else if (bytes != NULL)
22123 {
22124 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22125 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22126 }
22127 else
22128 {
22129 SYMBOL_VALUE (sym) = value;
f1e6e072 22130 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22131 }
2df3850c
JM
22132}
22133
c906108c
SS
22134/* Return the type of the die in question using its DW_AT_type attribute. */
22135
22136static struct type *
e7c27a73 22137die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22138{
c906108c 22139 struct attribute *type_attr;
c906108c 22140
e142c38c 22141 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22142 if (!type_attr)
22143 {
518817b3 22144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22145 /* A missing DW_AT_type represents a void type. */
518817b3 22146 return objfile_type (objfile)->builtin_void;
c906108c 22147 }
348e048f 22148
673bfd45 22149 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22150}
22151
b4ba55a1
JB
22152/* True iff CU's producer generates GNAT Ada auxiliary information
22153 that allows to find parallel types through that information instead
22154 of having to do expensive parallel lookups by type name. */
22155
22156static int
22157need_gnat_info (struct dwarf2_cu *cu)
22158{
de4cb04a
JB
22159 /* Assume that the Ada compiler was GNAT, which always produces
22160 the auxiliary information. */
22161 return (cu->language == language_ada);
b4ba55a1
JB
22162}
22163
b4ba55a1
JB
22164/* Return the auxiliary type of the die in question using its
22165 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22166 attribute is not present. */
22167
22168static struct type *
22169die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22170{
b4ba55a1 22171 struct attribute *type_attr;
b4ba55a1
JB
22172
22173 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22174 if (!type_attr)
22175 return NULL;
22176
673bfd45 22177 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22178}
22179
22180/* If DIE has a descriptive_type attribute, then set the TYPE's
22181 descriptive type accordingly. */
22182
22183static void
22184set_descriptive_type (struct type *type, struct die_info *die,
22185 struct dwarf2_cu *cu)
22186{
22187 struct type *descriptive_type = die_descriptive_type (die, cu);
22188
22189 if (descriptive_type)
22190 {
22191 ALLOCATE_GNAT_AUX_TYPE (type);
22192 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22193 }
22194}
22195
c906108c
SS
22196/* Return the containing type of the die in question using its
22197 DW_AT_containing_type attribute. */
22198
22199static struct type *
e7c27a73 22200die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22201{
c906108c 22202 struct attribute *type_attr;
518817b3 22203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22204
e142c38c 22205 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22206 if (!type_attr)
22207 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22208 "[in module %s]"), objfile_name (objfile));
33ac96f0 22209
673bfd45 22210 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22211}
22212
ac9ec31b
DE
22213/* Return an error marker type to use for the ill formed type in DIE/CU. */
22214
22215static struct type *
22216build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22217{
518817b3
SM
22218 struct dwarf2_per_objfile *dwarf2_per_objfile
22219 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22220 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22221 char *saved;
ac9ec31b 22222
528e1572
SM
22223 std::string message
22224 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22225 objfile_name (objfile),
22226 sect_offset_str (cu->header.sect_off),
22227 sect_offset_str (die->sect_off));
efba19b0 22228 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22229
19f392bc 22230 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22231}
22232
673bfd45 22233/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22234 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22235 DW_AT_containing_type.
673bfd45
DE
22236 If there is no type substitute an error marker. */
22237
c906108c 22238static struct type *
ff39bb5e 22239lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22240 struct dwarf2_cu *cu)
c906108c 22241{
518817b3
SM
22242 struct dwarf2_per_objfile *dwarf2_per_objfile
22243 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22244 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22245 struct type *this_type;
22246
ac9ec31b
DE
22247 gdb_assert (attr->name == DW_AT_type
22248 || attr->name == DW_AT_GNAT_descriptive_type
22249 || attr->name == DW_AT_containing_type);
22250
673bfd45
DE
22251 /* First see if we have it cached. */
22252
36586728
TT
22253 if (attr->form == DW_FORM_GNU_ref_alt)
22254 {
22255 struct dwarf2_per_cu_data *per_cu;
9c541725 22256 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22257
ed2dc618
SM
22258 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22259 dwarf2_per_objfile);
9c541725 22260 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22261 }
7771576e 22262 else if (attr_form_is_ref (attr))
673bfd45 22263 {
9c541725 22264 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22265
9c541725 22266 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22267 }
55f1336d 22268 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22269 {
ac9ec31b 22270 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22271
ac9ec31b 22272 return get_signatured_type (die, signature, cu);
673bfd45
DE
22273 }
22274 else
22275 {
b98664d3 22276 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22277 " at %s [in module %s]"),
22278 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22279 objfile_name (objfile));
ac9ec31b 22280 return build_error_marker_type (cu, die);
673bfd45
DE
22281 }
22282
22283 /* If not cached we need to read it in. */
22284
22285 if (this_type == NULL)
22286 {
ac9ec31b 22287 struct die_info *type_die = NULL;
673bfd45
DE
22288 struct dwarf2_cu *type_cu = cu;
22289
7771576e 22290 if (attr_form_is_ref (attr))
ac9ec31b
DE
22291 type_die = follow_die_ref (die, attr, &type_cu);
22292 if (type_die == NULL)
22293 return build_error_marker_type (cu, die);
22294 /* If we find the type now, it's probably because the type came
3019eac3
DE
22295 from an inter-CU reference and the type's CU got expanded before
22296 ours. */
ac9ec31b 22297 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22298 }
22299
22300 /* If we still don't have a type use an error marker. */
22301
22302 if (this_type == NULL)
ac9ec31b 22303 return build_error_marker_type (cu, die);
673bfd45 22304
f792889a 22305 return this_type;
c906108c
SS
22306}
22307
673bfd45
DE
22308/* Return the type in DIE, CU.
22309 Returns NULL for invalid types.
22310
02142a6c 22311 This first does a lookup in die_type_hash,
673bfd45
DE
22312 and only reads the die in if necessary.
22313
22314 NOTE: This can be called when reading in partial or full symbols. */
22315
f792889a 22316static struct type *
e7c27a73 22317read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22318{
f792889a
DJ
22319 struct type *this_type;
22320
22321 this_type = get_die_type (die, cu);
22322 if (this_type)
22323 return this_type;
22324
673bfd45
DE
22325 return read_type_die_1 (die, cu);
22326}
22327
22328/* Read the type in DIE, CU.
22329 Returns NULL for invalid types. */
22330
22331static struct type *
22332read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22333{
22334 struct type *this_type = NULL;
22335
c906108c
SS
22336 switch (die->tag)
22337 {
22338 case DW_TAG_class_type:
680b30c7 22339 case DW_TAG_interface_type:
c906108c
SS
22340 case DW_TAG_structure_type:
22341 case DW_TAG_union_type:
f792889a 22342 this_type = read_structure_type (die, cu);
c906108c
SS
22343 break;
22344 case DW_TAG_enumeration_type:
f792889a 22345 this_type = read_enumeration_type (die, cu);
c906108c
SS
22346 break;
22347 case DW_TAG_subprogram:
22348 case DW_TAG_subroutine_type:
edb3359d 22349 case DW_TAG_inlined_subroutine:
f792889a 22350 this_type = read_subroutine_type (die, cu);
c906108c
SS
22351 break;
22352 case DW_TAG_array_type:
f792889a 22353 this_type = read_array_type (die, cu);
c906108c 22354 break;
72019c9c 22355 case DW_TAG_set_type:
f792889a 22356 this_type = read_set_type (die, cu);
72019c9c 22357 break;
c906108c 22358 case DW_TAG_pointer_type:
f792889a 22359 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22360 break;
22361 case DW_TAG_ptr_to_member_type:
f792889a 22362 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22363 break;
22364 case DW_TAG_reference_type:
4297a3f0
AV
22365 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22366 break;
22367 case DW_TAG_rvalue_reference_type:
22368 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22369 break;
22370 case DW_TAG_const_type:
f792889a 22371 this_type = read_tag_const_type (die, cu);
c906108c
SS
22372 break;
22373 case DW_TAG_volatile_type:
f792889a 22374 this_type = read_tag_volatile_type (die, cu);
c906108c 22375 break;
06d66ee9
TT
22376 case DW_TAG_restrict_type:
22377 this_type = read_tag_restrict_type (die, cu);
22378 break;
c906108c 22379 case DW_TAG_string_type:
f792889a 22380 this_type = read_tag_string_type (die, cu);
c906108c
SS
22381 break;
22382 case DW_TAG_typedef:
f792889a 22383 this_type = read_typedef (die, cu);
c906108c 22384 break;
a02abb62 22385 case DW_TAG_subrange_type:
f792889a 22386 this_type = read_subrange_type (die, cu);
a02abb62 22387 break;
c906108c 22388 case DW_TAG_base_type:
f792889a 22389 this_type = read_base_type (die, cu);
c906108c 22390 break;
81a17f79 22391 case DW_TAG_unspecified_type:
f792889a 22392 this_type = read_unspecified_type (die, cu);
81a17f79 22393 break;
0114d602
DJ
22394 case DW_TAG_namespace:
22395 this_type = read_namespace_type (die, cu);
22396 break;
f55ee35c
JK
22397 case DW_TAG_module:
22398 this_type = read_module_type (die, cu);
22399 break;
a2c2acaf
MW
22400 case DW_TAG_atomic_type:
22401 this_type = read_tag_atomic_type (die, cu);
22402 break;
c906108c 22403 default:
b98664d3 22404 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22405 dwarf_tag_name (die->tag));
c906108c
SS
22406 break;
22407 }
63d06c5c 22408
f792889a 22409 return this_type;
63d06c5c
DC
22410}
22411
abc72ce4
DE
22412/* See if we can figure out if the class lives in a namespace. We do
22413 this by looking for a member function; its demangled name will
22414 contain namespace info, if there is any.
22415 Return the computed name or NULL.
22416 Space for the result is allocated on the objfile's obstack.
22417 This is the full-die version of guess_partial_die_structure_name.
22418 In this case we know DIE has no useful parent. */
22419
22420static char *
22421guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22422{
22423 struct die_info *spec_die;
22424 struct dwarf2_cu *spec_cu;
22425 struct die_info *child;
518817b3 22426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22427
22428 spec_cu = cu;
22429 spec_die = die_specification (die, &spec_cu);
22430 if (spec_die != NULL)
22431 {
22432 die = spec_die;
22433 cu = spec_cu;
22434 }
22435
22436 for (child = die->child;
22437 child != NULL;
22438 child = child->sibling)
22439 {
22440 if (child->tag == DW_TAG_subprogram)
22441 {
73b9be8b 22442 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22443
7d45c7c3 22444 if (linkage_name != NULL)
abc72ce4
DE
22445 {
22446 char *actual_name
22447 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22448 linkage_name);
abc72ce4
DE
22449 char *name = NULL;
22450
22451 if (actual_name != NULL)
22452 {
15d034d0 22453 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22454
22455 if (die_name != NULL
22456 && strcmp (die_name, actual_name) != 0)
22457 {
22458 /* Strip off the class name from the full name.
22459 We want the prefix. */
22460 int die_name_len = strlen (die_name);
22461 int actual_name_len = strlen (actual_name);
22462
22463 /* Test for '::' as a sanity check. */
22464 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22465 && actual_name[actual_name_len
22466 - die_name_len - 1] == ':')
0cf9feb9 22467 name = obstack_strndup (
e3b94546 22468 &objfile->per_bfd->storage_obstack,
224c3ddb 22469 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22470 }
22471 }
22472 xfree (actual_name);
22473 return name;
22474 }
22475 }
22476 }
22477
22478 return NULL;
22479}
22480
96408a79
SA
22481/* GCC might emit a nameless typedef that has a linkage name. Determine the
22482 prefix part in such case. See
22483 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22484
a121b7c1 22485static const char *
96408a79
SA
22486anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22487{
22488 struct attribute *attr;
e6a959d6 22489 const char *base;
96408a79
SA
22490
22491 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22492 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22493 return NULL;
22494
7d45c7c3 22495 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22496 return NULL;
22497
73b9be8b 22498 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22499 if (attr == NULL || DW_STRING (attr) == NULL)
22500 return NULL;
22501
22502 /* dwarf2_name had to be already called. */
22503 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22504
22505 /* Strip the base name, keep any leading namespaces/classes. */
22506 base = strrchr (DW_STRING (attr), ':');
22507 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22508 return "";
22509
518817b3 22510 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22511 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22512 DW_STRING (attr),
22513 &base[-1] - DW_STRING (attr));
96408a79
SA
22514}
22515
fdde2d81 22516/* Return the name of the namespace/class that DIE is defined within,
0114d602 22517 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22518
0114d602
DJ
22519 For example, if we're within the method foo() in the following
22520 code:
22521
22522 namespace N {
22523 class C {
22524 void foo () {
22525 }
22526 };
22527 }
22528
22529 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22530
0d5cff50 22531static const char *
e142c38c 22532determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22533{
518817b3
SM
22534 struct dwarf2_per_objfile *dwarf2_per_objfile
22535 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22536 struct die_info *parent, *spec_die;
22537 struct dwarf2_cu *spec_cu;
22538 struct type *parent_type;
a121b7c1 22539 const char *retval;
63d06c5c 22540
9c37b5ae 22541 if (cu->language != language_cplus
c44af4eb
TT
22542 && cu->language != language_fortran && cu->language != language_d
22543 && cu->language != language_rust)
0114d602
DJ
22544 return "";
22545
96408a79
SA
22546 retval = anonymous_struct_prefix (die, cu);
22547 if (retval)
22548 return retval;
22549
0114d602
DJ
22550 /* We have to be careful in the presence of DW_AT_specification.
22551 For example, with GCC 3.4, given the code
22552
22553 namespace N {
22554 void foo() {
22555 // Definition of N::foo.
22556 }
22557 }
22558
22559 then we'll have a tree of DIEs like this:
22560
22561 1: DW_TAG_compile_unit
22562 2: DW_TAG_namespace // N
22563 3: DW_TAG_subprogram // declaration of N::foo
22564 4: DW_TAG_subprogram // definition of N::foo
22565 DW_AT_specification // refers to die #3
22566
22567 Thus, when processing die #4, we have to pretend that we're in
22568 the context of its DW_AT_specification, namely the contex of die
22569 #3. */
22570 spec_cu = cu;
22571 spec_die = die_specification (die, &spec_cu);
22572 if (spec_die == NULL)
22573 parent = die->parent;
22574 else
63d06c5c 22575 {
0114d602
DJ
22576 parent = spec_die->parent;
22577 cu = spec_cu;
63d06c5c 22578 }
0114d602
DJ
22579
22580 if (parent == NULL)
22581 return "";
98bfdba5
PA
22582 else if (parent->building_fullname)
22583 {
22584 const char *name;
22585 const char *parent_name;
22586
22587 /* It has been seen on RealView 2.2 built binaries,
22588 DW_TAG_template_type_param types actually _defined_ as
22589 children of the parent class:
22590
22591 enum E {};
22592 template class <class Enum> Class{};
22593 Class<enum E> class_e;
22594
22595 1: DW_TAG_class_type (Class)
22596 2: DW_TAG_enumeration_type (E)
22597 3: DW_TAG_enumerator (enum1:0)
22598 3: DW_TAG_enumerator (enum2:1)
22599 ...
22600 2: DW_TAG_template_type_param
22601 DW_AT_type DW_FORM_ref_udata (E)
22602
22603 Besides being broken debug info, it can put GDB into an
22604 infinite loop. Consider:
22605
22606 When we're building the full name for Class<E>, we'll start
22607 at Class, and go look over its template type parameters,
22608 finding E. We'll then try to build the full name of E, and
22609 reach here. We're now trying to build the full name of E,
22610 and look over the parent DIE for containing scope. In the
22611 broken case, if we followed the parent DIE of E, we'd again
22612 find Class, and once again go look at its template type
22613 arguments, etc., etc. Simply don't consider such parent die
22614 as source-level parent of this die (it can't be, the language
22615 doesn't allow it), and break the loop here. */
22616 name = dwarf2_name (die, cu);
22617 parent_name = dwarf2_name (parent, cu);
b98664d3 22618 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22619 name ? name : "<unknown>",
22620 parent_name ? parent_name : "<unknown>");
22621 return "";
22622 }
63d06c5c 22623 else
0114d602
DJ
22624 switch (parent->tag)
22625 {
63d06c5c 22626 case DW_TAG_namespace:
0114d602 22627 parent_type = read_type_die (parent, cu);
acebe513
UW
22628 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22629 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22630 Work around this problem here. */
22631 if (cu->language == language_cplus
e86ca25f 22632 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22633 return "";
0114d602 22634 /* We give a name to even anonymous namespaces. */
e86ca25f 22635 return TYPE_NAME (parent_type);
63d06c5c 22636 case DW_TAG_class_type:
680b30c7 22637 case DW_TAG_interface_type:
63d06c5c 22638 case DW_TAG_structure_type:
0114d602 22639 case DW_TAG_union_type:
f55ee35c 22640 case DW_TAG_module:
0114d602 22641 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22642 if (TYPE_NAME (parent_type) != NULL)
22643 return TYPE_NAME (parent_type);
0114d602
DJ
22644 else
22645 /* An anonymous structure is only allowed non-static data
22646 members; no typedefs, no member functions, et cetera.
22647 So it does not need a prefix. */
22648 return "";
abc72ce4 22649 case DW_TAG_compile_unit:
95554aad 22650 case DW_TAG_partial_unit:
abc72ce4
DE
22651 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22652 if (cu->language == language_cplus
fd5866f6 22653 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22654 && die->child != NULL
22655 && (die->tag == DW_TAG_class_type
22656 || die->tag == DW_TAG_structure_type
22657 || die->tag == DW_TAG_union_type))
22658 {
22659 char *name = guess_full_die_structure_name (die, cu);
22660 if (name != NULL)
22661 return name;
22662 }
22663 return "";
0a4b0913
AB
22664 case DW_TAG_subprogram:
22665 /* Nested subroutines in Fortran get a prefix with the name
22666 of the parent's subroutine. */
22667 if (cu->language == language_fortran)
22668 {
22669 if ((die->tag == DW_TAG_subprogram)
22670 && (dwarf2_name (parent, cu) != NULL))
22671 return dwarf2_name (parent, cu);
22672 }
22673 return determine_prefix (parent, cu);
3d567982
TT
22674 case DW_TAG_enumeration_type:
22675 parent_type = read_type_die (parent, cu);
22676 if (TYPE_DECLARED_CLASS (parent_type))
22677 {
e86ca25f
TT
22678 if (TYPE_NAME (parent_type) != NULL)
22679 return TYPE_NAME (parent_type);
3d567982
TT
22680 return "";
22681 }
22682 /* Fall through. */
63d06c5c 22683 default:
8176b9b8 22684 return determine_prefix (parent, cu);
63d06c5c 22685 }
63d06c5c
DC
22686}
22687
3e43a32a
MS
22688/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22689 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22690 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22691 an obconcat, otherwise allocate storage for the result. The CU argument is
22692 used to determine the language and hence, the appropriate separator. */
987504bb 22693
f55ee35c 22694#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22695
22696static char *
f55ee35c
JK
22697typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22698 int physname, struct dwarf2_cu *cu)
63d06c5c 22699{
f55ee35c 22700 const char *lead = "";
5c315b68 22701 const char *sep;
63d06c5c 22702
3e43a32a
MS
22703 if (suffix == NULL || suffix[0] == '\0'
22704 || prefix == NULL || prefix[0] == '\0')
987504bb 22705 sep = "";
45280282
IB
22706 else if (cu->language == language_d)
22707 {
22708 /* For D, the 'main' function could be defined in any module, but it
22709 should never be prefixed. */
22710 if (strcmp (suffix, "D main") == 0)
22711 {
22712 prefix = "";
22713 sep = "";
22714 }
22715 else
22716 sep = ".";
22717 }
f55ee35c
JK
22718 else if (cu->language == language_fortran && physname)
22719 {
22720 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22721 DW_AT_MIPS_linkage_name is preferred and used instead. */
22722
22723 lead = "__";
22724 sep = "_MOD_";
22725 }
987504bb
JJ
22726 else
22727 sep = "::";
63d06c5c 22728
6dd47d34
DE
22729 if (prefix == NULL)
22730 prefix = "";
22731 if (suffix == NULL)
22732 suffix = "";
22733
987504bb
JJ
22734 if (obs == NULL)
22735 {
3e43a32a 22736 char *retval
224c3ddb
SM
22737 = ((char *)
22738 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22739
f55ee35c
JK
22740 strcpy (retval, lead);
22741 strcat (retval, prefix);
6dd47d34
DE
22742 strcat (retval, sep);
22743 strcat (retval, suffix);
63d06c5c
DC
22744 return retval;
22745 }
987504bb
JJ
22746 else
22747 {
22748 /* We have an obstack. */
f55ee35c 22749 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22750 }
63d06c5c
DC
22751}
22752
c906108c
SS
22753/* Return sibling of die, NULL if no sibling. */
22754
f9aca02d 22755static struct die_info *
fba45db2 22756sibling_die (struct die_info *die)
c906108c 22757{
639d11d3 22758 return die->sibling;
c906108c
SS
22759}
22760
71c25dea
TT
22761/* Get name of a die, return NULL if not found. */
22762
15d034d0
TT
22763static const char *
22764dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22765 struct obstack *obstack)
22766{
22767 if (name && cu->language == language_cplus)
22768 {
2f408ecb 22769 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22770
2f408ecb 22771 if (!canon_name.empty ())
71c25dea 22772 {
2f408ecb 22773 if (canon_name != name)
efba19b0 22774 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22775 }
22776 }
22777
22778 return name;
c906108c
SS
22779}
22780
96553a0c
DE
22781/* Get name of a die, return NULL if not found.
22782 Anonymous namespaces are converted to their magic string. */
9219021c 22783
15d034d0 22784static const char *
e142c38c 22785dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22786{
22787 struct attribute *attr;
518817b3 22788 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22789
e142c38c 22790 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22791 if ((!attr || !DW_STRING (attr))
96553a0c 22792 && die->tag != DW_TAG_namespace
53832f31
TT
22793 && die->tag != DW_TAG_class_type
22794 && die->tag != DW_TAG_interface_type
22795 && die->tag != DW_TAG_structure_type
22796 && die->tag != DW_TAG_union_type)
71c25dea
TT
22797 return NULL;
22798
22799 switch (die->tag)
22800 {
22801 case DW_TAG_compile_unit:
95554aad 22802 case DW_TAG_partial_unit:
71c25dea
TT
22803 /* Compilation units have a DW_AT_name that is a filename, not
22804 a source language identifier. */
22805 case DW_TAG_enumeration_type:
22806 case DW_TAG_enumerator:
22807 /* These tags always have simple identifiers already; no need
22808 to canonicalize them. */
22809 return DW_STRING (attr);
907af001 22810
96553a0c
DE
22811 case DW_TAG_namespace:
22812 if (attr != NULL && DW_STRING (attr) != NULL)
22813 return DW_STRING (attr);
22814 return CP_ANONYMOUS_NAMESPACE_STR;
22815
907af001
UW
22816 case DW_TAG_class_type:
22817 case DW_TAG_interface_type:
22818 case DW_TAG_structure_type:
22819 case DW_TAG_union_type:
22820 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22821 structures or unions. These were of the form "._%d" in GCC 4.1,
22822 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22823 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22824 if (attr && DW_STRING (attr)
61012eef
GB
22825 && (startswith (DW_STRING (attr), "._")
22826 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22827 return NULL;
53832f31
TT
22828
22829 /* GCC might emit a nameless typedef that has a linkage name. See
22830 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22831 if (!attr || DW_STRING (attr) == NULL)
22832 {
df5c6c50 22833 char *demangled = NULL;
53832f31 22834
73b9be8b 22835 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22836 if (attr == NULL || DW_STRING (attr) == NULL)
22837 return NULL;
22838
df5c6c50
JK
22839 /* Avoid demangling DW_STRING (attr) the second time on a second
22840 call for the same DIE. */
22841 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22842 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22843
22844 if (demangled)
22845 {
e6a959d6 22846 const char *base;
96408a79 22847
53832f31 22848 /* FIXME: we already did this for the partial symbol... */
34a68019 22849 DW_STRING (attr)
021887d8
TT
22850 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22851 demangled);
53832f31
TT
22852 DW_STRING_IS_CANONICAL (attr) = 1;
22853 xfree (demangled);
96408a79
SA
22854
22855 /* Strip any leading namespaces/classes, keep only the base name.
22856 DW_AT_name for named DIEs does not contain the prefixes. */
22857 base = strrchr (DW_STRING (attr), ':');
22858 if (base && base > DW_STRING (attr) && base[-1] == ':')
22859 return &base[1];
22860 else
22861 return DW_STRING (attr);
53832f31
TT
22862 }
22863 }
907af001
UW
22864 break;
22865
71c25dea 22866 default:
907af001
UW
22867 break;
22868 }
22869
22870 if (!DW_STRING_IS_CANONICAL (attr))
22871 {
22872 DW_STRING (attr)
22873 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22874 &objfile->per_bfd->storage_obstack);
907af001 22875 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22876 }
907af001 22877 return DW_STRING (attr);
9219021c
DC
22878}
22879
22880/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22881 is none. *EXT_CU is the CU containing DIE on input, and the CU
22882 containing the return value on output. */
9219021c
DC
22883
22884static struct die_info *
f2f0e013 22885dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22886{
22887 struct attribute *attr;
9219021c 22888
f2f0e013 22889 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22890 if (attr == NULL)
22891 return NULL;
22892
f2f0e013 22893 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22894}
22895
fa9c3fa0
TT
22896/* A convenience function that returns an "unknown" DWARF name,
22897 including the value of V. STR is the name of the entity being
22898 printed, e.g., "TAG". */
22899
22900static const char *
22901dwarf_unknown (const char *str, unsigned v)
22902{
22903 char *cell = get_print_cell ();
22904 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22905 return cell;
22906}
22907
c906108c
SS
22908/* Convert a DIE tag into its string name. */
22909
f39c6ffd 22910static const char *
aa1ee363 22911dwarf_tag_name (unsigned tag)
c906108c 22912{
f39c6ffd
TT
22913 const char *name = get_DW_TAG_name (tag);
22914
22915 if (name == NULL)
fa9c3fa0 22916 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22917
22918 return name;
c906108c
SS
22919}
22920
22921/* Convert a DWARF attribute code into its string name. */
22922
f39c6ffd 22923static const char *
aa1ee363 22924dwarf_attr_name (unsigned attr)
c906108c 22925{
f39c6ffd
TT
22926 const char *name;
22927
c764a876 22928#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22929 if (attr == DW_AT_MIPS_fde)
22930 return "DW_AT_MIPS_fde";
22931#else
22932 if (attr == DW_AT_HP_block_index)
22933 return "DW_AT_HP_block_index";
c764a876 22934#endif
f39c6ffd
TT
22935
22936 name = get_DW_AT_name (attr);
22937
22938 if (name == NULL)
fa9c3fa0 22939 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22940
22941 return name;
c906108c
SS
22942}
22943
a084a2a6
AT
22944/* Convert a unit type to corresponding DW_UT name. */
22945
22946static const char *
22947dwarf_unit_type_name (int unit_type) {
22948 switch (unit_type)
22949 {
22950 case 0x01:
22951 return "DW_UT_compile (0x01)";
22952 case 0x02:
22953 return "DW_UT_type (0x02)";
22954 case 0x03:
22955 return "DW_UT_partial (0x03)";
22956 case 0x04:
22957 return "DW_UT_skeleton (0x04)";
22958 case 0x05:
22959 return "DW_UT_split_compile (0x05)";
22960 case 0x06:
22961 return "DW_UT_split_type (0x06)";
22962 case 0x80:
22963 return "DW_UT_lo_user (0x80)";
22964 case 0xff:
22965 return "DW_UT_hi_user (0xff)";
22966 default:
22967 return nullptr;
22968 }
22969}
22970
c906108c
SS
22971/* Convert a DWARF value form code into its string name. */
22972
f39c6ffd 22973static const char *
aa1ee363 22974dwarf_form_name (unsigned form)
c906108c 22975{
f39c6ffd
TT
22976 const char *name = get_DW_FORM_name (form);
22977
22978 if (name == NULL)
fa9c3fa0 22979 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22980
22981 return name;
c906108c
SS
22982}
22983
a121b7c1 22984static const char *
fba45db2 22985dwarf_bool_name (unsigned mybool)
c906108c
SS
22986{
22987 if (mybool)
22988 return "TRUE";
22989 else
22990 return "FALSE";
22991}
22992
22993/* Convert a DWARF type code into its string name. */
22994
f39c6ffd 22995static const char *
aa1ee363 22996dwarf_type_encoding_name (unsigned enc)
c906108c 22997{
f39c6ffd 22998 const char *name = get_DW_ATE_name (enc);
c906108c 22999
f39c6ffd 23000 if (name == NULL)
fa9c3fa0 23001 return dwarf_unknown ("ATE", enc);
c906108c 23002
f39c6ffd 23003 return name;
c906108c 23004}
c906108c 23005
f9aca02d 23006static void
d97bc12b 23007dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23008{
23009 unsigned int i;
23010
d97bc12b 23011 print_spaces (indent, f);
9d8780f0 23012 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23013 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23014 sect_offset_str (die->sect_off));
d97bc12b
DE
23015
23016 if (die->parent != NULL)
23017 {
23018 print_spaces (indent, f);
9d8780f0
SM
23019 fprintf_unfiltered (f, " parent at offset: %s\n",
23020 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23021 }
23022
23023 print_spaces (indent, f);
23024 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23025 dwarf_bool_name (die->child != NULL));
c906108c 23026
d97bc12b
DE
23027 print_spaces (indent, f);
23028 fprintf_unfiltered (f, " attributes:\n");
23029
c906108c
SS
23030 for (i = 0; i < die->num_attrs; ++i)
23031 {
d97bc12b
DE
23032 print_spaces (indent, f);
23033 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23034 dwarf_attr_name (die->attrs[i].name),
23035 dwarf_form_name (die->attrs[i].form));
d97bc12b 23036
c906108c
SS
23037 switch (die->attrs[i].form)
23038 {
c906108c 23039 case DW_FORM_addr:
336d760d 23040 case DW_FORM_addrx:
3019eac3 23041 case DW_FORM_GNU_addr_index:
d97bc12b 23042 fprintf_unfiltered (f, "address: ");
5af949e3 23043 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23044 break;
23045 case DW_FORM_block2:
23046 case DW_FORM_block4:
23047 case DW_FORM_block:
23048 case DW_FORM_block1:
56eb65bd
SP
23049 fprintf_unfiltered (f, "block: size %s",
23050 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23051 break;
2dc7f7b3 23052 case DW_FORM_exprloc:
56eb65bd
SP
23053 fprintf_unfiltered (f, "expression: size %s",
23054 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23055 break;
0224619f
JK
23056 case DW_FORM_data16:
23057 fprintf_unfiltered (f, "constant of 16 bytes");
23058 break;
4568ecf9
DE
23059 case DW_FORM_ref_addr:
23060 fprintf_unfiltered (f, "ref address: ");
23061 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23062 break;
36586728
TT
23063 case DW_FORM_GNU_ref_alt:
23064 fprintf_unfiltered (f, "alt ref address: ");
23065 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23066 break;
10b3939b
DJ
23067 case DW_FORM_ref1:
23068 case DW_FORM_ref2:
23069 case DW_FORM_ref4:
4568ecf9
DE
23070 case DW_FORM_ref8:
23071 case DW_FORM_ref_udata:
d97bc12b 23072 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23073 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23074 break;
c906108c
SS
23075 case DW_FORM_data1:
23076 case DW_FORM_data2:
23077 case DW_FORM_data4:
ce5d95e1 23078 case DW_FORM_data8:
c906108c
SS
23079 case DW_FORM_udata:
23080 case DW_FORM_sdata:
43bbcdc2
PH
23081 fprintf_unfiltered (f, "constant: %s",
23082 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23083 break;
2dc7f7b3
TT
23084 case DW_FORM_sec_offset:
23085 fprintf_unfiltered (f, "section offset: %s",
23086 pulongest (DW_UNSND (&die->attrs[i])));
23087 break;
55f1336d 23088 case DW_FORM_ref_sig8:
ac9ec31b
DE
23089 fprintf_unfiltered (f, "signature: %s",
23090 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23091 break;
c906108c 23092 case DW_FORM_string:
4bdf3d34 23093 case DW_FORM_strp:
43988095 23094 case DW_FORM_line_strp:
cf532bd1 23095 case DW_FORM_strx:
3019eac3 23096 case DW_FORM_GNU_str_index:
36586728 23097 case DW_FORM_GNU_strp_alt:
8285870a 23098 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23099 DW_STRING (&die->attrs[i])
8285870a
JK
23100 ? DW_STRING (&die->attrs[i]) : "",
23101 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23102 break;
23103 case DW_FORM_flag:
23104 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23105 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23106 else
d97bc12b 23107 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23108 break;
2dc7f7b3
TT
23109 case DW_FORM_flag_present:
23110 fprintf_unfiltered (f, "flag: TRUE");
23111 break;
a8329558 23112 case DW_FORM_indirect:
0963b4bd
MS
23113 /* The reader will have reduced the indirect form to
23114 the "base form" so this form should not occur. */
5f48f8f3 23115 fprintf_unfiltered (f,
3e43a32a 23116 "unexpected attribute form: DW_FORM_indirect");
a8329558 23117 break;
663c44ac
JK
23118 case DW_FORM_implicit_const:
23119 fprintf_unfiltered (f, "constant: %s",
23120 plongest (DW_SND (&die->attrs[i])));
23121 break;
c906108c 23122 default:
d97bc12b 23123 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23124 die->attrs[i].form);
d97bc12b 23125 break;
c906108c 23126 }
d97bc12b 23127 fprintf_unfiltered (f, "\n");
c906108c
SS
23128 }
23129}
23130
f9aca02d 23131static void
d97bc12b 23132dump_die_for_error (struct die_info *die)
c906108c 23133{
d97bc12b
DE
23134 dump_die_shallow (gdb_stderr, 0, die);
23135}
23136
23137static void
23138dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23139{
23140 int indent = level * 4;
23141
23142 gdb_assert (die != NULL);
23143
23144 if (level >= max_level)
23145 return;
23146
23147 dump_die_shallow (f, indent, die);
23148
23149 if (die->child != NULL)
c906108c 23150 {
d97bc12b
DE
23151 print_spaces (indent, f);
23152 fprintf_unfiltered (f, " Children:");
23153 if (level + 1 < max_level)
23154 {
23155 fprintf_unfiltered (f, "\n");
23156 dump_die_1 (f, level + 1, max_level, die->child);
23157 }
23158 else
23159 {
3e43a32a
MS
23160 fprintf_unfiltered (f,
23161 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23162 }
23163 }
23164
23165 if (die->sibling != NULL && level > 0)
23166 {
23167 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23168 }
23169}
23170
d97bc12b
DE
23171/* This is called from the pdie macro in gdbinit.in.
23172 It's not static so gcc will keep a copy callable from gdb. */
23173
23174void
23175dump_die (struct die_info *die, int max_level)
23176{
23177 dump_die_1 (gdb_stdlog, 0, max_level, die);
23178}
23179
f9aca02d 23180static void
51545339 23181store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23182{
51545339 23183 void **slot;
c906108c 23184
9c541725
PA
23185 slot = htab_find_slot_with_hash (cu->die_hash, die,
23186 to_underlying (die->sect_off),
b64f50a1 23187 INSERT);
51545339
DJ
23188
23189 *slot = die;
c906108c
SS
23190}
23191
b64f50a1
JK
23192/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23193 required kind. */
23194
23195static sect_offset
ff39bb5e 23196dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23197{
7771576e 23198 if (attr_form_is_ref (attr))
9c541725 23199 return (sect_offset) DW_UNSND (attr);
93311388 23200
b98664d3 23201 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23202 dwarf_form_name (attr->form));
9c541725 23203 return {};
c906108c
SS
23204}
23205
43bbcdc2
PH
23206/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23207 * the value held by the attribute is not constant. */
a02abb62 23208
43bbcdc2 23209static LONGEST
ff39bb5e 23210dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23211{
663c44ac 23212 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23213 return DW_SND (attr);
23214 else if (attr->form == DW_FORM_udata
23215 || attr->form == DW_FORM_data1
23216 || attr->form == DW_FORM_data2
23217 || attr->form == DW_FORM_data4
23218 || attr->form == DW_FORM_data8)
23219 return DW_UNSND (attr);
23220 else
23221 {
0224619f 23222 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23223 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23224 dwarf_form_name (attr->form));
23225 return default_value;
23226 }
23227}
23228
348e048f
DE
23229/* Follow reference or signature attribute ATTR of SRC_DIE.
23230 On entry *REF_CU is the CU of SRC_DIE.
23231 On exit *REF_CU is the CU of the result. */
23232
23233static struct die_info *
ff39bb5e 23234follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23235 struct dwarf2_cu **ref_cu)
23236{
23237 struct die_info *die;
23238
7771576e 23239 if (attr_form_is_ref (attr))
348e048f 23240 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23241 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23242 die = follow_die_sig (src_die, attr, ref_cu);
23243 else
23244 {
23245 dump_die_for_error (src_die);
23246 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23247 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23248 }
23249
23250 return die;
03dd20cc
DJ
23251}
23252
5c631832 23253/* Follow reference OFFSET.
673bfd45
DE
23254 On entry *REF_CU is the CU of the source die referencing OFFSET.
23255 On exit *REF_CU is the CU of the result.
23256 Returns NULL if OFFSET is invalid. */
f504f079 23257
f9aca02d 23258static struct die_info *
9c541725 23259follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23260 struct dwarf2_cu **ref_cu)
c906108c 23261{
10b3939b 23262 struct die_info temp_die;
f2f0e013 23263 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23264 struct dwarf2_per_objfile *dwarf2_per_objfile
23265 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23266
348e048f
DE
23267 gdb_assert (cu->per_cu != NULL);
23268
98bfdba5
PA
23269 target_cu = cu;
23270
3019eac3 23271 if (cu->per_cu->is_debug_types)
348e048f
DE
23272 {
23273 /* .debug_types CUs cannot reference anything outside their CU.
23274 If they need to, they have to reference a signatured type via
55f1336d 23275 DW_FORM_ref_sig8. */
9c541725 23276 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23277 return NULL;
348e048f 23278 }
36586728 23279 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23280 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23281 {
23282 struct dwarf2_per_cu_data *per_cu;
9a619af0 23283
9c541725 23284 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23285 dwarf2_per_objfile);
03dd20cc
DJ
23286
23287 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23288 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23289 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23290
10b3939b
DJ
23291 target_cu = per_cu->cu;
23292 }
98bfdba5
PA
23293 else if (cu->dies == NULL)
23294 {
23295 /* We're loading full DIEs during partial symbol reading. */
23296 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23297 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23298 }
c906108c 23299
f2f0e013 23300 *ref_cu = target_cu;
9c541725 23301 temp_die.sect_off = sect_off;
c24bdb02
KS
23302
23303 if (target_cu != cu)
23304 target_cu->ancestor = cu;
23305
9a3c8263 23306 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23307 &temp_die,
23308 to_underlying (sect_off));
5c631832 23309}
10b3939b 23310
5c631832
JK
23311/* Follow reference attribute ATTR of SRC_DIE.
23312 On entry *REF_CU is the CU of SRC_DIE.
23313 On exit *REF_CU is the CU of the result. */
23314
23315static struct die_info *
ff39bb5e 23316follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23317 struct dwarf2_cu **ref_cu)
23318{
9c541725 23319 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23320 struct dwarf2_cu *cu = *ref_cu;
23321 struct die_info *die;
23322
9c541725 23323 die = follow_die_offset (sect_off,
36586728
TT
23324 (attr->form == DW_FORM_GNU_ref_alt
23325 || cu->per_cu->is_dwz),
23326 ref_cu);
5c631832 23327 if (!die)
9d8780f0
SM
23328 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23329 "at %s [in module %s]"),
23330 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23331 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23332
5c631832
JK
23333 return die;
23334}
23335
9c541725 23336/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23337 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23338 dwarf2_locexpr_baton->data has lifetime of
23339 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23340
23341struct dwarf2_locexpr_baton
9c541725 23342dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23343 struct dwarf2_per_cu_data *per_cu,
23344 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23345 void *baton, bool resolve_abstract_p)
5c631832 23346{
918dd910 23347 struct dwarf2_cu *cu;
5c631832
JK
23348 struct die_info *die;
23349 struct attribute *attr;
23350 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23351 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23352 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23353
918dd910 23354 if (per_cu->cu == NULL)
58f0c718 23355 load_cu (per_cu, false);
918dd910 23356 cu = per_cu->cu;
cc12ce38
DE
23357 if (cu == NULL)
23358 {
23359 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23360 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23361 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23362 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23363 }
918dd910 23364
9c541725 23365 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23366 if (!die)
9d8780f0
SM
23367 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23368 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23369
23370 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23371 if (!attr && resolve_abstract_p
3360b6e7 23372 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23373 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23374 {
23375 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23376 CORE_ADDR baseaddr
23377 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23378 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23379
3360b6e7
TV
23380 for (const auto &cand_off
23381 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23382 {
3360b6e7
TV
23383 struct dwarf2_cu *cand_cu = cu;
23384 struct die_info *cand
23385 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23386 if (!cand
23387 || !cand->parent
e4a62c65
TV
23388 || cand->parent->tag != DW_TAG_subprogram)
23389 continue;
23390
23391 CORE_ADDR pc_low, pc_high;
23392 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23393 if (pc_low == ((CORE_ADDR) -1))
23394 continue;
23395 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23396 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23397 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23398 continue;
23399
23400 die = cand;
23401 attr = dwarf2_attr (die, DW_AT_location, cu);
23402 break;
23403 }
23404 }
23405
5c631832
JK
23406 if (!attr)
23407 {
e103e986
JK
23408 /* DWARF: "If there is no such attribute, then there is no effect.".
23409 DATA is ignored if SIZE is 0. */
5c631832 23410
e103e986 23411 retval.data = NULL;
5c631832
JK
23412 retval.size = 0;
23413 }
8cf6f0b1
TT
23414 else if (attr_form_is_section_offset (attr))
23415 {
23416 struct dwarf2_loclist_baton loclist_baton;
23417 CORE_ADDR pc = (*get_frame_pc) (baton);
23418 size_t size;
23419
23420 fill_in_loclist_baton (cu, &loclist_baton, attr);
23421
23422 retval.data = dwarf2_find_location_expression (&loclist_baton,
23423 &size, pc);
23424 retval.size = size;
23425 }
5c631832
JK
23426 else
23427 {
23428 if (!attr_form_is_block (attr))
9d8780f0 23429 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23430 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23431 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23432
23433 retval.data = DW_BLOCK (attr)->data;
23434 retval.size = DW_BLOCK (attr)->size;
23435 }
23436 retval.per_cu = cu->per_cu;
918dd910 23437
ed2dc618 23438 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23439
5c631832 23440 return retval;
348e048f
DE
23441}
23442
8b9737bf
TT
23443/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23444 offset. */
23445
23446struct dwarf2_locexpr_baton
23447dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23448 struct dwarf2_per_cu_data *per_cu,
23449 CORE_ADDR (*get_frame_pc) (void *baton),
23450 void *baton)
23451{
9c541725 23452 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23453
9c541725 23454 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23455}
23456
b6807d98
TT
23457/* Write a constant of a given type as target-ordered bytes into
23458 OBSTACK. */
23459
23460static const gdb_byte *
23461write_constant_as_bytes (struct obstack *obstack,
23462 enum bfd_endian byte_order,
23463 struct type *type,
23464 ULONGEST value,
23465 LONGEST *len)
23466{
23467 gdb_byte *result;
23468
23469 *len = TYPE_LENGTH (type);
224c3ddb 23470 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23471 store_unsigned_integer (result, *len, byte_order, value);
23472
23473 return result;
23474}
23475
23476/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23477 pointer to the constant bytes and set LEN to the length of the
23478 data. If memory is needed, allocate it on OBSTACK. If the DIE
23479 does not have a DW_AT_const_value, return NULL. */
23480
23481const gdb_byte *
9c541725 23482dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23483 struct dwarf2_per_cu_data *per_cu,
23484 struct obstack *obstack,
23485 LONGEST *len)
23486{
23487 struct dwarf2_cu *cu;
23488 struct die_info *die;
23489 struct attribute *attr;
23490 const gdb_byte *result = NULL;
23491 struct type *type;
23492 LONGEST value;
23493 enum bfd_endian byte_order;
e3b94546 23494 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23495
b6807d98 23496 if (per_cu->cu == NULL)
58f0c718 23497 load_cu (per_cu, false);
b6807d98 23498 cu = per_cu->cu;
cc12ce38
DE
23499 if (cu == NULL)
23500 {
23501 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23502 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23503 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23504 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23505 }
b6807d98 23506
9c541725 23507 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23508 if (!die)
9d8780f0
SM
23509 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23510 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23511
23512 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23513 if (attr == NULL)
23514 return NULL;
23515
e3b94546 23516 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23517 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23518
23519 switch (attr->form)
23520 {
23521 case DW_FORM_addr:
336d760d 23522 case DW_FORM_addrx:
b6807d98
TT
23523 case DW_FORM_GNU_addr_index:
23524 {
23525 gdb_byte *tem;
23526
23527 *len = cu->header.addr_size;
224c3ddb 23528 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23529 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23530 result = tem;
23531 }
23532 break;
23533 case DW_FORM_string:
23534 case DW_FORM_strp:
cf532bd1 23535 case DW_FORM_strx:
b6807d98
TT
23536 case DW_FORM_GNU_str_index:
23537 case DW_FORM_GNU_strp_alt:
23538 /* DW_STRING is already allocated on the objfile obstack, point
23539 directly to it. */
23540 result = (const gdb_byte *) DW_STRING (attr);
23541 *len = strlen (DW_STRING (attr));
23542 break;
23543 case DW_FORM_block1:
23544 case DW_FORM_block2:
23545 case DW_FORM_block4:
23546 case DW_FORM_block:
23547 case DW_FORM_exprloc:
0224619f 23548 case DW_FORM_data16:
b6807d98
TT
23549 result = DW_BLOCK (attr)->data;
23550 *len = DW_BLOCK (attr)->size;
23551 break;
23552
23553 /* The DW_AT_const_value attributes are supposed to carry the
23554 symbol's value "represented as it would be on the target
23555 architecture." By the time we get here, it's already been
23556 converted to host endianness, so we just need to sign- or
23557 zero-extend it as appropriate. */
23558 case DW_FORM_data1:
23559 type = die_type (die, cu);
23560 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23561 if (result == NULL)
23562 result = write_constant_as_bytes (obstack, byte_order,
23563 type, value, len);
23564 break;
23565 case DW_FORM_data2:
23566 type = die_type (die, cu);
23567 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23568 if (result == NULL)
23569 result = write_constant_as_bytes (obstack, byte_order,
23570 type, value, len);
23571 break;
23572 case DW_FORM_data4:
23573 type = die_type (die, cu);
23574 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23575 if (result == NULL)
23576 result = write_constant_as_bytes (obstack, byte_order,
23577 type, value, len);
23578 break;
23579 case DW_FORM_data8:
23580 type = die_type (die, cu);
23581 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23582 if (result == NULL)
23583 result = write_constant_as_bytes (obstack, byte_order,
23584 type, value, len);
23585 break;
23586
23587 case DW_FORM_sdata:
663c44ac 23588 case DW_FORM_implicit_const:
b6807d98
TT
23589 type = die_type (die, cu);
23590 result = write_constant_as_bytes (obstack, byte_order,
23591 type, DW_SND (attr), len);
23592 break;
23593
23594 case DW_FORM_udata:
23595 type = die_type (die, cu);
23596 result = write_constant_as_bytes (obstack, byte_order,
23597 type, DW_UNSND (attr), len);
23598 break;
23599
23600 default:
b98664d3 23601 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23602 dwarf_form_name (attr->form));
23603 break;
23604 }
23605
23606 return result;
23607}
23608
7942e96e
AA
23609/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23610 valid type for this die is found. */
23611
23612struct type *
9c541725 23613dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23614 struct dwarf2_per_cu_data *per_cu)
23615{
23616 struct dwarf2_cu *cu;
23617 struct die_info *die;
23618
7942e96e 23619 if (per_cu->cu == NULL)
58f0c718 23620 load_cu (per_cu, false);
7942e96e
AA
23621 cu = per_cu->cu;
23622 if (!cu)
23623 return NULL;
23624
9c541725 23625 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23626 if (!die)
23627 return NULL;
23628
23629 return die_type (die, cu);
23630}
23631
8a9b8146
TT
23632/* Return the type of the DIE at DIE_OFFSET in the CU named by
23633 PER_CU. */
23634
23635struct type *
b64f50a1 23636dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23637 struct dwarf2_per_cu_data *per_cu)
23638{
9c541725 23639 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23640 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23641}
23642
ac9ec31b 23643/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23644 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23645 On exit *REF_CU is the CU of the result.
23646 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23647
23648static struct die_info *
ac9ec31b
DE
23649follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23650 struct dwarf2_cu **ref_cu)
348e048f 23651{
348e048f 23652 struct die_info temp_die;
c24bdb02 23653 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23654 struct die_info *die;
23655
ac9ec31b
DE
23656 /* While it might be nice to assert sig_type->type == NULL here,
23657 we can get here for DW_AT_imported_declaration where we need
23658 the DIE not the type. */
348e048f
DE
23659
23660 /* If necessary, add it to the queue and load its DIEs. */
23661
95554aad 23662 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23663 read_signatured_type (sig_type);
348e048f 23664
348e048f 23665 sig_cu = sig_type->per_cu.cu;
69d751e3 23666 gdb_assert (sig_cu != NULL);
9c541725
PA
23667 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23668 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23669 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23670 to_underlying (temp_die.sect_off));
348e048f
DE
23671 if (die)
23672 {
ed2dc618 23673 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23674 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23675
796a7ff8
DE
23676 /* For .gdb_index version 7 keep track of included TUs.
23677 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23678 if (dwarf2_per_objfile->index_table != NULL
23679 && dwarf2_per_objfile->index_table->version <= 7)
23680 {
23681 VEC_safe_push (dwarf2_per_cu_ptr,
23682 (*ref_cu)->per_cu->imported_symtabs,
23683 sig_cu->per_cu);
23684 }
23685
348e048f 23686 *ref_cu = sig_cu;
c24bdb02
KS
23687 if (sig_cu != cu)
23688 sig_cu->ancestor = cu;
23689
348e048f
DE
23690 return die;
23691 }
23692
ac9ec31b
DE
23693 return NULL;
23694}
23695
23696/* Follow signatured type referenced by ATTR in SRC_DIE.
23697 On entry *REF_CU is the CU of SRC_DIE.
23698 On exit *REF_CU is the CU of the result.
23699 The result is the DIE of the type.
23700 If the referenced type cannot be found an error is thrown. */
23701
23702static struct die_info *
ff39bb5e 23703follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23704 struct dwarf2_cu **ref_cu)
23705{
23706 ULONGEST signature = DW_SIGNATURE (attr);
23707 struct signatured_type *sig_type;
23708 struct die_info *die;
23709
23710 gdb_assert (attr->form == DW_FORM_ref_sig8);
23711
a2ce51a0 23712 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23713 /* sig_type will be NULL if the signatured type is missing from
23714 the debug info. */
23715 if (sig_type == NULL)
23716 {
23717 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23718 " from DIE at %s [in module %s]"),
23719 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23720 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23721 }
23722
23723 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23724 if (die == NULL)
23725 {
23726 dump_die_for_error (src_die);
23727 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23728 " from DIE at %s [in module %s]"),
23729 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23730 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23731 }
23732
23733 return die;
23734}
23735
23736/* Get the type specified by SIGNATURE referenced in DIE/CU,
23737 reading in and processing the type unit if necessary. */
23738
23739static struct type *
23740get_signatured_type (struct die_info *die, ULONGEST signature,
23741 struct dwarf2_cu *cu)
23742{
518817b3
SM
23743 struct dwarf2_per_objfile *dwarf2_per_objfile
23744 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23745 struct signatured_type *sig_type;
23746 struct dwarf2_cu *type_cu;
23747 struct die_info *type_die;
23748 struct type *type;
23749
a2ce51a0 23750 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23751 /* sig_type will be NULL if the signatured type is missing from
23752 the debug info. */
23753 if (sig_type == NULL)
23754 {
b98664d3 23755 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23756 " from DIE at %s [in module %s]"),
23757 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23758 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23759 return build_error_marker_type (cu, die);
23760 }
23761
23762 /* If we already know the type we're done. */
23763 if (sig_type->type != NULL)
23764 return sig_type->type;
23765
23766 type_cu = cu;
23767 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23768 if (type_die != NULL)
23769 {
23770 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23771 is created. This is important, for example, because for c++ classes
23772 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23773 type = read_type_die (type_die, type_cu);
23774 if (type == NULL)
23775 {
b98664d3 23776 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23777 " referenced from DIE at %s [in module %s]"),
23778 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23779 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23780 type = build_error_marker_type (cu, die);
23781 }
23782 }
23783 else
23784 {
b98664d3 23785 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23786 " from DIE at %s [in module %s]"),
23787 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23788 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23789 type = build_error_marker_type (cu, die);
23790 }
23791 sig_type->type = type;
23792
23793 return type;
23794}
23795
23796/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23797 reading in and processing the type unit if necessary. */
23798
23799static struct type *
ff39bb5e 23800get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23801 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23802{
23803 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23804 if (attr_form_is_ref (attr))
ac9ec31b
DE
23805 {
23806 struct dwarf2_cu *type_cu = cu;
23807 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23808
23809 return read_type_die (type_die, type_cu);
23810 }
23811 else if (attr->form == DW_FORM_ref_sig8)
23812 {
23813 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23814 }
23815 else
23816 {
518817b3
SM
23817 struct dwarf2_per_objfile *dwarf2_per_objfile
23818 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23819
b98664d3 23820 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23821 " at %s [in module %s]"),
23822 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23823 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23824 return build_error_marker_type (cu, die);
23825 }
348e048f
DE
23826}
23827
e5fe5e75 23828/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23829
23830static void
e5fe5e75 23831load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23832{
52dc124a 23833 struct signatured_type *sig_type;
348e048f 23834
f4dc4d17
DE
23835 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23836 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23837
6721b2ec
DE
23838 /* We have the per_cu, but we need the signatured_type.
23839 Fortunately this is an easy translation. */
23840 gdb_assert (per_cu->is_debug_types);
23841 sig_type = (struct signatured_type *) per_cu;
348e048f 23842
6721b2ec 23843 gdb_assert (per_cu->cu == NULL);
348e048f 23844
52dc124a 23845 read_signatured_type (sig_type);
348e048f 23846
6721b2ec 23847 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23848}
23849
dee91e82
DE
23850/* die_reader_func for read_signatured_type.
23851 This is identical to load_full_comp_unit_reader,
23852 but is kept separate for now. */
348e048f
DE
23853
23854static void
dee91e82 23855read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23856 const gdb_byte *info_ptr,
dee91e82
DE
23857 struct die_info *comp_unit_die,
23858 int has_children,
23859 void *data)
348e048f 23860{
dee91e82 23861 struct dwarf2_cu *cu = reader->cu;
348e048f 23862
dee91e82
DE
23863 gdb_assert (cu->die_hash == NULL);
23864 cu->die_hash =
23865 htab_create_alloc_ex (cu->header.length / 12,
23866 die_hash,
23867 die_eq,
23868 NULL,
23869 &cu->comp_unit_obstack,
23870 hashtab_obstack_allocate,
23871 dummy_obstack_deallocate);
348e048f 23872
dee91e82
DE
23873 if (has_children)
23874 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23875 &info_ptr, comp_unit_die);
23876 cu->dies = comp_unit_die;
23877 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23878
23879 /* We try not to read any attributes in this function, because not
9cdd5dbd 23880 all CUs needed for references have been loaded yet, and symbol
348e048f 23881 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23882 or we won't be able to build types correctly.
23883 Similarly, if we do not read the producer, we can not apply
23884 producer-specific interpretation. */
95554aad 23885 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23886}
348e048f 23887
3019eac3
DE
23888/* Read in a signatured type and build its CU and DIEs.
23889 If the type is a stub for the real type in a DWO file,
23890 read in the real type from the DWO file as well. */
dee91e82
DE
23891
23892static void
23893read_signatured_type (struct signatured_type *sig_type)
23894{
23895 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23896
3019eac3 23897 gdb_assert (per_cu->is_debug_types);
dee91e82 23898 gdb_assert (per_cu->cu == NULL);
348e048f 23899
58f0c718 23900 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23901 read_signatured_type_reader, NULL);
7ee85ab1 23902 sig_type->per_cu.tu_read = 1;
c906108c
SS
23903}
23904
c906108c
SS
23905/* Decode simple location descriptions.
23906 Given a pointer to a dwarf block that defines a location, compute
23907 the location and return the value.
23908
4cecd739
DJ
23909 NOTE drow/2003-11-18: This function is called in two situations
23910 now: for the address of static or global variables (partial symbols
23911 only) and for offsets into structures which are expected to be
23912 (more or less) constant. The partial symbol case should go away,
23913 and only the constant case should remain. That will let this
23914 function complain more accurately. A few special modes are allowed
23915 without complaint for global variables (for instance, global
23916 register values and thread-local values).
c906108c
SS
23917
23918 A location description containing no operations indicates that the
4cecd739 23919 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23920 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23921 callers will only want a very basic result and this can become a
21ae7a4d
JK
23922 complaint.
23923
23924 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23925
23926static CORE_ADDR
e7c27a73 23927decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23928{
518817b3 23929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23930 size_t i;
23931 size_t size = blk->size;
d521ce57 23932 const gdb_byte *data = blk->data;
21ae7a4d
JK
23933 CORE_ADDR stack[64];
23934 int stacki;
23935 unsigned int bytes_read, unsnd;
23936 gdb_byte op;
c906108c 23937
21ae7a4d
JK
23938 i = 0;
23939 stacki = 0;
23940 stack[stacki] = 0;
23941 stack[++stacki] = 0;
23942
23943 while (i < size)
23944 {
23945 op = data[i++];
23946 switch (op)
23947 {
23948 case DW_OP_lit0:
23949 case DW_OP_lit1:
23950 case DW_OP_lit2:
23951 case DW_OP_lit3:
23952 case DW_OP_lit4:
23953 case DW_OP_lit5:
23954 case DW_OP_lit6:
23955 case DW_OP_lit7:
23956 case DW_OP_lit8:
23957 case DW_OP_lit9:
23958 case DW_OP_lit10:
23959 case DW_OP_lit11:
23960 case DW_OP_lit12:
23961 case DW_OP_lit13:
23962 case DW_OP_lit14:
23963 case DW_OP_lit15:
23964 case DW_OP_lit16:
23965 case DW_OP_lit17:
23966 case DW_OP_lit18:
23967 case DW_OP_lit19:
23968 case DW_OP_lit20:
23969 case DW_OP_lit21:
23970 case DW_OP_lit22:
23971 case DW_OP_lit23:
23972 case DW_OP_lit24:
23973 case DW_OP_lit25:
23974 case DW_OP_lit26:
23975 case DW_OP_lit27:
23976 case DW_OP_lit28:
23977 case DW_OP_lit29:
23978 case DW_OP_lit30:
23979 case DW_OP_lit31:
23980 stack[++stacki] = op - DW_OP_lit0;
23981 break;
f1bea926 23982
21ae7a4d
JK
23983 case DW_OP_reg0:
23984 case DW_OP_reg1:
23985 case DW_OP_reg2:
23986 case DW_OP_reg3:
23987 case DW_OP_reg4:
23988 case DW_OP_reg5:
23989 case DW_OP_reg6:
23990 case DW_OP_reg7:
23991 case DW_OP_reg8:
23992 case DW_OP_reg9:
23993 case DW_OP_reg10:
23994 case DW_OP_reg11:
23995 case DW_OP_reg12:
23996 case DW_OP_reg13:
23997 case DW_OP_reg14:
23998 case DW_OP_reg15:
23999 case DW_OP_reg16:
24000 case DW_OP_reg17:
24001 case DW_OP_reg18:
24002 case DW_OP_reg19:
24003 case DW_OP_reg20:
24004 case DW_OP_reg21:
24005 case DW_OP_reg22:
24006 case DW_OP_reg23:
24007 case DW_OP_reg24:
24008 case DW_OP_reg25:
24009 case DW_OP_reg26:
24010 case DW_OP_reg27:
24011 case DW_OP_reg28:
24012 case DW_OP_reg29:
24013 case DW_OP_reg30:
24014 case DW_OP_reg31:
24015 stack[++stacki] = op - DW_OP_reg0;
24016 if (i < size)
24017 dwarf2_complex_location_expr_complaint ();
24018 break;
c906108c 24019
21ae7a4d
JK
24020 case DW_OP_regx:
24021 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24022 i += bytes_read;
24023 stack[++stacki] = unsnd;
24024 if (i < size)
24025 dwarf2_complex_location_expr_complaint ();
24026 break;
c906108c 24027
21ae7a4d
JK
24028 case DW_OP_addr:
24029 stack[++stacki] = read_address (objfile->obfd, &data[i],
24030 cu, &bytes_read);
24031 i += bytes_read;
24032 break;
d53d4ac5 24033
21ae7a4d
JK
24034 case DW_OP_const1u:
24035 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24036 i += 1;
24037 break;
24038
24039 case DW_OP_const1s:
24040 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24041 i += 1;
24042 break;
24043
24044 case DW_OP_const2u:
24045 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24046 i += 2;
24047 break;
24048
24049 case DW_OP_const2s:
24050 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24051 i += 2;
24052 break;
d53d4ac5 24053
21ae7a4d
JK
24054 case DW_OP_const4u:
24055 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24056 i += 4;
24057 break;
24058
24059 case DW_OP_const4s:
24060 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24061 i += 4;
24062 break;
24063
585861ea
JK
24064 case DW_OP_const8u:
24065 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24066 i += 8;
24067 break;
24068
21ae7a4d
JK
24069 case DW_OP_constu:
24070 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24071 &bytes_read);
24072 i += bytes_read;
24073 break;
24074
24075 case DW_OP_consts:
24076 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24077 i += bytes_read;
24078 break;
24079
24080 case DW_OP_dup:
24081 stack[stacki + 1] = stack[stacki];
24082 stacki++;
24083 break;
24084
24085 case DW_OP_plus:
24086 stack[stacki - 1] += stack[stacki];
24087 stacki--;
24088 break;
24089
24090 case DW_OP_plus_uconst:
24091 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24092 &bytes_read);
24093 i += bytes_read;
24094 break;
24095
24096 case DW_OP_minus:
24097 stack[stacki - 1] -= stack[stacki];
24098 stacki--;
24099 break;
24100
24101 case DW_OP_deref:
24102 /* If we're not the last op, then we definitely can't encode
24103 this using GDB's address_class enum. This is valid for partial
24104 global symbols, although the variable's address will be bogus
24105 in the psymtab. */
24106 if (i < size)
24107 dwarf2_complex_location_expr_complaint ();
24108 break;
24109
24110 case DW_OP_GNU_push_tls_address:
4aa4e28b 24111 case DW_OP_form_tls_address:
21ae7a4d
JK
24112 /* The top of the stack has the offset from the beginning
24113 of the thread control block at which the variable is located. */
24114 /* Nothing should follow this operator, so the top of stack would
24115 be returned. */
24116 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24117 address will be bogus in the psymtab. Make it always at least
24118 non-zero to not look as a variable garbage collected by linker
24119 which have DW_OP_addr 0. */
21ae7a4d
JK
24120 if (i < size)
24121 dwarf2_complex_location_expr_complaint ();
585861ea 24122 stack[stacki]++;
21ae7a4d
JK
24123 break;
24124
24125 case DW_OP_GNU_uninit:
24126 break;
24127
336d760d 24128 case DW_OP_addrx:
3019eac3 24129 case DW_OP_GNU_addr_index:
49f6c839 24130 case DW_OP_GNU_const_index:
3019eac3
DE
24131 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24132 &bytes_read);
24133 i += bytes_read;
24134 break;
24135
21ae7a4d
JK
24136 default:
24137 {
f39c6ffd 24138 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24139
24140 if (name)
b98664d3 24141 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24142 name);
24143 else
b98664d3 24144 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24145 op);
24146 }
24147
24148 return (stack[stacki]);
d53d4ac5 24149 }
3c6e0cb3 24150
21ae7a4d
JK
24151 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24152 outside of the allocated space. Also enforce minimum>0. */
24153 if (stacki >= ARRAY_SIZE (stack) - 1)
24154 {
b98664d3 24155 complaint (_("location description stack overflow"));
21ae7a4d
JK
24156 return 0;
24157 }
24158
24159 if (stacki <= 0)
24160 {
b98664d3 24161 complaint (_("location description stack underflow"));
21ae7a4d
JK
24162 return 0;
24163 }
24164 }
24165 return (stack[stacki]);
c906108c
SS
24166}
24167
24168/* memory allocation interface */
24169
c906108c 24170static struct dwarf_block *
7b5a2f43 24171dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24172{
8d749320 24173 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24174}
24175
c906108c 24176static struct die_info *
b60c80d6 24177dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24178{
24179 struct die_info *die;
b60c80d6
DJ
24180 size_t size = sizeof (struct die_info);
24181
24182 if (num_attrs > 1)
24183 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24184
b60c80d6 24185 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24186 memset (die, 0, sizeof (struct die_info));
24187 return (die);
24188}
2e276125
JB
24189
24190\f
24191/* Macro support. */
24192
233d95b5
JK
24193/* Return file name relative to the compilation directory of file number I in
24194 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24195 responsible for freeing it. */
233d95b5 24196
2e276125 24197static char *
233d95b5 24198file_file_name (int file, struct line_header *lh)
2e276125 24199{
6a83a1e6
EZ
24200 /* Is the file number a valid index into the line header's file name
24201 table? Remember that file numbers start with one, not zero. */
fff8551c 24202 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24203 {
8c43009f 24204 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24205
8c43009f
PA
24206 if (!IS_ABSOLUTE_PATH (fe.name))
24207 {
24208 const char *dir = fe.include_dir (lh);
24209 if (dir != NULL)
24210 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24211 }
24212 return xstrdup (fe.name);
6a83a1e6 24213 }
2e276125
JB
24214 else
24215 {
6a83a1e6
EZ
24216 /* The compiler produced a bogus file number. We can at least
24217 record the macro definitions made in the file, even if we
24218 won't be able to find the file by name. */
24219 char fake_name[80];
9a619af0 24220
8c042590
PM
24221 xsnprintf (fake_name, sizeof (fake_name),
24222 "<bad macro file number %d>", file);
2e276125 24223
b98664d3 24224 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24225 file);
2e276125 24226
6a83a1e6 24227 return xstrdup (fake_name);
2e276125
JB
24228 }
24229}
24230
233d95b5
JK
24231/* Return the full name of file number I in *LH's file name table.
24232 Use COMP_DIR as the name of the current directory of the
24233 compilation. The result is allocated using xmalloc; the caller is
24234 responsible for freeing it. */
24235static char *
24236file_full_name (int file, struct line_header *lh, const char *comp_dir)
24237{
24238 /* Is the file number a valid index into the line header's file name
24239 table? Remember that file numbers start with one, not zero. */
fff8551c 24240 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24241 {
24242 char *relative = file_file_name (file, lh);
24243
24244 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24245 return relative;
b36cec19
PA
24246 return reconcat (relative, comp_dir, SLASH_STRING,
24247 relative, (char *) NULL);
233d95b5
JK
24248 }
24249 else
24250 return file_file_name (file, lh);
24251}
24252
2e276125
JB
24253
24254static struct macro_source_file *
804d2729
TT
24255macro_start_file (struct dwarf2_cu *cu,
24256 int file, int line,
2e276125 24257 struct macro_source_file *current_file,
43f3e411 24258 struct line_header *lh)
2e276125 24259{
233d95b5
JK
24260 /* File name relative to the compilation directory of this source file. */
24261 char *file_name = file_file_name (file, lh);
2e276125 24262
2e276125 24263 if (! current_file)
abc9d0dc 24264 {
fc474241
DE
24265 /* Note: We don't create a macro table for this compilation unit
24266 at all until we actually get a filename. */
c24bdb02 24267 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24268
abc9d0dc
TT
24269 /* If we have no current file, then this must be the start_file
24270 directive for the compilation unit's main source file. */
fc474241
DE
24271 current_file = macro_set_main (macro_table, file_name);
24272 macro_define_special (macro_table);
abc9d0dc 24273 }
2e276125 24274 else
233d95b5 24275 current_file = macro_include (current_file, line, file_name);
2e276125 24276
233d95b5 24277 xfree (file_name);
6e70227d 24278
2e276125
JB
24279 return current_file;
24280}
24281
2e276125
JB
24282static const char *
24283consume_improper_spaces (const char *p, const char *body)
24284{
24285 if (*p == ' ')
24286 {
b98664d3 24287 complaint (_("macro definition contains spaces "
3e43a32a 24288 "in formal argument list:\n`%s'"),
4d3c2250 24289 body);
2e276125
JB
24290
24291 while (*p == ' ')
24292 p++;
24293 }
24294
24295 return p;
24296}
24297
24298
24299static void
24300parse_macro_definition (struct macro_source_file *file, int line,
24301 const char *body)
24302{
24303 const char *p;
24304
24305 /* The body string takes one of two forms. For object-like macro
24306 definitions, it should be:
24307
24308 <macro name> " " <definition>
24309
24310 For function-like macro definitions, it should be:
24311
24312 <macro name> "() " <definition>
24313 or
24314 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24315
24316 Spaces may appear only where explicitly indicated, and in the
24317 <definition>.
24318
24319 The Dwarf 2 spec says that an object-like macro's name is always
24320 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24321 the space when the macro's definition is the empty string.
2e276125
JB
24322
24323 The Dwarf 2 spec says that there should be no spaces between the
24324 formal arguments in a function-like macro's formal argument list,
24325 but versions of GCC around March 2002 include spaces after the
24326 commas. */
24327
24328
24329 /* Find the extent of the macro name. The macro name is terminated
24330 by either a space or null character (for an object-like macro) or
24331 an opening paren (for a function-like macro). */
24332 for (p = body; *p; p++)
24333 if (*p == ' ' || *p == '(')
24334 break;
24335
24336 if (*p == ' ' || *p == '\0')
24337 {
24338 /* It's an object-like macro. */
24339 int name_len = p - body;
3f8a7804 24340 char *name = savestring (body, name_len);
2e276125
JB
24341 const char *replacement;
24342
24343 if (*p == ' ')
24344 replacement = body + name_len + 1;
24345 else
24346 {
4d3c2250 24347 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24348 replacement = body + name_len;
24349 }
6e70227d 24350
2e276125
JB
24351 macro_define_object (file, line, name, replacement);
24352
24353 xfree (name);
24354 }
24355 else if (*p == '(')
24356 {
24357 /* It's a function-like macro. */
3f8a7804 24358 char *name = savestring (body, p - body);
2e276125
JB
24359 int argc = 0;
24360 int argv_size = 1;
8d749320 24361 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24362
24363 p++;
24364
24365 p = consume_improper_spaces (p, body);
24366
24367 /* Parse the formal argument list. */
24368 while (*p && *p != ')')
24369 {
24370 /* Find the extent of the current argument name. */
24371 const char *arg_start = p;
24372
24373 while (*p && *p != ',' && *p != ')' && *p != ' ')
24374 p++;
24375
24376 if (! *p || p == arg_start)
4d3c2250 24377 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24378 else
24379 {
24380 /* Make sure argv has room for the new argument. */
24381 if (argc >= argv_size)
24382 {
24383 argv_size *= 2;
224c3ddb 24384 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24385 }
24386
3f8a7804 24387 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24388 }
24389
24390 p = consume_improper_spaces (p, body);
24391
24392 /* Consume the comma, if present. */
24393 if (*p == ',')
24394 {
24395 p++;
24396
24397 p = consume_improper_spaces (p, body);
24398 }
24399 }
24400
24401 if (*p == ')')
24402 {
24403 p++;
24404
24405 if (*p == ' ')
24406 /* Perfectly formed definition, no complaints. */
24407 macro_define_function (file, line, name,
6e70227d 24408 argc, (const char **) argv,
2e276125
JB
24409 p + 1);
24410 else if (*p == '\0')
24411 {
24412 /* Complain, but do define it. */
4d3c2250 24413 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24414 macro_define_function (file, line, name,
6e70227d 24415 argc, (const char **) argv,
2e276125
JB
24416 p);
24417 }
24418 else
24419 /* Just complain. */
4d3c2250 24420 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24421 }
24422 else
24423 /* Just complain. */
4d3c2250 24424 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24425
24426 xfree (name);
24427 {
24428 int i;
24429
24430 for (i = 0; i < argc; i++)
24431 xfree (argv[i]);
24432 }
24433 xfree (argv);
24434 }
24435 else
4d3c2250 24436 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24437}
24438
cf2c3c16
TT
24439/* Skip some bytes from BYTES according to the form given in FORM.
24440 Returns the new pointer. */
2e276125 24441
d521ce57
TT
24442static const gdb_byte *
24443skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24444 enum dwarf_form form,
24445 unsigned int offset_size,
24446 struct dwarf2_section_info *section)
2e276125 24447{
cf2c3c16 24448 unsigned int bytes_read;
2e276125 24449
cf2c3c16 24450 switch (form)
2e276125 24451 {
cf2c3c16
TT
24452 case DW_FORM_data1:
24453 case DW_FORM_flag:
24454 ++bytes;
24455 break;
24456
24457 case DW_FORM_data2:
24458 bytes += 2;
24459 break;
24460
24461 case DW_FORM_data4:
24462 bytes += 4;
24463 break;
24464
24465 case DW_FORM_data8:
24466 bytes += 8;
24467 break;
24468
0224619f
JK
24469 case DW_FORM_data16:
24470 bytes += 16;
24471 break;
24472
cf2c3c16
TT
24473 case DW_FORM_string:
24474 read_direct_string (abfd, bytes, &bytes_read);
24475 bytes += bytes_read;
24476 break;
24477
24478 case DW_FORM_sec_offset:
24479 case DW_FORM_strp:
36586728 24480 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24481 bytes += offset_size;
24482 break;
24483
24484 case DW_FORM_block:
24485 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24486 bytes += bytes_read;
24487 break;
24488
24489 case DW_FORM_block1:
24490 bytes += 1 + read_1_byte (abfd, bytes);
24491 break;
24492 case DW_FORM_block2:
24493 bytes += 2 + read_2_bytes (abfd, bytes);
24494 break;
24495 case DW_FORM_block4:
24496 bytes += 4 + read_4_bytes (abfd, bytes);
24497 break;
24498
336d760d 24499 case DW_FORM_addrx:
cf2c3c16 24500 case DW_FORM_sdata:
cf532bd1 24501 case DW_FORM_strx:
cf2c3c16 24502 case DW_FORM_udata:
3019eac3
DE
24503 case DW_FORM_GNU_addr_index:
24504 case DW_FORM_GNU_str_index:
d521ce57 24505 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24506 if (bytes == NULL)
24507 {
24508 dwarf2_section_buffer_overflow_complaint (section);
24509 return NULL;
24510 }
cf2c3c16
TT
24511 break;
24512
663c44ac
JK
24513 case DW_FORM_implicit_const:
24514 break;
24515
cf2c3c16
TT
24516 default:
24517 {
b98664d3 24518 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24519 form, get_section_name (section));
cf2c3c16
TT
24520 return NULL;
24521 }
2e276125
JB
24522 }
24523
cf2c3c16
TT
24524 return bytes;
24525}
757a13d0 24526
cf2c3c16
TT
24527/* A helper for dwarf_decode_macros that handles skipping an unknown
24528 opcode. Returns an updated pointer to the macro data buffer; or,
24529 on error, issues a complaint and returns NULL. */
757a13d0 24530
d521ce57 24531static const gdb_byte *
cf2c3c16 24532skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24533 const gdb_byte **opcode_definitions,
24534 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24535 bfd *abfd,
24536 unsigned int offset_size,
24537 struct dwarf2_section_info *section)
24538{
24539 unsigned int bytes_read, i;
24540 unsigned long arg;
d521ce57 24541 const gdb_byte *defn;
2e276125 24542
cf2c3c16 24543 if (opcode_definitions[opcode] == NULL)
2e276125 24544 {
b98664d3 24545 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24546 opcode);
24547 return NULL;
24548 }
2e276125 24549
cf2c3c16
TT
24550 defn = opcode_definitions[opcode];
24551 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24552 defn += bytes_read;
2e276125 24553
cf2c3c16
TT
24554 for (i = 0; i < arg; ++i)
24555 {
aead7601
SM
24556 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24557 (enum dwarf_form) defn[i], offset_size,
f664829e 24558 section);
cf2c3c16
TT
24559 if (mac_ptr == NULL)
24560 {
24561 /* skip_form_bytes already issued the complaint. */
24562 return NULL;
24563 }
24564 }
757a13d0 24565
cf2c3c16
TT
24566 return mac_ptr;
24567}
757a13d0 24568
cf2c3c16
TT
24569/* A helper function which parses the header of a macro section.
24570 If the macro section is the extended (for now called "GNU") type,
24571 then this updates *OFFSET_SIZE. Returns a pointer to just after
24572 the header, or issues a complaint and returns NULL on error. */
757a13d0 24573
d521ce57
TT
24574static const gdb_byte *
24575dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24576 bfd *abfd,
d521ce57 24577 const gdb_byte *mac_ptr,
cf2c3c16
TT
24578 unsigned int *offset_size,
24579 int section_is_gnu)
24580{
24581 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24582
cf2c3c16
TT
24583 if (section_is_gnu)
24584 {
24585 unsigned int version, flags;
757a13d0 24586
cf2c3c16 24587 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24588 if (version != 4 && version != 5)
cf2c3c16 24589 {
b98664d3 24590 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24591 version);
24592 return NULL;
24593 }
24594 mac_ptr += 2;
757a13d0 24595
cf2c3c16
TT
24596 flags = read_1_byte (abfd, mac_ptr);
24597 ++mac_ptr;
24598 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24599
cf2c3c16
TT
24600 if ((flags & 2) != 0)
24601 /* We don't need the line table offset. */
24602 mac_ptr += *offset_size;
757a13d0 24603
cf2c3c16
TT
24604 /* Vendor opcode descriptions. */
24605 if ((flags & 4) != 0)
24606 {
24607 unsigned int i, count;
757a13d0 24608
cf2c3c16
TT
24609 count = read_1_byte (abfd, mac_ptr);
24610 ++mac_ptr;
24611 for (i = 0; i < count; ++i)
24612 {
24613 unsigned int opcode, bytes_read;
24614 unsigned long arg;
24615
24616 opcode = read_1_byte (abfd, mac_ptr);
24617 ++mac_ptr;
24618 opcode_definitions[opcode] = mac_ptr;
24619 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24620 mac_ptr += bytes_read;
24621 mac_ptr += arg;
24622 }
757a13d0 24623 }
cf2c3c16 24624 }
757a13d0 24625
cf2c3c16
TT
24626 return mac_ptr;
24627}
757a13d0 24628
cf2c3c16 24629/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24630 including DW_MACRO_import. */
cf2c3c16
TT
24631
24632static void
804d2729 24633dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24634 bfd *abfd,
d521ce57 24635 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24636 struct macro_source_file *current_file,
43f3e411 24637 struct line_header *lh,
cf2c3c16 24638 struct dwarf2_section_info *section,
36586728 24639 int section_is_gnu, int section_is_dwz,
cf2c3c16 24640 unsigned int offset_size,
8fc3fc34 24641 htab_t include_hash)
cf2c3c16 24642{
804d2729
TT
24643 struct dwarf2_per_objfile *dwarf2_per_objfile
24644 = cu->per_cu->dwarf2_per_objfile;
4d663531 24645 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24646 enum dwarf_macro_record_type macinfo_type;
24647 int at_commandline;
d521ce57 24648 const gdb_byte *opcode_definitions[256];
757a13d0 24649
cf2c3c16
TT
24650 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24651 &offset_size, section_is_gnu);
24652 if (mac_ptr == NULL)
24653 {
24654 /* We already issued a complaint. */
24655 return;
24656 }
757a13d0
JK
24657
24658 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24659 GDB is still reading the definitions from command line. First
24660 DW_MACINFO_start_file will need to be ignored as it was already executed
24661 to create CURRENT_FILE for the main source holding also the command line
24662 definitions. On first met DW_MACINFO_start_file this flag is reset to
24663 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24664
24665 at_commandline = 1;
24666
24667 do
24668 {
24669 /* Do we at least have room for a macinfo type byte? */
24670 if (mac_ptr >= mac_end)
24671 {
f664829e 24672 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24673 break;
24674 }
24675
aead7601 24676 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24677 mac_ptr++;
24678
cf2c3c16
TT
24679 /* Note that we rely on the fact that the corresponding GNU and
24680 DWARF constants are the same. */
132448f8
SM
24681 DIAGNOSTIC_PUSH
24682 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24683 switch (macinfo_type)
24684 {
24685 /* A zero macinfo type indicates the end of the macro
24686 information. */
24687 case 0:
24688 break;
2e276125 24689
0af92d60
JK
24690 case DW_MACRO_define:
24691 case DW_MACRO_undef:
24692 case DW_MACRO_define_strp:
24693 case DW_MACRO_undef_strp:
24694 case DW_MACRO_define_sup:
24695 case DW_MACRO_undef_sup:
2e276125 24696 {
891d2f0b 24697 unsigned int bytes_read;
2e276125 24698 int line;
d521ce57 24699 const char *body;
cf2c3c16 24700 int is_define;
2e276125 24701
cf2c3c16
TT
24702 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24703 mac_ptr += bytes_read;
24704
0af92d60
JK
24705 if (macinfo_type == DW_MACRO_define
24706 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24707 {
24708 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24709 mac_ptr += bytes_read;
24710 }
24711 else
24712 {
24713 LONGEST str_offset;
24714
24715 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24716 mac_ptr += offset_size;
2e276125 24717
0af92d60
JK
24718 if (macinfo_type == DW_MACRO_define_sup
24719 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24720 || section_is_dwz)
36586728 24721 {
ed2dc618
SM
24722 struct dwz_file *dwz
24723 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24724
ed2dc618
SM
24725 body = read_indirect_string_from_dwz (objfile,
24726 dwz, str_offset);
36586728
TT
24727 }
24728 else
ed2dc618
SM
24729 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24730 abfd, str_offset);
cf2c3c16
TT
24731 }
24732
0af92d60
JK
24733 is_define = (macinfo_type == DW_MACRO_define
24734 || macinfo_type == DW_MACRO_define_strp
24735 || macinfo_type == DW_MACRO_define_sup);
2e276125 24736 if (! current_file)
757a13d0
JK
24737 {
24738 /* DWARF violation as no main source is present. */
b98664d3 24739 complaint (_("debug info with no main source gives macro %s "
757a13d0 24740 "on line %d: %s"),
cf2c3c16
TT
24741 is_define ? _("definition") : _("undefinition"),
24742 line, body);
757a13d0
JK
24743 break;
24744 }
3e43a32a
MS
24745 if ((line == 0 && !at_commandline)
24746 || (line != 0 && at_commandline))
b98664d3 24747 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24748 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24749 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24750 line == 0 ? _("zero") : _("non-zero"), line, body);
24751
955b06fa 24752 if (body == NULL)
7bede828 24753 {
955b06fa
SDJ
24754 /* Fedora's rpm-build's "debugedit" binary
24755 corrupted .debug_macro sections.
24756
24757 For more info, see
24758 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24759 complaint (_("debug info gives %s invalid macro %s "
24760 "without body (corrupted?) at line %d "
24761 "on file %s"),
24762 at_commandline ? _("command-line") : _("in-file"),
24763 is_define ? _("definition") : _("undefinition"),
24764 line, current_file->filename);
7bede828 24765 }
955b06fa
SDJ
24766 else if (is_define)
24767 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24768 else
24769 {
0af92d60
JK
24770 gdb_assert (macinfo_type == DW_MACRO_undef
24771 || macinfo_type == DW_MACRO_undef_strp
24772 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24773 macro_undef (current_file, line, body);
24774 }
2e276125
JB
24775 }
24776 break;
24777
0af92d60 24778 case DW_MACRO_start_file:
2e276125 24779 {
891d2f0b 24780 unsigned int bytes_read;
2e276125
JB
24781 int line, file;
24782
24783 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24784 mac_ptr += bytes_read;
24785 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24786 mac_ptr += bytes_read;
24787
3e43a32a
MS
24788 if ((line == 0 && !at_commandline)
24789 || (line != 0 && at_commandline))
b98664d3 24790 complaint (_("debug info gives source %d included "
757a13d0
JK
24791 "from %s at %s line %d"),
24792 file, at_commandline ? _("command-line") : _("file"),
24793 line == 0 ? _("zero") : _("non-zero"), line);
24794
24795 if (at_commandline)
24796 {
0af92d60 24797 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24798 pass one. */
757a13d0
JK
24799 at_commandline = 0;
24800 }
24801 else
804d2729
TT
24802 current_file = macro_start_file (cu, file, line, current_file,
24803 lh);
2e276125
JB
24804 }
24805 break;
24806
0af92d60 24807 case DW_MACRO_end_file:
2e276125 24808 if (! current_file)
b98664d3 24809 complaint (_("macro debug info has an unmatched "
3e43a32a 24810 "`close_file' directive"));
2e276125
JB
24811 else
24812 {
24813 current_file = current_file->included_by;
24814 if (! current_file)
24815 {
cf2c3c16 24816 enum dwarf_macro_record_type next_type;
2e276125
JB
24817
24818 /* GCC circa March 2002 doesn't produce the zero
24819 type byte marking the end of the compilation
24820 unit. Complain if it's not there, but exit no
24821 matter what. */
24822
24823 /* Do we at least have room for a macinfo type byte? */
24824 if (mac_ptr >= mac_end)
24825 {
f664829e 24826 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24827 return;
24828 }
24829
24830 /* We don't increment mac_ptr here, so this is just
24831 a look-ahead. */
aead7601
SM
24832 next_type
24833 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24834 mac_ptr);
2e276125 24835 if (next_type != 0)
b98664d3 24836 complaint (_("no terminating 0-type entry for "
3e43a32a 24837 "macros in `.debug_macinfo' section"));
2e276125
JB
24838
24839 return;
24840 }
24841 }
24842 break;
24843
0af92d60
JK
24844 case DW_MACRO_import:
24845 case DW_MACRO_import_sup:
cf2c3c16
TT
24846 {
24847 LONGEST offset;
8fc3fc34 24848 void **slot;
a036ba48
TT
24849 bfd *include_bfd = abfd;
24850 struct dwarf2_section_info *include_section = section;
d521ce57 24851 const gdb_byte *include_mac_end = mac_end;
a036ba48 24852 int is_dwz = section_is_dwz;
d521ce57 24853 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24854
24855 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24856 mac_ptr += offset_size;
24857
0af92d60 24858 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24859 {
ed2dc618 24860 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24861
4d663531 24862 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24863
a036ba48 24864 include_section = &dwz->macro;
a32a8923 24865 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24866 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24867 is_dwz = 1;
24868 }
24869
24870 new_mac_ptr = include_section->buffer + offset;
24871 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24872
8fc3fc34
TT
24873 if (*slot != NULL)
24874 {
24875 /* This has actually happened; see
24876 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24877 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24878 ".debug_macro section"));
24879 }
24880 else
24881 {
d521ce57 24882 *slot = (void *) new_mac_ptr;
36586728 24883
804d2729 24884 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24885 include_mac_end, current_file, lh,
36586728 24886 section, section_is_gnu, is_dwz,
4d663531 24887 offset_size, include_hash);
8fc3fc34 24888
d521ce57 24889 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24890 }
cf2c3c16
TT
24891 }
24892 break;
24893
2e276125 24894 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24895 if (!section_is_gnu)
24896 {
24897 unsigned int bytes_read;
2e276125 24898
ac298888
TT
24899 /* This reads the constant, but since we don't recognize
24900 any vendor extensions, we ignore it. */
24901 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24902 mac_ptr += bytes_read;
24903 read_direct_string (abfd, mac_ptr, &bytes_read);
24904 mac_ptr += bytes_read;
2e276125 24905
cf2c3c16
TT
24906 /* We don't recognize any vendor extensions. */
24907 break;
24908 }
24909 /* FALLTHROUGH */
24910
24911 default:
24912 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24913 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24914 section);
24915 if (mac_ptr == NULL)
24916 return;
24917 break;
2e276125 24918 }
132448f8 24919 DIAGNOSTIC_POP
757a13d0 24920 } while (macinfo_type != 0);
2e276125 24921}
8e19ed76 24922
cf2c3c16 24923static void
09262596 24924dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24925 int section_is_gnu)
cf2c3c16 24926{
518817b3
SM
24927 struct dwarf2_per_objfile *dwarf2_per_objfile
24928 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24929 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24930 struct line_header *lh = cu->line_header;
24931 bfd *abfd;
d521ce57 24932 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24933 struct macro_source_file *current_file = 0;
24934 enum dwarf_macro_record_type macinfo_type;
24935 unsigned int offset_size = cu->header.offset_size;
d521ce57 24936 const gdb_byte *opcode_definitions[256];
8fc3fc34 24937 void **slot;
09262596
DE
24938 struct dwarf2_section_info *section;
24939 const char *section_name;
24940
24941 if (cu->dwo_unit != NULL)
24942 {
24943 if (section_is_gnu)
24944 {
24945 section = &cu->dwo_unit->dwo_file->sections.macro;
24946 section_name = ".debug_macro.dwo";
24947 }
24948 else
24949 {
24950 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24951 section_name = ".debug_macinfo.dwo";
24952 }
24953 }
24954 else
24955 {
24956 if (section_is_gnu)
24957 {
24958 section = &dwarf2_per_objfile->macro;
24959 section_name = ".debug_macro";
24960 }
24961 else
24962 {
24963 section = &dwarf2_per_objfile->macinfo;
24964 section_name = ".debug_macinfo";
24965 }
24966 }
cf2c3c16 24967
bb5ed363 24968 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24969 if (section->buffer == NULL)
24970 {
b98664d3 24971 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24972 return;
24973 }
a32a8923 24974 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24975
24976 /* First pass: Find the name of the base filename.
24977 This filename is needed in order to process all macros whose definition
24978 (or undefinition) comes from the command line. These macros are defined
24979 before the first DW_MACINFO_start_file entry, and yet still need to be
24980 associated to the base file.
24981
24982 To determine the base file name, we scan the macro definitions until we
24983 reach the first DW_MACINFO_start_file entry. We then initialize
24984 CURRENT_FILE accordingly so that any macro definition found before the
24985 first DW_MACINFO_start_file can still be associated to the base file. */
24986
24987 mac_ptr = section->buffer + offset;
24988 mac_end = section->buffer + section->size;
24989
24990 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24991 &offset_size, section_is_gnu);
24992 if (mac_ptr == NULL)
24993 {
24994 /* We already issued a complaint. */
24995 return;
24996 }
24997
24998 do
24999 {
25000 /* Do we at least have room for a macinfo type byte? */
25001 if (mac_ptr >= mac_end)
25002 {
25003 /* Complaint is printed during the second pass as GDB will probably
25004 stop the first pass earlier upon finding
25005 DW_MACINFO_start_file. */
25006 break;
25007 }
25008
aead7601 25009 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25010 mac_ptr++;
25011
25012 /* Note that we rely on the fact that the corresponding GNU and
25013 DWARF constants are the same. */
132448f8
SM
25014 DIAGNOSTIC_PUSH
25015 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25016 switch (macinfo_type)
25017 {
25018 /* A zero macinfo type indicates the end of the macro
25019 information. */
25020 case 0:
25021 break;
25022
0af92d60
JK
25023 case DW_MACRO_define:
25024 case DW_MACRO_undef:
cf2c3c16
TT
25025 /* Only skip the data by MAC_PTR. */
25026 {
25027 unsigned int bytes_read;
25028
25029 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25030 mac_ptr += bytes_read;
25031 read_direct_string (abfd, mac_ptr, &bytes_read);
25032 mac_ptr += bytes_read;
25033 }
25034 break;
25035
0af92d60 25036 case DW_MACRO_start_file:
cf2c3c16
TT
25037 {
25038 unsigned int bytes_read;
25039 int line, file;
25040
25041 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25042 mac_ptr += bytes_read;
25043 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25044 mac_ptr += bytes_read;
25045
804d2729 25046 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25047 }
25048 break;
25049
0af92d60 25050 case DW_MACRO_end_file:
cf2c3c16
TT
25051 /* No data to skip by MAC_PTR. */
25052 break;
25053
0af92d60
JK
25054 case DW_MACRO_define_strp:
25055 case DW_MACRO_undef_strp:
25056 case DW_MACRO_define_sup:
25057 case DW_MACRO_undef_sup:
cf2c3c16
TT
25058 {
25059 unsigned int bytes_read;
25060
25061 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25062 mac_ptr += bytes_read;
25063 mac_ptr += offset_size;
25064 }
25065 break;
25066
0af92d60
JK
25067 case DW_MACRO_import:
25068 case DW_MACRO_import_sup:
cf2c3c16 25069 /* Note that, according to the spec, a transparent include
0af92d60 25070 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25071 skip this opcode. */
25072 mac_ptr += offset_size;
25073 break;
25074
25075 case DW_MACINFO_vendor_ext:
25076 /* Only skip the data by MAC_PTR. */
25077 if (!section_is_gnu)
25078 {
25079 unsigned int bytes_read;
25080
25081 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25082 mac_ptr += bytes_read;
25083 read_direct_string (abfd, mac_ptr, &bytes_read);
25084 mac_ptr += bytes_read;
25085 }
25086 /* FALLTHROUGH */
25087
25088 default:
25089 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25090 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25091 section);
25092 if (mac_ptr == NULL)
25093 return;
25094 break;
25095 }
132448f8 25096 DIAGNOSTIC_POP
cf2c3c16
TT
25097 } while (macinfo_type != 0 && current_file == NULL);
25098
25099 /* Second pass: Process all entries.
25100
25101 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25102 command-line macro definitions/undefinitions. This flag is unset when we
25103 reach the first DW_MACINFO_start_file entry. */
25104
fc4007c9
TT
25105 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25106 htab_eq_pointer,
25107 NULL, xcalloc, xfree));
8fc3fc34 25108 mac_ptr = section->buffer + offset;
fc4007c9 25109 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25110 *slot = (void *) mac_ptr;
804d2729 25111 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25112 current_file, lh, section,
fc4007c9
TT
25113 section_is_gnu, 0, offset_size,
25114 include_hash.get ());
cf2c3c16
TT
25115}
25116
8e19ed76 25117/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25118 if so return true else false. */
380bca97 25119
8e19ed76 25120static int
6e5a29e1 25121attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25122{
25123 return (attr == NULL ? 0 :
25124 attr->form == DW_FORM_block1
25125 || attr->form == DW_FORM_block2
25126 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25127 || attr->form == DW_FORM_block
25128 || attr->form == DW_FORM_exprloc);
8e19ed76 25129}
4c2df51b 25130
c6a0999f
JB
25131/* Return non-zero if ATTR's value is a section offset --- classes
25132 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25133 You may use DW_UNSND (attr) to retrieve such offsets.
25134
25135 Section 7.5.4, "Attribute Encodings", explains that no attribute
25136 may have a value that belongs to more than one of these classes; it
25137 would be ambiguous if we did, because we use the same forms for all
25138 of them. */
380bca97 25139
3690dd37 25140static int
6e5a29e1 25141attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25142{
25143 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25144 || attr->form == DW_FORM_data8
25145 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25146}
25147
3690dd37
JB
25148/* Return non-zero if ATTR's value falls in the 'constant' class, or
25149 zero otherwise. When this function returns true, you can apply
25150 dwarf2_get_attr_constant_value to it.
25151
25152 However, note that for some attributes you must check
25153 attr_form_is_section_offset before using this test. DW_FORM_data4
25154 and DW_FORM_data8 are members of both the constant class, and of
25155 the classes that contain offsets into other debug sections
25156 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25157 that, if an attribute's can be either a constant or one of the
25158 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25159 taken as section offsets, not constants.
25160
25161 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25162 cannot handle that. */
380bca97 25163
3690dd37 25164static int
6e5a29e1 25165attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25166{
25167 switch (attr->form)
25168 {
25169 case DW_FORM_sdata:
25170 case DW_FORM_udata:
25171 case DW_FORM_data1:
25172 case DW_FORM_data2:
25173 case DW_FORM_data4:
25174 case DW_FORM_data8:
663c44ac 25175 case DW_FORM_implicit_const:
3690dd37
JB
25176 return 1;
25177 default:
25178 return 0;
25179 }
25180}
25181
7771576e
SA
25182
25183/* DW_ADDR is always stored already as sect_offset; despite for the forms
25184 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25185
25186static int
6e5a29e1 25187attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25188{
25189 switch (attr->form)
25190 {
25191 case DW_FORM_ref_addr:
25192 case DW_FORM_ref1:
25193 case DW_FORM_ref2:
25194 case DW_FORM_ref4:
25195 case DW_FORM_ref8:
25196 case DW_FORM_ref_udata:
25197 case DW_FORM_GNU_ref_alt:
25198 return 1;
25199 default:
25200 return 0;
25201 }
25202}
25203
3019eac3
DE
25204/* Return the .debug_loc section to use for CU.
25205 For DWO files use .debug_loc.dwo. */
25206
25207static struct dwarf2_section_info *
25208cu_debug_loc_section (struct dwarf2_cu *cu)
25209{
518817b3
SM
25210 struct dwarf2_per_objfile *dwarf2_per_objfile
25211 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25212
3019eac3 25213 if (cu->dwo_unit)
43988095
JK
25214 {
25215 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25216
43988095
JK
25217 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25218 }
25219 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25220 : &dwarf2_per_objfile->loc);
3019eac3
DE
25221}
25222
8cf6f0b1
TT
25223/* A helper function that fills in a dwarf2_loclist_baton. */
25224
25225static void
25226fill_in_loclist_baton (struct dwarf2_cu *cu,
25227 struct dwarf2_loclist_baton *baton,
ff39bb5e 25228 const struct attribute *attr)
8cf6f0b1 25229{
518817b3
SM
25230 struct dwarf2_per_objfile *dwarf2_per_objfile
25231 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25232 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25233
25234 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25235
25236 baton->per_cu = cu->per_cu;
25237 gdb_assert (baton->per_cu);
25238 /* We don't know how long the location list is, but make sure we
25239 don't run off the edge of the section. */
3019eac3
DE
25240 baton->size = section->size - DW_UNSND (attr);
25241 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25242 baton->base_address = cu->base_address;
f664829e 25243 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25244}
25245
4c2df51b 25246static void
ff39bb5e 25247dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25248 struct dwarf2_cu *cu, int is_block)
4c2df51b 25249{
518817b3
SM
25250 struct dwarf2_per_objfile *dwarf2_per_objfile
25251 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25252 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25253 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25254
3690dd37 25255 if (attr_form_is_section_offset (attr)
3019eac3 25256 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25257 the section. If so, fall through to the complaint in the
25258 other branch. */
3019eac3 25259 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25260 {
0d53c4c4 25261 struct dwarf2_loclist_baton *baton;
4c2df51b 25262
8d749320 25263 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25264
8cf6f0b1 25265 fill_in_loclist_baton (cu, baton, attr);
be391dca 25266
d00adf39 25267 if (cu->base_known == 0)
b98664d3 25268 complaint (_("Location list used without "
3e43a32a 25269 "specifying the CU base address."));
4c2df51b 25270
f1e6e072
TT
25271 SYMBOL_ACLASS_INDEX (sym) = (is_block
25272 ? dwarf2_loclist_block_index
25273 : dwarf2_loclist_index);
0d53c4c4
DJ
25274 SYMBOL_LOCATION_BATON (sym) = baton;
25275 }
25276 else
25277 {
25278 struct dwarf2_locexpr_baton *baton;
25279
8d749320 25280 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25281 baton->per_cu = cu->per_cu;
25282 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25283
25284 if (attr_form_is_block (attr))
25285 {
25286 /* Note that we're just copying the block's data pointer
25287 here, not the actual data. We're still pointing into the
6502dd73
DJ
25288 info_buffer for SYM's objfile; right now we never release
25289 that buffer, but when we do clean up properly this may
25290 need to change. */
0d53c4c4
DJ
25291 baton->size = DW_BLOCK (attr)->size;
25292 baton->data = DW_BLOCK (attr)->data;
25293 }
25294 else
25295 {
25296 dwarf2_invalid_attrib_class_complaint ("location description",
25297 SYMBOL_NATURAL_NAME (sym));
25298 baton->size = 0;
0d53c4c4 25299 }
6e70227d 25300
f1e6e072
TT
25301 SYMBOL_ACLASS_INDEX (sym) = (is_block
25302 ? dwarf2_locexpr_block_index
25303 : dwarf2_locexpr_index);
0d53c4c4
DJ
25304 SYMBOL_LOCATION_BATON (sym) = baton;
25305 }
4c2df51b 25306}
6502dd73 25307
9aa1f1e3
TT
25308/* Return the OBJFILE associated with the compilation unit CU. If CU
25309 came from a separate debuginfo file, then the master objfile is
25310 returned. */
ae0d2f24
UW
25311
25312struct objfile *
25313dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25314{
e3b94546 25315 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25316
25317 /* Return the master objfile, so that we can report and look up the
25318 correct file containing this variable. */
25319 if (objfile->separate_debug_objfile_backlink)
25320 objfile = objfile->separate_debug_objfile_backlink;
25321
25322 return objfile;
25323}
25324
96408a79
SA
25325/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25326 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25327 CU_HEADERP first. */
25328
25329static const struct comp_unit_head *
25330per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25331 struct dwarf2_per_cu_data *per_cu)
25332{
d521ce57 25333 const gdb_byte *info_ptr;
96408a79
SA
25334
25335 if (per_cu->cu)
25336 return &per_cu->cu->header;
25337
9c541725 25338 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25339
25340 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25341 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25342 rcuh_kind::COMPILE);
96408a79
SA
25343
25344 return cu_headerp;
25345}
25346
ae0d2f24
UW
25347/* Return the address size given in the compilation unit header for CU. */
25348
98714339 25349int
ae0d2f24
UW
25350dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25351{
96408a79
SA
25352 struct comp_unit_head cu_header_local;
25353 const struct comp_unit_head *cu_headerp;
c471e790 25354
96408a79
SA
25355 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25356
25357 return cu_headerp->addr_size;
ae0d2f24
UW
25358}
25359
9eae7c52
TT
25360/* Return the offset size given in the compilation unit header for CU. */
25361
25362int
25363dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25364{
96408a79
SA
25365 struct comp_unit_head cu_header_local;
25366 const struct comp_unit_head *cu_headerp;
9c6c53f7 25367
96408a79
SA
25368 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25369
25370 return cu_headerp->offset_size;
25371}
25372
25373/* See its dwarf2loc.h declaration. */
25374
25375int
25376dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25377{
25378 struct comp_unit_head cu_header_local;
25379 const struct comp_unit_head *cu_headerp;
25380
25381 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25382
25383 if (cu_headerp->version == 2)
25384 return cu_headerp->addr_size;
25385 else
25386 return cu_headerp->offset_size;
181cebd4
JK
25387}
25388
9aa1f1e3
TT
25389/* Return the text offset of the CU. The returned offset comes from
25390 this CU's objfile. If this objfile came from a separate debuginfo
25391 file, then the offset may be different from the corresponding
25392 offset in the parent objfile. */
25393
25394CORE_ADDR
25395dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25396{
e3b94546 25397 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25398
25399 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25400}
25401
9a49df9d
AB
25402/* Return a type that is a generic pointer type, the size of which matches
25403 the address size given in the compilation unit header for PER_CU. */
25404static struct type *
25405dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25406{
25407 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25408 struct type *void_type = objfile_type (objfile)->builtin_void;
25409 struct type *addr_type = lookup_pointer_type (void_type);
25410 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25411
25412 if (TYPE_LENGTH (addr_type) == addr_size)
25413 return addr_type;
25414
25415 addr_type
25416 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25417 return addr_type;
25418}
25419
43988095
JK
25420/* Return DWARF version number of PER_CU. */
25421
25422short
25423dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25424{
25425 return per_cu->dwarf_version;
25426}
25427
348e048f
DE
25428/* Locate the .debug_info compilation unit from CU's objfile which contains
25429 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25430
25431static struct dwarf2_per_cu_data *
9c541725 25432dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25433 unsigned int offset_in_dwz,
ed2dc618 25434 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25435{
25436 struct dwarf2_per_cu_data *this_cu;
25437 int low, high;
25438
ae038cb0 25439 low = 0;
b76e467d 25440 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25441 while (high > low)
25442 {
36586728 25443 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25444 int mid = low + (high - low) / 2;
9a619af0 25445
36586728 25446 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25447 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25448 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25449 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25450 high = mid;
25451 else
25452 low = mid + 1;
25453 }
25454 gdb_assert (low == high);
36586728 25455 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25456 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25457 {
36586728 25458 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25459 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25460 "offset %s [in module %s]"),
25461 sect_offset_str (sect_off),
ed2dc618 25462 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25463
9c541725
PA
25464 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25465 <= sect_off);
ae038cb0
DJ
25466 return dwarf2_per_objfile->all_comp_units[low-1];
25467 }
25468 else
25469 {
b76e467d 25470 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25471 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25472 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25473 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25474 return this_cu;
25475 }
25476}
25477
23745b47 25478/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25479
fcd3b13d
SM
25480dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25481 : per_cu (per_cu_),
9068261f
AB
25482 mark (false),
25483 has_loclist (false),
25484 checked_producer (false),
25485 producer_is_gxx_lt_4_6 (false),
25486 producer_is_gcc_lt_4_3 (false),
eb77c9df 25487 producer_is_icc (false),
9068261f 25488 producer_is_icc_lt_14 (false),
c258c396 25489 producer_is_codewarrior (false),
9068261f 25490 processing_has_namespace_info (false)
93311388 25491{
fcd3b13d
SM
25492 per_cu->cu = this;
25493}
25494
25495/* Destroy a dwarf2_cu. */
25496
25497dwarf2_cu::~dwarf2_cu ()
25498{
25499 per_cu->cu = NULL;
9816fde3
JK
25500}
25501
25502/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25503
25504static void
95554aad
TT
25505prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25506 enum language pretend_language)
9816fde3
JK
25507{
25508 struct attribute *attr;
25509
25510 /* Set the language we're debugging. */
25511 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25512 if (attr)
25513 set_cu_language (DW_UNSND (attr), cu);
25514 else
9cded63f 25515 {
95554aad 25516 cu->language = pretend_language;
9cded63f
TT
25517 cu->language_defn = language_def (cu->language);
25518 }
dee91e82 25519
7d45c7c3 25520 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25521}
25522
ae038cb0
DJ
25523/* Increase the age counter on each cached compilation unit, and free
25524 any that are too old. */
25525
25526static void
ed2dc618 25527age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25528{
25529 struct dwarf2_per_cu_data *per_cu, **last_chain;
25530
25531 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25532 per_cu = dwarf2_per_objfile->read_in_chain;
25533 while (per_cu != NULL)
25534 {
25535 per_cu->cu->last_used ++;
b4f54984 25536 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25537 dwarf2_mark (per_cu->cu);
25538 per_cu = per_cu->cu->read_in_chain;
25539 }
25540
25541 per_cu = dwarf2_per_objfile->read_in_chain;
25542 last_chain = &dwarf2_per_objfile->read_in_chain;
25543 while (per_cu != NULL)
25544 {
25545 struct dwarf2_per_cu_data *next_cu;
25546
25547 next_cu = per_cu->cu->read_in_chain;
25548
25549 if (!per_cu->cu->mark)
25550 {
fcd3b13d 25551 delete per_cu->cu;
ae038cb0
DJ
25552 *last_chain = next_cu;
25553 }
25554 else
25555 last_chain = &per_cu->cu->read_in_chain;
25556
25557 per_cu = next_cu;
25558 }
25559}
25560
25561/* Remove a single compilation unit from the cache. */
25562
25563static void
dee91e82 25564free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25565{
25566 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25567 struct dwarf2_per_objfile *dwarf2_per_objfile
25568 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25569
25570 per_cu = dwarf2_per_objfile->read_in_chain;
25571 last_chain = &dwarf2_per_objfile->read_in_chain;
25572 while (per_cu != NULL)
25573 {
25574 struct dwarf2_per_cu_data *next_cu;
25575
25576 next_cu = per_cu->cu->read_in_chain;
25577
dee91e82 25578 if (per_cu == target_per_cu)
ae038cb0 25579 {
fcd3b13d 25580 delete per_cu->cu;
dee91e82 25581 per_cu->cu = NULL;
ae038cb0
DJ
25582 *last_chain = next_cu;
25583 break;
25584 }
25585 else
25586 last_chain = &per_cu->cu->read_in_chain;
25587
25588 per_cu = next_cu;
25589 }
25590}
25591
dee91e82
DE
25592/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25593 We store these in a hash table separate from the DIEs, and preserve them
25594 when the DIEs are flushed out of cache.
25595
25596 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25597 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25598 or the type may come from a DWO file. Furthermore, while it's more logical
25599 to use per_cu->section+offset, with Fission the section with the data is in
25600 the DWO file but we don't know that section at the point we need it.
25601 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25602 because we can enter the lookup routine, get_die_type_at_offset, from
25603 outside this file, and thus won't necessarily have PER_CU->cu.
25604 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25605
dee91e82 25606struct dwarf2_per_cu_offset_and_type
1c379e20 25607{
dee91e82 25608 const struct dwarf2_per_cu_data *per_cu;
9c541725 25609 sect_offset sect_off;
1c379e20
DJ
25610 struct type *type;
25611};
25612
dee91e82 25613/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25614
25615static hashval_t
dee91e82 25616per_cu_offset_and_type_hash (const void *item)
1c379e20 25617{
9a3c8263
SM
25618 const struct dwarf2_per_cu_offset_and_type *ofs
25619 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25620
9c541725 25621 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25622}
25623
dee91e82 25624/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25625
25626static int
dee91e82 25627per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25628{
9a3c8263
SM
25629 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25630 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25631 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25632 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25633
dee91e82 25634 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25635 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25636}
25637
25638/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25639 table if necessary. For convenience, return TYPE.
25640
25641 The DIEs reading must have careful ordering to:
25642 * Not cause infite loops trying to read in DIEs as a prerequisite for
25643 reading current DIE.
25644 * Not trying to dereference contents of still incompletely read in types
25645 while reading in other DIEs.
25646 * Enable referencing still incompletely read in types just by a pointer to
25647 the type without accessing its fields.
25648
25649 Therefore caller should follow these rules:
25650 * Try to fetch any prerequisite types we may need to build this DIE type
25651 before building the type and calling set_die_type.
e71ec853 25652 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25653 possible before fetching more types to complete the current type.
25654 * Make the type as complete as possible before fetching more types. */
1c379e20 25655
f792889a 25656static struct type *
1c379e20
DJ
25657set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25658{
518817b3
SM
25659 struct dwarf2_per_objfile *dwarf2_per_objfile
25660 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25661 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25662 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25663 struct attribute *attr;
25664 struct dynamic_prop prop;
1c379e20 25665
b4ba55a1
JB
25666 /* For Ada types, make sure that the gnat-specific data is always
25667 initialized (if not already set). There are a few types where
25668 we should not be doing so, because the type-specific area is
25669 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25670 where the type-specific area is used to store the floatformat).
25671 But this is not a problem, because the gnat-specific information
25672 is actually not needed for these types. */
25673 if (need_gnat_info (cu)
25674 && TYPE_CODE (type) != TYPE_CODE_FUNC
25675 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25676 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25677 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25678 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25679 && !HAVE_GNAT_AUX_INFO (type))
25680 INIT_GNAT_SPECIFIC (type);
25681
3f2f83dd
KB
25682 /* Read DW_AT_allocated and set in type. */
25683 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25684 if (attr_form_is_block (attr))
25685 {
9a49df9d
AB
25686 struct type *prop_type
25687 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25688 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25689 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25690 }
25691 else if (attr != NULL)
25692 {
b98664d3 25693 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25694 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25695 sect_offset_str (die->sect_off));
3f2f83dd
KB
25696 }
25697
25698 /* Read DW_AT_associated and set in type. */
25699 attr = dwarf2_attr (die, DW_AT_associated, cu);
25700 if (attr_form_is_block (attr))
25701 {
9a49df9d
AB
25702 struct type *prop_type
25703 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25704 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25705 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25706 }
25707 else if (attr != NULL)
25708 {
b98664d3 25709 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25710 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25711 sect_offset_str (die->sect_off));
3f2f83dd
KB
25712 }
25713
3cdcd0ce
JB
25714 /* Read DW_AT_data_location and set in type. */
25715 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25716 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25717 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25718 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25719
dee91e82 25720 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25721 {
dee91e82
DE
25722 dwarf2_per_objfile->die_type_hash =
25723 htab_create_alloc_ex (127,
25724 per_cu_offset_and_type_hash,
25725 per_cu_offset_and_type_eq,
25726 NULL,
25727 &objfile->objfile_obstack,
25728 hashtab_obstack_allocate,
25729 dummy_obstack_deallocate);
f792889a 25730 }
1c379e20 25731
dee91e82 25732 ofs.per_cu = cu->per_cu;
9c541725 25733 ofs.sect_off = die->sect_off;
1c379e20 25734 ofs.type = type;
dee91e82
DE
25735 slot = (struct dwarf2_per_cu_offset_and_type **)
25736 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25737 if (*slot)
b98664d3 25738 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25739 sect_offset_str (die->sect_off));
8d749320
SM
25740 *slot = XOBNEW (&objfile->objfile_obstack,
25741 struct dwarf2_per_cu_offset_and_type);
1c379e20 25742 **slot = ofs;
f792889a 25743 return type;
1c379e20
DJ
25744}
25745
9c541725 25746/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25747 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25748
25749static struct type *
9c541725 25750get_die_type_at_offset (sect_offset sect_off,
673bfd45 25751 struct dwarf2_per_cu_data *per_cu)
1c379e20 25752{
dee91e82 25753 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25754 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25755
dee91e82 25756 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25757 return NULL;
1c379e20 25758
dee91e82 25759 ofs.per_cu = per_cu;
9c541725 25760 ofs.sect_off = sect_off;
9a3c8263
SM
25761 slot = ((struct dwarf2_per_cu_offset_and_type *)
25762 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25763 if (slot)
25764 return slot->type;
25765 else
25766 return NULL;
25767}
25768
02142a6c 25769/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25770 or return NULL if DIE does not have a saved type. */
25771
25772static struct type *
25773get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25774{
9c541725 25775 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25776}
25777
10b3939b
DJ
25778/* Add a dependence relationship from CU to REF_PER_CU. */
25779
25780static void
25781dwarf2_add_dependence (struct dwarf2_cu *cu,
25782 struct dwarf2_per_cu_data *ref_per_cu)
25783{
25784 void **slot;
25785
25786 if (cu->dependencies == NULL)
25787 cu->dependencies
25788 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25789 NULL, &cu->comp_unit_obstack,
25790 hashtab_obstack_allocate,
25791 dummy_obstack_deallocate);
25792
25793 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25794 if (*slot == NULL)
25795 *slot = ref_per_cu;
25796}
1c379e20 25797
f504f079
DE
25798/* Subroutine of dwarf2_mark to pass to htab_traverse.
25799 Set the mark field in every compilation unit in the
ae038cb0
DJ
25800 cache that we must keep because we are keeping CU. */
25801
10b3939b
DJ
25802static int
25803dwarf2_mark_helper (void **slot, void *data)
25804{
25805 struct dwarf2_per_cu_data *per_cu;
25806
25807 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25808
25809 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25810 reading of the chain. As such dependencies remain valid it is not much
25811 useful to track and undo them during QUIT cleanups. */
25812 if (per_cu->cu == NULL)
25813 return 1;
25814
10b3939b
DJ
25815 if (per_cu->cu->mark)
25816 return 1;
9068261f 25817 per_cu->cu->mark = true;
10b3939b
DJ
25818
25819 if (per_cu->cu->dependencies != NULL)
25820 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25821
25822 return 1;
25823}
25824
f504f079
DE
25825/* Set the mark field in CU and in every other compilation unit in the
25826 cache that we must keep because we are keeping CU. */
25827
ae038cb0
DJ
25828static void
25829dwarf2_mark (struct dwarf2_cu *cu)
25830{
25831 if (cu->mark)
25832 return;
9068261f 25833 cu->mark = true;
10b3939b
DJ
25834 if (cu->dependencies != NULL)
25835 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25836}
25837
25838static void
25839dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25840{
25841 while (per_cu)
25842 {
9068261f 25843 per_cu->cu->mark = false;
ae038cb0
DJ
25844 per_cu = per_cu->cu->read_in_chain;
25845 }
72bf9492
DJ
25846}
25847
72bf9492
DJ
25848/* Trivial hash function for partial_die_info: the hash value of a DIE
25849 is its offset in .debug_info for this objfile. */
25850
25851static hashval_t
25852partial_die_hash (const void *item)
25853{
9a3c8263
SM
25854 const struct partial_die_info *part_die
25855 = (const struct partial_die_info *) item;
9a619af0 25856
9c541725 25857 return to_underlying (part_die->sect_off);
72bf9492
DJ
25858}
25859
25860/* Trivial comparison function for partial_die_info structures: two DIEs
25861 are equal if they have the same offset. */
25862
25863static int
25864partial_die_eq (const void *item_lhs, const void *item_rhs)
25865{
9a3c8263
SM
25866 const struct partial_die_info *part_die_lhs
25867 = (const struct partial_die_info *) item_lhs;
25868 const struct partial_die_info *part_die_rhs
25869 = (const struct partial_die_info *) item_rhs;
9a619af0 25870
9c541725 25871 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25872}
25873
3c3bb058
AB
25874struct cmd_list_element *set_dwarf_cmdlist;
25875struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25876
25877static void
981a3fb3 25878set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25879{
b4f54984 25880 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25881 gdb_stdout);
ae038cb0
DJ
25882}
25883
25884static void
981a3fb3 25885show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25886{
b4f54984 25887 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25888}
25889
491144b5 25890bool dwarf_always_disassemble;
437afbb8 25891
437afbb8 25892static void
cd4fb1b2
SM
25893show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25894 struct cmd_list_element *c, const char *value)
9291a0cd 25895{
cd4fb1b2
SM
25896 fprintf_filtered (file,
25897 _("Whether to always disassemble "
25898 "DWARF expressions is %s.\n"),
25899 value);
9291a0cd
TT
25900}
25901
9291a0cd 25902static void
cd4fb1b2
SM
25903show_check_physname (struct ui_file *file, int from_tty,
25904 struct cmd_list_element *c, const char *value)
9291a0cd 25905{
cd4fb1b2
SM
25906 fprintf_filtered (file,
25907 _("Whether to check \"physname\" is %s.\n"),
25908 value);
9291a0cd
TT
25909}
25910
cd4fb1b2
SM
25911void
25912_initialize_dwarf2_read (void)
9291a0cd 25913{
cd4fb1b2
SM
25914 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25915Set DWARF specific variables.\n\
590042fc 25916Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25917 &set_dwarf_cmdlist, "maintenance set dwarf ",
25918 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25919
cd4fb1b2 25920 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25921Show DWARF specific variables.\n\
25922Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25923 &show_dwarf_cmdlist, "maintenance show dwarf ",
25924 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25925
cd4fb1b2
SM
25926 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25927 &dwarf_max_cache_age, _("\
25928Set the upper bound on the age of cached DWARF compilation units."), _("\
25929Show the upper bound on the age of cached DWARF compilation units."), _("\
25930A higher limit means that cached compilation units will be stored\n\
25931in memory longer, and more total memory will be used. Zero disables\n\
25932caching, which can slow down startup."),
25933 NULL,
25934 show_dwarf_max_cache_age,
25935 &set_dwarf_cmdlist,
25936 &show_dwarf_cmdlist);
156942c7 25937
cd4fb1b2
SM
25938 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25939 &dwarf_always_disassemble, _("\
25940Set whether `info address' always disassembles DWARF expressions."), _("\
25941Show whether `info address' always disassembles DWARF expressions."), _("\
25942When enabled, DWARF expressions are always printed in an assembly-like\n\
25943syntax. When disabled, expressions will be printed in a more\n\
25944conversational style, when possible."),
25945 NULL,
25946 show_dwarf_always_disassemble,
25947 &set_dwarf_cmdlist,
25948 &show_dwarf_cmdlist);
9291a0cd 25949
cd4fb1b2
SM
25950 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25951Set debugging of the DWARF reader."), _("\
25952Show debugging of the DWARF reader."), _("\
25953When enabled (non-zero), debugging messages are printed during DWARF\n\
25954reading and symtab expansion. A value of 1 (one) provides basic\n\
25955information. A value greater than 1 provides more verbose information."),
25956 NULL,
25957 NULL,
25958 &setdebuglist, &showdebuglist);
9291a0cd 25959
cd4fb1b2
SM
25960 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25961Set debugging of the DWARF DIE reader."), _("\
25962Show debugging of the DWARF DIE reader."), _("\
25963When enabled (non-zero), DIEs are dumped after they are read in.\n\
25964The value is the maximum depth to print."),
25965 NULL,
25966 NULL,
25967 &setdebuglist, &showdebuglist);
9291a0cd 25968
cd4fb1b2
SM
25969 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25970Set debugging of the dwarf line reader."), _("\
25971Show debugging of the dwarf line reader."), _("\
25972When enabled (non-zero), line number entries are dumped as they are read in.\n\
25973A value of 1 (one) provides basic information.\n\
25974A value greater than 1 provides more verbose information."),
25975 NULL,
25976 NULL,
25977 &setdebuglist, &showdebuglist);
437afbb8 25978
cd4fb1b2
SM
25979 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25980Set cross-checking of \"physname\" code against demangler."), _("\
25981Show cross-checking of \"physname\" code against demangler."), _("\
25982When enabled, GDB's internal \"physname\" code is checked against\n\
25983the demangler."),
25984 NULL, show_check_physname,
25985 &setdebuglist, &showdebuglist);
900e11f9 25986
e615022a
DE
25987 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25988 no_class, &use_deprecated_index_sections, _("\
25989Set whether to use deprecated gdb_index sections."), _("\
25990Show whether to use deprecated gdb_index sections."), _("\
25991When enabled, deprecated .gdb_index sections are used anyway.\n\
25992Normally they are ignored either because of a missing feature or\n\
25993performance issue.\n\
25994Warning: This option must be enabled before gdb reads the file."),
25995 NULL,
25996 NULL,
25997 &setlist, &showlist);
25998
f1e6e072
TT
25999 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26000 &dwarf2_locexpr_funcs);
26001 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26002 &dwarf2_loclist_funcs);
26003
26004 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26005 &dwarf2_block_frame_base_locexpr_funcs);
26006 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26007 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26008
26009#if GDB_SELF_TEST
26010 selftests::register_test ("dw2_expand_symtabs_matching",
26011 selftests::dw2_expand_symtabs_matching::run_test);
26012#endif
6502dd73 26013}
This page took 5.030385 seconds and 4 git commands to generate.