gdb: Remove a use of VEC from dwarf2read.{c,h}
[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. */
df07e2c7 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_,
2141 const dwarf2_debug_sections *names)
2142 : objfile (objfile_)
2143{
2144 if (names == NULL)
2145 names = &dwarf2_elf_names;
2146
2147 bfd *obfd = objfile->obfd;
2148
2149 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2150 locate_sections (obfd, sec, *names);
2151}
2152
2153dwarf2_per_objfile::~dwarf2_per_objfile ()
2154{
2155 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2156 free_cached_comp_units ();
2157
2158 if (quick_file_names_table)
2159 htab_delete (quick_file_names_table);
2160
2161 if (line_header_hash)
2162 htab_delete (line_header_hash);
2163
b76e467d
SM
2164 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2165 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2166
b2bdb8cf
SM
2167 for (signatured_type *sig_type : all_type_units)
2168 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75 2169
330cdd98
PA
2170 /* Everything else should be on the objfile obstack. */
2171}
2172
2173/* See declaration. */
2174
2175void
2176dwarf2_per_objfile::free_cached_comp_units ()
2177{
2178 dwarf2_per_cu_data *per_cu = read_in_chain;
2179 dwarf2_per_cu_data **last_chain = &read_in_chain;
2180 while (per_cu != NULL)
2181 {
2182 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2183
fcd3b13d 2184 delete per_cu->cu;
330cdd98
PA
2185 *last_chain = next_cu;
2186 per_cu = next_cu;
2187 }
2188}
2189
11ed8cad
TT
2190/* A helper class that calls free_cached_comp_units on
2191 destruction. */
2192
2193class free_cached_comp_units
2194{
2195public:
2196
2197 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2198 : m_per_objfile (per_objfile)
2199 {
2200 }
2201
2202 ~free_cached_comp_units ()
2203 {
2204 m_per_objfile->free_cached_comp_units ();
2205 }
2206
2207 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2208
2209private:
2210
2211 dwarf2_per_objfile *m_per_objfile;
2212};
2213
c906108c 2214/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2215 information and return true if we have enough to do something.
2216 NAMES points to the dwarf2 section names, or is NULL if the standard
2217 ELF names are used. */
c906108c
SS
2218
2219int
251d32d9
TG
2220dwarf2_has_info (struct objfile *objfile,
2221 const struct dwarf2_debug_sections *names)
c906108c 2222{
97cbe998
SDJ
2223 if (objfile->flags & OBJF_READNEVER)
2224 return 0;
2225
ed2dc618
SM
2226 struct dwarf2_per_objfile *dwarf2_per_objfile
2227 = get_dwarf2_per_objfile (objfile);
2228
2229 if (dwarf2_per_objfile == NULL)
5bfd760d
TT
2230 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2231 names);
2232
73869dc2 2233 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2234 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2235 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2236 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2237}
2238
2239/* Return the containing section of virtual section SECTION. */
2240
2241static struct dwarf2_section_info *
2242get_containing_section (const struct dwarf2_section_info *section)
2243{
2244 gdb_assert (section->is_virtual);
2245 return section->s.containing_section;
c906108c
SS
2246}
2247
a32a8923
DE
2248/* Return the bfd owner of SECTION. */
2249
2250static struct bfd *
2251get_section_bfd_owner (const struct dwarf2_section_info *section)
2252{
73869dc2
DE
2253 if (section->is_virtual)
2254 {
2255 section = get_containing_section (section);
2256 gdb_assert (!section->is_virtual);
2257 }
049412e3 2258 return section->s.section->owner;
a32a8923
DE
2259}
2260
2261/* Return the bfd section of SECTION.
2262 Returns NULL if the section is not present. */
2263
2264static asection *
2265get_section_bfd_section (const struct dwarf2_section_info *section)
2266{
73869dc2
DE
2267 if (section->is_virtual)
2268 {
2269 section = get_containing_section (section);
2270 gdb_assert (!section->is_virtual);
2271 }
049412e3 2272 return section->s.section;
a32a8923
DE
2273}
2274
2275/* Return the name of SECTION. */
2276
2277static const char *
2278get_section_name (const struct dwarf2_section_info *section)
2279{
2280 asection *sectp = get_section_bfd_section (section);
2281
2282 gdb_assert (sectp != NULL);
fd361982 2283 return bfd_section_name (sectp);
a32a8923
DE
2284}
2285
2286/* Return the name of the file SECTION is in. */
2287
2288static const char *
2289get_section_file_name (const struct dwarf2_section_info *section)
2290{
2291 bfd *abfd = get_section_bfd_owner (section);
2292
2293 return bfd_get_filename (abfd);
2294}
2295
2296/* Return the id of SECTION.
2297 Returns 0 if SECTION doesn't exist. */
2298
2299static int
2300get_section_id (const struct dwarf2_section_info *section)
2301{
2302 asection *sectp = get_section_bfd_section (section);
2303
2304 if (sectp == NULL)
2305 return 0;
2306 return sectp->id;
2307}
2308
2309/* Return the flags of SECTION.
73869dc2 2310 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2311
2312static int
2313get_section_flags (const struct dwarf2_section_info *section)
2314{
2315 asection *sectp = get_section_bfd_section (section);
2316
2317 gdb_assert (sectp != NULL);
fd361982 2318 return bfd_section_flags (sectp);
a32a8923
DE
2319}
2320
251d32d9
TG
2321/* When loading sections, we look either for uncompressed section or for
2322 compressed section names. */
233a11ab
CS
2323
2324static int
251d32d9
TG
2325section_is_p (const char *section_name,
2326 const struct dwarf2_section_names *names)
233a11ab 2327{
251d32d9
TG
2328 if (names->normal != NULL
2329 && strcmp (section_name, names->normal) == 0)
2330 return 1;
2331 if (names->compressed != NULL
2332 && strcmp (section_name, names->compressed) == 0)
2333 return 1;
2334 return 0;
233a11ab
CS
2335}
2336
330cdd98 2337/* See declaration. */
c906108c 2338
330cdd98
PA
2339void
2340dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2341 const dwarf2_debug_sections &names)
c906108c 2342{
fd361982 2343 flagword aflag = bfd_section_flags (sectp);
251d32d9 2344
dc7650b8
JK
2345 if ((aflag & SEC_HAS_CONTENTS) == 0)
2346 {
2347 }
330cdd98 2348 else if (section_is_p (sectp->name, &names.info))
c906108c 2349 {
330cdd98 2350 this->info.s.section = sectp;
fd361982 2351 this->info.size = bfd_section_size (sectp);
c906108c 2352 }
330cdd98 2353 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2354 {
330cdd98 2355 this->abbrev.s.section = sectp;
fd361982 2356 this->abbrev.size = bfd_section_size (sectp);
c906108c 2357 }
330cdd98 2358 else if (section_is_p (sectp->name, &names.line))
c906108c 2359 {
330cdd98 2360 this->line.s.section = sectp;
fd361982 2361 this->line.size = bfd_section_size (sectp);
c906108c 2362 }
330cdd98 2363 else if (section_is_p (sectp->name, &names.loc))
c906108c 2364 {
330cdd98 2365 this->loc.s.section = sectp;
fd361982 2366 this->loc.size = bfd_section_size (sectp);
c906108c 2367 }
330cdd98 2368 else if (section_is_p (sectp->name, &names.loclists))
43988095 2369 {
330cdd98 2370 this->loclists.s.section = sectp;
fd361982 2371 this->loclists.size = bfd_section_size (sectp);
43988095 2372 }
330cdd98 2373 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2374 {
330cdd98 2375 this->macinfo.s.section = sectp;
fd361982 2376 this->macinfo.size = bfd_section_size (sectp);
c906108c 2377 }
330cdd98 2378 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2379 {
330cdd98 2380 this->macro.s.section = sectp;
fd361982 2381 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2382 }
330cdd98 2383 else if (section_is_p (sectp->name, &names.str))
c906108c 2384 {
330cdd98 2385 this->str.s.section = sectp;
fd361982 2386 this->str.size = bfd_section_size (sectp);
c906108c 2387 }
330cdd98 2388 else if (section_is_p (sectp->name, &names.line_str))
43988095 2389 {
330cdd98 2390 this->line_str.s.section = sectp;
fd361982 2391 this->line_str.size = bfd_section_size (sectp);
43988095 2392 }
330cdd98 2393 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2394 {
330cdd98 2395 this->addr.s.section = sectp;
fd361982 2396 this->addr.size = bfd_section_size (sectp);
3019eac3 2397 }
330cdd98 2398 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2399 {
330cdd98 2400 this->frame.s.section = sectp;
fd361982 2401 this->frame.size = bfd_section_size (sectp);
b6af0555 2402 }
330cdd98 2403 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2404 {
330cdd98 2405 this->eh_frame.s.section = sectp;
fd361982 2406 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2407 }
330cdd98 2408 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2409 {
330cdd98 2410 this->ranges.s.section = sectp;
fd361982 2411 this->ranges.size = bfd_section_size (sectp);
af34e669 2412 }
330cdd98 2413 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2414 {
330cdd98 2415 this->rnglists.s.section = sectp;
fd361982 2416 this->rnglists.size = bfd_section_size (sectp);
43988095 2417 }
330cdd98 2418 else if (section_is_p (sectp->name, &names.types))
348e048f 2419 {
8b70b953
TT
2420 struct dwarf2_section_info type_section;
2421
2422 memset (&type_section, 0, sizeof (type_section));
049412e3 2423 type_section.s.section = sectp;
fd361982 2424 type_section.size = bfd_section_size (sectp);
8b70b953 2425
fd5866f6 2426 this->types.push_back (type_section);
348e048f 2427 }
330cdd98 2428 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2429 {
330cdd98 2430 this->gdb_index.s.section = sectp;
fd361982 2431 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2432 }
927aa2e7
JK
2433 else if (section_is_p (sectp->name, &names.debug_names))
2434 {
2435 this->debug_names.s.section = sectp;
fd361982 2436 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2437 }
2438 else if (section_is_p (sectp->name, &names.debug_aranges))
2439 {
2440 this->debug_aranges.s.section = sectp;
fd361982 2441 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2442 }
dce234bc 2443
fd361982
AM
2444 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2445 && bfd_section_vma (sectp) == 0)
330cdd98 2446 this->has_section_at_zero = true;
c906108c
SS
2447}
2448
fceca515
DE
2449/* A helper function that decides whether a section is empty,
2450 or not present. */
9e0ac564
TT
2451
2452static int
19ac8c2e 2453dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2454{
73869dc2
DE
2455 if (section->is_virtual)
2456 return section->size == 0;
049412e3 2457 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2458}
2459
cd4fb1b2 2460/* See dwarf2read.h. */
c906108c 2461
cd4fb1b2
SM
2462void
2463dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2464{
a32a8923 2465 asection *sectp;
3019eac3 2466 bfd *abfd;
dce234bc 2467 gdb_byte *buf, *retbuf;
c906108c 2468
be391dca
TT
2469 if (info->readin)
2470 return;
dce234bc 2471 info->buffer = NULL;
dc4ccb6f 2472 info->readin = true;
188dd5d6 2473
9e0ac564 2474 if (dwarf2_section_empty_p (info))
dce234bc 2475 return;
c906108c 2476
a32a8923 2477 sectp = get_section_bfd_section (info);
3019eac3 2478
73869dc2
DE
2479 /* If this is a virtual section we need to read in the real one first. */
2480 if (info->is_virtual)
2481 {
2482 struct dwarf2_section_info *containing_section =
2483 get_containing_section (info);
2484
2485 gdb_assert (sectp != NULL);
2486 if ((sectp->flags & SEC_RELOC) != 0)
2487 {
2488 error (_("Dwarf Error: DWP format V2 with relocations is not"
2489 " supported in section %s [in module %s]"),
2490 get_section_name (info), get_section_file_name (info));
2491 }
2492 dwarf2_read_section (objfile, containing_section);
2493 /* Other code should have already caught virtual sections that don't
2494 fit. */
2495 gdb_assert (info->virtual_offset + info->size
2496 <= containing_section->size);
2497 /* If the real section is empty or there was a problem reading the
2498 section we shouldn't get here. */
2499 gdb_assert (containing_section->buffer != NULL);
2500 info->buffer = containing_section->buffer + info->virtual_offset;
2501 return;
2502 }
2503
4bf44c1c
TT
2504 /* If the section has relocations, we must read it ourselves.
2505 Otherwise we attach it to the BFD. */
2506 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2507 {
d521ce57 2508 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2509 return;
dce234bc 2510 }
dce234bc 2511
224c3ddb 2512 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2513 info->buffer = buf;
dce234bc
PP
2514
2515 /* When debugging .o files, we may need to apply relocations; see
2516 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2517 We never compress sections in .o files, so we only need to
2518 try this when the section is not compressed. */
ac8035ab 2519 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2520 if (retbuf != NULL)
2521 {
2522 info->buffer = retbuf;
2523 return;
2524 }
2525
a32a8923
DE
2526 abfd = get_section_bfd_owner (info);
2527 gdb_assert (abfd != NULL);
2528
dce234bc
PP
2529 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2530 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2531 {
2532 error (_("Dwarf Error: Can't read DWARF data"
2533 " in section %s [in module %s]"),
fd361982 2534 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2535 }
dce234bc
PP
2536}
2537
9e0ac564
TT
2538/* A helper function that returns the size of a section in a safe way.
2539 If you are positive that the section has been read before using the
2540 size, then it is safe to refer to the dwarf2_section_info object's
2541 "size" field directly. In other cases, you must call this
2542 function, because for compressed sections the size field is not set
2543 correctly until the section has been read. */
2544
2545static bfd_size_type
2546dwarf2_section_size (struct objfile *objfile,
2547 struct dwarf2_section_info *info)
2548{
2549 if (!info->readin)
2550 dwarf2_read_section (objfile, info);
2551 return info->size;
2552}
2553
dce234bc 2554/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2555 SECTION_NAME. */
af34e669 2556
dce234bc 2557void
3017a003
TG
2558dwarf2_get_section_info (struct objfile *objfile,
2559 enum dwarf2_section_enum sect,
d521ce57 2560 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2561 bfd_size_type *sizep)
2562{
5bfd760d 2563 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2564 struct dwarf2_section_info *info;
a3b2a86b
TT
2565
2566 /* We may see an objfile without any DWARF, in which case we just
2567 return nothing. */
2568 if (data == NULL)
2569 {
2570 *sectp = NULL;
2571 *bufp = NULL;
2572 *sizep = 0;
2573 return;
2574 }
3017a003
TG
2575 switch (sect)
2576 {
2577 case DWARF2_DEBUG_FRAME:
2578 info = &data->frame;
2579 break;
2580 case DWARF2_EH_FRAME:
2581 info = &data->eh_frame;
2582 break;
2583 default:
2584 gdb_assert_not_reached ("unexpected section");
2585 }
dce234bc 2586
9e0ac564 2587 dwarf2_read_section (objfile, info);
dce234bc 2588
a32a8923 2589 *sectp = get_section_bfd_section (info);
dce234bc
PP
2590 *bufp = info->buffer;
2591 *sizep = info->size;
2592}
2593
36586728
TT
2594/* A helper function to find the sections for a .dwz file. */
2595
2596static void
2597locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2598{
9a3c8263 2599 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2600
2601 /* Note that we only support the standard ELF names, because .dwz
2602 is ELF-only (at the time of writing). */
2603 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2604 {
049412e3 2605 dwz_file->abbrev.s.section = sectp;
fd361982 2606 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2607 }
2608 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2609 {
049412e3 2610 dwz_file->info.s.section = sectp;
fd361982 2611 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2612 }
2613 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2614 {
049412e3 2615 dwz_file->str.s.section = sectp;
fd361982 2616 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2617 }
2618 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2619 {
049412e3 2620 dwz_file->line.s.section = sectp;
fd361982 2621 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2622 }
2623 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2624 {
049412e3 2625 dwz_file->macro.s.section = sectp;
fd361982 2626 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2627 }
2ec9a5e0
TT
2628 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2629 {
049412e3 2630 dwz_file->gdb_index.s.section = sectp;
fd361982 2631 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2632 }
927aa2e7
JK
2633 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2634 {
2635 dwz_file->debug_names.s.section = sectp;
fd361982 2636 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2637 }
36586728
TT
2638}
2639
c4973306 2640/* See dwarf2read.h. */
36586728 2641
c4973306 2642struct dwz_file *
ed2dc618 2643dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2644{
36586728 2645 const char *filename;
acd13123 2646 bfd_size_type buildid_len_arg;
dc294be5
TT
2647 size_t buildid_len;
2648 bfd_byte *buildid;
36586728
TT
2649
2650 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2651 return dwarf2_per_objfile->dwz_file.get ();
36586728 2652
4db1a1dc 2653 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2654 gdb::unique_xmalloc_ptr<char> data
2655 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2656 &buildid_len_arg, &buildid));
4db1a1dc
TT
2657 if (data == NULL)
2658 {
2659 if (bfd_get_error () == bfd_error_no_error)
2660 return NULL;
2661 error (_("could not read '.gnu_debugaltlink' section: %s"),
2662 bfd_errmsg (bfd_get_error ()));
2663 }
791afaa2
TT
2664
2665 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2666
acd13123
TT
2667 buildid_len = (size_t) buildid_len_arg;
2668
791afaa2 2669 filename = data.get ();
d721ba37
PA
2670
2671 std::string abs_storage;
36586728
TT
2672 if (!IS_ABSOLUTE_PATH (filename))
2673 {
14278e1f
TT
2674 gdb::unique_xmalloc_ptr<char> abs
2675 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2676
14278e1f 2677 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2678 filename = abs_storage.c_str ();
36586728
TT
2679 }
2680
dc294be5
TT
2681 /* First try the file name given in the section. If that doesn't
2682 work, try to use the build-id instead. */
192b62ce 2683 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2684 if (dwz_bfd != NULL)
36586728 2685 {
192b62ce 2686 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2687 dwz_bfd.reset (nullptr);
36586728
TT
2688 }
2689
dc294be5
TT
2690 if (dwz_bfd == NULL)
2691 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2692
2693 if (dwz_bfd == NULL)
2694 error (_("could not find '.gnu_debugaltlink' file for %s"),
2695 objfile_name (dwarf2_per_objfile->objfile));
2696
7ff8cb8c
TT
2697 std::unique_ptr<struct dwz_file> result
2698 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2699
7ff8cb8c
TT
2700 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2701 result.get ());
36586728 2702
7ff8cb8c
TT
2703 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2704 result->dwz_bfd.get ());
2705 dwarf2_per_objfile->dwz_file = std::move (result);
2706 return dwarf2_per_objfile->dwz_file.get ();
36586728 2707}
9291a0cd 2708\f
7b9f3c50
DE
2709/* DWARF quick_symbols_functions support. */
2710
2711/* TUs can share .debug_line entries, and there can be a lot more TUs than
2712 unique line tables, so we maintain a separate table of all .debug_line
2713 derived entries to support the sharing.
2714 All the quick functions need is the list of file names. We discard the
2715 line_header when we're done and don't need to record it here. */
2716struct quick_file_names
2717{
094b34ac
DE
2718 /* The data used to construct the hash key. */
2719 struct stmt_list_hash hash;
7b9f3c50
DE
2720
2721 /* The number of entries in file_names, real_names. */
2722 unsigned int num_file_names;
2723
2724 /* The file names from the line table, after being run through
2725 file_full_name. */
2726 const char **file_names;
2727
2728 /* The file names from the line table after being run through
2729 gdb_realpath. These are computed lazily. */
2730 const char **real_names;
2731};
2732
2733/* When using the index (and thus not using psymtabs), each CU has an
2734 object of this type. This is used to hold information needed by
2735 the various "quick" methods. */
2736struct dwarf2_per_cu_quick_data
2737{
2738 /* The file table. This can be NULL if there was no file table
2739 or it's currently not read in.
2740 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2741 struct quick_file_names *file_names;
2742
2743 /* The corresponding symbol table. This is NULL if symbols for this
2744 CU have not yet been read. */
43f3e411 2745 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2746
2747 /* A temporary mark bit used when iterating over all CUs in
2748 expand_symtabs_matching. */
2749 unsigned int mark : 1;
2750
2751 /* True if we've tried to read the file table and found there isn't one.
2752 There will be no point in trying to read it again next time. */
2753 unsigned int no_file_data : 1;
2754};
2755
094b34ac
DE
2756/* Utility hash function for a stmt_list_hash. */
2757
2758static hashval_t
2759hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2760{
2761 hashval_t v = 0;
2762
2763 if (stmt_list_hash->dwo_unit != NULL)
2764 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2765 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2766 return v;
2767}
2768
2769/* Utility equality function for a stmt_list_hash. */
2770
2771static int
2772eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2773 const struct stmt_list_hash *rhs)
2774{
2775 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2776 return 0;
2777 if (lhs->dwo_unit != NULL
2778 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2779 return 0;
2780
9c541725 2781 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2782}
2783
7b9f3c50
DE
2784/* Hash function for a quick_file_names. */
2785
2786static hashval_t
2787hash_file_name_entry (const void *e)
2788{
9a3c8263
SM
2789 const struct quick_file_names *file_data
2790 = (const struct quick_file_names *) e;
7b9f3c50 2791
094b34ac 2792 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2793}
2794
2795/* Equality function for a quick_file_names. */
2796
2797static int
2798eq_file_name_entry (const void *a, const void *b)
2799{
9a3c8263
SM
2800 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2801 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2802
094b34ac 2803 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2804}
2805
2806/* Delete function for a quick_file_names. */
2807
2808static void
2809delete_file_name_entry (void *e)
2810{
9a3c8263 2811 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2812 int i;
2813
2814 for (i = 0; i < file_data->num_file_names; ++i)
2815 {
2816 xfree ((void*) file_data->file_names[i]);
2817 if (file_data->real_names)
2818 xfree ((void*) file_data->real_names[i]);
2819 }
2820
2821 /* The space for the struct itself lives on objfile_obstack,
2822 so we don't free it here. */
2823}
2824
2825/* Create a quick_file_names hash table. */
2826
2827static htab_t
2828create_quick_file_names_table (unsigned int nr_initial_entries)
2829{
2830 return htab_create_alloc (nr_initial_entries,
2831 hash_file_name_entry, eq_file_name_entry,
2832 delete_file_name_entry, xcalloc, xfree);
2833}
9291a0cd 2834
918dd910
JK
2835/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2836 have to be created afterwards. You should call age_cached_comp_units after
2837 processing PER_CU->CU. dw2_setup must have been already called. */
2838
2839static void
58f0c718 2840load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2841{
3019eac3 2842 if (per_cu->is_debug_types)
e5fe5e75 2843 load_full_type_unit (per_cu);
918dd910 2844 else
58f0c718 2845 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2846
cc12ce38
DE
2847 if (per_cu->cu == NULL)
2848 return; /* Dummy CU. */
2dc860c0
DE
2849
2850 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2851}
2852
a0f42c21 2853/* Read in the symbols for PER_CU. */
2fdf6df6 2854
9291a0cd 2855static void
58f0c718 2856dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2857{
ed2dc618 2858 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2859
f4dc4d17
DE
2860 /* Skip type_unit_groups, reading the type units they contain
2861 is handled elsewhere. */
2862 if (IS_TYPE_UNIT_GROUP (per_cu))
2863 return;
2864
b303c6f6
AB
2865 /* The destructor of dwarf2_queue_guard frees any entries left on
2866 the queue. After this point we're guaranteed to leave this function
2867 with the dwarf queue empty. */
2868 dwarf2_queue_guard q_guard;
9291a0cd 2869
95554aad 2870 if (dwarf2_per_objfile->using_index
43f3e411 2871 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2872 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2873 {
2874 queue_comp_unit (per_cu, language_minimal);
58f0c718 2875 load_cu (per_cu, skip_partial);
89e63ee4
DE
2876
2877 /* If we just loaded a CU from a DWO, and we're working with an index
2878 that may badly handle TUs, load all the TUs in that DWO as well.
2879 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2880 if (!per_cu->is_debug_types
cc12ce38 2881 && per_cu->cu != NULL
89e63ee4
DE
2882 && per_cu->cu->dwo_unit != NULL
2883 && dwarf2_per_objfile->index_table != NULL
2884 && dwarf2_per_objfile->index_table->version <= 7
2885 /* DWP files aren't supported yet. */
ed2dc618 2886 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2887 queue_and_load_all_dwo_tus (per_cu);
95554aad 2888 }
9291a0cd 2889
ed2dc618 2890 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2891
2892 /* Age the cache, releasing compilation units that have not
2893 been used recently. */
ed2dc618 2894 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2895}
2896
2897/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2898 the objfile from which this CU came. Returns the resulting symbol
2899 table. */
2fdf6df6 2900
43f3e411 2901static struct compunit_symtab *
58f0c718 2902dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2903{
ed2dc618
SM
2904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2905
95554aad 2906 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2907 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2908 {
11ed8cad 2909 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2910 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2911 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2912 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2913 }
f194fefb 2914
43f3e411 2915 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2916}
2917
ff4c9fec 2918/* See declaration. */
f4dc4d17 2919
ff4c9fec
SM
2920dwarf2_per_cu_data *
2921dwarf2_per_objfile::get_cutu (int index)
2922{
b76e467d 2923 if (index >= this->all_comp_units.size ())
ff4c9fec 2924 {
b76e467d 2925 index -= this->all_comp_units.size ();
b2bdb8cf 2926 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2927 return &this->all_type_units[index]->per_cu;
2928 }
f4dc4d17 2929
ff4c9fec
SM
2930 return this->all_comp_units[index];
2931}
f4dc4d17 2932
ff4c9fec 2933/* See declaration. */
2fdf6df6 2934
ff4c9fec
SM
2935dwarf2_per_cu_data *
2936dwarf2_per_objfile::get_cu (int index)
1fd400ff 2937{
b76e467d 2938 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2939
ff4c9fec 2940 return this->all_comp_units[index];
f4dc4d17
DE
2941}
2942
ff4c9fec 2943/* See declaration. */
f4dc4d17 2944
ff4c9fec
SM
2945signatured_type *
2946dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2947{
b2bdb8cf 2948 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2949
ff4c9fec 2950 return this->all_type_units[index];
1fd400ff
TT
2951}
2952
4b514bc8
JK
2953/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2954 objfile_obstack, and constructed with the specified field
2955 values. */
2956
2957static dwarf2_per_cu_data *
ed2dc618 2958create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2959 struct dwarf2_section_info *section,
2960 int is_dwz,
2961 sect_offset sect_off, ULONGEST length)
2962{
ed2dc618 2963 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2964 dwarf2_per_cu_data *the_cu
2965 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_data);
2967 the_cu->sect_off = sect_off;
2968 the_cu->length = length;
e3b94546 2969 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2970 the_cu->section = section;
2971 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2972 struct dwarf2_per_cu_quick_data);
2973 the_cu->is_dwz = is_dwz;
2974 return the_cu;
2975}
2976
2ec9a5e0
TT
2977/* A helper for create_cus_from_index that handles a given list of
2978 CUs. */
2fdf6df6 2979
74a0d9f6 2980static void
12359b5e 2981create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2982 const gdb_byte *cu_list, offset_type n_elements,
2983 struct dwarf2_section_info *section,
b76e467d 2984 int is_dwz)
9291a0cd 2985{
12359b5e 2986 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2987 {
74a0d9f6 2988 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2989
2990 sect_offset sect_off
2991 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2992 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2993 cu_list += 2 * 8;
2994
b76e467d 2995 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2996 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2997 sect_off, length);
b76e467d 2998 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2999 }
9291a0cd
TT
3000}
3001
2ec9a5e0 3002/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3003 the CU objects for this objfile. */
2ec9a5e0 3004
74a0d9f6 3005static void
12359b5e 3006create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3007 const gdb_byte *cu_list, offset_type cu_list_elements,
3008 const gdb_byte *dwz_list, offset_type dwz_elements)
3009{
b76e467d
SM
3010 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3011 dwarf2_per_objfile->all_comp_units.reserve
3012 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3013
12359b5e 3014 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3015 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3016
3017 if (dwz_elements == 0)
74a0d9f6 3018 return;
2ec9a5e0 3019
12359b5e
SM
3020 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3021 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3022 &dwz->info, 1);
2ec9a5e0
TT
3023}
3024
1fd400ff 3025/* Create the signatured type hash table from the index. */
673bfd45 3026
74a0d9f6 3027static void
12359b5e
SM
3028create_signatured_type_table_from_index
3029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3030 struct dwarf2_section_info *section,
3031 const gdb_byte *bytes,
3032 offset_type elements)
1fd400ff 3033{
12359b5e 3034 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3035
b2bdb8cf
SM
3036 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3037 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3038
12359b5e 3039 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3040
12359b5e 3041 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3042 {
52dc124a 3043 struct signatured_type *sig_type;
9c541725 3044 ULONGEST signature;
1fd400ff 3045 void **slot;
9c541725 3046 cu_offset type_offset_in_tu;
1fd400ff 3047
74a0d9f6 3048 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3049 sect_offset sect_off
3050 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3051 type_offset_in_tu
3052 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3053 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3054 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3055 bytes += 3 * 8;
3056
52dc124a 3057 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3058 struct signatured_type);
52dc124a 3059 sig_type->signature = signature;
9c541725 3060 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3061 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3062 sig_type->per_cu.section = section;
9c541725 3063 sig_type->per_cu.sect_off = sect_off;
e3b94546 3064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3065 sig_type->per_cu.v.quick
1fd400ff
TT
3066 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3067 struct dwarf2_per_cu_quick_data);
3068
52dc124a
DE
3069 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3070 *slot = sig_type;
1fd400ff 3071
b2bdb8cf 3072 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3073 }
3074
673bfd45 3075 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3076}
3077
927aa2e7
JK
3078/* Create the signatured type hash table from .debug_names. */
3079
3080static void
3081create_signatured_type_table_from_debug_names
ed2dc618 3082 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3083 const mapped_debug_names &map,
3084 struct dwarf2_section_info *section,
3085 struct dwarf2_section_info *abbrev_section)
3086{
ed2dc618
SM
3087 struct objfile *objfile = dwarf2_per_objfile->objfile;
3088
927aa2e7
JK
3089 dwarf2_read_section (objfile, section);
3090 dwarf2_read_section (objfile, abbrev_section);
3091
b2bdb8cf
SM
3092 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3093 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3094
3095 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3096
3097 for (uint32_t i = 0; i < map.tu_count; ++i)
3098 {
3099 struct signatured_type *sig_type;
927aa2e7 3100 void **slot;
927aa2e7
JK
3101
3102 sect_offset sect_off
3103 = (sect_offset) (extract_unsigned_integer
3104 (map.tu_table_reordered + i * map.offset_size,
3105 map.offset_size,
3106 map.dwarf5_byte_order));
3107
3108 comp_unit_head cu_header;
ed2dc618
SM
3109 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3110 abbrev_section,
927aa2e7
JK
3111 section->buffer + to_underlying (sect_off),
3112 rcuh_kind::TYPE);
3113
3114 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3115 struct signatured_type);
3116 sig_type->signature = cu_header.signature;
3117 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3118 sig_type->per_cu.is_debug_types = 1;
3119 sig_type->per_cu.section = section;
3120 sig_type->per_cu.sect_off = sect_off;
e3b94546 3121 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3122 sig_type->per_cu.v.quick
3123 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3124 struct dwarf2_per_cu_quick_data);
3125
3126 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3127 *slot = sig_type;
3128
b2bdb8cf 3129 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3130 }
3131
3132 dwarf2_per_objfile->signatured_types = sig_types_hash;
3133}
3134
9291a0cd
TT
3135/* Read the address map data from the mapped index, and use it to
3136 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3137
9291a0cd 3138static void
ed2dc618
SM
3139create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3140 struct mapped_index *index)
9291a0cd 3141{
ed2dc618 3142 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3143 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3144 const gdb_byte *iter, *end;
9291a0cd 3145 struct addrmap *mutable_map;
9291a0cd
TT
3146 CORE_ADDR baseaddr;
3147
8268c778
PA
3148 auto_obstack temp_obstack;
3149
9291a0cd
TT
3150 mutable_map = addrmap_create_mutable (&temp_obstack);
3151
f00a2de2
PA
3152 iter = index->address_table.data ();
3153 end = iter + index->address_table.size ();
9291a0cd
TT
3154
3155 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3156
3157 while (iter < end)
3158 {
3159 ULONGEST hi, lo, cu_index;
3160 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3161 iter += 8;
3162 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3163 iter += 8;
3164 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3165 iter += 4;
f652bce2 3166
24a55014 3167 if (lo > hi)
f652bce2 3168 {
b98664d3 3169 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3170 hex_string (lo), hex_string (hi));
24a55014 3171 continue;
f652bce2 3172 }
24a55014 3173
b76e467d 3174 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3175 {
b98664d3 3176 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3177 (unsigned) cu_index);
24a55014 3178 continue;
f652bce2 3179 }
24a55014 3180
79748972
TT
3181 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3182 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3183 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3184 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3185 }
3186
d320c2b5 3187 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3188 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3189}
3190
927aa2e7
JK
3191/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3192 populate the objfile's psymtabs_addrmap. */
3193
3194static void
ed2dc618 3195create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3196 struct dwarf2_section_info *section)
3197{
ed2dc618 3198 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3199 bfd *abfd = objfile->obfd;
3200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3201 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3202 SECT_OFF_TEXT (objfile));
3203
3204 auto_obstack temp_obstack;
3205 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3206
3207 std::unordered_map<sect_offset,
3208 dwarf2_per_cu_data *,
3209 gdb::hash_enum<sect_offset>>
3210 debug_info_offset_to_per_cu;
b76e467d 3211 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3212 {
927aa2e7
JK
3213 const auto insertpair
3214 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3215 if (!insertpair.second)
3216 {
3217 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3218 "debug_info_offset %s, ignoring .debug_aranges."),
3219 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3220 return;
3221 }
3222 }
3223
3224 dwarf2_read_section (objfile, section);
3225
3226 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3227
3228 const gdb_byte *addr = section->buffer;
3229
3230 while (addr < section->buffer + section->size)
3231 {
3232 const gdb_byte *const entry_addr = addr;
3233 unsigned int bytes_read;
3234
3235 const LONGEST entry_length = read_initial_length (abfd, addr,
3236 &bytes_read);
3237 addr += bytes_read;
3238
3239 const gdb_byte *const entry_end = addr + entry_length;
3240 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3241 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3242 if (addr + entry_length > section->buffer + section->size)
3243 {
47e3f474 3244 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3245 "length %s exceeds section length %s, "
3246 "ignoring .debug_aranges."),
47e3f474
TV
3247 objfile_name (objfile),
3248 plongest (entry_addr - section->buffer),
927aa2e7
JK
3249 plongest (bytes_read + entry_length),
3250 pulongest (section->size));
3251 return;
3252 }
3253
3254 /* The version number. */
3255 const uint16_t version = read_2_bytes (abfd, addr);
3256 addr += 2;
3257 if (version != 2)
3258 {
47e3f474 3259 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3260 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3261 objfile_name (objfile),
3262 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3263 return;
3264 }
3265
3266 const uint64_t debug_info_offset
3267 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3268 addr += offset_size;
3269 const auto per_cu_it
3270 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3271 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3272 {
47e3f474 3273 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3274 "debug_info_offset %s does not exists, "
3275 "ignoring .debug_aranges."),
47e3f474
TV
3276 objfile_name (objfile),
3277 plongest (entry_addr - section->buffer),
927aa2e7
JK
3278 pulongest (debug_info_offset));
3279 return;
3280 }
3281 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3282
3283 const uint8_t address_size = *addr++;
3284 if (address_size < 1 || address_size > 8)
3285 {
47e3f474 3286 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3287 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3288 objfile_name (objfile),
3289 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3290 return;
3291 }
3292
3293 const uint8_t segment_selector_size = *addr++;
3294 if (segment_selector_size != 0)
3295 {
47e3f474 3296 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3297 "segment_selector_size %u is not supported, "
3298 "ignoring .debug_aranges."),
47e3f474
TV
3299 objfile_name (objfile),
3300 plongest (entry_addr - section->buffer),
927aa2e7
JK
3301 segment_selector_size);
3302 return;
3303 }
3304
3305 /* Must pad to an alignment boundary that is twice the address
3306 size. It is undocumented by the DWARF standard but GCC does
3307 use it. */
3308 for (size_t padding = ((-(addr - section->buffer))
3309 & (2 * address_size - 1));
3310 padding > 0; padding--)
3311 if (*addr++ != 0)
3312 {
47e3f474 3313 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3314 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3315 objfile_name (objfile),
3316 plongest (entry_addr - section->buffer));
927aa2e7
JK
3317 return;
3318 }
3319
3320 for (;;)
3321 {
3322 if (addr + 2 * address_size > entry_end)
3323 {
47e3f474 3324 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3325 "address list is not properly terminated, "
3326 "ignoring .debug_aranges."),
47e3f474
TV
3327 objfile_name (objfile),
3328 plongest (entry_addr - section->buffer));
927aa2e7
JK
3329 return;
3330 }
3331 ULONGEST start = extract_unsigned_integer (addr, address_size,
3332 dwarf5_byte_order);
3333 addr += address_size;
3334 ULONGEST length = extract_unsigned_integer (addr, address_size,
3335 dwarf5_byte_order);
3336 addr += address_size;
3337 if (start == 0 && length == 0)
3338 break;
3339 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3340 {
3341 /* Symbol was eliminated due to a COMDAT group. */
3342 continue;
3343 }
3344 ULONGEST end = start + length;
79748972
TT
3345 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3346 - baseaddr);
3347 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3348 - baseaddr);
927aa2e7
JK
3349 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3350 }
3351 }
3352
d320c2b5 3353 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3354 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3355}
3356
9291a0cd
TT
3357/* Find a slot in the mapped index INDEX for the object named NAME.
3358 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3359 constant pool and return true. If NAME cannot be found, return
3360 false. */
2fdf6df6 3361
109483d9 3362static bool
9291a0cd
TT
3363find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3364 offset_type **vec_out)
3365{
0cf03b49 3366 offset_type hash;
9291a0cd 3367 offset_type slot, step;
559a7a62 3368 int (*cmp) (const char *, const char *);
9291a0cd 3369
791afaa2 3370 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3371 if (current_language->la_language == language_cplus
45280282
IB
3372 || current_language->la_language == language_fortran
3373 || current_language->la_language == language_d)
0cf03b49
JK
3374 {
3375 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3376 not contain any. */
a8719064 3377
72998fb3 3378 if (strchr (name, '(') != NULL)
0cf03b49 3379 {
109483d9 3380 without_params = cp_remove_params (name);
0cf03b49 3381
72998fb3 3382 if (without_params != NULL)
791afaa2 3383 name = without_params.get ();
0cf03b49
JK
3384 }
3385 }
3386
559a7a62 3387 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3388 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3389 simulate our NAME being searched is also lowercased. */
3390 hash = mapped_index_string_hash ((index->version == 4
3391 && case_sensitivity == case_sensitive_off
3392 ? 5 : index->version),
3393 name);
3394
f00a2de2
PA
3395 slot = hash & (index->symbol_table.size () - 1);
3396 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3397 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3398
3399 for (;;)
3400 {
9291a0cd 3401 const char *str;
f00a2de2
PA
3402
3403 const auto &bucket = index->symbol_table[slot];
3404 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3405 return false;
9291a0cd 3406
f00a2de2 3407 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3408 if (!cmp (name, str))
9291a0cd
TT
3409 {
3410 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3411 + MAYBE_SWAP (bucket.vec));
109483d9 3412 return true;
9291a0cd
TT
3413 }
3414
f00a2de2 3415 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3416 }
3417}
3418
4485a1c1
SM
3419/* A helper function that reads the .gdb_index from BUFFER and fills
3420 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3421 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3422 ok to use deprecated sections.
3423
3424 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3425 out parameters that are filled in with information about the CU and
3426 TU lists in the section.
3427
4485a1c1 3428 Returns true if all went well, false otherwise. */
2fdf6df6 3429
d33bc52e 3430static bool
4485a1c1
SM
3431read_gdb_index_from_buffer (struct objfile *objfile,
3432 const char *filename,
3433 bool deprecated_ok,
3434 gdb::array_view<const gdb_byte> buffer,
3435 struct mapped_index *map,
3436 const gdb_byte **cu_list,
3437 offset_type *cu_list_elements,
3438 const gdb_byte **types_list,
3439 offset_type *types_list_elements)
3440{
3441 const gdb_byte *addr = &buffer[0];
82430852 3442
9291a0cd 3443 /* Version check. */
4485a1c1 3444 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3445 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3446 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3447 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3448 indices. */
831adc1f 3449 if (version < 4)
481860b3
GB
3450 {
3451 static int warning_printed = 0;
3452 if (!warning_printed)
3453 {
3454 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3455 filename);
481860b3
GB
3456 warning_printed = 1;
3457 }
3458 return 0;
3459 }
3460 /* Index version 4 uses a different hash function than index version
3461 5 and later.
3462
3463 Versions earlier than 6 did not emit psymbols for inlined
3464 functions. Using these files will cause GDB not to be able to
3465 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3466 indices unless the user has done
3467 "set use-deprecated-index-sections on". */
2ec9a5e0 3468 if (version < 6 && !deprecated_ok)
481860b3
GB
3469 {
3470 static int warning_printed = 0;
3471 if (!warning_printed)
3472 {
e615022a
DE
3473 warning (_("\
3474Skipping deprecated .gdb_index section in %s.\n\
3475Do \"set use-deprecated-index-sections on\" before the file is read\n\
3476to use the section anyway."),
2ec9a5e0 3477 filename);
481860b3
GB
3478 warning_printed = 1;
3479 }
3480 return 0;
3481 }
796a7ff8 3482 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3483 of the TU (for symbols coming from TUs),
3484 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3485 Plus gold-generated indices can have duplicate entries for global symbols,
3486 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3487 These are just performance bugs, and we can't distinguish gdb-generated
3488 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3489
481860b3 3490 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3491 longer backward compatible. */
796a7ff8 3492 if (version > 8)
594e8718 3493 return 0;
9291a0cd 3494
559a7a62 3495 map->version = version;
9291a0cd 3496
4485a1c1 3497 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3498
4485a1c1 3499 int i = 0;
2ec9a5e0
TT
3500 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3501 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3502 / 8);
1fd400ff
TT
3503 ++i;
3504
2ec9a5e0
TT
3505 *types_list = addr + MAYBE_SWAP (metadata[i]);
3506 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3507 - MAYBE_SWAP (metadata[i]))
3508 / 8);
987d643c 3509 ++i;
1fd400ff 3510
f00a2de2
PA
3511 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3512 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3513 map->address_table
3514 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3515 ++i;
3516
f00a2de2
PA
3517 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3518 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 map->symbol_table
3520 = gdb::array_view<mapped_index::symbol_table_slot>
3521 ((mapped_index::symbol_table_slot *) symbol_table,
3522 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3523
f00a2de2 3524 ++i;
f9d83a0b 3525 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3526
2ec9a5e0
TT
3527 return 1;
3528}
3529
4485a1c1
SM
3530/* Callback types for dwarf2_read_gdb_index. */
3531
3532typedef gdb::function_view
3533 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3534 get_gdb_index_contents_ftype;
3535typedef gdb::function_view
3536 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3537 get_gdb_index_contents_dwz_ftype;
3538
927aa2e7 3539/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3540 elements of all the CUs and return 1. Otherwise, return 0. */
3541
3542static int
4485a1c1
SM
3543dwarf2_read_gdb_index
3544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3545 get_gdb_index_contents_ftype get_gdb_index_contents,
3546 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3547{
2ec9a5e0
TT
3548 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3549 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3550 struct dwz_file *dwz;
12359b5e 3551 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3552
4485a1c1
SM
3553 gdb::array_view<const gdb_byte> main_index_contents
3554 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3555
3556 if (main_index_contents.empty ())
3557 return 0;
3558
3063847f 3559 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3560 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3561 use_deprecated_index_sections,
3562 main_index_contents, map.get (), &cu_list,
3563 &cu_list_elements, &types_list,
3564 &types_list_elements))
2ec9a5e0
TT
3565 return 0;
3566
0fefef59 3567 /* Don't use the index if it's empty. */
3063847f 3568 if (map->symbol_table.empty ())
0fefef59
DE
3569 return 0;
3570
2ec9a5e0
TT
3571 /* If there is a .dwz file, read it so we can get its CU list as
3572 well. */
ed2dc618 3573 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3574 if (dwz != NULL)
2ec9a5e0 3575 {
2ec9a5e0
TT
3576 struct mapped_index dwz_map;
3577 const gdb_byte *dwz_types_ignore;
3578 offset_type dwz_types_elements_ignore;
3579
4485a1c1
SM
3580 gdb::array_view<const gdb_byte> dwz_index_content
3581 = get_gdb_index_contents_dwz (objfile, dwz);
3582
3583 if (dwz_index_content.empty ())
3584 return 0;
3585
3586 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3587 bfd_get_filename (dwz->dwz_bfd.get ()),
3588 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3589 &dwz_list, &dwz_list_elements,
3590 &dwz_types_ignore,
3591 &dwz_types_elements_ignore))
2ec9a5e0
TT
3592 {
3593 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3594 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3595 return 0;
3596 }
3597 }
3598
12359b5e
SM
3599 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3600 dwz_list, dwz_list_elements);
1fd400ff 3601
8b70b953
TT
3602 if (types_list_elements)
3603 {
8b70b953
TT
3604 /* We can only handle a single .debug_types when we have an
3605 index. */
fd5866f6 3606 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3607 return 0;
3608
fd5866f6 3609 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3610
12359b5e
SM
3611 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3612 types_list, types_list_elements);
8b70b953 3613 }
9291a0cd 3614
3063847f 3615 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3616
3063847f 3617 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3618 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3619 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3620 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3621
3622 return 1;
3623}
3624
dee91e82 3625/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3626
dee91e82
DE
3627static void
3628dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3629 const gdb_byte *info_ptr,
dee91e82
DE
3630 struct die_info *comp_unit_die,
3631 int has_children,
3632 void *data)
9291a0cd 3633{
dee91e82 3634 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3635 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3636 struct dwarf2_per_objfile *dwarf2_per_objfile
3637 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3638 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3639 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3640 struct attribute *attr;
dee91e82 3641 int i;
7b9f3c50
DE
3642 void **slot;
3643 struct quick_file_names *qfn;
9291a0cd 3644
0186c6a7
DE
3645 gdb_assert (! this_cu->is_debug_types);
3646
07261596
TT
3647 /* Our callers never want to match partial units -- instead they
3648 will match the enclosing full CU. */
3649 if (comp_unit_die->tag == DW_TAG_partial_unit)
3650 {
3651 this_cu->v.quick->no_file_data = 1;
3652 return;
3653 }
3654
0186c6a7 3655 lh_cu = this_cu;
7b9f3c50 3656 slot = NULL;
dee91e82 3657
fff8551c 3658 line_header_up lh;
9c541725 3659 sect_offset line_offset {};
fff8551c 3660
dee91e82 3661 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3662 if (attr)
3663 {
7b9f3c50
DE
3664 struct quick_file_names find_entry;
3665
9c541725 3666 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3667
3668 /* We may have already read in this line header (TU line header sharing).
3669 If we have we're done. */
094b34ac 3670 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3671 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3672 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3673 &find_entry, INSERT);
3674 if (*slot != NULL)
3675 {
9a3c8263 3676 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3677 return;
7b9f3c50
DE
3678 }
3679
3019eac3 3680 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3681 }
3682 if (lh == NULL)
3683 {
094b34ac 3684 lh_cu->v.quick->no_file_data = 1;
dee91e82 3685 return;
9291a0cd
TT
3686 }
3687
8d749320 3688 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3689 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3690 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3691 gdb_assert (slot != NULL);
3692 *slot = qfn;
9291a0cd 3693
d721ba37 3694 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3695
aa391654
TT
3696 int offset = 0;
3697 if (strcmp (fnd.name, "<unknown>") != 0)
3698 ++offset;
3699
3700 qfn->num_file_names = offset + lh->file_names.size ();
8d749320 3701 qfn->file_names =
aa391654
TT
3702 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3703 if (offset != 0)
3704 qfn->file_names[0] = xstrdup (fnd.name);
fff8551c 3705 for (i = 0; i < lh->file_names.size (); ++i)
aa391654 3706 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3707 qfn->real_names = NULL;
9291a0cd 3708
094b34ac 3709 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3710}
3711
3712/* A helper for the "quick" functions which attempts to read the line
3713 table for THIS_CU. */
3714
3715static struct quick_file_names *
e4a48d9d 3716dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3717{
0186c6a7
DE
3718 /* This should never be called for TUs. */
3719 gdb_assert (! this_cu->is_debug_types);
3720 /* Nor type unit groups. */
3721 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3722
dee91e82
DE
3723 if (this_cu->v.quick->file_names != NULL)
3724 return this_cu->v.quick->file_names;
3725 /* If we know there is no line data, no point in looking again. */
3726 if (this_cu->v.quick->no_file_data)
3727 return NULL;
3728
0186c6a7 3729 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3730
3731 if (this_cu->v.quick->no_file_data)
3732 return NULL;
3733 return this_cu->v.quick->file_names;
9291a0cd
TT
3734}
3735
3736/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3737 real path for a given file name from the line table. */
2fdf6df6 3738
9291a0cd 3739static const char *
7b9f3c50
DE
3740dw2_get_real_path (struct objfile *objfile,
3741 struct quick_file_names *qfn, int index)
9291a0cd 3742{
7b9f3c50
DE
3743 if (qfn->real_names == NULL)
3744 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3745 qfn->num_file_names, const char *);
9291a0cd 3746
7b9f3c50 3747 if (qfn->real_names[index] == NULL)
14278e1f 3748 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3749
7b9f3c50 3750 return qfn->real_names[index];
9291a0cd
TT
3751}
3752
3753static struct symtab *
3754dw2_find_last_source_symtab (struct objfile *objfile)
3755{
ed2dc618
SM
3756 struct dwarf2_per_objfile *dwarf2_per_objfile
3757 = get_dwarf2_per_objfile (objfile);
b76e467d 3758 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3759 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3760
43f3e411
DE
3761 if (cust == NULL)
3762 return NULL;
ed2dc618 3763
43f3e411 3764 return compunit_primary_filetab (cust);
9291a0cd
TT
3765}
3766
7b9f3c50
DE
3767/* Traversal function for dw2_forget_cached_source_info. */
3768
3769static int
3770dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3771{
7b9f3c50 3772 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3773
7b9f3c50 3774 if (file_data->real_names)
9291a0cd 3775 {
7b9f3c50 3776 int i;
9291a0cd 3777
7b9f3c50 3778 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3779 {
7b9f3c50
DE
3780 xfree ((void*) file_data->real_names[i]);
3781 file_data->real_names[i] = NULL;
9291a0cd
TT
3782 }
3783 }
7b9f3c50
DE
3784
3785 return 1;
3786}
3787
3788static void
3789dw2_forget_cached_source_info (struct objfile *objfile)
3790{
ed2dc618
SM
3791 struct dwarf2_per_objfile *dwarf2_per_objfile
3792 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3793
3794 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3795 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3796}
3797
f8eba3c6
TT
3798/* Helper function for dw2_map_symtabs_matching_filename that expands
3799 the symtabs and calls the iterator. */
3800
3801static int
3802dw2_map_expand_apply (struct objfile *objfile,
3803 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3804 const char *name, const char *real_path,
14bc53a8 3805 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3806{
43f3e411 3807 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3808
3809 /* Don't visit already-expanded CUs. */
43f3e411 3810 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3811 return 0;
3812
3813 /* This may expand more than one symtab, and we want to iterate over
3814 all of them. */
58f0c718 3815 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3816
14bc53a8
PA
3817 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3818 last_made, callback);
f8eba3c6
TT
3819}
3820
3821/* Implementation of the map_symtabs_matching_filename method. */
3822
14bc53a8
PA
3823static bool
3824dw2_map_symtabs_matching_filename
3825 (struct objfile *objfile, const char *name, const char *real_path,
3826 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3827{
c011a4f4 3828 const char *name_basename = lbasename (name);
ed2dc618
SM
3829 struct dwarf2_per_objfile *dwarf2_per_objfile
3830 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3831
848e3e78
DE
3832 /* The rule is CUs specify all the files, including those used by
3833 any TU, so there's no need to scan TUs here. */
f4dc4d17 3834
b76e467d 3835 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3836 {
3d7bb9d9 3837 /* We only need to look at symtabs not already expanded. */
43f3e411 3838 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3839 continue;
3840
b76e467d 3841 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3842 if (file_data == NULL)
9291a0cd
TT
3843 continue;
3844
b76e467d 3845 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3846 {
7b9f3c50 3847 const char *this_name = file_data->file_names[j];
da235a7c 3848 const char *this_real_name;
9291a0cd 3849
af529f8f 3850 if (compare_filenames_for_search (this_name, name))
9291a0cd 3851 {
f5b95b50 3852 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3853 callback))
3854 return true;
288e77a7 3855 continue;
4aac40c8 3856 }
9291a0cd 3857
c011a4f4
DE
3858 /* Before we invoke realpath, which can get expensive when many
3859 files are involved, do a quick comparison of the basenames. */
3860 if (! basenames_may_differ
3861 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3862 continue;
3863
da235a7c
JK
3864 this_real_name = dw2_get_real_path (objfile, file_data, j);
3865 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3866 {
da235a7c 3867 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3868 callback))
3869 return true;
288e77a7 3870 continue;
da235a7c 3871 }
9291a0cd 3872
da235a7c
JK
3873 if (real_path != NULL)
3874 {
af529f8f
JK
3875 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3876 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3877 if (this_real_name != NULL
af529f8f 3878 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3879 {
f5b95b50 3880 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3881 callback))
3882 return true;
288e77a7 3883 continue;
9291a0cd
TT
3884 }
3885 }
3886 }
3887 }
3888
14bc53a8 3889 return false;
9291a0cd
TT
3890}
3891
da51c347
DE
3892/* Struct used to manage iterating over all CUs looking for a symbol. */
3893
3894struct dw2_symtab_iterator
9291a0cd 3895{
ed2dc618
SM
3896 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3897 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3898 /* If set, only look for symbols that match that block. Valid values are
3899 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3900 gdb::optional<block_enum> block_index;
da51c347
DE
3901 /* The kind of symbol we're looking for. */
3902 domain_enum domain;
3903 /* The list of CUs from the index entry of the symbol,
3904 or NULL if not found. */
3905 offset_type *vec;
3906 /* The next element in VEC to look at. */
3907 int next;
3908 /* The number of elements in VEC, or zero if there is no match. */
3909 int length;
8943b874
DE
3910 /* Have we seen a global version of the symbol?
3911 If so we can ignore all further global instances.
3912 This is to work around gold/15646, inefficient gold-generated
3913 indices. */
3914 int global_seen;
da51c347 3915};
9291a0cd 3916
2b79f376 3917/* Initialize the index symtab iterator ITER. */
2fdf6df6 3918
9291a0cd 3919static void
da51c347 3920dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3921 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3922 gdb::optional<block_enum> block_index,
da51c347
DE
3923 domain_enum domain,
3924 const char *name)
3925{
ed2dc618 3926 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3927 iter->block_index = block_index;
3928 iter->domain = domain;
3929 iter->next = 0;
8943b874 3930 iter->global_seen = 0;
da51c347 3931
3063847f 3932 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3933
3934 /* index is NULL if OBJF_READNOW. */
3935 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3936 iter->length = MAYBE_SWAP (*iter->vec);
3937 else
3938 {
3939 iter->vec = NULL;
3940 iter->length = 0;
3941 }
3942}
3943
3944/* Return the next matching CU or NULL if there are no more. */
3945
3946static struct dwarf2_per_cu_data *
3947dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3948{
ed2dc618
SM
3949 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3950
da51c347
DE
3951 for ( ; iter->next < iter->length; ++iter->next)
3952 {
3953 offset_type cu_index_and_attrs =
3954 MAYBE_SWAP (iter->vec[iter->next + 1]);
3955 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3956 gdb_index_symbol_kind symbol_kind =
3957 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3958 /* Only check the symbol attributes if they're present.
3959 Indices prior to version 7 don't record them,
3960 and indices >= 7 may elide them for certain symbols
3961 (gold does this). */
3962 int attrs_valid =
ed2dc618 3963 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3964 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3965
3190f0c6 3966 /* Don't crash on bad data. */
b76e467d 3967 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3968 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3969 {
b98664d3 3970 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3971 " [in module %s]"),
3972 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3973 continue;
3974 }
3975
ff4c9fec 3976 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3977
da51c347 3978 /* Skip if already read in. */
43f3e411 3979 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3980 continue;
3981
8943b874
DE
3982 /* Check static vs global. */
3983 if (attrs_valid)
3984 {
2b79f376
SM
3985 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3986
3987 if (iter->block_index.has_value ())
3988 {
3989 bool want_static = *iter->block_index == STATIC_BLOCK;
3990
3991 if (is_static != want_static)
3992 continue;
3993 }
3994
8943b874
DE
3995 /* Work around gold/15646. */
3996 if (!is_static && iter->global_seen)
3997 continue;
3998 if (!is_static)
3999 iter->global_seen = 1;
4000 }
da51c347
DE
4001
4002 /* Only check the symbol's kind if it has one. */
4003 if (attrs_valid)
4004 {
4005 switch (iter->domain)
4006 {
4007 case VAR_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4009 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4010 /* Some types are also in VAR_DOMAIN. */
4011 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4012 continue;
4013 break;
4014 case STRUCT_DOMAIN:
4015 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4016 continue;
4017 break;
4018 case LABEL_DOMAIN:
4019 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4020 continue;
4021 break;
4022 default:
4023 break;
4024 }
4025 }
4026
4027 ++iter->next;
4028 return per_cu;
4029 }
4030
4031 return NULL;
4032}
4033
43f3e411 4034static struct compunit_symtab *
c7f839cb 4035dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4036 const char *name, domain_enum domain)
9291a0cd 4037{
43f3e411 4038 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4039 struct dwarf2_per_objfile *dwarf2_per_objfile
4040 = get_dwarf2_per_objfile (objfile);
9291a0cd 4041
b5ec771e
PA
4042 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4043
ed2dc618
SM
4044 struct dw2_symtab_iterator iter;
4045 struct dwarf2_per_cu_data *per_cu;
da51c347 4046
2b79f376 4047 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4048
ed2dc618
SM
4049 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4050 {
4051 struct symbol *sym, *with_opaque = NULL;
58f0c718 4052 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4053 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4054 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4055
ed2dc618
SM
4056 sym = block_find_symbol (block, name, domain,
4057 block_find_non_opaque_type_preferred,
4058 &with_opaque);
b2e2f908 4059
ed2dc618
SM
4060 /* Some caution must be observed with overloaded functions
4061 and methods, since the index will not contain any overload
4062 information (but NAME might contain it). */
da51c347 4063
ed2dc618
SM
4064 if (sym != NULL
4065 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4066 return stab;
4067 if (with_opaque != NULL
4068 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4069 stab_best = stab;
da51c347 4070
ed2dc618 4071 /* Keep looking through other CUs. */
9291a0cd 4072 }
9291a0cd 4073
da51c347 4074 return stab_best;
9291a0cd
TT
4075}
4076
4077static void
4078dw2_print_stats (struct objfile *objfile)
4079{
ed2dc618
SM
4080 struct dwarf2_per_objfile *dwarf2_per_objfile
4081 = get_dwarf2_per_objfile (objfile);
b76e467d 4082 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4083 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4084 int count = 0;
9291a0cd 4085
ed2dc618 4086 for (int i = 0; i < total; ++i)
9291a0cd 4087 {
ff4c9fec 4088 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4089
43f3e411 4090 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4091 ++count;
4092 }
e4a48d9d 4093 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4094 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4095}
4096
779bd270
DE
4097/* This dumps minimal information about the index.
4098 It is called via "mt print objfiles".
4099 One use is to verify .gdb_index has been loaded by the
4100 gdb.dwarf2/gdb-index.exp testcase. */
4101
9291a0cd
TT
4102static void
4103dw2_dump (struct objfile *objfile)
4104{
ed2dc618
SM
4105 struct dwarf2_per_objfile *dwarf2_per_objfile
4106 = get_dwarf2_per_objfile (objfile);
4107
779bd270
DE
4108 gdb_assert (dwarf2_per_objfile->using_index);
4109 printf_filtered (".gdb_index:");
4110 if (dwarf2_per_objfile->index_table != NULL)
4111 {
4112 printf_filtered (" version %d\n",
4113 dwarf2_per_objfile->index_table->version);
4114 }
4115 else
4116 printf_filtered (" faked for \"readnow\"\n");
4117 printf_filtered ("\n");
9291a0cd
TT
4118}
4119
9291a0cd
TT
4120static void
4121dw2_expand_symtabs_for_function (struct objfile *objfile,
4122 const char *func_name)
4123{
ed2dc618
SM
4124 struct dwarf2_per_objfile *dwarf2_per_objfile
4125 = get_dwarf2_per_objfile (objfile);
da51c347 4126
ed2dc618
SM
4127 struct dw2_symtab_iterator iter;
4128 struct dwarf2_per_cu_data *per_cu;
da51c347 4129
2b79f376 4130 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4131
ed2dc618 4132 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4133 dw2_instantiate_symtab (per_cu, false);
da51c347 4134
9291a0cd
TT
4135}
4136
4137static void
4138dw2_expand_all_symtabs (struct objfile *objfile)
4139{
ed2dc618
SM
4140 struct dwarf2_per_objfile *dwarf2_per_objfile
4141 = get_dwarf2_per_objfile (objfile);
b76e467d 4142 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4143 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4144
ed2dc618 4145 for (int i = 0; i < total_units; ++i)
9291a0cd 4146 {
ff4c9fec 4147 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4148
58f0c718
TT
4149 /* We don't want to directly expand a partial CU, because if we
4150 read it with the wrong language, then assertion failures can
4151 be triggered later on. See PR symtab/23010. So, tell
4152 dw2_instantiate_symtab to skip partial CUs -- any important
4153 partial CU will be read via DW_TAG_imported_unit anyway. */
4154 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4155 }
4156}
4157
4158static void
652a8996
JK
4159dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4160 const char *fullname)
9291a0cd 4161{
ed2dc618
SM
4162 struct dwarf2_per_objfile *dwarf2_per_objfile
4163 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4164
4165 /* We don't need to consider type units here.
4166 This is only called for examining code, e.g. expand_line_sal.
4167 There can be an order of magnitude (or more) more type units
4168 than comp units, and we avoid them if we can. */
4169
b76e467d 4170 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4171 {
3d7bb9d9 4172 /* We only need to look at symtabs not already expanded. */
43f3e411 4173 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4174 continue;
4175
b76e467d 4176 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4177 if (file_data == NULL)
9291a0cd
TT
4178 continue;
4179
b76e467d 4180 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4181 {
652a8996
JK
4182 const char *this_fullname = file_data->file_names[j];
4183
4184 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4185 {
58f0c718 4186 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4187 break;
4188 }
4189 }
4190 }
4191}
4192
9291a0cd 4193static void
199b4314
TT
4194dw2_map_matching_symbols
4195 (struct objfile *objfile,
b054970d 4196 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4197 int global,
4198 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4199 symbol_compare_ftype *ordered_compare)
9291a0cd 4200{
40658b94 4201 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4202 current language is Ada for a non-Ada objfile using GNU index. As Ada
4203 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4204}
4205
e1ef7d7a
PA
4206/* Starting from a search name, return the string that finds the upper
4207 bound of all strings that start with SEARCH_NAME in a sorted name
4208 list. Returns the empty string to indicate that the upper bound is
4209 the end of the list. */
4210
4211static std::string
4212make_sort_after_prefix_name (const char *search_name)
4213{
4214 /* When looking to complete "func", we find the upper bound of all
4215 symbols that start with "func" by looking for where we'd insert
4216 the closest string that would follow "func" in lexicographical
4217 order. Usually, that's "func"-with-last-character-incremented,
4218 i.e. "fund". Mind non-ASCII characters, though. Usually those
4219 will be UTF-8 multi-byte sequences, but we can't be certain.
4220 Especially mind the 0xff character, which is a valid character in
4221 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4222 rule out compilers allowing it in identifiers. Note that
4223 conveniently, strcmp/strcasecmp are specified to compare
4224 characters interpreted as unsigned char. So what we do is treat
4225 the whole string as a base 256 number composed of a sequence of
4226 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4227 to 0, and carries 1 to the following more-significant position.
4228 If the very first character in SEARCH_NAME ends up incremented
4229 and carries/overflows, then the upper bound is the end of the
4230 list. The string after the empty string is also the empty
4231 string.
4232
4233 Some examples of this operation:
4234
4235 SEARCH_NAME => "+1" RESULT
4236
4237 "abc" => "abd"
4238 "ab\xff" => "ac"
4239 "\xff" "a" "\xff" => "\xff" "b"
4240 "\xff" => ""
4241 "\xff\xff" => ""
4242 "" => ""
4243
4244 Then, with these symbols for example:
4245
4246 func
4247 func1
4248 fund
4249
4250 completing "func" looks for symbols between "func" and
4251 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4252 which finds "func" and "func1", but not "fund".
4253
4254 And with:
4255
4256 funcÿ (Latin1 'ÿ' [0xff])
4257 funcÿ1
4258 fund
4259
4260 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4261 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4262
4263 And with:
4264
4265 ÿÿ (Latin1 'ÿ' [0xff])
4266 ÿÿ1
4267
4268 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4269 the end of the list.
4270 */
4271 std::string after = search_name;
4272 while (!after.empty () && (unsigned char) after.back () == 0xff)
4273 after.pop_back ();
4274 if (!after.empty ())
4275 after.back () = (unsigned char) after.back () + 1;
4276 return after;
4277}
4278
5c58de74 4279/* See declaration. */
61d96d7e 4280
5c58de74
PA
4281std::pair<std::vector<name_component>::const_iterator,
4282 std::vector<name_component>::const_iterator>
44ed8f3e 4283mapped_index_base::find_name_components_bounds
3b00ef10 4284 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4285{
5c58de74
PA
4286 auto *name_cmp
4287 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4288
3b00ef10
TT
4289 const char *lang_name
4290 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4291
3f563c84
PA
4292 /* Comparison function object for lower_bound that matches against a
4293 given symbol name. */
4294 auto lookup_compare_lower = [&] (const name_component &elem,
4295 const char *name)
4296 {
5c58de74 4297 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4298 const char *elem_name = elem_qualified + elem.name_offset;
4299 return name_cmp (elem_name, name) < 0;
4300 };
4301
4302 /* Comparison function object for upper_bound that matches against a
4303 given symbol name. */
4304 auto lookup_compare_upper = [&] (const char *name,
4305 const name_component &elem)
4306 {
5c58de74 4307 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4308 const char *elem_name = elem_qualified + elem.name_offset;
4309 return name_cmp (name, elem_name) < 0;
4310 };
4311
5c58de74
PA
4312 auto begin = this->name_components.begin ();
4313 auto end = this->name_components.end ();
3f563c84
PA
4314
4315 /* Find the lower bound. */
4316 auto lower = [&] ()
4317 {
3b00ef10 4318 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4319 return begin;
4320 else
3b00ef10 4321 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4322 } ();
4323
4324 /* Find the upper bound. */
4325 auto upper = [&] ()
4326 {
5c58de74 4327 if (lookup_name_without_params.completion_mode ())
3f563c84 4328 {
e1ef7d7a
PA
4329 /* In completion mode, we want UPPER to point past all
4330 symbols names that have the same prefix. I.e., with
4331 these symbols, and completing "func":
4332
4333 function << lower bound
4334 function1
4335 other_function << upper bound
4336
4337 We find the upper bound by looking for the insertion
4338 point of "func"-with-last-character-incremented,
4339 i.e. "fund". */
3b00ef10 4340 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4341 if (after.empty ())
3f563c84 4342 return end;
e6b2f5ef
PA
4343 return std::lower_bound (lower, end, after.c_str (),
4344 lookup_compare_lower);
3f563c84
PA
4345 }
4346 else
3b00ef10 4347 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4348 } ();
4349
5c58de74
PA
4350 return {lower, upper};
4351}
4352
4353/* See declaration. */
4354
4355void
44ed8f3e 4356mapped_index_base::build_name_components ()
5c58de74
PA
4357{
4358 if (!this->name_components.empty ())
4359 return;
4360
4361 this->name_components_casing = case_sensitivity;
4362 auto *name_cmp
4363 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4364
4365 /* The code below only knows how to break apart components of C++
4366 symbol names (and other languages that use '::' as
3b00ef10 4367 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4368 auto count = this->symbol_name_count ();
4369 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4370 {
44ed8f3e 4371 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4372 continue;
4373
4374 const char *name = this->symbol_name_at (idx);
4375
4376 /* Add each name component to the name component table. */
4377 unsigned int previous_len = 0;
3b00ef10
TT
4378
4379 if (strstr (name, "::") != nullptr)
4380 {
4381 for (unsigned int current_len = cp_find_first_component (name);
4382 name[current_len] != '\0';
4383 current_len += cp_find_first_component (name + current_len))
4384 {
4385 gdb_assert (name[current_len] == ':');
4386 this->name_components.push_back ({previous_len, idx});
4387 /* Skip the '::'. */
4388 current_len += 2;
4389 previous_len = current_len;
4390 }
4391 }
4392 else
5c58de74 4393 {
3b00ef10
TT
4394 /* Handle the Ada encoded (aka mangled) form here. */
4395 for (const char *iter = strstr (name, "__");
4396 iter != nullptr;
4397 iter = strstr (iter, "__"))
4398 {
4399 this->name_components.push_back ({previous_len, idx});
4400 iter += 2;
4401 previous_len = iter - name;
4402 }
5c58de74 4403 }
3b00ef10 4404
5c58de74
PA
4405 this->name_components.push_back ({previous_len, idx});
4406 }
4407
4408 /* Sort name_components elements by name. */
4409 auto name_comp_compare = [&] (const name_component &left,
4410 const name_component &right)
4411 {
4412 const char *left_qualified = this->symbol_name_at (left.idx);
4413 const char *right_qualified = this->symbol_name_at (right.idx);
4414
4415 const char *left_name = left_qualified + left.name_offset;
4416 const char *right_name = right_qualified + right.name_offset;
4417
4418 return name_cmp (left_name, right_name) < 0;
4419 };
4420
4421 std::sort (this->name_components.begin (),
4422 this->name_components.end (),
4423 name_comp_compare);
4424}
4425
4426/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4427 mapped_index_base instead of the containing objfile. This is split
4428 to a separate function in order to be able to unit test the
4429 name_components matching using a mock mapped_index_base. For each
5c58de74 4430 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4431 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4432
4433static void
4434dw2_expand_symtabs_matching_symbol
44ed8f3e 4435 (mapped_index_base &index,
5c58de74
PA
4436 const lookup_name_info &lookup_name_in,
4437 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4438 enum search_domain kind,
3b00ef10 4439 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4440{
4441 lookup_name_info lookup_name_without_params
4442 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4443
4444 /* Build the symbol name component sorted vector, if we haven't
4445 yet. */
4446 index.build_name_components ();
4447
3f563c84
PA
4448 /* The same symbol may appear more than once in the range though.
4449 E.g., if we're looking for symbols that complete "w", and we have
4450 a symbol named "w1::w2", we'll find the two name components for
4451 that same symbol in the range. To be sure we only call the
4452 callback once per symbol, we first collect the symbol name
4453 indexes that matched in a temporary vector and ignore
4454 duplicates. */
4455 std::vector<offset_type> matches;
3f563c84 4456
3b00ef10
TT
4457 struct name_and_matcher
4458 {
4459 symbol_name_matcher_ftype *matcher;
4460 const std::string &name;
4461
4462 bool operator== (const name_and_matcher &other) const
3f563c84 4463 {
3b00ef10
TT
4464 return matcher == other.matcher && name == other.name;
4465 }
4466 };
4467
4468 /* A vector holding all the different symbol name matchers, for all
4469 languages. */
4470 std::vector<name_and_matcher> matchers;
4471
4472 for (int i = 0; i < nr_languages; i++)
4473 {
4474 enum language lang_e = (enum language) i;
4475
4476 const language_defn *lang = language_def (lang_e);
4477 symbol_name_matcher_ftype *name_matcher
4478 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4479
3b00ef10
TT
4480 name_and_matcher key {
4481 name_matcher,
4482 lookup_name_without_params.language_lookup_name (lang_e)
4483 };
4484
4485 /* Don't insert the same comparison routine more than once.
4486 Note that we do this linear walk. This is not a problem in
4487 practice because the number of supported languages is
4488 low. */
4489 if (std::find (matchers.begin (), matchers.end (), key)
4490 != matchers.end ())
9291a0cd 4491 continue;
3b00ef10
TT
4492 matchers.push_back (std::move (key));
4493
4494 auto bounds
4495 = index.find_name_components_bounds (lookup_name_without_params,
4496 lang_e);
4497
4498 /* Now for each symbol name in range, check to see if we have a name
4499 match, and if so, call the MATCH_CALLBACK callback. */
4500
4501 for (; bounds.first != bounds.second; ++bounds.first)
4502 {
4503 const char *qualified = index.symbol_name_at (bounds.first->idx);
4504
4505 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4506 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4507 continue;
9291a0cd 4508
3b00ef10
TT
4509 matches.push_back (bounds.first->idx);
4510 }
3f563c84
PA
4511 }
4512
4513 std::sort (matches.begin (), matches.end ());
4514
4515 /* Finally call the callback, once per match. */
4516 ULONGEST prev = -1;
4517 for (offset_type idx : matches)
4518 {
4519 if (prev != idx)
4520 {
3b00ef10
TT
4521 if (!match_callback (idx))
4522 break;
3f563c84
PA
4523 prev = idx;
4524 }
4525 }
4526
4527 /* Above we use a type wider than idx's for 'prev', since 0 and
4528 (offset_type)-1 are both possible values. */
4529 static_assert (sizeof (prev) > sizeof (offset_type), "");
4530}
4531
c62446b1
PA
4532#if GDB_SELF_TEST
4533
4534namespace selftests { namespace dw2_expand_symtabs_matching {
4535
a3c5fafd
PA
4536/* A mock .gdb_index/.debug_names-like name index table, enough to
4537 exercise dw2_expand_symtabs_matching_symbol, which works with the
4538 mapped_index_base interface. Builds an index from the symbol list
4539 passed as parameter to the constructor. */
4540class mock_mapped_index : public mapped_index_base
c62446b1
PA
4541{
4542public:
a3c5fafd
PA
4543 mock_mapped_index (gdb::array_view<const char *> symbols)
4544 : m_symbol_table (symbols)
c62446b1
PA
4545 {}
4546
a3c5fafd 4547 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4548
a3c5fafd 4549 /* Return the number of names in the symbol table. */
632e107b 4550 size_t symbol_name_count () const override
c62446b1 4551 {
a3c5fafd 4552 return m_symbol_table.size ();
c62446b1
PA
4553 }
4554
a3c5fafd 4555 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4556 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4557 {
4558 return m_symbol_table[idx];
4559 }
c62446b1 4560
a3c5fafd
PA
4561private:
4562 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4563};
4564
4565/* Convenience function that converts a NULL pointer to a "<null>"
4566 string, to pass to print routines. */
4567
4568static const char *
4569string_or_null (const char *str)
4570{
4571 return str != NULL ? str : "<null>";
4572}
4573
4574/* Check if a lookup_name_info built from
4575 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4576 index. EXPECTED_LIST is the list of expected matches, in expected
4577 matching order. If no match expected, then an empty list is
4578 specified. Returns true on success. On failure prints a warning
4579 indicating the file:line that failed, and returns false. */
4580
4581static bool
4582check_match (const char *file, int line,
4583 mock_mapped_index &mock_index,
4584 const char *name, symbol_name_match_type match_type,
4585 bool completion_mode,
4586 std::initializer_list<const char *> expected_list)
4587{
4588 lookup_name_info lookup_name (name, match_type, completion_mode);
4589
4590 bool matched = true;
4591
4592 auto mismatch = [&] (const char *expected_str,
4593 const char *got)
4594 {
4595 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4596 "expected=\"%s\", got=\"%s\"\n"),
4597 file, line,
4598 (match_type == symbol_name_match_type::FULL
4599 ? "FULL" : "WILD"),
4600 name, string_or_null (expected_str), string_or_null (got));
4601 matched = false;
4602 };
4603
4604 auto expected_it = expected_list.begin ();
4605 auto expected_end = expected_list.end ();
4606
a3c5fafd 4607 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4608 NULL, ALL_DOMAIN,
4609 [&] (offset_type idx)
4610 {
a3c5fafd 4611 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4612 const char *expected_str
4613 = expected_it == expected_end ? NULL : *expected_it++;
4614
4615 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4616 mismatch (expected_str, matched_name);
3b00ef10 4617 return true;
c62446b1
PA
4618 });
4619
4620 const char *expected_str
4621 = expected_it == expected_end ? NULL : *expected_it++;
4622 if (expected_str != NULL)
4623 mismatch (expected_str, NULL);
4624
4625 return matched;
4626}
4627
4628/* The symbols added to the mock mapped_index for testing (in
4629 canonical form). */
4630static const char *test_symbols[] = {
4631 "function",
4632 "std::bar",
4633 "std::zfunction",
4634 "std::zfunction2",
4635 "w1::w2",
4636 "ns::foo<char*>",
4637 "ns::foo<int>",
4638 "ns::foo<long>",
a20714ff
PA
4639 "ns2::tmpl<int>::foo2",
4640 "(anonymous namespace)::A::B::C",
c62446b1 4641
e1ef7d7a
PA
4642 /* These are used to check that the increment-last-char in the
4643 matching algorithm for completion doesn't match "t1_fund" when
4644 completing "t1_func". */
4645 "t1_func",
4646 "t1_func1",
4647 "t1_fund",
4648 "t1_fund1",
4649
4650 /* A UTF-8 name with multi-byte sequences to make sure that
4651 cp-name-parser understands this as a single identifier ("função"
4652 is "function" in PT). */
4653 u8"u8função",
4654
4655 /* \377 (0xff) is Latin1 'ÿ'. */
4656 "yfunc\377",
4657
4658 /* \377 (0xff) is Latin1 'ÿ'. */
4659 "\377",
4660 "\377\377123",
4661
c62446b1
PA
4662 /* A name with all sorts of complications. Starts with "z" to make
4663 it easier for the completion tests below. */
4664#define Z_SYM_NAME \
4665 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4666 "::tuple<(anonymous namespace)::ui*, " \
4667 "std::default_delete<(anonymous namespace)::ui>, void>"
4668
4669 Z_SYM_NAME
4670};
4671
a3c5fafd
PA
4672/* Returns true if the mapped_index_base::find_name_component_bounds
4673 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4674 in completion mode. */
5c58de74
PA
4675
4676static bool
a3c5fafd 4677check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4678 const char *search_name,
4679 gdb::array_view<const char *> expected_syms)
4680{
4681 lookup_name_info lookup_name (search_name,
4682 symbol_name_match_type::FULL, true);
4683
3b00ef10
TT
4684 auto bounds = index.find_name_components_bounds (lookup_name,
4685 language_cplus);
5c58de74
PA
4686
4687 size_t distance = std::distance (bounds.first, bounds.second);
4688 if (distance != expected_syms.size ())
4689 return false;
4690
4691 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4692 {
4693 auto nc_elem = bounds.first + exp_elem;
4694 const char *qualified = index.symbol_name_at (nc_elem->idx);
4695 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4696 return false;
4697 }
4698
4699 return true;
4700}
4701
4702/* Test the lower-level mapped_index::find_name_component_bounds
4703 method. */
4704
c62446b1 4705static void
5c58de74
PA
4706test_mapped_index_find_name_component_bounds ()
4707{
4708 mock_mapped_index mock_index (test_symbols);
4709
a3c5fafd 4710 mock_index.build_name_components ();
5c58de74
PA
4711
4712 /* Test the lower-level mapped_index::find_name_component_bounds
4713 method in completion mode. */
4714 {
4715 static const char *expected_syms[] = {
4716 "t1_func",
4717 "t1_func1",
5c58de74
PA
4718 };
4719
a3c5fafd 4720 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4721 "t1_func", expected_syms));
4722 }
4723
4724 /* Check that the increment-last-char in the name matching algorithm
4725 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4726 {
4727 static const char *expected_syms1[] = {
4728 "\377",
4729 "\377\377123",
4730 };
a3c5fafd 4731 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4732 "\377", expected_syms1));
4733
4734 static const char *expected_syms2[] = {
4735 "\377\377123",
4736 };
a3c5fafd 4737 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4738 "\377\377", expected_syms2));
4739 }
4740}
4741
4742/* Test dw2_expand_symtabs_matching_symbol. */
4743
4744static void
4745test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4746{
4747 mock_mapped_index mock_index (test_symbols);
4748
4749 /* We let all tests run until the end even if some fails, for debug
4750 convenience. */
4751 bool any_mismatch = false;
4752
4753 /* Create the expected symbols list (an initializer_list). Needed
4754 because lists have commas, and we need to pass them to CHECK,
4755 which is a macro. */
4756#define EXPECT(...) { __VA_ARGS__ }
4757
4758 /* Wrapper for check_match that passes down the current
4759 __FILE__/__LINE__. */
4760#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4761 any_mismatch |= !check_match (__FILE__, __LINE__, \
4762 mock_index, \
4763 NAME, MATCH_TYPE, COMPLETION_MODE, \
4764 EXPECTED_LIST)
4765
4766 /* Identity checks. */
4767 for (const char *sym : test_symbols)
4768 {
4769 /* Should be able to match all existing symbols. */
4770 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4771 EXPECT (sym));
4772
4773 /* Should be able to match all existing symbols with
4774 parameters. */
4775 std::string with_params = std::string (sym) + "(int)";
4776 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4777 EXPECT (sym));
4778
4779 /* Should be able to match all existing symbols with
4780 parameters and qualifiers. */
4781 with_params = std::string (sym) + " ( int ) const";
4782 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4783 EXPECT (sym));
4784
4785 /* This should really find sym, but cp-name-parser.y doesn't
4786 know about lvalue/rvalue qualifiers yet. */
4787 with_params = std::string (sym) + " ( int ) &&";
4788 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4789 {});
4790 }
4791
e1ef7d7a
PA
4792 /* Check that the name matching algorithm for completion doesn't get
4793 confused with Latin1 'ÿ' / 0xff. */
4794 {
4795 static const char str[] = "\377";
4796 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4797 EXPECT ("\377", "\377\377123"));
4798 }
4799
4800 /* Check that the increment-last-char in the matching algorithm for
4801 completion doesn't match "t1_fund" when completing "t1_func". */
4802 {
4803 static const char str[] = "t1_func";
4804 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4805 EXPECT ("t1_func", "t1_func1"));
4806 }
4807
c62446b1
PA
4808 /* Check that completion mode works at each prefix of the expected
4809 symbol name. */
4810 {
4811 static const char str[] = "function(int)";
4812 size_t len = strlen (str);
4813 std::string lookup;
4814
4815 for (size_t i = 1; i < len; i++)
4816 {
4817 lookup.assign (str, i);
4818 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4819 EXPECT ("function"));
4820 }
4821 }
4822
4823 /* While "w" is a prefix of both components, the match function
4824 should still only be called once. */
4825 {
4826 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4827 EXPECT ("w1::w2"));
a20714ff
PA
4828 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4829 EXPECT ("w1::w2"));
c62446b1
PA
4830 }
4831
4832 /* Same, with a "complicated" symbol. */
4833 {
4834 static const char str[] = Z_SYM_NAME;
4835 size_t len = strlen (str);
4836 std::string lookup;
4837
4838 for (size_t i = 1; i < len; i++)
4839 {
4840 lookup.assign (str, i);
4841 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4842 EXPECT (Z_SYM_NAME));
4843 }
4844 }
4845
4846 /* In FULL mode, an incomplete symbol doesn't match. */
4847 {
4848 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4849 {});
4850 }
4851
4852 /* A complete symbol with parameters matches any overload, since the
4853 index has no overload info. */
4854 {
4855 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4856 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4857 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4858 EXPECT ("std::zfunction", "std::zfunction2"));
4859 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4860 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4861 }
4862
4863 /* Check that whitespace is ignored appropriately. A symbol with a
4864 template argument list. */
4865 {
4866 static const char expected[] = "ns::foo<int>";
4867 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4868 EXPECT (expected));
a20714ff
PA
4869 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4870 EXPECT (expected));
c62446b1
PA
4871 }
4872
4873 /* Check that whitespace is ignored appropriately. A symbol with a
4874 template argument list that includes a pointer. */
4875 {
4876 static const char expected[] = "ns::foo<char*>";
4877 /* Try both completion and non-completion modes. */
4878 static const bool completion_mode[2] = {false, true};
4879 for (size_t i = 0; i < 2; i++)
4880 {
4881 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4882 completion_mode[i], EXPECT (expected));
a20714ff
PA
4883 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4884 completion_mode[i], EXPECT (expected));
c62446b1
PA
4885
4886 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4887 completion_mode[i], EXPECT (expected));
a20714ff
PA
4888 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4889 completion_mode[i], EXPECT (expected));
c62446b1
PA
4890 }
4891 }
4892
4893 {
4894 /* Check method qualifiers are ignored. */
4895 static const char expected[] = "ns::foo<char*>";
4896 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4897 symbol_name_match_type::FULL, true, EXPECT (expected));
4898 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4899 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4900 CHECK_MATCH ("foo < char * > ( int ) const",
4901 symbol_name_match_type::WILD, true, EXPECT (expected));
4902 CHECK_MATCH ("foo < char * > ( int ) &&",
4903 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4904 }
4905
4906 /* Test lookup names that don't match anything. */
4907 {
a20714ff
PA
4908 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4909 {});
4910
c62446b1
PA
4911 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4912 {});
4913 }
4914
a20714ff
PA
4915 /* Some wild matching tests, exercising "(anonymous namespace)",
4916 which should not be confused with a parameter list. */
4917 {
4918 static const char *syms[] = {
4919 "A::B::C",
4920 "B::C",
4921 "C",
4922 "A :: B :: C ( int )",
4923 "B :: C ( int )",
4924 "C ( int )",
4925 };
4926
4927 for (const char *s : syms)
4928 {
4929 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4930 EXPECT ("(anonymous namespace)::A::B::C"));
4931 }
4932 }
4933
4934 {
4935 static const char expected[] = "ns2::tmpl<int>::foo2";
4936 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4937 EXPECT (expected));
4938 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4939 EXPECT (expected));
4940 }
4941
c62446b1
PA
4942 SELF_CHECK (!any_mismatch);
4943
4944#undef EXPECT
4945#undef CHECK_MATCH
4946}
4947
5c58de74
PA
4948static void
4949run_test ()
4950{
4951 test_mapped_index_find_name_component_bounds ();
4952 test_dw2_expand_symtabs_matching_symbol ();
4953}
4954
c62446b1
PA
4955}} // namespace selftests::dw2_expand_symtabs_matching
4956
4957#endif /* GDB_SELF_TEST */
4958
4b514bc8
JK
4959/* If FILE_MATCHER is NULL or if PER_CU has
4960 dwarf2_per_cu_quick_data::MARK set (see
4961 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4962 EXPANSION_NOTIFY on it. */
4963
4964static void
4965dw2_expand_symtabs_matching_one
4966 (struct dwarf2_per_cu_data *per_cu,
4967 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4968 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4969{
4970 if (file_matcher == NULL || per_cu->v.quick->mark)
4971 {
4972 bool symtab_was_null
4973 = (per_cu->v.quick->compunit_symtab == NULL);
4974
58f0c718 4975 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4976
4977 if (expansion_notify != NULL
4978 && symtab_was_null
4979 && per_cu->v.quick->compunit_symtab != NULL)
4980 expansion_notify (per_cu->v.quick->compunit_symtab);
4981 }
4982}
4983
3f563c84
PA
4984/* Helper for dw2_expand_matching symtabs. Called on each symbol
4985 matched, to expand corresponding CUs that were marked. IDX is the
4986 index of the symbol name that matched. */
4987
4988static void
4989dw2_expand_marked_cus
ed2dc618 4990 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4991 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4992 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4993 search_domain kind)
4994{
3f563c84
PA
4995 offset_type *vec, vec_len, vec_idx;
4996 bool global_seen = false;
ed2dc618 4997 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4998
61920122 4999 vec = (offset_type *) (index.constant_pool
f00a2de2 5000 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5001 vec_len = MAYBE_SWAP (vec[0]);
5002 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5003 {
61920122
PA
5004 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5005 /* This value is only valid for index versions >= 7. */
5006 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5007 gdb_index_symbol_kind symbol_kind =
5008 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5009 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5010 /* Only check the symbol attributes if they're present.
5011 Indices prior to version 7 don't record them,
5012 and indices >= 7 may elide them for certain symbols
5013 (gold does this). */
5014 int attrs_valid =
5015 (index.version >= 7
5016 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5017
5018 /* Work around gold/15646. */
5019 if (attrs_valid)
9291a0cd 5020 {
61920122
PA
5021 if (!is_static && global_seen)
5022 continue;
5023 if (!is_static)
5024 global_seen = true;
5025 }
3190f0c6 5026
61920122
PA
5027 /* Only check the symbol's kind if it has one. */
5028 if (attrs_valid)
5029 {
5030 switch (kind)
8943b874 5031 {
61920122
PA
5032 case VARIABLES_DOMAIN:
5033 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5034 continue;
5035 break;
5036 case FUNCTIONS_DOMAIN:
5037 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5038 continue;
61920122
PA
5039 break;
5040 case TYPES_DOMAIN:
5041 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5042 continue;
5043 break;
5044 default:
5045 break;
8943b874 5046 }
61920122 5047 }
8943b874 5048
61920122 5049 /* Don't crash on bad data. */
b76e467d 5050 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5051 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5052 {
b98664d3 5053 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5054 " [in module %s]"),
5055 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5056 continue;
5057 }
5058
ff4c9fec 5059 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5060 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5061 expansion_notify);
61920122
PA
5062 }
5063}
5064
4b514bc8
JK
5065/* If FILE_MATCHER is non-NULL, set all the
5066 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5067 that match FILE_MATCHER. */
5068
61920122 5069static void
4b514bc8 5070dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5071 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5072 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5073{
4b514bc8 5074 if (file_matcher == NULL)
61920122
PA
5075 return;
5076
4b514bc8
JK
5077 objfile *const objfile = dwarf2_per_objfile->objfile;
5078
5079 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5080 htab_eq_pointer,
5081 NULL, xcalloc, xfree));
5082 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5083 htab_eq_pointer,
5084 NULL, xcalloc, xfree));
61920122 5085
4b514bc8
JK
5086 /* The rule is CUs specify all the files, including those used by
5087 any TU, so there's no need to scan TUs here. */
61920122 5088
b76e467d 5089 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5090 {
927aa2e7
JK
5091 QUIT;
5092
5093 per_cu->v.quick->mark = 0;
5094
5095 /* We only need to look at symtabs not already expanded. */
5096 if (per_cu->v.quick->compunit_symtab)
5097 continue;
5098
b76e467d 5099 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5100 if (file_data == NULL)
5101 continue;
5102
5103 if (htab_find (visited_not_found.get (), file_data) != NULL)
5104 continue;
5105 else if (htab_find (visited_found.get (), file_data) != NULL)
5106 {
5107 per_cu->v.quick->mark = 1;
5108 continue;
5109 }
5110
b76e467d 5111 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5112 {
5113 const char *this_real_name;
5114
5115 if (file_matcher (file_data->file_names[j], false))
5116 {
5117 per_cu->v.quick->mark = 1;
5118 break;
5119 }
5120
5121 /* Before we invoke realpath, which can get expensive when many
5122 files are involved, do a quick comparison of the basenames. */
5123 if (!basenames_may_differ
5124 && !file_matcher (lbasename (file_data->file_names[j]),
5125 true))
5126 continue;
5127
5128 this_real_name = dw2_get_real_path (objfile, file_data, j);
5129 if (file_matcher (this_real_name, false))
5130 {
5131 per_cu->v.quick->mark = 1;
5132 break;
5133 }
5134 }
5135
b76e467d
SM
5136 void **slot = htab_find_slot (per_cu->v.quick->mark
5137 ? visited_found.get ()
5138 : visited_not_found.get (),
5139 file_data, INSERT);
927aa2e7
JK
5140 *slot = file_data;
5141 }
5142}
5143
5144static void
5145dw2_expand_symtabs_matching
5146 (struct objfile *objfile,
5147 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5148 const lookup_name_info &lookup_name,
5149 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5150 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5151 enum search_domain kind)
5152{
ed2dc618
SM
5153 struct dwarf2_per_objfile *dwarf2_per_objfile
5154 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5155
5156 /* index_table is NULL if OBJF_READNOW. */
5157 if (!dwarf2_per_objfile->index_table)
5158 return;
5159
ed2dc618 5160 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5161
5162 mapped_index &index = *dwarf2_per_objfile->index_table;
5163
5164 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5165 symbol_matcher,
5166 kind, [&] (offset_type idx)
5167 {
ed2dc618 5168 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5169 expansion_notify, kind);
3b00ef10 5170 return true;
927aa2e7
JK
5171 });
5172}
5173
5174/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5175 symtab. */
5176
5177static struct compunit_symtab *
5178recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5179 CORE_ADDR pc)
5180{
5181 int i;
5182
5183 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5184 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5185 return cust;
5186
5187 if (cust->includes == NULL)
5188 return NULL;
5189
5190 for (i = 0; cust->includes[i]; ++i)
5191 {
5192 struct compunit_symtab *s = cust->includes[i];
5193
5194 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5195 if (s != NULL)
5196 return s;
5197 }
5198
5199 return NULL;
5200}
5201
5202static struct compunit_symtab *
5203dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5204 struct bound_minimal_symbol msymbol,
5205 CORE_ADDR pc,
5206 struct obj_section *section,
5207 int warn_if_readin)
5208{
5209 struct dwarf2_per_cu_data *data;
5210 struct compunit_symtab *result;
5211
d320c2b5 5212 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5213 return NULL;
5214
79748972
TT
5215 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5216 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5217 data = (struct dwarf2_per_cu_data *) addrmap_find
5218 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5219 if (!data)
5220 return NULL;
5221
5222 if (warn_if_readin && data->v.quick->compunit_symtab)
5223 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5224 paddress (get_objfile_arch (objfile), pc));
5225
5226 result
58f0c718
TT
5227 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5228 false),
927aa2e7
JK
5229 pc);
5230 gdb_assert (result != NULL);
5231 return result;
5232}
5233
5234static void
5235dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5236 void *data, int need_fullname)
5237{
ed2dc618
SM
5238 struct dwarf2_per_objfile *dwarf2_per_objfile
5239 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5240
5241 if (!dwarf2_per_objfile->filenames_cache)
5242 {
5243 dwarf2_per_objfile->filenames_cache.emplace ();
5244
5245 htab_up visited (htab_create_alloc (10,
5246 htab_hash_pointer, htab_eq_pointer,
5247 NULL, xcalloc, xfree));
5248
5249 /* The rule is CUs specify all the files, including those used
5250 by any TU, so there's no need to scan TUs here. We can
5251 ignore file names coming from already-expanded CUs. */
5252
b76e467d 5253 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5254 {
927aa2e7
JK
5255 if (per_cu->v.quick->compunit_symtab)
5256 {
5257 void **slot = htab_find_slot (visited.get (),
5258 per_cu->v.quick->file_names,
5259 INSERT);
5260
5261 *slot = per_cu->v.quick->file_names;
5262 }
5263 }
5264
b76e467d 5265 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5266 {
927aa2e7
JK
5267 /* We only need to look at symtabs not already expanded. */
5268 if (per_cu->v.quick->compunit_symtab)
5269 continue;
5270
b76e467d 5271 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5272 if (file_data == NULL)
5273 continue;
5274
b76e467d 5275 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5276 if (*slot)
5277 {
5278 /* Already visited. */
5279 continue;
5280 }
5281 *slot = file_data;
5282
5283 for (int j = 0; j < file_data->num_file_names; ++j)
5284 {
5285 const char *filename = file_data->file_names[j];
5286 dwarf2_per_objfile->filenames_cache->seen (filename);
5287 }
5288 }
5289 }
5290
5291 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5292 {
5293 gdb::unique_xmalloc_ptr<char> this_real_name;
5294
5295 if (need_fullname)
5296 this_real_name = gdb_realpath (filename);
5297 (*fun) (filename, this_real_name.get (), data);
5298 });
5299}
5300
5301static int
5302dw2_has_symbols (struct objfile *objfile)
5303{
5304 return 1;
5305}
5306
5307const struct quick_symbol_functions dwarf2_gdb_index_functions =
5308{
5309 dw2_has_symbols,
5310 dw2_find_last_source_symtab,
5311 dw2_forget_cached_source_info,
5312 dw2_map_symtabs_matching_filename,
5313 dw2_lookup_symbol,
5314 dw2_print_stats,
5315 dw2_dump,
927aa2e7
JK
5316 dw2_expand_symtabs_for_function,
5317 dw2_expand_all_symtabs,
5318 dw2_expand_symtabs_with_fullname,
5319 dw2_map_matching_symbols,
5320 dw2_expand_symtabs_matching,
5321 dw2_find_pc_sect_compunit_symtab,
5322 NULL,
5323 dw2_map_symbol_filenames
5324};
5325
5326/* DWARF-5 debug_names reader. */
5327
5328/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5329static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5330
5331/* A helper function that reads the .debug_names section in SECTION
5332 and fills in MAP. FILENAME is the name of the file containing the
5333 section; it is used for error reporting.
5334
5335 Returns true if all went well, false otherwise. */
5336
5337static bool
5338read_debug_names_from_section (struct objfile *objfile,
5339 const char *filename,
5340 struct dwarf2_section_info *section,
5341 mapped_debug_names &map)
5342{
5343 if (dwarf2_section_empty_p (section))
5344 return false;
5345
5346 /* Older elfutils strip versions could keep the section in the main
5347 executable while splitting it for the separate debug info file. */
5348 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5349 return false;
5350
5351 dwarf2_read_section (objfile, section);
5352
5353 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5354
5355 const gdb_byte *addr = section->buffer;
5356
5357 bfd *const abfd = get_section_bfd_owner (section);
5358
5359 unsigned int bytes_read;
5360 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5361 addr += bytes_read;
5362
5363 map.dwarf5_is_dwarf64 = bytes_read != 4;
5364 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5365 if (bytes_read + length != section->size)
5366 {
5367 /* There may be multiple per-CU indices. */
5368 warning (_("Section .debug_names in %s length %s does not match "
5369 "section length %s, ignoring .debug_names."),
5370 filename, plongest (bytes_read + length),
5371 pulongest (section->size));
5372 return false;
5373 }
5374
5375 /* The version number. */
5376 uint16_t version = read_2_bytes (abfd, addr);
5377 addr += 2;
5378 if (version != 5)
5379 {
5380 warning (_("Section .debug_names in %s has unsupported version %d, "
5381 "ignoring .debug_names."),
5382 filename, version);
5383 return false;
5384 }
5385
5386 /* Padding. */
5387 uint16_t padding = read_2_bytes (abfd, addr);
5388 addr += 2;
5389 if (padding != 0)
5390 {
5391 warning (_("Section .debug_names in %s has unsupported padding %d, "
5392 "ignoring .debug_names."),
5393 filename, padding);
5394 return false;
5395 }
5396
5397 /* comp_unit_count - The number of CUs in the CU list. */
5398 map.cu_count = read_4_bytes (abfd, addr);
5399 addr += 4;
5400
5401 /* local_type_unit_count - The number of TUs in the local TU
5402 list. */
5403 map.tu_count = read_4_bytes (abfd, addr);
5404 addr += 4;
5405
5406 /* foreign_type_unit_count - The number of TUs in the foreign TU
5407 list. */
5408 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5409 addr += 4;
5410 if (foreign_tu_count != 0)
5411 {
5412 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5413 "ignoring .debug_names."),
5414 filename, static_cast<unsigned long> (foreign_tu_count));
5415 return false;
5416 }
5417
5418 /* bucket_count - The number of hash buckets in the hash lookup
5419 table. */
5420 map.bucket_count = read_4_bytes (abfd, addr);
5421 addr += 4;
5422
5423 /* name_count - The number of unique names in the index. */
5424 map.name_count = read_4_bytes (abfd, addr);
5425 addr += 4;
5426
5427 /* abbrev_table_size - The size in bytes of the abbreviations
5428 table. */
5429 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5430 addr += 4;
5431
5432 /* augmentation_string_size - The size in bytes of the augmentation
5433 string. This value is rounded up to a multiple of 4. */
5434 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5435 addr += 4;
5436 map.augmentation_is_gdb = ((augmentation_string_size
5437 == sizeof (dwarf5_augmentation))
5438 && memcmp (addr, dwarf5_augmentation,
5439 sizeof (dwarf5_augmentation)) == 0);
5440 augmentation_string_size += (-augmentation_string_size) & 3;
5441 addr += augmentation_string_size;
5442
5443 /* List of CUs */
5444 map.cu_table_reordered = addr;
5445 addr += map.cu_count * map.offset_size;
5446
5447 /* List of Local TUs */
5448 map.tu_table_reordered = addr;
5449 addr += map.tu_count * map.offset_size;
5450
5451 /* Hash Lookup Table */
5452 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5453 addr += map.bucket_count * 4;
5454 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5455 addr += map.name_count * 4;
5456
5457 /* Name Table */
5458 map.name_table_string_offs_reordered = addr;
5459 addr += map.name_count * map.offset_size;
5460 map.name_table_entry_offs_reordered = addr;
5461 addr += map.name_count * map.offset_size;
5462
5463 const gdb_byte *abbrev_table_start = addr;
5464 for (;;)
5465 {
927aa2e7
JK
5466 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5467 addr += bytes_read;
5468 if (index_num == 0)
5469 break;
5470
5471 const auto insertpair
5472 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5473 if (!insertpair.second)
5474 {
5475 warning (_("Section .debug_names in %s has duplicate index %s, "
5476 "ignoring .debug_names."),
5477 filename, pulongest (index_num));
5478 return false;
5479 }
5480 mapped_debug_names::index_val &indexval = insertpair.first->second;
5481 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5482 addr += bytes_read;
5483
5484 for (;;)
5485 {
5486 mapped_debug_names::index_val::attr attr;
5487 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5488 addr += bytes_read;
5489 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 if (attr.form == DW_FORM_implicit_const)
5492 {
5493 attr.implicit_const = read_signed_leb128 (abfd, addr,
5494 &bytes_read);
5495 addr += bytes_read;
5496 }
5497 if (attr.dw_idx == 0 && attr.form == 0)
5498 break;
5499 indexval.attr_vec.push_back (std::move (attr));
5500 }
5501 }
5502 if (addr != abbrev_table_start + abbrev_table_size)
5503 {
5504 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5505 "of size %s vs. written as %u, ignoring .debug_names."),
5506 filename, plongest (addr - abbrev_table_start),
5507 abbrev_table_size);
927aa2e7
JK
5508 return false;
5509 }
5510 map.entry_pool = addr;
5511
5512 return true;
5513}
5514
5515/* A helper for create_cus_from_debug_names that handles the MAP's CU
5516 list. */
5517
5518static void
ed2dc618 5519create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5520 const mapped_debug_names &map,
5521 dwarf2_section_info &section,
b76e467d 5522 bool is_dwz)
927aa2e7
JK
5523{
5524 sect_offset sect_off_prev;
5525 for (uint32_t i = 0; i <= map.cu_count; ++i)
5526 {
5527 sect_offset sect_off_next;
5528 if (i < map.cu_count)
5529 {
5530 sect_off_next
5531 = (sect_offset) (extract_unsigned_integer
5532 (map.cu_table_reordered + i * map.offset_size,
5533 map.offset_size,
5534 map.dwarf5_byte_order));
5535 }
5536 else
5537 sect_off_next = (sect_offset) section.size;
5538 if (i >= 1)
5539 {
5540 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5541 dwarf2_per_cu_data *per_cu
ed2dc618 5542 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5543 sect_off_prev, length);
b76e467d 5544 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5545 }
5546 sect_off_prev = sect_off_next;
5547 }
5548}
5549
5550/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5551 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5552
5553static void
ed2dc618 5554create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5555 const mapped_debug_names &map,
5556 const mapped_debug_names &dwz_map)
5557{
b76e467d
SM
5558 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5559 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5560
ed2dc618
SM
5561 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5562 dwarf2_per_objfile->info,
b76e467d 5563 false /* is_dwz */);
927aa2e7
JK
5564
5565 if (dwz_map.cu_count == 0)
5566 return;
5567
ed2dc618
SM
5568 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5569 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5570 true /* is_dwz */);
927aa2e7
JK
5571}
5572
5573/* Read .debug_names. If everything went ok, initialize the "quick"
5574 elements of all the CUs and return true. Otherwise, return false. */
5575
5576static bool
ed2dc618 5577dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5578{
22ca247e
TT
5579 std::unique_ptr<mapped_debug_names> map
5580 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5581 mapped_debug_names dwz_map (dwarf2_per_objfile);
5582 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5583
5584 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5585 &dwarf2_per_objfile->debug_names,
22ca247e 5586 *map))
927aa2e7
JK
5587 return false;
5588
5589 /* Don't use the index if it's empty. */
22ca247e 5590 if (map->name_count == 0)
927aa2e7
JK
5591 return false;
5592
5593 /* If there is a .dwz file, read it so we can get its CU list as
5594 well. */
ed2dc618 5595 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5596 if (dwz != NULL)
5597 {
5598 if (!read_debug_names_from_section (objfile,
00f93c44 5599 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5600 &dwz->debug_names, dwz_map))
5601 {
5602 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5603 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5604 return false;
5605 }
5606 }
5607
22ca247e 5608 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5609
22ca247e 5610 if (map->tu_count != 0)
927aa2e7
JK
5611 {
5612 /* We can only handle a single .debug_types when we have an
5613 index. */
fd5866f6 5614 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5615 return false;
5616
fd5866f6 5617 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5618
5619 create_signatured_type_table_from_debug_names
22ca247e 5620 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5621 }
5622
ed2dc618
SM
5623 create_addrmap_from_aranges (dwarf2_per_objfile,
5624 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5625
22ca247e 5626 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5627 dwarf2_per_objfile->using_index = 1;
5628 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5629 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5630
5631 return true;
5632}
5633
927aa2e7
JK
5634/* Type used to manage iterating over all CUs looking for a symbol for
5635 .debug_names. */
5636
5637class dw2_debug_names_iterator
5638{
5639public:
927aa2e7 5640 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5641 gdb::optional<block_enum> block_index,
5642 domain_enum domain,
927aa2e7 5643 const char *name)
2b79f376 5644 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5645 m_addr (find_vec_in_debug_names (map, name))
5646 {}
5647
5648 dw2_debug_names_iterator (const mapped_debug_names &map,
5649 search_domain search, uint32_t namei)
5650 : m_map (map),
5651 m_search (search),
5652 m_addr (find_vec_in_debug_names (map, namei))
5653 {}
5654
3b00ef10
TT
5655 dw2_debug_names_iterator (const mapped_debug_names &map,
5656 block_enum block_index, domain_enum domain,
5657 uint32_t namei)
5658 : m_map (map), m_block_index (block_index), m_domain (domain),
5659 m_addr (find_vec_in_debug_names (map, namei))
5660 {}
5661
927aa2e7
JK
5662 /* Return the next matching CU or NULL if there are no more. */
5663 dwarf2_per_cu_data *next ();
5664
5665private:
5666 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5667 const char *name);
5668 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5669 uint32_t namei);
5670
5671 /* The internalized form of .debug_names. */
5672 const mapped_debug_names &m_map;
5673
2b79f376
SM
5674 /* If set, only look for symbols that match that block. Valid values are
5675 GLOBAL_BLOCK and STATIC_BLOCK. */
5676 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5677
5678 /* The kind of symbol we're looking for. */
5679 const domain_enum m_domain = UNDEF_DOMAIN;
5680 const search_domain m_search = ALL_DOMAIN;
5681
5682 /* The list of CUs from the index entry of the symbol, or NULL if
5683 not found. */
5684 const gdb_byte *m_addr;
5685};
5686
5687const char *
5688mapped_debug_names::namei_to_name (uint32_t namei) const
5689{
5690 const ULONGEST namei_string_offs
5691 = extract_unsigned_integer ((name_table_string_offs_reordered
5692 + namei * offset_size),
5693 offset_size,
5694 dwarf5_byte_order);
5695 return read_indirect_string_at_offset
ed2dc618 5696 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5697}
5698
5699/* Find a slot in .debug_names for the object named NAME. If NAME is
5700 found, return pointer to its pool data. If NAME cannot be found,
5701 return NULL. */
5702
5703const gdb_byte *
5704dw2_debug_names_iterator::find_vec_in_debug_names
5705 (const mapped_debug_names &map, const char *name)
5706{
5707 int (*cmp) (const char *, const char *);
5708
54ee4252 5709 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5710 if (current_language->la_language == language_cplus
5711 || current_language->la_language == language_fortran
5712 || current_language->la_language == language_d)
5713 {
5714 /* NAME is already canonical. Drop any qualifiers as
5715 .debug_names does not contain any. */
5716
5717 if (strchr (name, '(') != NULL)
5718 {
54ee4252 5719 without_params = cp_remove_params (name);
927aa2e7 5720 if (without_params != NULL)
54ee4252 5721 name = without_params.get ();
927aa2e7
JK
5722 }
5723 }
5724
5725 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5726
5727 const uint32_t full_hash = dwarf5_djb_hash (name);
5728 uint32_t namei
5729 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5730 (map.bucket_table_reordered
5731 + (full_hash % map.bucket_count)), 4,
5732 map.dwarf5_byte_order);
5733 if (namei == 0)
5734 return NULL;
5735 --namei;
5736 if (namei >= map.name_count)
5737 {
b98664d3 5738 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5739 "[in module %s]"),
5740 namei, map.name_count,
ed2dc618 5741 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5742 return NULL;
5743 }
5744
5745 for (;;)
5746 {
5747 const uint32_t namei_full_hash
5748 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5749 (map.hash_table_reordered + namei), 4,
5750 map.dwarf5_byte_order);
5751 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5752 return NULL;
5753
5754 if (full_hash == namei_full_hash)
5755 {
5756 const char *const namei_string = map.namei_to_name (namei);
5757
5758#if 0 /* An expensive sanity check. */
5759 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5760 {
b98664d3 5761 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5762 "[in module %s]"),
5763 namei, objfile_name (dwarf2_per_objfile->objfile));
5764 return NULL;
5765 }
5766#endif
5767
5768 if (cmp (namei_string, name) == 0)
5769 {
5770 const ULONGEST namei_entry_offs
5771 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5772 + namei * map.offset_size),
5773 map.offset_size, map.dwarf5_byte_order);
5774 return map.entry_pool + namei_entry_offs;
5775 }
5776 }
5777
5778 ++namei;
5779 if (namei >= map.name_count)
5780 return NULL;
5781 }
5782}
5783
5784const gdb_byte *
5785dw2_debug_names_iterator::find_vec_in_debug_names
5786 (const mapped_debug_names &map, uint32_t namei)
5787{
5788 if (namei >= map.name_count)
5789 {
b98664d3 5790 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5791 "[in module %s]"),
5792 namei, map.name_count,
ed2dc618 5793 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5794 return NULL;
5795 }
5796
5797 const ULONGEST namei_entry_offs
5798 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5799 + namei * map.offset_size),
5800 map.offset_size, map.dwarf5_byte_order);
5801 return map.entry_pool + namei_entry_offs;
5802}
5803
5804/* See dw2_debug_names_iterator. */
5805
5806dwarf2_per_cu_data *
5807dw2_debug_names_iterator::next ()
5808{
5809 if (m_addr == NULL)
5810 return NULL;
5811
ed2dc618
SM
5812 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5813 struct objfile *objfile = dwarf2_per_objfile->objfile;
5814 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5815
5816 again:
5817
5818 unsigned int bytes_read;
5819 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5820 m_addr += bytes_read;
5821 if (abbrev == 0)
5822 return NULL;
5823
5824 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5825 if (indexval_it == m_map.abbrev_map.cend ())
5826 {
b98664d3 5827 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5828 "[in module %s]"),
ed2dc618 5829 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5830 return NULL;
5831 }
5832 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5833 enum class symbol_linkage {
5834 unknown,
5835 static_,
5836 extern_,
23c13d42 5837 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5838 dwarf2_per_cu_data *per_cu = NULL;
5839 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5840 {
5841 ULONGEST ull;
5842 switch (attr.form)
5843 {
5844 case DW_FORM_implicit_const:
5845 ull = attr.implicit_const;
5846 break;
5847 case DW_FORM_flag_present:
5848 ull = 1;
5849 break;
5850 case DW_FORM_udata:
5851 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5852 m_addr += bytes_read;
5853 break;
5854 default:
b98664d3 5855 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5856 dwarf_form_name (attr.form),
ed2dc618 5857 objfile_name (objfile));
927aa2e7
JK
5858 return NULL;
5859 }
5860 switch (attr.dw_idx)
5861 {
5862 case DW_IDX_compile_unit:
5863 /* Don't crash on bad data. */
b76e467d 5864 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5865 {
b98664d3 5866 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5867 " [in module %s]"),
5868 pulongest (ull),
5869 objfile_name (dwarf2_per_objfile->objfile));
5870 continue;
5871 }
ff4c9fec 5872 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5873 break;
8af5c486
JK
5874 case DW_IDX_type_unit:
5875 /* Don't crash on bad data. */
b2bdb8cf 5876 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5877 {
b98664d3 5878 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5879 " [in module %s]"),
5880 pulongest (ull),
5881 objfile_name (dwarf2_per_objfile->objfile));
5882 continue;
5883 }
ff4c9fec 5884 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5885 break;
927aa2e7
JK
5886 case DW_IDX_GNU_internal:
5887 if (!m_map.augmentation_is_gdb)
5888 break;
23c13d42 5889 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5890 break;
5891 case DW_IDX_GNU_external:
5892 if (!m_map.augmentation_is_gdb)
5893 break;
23c13d42 5894 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5895 break;
5896 }
5897 }
5898
5899 /* Skip if already read in. */
5900 if (per_cu->v.quick->compunit_symtab)
5901 goto again;
5902
5903 /* Check static vs global. */
23c13d42 5904 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5905 {
2b79f376 5906 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5907 const bool symbol_is_static =
5908 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5909 if (want_static != symbol_is_static)
2b79f376 5910 goto again;
927aa2e7
JK
5911 }
5912
5913 /* Match dw2_symtab_iter_next, symbol_kind
5914 and debug_names::psymbol_tag. */
5915 switch (m_domain)
5916 {
5917 case VAR_DOMAIN:
5918 switch (indexval.dwarf_tag)
5919 {
5920 case DW_TAG_variable:
5921 case DW_TAG_subprogram:
5922 /* Some types are also in VAR_DOMAIN. */
5923 case DW_TAG_typedef:
5924 case DW_TAG_structure_type:
5925 break;
5926 default:
5927 goto again;
5928 }
5929 break;
5930 case STRUCT_DOMAIN:
5931 switch (indexval.dwarf_tag)
5932 {
5933 case DW_TAG_typedef:
5934 case DW_TAG_structure_type:
5935 break;
5936 default:
5937 goto again;
5938 }
5939 break;
5940 case LABEL_DOMAIN:
5941 switch (indexval.dwarf_tag)
5942 {
5943 case 0:
5944 case DW_TAG_variable:
5945 break;
5946 default:
5947 goto again;
5948 }
5949 break;
5950 default:
5951 break;
5952 }
5953
5954 /* Match dw2_expand_symtabs_matching, symbol_kind and
5955 debug_names::psymbol_tag. */
5956 switch (m_search)
4b514bc8 5957 {
927aa2e7
JK
5958 case VARIABLES_DOMAIN:
5959 switch (indexval.dwarf_tag)
4b514bc8 5960 {
927aa2e7
JK
5961 case DW_TAG_variable:
5962 break;
5963 default:
5964 goto again;
4b514bc8 5965 }
927aa2e7
JK
5966 break;
5967 case FUNCTIONS_DOMAIN:
5968 switch (indexval.dwarf_tag)
4b514bc8 5969 {
927aa2e7
JK
5970 case DW_TAG_subprogram:
5971 break;
5972 default:
5973 goto again;
4b514bc8 5974 }
927aa2e7
JK
5975 break;
5976 case TYPES_DOMAIN:
5977 switch (indexval.dwarf_tag)
5978 {
5979 case DW_TAG_typedef:
5980 case DW_TAG_structure_type:
5981 break;
5982 default:
5983 goto again;
5984 }
5985 break;
5986 default:
5987 break;
4b514bc8 5988 }
927aa2e7
JK
5989
5990 return per_cu;
4b514bc8 5991}
61920122 5992
927aa2e7 5993static struct compunit_symtab *
c7f839cb 5994dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5995 const char *name, domain_enum domain)
4b514bc8 5996{
ed2dc618
SM
5997 struct dwarf2_per_objfile *dwarf2_per_objfile
5998 = get_dwarf2_per_objfile (objfile);
61920122 5999
927aa2e7
JK
6000 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6001 if (!mapp)
61920122 6002 {
927aa2e7
JK
6003 /* index is NULL if OBJF_READNOW. */
6004 return NULL;
6005 }
6006 const auto &map = *mapp;
9291a0cd 6007
2b79f376 6008 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6009
927aa2e7
JK
6010 struct compunit_symtab *stab_best = NULL;
6011 struct dwarf2_per_cu_data *per_cu;
6012 while ((per_cu = iter.next ()) != NULL)
6013 {
6014 struct symbol *sym, *with_opaque = NULL;
58f0c718 6015 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6016 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6017 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6018
927aa2e7
JK
6019 sym = block_find_symbol (block, name, domain,
6020 block_find_non_opaque_type_preferred,
6021 &with_opaque);
9703b513 6022
927aa2e7
JK
6023 /* Some caution must be observed with overloaded functions and
6024 methods, since the index will not contain any overload
6025 information (but NAME might contain it). */
a3ec0bb1 6026
927aa2e7
JK
6027 if (sym != NULL
6028 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6029 return stab;
6030 if (with_opaque != NULL
6031 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6032 stab_best = stab;
9703b513 6033
927aa2e7 6034 /* Keep looking through other CUs. */
9703b513
TT
6035 }
6036
927aa2e7 6037 return stab_best;
9703b513
TT
6038}
6039
927aa2e7
JK
6040/* This dumps minimal information about .debug_names. It is called
6041 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6042 uses this to verify that .debug_names has been loaded. */
9291a0cd 6043
927aa2e7
JK
6044static void
6045dw2_debug_names_dump (struct objfile *objfile)
6046{
ed2dc618
SM
6047 struct dwarf2_per_objfile *dwarf2_per_objfile
6048 = get_dwarf2_per_objfile (objfile);
6049
927aa2e7
JK
6050 gdb_assert (dwarf2_per_objfile->using_index);
6051 printf_filtered (".debug_names:");
6052 if (dwarf2_per_objfile->debug_names_table)
6053 printf_filtered (" exists\n");
6054 else
6055 printf_filtered (" faked for \"readnow\"\n");
6056 printf_filtered ("\n");
9291a0cd
TT
6057}
6058
9291a0cd 6059static void
927aa2e7
JK
6060dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6061 const char *func_name)
9291a0cd 6062{
ed2dc618
SM
6063 struct dwarf2_per_objfile *dwarf2_per_objfile
6064 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6065
927aa2e7
JK
6066 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6067 if (dwarf2_per_objfile->debug_names_table)
24c79950 6068 {
927aa2e7 6069 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6070
2b79f376 6071 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6072
927aa2e7
JK
6073 struct dwarf2_per_cu_data *per_cu;
6074 while ((per_cu = iter.next ()) != NULL)
58f0c718 6075 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6076 }
6077}
24c79950 6078
3b00ef10
TT
6079static void
6080dw2_debug_names_map_matching_symbols
6081 (struct objfile *objfile,
6082 const lookup_name_info &name, domain_enum domain,
6083 int global,
6084 gdb::function_view<symbol_found_callback_ftype> callback,
6085 symbol_compare_ftype *ordered_compare)
6086{
6087 struct dwarf2_per_objfile *dwarf2_per_objfile
6088 = get_dwarf2_per_objfile (objfile);
6089
6090 /* debug_names_table is NULL if OBJF_READNOW. */
6091 if (!dwarf2_per_objfile->debug_names_table)
6092 return;
6093
6094 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6095 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6096
6097 const char *match_name = name.ada ().lookup_name ().c_str ();
6098 auto matcher = [&] (const char *symname)
6099 {
6100 if (ordered_compare == nullptr)
6101 return true;
6102 return ordered_compare (symname, match_name) == 0;
6103 };
6104
6105 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6106 [&] (offset_type namei)
6107 {
6108 /* The name was matched, now expand corresponding CUs that were
6109 marked. */
6110 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6111
6112 struct dwarf2_per_cu_data *per_cu;
6113 while ((per_cu = iter.next ()) != NULL)
6114 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6115 return true;
6116 });
6117
6118 /* It's a shame we couldn't do this inside the
6119 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6120 that have already been expanded. Instead, this loop matches what
6121 the psymtab code does. */
6122 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6123 {
6124 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6125 if (cust != nullptr)
6126 {
6127 const struct block *block
6128 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6129 if (!iterate_over_symbols_terminated (block, name,
6130 domain, callback))
6131 break;
6132 }
6133 }
6134}
6135
927aa2e7
JK
6136static void
6137dw2_debug_names_expand_symtabs_matching
6138 (struct objfile *objfile,
6139 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6140 const lookup_name_info &lookup_name,
6141 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6142 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6143 enum search_domain kind)
6144{
ed2dc618
SM
6145 struct dwarf2_per_objfile *dwarf2_per_objfile
6146 = get_dwarf2_per_objfile (objfile);
9291a0cd 6147
927aa2e7
JK
6148 /* debug_names_table is NULL if OBJF_READNOW. */
6149 if (!dwarf2_per_objfile->debug_names_table)
6150 return;
9291a0cd 6151
ed2dc618 6152 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6153
44ed8f3e 6154 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6155
44ed8f3e
PA
6156 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6157 symbol_matcher,
6158 kind, [&] (offset_type namei)
927aa2e7 6159 {
927aa2e7
JK
6160 /* The name was matched, now expand corresponding CUs that were
6161 marked. */
6162 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6163
927aa2e7
JK
6164 struct dwarf2_per_cu_data *per_cu;
6165 while ((per_cu = iter.next ()) != NULL)
6166 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6167 expansion_notify);
3b00ef10 6168 return true;
44ed8f3e 6169 });
9291a0cd
TT
6170}
6171
927aa2e7 6172const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6173{
6174 dw2_has_symbols,
6175 dw2_find_last_source_symtab,
6176 dw2_forget_cached_source_info,
f8eba3c6 6177 dw2_map_symtabs_matching_filename,
927aa2e7 6178 dw2_debug_names_lookup_symbol,
9291a0cd 6179 dw2_print_stats,
927aa2e7 6180 dw2_debug_names_dump,
927aa2e7 6181 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6182 dw2_expand_all_symtabs,
652a8996 6183 dw2_expand_symtabs_with_fullname,
3b00ef10 6184 dw2_debug_names_map_matching_symbols,
927aa2e7 6185 dw2_debug_names_expand_symtabs_matching,
43f3e411 6186 dw2_find_pc_sect_compunit_symtab,
71a3c369 6187 NULL,
9291a0cd
TT
6188 dw2_map_symbol_filenames
6189};
6190
4485a1c1
SM
6191/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6192 to either a dwarf2_per_objfile or dwz_file object. */
6193
6194template <typename T>
6195static gdb::array_view<const gdb_byte>
6196get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6197{
6198 dwarf2_section_info *section = &section_owner->gdb_index;
6199
6200 if (dwarf2_section_empty_p (section))
6201 return {};
6202
6203 /* Older elfutils strip versions could keep the section in the main
6204 executable while splitting it for the separate debug info file. */
6205 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6206 return {};
6207
6208 dwarf2_read_section (obj, section);
6209
8bebfcda
PA
6210 /* dwarf2_section_info::size is a bfd_size_type, while
6211 gdb::array_view works with size_t. On 32-bit hosts, with
6212 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6213 is 32-bit. So we need an explicit narrowing conversion here.
6214 This is fine, because it's impossible to allocate or mmap an
6215 array/buffer larger than what size_t can represent. */
6216 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6217}
6218
87d6a7aa
SM
6219/* Lookup the index cache for the contents of the index associated to
6220 DWARF2_OBJ. */
6221
6222static gdb::array_view<const gdb_byte>
6223get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6224{
6225 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6226 if (build_id == nullptr)
6227 return {};
6228
6229 return global_index_cache.lookup_gdb_index (build_id,
6230 &dwarf2_obj->index_cache_res);
6231}
6232
6233/* Same as the above, but for DWZ. */
6234
6235static gdb::array_view<const gdb_byte>
6236get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6237{
6238 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6239 if (build_id == nullptr)
6240 return {};
6241
6242 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6243}
6244
3c0aa29a 6245/* See symfile.h. */
9291a0cd 6246
3c0aa29a
PA
6247bool
6248dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6249{
ed2dc618
SM
6250 struct dwarf2_per_objfile *dwarf2_per_objfile
6251 = get_dwarf2_per_objfile (objfile);
6252
9291a0cd
TT
6253 /* If we're about to read full symbols, don't bother with the
6254 indices. In this case we also don't care if some other debug
6255 format is making psymtabs, because they are all about to be
6256 expanded anyway. */
6257 if ((objfile->flags & OBJF_READNOW))
6258 {
9291a0cd 6259 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6260 create_all_comp_units (dwarf2_per_objfile);
6261 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6262 dwarf2_per_objfile->quick_file_names_table
6263 = create_quick_file_names_table
6264 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6265
b76e467d 6266 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6267 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6268 {
ff4c9fec 6269 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6270
e254ef6a
DE
6271 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6272 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6273 }
6274
6275 /* Return 1 so that gdb sees the "quick" functions. However,
6276 these functions will be no-ops because we will have expanded
6277 all symtabs. */
3c0aa29a
PA
6278 *index_kind = dw_index_kind::GDB_INDEX;
6279 return true;
9291a0cd
TT
6280 }
6281
ed2dc618 6282 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6283 {
6284 *index_kind = dw_index_kind::DEBUG_NAMES;
6285 return true;
6286 }
927aa2e7 6287
4485a1c1
SM
6288 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6289 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6290 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6291 {
6292 *index_kind = dw_index_kind::GDB_INDEX;
6293 return true;
6294 }
9291a0cd 6295
87d6a7aa
SM
6296 /* ... otherwise, try to find the index in the index cache. */
6297 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6298 get_gdb_index_contents_from_cache,
6299 get_gdb_index_contents_from_cache_dwz))
6300 {
6301 global_index_cache.hit ();
6302 *index_kind = dw_index_kind::GDB_INDEX;
6303 return true;
6304 }
6305
6306 global_index_cache.miss ();
3c0aa29a 6307 return false;
9291a0cd
TT
6308}
6309
6310\f
6311
dce234bc
PP
6312/* Build a partial symbol table. */
6313
6314void
f29dff0a 6315dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6316{
ed2dc618
SM
6317 struct dwarf2_per_objfile *dwarf2_per_objfile
6318 = get_dwarf2_per_objfile (objfile);
c9bf0622 6319
6eee24ce 6320 init_psymbol_list (objfile, 1024);
c906108c 6321
a70b8144 6322 try
c9bf0622
TT
6323 {
6324 /* This isn't really ideal: all the data we allocate on the
6325 objfile's obstack is still uselessly kept around. However,
6326 freeing it seems unsafe. */
906768f9 6327 psymtab_discarder psymtabs (objfile);
ed2dc618 6328 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6329 psymtabs.keep ();
87d6a7aa
SM
6330
6331 /* (maybe) store an index in the cache. */
6332 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6333 }
230d2906 6334 catch (const gdb_exception_error &except)
492d29ea
PA
6335 {
6336 exception_print (gdb_stderr, except);
6337 }
c906108c 6338}
c906108c 6339
1ce1cefd
DE
6340/* Return the total length of the CU described by HEADER. */
6341
6342static unsigned int
6343get_cu_length (const struct comp_unit_head *header)
6344{
6345 return header->initial_length_size + header->length;
6346}
6347
9c541725 6348/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6349
9c541725
PA
6350static inline bool
6351offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6352{
9c541725
PA
6353 sect_offset bottom = cu_header->sect_off;
6354 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6355
9c541725 6356 return sect_off >= bottom && sect_off < top;
45452591
DE
6357}
6358
3b80fe9b
DE
6359/* Find the base address of the compilation unit for range lists and
6360 location lists. It will normally be specified by DW_AT_low_pc.
6361 In DWARF-3 draft 4, the base address could be overridden by
6362 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6363 compilation units with discontinuous ranges. */
6364
6365static void
6366dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6367{
6368 struct attribute *attr;
6369
6370 cu->base_known = 0;
6371 cu->base_address = 0;
6372
6373 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6374 if (attr)
6375 {
31aa7e4e 6376 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6377 cu->base_known = 1;
6378 }
6379 else
6380 {
6381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6382 if (attr)
6383 {
31aa7e4e 6384 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6385 cu->base_known = 1;
6386 }
6387 }
6388}
6389
93311388 6390/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6391 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6392 NOTE: This leaves members offset, first_die_offset to be filled in
6393 by the caller. */
107d2387 6394
d521ce57 6395static const gdb_byte *
107d2387 6396read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6397 const gdb_byte *info_ptr,
6398 struct dwarf2_section_info *section,
6399 rcuh_kind section_kind)
107d2387
AC
6400{
6401 int signed_addr;
891d2f0b 6402 unsigned int bytes_read;
43988095
JK
6403 const char *filename = get_section_file_name (section);
6404 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6405
6406 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6407 cu_header->initial_length_size = bytes_read;
6408 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6409 info_ptr += bytes_read;
107d2387 6410 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6411 if (cu_header->version < 2 || cu_header->version > 5)
6412 error (_("Dwarf Error: wrong version in compilation unit header "
6413 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6414 cu_header->version, filename);
107d2387 6415 info_ptr += 2;
43988095
JK
6416 if (cu_header->version < 5)
6417 switch (section_kind)
6418 {
6419 case rcuh_kind::COMPILE:
6420 cu_header->unit_type = DW_UT_compile;
6421 break;
6422 case rcuh_kind::TYPE:
6423 cu_header->unit_type = DW_UT_type;
6424 break;
6425 default:
6426 internal_error (__FILE__, __LINE__,
6427 _("read_comp_unit_head: invalid section_kind"));
6428 }
6429 else
6430 {
6431 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6432 (read_1_byte (abfd, info_ptr));
6433 info_ptr += 1;
6434 switch (cu_header->unit_type)
6435 {
6436 case DW_UT_compile:
a084a2a6
AT
6437 case DW_UT_partial:
6438 case DW_UT_skeleton:
6439 case DW_UT_split_compile:
43988095
JK
6440 if (section_kind != rcuh_kind::COMPILE)
6441 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6442 "(is %s, should be %s) [in module %s]"),
6443 dwarf_unit_type_name (cu_header->unit_type),
6444 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6445 break;
6446 case DW_UT_type:
a084a2a6 6447 case DW_UT_split_type:
43988095
JK
6448 section_kind = rcuh_kind::TYPE;
6449 break;
6450 default:
6451 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6452 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6453 "[in module %s]"), cu_header->unit_type,
6454 dwarf_unit_type_name (DW_UT_compile),
6455 dwarf_unit_type_name (DW_UT_skeleton),
6456 dwarf_unit_type_name (DW_UT_split_compile),
6457 dwarf_unit_type_name (DW_UT_type),
6458 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6459 }
6460
6461 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6462 info_ptr += 1;
6463 }
9c541725
PA
6464 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6465 cu_header,
6466 &bytes_read);
613e1657 6467 info_ptr += bytes_read;
43988095
JK
6468 if (cu_header->version < 5)
6469 {
6470 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6471 info_ptr += 1;
6472 }
107d2387
AC
6473 signed_addr = bfd_get_sign_extend_vma (abfd);
6474 if (signed_addr < 0)
8e65ff28 6475 internal_error (__FILE__, __LINE__,
e2e0b3e5 6476 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6477 cu_header->signed_addr_p = signed_addr;
c764a876 6478
a084a2a6
AT
6479 bool header_has_signature = section_kind == rcuh_kind::TYPE
6480 || cu_header->unit_type == DW_UT_skeleton
6481 || cu_header->unit_type == DW_UT_split_compile;
43988095 6482
a084a2a6
AT
6483 if (header_has_signature)
6484 {
43988095
JK
6485 cu_header->signature = read_8_bytes (abfd, info_ptr);
6486 info_ptr += 8;
a084a2a6 6487 }
43988095 6488
a084a2a6
AT
6489 if (section_kind == rcuh_kind::TYPE)
6490 {
6491 LONGEST type_offset;
43988095
JK
6492 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6493 info_ptr += bytes_read;
9c541725
PA
6494 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6495 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6496 error (_("Dwarf Error: Too big type_offset in compilation unit "
6497 "header (is %s) [in module %s]"), plongest (type_offset),
6498 filename);
6499 }
6500
107d2387
AC
6501 return info_ptr;
6502}
6503
36586728
TT
6504/* Helper function that returns the proper abbrev section for
6505 THIS_CU. */
6506
6507static struct dwarf2_section_info *
6508get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6509{
6510 struct dwarf2_section_info *abbrev;
ed2dc618 6511 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6512
6513 if (this_cu->is_dwz)
ed2dc618 6514 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6515 else
6516 abbrev = &dwarf2_per_objfile->abbrev;
6517
6518 return abbrev;
6519}
6520
9ff913ba
DE
6521/* Subroutine of read_and_check_comp_unit_head and
6522 read_and_check_type_unit_head to simplify them.
6523 Perform various error checking on the header. */
6524
6525static void
ed2dc618
SM
6526error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6527 struct comp_unit_head *header,
4bdcc0c1
DE
6528 struct dwarf2_section_info *section,
6529 struct dwarf2_section_info *abbrev_section)
9ff913ba 6530{
a32a8923 6531 const char *filename = get_section_file_name (section);
9ff913ba 6532
9c541725 6533 if (to_underlying (header->abbrev_sect_off)
36586728 6534 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6535 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6536 "(offset %s + 6) [in module %s]"),
6537 sect_offset_str (header->abbrev_sect_off),
6538 sect_offset_str (header->sect_off),
9ff913ba
DE
6539 filename);
6540
9c541725 6541 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6542 avoid potential 32-bit overflow. */
9c541725 6543 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6544 > section->size)
9c541725 6545 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6546 "(offset %s + 0) [in module %s]"),
6547 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6548 filename);
6549}
6550
6551/* Read in a CU/TU header and perform some basic error checking.
6552 The contents of the header are stored in HEADER.
6553 The result is a pointer to the start of the first DIE. */
adabb602 6554
d521ce57 6555static const gdb_byte *
ed2dc618
SM
6556read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6557 struct comp_unit_head *header,
9ff913ba 6558 struct dwarf2_section_info *section,
4bdcc0c1 6559 struct dwarf2_section_info *abbrev_section,
d521ce57 6560 const gdb_byte *info_ptr,
43988095 6561 rcuh_kind section_kind)
72bf9492 6562{
d521ce57 6563 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6564
9c541725 6565 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6566
43988095 6567 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6568
9c541725 6569 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6570
ed2dc618
SM
6571 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6572 abbrev_section);
9ff913ba
DE
6573
6574 return info_ptr;
348e048f
DE
6575}
6576
f4dc4d17
DE
6577/* Fetch the abbreviation table offset from a comp or type unit header. */
6578
6579static sect_offset
ed2dc618
SM
6580read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6581 struct dwarf2_section_info *section,
9c541725 6582 sect_offset sect_off)
f4dc4d17 6583{
a32a8923 6584 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6585 const gdb_byte *info_ptr;
ac298888 6586 unsigned int initial_length_size, offset_size;
43988095 6587 uint16_t version;
f4dc4d17
DE
6588
6589 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6590 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6591 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6592 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6593 info_ptr += initial_length_size;
6594
6595 version = read_2_bytes (abfd, info_ptr);
6596 info_ptr += 2;
6597 if (version >= 5)
6598 {
6599 /* Skip unit type and address size. */
6600 info_ptr += 2;
6601 }
6602
9c541725 6603 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6604}
6605
aaa75496
JB
6606/* Allocate a new partial symtab for file named NAME and mark this new
6607 partial symtab as being an include of PST. */
6608
6609static void
d521ce57 6610dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6611 struct objfile *objfile)
6612{
6613 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6614
fbd9ab74
JK
6615 if (!IS_ABSOLUTE_PATH (subpst->filename))
6616 {
6617 /* It shares objfile->objfile_obstack. */
6618 subpst->dirname = pst->dirname;
6619 }
6620
a9342b62 6621 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6622 subpst->dependencies[0] = pst;
6623 subpst->number_of_dependencies = 1;
6624
aaa75496 6625 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6626
6627 /* No private part is necessary for include psymtabs. This property
6628 can be used to differentiate between such include psymtabs and
10b3939b 6629 the regular ones. */
58a9656e 6630 subpst->read_symtab_private = NULL;
aaa75496
JB
6631}
6632
6633/* Read the Line Number Program data and extract the list of files
6634 included by the source file represented by PST. Build an include
d85a05f0 6635 partial symtab for each of these included files. */
aaa75496
JB
6636
6637static void
6638dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6639 struct die_info *die,
6640 struct partial_symtab *pst)
aaa75496 6641{
fff8551c 6642 line_header_up lh;
d85a05f0 6643 struct attribute *attr;
aaa75496 6644
d85a05f0
DJ
6645 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6646 if (attr)
9c541725 6647 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6648 if (lh == NULL)
6649 return; /* No linetable, so no includes. */
6650
79748972
TT
6651 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6652 that we pass in the raw text_low here; that is ok because we're
6653 only decoding the line table to make include partial symtabs, and
6654 so the addresses aren't really used. */
4ae976d1 6655 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6656 pst->raw_text_low (), 1);
aaa75496
JB
6657}
6658
348e048f 6659static hashval_t
52dc124a 6660hash_signatured_type (const void *item)
348e048f 6661{
9a3c8263
SM
6662 const struct signatured_type *sig_type
6663 = (const struct signatured_type *) item;
9a619af0 6664
348e048f 6665 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6666 return sig_type->signature;
348e048f
DE
6667}
6668
6669static int
52dc124a 6670eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6671{
9a3c8263
SM
6672 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6673 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6674
348e048f
DE
6675 return lhs->signature == rhs->signature;
6676}
6677
1fd400ff
TT
6678/* Allocate a hash table for signatured types. */
6679
6680static htab_t
673bfd45 6681allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6682{
6683 return htab_create_alloc_ex (41,
52dc124a
DE
6684 hash_signatured_type,
6685 eq_signatured_type,
1fd400ff
TT
6686 NULL,
6687 &objfile->objfile_obstack,
6688 hashtab_obstack_allocate,
6689 dummy_obstack_deallocate);
6690}
6691
d467dd73 6692/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6693
6694static int
d467dd73 6695add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6696{
9a3c8263 6697 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6698 std::vector<signatured_type *> *all_type_units
6699 = (std::vector<signatured_type *> *) datum;
1fd400ff 6700
b2bdb8cf 6701 all_type_units->push_back (sigt);
1fd400ff
TT
6702
6703 return 1;
6704}
6705
78d4d2c5 6706/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6707 and fill them into TYPES_HTAB. It will process only type units,
6708 therefore DW_UT_type. */
c88ee1f0 6709
78d4d2c5 6710static void
ed2dc618
SM
6711create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6712 struct dwo_file *dwo_file,
43988095
JK
6713 dwarf2_section_info *section, htab_t &types_htab,
6714 rcuh_kind section_kind)
348e048f 6715{
3019eac3 6716 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6717 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6718 bfd *abfd;
6719 const gdb_byte *info_ptr, *end_ptr;
348e048f 6720
4bdcc0c1
DE
6721 abbrev_section = (dwo_file != NULL
6722 ? &dwo_file->sections.abbrev
6723 : &dwarf2_per_objfile->abbrev);
6724
b4f54984 6725 if (dwarf_read_debug)
43988095
JK
6726 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6727 get_section_name (section),
a32a8923 6728 get_section_file_name (abbrev_section));
09406207 6729
78d4d2c5
JK
6730 dwarf2_read_section (objfile, section);
6731 info_ptr = section->buffer;
348e048f 6732
78d4d2c5
JK
6733 if (info_ptr == NULL)
6734 return;
348e048f 6735
78d4d2c5
JK
6736 /* We can't set abfd until now because the section may be empty or
6737 not present, in which case the bfd is unknown. */
6738 abfd = get_section_bfd_owner (section);
348e048f 6739
78d4d2c5
JK
6740 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6741 because we don't need to read any dies: the signature is in the
6742 header. */
3019eac3 6743
78d4d2c5
JK
6744 end_ptr = info_ptr + section->size;
6745 while (info_ptr < end_ptr)
6746 {
78d4d2c5
JK
6747 struct signatured_type *sig_type;
6748 struct dwo_unit *dwo_tu;
6749 void **slot;
6750 const gdb_byte *ptr = info_ptr;
6751 struct comp_unit_head header;
6752 unsigned int length;
8b70b953 6753
9c541725 6754 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6755
a49dd8dd
JK
6756 /* Initialize it due to a false compiler warning. */
6757 header.signature = -1;
9c541725 6758 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6759
78d4d2c5
JK
6760 /* We need to read the type's signature in order to build the hash
6761 table, but we don't need anything else just yet. */
348e048f 6762
ed2dc618 6763 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6764 abbrev_section, ptr, section_kind);
348e048f 6765
78d4d2c5 6766 length = get_cu_length (&header);
6caca83c 6767
78d4d2c5
JK
6768 /* Skip dummy type units. */
6769 if (ptr >= info_ptr + length
43988095
JK
6770 || peek_abbrev_code (abfd, ptr) == 0
6771 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6772 {
6773 info_ptr += length;
6774 continue;
6775 }
dee91e82 6776
78d4d2c5
JK
6777 if (types_htab == NULL)
6778 {
6779 if (dwo_file)
6780 types_htab = allocate_dwo_unit_table (objfile);
6781 else
6782 types_htab = allocate_signatured_type_table (objfile);
6783 }
8b70b953 6784
78d4d2c5
JK
6785 if (dwo_file)
6786 {
6787 sig_type = NULL;
6788 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6789 struct dwo_unit);
6790 dwo_tu->dwo_file = dwo_file;
43988095 6791 dwo_tu->signature = header.signature;
9c541725 6792 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6793 dwo_tu->section = section;
9c541725 6794 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6795 dwo_tu->length = length;
6796 }
6797 else
6798 {
6799 /* N.B.: type_offset is not usable if this type uses a DWO file.
6800 The real type_offset is in the DWO file. */
6801 dwo_tu = NULL;
6802 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6803 struct signatured_type);
43988095 6804 sig_type->signature = header.signature;
9c541725 6805 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6806 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6807 sig_type->per_cu.is_debug_types = 1;
6808 sig_type->per_cu.section = section;
9c541725 6809 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6810 sig_type->per_cu.length = length;
6811 }
6812
6813 slot = htab_find_slot (types_htab,
6814 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6815 INSERT);
6816 gdb_assert (slot != NULL);
6817 if (*slot != NULL)
6818 {
9c541725 6819 sect_offset dup_sect_off;
0349ea22 6820
3019eac3
DE
6821 if (dwo_file)
6822 {
78d4d2c5
JK
6823 const struct dwo_unit *dup_tu
6824 = (const struct dwo_unit *) *slot;
6825
9c541725 6826 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6827 }
6828 else
6829 {
78d4d2c5
JK
6830 const struct signatured_type *dup_tu
6831 = (const struct signatured_type *) *slot;
6832
9c541725 6833 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6834 }
8b70b953 6835
b98664d3 6836 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6837 " the entry at offset %s, signature %s"),
6838 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6839 hex_string (header.signature));
78d4d2c5
JK
6840 }
6841 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6842
78d4d2c5 6843 if (dwarf_read_debug > 1)
9d8780f0
SM
6844 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6845 sect_offset_str (sect_off),
43988095 6846 hex_string (header.signature));
3019eac3 6847
78d4d2c5
JK
6848 info_ptr += length;
6849 }
6850}
3019eac3 6851
78d4d2c5
JK
6852/* Create the hash table of all entries in the .debug_types
6853 (or .debug_types.dwo) section(s).
6854 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6855 otherwise it is NULL.
b3c8eb43 6856
78d4d2c5 6857 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6858
78d4d2c5 6859 Note: This function processes DWO files only, not DWP files. */
348e048f 6860
78d4d2c5 6861static void
ed2dc618
SM
6862create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6863 struct dwo_file *dwo_file,
fd5866f6 6864 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6865 htab_t &types_htab)
6866{
fd5866f6
SM
6867 for (dwarf2_section_info &section : type_sections)
6868 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6869 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6870}
6871
6872/* Create the hash table of all entries in the .debug_types section,
6873 and initialize all_type_units.
6874 The result is zero if there is an error (e.g. missing .debug_types section),
6875 otherwise non-zero. */
6876
6877static int
ed2dc618 6878create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6879{
78d4d2c5 6880 htab_t types_htab = NULL;
3019eac3 6881
ed2dc618
SM
6882 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6883 &dwarf2_per_objfile->info, types_htab,
43988095 6884 rcuh_kind::COMPILE);
ed2dc618
SM
6885 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6886 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6887 if (types_htab == NULL)
6888 {
6889 dwarf2_per_objfile->signatured_types = NULL;
6890 return 0;
6891 }
6892
348e048f
DE
6893 dwarf2_per_objfile->signatured_types = types_htab;
6894
b2bdb8cf
SM
6895 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6896 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6897
6898 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6899 &dwarf2_per_objfile->all_type_units);
1fd400ff 6900
348e048f
DE
6901 return 1;
6902}
6903
6aa5f3a6
DE
6904/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6905 If SLOT is non-NULL, it is the entry to use in the hash table.
6906 Otherwise we find one. */
6907
6908static struct signatured_type *
ed2dc618
SM
6909add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6910 void **slot)
6aa5f3a6
DE
6911{
6912 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6913
b2bdb8cf
SM
6914 if (dwarf2_per_objfile->all_type_units.size ()
6915 == dwarf2_per_objfile->all_type_units.capacity ())
6916 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6917
b2bdb8cf
SM
6918 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6919 struct signatured_type);
6920
6921 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6922 sig_type->signature = sig;
6923 sig_type->per_cu.is_debug_types = 1;
6924 if (dwarf2_per_objfile->using_index)
6925 {
6926 sig_type->per_cu.v.quick =
6927 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6928 struct dwarf2_per_cu_quick_data);
6929 }
6930
6931 if (slot == NULL)
6932 {
6933 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6934 sig_type, INSERT);
6935 }
6936 gdb_assert (*slot == NULL);
6937 *slot = sig_type;
6938 /* The rest of sig_type must be filled in by the caller. */
6939 return sig_type;
6940}
6941
a2ce51a0
DE
6942/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6943 Fill in SIG_ENTRY with DWO_ENTRY. */
6944
6945static void
ed2dc618 6946fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6947 struct signatured_type *sig_entry,
6948 struct dwo_unit *dwo_entry)
6949{
7ee85ab1 6950 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6951 gdb_assert (! sig_entry->per_cu.queued);
6952 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6953 if (dwarf2_per_objfile->using_index)
6954 {
6955 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6956 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6957 }
6958 else
6959 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6960 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6961 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6962 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6963 gdb_assert (sig_entry->dwo_unit == NULL);
6964
6965 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6966 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6967 sig_entry->per_cu.length = dwo_entry->length;
6968 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6969 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6970 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6971 sig_entry->dwo_unit = dwo_entry;
6972}
6973
6974/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6975 If we haven't read the TU yet, create the signatured_type data structure
6976 for a TU to be read in directly from a DWO file, bypassing the stub.
6977 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6978 using .gdb_index, then when reading a CU we want to stay in the DWO file
6979 containing that CU. Otherwise we could end up reading several other DWO
6980 files (due to comdat folding) to process the transitive closure of all the
6981 mentioned TUs, and that can be slow. The current DWO file will have every
6982 type signature that it needs.
a2ce51a0
DE
6983 We only do this for .gdb_index because in the psymtab case we already have
6984 to read all the DWOs to build the type unit groups. */
6985
6986static struct signatured_type *
6987lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6988{
518817b3
SM
6989 struct dwarf2_per_objfile *dwarf2_per_objfile
6990 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6992 struct dwo_file *dwo_file;
6993 struct dwo_unit find_dwo_entry, *dwo_entry;
6994 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6995 void **slot;
a2ce51a0
DE
6996
6997 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6998
6aa5f3a6
DE
6999 /* If TU skeletons have been removed then we may not have read in any
7000 TUs yet. */
7001 if (dwarf2_per_objfile->signatured_types == NULL)
7002 {
7003 dwarf2_per_objfile->signatured_types
7004 = allocate_signatured_type_table (objfile);
7005 }
a2ce51a0
DE
7006
7007 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7008 Use the global signatured_types array to do our own comdat-folding
7009 of types. If this is the first time we're reading this TU, and
7010 the TU has an entry in .gdb_index, replace the recorded data from
7011 .gdb_index with this TU. */
a2ce51a0 7012
a2ce51a0 7013 find_sig_entry.signature = sig;
6aa5f3a6
DE
7014 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7015 &find_sig_entry, INSERT);
9a3c8263 7016 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7017
7018 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7019 read. Don't reassign the global entry to point to this DWO if that's
7020 the case. Also note that if the TU is already being read, it may not
7021 have come from a DWO, the program may be a mix of Fission-compiled
7022 code and non-Fission-compiled code. */
7023
7024 /* Have we already tried to read this TU?
7025 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7026 needn't exist in the global table yet). */
7027 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7028 return sig_entry;
7029
6aa5f3a6
DE
7030 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7031 dwo_unit of the TU itself. */
7032 dwo_file = cu->dwo_unit->dwo_file;
7033
a2ce51a0
DE
7034 /* Ok, this is the first time we're reading this TU. */
7035 if (dwo_file->tus == NULL)
7036 return NULL;
7037 find_dwo_entry.signature = sig;
9a3c8263 7038 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7039 if (dwo_entry == NULL)
7040 return NULL;
7041
6aa5f3a6
DE
7042 /* If the global table doesn't have an entry for this TU, add one. */
7043 if (sig_entry == NULL)
ed2dc618 7044 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7045
ed2dc618 7046 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7047 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7048 return sig_entry;
7049}
7050
a2ce51a0
DE
7051/* Subroutine of lookup_signatured_type.
7052 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7053 then try the DWP file. If the TU stub (skeleton) has been removed then
7054 it won't be in .gdb_index. */
a2ce51a0
DE
7055
7056static struct signatured_type *
7057lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7058{
518817b3
SM
7059 struct dwarf2_per_objfile *dwarf2_per_objfile
7060 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7061 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7062 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7063 struct dwo_unit *dwo_entry;
7064 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7065 void **slot;
a2ce51a0
DE
7066
7067 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7068 gdb_assert (dwp_file != NULL);
7069
6aa5f3a6
DE
7070 /* If TU skeletons have been removed then we may not have read in any
7071 TUs yet. */
7072 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7073 {
6aa5f3a6
DE
7074 dwarf2_per_objfile->signatured_types
7075 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7076 }
7077
6aa5f3a6
DE
7078 find_sig_entry.signature = sig;
7079 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7080 &find_sig_entry, INSERT);
9a3c8263 7081 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7082
7083 /* Have we already tried to read this TU?
7084 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7085 needn't exist in the global table yet). */
7086 if (sig_entry != NULL)
7087 return sig_entry;
7088
a2ce51a0
DE
7089 if (dwp_file->tus == NULL)
7090 return NULL;
ed2dc618 7091 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7092 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7093 if (dwo_entry == NULL)
7094 return NULL;
7095
ed2dc618
SM
7096 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7097 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7098
a2ce51a0
DE
7099 return sig_entry;
7100}
7101
380bca97 7102/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7103 Returns NULL if signature SIG is not present in the table.
7104 It is up to the caller to complain about this. */
348e048f
DE
7105
7106static struct signatured_type *
a2ce51a0 7107lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7108{
518817b3
SM
7109 struct dwarf2_per_objfile *dwarf2_per_objfile
7110 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7111
a2ce51a0
DE
7112 if (cu->dwo_unit
7113 && dwarf2_per_objfile->using_index)
7114 {
7115 /* We're in a DWO/DWP file, and we're using .gdb_index.
7116 These cases require special processing. */
ed2dc618 7117 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7118 return lookup_dwo_signatured_type (cu, sig);
7119 else
7120 return lookup_dwp_signatured_type (cu, sig);
7121 }
7122 else
7123 {
7124 struct signatured_type find_entry, *entry;
348e048f 7125
a2ce51a0
DE
7126 if (dwarf2_per_objfile->signatured_types == NULL)
7127 return NULL;
7128 find_entry.signature = sig;
9a3c8263
SM
7129 entry = ((struct signatured_type *)
7130 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7131 return entry;
7132 }
348e048f 7133}
42e7ad6c
DE
7134\f
7135/* Low level DIE reading support. */
348e048f 7136
d85a05f0
DJ
7137/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7138
7139static void
7140init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7141 struct dwarf2_cu *cu,
3019eac3 7142 struct dwarf2_section_info *section,
685af9cd
TT
7143 struct dwo_file *dwo_file,
7144 struct abbrev_table *abbrev_table)
d85a05f0 7145{
fceca515 7146 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7147 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7148 reader->cu = cu;
3019eac3 7149 reader->dwo_file = dwo_file;
dee91e82
DE
7150 reader->die_section = section;
7151 reader->buffer = section->buffer;
f664829e 7152 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7153 reader->comp_dir = NULL;
685af9cd 7154 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7155}
7156
b0c7bfa9
DE
7157/* Subroutine of init_cutu_and_read_dies to simplify it.
7158 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7159 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7160 already.
7161
7162 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7163 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7164 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7165 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7166 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7167 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7168 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7169 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7170 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7171 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7172 kept around for at least as long as *RESULT_READER.
7173
b0c7bfa9
DE
7174 The result is non-zero if a valid (non-dummy) DIE was found. */
7175
7176static int
7177read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7178 struct dwo_unit *dwo_unit,
b0c7bfa9 7179 struct die_info *stub_comp_unit_die,
a2ce51a0 7180 const char *stub_comp_dir,
b0c7bfa9 7181 struct die_reader_specs *result_reader,
d521ce57 7182 const gdb_byte **result_info_ptr,
b0c7bfa9 7183 struct die_info **result_comp_unit_die,
685af9cd
TT
7184 int *result_has_children,
7185 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7186{
ed2dc618 7187 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7188 struct objfile *objfile = dwarf2_per_objfile->objfile;
7189 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7190 bfd *abfd;
d521ce57 7191 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7192 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7193 int i,num_extra_attrs;
7194 struct dwarf2_section_info *dwo_abbrev_section;
7195 struct attribute *attr;
7196 struct die_info *comp_unit_die;
7197
b0aeadb3
DE
7198 /* At most one of these may be provided. */
7199 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7200
b0c7bfa9
DE
7201 /* These attributes aren't processed until later:
7202 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7203 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7204 referenced later. However, these attributes are found in the stub
7205 which we won't have later. In order to not impose this complication
7206 on the rest of the code, we read them here and copy them to the
7207 DWO CU/TU die. */
b0c7bfa9
DE
7208
7209 stmt_list = NULL;
7210 low_pc = NULL;
7211 high_pc = NULL;
7212 ranges = NULL;
7213 comp_dir = NULL;
7214
7215 if (stub_comp_unit_die != NULL)
7216 {
7217 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7218 DWO file. */
7219 if (! this_cu->is_debug_types)
7220 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7221 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7222 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7223 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7224 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7225
7226 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7227 We need the value before we can process DW_FORM_GNU_addr_index
7228 or DW_FORM_addrx. */
b0c7bfa9
DE
7229 cu->addr_base = 0;
7230 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7231 if (attr)
7232 cu->addr_base = DW_UNSND (attr);
7233
7234 /* There should be a DW_AT_ranges_base attribute here (if needed).
7235 We need the value before we can process DW_AT_ranges. */
7236 cu->ranges_base = 0;
7237 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7238 if (attr)
7239 cu->ranges_base = DW_UNSND (attr);
7240 }
a2ce51a0
DE
7241 else if (stub_comp_dir != NULL)
7242 {
7243 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7244 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7245 comp_dir->name = DW_AT_comp_dir;
7246 comp_dir->form = DW_FORM_string;
7247 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7248 DW_STRING (comp_dir) = stub_comp_dir;
7249 }
b0c7bfa9
DE
7250
7251 /* Set up for reading the DWO CU/TU. */
7252 cu->dwo_unit = dwo_unit;
685af9cd 7253 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7254 dwarf2_read_section (objfile, section);
a32a8923 7255 abfd = get_section_bfd_owner (section);
9c541725
PA
7256 begin_info_ptr = info_ptr = (section->buffer
7257 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7258 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7259
7260 if (this_cu->is_debug_types)
7261 {
b0c7bfa9
DE
7262 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7263
ed2dc618
SM
7264 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7265 &cu->header, section,
b0c7bfa9 7266 dwo_abbrev_section,
43988095 7267 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7268 /* This is not an assert because it can be caused by bad debug info. */
43988095 7269 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7270 {
7271 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7272 " TU at offset %s [in module %s]"),
a2ce51a0 7273 hex_string (sig_type->signature),
43988095 7274 hex_string (cu->header.signature),
9d8780f0 7275 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7276 bfd_get_filename (abfd));
7277 }
9c541725 7278 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7279 /* For DWOs coming from DWP files, we don't know the CU length
7280 nor the type's offset in the TU until now. */
7281 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7282 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7283
7284 /* Establish the type offset that can be used to lookup the type.
7285 For DWO files, we don't know it until now. */
9c541725
PA
7286 sig_type->type_offset_in_section
7287 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7288 }
7289 else
7290 {
ed2dc618
SM
7291 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7292 &cu->header, section,
b0c7bfa9 7293 dwo_abbrev_section,
43988095 7294 info_ptr, rcuh_kind::COMPILE);
9c541725 7295 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7296 /* For DWOs coming from DWP files, we don't know the CU length
7297 until now. */
7298 dwo_unit->length = get_cu_length (&cu->header);
7299 }
7300
685af9cd
TT
7301 *result_dwo_abbrev_table
7302 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7303 cu->header.abbrev_sect_off);
7304 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7305 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7306
7307 /* Read in the die, but leave space to copy over the attributes
7308 from the stub. This has the benefit of simplifying the rest of
7309 the code - all the work to maintain the illusion of a single
7310 DW_TAG_{compile,type}_unit DIE is done here. */
7311 num_extra_attrs = ((stmt_list != NULL)
7312 + (low_pc != NULL)
7313 + (high_pc != NULL)
7314 + (ranges != NULL)
7315 + (comp_dir != NULL));
7316 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7317 result_has_children, num_extra_attrs);
7318
7319 /* Copy over the attributes from the stub to the DIE we just read in. */
7320 comp_unit_die = *result_comp_unit_die;
7321 i = comp_unit_die->num_attrs;
7322 if (stmt_list != NULL)
7323 comp_unit_die->attrs[i++] = *stmt_list;
7324 if (low_pc != NULL)
7325 comp_unit_die->attrs[i++] = *low_pc;
7326 if (high_pc != NULL)
7327 comp_unit_die->attrs[i++] = *high_pc;
7328 if (ranges != NULL)
7329 comp_unit_die->attrs[i++] = *ranges;
7330 if (comp_dir != NULL)
7331 comp_unit_die->attrs[i++] = *comp_dir;
7332 comp_unit_die->num_attrs += num_extra_attrs;
7333
b4f54984 7334 if (dwarf_die_debug)
bf6af496
DE
7335 {
7336 fprintf_unfiltered (gdb_stdlog,
7337 "Read die from %s@0x%x of %s:\n",
a32a8923 7338 get_section_name (section),
bf6af496
DE
7339 (unsigned) (begin_info_ptr - section->buffer),
7340 bfd_get_filename (abfd));
b4f54984 7341 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7342 }
7343
a2ce51a0
DE
7344 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7345 TUs by skipping the stub and going directly to the entry in the DWO file.
7346 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7347 to get it via circuitous means. Blech. */
7348 if (comp_dir != NULL)
7349 result_reader->comp_dir = DW_STRING (comp_dir);
7350
b0c7bfa9
DE
7351 /* Skip dummy compilation units. */
7352 if (info_ptr >= begin_info_ptr + dwo_unit->length
7353 || peek_abbrev_code (abfd, info_ptr) == 0)
7354 return 0;
7355
7356 *result_info_ptr = info_ptr;
7357 return 1;
7358}
7359
a084a2a6
AT
7360/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7361 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7362 signature is part of the header. */
7363static gdb::optional<ULONGEST>
7364lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7365{
7366 if (cu->header.version >= 5)
7367 return cu->header.signature;
7368 struct attribute *attr;
7369 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7370 if (attr == nullptr)
7371 return gdb::optional<ULONGEST> ();
7372 return DW_UNSND (attr);
7373}
7374
b0c7bfa9
DE
7375/* Subroutine of init_cutu_and_read_dies to simplify it.
7376 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7377 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7378
7379static struct dwo_unit *
7380lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7381 struct die_info *comp_unit_die)
7382{
7383 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7384 struct dwo_unit *dwo_unit;
7385 const char *comp_dir, *dwo_name;
7386
a2ce51a0
DE
7387 gdb_assert (cu != NULL);
7388
b0c7bfa9 7389 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7390 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7391 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7392
7393 if (this_cu->is_debug_types)
7394 {
7395 struct signatured_type *sig_type;
7396
7397 /* Since this_cu is the first member of struct signatured_type,
7398 we can go from a pointer to one to a pointer to the other. */
7399 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7400 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7401 }
7402 else
7403 {
a084a2a6
AT
7404 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7405 if (!signature.has_value ())
b0c7bfa9
DE
7406 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7407 " [in module %s]"),
e3b94546 7408 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7409 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7410 *signature);
b0c7bfa9
DE
7411 }
7412
b0c7bfa9
DE
7413 return dwo_unit;
7414}
7415
a2ce51a0 7416/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7417 See it for a description of the parameters.
fcd3b13d 7418 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7419
7420static void
6aa5f3a6
DE
7421init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7422 int use_existing_cu, int keep,
a2ce51a0
DE
7423 die_reader_func_ftype *die_reader_func,
7424 void *data)
7425{
fcd3b13d 7426 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7427 struct signatured_type *sig_type;
a2ce51a0
DE
7428 struct die_reader_specs reader;
7429 const gdb_byte *info_ptr;
7430 struct die_info *comp_unit_die;
7431 int has_children;
ed2dc618 7432 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7433
7434 /* Verify we can do the following downcast, and that we have the
7435 data we need. */
7436 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7437 sig_type = (struct signatured_type *) this_cu;
7438 gdb_assert (sig_type->dwo_unit != NULL);
7439
6aa5f3a6
DE
7440 if (use_existing_cu && this_cu->cu != NULL)
7441 {
7442 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7443 /* There's no need to do the rereading_dwo_cu handling that
7444 init_cutu_and_read_dies does since we don't read the stub. */
7445 }
7446 else
7447 {
7448 /* If !use_existing_cu, this_cu->cu must be NULL. */
7449 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7450 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7451 }
7452
7453 /* A future optimization, if needed, would be to use an existing
7454 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7455 could share abbrev tables. */
a2ce51a0 7456
685af9cd
TT
7457 /* The abbreviation table used by READER, this must live at least as long as
7458 READER. */
7459 abbrev_table_up dwo_abbrev_table;
7460
a2ce51a0 7461 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7462 NULL /* stub_comp_unit_die */,
7463 sig_type->dwo_unit->dwo_file->comp_dir,
7464 &reader, &info_ptr,
685af9cd
TT
7465 &comp_unit_die, &has_children,
7466 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7467 {
7468 /* Dummy die. */
a2ce51a0
DE
7469 return;
7470 }
7471
7472 /* All the "real" work is done here. */
7473 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7474
6aa5f3a6 7475 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7476 but the alternative is making the latter more complex.
7477 This function is only for the special case of using DWO files directly:
7478 no point in overly complicating the general case just to handle this. */
fcd3b13d 7479 if (new_cu != NULL && keep)
a2ce51a0 7480 {
fcd3b13d
SM
7481 /* Link this CU into read_in_chain. */
7482 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7483 dwarf2_per_objfile->read_in_chain = this_cu;
7484 /* The chain owns it now. */
7485 new_cu.release ();
a2ce51a0 7486 }
a2ce51a0
DE
7487}
7488
fd820528 7489/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7490 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7491
f4dc4d17
DE
7492 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7493 Otherwise the table specified in the comp unit header is read in and used.
7494 This is an optimization for when we already have the abbrev table.
7495
dee91e82
DE
7496 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7497 Otherwise, a new CU is allocated with xmalloc.
7498
7499 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7500 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7501
7502 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7503 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7504
70221824 7505static void
fd820528 7506init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7507 struct abbrev_table *abbrev_table,
fd820528 7508 int use_existing_cu, int keep,
58f0c718 7509 bool skip_partial,
fd820528
DE
7510 die_reader_func_ftype *die_reader_func,
7511 void *data)
c906108c 7512{
ed2dc618 7513 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7514 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7515 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7516 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7517 struct dwarf2_cu *cu;
d521ce57 7518 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7519 struct die_reader_specs reader;
d85a05f0 7520 struct die_info *comp_unit_die;
dee91e82 7521 int has_children;
dee91e82 7522 struct signatured_type *sig_type = NULL;
4bdcc0c1 7523 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7524 /* Non-zero if CU currently points to a DWO file and we need to
7525 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7526 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7527 int rereading_dwo_cu = 0;
c906108c 7528
b4f54984 7529 if (dwarf_die_debug)
9d8780f0 7530 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7531 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7532 sect_offset_str (this_cu->sect_off));
09406207 7533
dee91e82
DE
7534 if (use_existing_cu)
7535 gdb_assert (keep);
23745b47 7536
a2ce51a0
DE
7537 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7538 file (instead of going through the stub), short-circuit all of this. */
7539 if (this_cu->reading_dwo_directly)
7540 {
7541 /* Narrow down the scope of possibilities to have to understand. */
7542 gdb_assert (this_cu->is_debug_types);
7543 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7544 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7545 die_reader_func, data);
a2ce51a0
DE
7546 return;
7547 }
7548
dee91e82
DE
7549 /* This is cheap if the section is already read in. */
7550 dwarf2_read_section (objfile, section);
7551
9c541725 7552 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7553
7554 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7555
fcd3b13d 7556 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7557 if (use_existing_cu && this_cu->cu != NULL)
7558 {
7559 cu = this_cu->cu;
42e7ad6c
DE
7560 /* If this CU is from a DWO file we need to start over, we need to
7561 refetch the attributes from the skeleton CU.
7562 This could be optimized by retrieving those attributes from when we
7563 were here the first time: the previous comp_unit_die was stored in
7564 comp_unit_obstack. But there's no data yet that we need this
7565 optimization. */
7566 if (cu->dwo_unit != NULL)
7567 rereading_dwo_cu = 1;
dee91e82
DE
7568 }
7569 else
7570 {
7571 /* If !use_existing_cu, this_cu->cu must be NULL. */
7572 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7573 new_cu.reset (new dwarf2_cu (this_cu));
7574 cu = new_cu.get ();
42e7ad6c 7575 }
dee91e82 7576
b0c7bfa9 7577 /* Get the header. */
9c541725 7578 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7579 {
7580 /* We already have the header, there's no need to read it in again. */
9c541725 7581 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7582 }
7583 else
7584 {
3019eac3 7585 if (this_cu->is_debug_types)
dee91e82 7586 {
ed2dc618
SM
7587 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7588 &cu->header, section,
4bdcc0c1 7589 abbrev_section, info_ptr,
43988095 7590 rcuh_kind::TYPE);
dee91e82 7591
42e7ad6c
DE
7592 /* Since per_cu is the first member of struct signatured_type,
7593 we can go from a pointer to one to a pointer to the other. */
7594 sig_type = (struct signatured_type *) this_cu;
43988095 7595 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7596 gdb_assert (sig_type->type_offset_in_tu
7597 == cu->header.type_cu_offset_in_tu);
7598 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7599
42e7ad6c
DE
7600 /* LENGTH has not been set yet for type units if we're
7601 using .gdb_index. */
1ce1cefd 7602 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7603
7604 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7605 sig_type->type_offset_in_section =
7606 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7607
7608 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7609 }
7610 else
7611 {
ed2dc618
SM
7612 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7613 &cu->header, section,
4bdcc0c1 7614 abbrev_section,
43988095
JK
7615 info_ptr,
7616 rcuh_kind::COMPILE);
dee91e82 7617
9c541725 7618 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7619 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7620 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7621 }
7622 }
10b3939b 7623
6caca83c 7624 /* Skip dummy compilation units. */
dee91e82 7625 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7626 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7627 return;
6caca83c 7628
433df2d4
DE
7629 /* If we don't have them yet, read the abbrevs for this compilation unit.
7630 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7631 done (own the table through ABBREV_TABLE_HOLDER). */
7632 abbrev_table_up abbrev_table_holder;
f4dc4d17 7633 if (abbrev_table != NULL)
685af9cd
TT
7634 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7635 else
f4dc4d17 7636 {
685af9cd
TT
7637 abbrev_table_holder
7638 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7639 cu->header.abbrev_sect_off);
7640 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7641 }
af703f96 7642
dee91e82 7643 /* Read the top level CU/TU die. */
685af9cd 7644 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7645 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7646
58f0c718
TT
7647 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7648 return;
7649
b0c7bfa9 7650 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7651 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7652 table from the DWO file and pass the ownership over to us. It will be
7653 referenced from READER, so we must make sure to free it after we're done
7654 with READER.
7655
b0c7bfa9
DE
7656 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7657 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7658 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7659 abbrev_table_up dwo_abbrev_table;
a084a2a6 7660 if (dwo_name != nullptr)
3019eac3 7661 {
3019eac3 7662 struct dwo_unit *dwo_unit;
b0c7bfa9 7663 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7664
7665 if (has_children)
6a506a2d 7666 {
b98664d3 7667 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7668 " has children (offset %s) [in module %s]"),
7669 sect_offset_str (this_cu->sect_off),
7670 bfd_get_filename (abfd));
6a506a2d 7671 }
b0c7bfa9 7672 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7673 if (dwo_unit != NULL)
3019eac3 7674 {
6a506a2d 7675 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7676 comp_unit_die, NULL,
6a506a2d 7677 &reader, &info_ptr,
685af9cd
TT
7678 &dwo_comp_unit_die, &has_children,
7679 &dwo_abbrev_table) == 0)
6a506a2d
DE
7680 {
7681 /* Dummy die. */
6a506a2d
DE
7682 return;
7683 }
7684 comp_unit_die = dwo_comp_unit_die;
7685 }
7686 else
7687 {
7688 /* Yikes, we couldn't find the rest of the DIE, we only have
7689 the stub. A complaint has already been logged. There's
7690 not much more we can do except pass on the stub DIE to
7691 die_reader_func. We don't want to throw an error on bad
7692 debug info. */
3019eac3
DE
7693 }
7694 }
7695
b0c7bfa9 7696 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7697 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7698
b0c7bfa9 7699 /* Done, clean up. */
fcd3b13d 7700 if (new_cu != NULL && keep)
348e048f 7701 {
fcd3b13d
SM
7702 /* Link this CU into read_in_chain. */
7703 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7704 dwarf2_per_objfile->read_in_chain = this_cu;
7705 /* The chain owns it now. */
7706 new_cu.release ();
348e048f 7707 }
dee91e82
DE
7708}
7709
33e80786
DE
7710/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7711 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7712 to have already done the lookup to find the DWO file).
dee91e82
DE
7713
7714 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7715 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7716
7717 We fill in THIS_CU->length.
7718
7719 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7720 linker) then DIE_READER_FUNC will not get called.
7721
7722 THIS_CU->cu is always freed when done.
3019eac3
DE
7723 This is done in order to not leave THIS_CU->cu in a state where we have
7724 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7725
7726static void
7727init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7728 struct dwo_file *dwo_file,
dee91e82
DE
7729 die_reader_func_ftype *die_reader_func,
7730 void *data)
7731{
ed2dc618 7732 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7733 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7734 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7735 bfd *abfd = get_section_bfd_owner (section);
33e80786 7736 struct dwarf2_section_info *abbrev_section;
d521ce57 7737 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7738 struct die_reader_specs reader;
dee91e82
DE
7739 struct die_info *comp_unit_die;
7740 int has_children;
7741
b4f54984 7742 if (dwarf_die_debug)
9d8780f0 7743 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7744 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7745 sect_offset_str (this_cu->sect_off));
09406207 7746
dee91e82
DE
7747 gdb_assert (this_cu->cu == NULL);
7748
33e80786
DE
7749 abbrev_section = (dwo_file != NULL
7750 ? &dwo_file->sections.abbrev
7751 : get_abbrev_section_for_cu (this_cu));
7752
dee91e82
DE
7753 /* This is cheap if the section is already read in. */
7754 dwarf2_read_section (objfile, section);
7755
fcd3b13d 7756 struct dwarf2_cu cu (this_cu);
dee91e82 7757
9c541725 7758 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7759 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7760 &cu.header, section,
4bdcc0c1 7761 abbrev_section, info_ptr,
43988095
JK
7762 (this_cu->is_debug_types
7763 ? rcuh_kind::TYPE
7764 : rcuh_kind::COMPILE));
dee91e82 7765
1ce1cefd 7766 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7767
7768 /* Skip dummy compilation units. */
7769 if (info_ptr >= begin_info_ptr + this_cu->length
7770 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7771 return;
72bf9492 7772
685af9cd
TT
7773 abbrev_table_up abbrev_table
7774 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7775 cu.header.abbrev_sect_off);
dee91e82 7776
685af9cd 7777 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7778 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7779
7780 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7781}
7782
3019eac3
DE
7783/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7784 does not lookup the specified DWO file.
7785 This cannot be used to read DWO files.
dee91e82
DE
7786
7787 THIS_CU->cu is always freed when done.
3019eac3
DE
7788 This is done in order to not leave THIS_CU->cu in a state where we have
7789 to care whether it refers to the "main" CU or the DWO CU.
7790 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7791
7792static void
7793init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7794 die_reader_func_ftype *die_reader_func,
7795 void *data)
7796{
33e80786 7797 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7798}
0018ea6f
DE
7799\f
7800/* Type Unit Groups.
dee91e82 7801
0018ea6f
DE
7802 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7803 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7804 so that all types coming from the same compilation (.o file) are grouped
7805 together. A future step could be to put the types in the same symtab as
7806 the CU the types ultimately came from. */
ff013f42 7807
f4dc4d17
DE
7808static hashval_t
7809hash_type_unit_group (const void *item)
7810{
9a3c8263
SM
7811 const struct type_unit_group *tu_group
7812 = (const struct type_unit_group *) item;
f4dc4d17 7813
094b34ac 7814 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7815}
348e048f
DE
7816
7817static int
f4dc4d17 7818eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7819{
9a3c8263
SM
7820 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7821 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7822
094b34ac 7823 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7824}
348e048f 7825
f4dc4d17
DE
7826/* Allocate a hash table for type unit groups. */
7827
7828static htab_t
ed2dc618 7829allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7830{
7831 return htab_create_alloc_ex (3,
7832 hash_type_unit_group,
7833 eq_type_unit_group,
7834 NULL,
ed2dc618 7835 &objfile->objfile_obstack,
f4dc4d17
DE
7836 hashtab_obstack_allocate,
7837 dummy_obstack_deallocate);
7838}
dee91e82 7839
f4dc4d17
DE
7840/* Type units that don't have DW_AT_stmt_list are grouped into their own
7841 partial symtabs. We combine several TUs per psymtab to not let the size
7842 of any one psymtab grow too big. */
7843#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7844#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7845
094b34ac 7846/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7847 Create the type_unit_group object used to hold one or more TUs. */
7848
7849static struct type_unit_group *
094b34ac 7850create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7851{
518817b3
SM
7852 struct dwarf2_per_objfile *dwarf2_per_objfile
7853 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7854 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7855 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7856 struct type_unit_group *tu_group;
f4dc4d17
DE
7857
7858 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7859 struct type_unit_group);
094b34ac 7860 per_cu = &tu_group->per_cu;
518817b3 7861 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7862
094b34ac
DE
7863 if (dwarf2_per_objfile->using_index)
7864 {
7865 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7866 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7867 }
7868 else
7869 {
9c541725 7870 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7871 struct partial_symtab *pst;
528e1572 7872 std::string name;
094b34ac
DE
7873
7874 /* Give the symtab a useful name for debug purposes. */
7875 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7876 name = string_printf ("<type_units_%d>",
7877 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7878 else
528e1572 7879 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7880
528e1572 7881 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7882 pst->anonymous = 1;
094b34ac 7883 }
f4dc4d17 7884
094b34ac 7885 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7886 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7887
7888 return tu_group;
7889}
7890
094b34ac
DE
7891/* Look up the type_unit_group for type unit CU, and create it if necessary.
7892 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7893
7894static struct type_unit_group *
ff39bb5e 7895get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7896{
518817b3
SM
7897 struct dwarf2_per_objfile *dwarf2_per_objfile
7898 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7899 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7900 struct type_unit_group *tu_group;
7901 void **slot;
7902 unsigned int line_offset;
7903 struct type_unit_group type_unit_group_for_lookup;
7904
7905 if (dwarf2_per_objfile->type_unit_groups == NULL)
7906 {
7907 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7908 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7909 }
7910
7911 /* Do we need to create a new group, or can we use an existing one? */
7912
7913 if (stmt_list)
7914 {
7915 line_offset = DW_UNSND (stmt_list);
7916 ++tu_stats->nr_symtab_sharers;
7917 }
7918 else
7919 {
7920 /* Ugh, no stmt_list. Rare, but we have to handle it.
7921 We can do various things here like create one group per TU or
7922 spread them over multiple groups to split up the expansion work.
7923 To avoid worst case scenarios (too many groups or too large groups)
7924 we, umm, group them in bunches. */
7925 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7926 | (tu_stats->nr_stmt_less_type_units
7927 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7928 ++tu_stats->nr_stmt_less_type_units;
7929 }
7930
094b34ac 7931 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7932 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7933 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7934 &type_unit_group_for_lookup, INSERT);
7935 if (*slot != NULL)
7936 {
9a3c8263 7937 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7938 gdb_assert (tu_group != NULL);
7939 }
7940 else
7941 {
9c541725 7942 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7943 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7944 *slot = tu_group;
7945 ++tu_stats->nr_symtabs;
7946 }
7947
7948 return tu_group;
7949}
0018ea6f
DE
7950\f
7951/* Partial symbol tables. */
7952
7953/* Create a psymtab named NAME and assign it to PER_CU.
7954
7955 The caller must fill in the following details:
7956 dirname, textlow, texthigh. */
7957
7958static struct partial_symtab *
7959create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7960{
e3b94546 7961 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7962 struct partial_symtab *pst;
7963
939652a5 7964 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7965
7966 pst->psymtabs_addrmap_supported = 1;
7967
7968 /* This is the glue that links PST into GDB's symbol API. */
7969 pst->read_symtab_private = per_cu;
7970 pst->read_symtab = dwarf2_read_symtab;
7971 per_cu->v.psymtab = pst;
7972
7973 return pst;
7974}
7975
b93601f3
TT
7976/* The DATA object passed to process_psymtab_comp_unit_reader has this
7977 type. */
7978
7979struct process_psymtab_comp_unit_data
7980{
7981 /* True if we are reading a DW_TAG_partial_unit. */
7982
7983 int want_partial_unit;
7984
7985 /* The "pretend" language that is used if the CU doesn't declare a
7986 language. */
7987
7988 enum language pretend_language;
7989};
7990
0018ea6f
DE
7991/* die_reader_func for process_psymtab_comp_unit. */
7992
7993static void
7994process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7995 const gdb_byte *info_ptr,
0018ea6f
DE
7996 struct die_info *comp_unit_die,
7997 int has_children,
7998 void *data)
7999{
8000 struct dwarf2_cu *cu = reader->cu;
518817b3 8001 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8003 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8004 CORE_ADDR baseaddr;
8005 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8006 struct partial_symtab *pst;
3a2b436a 8007 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8008 const char *filename;
9a3c8263
SM
8009 struct process_psymtab_comp_unit_data *info
8010 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8011
b93601f3 8012 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8013 return;
8014
8015 gdb_assert (! per_cu->is_debug_types);
8016
b93601f3 8017 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8018
0018ea6f 8019 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8020 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8021 if (filename == NULL)
0018ea6f 8022 filename = "";
0018ea6f
DE
8023
8024 pst = create_partial_symtab (per_cu, filename);
8025
8026 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8027 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8028
8029 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8030
8031 dwarf2_find_base_address (comp_unit_die, cu);
8032
8033 /* Possibly set the default values of LOWPC and HIGHPC from
8034 `DW_AT_ranges'. */
3a2b436a
JK
8035 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8036 &best_highpc, cu, pst);
8037 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8038 {
8039 CORE_ADDR low
8040 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8041 - baseaddr);
8042 CORE_ADDR high
8043 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8044 - baseaddr - 1);
8045 /* Store the contiguous range if it is not empty; it can be
8046 empty for CUs with no code. */
d320c2b5
TT
8047 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8048 low, high, pst);
79748972 8049 }
0018ea6f
DE
8050
8051 /* Check if comp unit has_children.
8052 If so, read the rest of the partial symbols from this comp unit.
8053 If not, there's no more debug_info for this comp unit. */
8054 if (has_children)
8055 {
8056 struct partial_die_info *first_die;
8057 CORE_ADDR lowpc, highpc;
8058
8059 lowpc = ((CORE_ADDR) -1);
8060 highpc = ((CORE_ADDR) 0);
8061
8062 first_die = load_partial_dies (reader, info_ptr, 1);
8063
8064 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8065 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8066
8067 /* If we didn't find a lowpc, set it to highpc to avoid
8068 complaints from `maint check'. */
8069 if (lowpc == ((CORE_ADDR) -1))
8070 lowpc = highpc;
8071
8072 /* If the compilation unit didn't have an explicit address range,
8073 then use the information extracted from its child dies. */
e385593e 8074 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8075 {
8076 best_lowpc = lowpc;
8077 best_highpc = highpc;
8078 }
8079 }
4ae976d1 8080 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8081 best_lowpc + baseaddr)
8082 - baseaddr);
4ae976d1 8083 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8084 best_highpc + baseaddr)
8085 - baseaddr);
0018ea6f 8086
8763cede 8087 end_psymtab_common (objfile, pst);
0018ea6f
DE
8088
8089 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8090 {
8091 int i;
8092 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8093 struct dwarf2_per_cu_data *iter;
8094
8095 /* Fill in 'dependencies' here; we fill in 'users' in a
8096 post-pass. */
8097 pst->number_of_dependencies = len;
a9342b62
TT
8098 pst->dependencies
8099 = objfile->partial_symtabs->allocate_dependencies (len);
0018ea6f
DE
8100 for (i = 0;
8101 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8102 i, iter);
8103 ++i)
8104 pst->dependencies[i] = iter->v.psymtab;
8105
8106 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8107 }
8108
8109 /* Get the list of files included in the current compilation unit,
8110 and build a psymtab for each of them. */
8111 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8112
b4f54984 8113 if (dwarf_read_debug)
b926417a
TT
8114 fprintf_unfiltered (gdb_stdlog,
8115 "Psymtab for %s unit @%s: %s - %s"
8116 ", %d global, %d static syms\n",
8117 per_cu->is_debug_types ? "type" : "comp",
8118 sect_offset_str (per_cu->sect_off),
8119 paddress (gdbarch, pst->text_low (objfile)),
8120 paddress (gdbarch, pst->text_high (objfile)),
8121 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8122}
8123
8124/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8125 Process compilation unit THIS_CU for a psymtab. */
8126
8127static void
8128process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8129 int want_partial_unit,
8130 enum language pretend_language)
0018ea6f
DE
8131{
8132 /* If this compilation unit was already read in, free the
8133 cached copy in order to read it in again. This is
8134 necessary because we skipped some symbols when we first
8135 read in the compilation unit (see load_partial_dies).
8136 This problem could be avoided, but the benefit is unclear. */
8137 if (this_cu->cu != NULL)
8138 free_one_cached_comp_unit (this_cu);
8139
f1902523 8140 if (this_cu->is_debug_types)
58f0c718
TT
8141 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8142 build_type_psymtabs_reader, NULL);
f1902523
JK
8143 else
8144 {
8145 process_psymtab_comp_unit_data info;
8146 info.want_partial_unit = want_partial_unit;
8147 info.pretend_language = pretend_language;
58f0c718 8148 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8149 process_psymtab_comp_unit_reader, &info);
8150 }
0018ea6f
DE
8151
8152 /* Age out any secondary CUs. */
ed2dc618 8153 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8154}
f4dc4d17
DE
8155
8156/* Reader function for build_type_psymtabs. */
8157
8158static void
8159build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8160 const gdb_byte *info_ptr,
f4dc4d17
DE
8161 struct die_info *type_unit_die,
8162 int has_children,
8163 void *data)
8164{
ed2dc618 8165 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8166 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8167 struct objfile *objfile = dwarf2_per_objfile->objfile;
8168 struct dwarf2_cu *cu = reader->cu;
8169 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8170 struct signatured_type *sig_type;
f4dc4d17
DE
8171 struct type_unit_group *tu_group;
8172 struct attribute *attr;
8173 struct partial_die_info *first_die;
8174 CORE_ADDR lowpc, highpc;
8175 struct partial_symtab *pst;
8176
8177 gdb_assert (data == NULL);
0186c6a7
DE
8178 gdb_assert (per_cu->is_debug_types);
8179 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8180
8181 if (! has_children)
8182 return;
8183
8184 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8185 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8186
df07e2c7
AB
8187 if (tu_group->tus == nullptr)
8188 tu_group->tus = new std::vector <signatured_type *>;
8189 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8190
8191 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8192 pst = create_partial_symtab (per_cu, "");
8193 pst->anonymous = 1;
8194
8195 first_die = load_partial_dies (reader, info_ptr, 1);
8196
8197 lowpc = (CORE_ADDR) -1;
8198 highpc = (CORE_ADDR) 0;
8199 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8200
8763cede 8201 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8202}
8203
73051182
DE
8204/* Struct used to sort TUs by their abbreviation table offset. */
8205
8206struct tu_abbrev_offset
8207{
b2bdb8cf
SM
8208 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8209 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8210 {}
8211
8212 signatured_type *sig_type;
73051182
DE
8213 sect_offset abbrev_offset;
8214};
8215
484cf504 8216/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8217
484cf504
TT
8218static bool
8219sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8220 const struct tu_abbrev_offset &b)
73051182 8221{
484cf504 8222 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8223}
8224
8225/* Efficiently read all the type units.
8226 This does the bulk of the work for build_type_psymtabs.
8227
8228 The efficiency is because we sort TUs by the abbrev table they use and
8229 only read each abbrev table once. In one program there are 200K TUs
8230 sharing 8K abbrev tables.
8231
8232 The main purpose of this function is to support building the
8233 dwarf2_per_objfile->type_unit_groups table.
8234 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8235 can collapse the search space by grouping them by stmt_list.
8236 The savings can be significant, in the same program from above the 200K TUs
8237 share 8K stmt_list tables.
8238
8239 FUNC is expected to call get_type_unit_group, which will create the
8240 struct type_unit_group if necessary and add it to
8241 dwarf2_per_objfile->type_unit_groups. */
8242
8243static void
ed2dc618 8244build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8245{
73051182 8246 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8247 abbrev_table_up abbrev_table;
73051182 8248 sect_offset abbrev_offset;
73051182
DE
8249
8250 /* It's up to the caller to not call us multiple times. */
8251 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8252
b2bdb8cf 8253 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8254 return;
8255
8256 /* TUs typically share abbrev tables, and there can be way more TUs than
8257 abbrev tables. Sort by abbrev table to reduce the number of times we
8258 read each abbrev table in.
8259 Alternatives are to punt or to maintain a cache of abbrev tables.
8260 This is simpler and efficient enough for now.
8261
8262 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8263 symtab to use). Typically TUs with the same abbrev offset have the same
8264 stmt_list value too so in practice this should work well.
8265
8266 The basic algorithm here is:
8267
8268 sort TUs by abbrev table
8269 for each TU with same abbrev table:
8270 read abbrev table if first user
8271 read TU top level DIE
8272 [IWBN if DWO skeletons had DW_AT_stmt_list]
8273 call FUNC */
8274
b4f54984 8275 if (dwarf_read_debug)
73051182
DE
8276 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8277
8278 /* Sort in a separate table to maintain the order of all_type_units
8279 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8280 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8281 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8282
8283 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8284 sorted_by_abbrev.emplace_back
8285 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8286 sig_type->per_cu.section,
8287 sig_type->per_cu.sect_off));
73051182 8288
484cf504
TT
8289 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8290 sort_tu_by_abbrev_offset);
73051182 8291
9c541725 8292 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8293
b2bdb8cf 8294 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8295 {
73051182
DE
8296 /* Switch to the next abbrev table if necessary. */
8297 if (abbrev_table == NULL
b2bdb8cf 8298 || tu.abbrev_offset != abbrev_offset)
73051182 8299 {
b2bdb8cf 8300 abbrev_offset = tu.abbrev_offset;
73051182 8301 abbrev_table =
ed2dc618
SM
8302 abbrev_table_read_table (dwarf2_per_objfile,
8303 &dwarf2_per_objfile->abbrev,
73051182
DE
8304 abbrev_offset);
8305 ++tu_stats->nr_uniq_abbrev_tables;
8306 }
8307
b2bdb8cf 8308 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8309 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8310 }
6aa5f3a6 8311}
73051182 8312
6aa5f3a6
DE
8313/* Print collected type unit statistics. */
8314
8315static void
ed2dc618 8316print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8317{
8318 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8319
8320 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8321 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8322 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8323 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8324 tu_stats->nr_uniq_abbrev_tables);
8325 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8326 tu_stats->nr_symtabs);
8327 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8328 tu_stats->nr_symtab_sharers);
8329 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8330 tu_stats->nr_stmt_less_type_units);
8331 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8332 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8333}
8334
f4dc4d17
DE
8335/* Traversal function for build_type_psymtabs. */
8336
8337static int
8338build_type_psymtab_dependencies (void **slot, void *info)
8339{
ed2dc618
SM
8340 struct dwarf2_per_objfile *dwarf2_per_objfile
8341 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8342 struct objfile *objfile = dwarf2_per_objfile->objfile;
8343 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8344 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8345 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8346 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8347 int i;
8348
8349 gdb_assert (len > 0);
0186c6a7 8350 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8351
8352 pst->number_of_dependencies = len;
a9342b62 8353 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8354 for (i = 0; i < len; ++i)
f4dc4d17 8355 {
df07e2c7 8356 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8357 gdb_assert (iter->per_cu.is_debug_types);
8358 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8359 iter->type_unit_group = tu_group;
f4dc4d17
DE
8360 }
8361
df07e2c7
AB
8362 delete tu_group->tus;
8363 tu_group->tus = nullptr;
348e048f
DE
8364
8365 return 1;
8366}
8367
8368/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8369 Build partial symbol tables for the .debug_types comp-units. */
8370
8371static void
ed2dc618 8372build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8373{
ed2dc618 8374 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8375 return;
8376
ed2dc618 8377 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8378}
f4dc4d17 8379
6aa5f3a6
DE
8380/* Traversal function for process_skeletonless_type_unit.
8381 Read a TU in a DWO file and build partial symbols for it. */
8382
8383static int
8384process_skeletonless_type_unit (void **slot, void *info)
8385{
8386 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8387 struct dwarf2_per_objfile *dwarf2_per_objfile
8388 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8389 struct signatured_type find_entry, *entry;
8390
8391 /* If this TU doesn't exist in the global table, add it and read it in. */
8392
8393 if (dwarf2_per_objfile->signatured_types == NULL)
8394 {
8395 dwarf2_per_objfile->signatured_types
ed2dc618 8396 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8397 }
8398
8399 find_entry.signature = dwo_unit->signature;
8400 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8401 INSERT);
8402 /* If we've already seen this type there's nothing to do. What's happening
8403 is we're doing our own version of comdat-folding here. */
8404 if (*slot != NULL)
8405 return 1;
8406
8407 /* This does the job that create_all_type_units would have done for
8408 this TU. */
ed2dc618
SM
8409 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8410 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8411 *slot = entry;
8412
8413 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8414 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8415 build_type_psymtabs_reader, NULL);
8416
8417 return 1;
8418}
8419
8420/* Traversal function for process_skeletonless_type_units. */
8421
8422static int
8423process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8424{
8425 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8426
8427 if (dwo_file->tus != NULL)
8428 {
8429 htab_traverse_noresize (dwo_file->tus,
8430 process_skeletonless_type_unit, info);
8431 }
8432
8433 return 1;
8434}
8435
8436/* Scan all TUs of DWO files, verifying we've processed them.
8437 This is needed in case a TU was emitted without its skeleton.
8438 Note: This can't be done until we know what all the DWO files are. */
8439
8440static void
ed2dc618 8441process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8442{
8443 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8444 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8445 && dwarf2_per_objfile->dwo_files != NULL)
8446 {
51ac9db5 8447 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8448 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8449 dwarf2_per_objfile);
6aa5f3a6 8450 }
348e048f
DE
8451}
8452
ed2dc618 8453/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8454
8455static void
ed2dc618 8456set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8457{
b76e467d 8458 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8459 {
95554aad 8460 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8461
36586728
TT
8462 if (pst == NULL)
8463 continue;
8464
b76e467d 8465 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8466 {
8467 /* Set the 'user' field only if it is not already set. */
8468 if (pst->dependencies[j]->user == NULL)
8469 pst->dependencies[j]->user = pst;
8470 }
8471 }
8472}
8473
93311388
DE
8474/* Build the partial symbol table by doing a quick pass through the
8475 .debug_info and .debug_abbrev sections. */
72bf9492 8476
93311388 8477static void
ed2dc618 8478dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8479{
ed2dc618 8480 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8481
b4f54984 8482 if (dwarf_read_debug)
45cfd468
DE
8483 {
8484 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8485 objfile_name (objfile));
45cfd468
DE
8486 }
8487
98bfdba5
PA
8488 dwarf2_per_objfile->reading_partial_symbols = 1;
8489
be391dca 8490 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8491
93311388
DE
8492 /* Any cached compilation units will be linked by the per-objfile
8493 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8494 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8495
ed2dc618 8496 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8497
ed2dc618 8498 create_all_comp_units (dwarf2_per_objfile);
c906108c 8499
60606b2c
TT
8500 /* Create a temporary address map on a temporary obstack. We later
8501 copy this to the final obstack. */
8268c778 8502 auto_obstack temp_obstack;
791afaa2
TT
8503
8504 scoped_restore save_psymtabs_addrmap
d320c2b5 8505 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8506 addrmap_create_mutable (&temp_obstack));
72bf9492 8507
b76e467d
SM
8508 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8509 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8510
6aa5f3a6 8511 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8512 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8513
8514 /* Now that all TUs have been processed we can fill in the dependencies. */
8515 if (dwarf2_per_objfile->type_unit_groups != NULL)
8516 {
8517 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8518 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8519 }
8520
b4f54984 8521 if (dwarf_read_debug)
ed2dc618 8522 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8523
ed2dc618 8524 set_partial_user (dwarf2_per_objfile);
95554aad 8525
d320c2b5
TT
8526 objfile->partial_symtabs->psymtabs_addrmap
8527 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8528 objfile->partial_symtabs->obstack ());
791afaa2
TT
8529 /* At this point we want to keep the address map. */
8530 save_psymtabs_addrmap.release ();
ff013f42 8531
b4f54984 8532 if (dwarf_read_debug)
45cfd468 8533 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8534 objfile_name (objfile));
ae038cb0
DJ
8535}
8536
3019eac3 8537/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8538
8539static void
dee91e82 8540load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8541 const gdb_byte *info_ptr,
dee91e82
DE
8542 struct die_info *comp_unit_die,
8543 int has_children,
8544 void *data)
ae038cb0 8545{
dee91e82 8546 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8547
95554aad 8548 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8549
ae038cb0
DJ
8550 /* Check if comp unit has_children.
8551 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8552 If not, there's no more debug_info for this comp unit. */
d85a05f0 8553 if (has_children)
dee91e82
DE
8554 load_partial_dies (reader, info_ptr, 0);
8555}
98bfdba5 8556
dee91e82
DE
8557/* Load the partial DIEs for a secondary CU into memory.
8558 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8559
dee91e82
DE
8560static void
8561load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8562{
58f0c718 8563 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8564 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8565}
8566
ae038cb0 8567static void
ed2dc618 8568read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8569 struct dwarf2_section_info *section,
f1902523 8570 struct dwarf2_section_info *abbrev_section,
b76e467d 8571 unsigned int is_dwz)
ae038cb0 8572{
d521ce57 8573 const gdb_byte *info_ptr;
ed2dc618 8574 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8575
b4f54984 8576 if (dwarf_read_debug)
bf6af496 8577 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8578 get_section_name (section),
8579 get_section_file_name (section));
bf6af496 8580
36586728 8581 dwarf2_read_section (objfile, section);
ae038cb0 8582
36586728 8583 info_ptr = section->buffer;
6e70227d 8584
36586728 8585 while (info_ptr < section->buffer + section->size)
ae038cb0 8586 {
ae038cb0 8587 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8588
9c541725 8589 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8590
f1902523 8591 comp_unit_head cu_header;
ed2dc618
SM
8592 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8593 abbrev_section, info_ptr,
8594 rcuh_kind::COMPILE);
ae038cb0
DJ
8595
8596 /* Save the compilation unit for later lookup. */
f1902523
JK
8597 if (cu_header.unit_type != DW_UT_type)
8598 {
8599 this_cu = XOBNEW (&objfile->objfile_obstack,
8600 struct dwarf2_per_cu_data);
8601 memset (this_cu, 0, sizeof (*this_cu));
8602 }
8603 else
8604 {
8605 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8606 struct signatured_type);
8607 memset (sig_type, 0, sizeof (*sig_type));
8608 sig_type->signature = cu_header.signature;
8609 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8610 this_cu = &sig_type->per_cu;
8611 }
8612 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8613 this_cu->sect_off = sect_off;
f1902523 8614 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8615 this_cu->is_dwz = is_dwz;
e3b94546 8616 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8617 this_cu->section = section;
ae038cb0 8618
b76e467d 8619 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8620
8621 info_ptr = info_ptr + this_cu->length;
8622 }
36586728
TT
8623}
8624
8625/* Create a list of all compilation units in OBJFILE.
8626 This is only done for -readnow and building partial symtabs. */
8627
8628static void
ed2dc618 8629create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8630{
b76e467d 8631 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8632 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8633 &dwarf2_per_objfile->abbrev, 0);
36586728 8634
b76e467d 8635 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8636 if (dwz != NULL)
ed2dc618 8637 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8638 1);
c906108c
SS
8639}
8640
5734ee8b 8641/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8642 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8643 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8644 DW_AT_ranges). See the comments of add_partial_subprogram on how
8645 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8646
72bf9492
DJ
8647static void
8648scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8649 CORE_ADDR *highpc, int set_addrmap,
8650 struct dwarf2_cu *cu)
c906108c 8651{
72bf9492 8652 struct partial_die_info *pdi;
c906108c 8653
91c24f0a
DC
8654 /* Now, march along the PDI's, descending into ones which have
8655 interesting children but skipping the children of the other ones,
8656 until we reach the end of the compilation unit. */
c906108c 8657
72bf9492 8658 pdi = first_die;
91c24f0a 8659
72bf9492
DJ
8660 while (pdi != NULL)
8661 {
52356b79 8662 pdi->fixup (cu);
c906108c 8663
f55ee35c 8664 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8665 children, so we need to look at them. Ditto for anonymous
8666 enums. */
933c6fe4 8667
72bf9492 8668 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8669 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8670 || pdi->tag == DW_TAG_imported_unit
8671 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8672 {
72bf9492 8673 switch (pdi->tag)
c906108c
SS
8674 {
8675 case DW_TAG_subprogram:
b1dc1806 8676 case DW_TAG_inlined_subroutine:
cdc07690 8677 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8678 break;
72929c62 8679 case DW_TAG_constant:
c906108c
SS
8680 case DW_TAG_variable:
8681 case DW_TAG_typedef:
91c24f0a 8682 case DW_TAG_union_type:
72bf9492 8683 if (!pdi->is_declaration)
63d06c5c 8684 {
72bf9492 8685 add_partial_symbol (pdi, cu);
63d06c5c
DC
8686 }
8687 break;
c906108c 8688 case DW_TAG_class_type:
680b30c7 8689 case DW_TAG_interface_type:
c906108c 8690 case DW_TAG_structure_type:
72bf9492 8691 if (!pdi->is_declaration)
c906108c 8692 {
72bf9492 8693 add_partial_symbol (pdi, cu);
c906108c 8694 }
b7fee5a3
KS
8695 if ((cu->language == language_rust
8696 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8697 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8698 set_addrmap, cu);
c906108c 8699 break;
91c24f0a 8700 case DW_TAG_enumeration_type:
72bf9492
DJ
8701 if (!pdi->is_declaration)
8702 add_partial_enumeration (pdi, cu);
c906108c
SS
8703 break;
8704 case DW_TAG_base_type:
a02abb62 8705 case DW_TAG_subrange_type:
c906108c 8706 /* File scope base type definitions are added to the partial
c5aa993b 8707 symbol table. */
72bf9492 8708 add_partial_symbol (pdi, cu);
c906108c 8709 break;
d9fa45fe 8710 case DW_TAG_namespace:
cdc07690 8711 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8712 break;
5d7cb8df 8713 case DW_TAG_module:
cdc07690 8714 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8715 break;
95554aad
TT
8716 case DW_TAG_imported_unit:
8717 {
8718 struct dwarf2_per_cu_data *per_cu;
8719
f4dc4d17
DE
8720 /* For now we don't handle imported units in type units. */
8721 if (cu->per_cu->is_debug_types)
8722 {
8723 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8724 " supported in type units [in module %s]"),
518817b3 8725 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8726 }
8727
e3b94546
SM
8728 per_cu = dwarf2_find_containing_comp_unit
8729 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8730 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8731
8732 /* Go read the partial unit, if needed. */
8733 if (per_cu->v.psymtab == NULL)
b93601f3 8734 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8735
f4dc4d17 8736 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8737 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8738 }
8739 break;
74921315
KS
8740 case DW_TAG_imported_declaration:
8741 add_partial_symbol (pdi, cu);
8742 break;
c906108c
SS
8743 default:
8744 break;
8745 }
8746 }
8747
72bf9492
DJ
8748 /* If the die has a sibling, skip to the sibling. */
8749
8750 pdi = pdi->die_sibling;
8751 }
8752}
8753
8754/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8755
72bf9492 8756 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8757 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8758 Enumerators are an exception; they use the scope of their parent
8759 enumeration type, i.e. the name of the enumeration type is not
8760 prepended to the enumerator.
91c24f0a 8761
72bf9492
DJ
8762 There are two complexities. One is DW_AT_specification; in this
8763 case "parent" means the parent of the target of the specification,
8764 instead of the direct parent of the DIE. The other is compilers
8765 which do not emit DW_TAG_namespace; in this case we try to guess
8766 the fully qualified name of structure types from their members'
8767 linkage names. This must be done using the DIE's children rather
8768 than the children of any DW_AT_specification target. We only need
8769 to do this for structures at the top level, i.e. if the target of
8770 any DW_AT_specification (if any; otherwise the DIE itself) does not
8771 have a parent. */
8772
8773/* Compute the scope prefix associated with PDI's parent, in
8774 compilation unit CU. The result will be allocated on CU's
8775 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8776 field. NULL is returned if no prefix is necessary. */
15d034d0 8777static const char *
72bf9492
DJ
8778partial_die_parent_scope (struct partial_die_info *pdi,
8779 struct dwarf2_cu *cu)
8780{
15d034d0 8781 const char *grandparent_scope;
72bf9492 8782 struct partial_die_info *parent, *real_pdi;
91c24f0a 8783
72bf9492
DJ
8784 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8785 then this means the parent of the specification DIE. */
8786
8787 real_pdi = pdi;
72bf9492 8788 while (real_pdi->has_specification)
fb816e8b 8789 {
122cf0f2
AB
8790 auto res = find_partial_die (real_pdi->spec_offset,
8791 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8792 real_pdi = res.pdi;
8793 cu = res.cu;
8794 }
72bf9492
DJ
8795
8796 parent = real_pdi->die_parent;
8797 if (parent == NULL)
8798 return NULL;
8799
8800 if (parent->scope_set)
8801 return parent->scope;
8802
52356b79 8803 parent->fixup (cu);
72bf9492 8804
10b3939b 8805 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8806
acebe513
UW
8807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8809 Work around this problem here. */
8810 if (cu->language == language_cplus
6e70227d 8811 && parent->tag == DW_TAG_namespace
acebe513
UW
8812 && strcmp (parent->name, "::") == 0
8813 && grandparent_scope == NULL)
8814 {
8815 parent->scope = NULL;
8816 parent->scope_set = 1;
8817 return NULL;
8818 }
8819
9c6c53f7
SA
8820 if (pdi->tag == DW_TAG_enumerator)
8821 /* Enumerators should not get the name of the enumeration as a prefix. */
8822 parent->scope = grandparent_scope;
8823 else if (parent->tag == DW_TAG_namespace
f55ee35c 8824 || parent->tag == DW_TAG_module
72bf9492
DJ
8825 || parent->tag == DW_TAG_structure_type
8826 || parent->tag == DW_TAG_class_type
680b30c7 8827 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8828 || parent->tag == DW_TAG_union_type
8829 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8830 {
8831 if (grandparent_scope == NULL)
8832 parent->scope = parent->name;
8833 else
3e43a32a
MS
8834 parent->scope = typename_concat (&cu->comp_unit_obstack,
8835 grandparent_scope,
f55ee35c 8836 parent->name, 0, cu);
72bf9492 8837 }
72bf9492
DJ
8838 else
8839 {
8840 /* FIXME drow/2004-04-01: What should we be doing with
8841 function-local names? For partial symbols, we should probably be
8842 ignoring them. */
fa9c3fa0
TT
8843 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8844 dwarf_tag_name (parent->tag),
8845 sect_offset_str (pdi->sect_off));
72bf9492 8846 parent->scope = grandparent_scope;
c906108c
SS
8847 }
8848
72bf9492
DJ
8849 parent->scope_set = 1;
8850 return parent->scope;
8851}
8852
8853/* Return the fully scoped name associated with PDI, from compilation unit
8854 CU. The result will be allocated with malloc. */
4568ecf9 8855
72bf9492
DJ
8856static char *
8857partial_die_full_name (struct partial_die_info *pdi,
8858 struct dwarf2_cu *cu)
8859{
15d034d0 8860 const char *parent_scope;
72bf9492 8861
98bfdba5
PA
8862 /* If this is a template instantiation, we can not work out the
8863 template arguments from partial DIEs. So, unfortunately, we have
8864 to go through the full DIEs. At least any work we do building
8865 types here will be reused if full symbols are loaded later. */
8866 if (pdi->has_template_arguments)
8867 {
52356b79 8868 pdi->fixup (cu);
98bfdba5
PA
8869
8870 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8871 {
8872 struct die_info *die;
8873 struct attribute attr;
8874 struct dwarf2_cu *ref_cu = cu;
8875
b64f50a1 8876 /* DW_FORM_ref_addr is using section offset. */
b4069958 8877 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8878 attr.form = DW_FORM_ref_addr;
9c541725 8879 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8880 die = follow_die_ref (NULL, &attr, &ref_cu);
8881
8882 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8883 }
8884 }
8885
72bf9492
DJ
8886 parent_scope = partial_die_parent_scope (pdi, cu);
8887 if (parent_scope == NULL)
8888 return NULL;
8889 else
f55ee35c 8890 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8891}
8892
8893static void
72bf9492 8894add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8895{
518817b3
SM
8896 struct dwarf2_per_objfile *dwarf2_per_objfile
8897 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8898 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8899 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8900 CORE_ADDR addr = 0;
15d034d0 8901 const char *actual_name = NULL;
e142c38c 8902 CORE_ADDR baseaddr;
15d034d0 8903 char *built_actual_name;
e142c38c
DJ
8904
8905 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8906
15d034d0
TT
8907 built_actual_name = partial_die_full_name (pdi, cu);
8908 if (built_actual_name != NULL)
8909 actual_name = built_actual_name;
63d06c5c 8910
72bf9492
DJ
8911 if (actual_name == NULL)
8912 actual_name = pdi->name;
8913
c906108c
SS
8914 switch (pdi->tag)
8915 {
b1dc1806 8916 case DW_TAG_inlined_subroutine:
c906108c 8917 case DW_TAG_subprogram:
79748972
TT
8918 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8919 - baseaddr);
2cfa0c8d 8920 if (pdi->is_external || cu->language == language_ada)
c906108c 8921 {
2cfa0c8d
JB
8922 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8923 of the global scope. But in Ada, we want to be able to access
8924 nested procedures globally. So all Ada subprograms are stored
8925 in the global scope. */
f47fb265 8926 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8927 built_actual_name != NULL,
f47fb265 8928 VAR_DOMAIN, LOC_BLOCK,
79748972 8929 SECT_OFF_TEXT (objfile),
75aedd27 8930 psymbol_placement::GLOBAL,
79748972
TT
8931 addr,
8932 cu->language, objfile);
c906108c
SS
8933 }
8934 else
8935 {
f47fb265 8936 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8937 built_actual_name != NULL,
f47fb265 8938 VAR_DOMAIN, LOC_BLOCK,
79748972 8939 SECT_OFF_TEXT (objfile),
75aedd27 8940 psymbol_placement::STATIC,
1762568f 8941 addr, cu->language, objfile);
c906108c 8942 }
0c1b455e
TT
8943
8944 if (pdi->main_subprogram && actual_name != NULL)
8945 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8946 break;
72929c62 8947 case DW_TAG_constant:
75aedd27
TT
8948 add_psymbol_to_list (actual_name, strlen (actual_name),
8949 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8950 -1, (pdi->is_external
8951 ? psymbol_placement::GLOBAL
8952 : psymbol_placement::STATIC),
8953 0, cu->language, objfile);
72929c62 8954 break;
c906108c 8955 case DW_TAG_variable:
95554aad
TT
8956 if (pdi->d.locdesc)
8957 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8958
95554aad 8959 if (pdi->d.locdesc
caac4577
JG
8960 && addr == 0
8961 && !dwarf2_per_objfile->has_section_at_zero)
8962 {
8963 /* A global or static variable may also have been stripped
8964 out by the linker if unused, in which case its address
8965 will be nullified; do not add such variables into partial
8966 symbol table then. */
8967 }
8968 else if (pdi->is_external)
c906108c
SS
8969 {
8970 /* Global Variable.
8971 Don't enter into the minimal symbol tables as there is
8972 a minimal symbol table entry from the ELF symbols already.
8973 Enter into partial symbol table if it has a location
8974 descriptor or a type.
8975 If the location descriptor is missing, new_symbol will create
8976 a LOC_UNRESOLVED symbol, the address of the variable will then
8977 be determined from the minimal symbol table whenever the variable
8978 is referenced.
8979 The address for the partial symbol table entry is not
8980 used by GDB, but it comes in handy for debugging partial symbol
8981 table building. */
8982
95554aad 8983 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8984 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8985 built_actual_name != NULL,
f47fb265 8986 VAR_DOMAIN, LOC_STATIC,
79748972 8987 SECT_OFF_TEXT (objfile),
75aedd27 8988 psymbol_placement::GLOBAL,
79748972 8989 addr, cu->language, objfile);
c906108c
SS
8990 }
8991 else
8992 {
ff908ebf
AW
8993 int has_loc = pdi->d.locdesc != NULL;
8994
8995 /* Static Variable. Skip symbols whose value we cannot know (those
8996 without location descriptors or constant values). */
8997 if (!has_loc && !pdi->has_const_value)
decbce07 8998 {
15d034d0 8999 xfree (built_actual_name);
decbce07
MS
9000 return;
9001 }
ff908ebf 9002
f47fb265 9003 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9004 built_actual_name != NULL,
f47fb265 9005 VAR_DOMAIN, LOC_STATIC,
79748972 9006 SECT_OFF_TEXT (objfile),
75aedd27 9007 psymbol_placement::STATIC,
79748972 9008 has_loc ? addr : 0,
f47fb265 9009 cu->language, objfile);
c906108c
SS
9010 }
9011 break;
9012 case DW_TAG_typedef:
9013 case DW_TAG_base_type:
a02abb62 9014 case DW_TAG_subrange_type:
38d518c9 9015 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9016 built_actual_name != NULL,
79748972 9017 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9018 psymbol_placement::STATIC,
1762568f 9019 0, cu->language, objfile);
c906108c 9020 break;
74921315 9021 case DW_TAG_imported_declaration:
72bf9492
DJ
9022 case DW_TAG_namespace:
9023 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9024 built_actual_name != NULL,
79748972 9025 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9026 psymbol_placement::GLOBAL,
1762568f 9027 0, cu->language, objfile);
72bf9492 9028 break;
530e8392 9029 case DW_TAG_module:
a5fd13a9
BH
9030 /* With Fortran 77 there might be a "BLOCK DATA" module
9031 available without any name. If so, we skip the module as it
9032 doesn't bring any value. */
9033 if (actual_name != nullptr)
9034 add_psymbol_to_list (actual_name, strlen (actual_name),
9035 built_actual_name != NULL,
9036 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9037 psymbol_placement::GLOBAL,
9038 0, cu->language, objfile);
530e8392 9039 break;
c906108c 9040 case DW_TAG_class_type:
680b30c7 9041 case DW_TAG_interface_type:
c906108c
SS
9042 case DW_TAG_structure_type:
9043 case DW_TAG_union_type:
9044 case DW_TAG_enumeration_type:
fa4028e9
JB
9045 /* Skip external references. The DWARF standard says in the section
9046 about "Structure, Union, and Class Type Entries": "An incomplete
9047 structure, union or class type is represented by a structure,
9048 union or class entry that does not have a byte size attribute
9049 and that has a DW_AT_declaration attribute." */
9050 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9051 {
15d034d0 9052 xfree (built_actual_name);
decbce07
MS
9053 return;
9054 }
fa4028e9 9055
63d06c5c
DC
9056 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9057 static vs. global. */
38d518c9 9058 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9059 built_actual_name != NULL,
79748972 9060 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9061 cu->language == language_cplus
75aedd27
TT
9062 ? psymbol_placement::GLOBAL
9063 : psymbol_placement::STATIC,
1762568f 9064 0, cu->language, objfile);
c906108c 9065
c906108c
SS
9066 break;
9067 case DW_TAG_enumerator:
38d518c9 9068 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9069 built_actual_name != NULL,
79748972 9070 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9071 cu->language == language_cplus
75aedd27
TT
9072 ? psymbol_placement::GLOBAL
9073 : psymbol_placement::STATIC,
1762568f 9074 0, cu->language, objfile);
c906108c
SS
9075 break;
9076 default:
9077 break;
9078 }
5c4e30ca 9079
15d034d0 9080 xfree (built_actual_name);
c906108c
SS
9081}
9082
5c4e30ca
DC
9083/* Read a partial die corresponding to a namespace; also, add a symbol
9084 corresponding to that namespace to the symbol table. NAMESPACE is
9085 the name of the enclosing namespace. */
91c24f0a 9086
72bf9492
DJ
9087static void
9088add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9089 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9090 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9091{
72bf9492 9092 /* Add a symbol for the namespace. */
e7c27a73 9093
72bf9492 9094 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9095
9096 /* Now scan partial symbols in that namespace. */
9097
91c24f0a 9098 if (pdi->has_children)
cdc07690 9099 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9100}
9101
5d7cb8df
JK
9102/* Read a partial die corresponding to a Fortran module. */
9103
9104static void
9105add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9106 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9107{
530e8392
KB
9108 /* Add a symbol for the namespace. */
9109
9110 add_partial_symbol (pdi, cu);
9111
f55ee35c 9112 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9113
9114 if (pdi->has_children)
cdc07690 9115 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9116}
9117
b1dc1806
XR
9118/* Read a partial die corresponding to a subprogram or an inlined
9119 subprogram and create a partial symbol for that subprogram.
9120 When the CU language allows it, this routine also defines a partial
9121 symbol for each nested subprogram that this subprogram contains.
9122 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9123 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9124
cdc07690
YQ
9125 PDI may also be a lexical block, in which case we simply search
9126 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9127 Again, this is only performed when the CU language allows this
9128 type of definitions. */
9129
9130static void
9131add_partial_subprogram (struct partial_die_info *pdi,
9132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9133 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9134{
b1dc1806 9135 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9136 {
9137 if (pdi->has_pc_info)
9138 {
9139 if (pdi->lowpc < *lowpc)
9140 *lowpc = pdi->lowpc;
9141 if (pdi->highpc > *highpc)
9142 *highpc = pdi->highpc;
cdc07690 9143 if (set_addrmap)
5734ee8b 9144 {
518817b3 9145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9146 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9147 CORE_ADDR baseaddr;
b926417a
TT
9148 CORE_ADDR this_highpc;
9149 CORE_ADDR this_lowpc;
5734ee8b
DJ
9150
9151 baseaddr = ANOFFSET (objfile->section_offsets,
9152 SECT_OFF_TEXT (objfile));
b926417a
TT
9153 this_lowpc
9154 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9155 pdi->lowpc + baseaddr)
9156 - baseaddr);
9157 this_highpc
9158 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9159 pdi->highpc + baseaddr)
9160 - baseaddr);
d320c2b5 9161 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9162 this_lowpc, this_highpc - 1,
9291a0cd 9163 cu->per_cu->v.psymtab);
5734ee8b 9164 }
481860b3
GB
9165 }
9166
9167 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9168 {
bc30ff58 9169 if (!pdi->is_declaration)
e8d05480
JB
9170 /* Ignore subprogram DIEs that do not have a name, they are
9171 illegal. Do not emit a complaint at this point, we will
9172 do so when we convert this psymtab into a symtab. */
9173 if (pdi->name)
9174 add_partial_symbol (pdi, cu);
bc30ff58
JB
9175 }
9176 }
6e70227d 9177
bc30ff58
JB
9178 if (! pdi->has_children)
9179 return;
9180
9181 if (cu->language == language_ada)
9182 {
9183 pdi = pdi->die_child;
9184 while (pdi != NULL)
9185 {
52356b79 9186 pdi->fixup (cu);
bc30ff58 9187 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9188 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9189 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9190 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9191 pdi = pdi->die_sibling;
9192 }
9193 }
9194}
9195
91c24f0a
DC
9196/* Read a partial die corresponding to an enumeration type. */
9197
72bf9492
DJ
9198static void
9199add_partial_enumeration (struct partial_die_info *enum_pdi,
9200 struct dwarf2_cu *cu)
91c24f0a 9201{
72bf9492 9202 struct partial_die_info *pdi;
91c24f0a
DC
9203
9204 if (enum_pdi->name != NULL)
72bf9492
DJ
9205 add_partial_symbol (enum_pdi, cu);
9206
9207 pdi = enum_pdi->die_child;
9208 while (pdi)
91c24f0a 9209 {
72bf9492 9210 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9211 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9212 else
72bf9492
DJ
9213 add_partial_symbol (pdi, cu);
9214 pdi = pdi->die_sibling;
91c24f0a 9215 }
91c24f0a
DC
9216}
9217
6caca83c
CC
9218/* Return the initial uleb128 in the die at INFO_PTR. */
9219
9220static unsigned int
d521ce57 9221peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9222{
9223 unsigned int bytes_read;
9224
9225 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9226}
9227
685af9cd
TT
9228/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9229 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9230
4bb7a0a7
DJ
9231 Return the corresponding abbrev, or NULL if the number is zero (indicating
9232 an empty DIE). In either case *BYTES_READ will be set to the length of
9233 the initial number. */
9234
9235static struct abbrev_info *
685af9cd
TT
9236peek_die_abbrev (const die_reader_specs &reader,
9237 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9238{
685af9cd 9239 dwarf2_cu *cu = reader.cu;
518817b3 9240 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9241 unsigned int abbrev_number
9242 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9243
9244 if (abbrev_number == 0)
9245 return NULL;
9246
685af9cd 9247 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9248 if (!abbrev)
9249 {
422b9917 9250 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9251 " at offset %s [in module %s]"),
422b9917 9252 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9253 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9254 }
9255
9256 return abbrev;
9257}
9258
93311388
DE
9259/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9260 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9261 DIE. Any children of the skipped DIEs will also be skipped. */
9262
d521ce57
TT
9263static const gdb_byte *
9264skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9265{
4bb7a0a7
DJ
9266 while (1)
9267 {
685af9cd
TT
9268 unsigned int bytes_read;
9269 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9270
4bb7a0a7
DJ
9271 if (abbrev == NULL)
9272 return info_ptr + bytes_read;
9273 else
dee91e82 9274 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9275 }
9276}
9277
93311388
DE
9278/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9279 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9280 abbrev corresponding to that skipped uleb128 should be passed in
9281 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9282 children. */
9283
d521ce57
TT
9284static const gdb_byte *
9285skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9286 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9287{
9288 unsigned int bytes_read;
9289 struct attribute attr;
dee91e82
DE
9290 bfd *abfd = reader->abfd;
9291 struct dwarf2_cu *cu = reader->cu;
d521ce57 9292 const gdb_byte *buffer = reader->buffer;
f664829e 9293 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9294 unsigned int form, i;
9295
9296 for (i = 0; i < abbrev->num_attrs; i++)
9297 {
9298 /* The only abbrev we care about is DW_AT_sibling. */
9299 if (abbrev->attrs[i].name == DW_AT_sibling)
9300 {
dee91e82 9301 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9302 if (attr.form == DW_FORM_ref_addr)
b98664d3 9303 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9304 else
b9502d3f 9305 {
9c541725
PA
9306 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9307 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9308
9309 if (sibling_ptr < info_ptr)
b98664d3 9310 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9311 else if (sibling_ptr > reader->buffer_end)
9312 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9313 else
9314 return sibling_ptr;
9315 }
4bb7a0a7
DJ
9316 }
9317
9318 /* If it isn't DW_AT_sibling, skip this attribute. */
9319 form = abbrev->attrs[i].form;
9320 skip_attribute:
9321 switch (form)
9322 {
4bb7a0a7 9323 case DW_FORM_ref_addr:
ae411497
TT
9324 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9325 and later it is offset sized. */
9326 if (cu->header.version == 2)
9327 info_ptr += cu->header.addr_size;
9328 else
9329 info_ptr += cu->header.offset_size;
9330 break;
36586728
TT
9331 case DW_FORM_GNU_ref_alt:
9332 info_ptr += cu->header.offset_size;
9333 break;
ae411497 9334 case DW_FORM_addr:
4bb7a0a7
DJ
9335 info_ptr += cu->header.addr_size;
9336 break;
9337 case DW_FORM_data1:
9338 case DW_FORM_ref1:
9339 case DW_FORM_flag:
8fe0f950 9340 case DW_FORM_strx1:
4bb7a0a7
DJ
9341 info_ptr += 1;
9342 break;
2dc7f7b3 9343 case DW_FORM_flag_present:
43988095 9344 case DW_FORM_implicit_const:
2dc7f7b3 9345 break;
4bb7a0a7
DJ
9346 case DW_FORM_data2:
9347 case DW_FORM_ref2:
8fe0f950 9348 case DW_FORM_strx2:
4bb7a0a7
DJ
9349 info_ptr += 2;
9350 break;
8fe0f950
AT
9351 case DW_FORM_strx3:
9352 info_ptr += 3;
9353 break;
4bb7a0a7
DJ
9354 case DW_FORM_data4:
9355 case DW_FORM_ref4:
8fe0f950 9356 case DW_FORM_strx4:
4bb7a0a7
DJ
9357 info_ptr += 4;
9358 break;
9359 case DW_FORM_data8:
9360 case DW_FORM_ref8:
55f1336d 9361 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9362 info_ptr += 8;
9363 break;
0224619f
JK
9364 case DW_FORM_data16:
9365 info_ptr += 16;
9366 break;
4bb7a0a7 9367 case DW_FORM_string:
9b1c24c8 9368 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9369 info_ptr += bytes_read;
9370 break;
2dc7f7b3 9371 case DW_FORM_sec_offset:
4bb7a0a7 9372 case DW_FORM_strp:
36586728 9373 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9374 info_ptr += cu->header.offset_size;
9375 break;
2dc7f7b3 9376 case DW_FORM_exprloc:
4bb7a0a7
DJ
9377 case DW_FORM_block:
9378 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9379 info_ptr += bytes_read;
9380 break;
9381 case DW_FORM_block1:
9382 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9383 break;
9384 case DW_FORM_block2:
9385 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9386 break;
9387 case DW_FORM_block4:
9388 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9389 break;
336d760d 9390 case DW_FORM_addrx:
cf532bd1 9391 case DW_FORM_strx:
4bb7a0a7
DJ
9392 case DW_FORM_sdata:
9393 case DW_FORM_udata:
9394 case DW_FORM_ref_udata:
3019eac3
DE
9395 case DW_FORM_GNU_addr_index:
9396 case DW_FORM_GNU_str_index:
d521ce57 9397 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9398 break;
9399 case DW_FORM_indirect:
9400 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9401 info_ptr += bytes_read;
9402 /* We need to continue parsing from here, so just go back to
9403 the top. */
9404 goto skip_attribute;
9405
9406 default:
3e43a32a
MS
9407 error (_("Dwarf Error: Cannot handle %s "
9408 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9409 dwarf_form_name (form),
9410 bfd_get_filename (abfd));
9411 }
9412 }
9413
9414 if (abbrev->has_children)
dee91e82 9415 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9416 else
9417 return info_ptr;
9418}
9419
93311388 9420/* Locate ORIG_PDI's sibling.
dee91e82 9421 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9422
d521ce57 9423static const gdb_byte *
dee91e82
DE
9424locate_pdi_sibling (const struct die_reader_specs *reader,
9425 struct partial_die_info *orig_pdi,
d521ce57 9426 const gdb_byte *info_ptr)
91c24f0a
DC
9427{
9428 /* Do we know the sibling already? */
72bf9492 9429
91c24f0a
DC
9430 if (orig_pdi->sibling)
9431 return orig_pdi->sibling;
9432
9433 /* Are there any children to deal with? */
9434
9435 if (!orig_pdi->has_children)
9436 return info_ptr;
9437
4bb7a0a7 9438 /* Skip the children the long way. */
91c24f0a 9439
dee91e82 9440 return skip_children (reader, info_ptr);
91c24f0a
DC
9441}
9442
257e7a09 9443/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9444 not NULL. */
c906108c
SS
9445
9446static void
257e7a09
YQ
9447dwarf2_read_symtab (struct partial_symtab *self,
9448 struct objfile *objfile)
c906108c 9449{
ed2dc618
SM
9450 struct dwarf2_per_objfile *dwarf2_per_objfile
9451 = get_dwarf2_per_objfile (objfile);
9452
257e7a09 9453 if (self->readin)
c906108c 9454 {
442e4d9c 9455 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9456 self->filename);
442e4d9c
YQ
9457 }
9458 else
9459 {
9460 if (info_verbose)
c906108c 9461 {
442e4d9c 9462 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9463 self->filename);
442e4d9c 9464 gdb_flush (gdb_stdout);
c906108c 9465 }
c906108c 9466
442e4d9c
YQ
9467 /* If this psymtab is constructed from a debug-only objfile, the
9468 has_section_at_zero flag will not necessarily be correct. We
9469 can get the correct value for this flag by looking at the data
9470 associated with the (presumably stripped) associated objfile. */
9471 if (objfile->separate_debug_objfile_backlink)
9472 {
9473 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9474 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9475
442e4d9c
YQ
9476 dwarf2_per_objfile->has_section_at_zero
9477 = dpo_backlink->has_section_at_zero;
9478 }
b2ab525c 9479
442e4d9c 9480 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9481
257e7a09 9482 psymtab_to_symtab_1 (self);
c906108c 9483
442e4d9c
YQ
9484 /* Finish up the debug error message. */
9485 if (info_verbose)
9486 printf_filtered (_("done.\n"));
c906108c 9487 }
95554aad 9488
ed2dc618 9489 process_cu_includes (dwarf2_per_objfile);
c906108c 9490}
9cdd5dbd
DE
9491\f
9492/* Reading in full CUs. */
c906108c 9493
10b3939b
DJ
9494/* Add PER_CU to the queue. */
9495
9496static void
95554aad
TT
9497queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9498 enum language pretend_language)
10b3939b
DJ
9499{
9500 struct dwarf2_queue_item *item;
9501
9502 per_cu->queued = 1;
8d749320 9503 item = XNEW (struct dwarf2_queue_item);
10b3939b 9504 item->per_cu = per_cu;
95554aad 9505 item->pretend_language = pretend_language;
10b3939b
DJ
9506 item->next = NULL;
9507
9508 if (dwarf2_queue == NULL)
9509 dwarf2_queue = item;
9510 else
9511 dwarf2_queue_tail->next = item;
9512
9513 dwarf2_queue_tail = item;
9514}
9515
89e63ee4
DE
9516/* If PER_CU is not yet queued, add it to the queue.
9517 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9518 dependency.
0907af0c 9519 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9520 meaning either PER_CU is already queued or it is already loaded.
9521
9522 N.B. There is an invariant here that if a CU is queued then it is loaded.
9523 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9524
9525static int
89e63ee4 9526maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9527 struct dwarf2_per_cu_data *per_cu,
9528 enum language pretend_language)
9529{
9530 /* We may arrive here during partial symbol reading, if we need full
9531 DIEs to process an unusual case (e.g. template arguments). Do
9532 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9533 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9534 {
9535 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9536 return 1;
9537 return 0;
9538 }
9539
9540 /* Mark the dependence relation so that we don't flush PER_CU
9541 too early. */
89e63ee4
DE
9542 if (dependent_cu != NULL)
9543 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9544
9545 /* If it's already on the queue, we have nothing to do. */
9546 if (per_cu->queued)
9547 return 0;
9548
9549 /* If the compilation unit is already loaded, just mark it as
9550 used. */
9551 if (per_cu->cu != NULL)
9552 {
9553 per_cu->cu->last_used = 0;
9554 return 0;
9555 }
9556
9557 /* Add it to the queue. */
9558 queue_comp_unit (per_cu, pretend_language);
9559
9560 return 1;
9561}
9562
10b3939b
DJ
9563/* Process the queue. */
9564
9565static void
ed2dc618 9566process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9567{
9568 struct dwarf2_queue_item *item, *next_item;
9569
b4f54984 9570 if (dwarf_read_debug)
45cfd468
DE
9571 {
9572 fprintf_unfiltered (gdb_stdlog,
9573 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9574 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9575 }
9576
03dd20cc
DJ
9577 /* The queue starts out with one item, but following a DIE reference
9578 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9579 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9580 {
cc12ce38
DE
9581 if ((dwarf2_per_objfile->using_index
9582 ? !item->per_cu->v.quick->compunit_symtab
9583 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9584 /* Skip dummy CUs. */
9585 && item->per_cu->cu != NULL)
f4dc4d17
DE
9586 {
9587 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9588 unsigned int debug_print_threshold;
247f5c4f 9589 char buf[100];
f4dc4d17 9590
247f5c4f 9591 if (per_cu->is_debug_types)
f4dc4d17 9592 {
247f5c4f
DE
9593 struct signatured_type *sig_type =
9594 (struct signatured_type *) per_cu;
9595
9d8780f0 9596 sprintf (buf, "TU %s at offset %s",
73be47f5 9597 hex_string (sig_type->signature),
9d8780f0 9598 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9599 /* There can be 100s of TUs.
9600 Only print them in verbose mode. */
9601 debug_print_threshold = 2;
f4dc4d17 9602 }
247f5c4f 9603 else
73be47f5 9604 {
9d8780f0
SM
9605 sprintf (buf, "CU at offset %s",
9606 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9607 debug_print_threshold = 1;
9608 }
247f5c4f 9609
b4f54984 9610 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9611 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9612
9613 if (per_cu->is_debug_types)
9614 process_full_type_unit (per_cu, item->pretend_language);
9615 else
9616 process_full_comp_unit (per_cu, item->pretend_language);
9617
b4f54984 9618 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9619 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9620 }
10b3939b
DJ
9621
9622 item->per_cu->queued = 0;
9623 next_item = item->next;
9624 xfree (item);
9625 }
9626
9627 dwarf2_queue_tail = NULL;
45cfd468 9628
b4f54984 9629 if (dwarf_read_debug)
45cfd468
DE
9630 {
9631 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9632 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9633 }
10b3939b
DJ
9634}
9635
10b3939b
DJ
9636/* Read in full symbols for PST, and anything it depends on. */
9637
c906108c 9638static void
fba45db2 9639psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9640{
10b3939b 9641 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9642 int i;
9643
95554aad
TT
9644 if (pst->readin)
9645 return;
9646
aaa75496 9647 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9648 if (!pst->dependencies[i]->readin
9649 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9650 {
9651 /* Inform about additional files that need to be read in. */
9652 if (info_verbose)
9653 {
a3f17187 9654 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9655 fputs_filtered (" ", gdb_stdout);
9656 wrap_here ("");
9657 fputs_filtered ("and ", gdb_stdout);
9658 wrap_here ("");
9659 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9660 wrap_here (""); /* Flush output. */
aaa75496
JB
9661 gdb_flush (gdb_stdout);
9662 }
9663 psymtab_to_symtab_1 (pst->dependencies[i]);
9664 }
9665
9a3c8263 9666 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9667
9668 if (per_cu == NULL)
aaa75496
JB
9669 {
9670 /* It's an include file, no symbols to read for it.
9671 Everything is in the parent symtab. */
9672 pst->readin = 1;
9673 return;
9674 }
c906108c 9675
58f0c718 9676 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9677}
9678
dee91e82
DE
9679/* Trivial hash function for die_info: the hash value of a DIE
9680 is its offset in .debug_info for this objfile. */
10b3939b 9681
dee91e82
DE
9682static hashval_t
9683die_hash (const void *item)
10b3939b 9684{
9a3c8263 9685 const struct die_info *die = (const struct die_info *) item;
6502dd73 9686
9c541725 9687 return to_underlying (die->sect_off);
dee91e82 9688}
63d06c5c 9689
dee91e82
DE
9690/* Trivial comparison function for die_info structures: two DIEs
9691 are equal if they have the same offset. */
98bfdba5 9692
dee91e82
DE
9693static int
9694die_eq (const void *item_lhs, const void *item_rhs)
9695{
9a3c8263
SM
9696 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9697 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9698
9c541725 9699 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9700}
c906108c 9701
dee91e82
DE
9702/* die_reader_func for load_full_comp_unit.
9703 This is identical to read_signatured_type_reader,
9704 but is kept separate for now. */
c906108c 9705
dee91e82
DE
9706static void
9707load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9708 const gdb_byte *info_ptr,
dee91e82
DE
9709 struct die_info *comp_unit_die,
9710 int has_children,
9711 void *data)
9712{
9713 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9714 enum language *language_ptr = (enum language *) data;
6caca83c 9715
dee91e82
DE
9716 gdb_assert (cu->die_hash == NULL);
9717 cu->die_hash =
9718 htab_create_alloc_ex (cu->header.length / 12,
9719 die_hash,
9720 die_eq,
9721 NULL,
9722 &cu->comp_unit_obstack,
9723 hashtab_obstack_allocate,
9724 dummy_obstack_deallocate);
e142c38c 9725
dee91e82
DE
9726 if (has_children)
9727 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9728 &info_ptr, comp_unit_die);
9729 cu->dies = comp_unit_die;
9730 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9731
9732 /* We try not to read any attributes in this function, because not
9cdd5dbd 9733 all CUs needed for references have been loaded yet, and symbol
10b3939b 9734 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9735 or we won't be able to build types correctly.
9736 Similarly, if we do not read the producer, we can not apply
9737 producer-specific interpretation. */
95554aad 9738 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9739}
10b3939b 9740
dee91e82 9741/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9742
dee91e82 9743static void
95554aad 9744load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9745 bool skip_partial,
95554aad 9746 enum language pretend_language)
dee91e82 9747{
3019eac3 9748 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9749
58f0c718 9750 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9751 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9752}
9753
3da10d80
KS
9754/* Add a DIE to the delayed physname list. */
9755
9756static void
9757add_to_method_list (struct type *type, int fnfield_index, int index,
9758 const char *name, struct die_info *die,
9759 struct dwarf2_cu *cu)
9760{
9761 struct delayed_method_info mi;
9762 mi.type = type;
9763 mi.fnfield_index = fnfield_index;
9764 mi.index = index;
9765 mi.name = name;
9766 mi.die = die;
c89b44cd 9767 cu->method_list.push_back (mi);
3da10d80
KS
9768}
9769
3693fdb3
PA
9770/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9771 "const" / "volatile". If so, decrements LEN by the length of the
9772 modifier and return true. Otherwise return false. */
9773
9774template<size_t N>
9775static bool
9776check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9777{
9778 size_t mod_len = sizeof (mod) - 1;
9779 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9780 {
9781 len -= mod_len;
9782 return true;
9783 }
9784 return false;
9785}
9786
3da10d80
KS
9787/* Compute the physnames of any methods on the CU's method list.
9788
9789 The computation of method physnames is delayed in order to avoid the
9790 (bad) condition that one of the method's formal parameters is of an as yet
9791 incomplete type. */
9792
9793static void
9794compute_delayed_physnames (struct dwarf2_cu *cu)
9795{
3693fdb3 9796 /* Only C++ delays computing physnames. */
c89b44cd 9797 if (cu->method_list.empty ())
3693fdb3
PA
9798 return;
9799 gdb_assert (cu->language == language_cplus);
9800
52941706 9801 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9802 {
1d06ead6 9803 const char *physname;
3da10d80 9804 struct fn_fieldlist *fn_flp
c89b44cd
TT
9805 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9806 physname = dwarf2_physname (mi.name, mi.die, cu);
9807 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9808 = physname ? physname : "";
3693fdb3
PA
9809
9810 /* Since there's no tag to indicate whether a method is a
9811 const/volatile overload, extract that information out of the
9812 demangled name. */
9813 if (physname != NULL)
9814 {
9815 size_t len = strlen (physname);
9816
9817 while (1)
9818 {
9819 if (physname[len] == ')') /* shortcut */
9820 break;
9821 else if (check_modifier (physname, len, " const"))
c89b44cd 9822 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9823 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9824 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9825 else
9826 break;
9827 }
9828 }
3da10d80 9829 }
c89b44cd
TT
9830
9831 /* The list is no longer needed. */
9832 cu->method_list.clear ();
3da10d80
KS
9833}
9834
a766d390
DE
9835/* Go objects should be embedded in a DW_TAG_module DIE,
9836 and it's not clear if/how imported objects will appear.
9837 To keep Go support simple until that's worked out,
9838 go back through what we've read and create something usable.
9839 We could do this while processing each DIE, and feels kinda cleaner,
9840 but that way is more invasive.
9841 This is to, for example, allow the user to type "p var" or "b main"
9842 without having to specify the package name, and allow lookups
9843 of module.object to work in contexts that use the expression
9844 parser. */
9845
9846static void
9847fixup_go_packaging (struct dwarf2_cu *cu)
9848{
9849 char *package_name = NULL;
9850 struct pending *list;
9851 int i;
9852
c24bdb02 9853 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9854 list != NULL;
9855 list = list->next)
a766d390
DE
9856 {
9857 for (i = 0; i < list->nsyms; ++i)
9858 {
9859 struct symbol *sym = list->symbol[i];
9860
9861 if (SYMBOL_LANGUAGE (sym) == language_go
9862 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9863 {
9864 char *this_package_name = go_symbol_package_name (sym);
9865
9866 if (this_package_name == NULL)
9867 continue;
9868 if (package_name == NULL)
9869 package_name = this_package_name;
9870 else
9871 {
518817b3
SM
9872 struct objfile *objfile
9873 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9874 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9875 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9876 (symbol_symtab (sym) != NULL
9877 ? symtab_to_filename_for_display
9878 (symbol_symtab (sym))
e3b94546 9879 : objfile_name (objfile)),
a766d390
DE
9880 this_package_name, package_name);
9881 xfree (this_package_name);
9882 }
9883 }
9884 }
9885 }
9886
9887 if (package_name != NULL)
9888 {
518817b3 9889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9890 const char *saved_package_name
021887d8 9891 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9892 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9893 saved_package_name);
a766d390
DE
9894 struct symbol *sym;
9895
e623cf5d 9896 sym = allocate_symbol (objfile);
f85f34ed 9897 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9898 SYMBOL_SET_NAMES (sym, saved_package_name,
9899 strlen (saved_package_name), 0, objfile);
a766d390
DE
9900 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9901 e.g., "main" finds the "main" module and not C's main(). */
9902 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9903 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9904 SYMBOL_TYPE (sym) = type;
9905
c24bdb02 9906 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9907
9908 xfree (package_name);
9909 }
9910}
9911
c9317f21
TT
9912/* Allocate a fully-qualified name consisting of the two parts on the
9913 obstack. */
9914
9915static const char *
9916rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9917{
9918 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9919}
9920
9921/* A helper that allocates a struct discriminant_info to attach to a
9922 union type. */
9923
9924static struct discriminant_info *
9925alloc_discriminant_info (struct type *type, int discriminant_index,
9926 int default_index)
9927{
9928 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9929 gdb_assert (discriminant_index == -1
9930 || (discriminant_index >= 0
9931 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9932 gdb_assert (default_index == -1
c7b15a66 9933 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9934
9935 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9936
9937 struct discriminant_info *disc
9938 = ((struct discriminant_info *)
9939 TYPE_ZALLOC (type,
9940 offsetof (struct discriminant_info, discriminants)
9941 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9942 disc->default_index = default_index;
9943 disc->discriminant_index = discriminant_index;
9944
9945 struct dynamic_prop prop;
9946 prop.kind = PROP_UNDEFINED;
9947 prop.data.baton = disc;
9948
9949 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9950
9951 return disc;
9952}
9953
9954/* Some versions of rustc emitted enums in an unusual way.
9955
9956 Ordinary enums were emitted as unions. The first element of each
9957 structure in the union was named "RUST$ENUM$DISR". This element
9958 held the discriminant.
9959
9960 These versions of Rust also implemented the "non-zero"
9961 optimization. When the enum had two values, and one is empty and
9962 the other holds a pointer that cannot be zero, the pointer is used
9963 as the discriminant, with a zero value meaning the empty variant.
9964 Here, the union's first member is of the form
9965 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9966 where the fieldnos are the indices of the fields that should be
9967 traversed in order to find the field (which may be several fields deep)
9968 and the variantname is the name of the variant of the case when the
9969 field is zero.
9970
9971 This function recognizes whether TYPE is of one of these forms,
9972 and, if so, smashes it to be a variant type. */
9973
9974static void
9975quirk_rust_enum (struct type *type, struct objfile *objfile)
9976{
9977 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9978
9979 /* We don't need to deal with empty enums. */
9980 if (TYPE_NFIELDS (type) == 0)
9981 return;
9982
9983#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9984 if (TYPE_NFIELDS (type) == 1
9985 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9986 {
9987 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9988
9989 /* Decode the field name to find the offset of the
9990 discriminant. */
9991 ULONGEST bit_offset = 0;
9992 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9993 while (name[0] >= '0' && name[0] <= '9')
9994 {
9995 char *tail;
9996 unsigned long index = strtoul (name, &tail, 10);
9997 name = tail;
9998 if (*name != '$'
9999 || index >= TYPE_NFIELDS (field_type)
10000 || (TYPE_FIELD_LOC_KIND (field_type, index)
10001 != FIELD_LOC_KIND_BITPOS))
10002 {
b98664d3 10003 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10004 "[in module %s]"),
10005 TYPE_FIELD_NAME (type, 0),
10006 objfile_name (objfile));
10007 return;
10008 }
10009 ++name;
10010
10011 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10012 field_type = TYPE_FIELD_TYPE (field_type, index);
10013 }
10014
10015 /* Make a union to hold the variants. */
10016 struct type *union_type = alloc_type (objfile);
10017 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10018 TYPE_NFIELDS (union_type) = 3;
10019 TYPE_FIELDS (union_type)
10020 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10021 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10022 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10023
10024 /* Put the discriminant must at index 0. */
10025 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10026 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10027 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10028 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10029
10030 /* The order of fields doesn't really matter, so put the real
10031 field at index 1 and the data-less field at index 2. */
10032 struct discriminant_info *disc
10033 = alloc_discriminant_info (union_type, 0, 1);
10034 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10035 TYPE_FIELD_NAME (union_type, 1)
10036 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10037 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10038 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10039 TYPE_FIELD_NAME (union_type, 1));
10040
10041 const char *dataless_name
10042 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10043 name);
10044 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10045 dataless_name);
10046 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10047 /* NAME points into the original discriminant name, which
10048 already has the correct lifetime. */
10049 TYPE_FIELD_NAME (union_type, 2) = name;
10050 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10051 disc->discriminants[2] = 0;
10052
10053 /* Smash this type to be a structure type. We have to do this
10054 because the type has already been recorded. */
10055 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10056 TYPE_NFIELDS (type) = 1;
10057 TYPE_FIELDS (type)
10058 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10059
10060 /* Install the variant part. */
10061 TYPE_FIELD_TYPE (type, 0) = union_type;
10062 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10063 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10064 }
10065 else if (TYPE_NFIELDS (type) == 1)
10066 {
10067 /* We assume that a union with a single field is a univariant
10068 enum. */
10069 /* Smash this type to be a structure type. We have to do this
10070 because the type has already been recorded. */
10071 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10072
10073 /* Make a union to hold the variants. */
10074 struct type *union_type = alloc_type (objfile);
10075 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10076 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10077 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10078 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10079 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10080
10081 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10082 const char *variant_name
10083 = rust_last_path_segment (TYPE_NAME (field_type));
10084 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10085 TYPE_NAME (field_type)
10086 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10087 TYPE_NAME (type), variant_name);
c9317f21
TT
10088
10089 /* Install the union in the outer struct type. */
10090 TYPE_NFIELDS (type) = 1;
10091 TYPE_FIELDS (type)
10092 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10093 TYPE_FIELD_TYPE (type, 0) = union_type;
10094 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10095 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10096
10097 alloc_discriminant_info (union_type, -1, 0);
10098 }
10099 else
10100 {
10101 struct type *disr_type = nullptr;
10102 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10103 {
10104 disr_type = TYPE_FIELD_TYPE (type, i);
10105
a037790e
TT
10106 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10107 {
10108 /* All fields of a true enum will be structs. */
10109 return;
10110 }
10111 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10112 {
10113 /* Could be data-less variant, so keep going. */
a037790e 10114 disr_type = nullptr;
c9317f21
TT
10115 }
10116 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10117 "RUST$ENUM$DISR") != 0)
10118 {
10119 /* Not a Rust enum. */
10120 return;
10121 }
10122 else
10123 {
10124 /* Found one. */
10125 break;
10126 }
10127 }
10128
10129 /* If we got here without a discriminant, then it's probably
10130 just a union. */
10131 if (disr_type == nullptr)
10132 return;
10133
10134 /* Smash this type to be a structure type. We have to do this
10135 because the type has already been recorded. */
10136 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10137
10138 /* Make a union to hold the variants. */
10139 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10140 struct type *union_type = alloc_type (objfile);
10141 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10142 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10143 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10144 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10145 TYPE_FIELDS (union_type)
10146 = (struct field *) TYPE_ZALLOC (union_type,
10147 (TYPE_NFIELDS (union_type)
10148 * sizeof (struct field)));
10149
10150 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10151 TYPE_NFIELDS (type) * sizeof (struct field));
10152
10153 /* Install the discriminant at index 0 in the union. */
10154 TYPE_FIELD (union_type, 0) = *disr_field;
10155 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10156 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10157
10158 /* Install the union in the outer struct type. */
10159 TYPE_FIELD_TYPE (type, 0) = union_type;
10160 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10161 TYPE_NFIELDS (type) = 1;
10162
10163 /* Set the size and offset of the union type. */
10164 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10165
10166 /* We need a way to find the correct discriminant given a
10167 variant name. For convenience we build a map here. */
10168 struct type *enum_type = FIELD_TYPE (*disr_field);
10169 std::unordered_map<std::string, ULONGEST> discriminant_map;
10170 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10171 {
10172 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10173 {
10174 const char *name
10175 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10176 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10177 }
10178 }
10179
10180 int n_fields = TYPE_NFIELDS (union_type);
10181 struct discriminant_info *disc
10182 = alloc_discriminant_info (union_type, 0, -1);
10183 /* Skip the discriminant here. */
10184 for (int i = 1; i < n_fields; ++i)
10185 {
10186 /* Find the final word in the name of this variant's type.
10187 That name can be used to look up the correct
10188 discriminant. */
10189 const char *variant_name
10190 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10191 i)));
10192
10193 auto iter = discriminant_map.find (variant_name);
10194 if (iter != discriminant_map.end ())
10195 disc->discriminants[i] = iter->second;
10196
bedda9ac 10197 /* Remove the discriminant field, if it exists. */
c9317f21 10198 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10199 if (TYPE_NFIELDS (sub_type) > 0)
10200 {
10201 --TYPE_NFIELDS (sub_type);
10202 ++TYPE_FIELDS (sub_type);
10203 }
c9317f21
TT
10204 TYPE_FIELD_NAME (union_type, i) = variant_name;
10205 TYPE_NAME (sub_type)
10206 = rust_fully_qualify (&objfile->objfile_obstack,
10207 TYPE_NAME (type), variant_name);
10208 }
10209 }
10210}
10211
10212/* Rewrite some Rust unions to be structures with variants parts. */
10213
10214static void
10215rust_union_quirks (struct dwarf2_cu *cu)
10216{
10217 gdb_assert (cu->language == language_rust);
52941706
SM
10218 for (type *type_ : cu->rust_unions)
10219 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10220 /* We don't need this any more. */
10221 cu->rust_unions.clear ();
c9317f21
TT
10222}
10223
95554aad
TT
10224/* Return the symtab for PER_CU. This works properly regardless of
10225 whether we're using the index or psymtabs. */
10226
43f3e411
DE
10227static struct compunit_symtab *
10228get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10229{
ed2dc618 10230 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10231 ? per_cu->v.quick->compunit_symtab
10232 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10233}
10234
10235/* A helper function for computing the list of all symbol tables
10236 included by PER_CU. */
10237
10238static void
4c39bc03 10239recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10240 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10241 struct dwarf2_per_cu_data *per_cu,
43f3e411 10242 struct compunit_symtab *immediate_parent)
95554aad
TT
10243{
10244 void **slot;
10245 int ix;
43f3e411 10246 struct compunit_symtab *cust;
95554aad
TT
10247 struct dwarf2_per_cu_data *iter;
10248
10249 slot = htab_find_slot (all_children, per_cu, INSERT);
10250 if (*slot != NULL)
10251 {
10252 /* This inclusion and its children have been processed. */
10253 return;
10254 }
10255
10256 *slot = per_cu;
10257 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10258 cust = get_compunit_symtab (per_cu);
10259 if (cust != NULL)
ec94af83
DE
10260 {
10261 /* If this is a type unit only add its symbol table if we haven't
10262 seen it yet (type unit per_cu's can share symtabs). */
10263 if (per_cu->is_debug_types)
10264 {
43f3e411 10265 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10266 if (*slot == NULL)
10267 {
43f3e411 10268 *slot = cust;
4c39bc03 10269 result->push_back (cust);
43f3e411
DE
10270 if (cust->user == NULL)
10271 cust->user = immediate_parent;
ec94af83
DE
10272 }
10273 }
10274 else
f9125b6c 10275 {
4c39bc03 10276 result->push_back (cust);
43f3e411
DE
10277 if (cust->user == NULL)
10278 cust->user = immediate_parent;
f9125b6c 10279 }
ec94af83 10280 }
95554aad
TT
10281
10282 for (ix = 0;
796a7ff8 10283 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10284 ++ix)
ec94af83
DE
10285 {
10286 recursively_compute_inclusions (result, all_children,
43f3e411 10287 all_type_symtabs, iter, cust);
ec94af83 10288 }
95554aad
TT
10289}
10290
43f3e411 10291/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10292 PER_CU. */
10293
10294static void
43f3e411 10295compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10296{
f4dc4d17
DE
10297 gdb_assert (! per_cu->is_debug_types);
10298
796a7ff8 10299 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10300 {
10301 int ix, len;
ec94af83 10302 struct dwarf2_per_cu_data *per_cu_iter;
4c39bc03 10303 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10304 htab_t all_children, all_type_symtabs;
43f3e411 10305 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10306
10307 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10308 if (cust == NULL)
95554aad
TT
10309 return;
10310
10311 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10312 NULL, xcalloc, xfree);
ec94af83
DE
10313 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10314 NULL, xcalloc, xfree);
95554aad
TT
10315
10316 for (ix = 0;
796a7ff8 10317 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10318 ix, per_cu_iter);
95554aad 10319 ++ix)
ec94af83
DE
10320 {
10321 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10322 all_type_symtabs, per_cu_iter,
43f3e411 10323 cust);
ec94af83 10324 }
95554aad 10325
ec94af83 10326 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10327 len = result_symtabs.size ();
43f3e411 10328 cust->includes
ed2dc618 10329 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10330 struct compunit_symtab *, len + 1);
4c39bc03
TT
10331 memcpy (cust->includes, result_symtabs.data (),
10332 len * sizeof (compunit_symtab *));
43f3e411 10333 cust->includes[len] = NULL;
95554aad 10334
95554aad 10335 htab_delete (all_children);
ec94af83 10336 htab_delete (all_type_symtabs);
95554aad
TT
10337 }
10338}
10339
10340/* Compute the 'includes' field for the symtabs of all the CUs we just
10341 read. */
10342
10343static void
ed2dc618 10344process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10345{
71b73764 10346 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10347 {
10348 if (! iter->is_debug_types)
43f3e411 10349 compute_compunit_symtab_includes (iter);
f4dc4d17 10350 }
95554aad 10351
c5d0225d 10352 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10353}
10354
9cdd5dbd 10355/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10356 already been loaded into memory. */
10357
10358static void
95554aad
TT
10359process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10360 enum language pretend_language)
10b3939b 10361{
10b3939b 10362 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10363 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10364 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10366 CORE_ADDR lowpc, highpc;
43f3e411 10367 struct compunit_symtab *cust;
10b3939b 10368 CORE_ADDR baseaddr;
4359dff1 10369 struct block *static_block;
3e29f34a 10370 CORE_ADDR addr;
10b3939b
DJ
10371
10372 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10373
c89b44cd
TT
10374 /* Clear the list here in case something was left over. */
10375 cu->method_list.clear ();
10b3939b 10376
95554aad
TT
10377 cu->language = pretend_language;
10378 cu->language_defn = language_def (cu->language);
10379
c906108c 10380 /* Do line number decoding in read_file_scope () */
10b3939b 10381 process_die (cu->dies, cu);
c906108c 10382
a766d390
DE
10383 /* For now fudge the Go package. */
10384 if (cu->language == language_go)
10385 fixup_go_packaging (cu);
10386
5f48f8f3 10387 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10388 should be complete, and it should now be safe to compute all of the
10389 physnames. */
10390 compute_delayed_physnames (cu);
3da10d80 10391
c9317f21
TT
10392 if (cu->language == language_rust)
10393 rust_union_quirks (cu);
10394
fae299cd
DC
10395 /* Some compilers don't define a DW_AT_high_pc attribute for the
10396 compilation unit. If the DW_AT_high_pc is missing, synthesize
10397 it, by scanning the DIE's below the compilation unit. */
10b3939b 10398 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10399
3e29f34a 10400 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10401 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10402
10403 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10404 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10405 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10406 addrmap to help ensure it has an accurate map of pc values belonging to
10407 this comp unit. */
10408 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10409
c24bdb02 10410 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10411 SECT_OFF_TEXT (objfile),
10412 0);
c906108c 10413
43f3e411 10414 if (cust != NULL)
c906108c 10415 {
df15bd07 10416 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10417
8be455d7
JK
10418 /* Set symtab language to language from DW_AT_language. If the
10419 compilation is from a C file generated by language preprocessors, do
10420 not set the language if it was already deduced by start_subfile. */
43f3e411 10421 if (!(cu->language == language_c
40e3ad0e 10422 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10423 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10424
10425 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10426 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10427 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10428 there were bugs in prologue debug info, fixed later in GCC-4.5
10429 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10430
10431 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10432 needed, it would be wrong due to missing DW_AT_producer there.
10433
10434 Still one can confuse GDB by using non-standard GCC compilation
10435 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10436 */
ab260dad 10437 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10438 cust->locations_valid = 1;
e0d00bc7
JK
10439
10440 if (gcc_4_minor >= 5)
43f3e411 10441 cust->epilogue_unwind_valid = 1;
96408a79 10442
43f3e411 10443 cust->call_site_htab = cu->call_site_htab;
c906108c 10444 }
9291a0cd
TT
10445
10446 if (dwarf2_per_objfile->using_index)
43f3e411 10447 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10448 else
10449 {
10450 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10451 pst->compunit_symtab = cust;
9291a0cd
TT
10452 pst->readin = 1;
10453 }
c906108c 10454
95554aad 10455 /* Push it for inclusion processing later. */
c5d0225d 10456 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10457
10458 /* Not needed any more. */
c24bdb02 10459 cu->reset_builder ();
f4dc4d17 10460}
45cfd468 10461
f4dc4d17
DE
10462/* Generate full symbol information for type unit PER_CU, whose DIEs have
10463 already been loaded into memory. */
10464
10465static void
10466process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10467 enum language pretend_language)
10468{
10469 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10470 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10471 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10472 struct compunit_symtab *cust;
0186c6a7
DE
10473 struct signatured_type *sig_type;
10474
10475 gdb_assert (per_cu->is_debug_types);
10476 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10477
c89b44cd
TT
10478 /* Clear the list here in case something was left over. */
10479 cu->method_list.clear ();
f4dc4d17 10480
f4dc4d17
DE
10481 cu->language = pretend_language;
10482 cu->language_defn = language_def (cu->language);
10483
10484 /* The symbol tables are set up in read_type_unit_scope. */
10485 process_die (cu->dies, cu);
10486
10487 /* For now fudge the Go package. */
10488 if (cu->language == language_go)
10489 fixup_go_packaging (cu);
10490
5f48f8f3 10491 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10492 should be complete, and it should now be safe to compute all of the
10493 physnames. */
10494 compute_delayed_physnames (cu);
f4dc4d17 10495
c9317f21
TT
10496 if (cu->language == language_rust)
10497 rust_union_quirks (cu);
10498
f4dc4d17
DE
10499 /* TUs share symbol tables.
10500 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10501 of it with end_expandable_symtab. Otherwise, complete the addition of
10502 this TU's symbols to the existing symtab. */
43f3e411 10503 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10504 {
c24bdb02
KS
10505 buildsym_compunit *builder = cu->get_builder ();
10506 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10507 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10508
43f3e411 10509 if (cust != NULL)
f4dc4d17
DE
10510 {
10511 /* Set symtab language to language from DW_AT_language. If the
10512 compilation is from a C file generated by language preprocessors,
10513 do not set the language if it was already deduced by
10514 start_subfile. */
43f3e411
DE
10515 if (!(cu->language == language_c
10516 && COMPUNIT_FILETABS (cust)->language != language_c))
10517 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10518 }
10519 }
10520 else
10521 {
c24bdb02 10522 cu->get_builder ()->augment_type_symtab ();
43f3e411 10523 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10524 }
10525
10526 if (dwarf2_per_objfile->using_index)
43f3e411 10527 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10528 else
10529 {
10530 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10531 pst->compunit_symtab = cust;
f4dc4d17 10532 pst->readin = 1;
45cfd468 10533 }
804d2729
TT
10534
10535 /* Not needed any more. */
c24bdb02 10536 cu->reset_builder ();
c906108c
SS
10537}
10538
95554aad
TT
10539/* Process an imported unit DIE. */
10540
10541static void
10542process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10543{
10544 struct attribute *attr;
10545
f4dc4d17
DE
10546 /* For now we don't handle imported units in type units. */
10547 if (cu->per_cu->is_debug_types)
10548 {
10549 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10550 " supported in type units [in module %s]"),
518817b3 10551 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10552 }
10553
95554aad
TT
10554 attr = dwarf2_attr (die, DW_AT_import, cu);
10555 if (attr != NULL)
10556 {
9c541725
PA
10557 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10558 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10559 dwarf2_per_cu_data *per_cu
e3b94546 10560 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10561 cu->per_cu->dwarf2_per_objfile);
95554aad 10562
69d751e3 10563 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10564 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10565 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10566
796a7ff8 10567 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10568 per_cu);
10569 }
10570}
10571
4c8aa72d
PA
10572/* RAII object that represents a process_die scope: i.e.,
10573 starts/finishes processing a DIE. */
10574class process_die_scope
adde2bff 10575{
4c8aa72d
PA
10576public:
10577 process_die_scope (die_info *die, dwarf2_cu *cu)
10578 : m_die (die), m_cu (cu)
10579 {
10580 /* We should only be processing DIEs not already in process. */
10581 gdb_assert (!m_die->in_process);
10582 m_die->in_process = true;
10583 }
8c3cb9fa 10584
4c8aa72d
PA
10585 ~process_die_scope ()
10586 {
10587 m_die->in_process = false;
10588
10589 /* If we're done processing the DIE for the CU that owns the line
10590 header, we don't need the line header anymore. */
10591 if (m_cu->line_header_die_owner == m_die)
10592 {
10593 delete m_cu->line_header;
10594 m_cu->line_header = NULL;
10595 m_cu->line_header_die_owner = NULL;
10596 }
10597 }
10598
10599private:
10600 die_info *m_die;
10601 dwarf2_cu *m_cu;
10602};
adde2bff 10603
c906108c
SS
10604/* Process a die and its children. */
10605
10606static void
e7c27a73 10607process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10608{
4c8aa72d 10609 process_die_scope scope (die, cu);
adde2bff 10610
c906108c
SS
10611 switch (die->tag)
10612 {
10613 case DW_TAG_padding:
10614 break;
10615 case DW_TAG_compile_unit:
95554aad 10616 case DW_TAG_partial_unit:
e7c27a73 10617 read_file_scope (die, cu);
c906108c 10618 break;
348e048f
DE
10619 case DW_TAG_type_unit:
10620 read_type_unit_scope (die, cu);
10621 break;
c906108c 10622 case DW_TAG_subprogram:
c906108c 10623 case DW_TAG_inlined_subroutine:
edb3359d 10624 read_func_scope (die, cu);
c906108c
SS
10625 break;
10626 case DW_TAG_lexical_block:
14898363
L
10627 case DW_TAG_try_block:
10628 case DW_TAG_catch_block:
e7c27a73 10629 read_lexical_block_scope (die, cu);
c906108c 10630 break;
216f72a1 10631 case DW_TAG_call_site:
96408a79
SA
10632 case DW_TAG_GNU_call_site:
10633 read_call_site_scope (die, cu);
10634 break;
c906108c 10635 case DW_TAG_class_type:
680b30c7 10636 case DW_TAG_interface_type:
c906108c
SS
10637 case DW_TAG_structure_type:
10638 case DW_TAG_union_type:
134d01f1 10639 process_structure_scope (die, cu);
c906108c
SS
10640 break;
10641 case DW_TAG_enumeration_type:
134d01f1 10642 process_enumeration_scope (die, cu);
c906108c 10643 break;
134d01f1 10644
f792889a
DJ
10645 /* These dies have a type, but processing them does not create
10646 a symbol or recurse to process the children. Therefore we can
10647 read them on-demand through read_type_die. */
c906108c 10648 case DW_TAG_subroutine_type:
72019c9c 10649 case DW_TAG_set_type:
c906108c 10650 case DW_TAG_array_type:
c906108c 10651 case DW_TAG_pointer_type:
c906108c 10652 case DW_TAG_ptr_to_member_type:
c906108c 10653 case DW_TAG_reference_type:
4297a3f0 10654 case DW_TAG_rvalue_reference_type:
c906108c 10655 case DW_TAG_string_type:
c906108c 10656 break;
134d01f1 10657
c906108c 10658 case DW_TAG_base_type:
a02abb62 10659 case DW_TAG_subrange_type:
cb249c71 10660 case DW_TAG_typedef:
134d01f1
DJ
10661 /* Add a typedef symbol for the type definition, if it has a
10662 DW_AT_name. */
f792889a 10663 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10664 break;
c906108c 10665 case DW_TAG_common_block:
e7c27a73 10666 read_common_block (die, cu);
c906108c
SS
10667 break;
10668 case DW_TAG_common_inclusion:
10669 break;
d9fa45fe 10670 case DW_TAG_namespace:
9068261f 10671 cu->processing_has_namespace_info = true;
e7c27a73 10672 read_namespace (die, cu);
d9fa45fe 10673 break;
5d7cb8df 10674 case DW_TAG_module:
9068261f 10675 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10676 read_module (die, cu);
10677 break;
d9fa45fe 10678 case DW_TAG_imported_declaration:
9068261f 10679 cu->processing_has_namespace_info = true;
74921315
KS
10680 if (read_namespace_alias (die, cu))
10681 break;
86a73007
TT
10682 /* The declaration is not a global namespace alias. */
10683 /* Fall through. */
d9fa45fe 10684 case DW_TAG_imported_module:
9068261f 10685 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10686 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10687 || cu->language != language_fortran))
b98664d3 10688 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10689 dwarf_tag_name (die->tag));
10690 read_import_statement (die, cu);
d9fa45fe 10691 break;
95554aad
TT
10692
10693 case DW_TAG_imported_unit:
10694 process_imported_unit_die (die, cu);
10695 break;
10696
71a3c369
TT
10697 case DW_TAG_variable:
10698 read_variable (die, cu);
10699 break;
10700
c906108c 10701 default:
e7c27a73 10702 new_symbol (die, NULL, cu);
c906108c
SS
10703 break;
10704 }
10705}
ca69b9e6
DE
10706\f
10707/* DWARF name computation. */
c906108c 10708
94af9270
KS
10709/* A helper function for dwarf2_compute_name which determines whether DIE
10710 needs to have the name of the scope prepended to the name listed in the
10711 die. */
10712
10713static int
10714die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10715{
1c809c68
TT
10716 struct attribute *attr;
10717
94af9270
KS
10718 switch (die->tag)
10719 {
10720 case DW_TAG_namespace:
10721 case DW_TAG_typedef:
10722 case DW_TAG_class_type:
10723 case DW_TAG_interface_type:
10724 case DW_TAG_structure_type:
10725 case DW_TAG_union_type:
10726 case DW_TAG_enumeration_type:
10727 case DW_TAG_enumerator:
10728 case DW_TAG_subprogram:
08a76f8a 10729 case DW_TAG_inlined_subroutine:
94af9270 10730 case DW_TAG_member:
74921315 10731 case DW_TAG_imported_declaration:
94af9270
KS
10732 return 1;
10733
10734 case DW_TAG_variable:
c2b0a229 10735 case DW_TAG_constant:
94af9270
KS
10736 /* We only need to prefix "globally" visible variables. These include
10737 any variable marked with DW_AT_external or any variable that
10738 lives in a namespace. [Variables in anonymous namespaces
10739 require prefixing, but they are not DW_AT_external.] */
10740
10741 if (dwarf2_attr (die, DW_AT_specification, cu))
10742 {
10743 struct dwarf2_cu *spec_cu = cu;
9a619af0 10744
94af9270
KS
10745 return die_needs_namespace (die_specification (die, &spec_cu),
10746 spec_cu);
10747 }
10748
1c809c68 10749 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10750 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10751 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10752 return 0;
10753 /* A variable in a lexical block of some kind does not need a
10754 namespace, even though in C++ such variables may be external
10755 and have a mangled name. */
10756 if (die->parent->tag == DW_TAG_lexical_block
10757 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10758 || die->parent->tag == DW_TAG_catch_block
10759 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10760 return 0;
10761 return 1;
94af9270
KS
10762
10763 default:
10764 return 0;
10765 }
10766}
10767
73b9be8b
KS
10768/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10769 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10770 defined for the given DIE. */
10771
10772static struct attribute *
10773dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10774{
10775 struct attribute *attr;
10776
10777 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10778 if (attr == NULL)
10779 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10780
10781 return attr;
10782}
10783
10784/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10785 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10786 defined for the given DIE. */
10787
10788static const char *
10789dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10790{
10791 const char *linkage_name;
10792
10793 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10794 if (linkage_name == NULL)
10795 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10796
10797 return linkage_name;
10798}
10799
94af9270 10800/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10801 compute the physname for the object, which include a method's:
9c37b5ae 10802 - formal parameters (C++),
a766d390 10803 - receiver type (Go),
a766d390
DE
10804
10805 The term "physname" is a bit confusing.
10806 For C++, for example, it is the demangled name.
10807 For Go, for example, it's the mangled name.
94af9270 10808
af6b7be1
JB
10809 For Ada, return the DIE's linkage name rather than the fully qualified
10810 name. PHYSNAME is ignored..
10811
94af9270
KS
10812 The result is allocated on the objfile_obstack and canonicalized. */
10813
10814static const char *
15d034d0
TT
10815dwarf2_compute_name (const char *name,
10816 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10817 int physname)
10818{
518817b3 10819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10820
94af9270
KS
10821 if (name == NULL)
10822 name = dwarf2_name (die, cu);
10823
2ee7123e
DE
10824 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10825 but otherwise compute it by typename_concat inside GDB.
10826 FIXME: Actually this is not really true, or at least not always true.
10827 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10828 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10829 will set the demangled name to the result of dwarf2_full_name, and it is
10830 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10831 if (cu->language == language_ada
10832 || (cu->language == language_fortran && physname))
10833 {
10834 /* For Ada unit, we prefer the linkage name over the name, as
10835 the former contains the exported name, which the user expects
10836 to be able to reference. Ideally, we want the user to be able
10837 to reference this entity using either natural or linkage name,
10838 but we haven't started looking at this enhancement yet. */
73b9be8b 10839 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10840
2ee7123e
DE
10841 if (linkage_name != NULL)
10842 return linkage_name;
f55ee35c
JK
10843 }
10844
94af9270
KS
10845 /* These are the only languages we know how to qualify names in. */
10846 if (name != NULL
9c37b5ae 10847 && (cu->language == language_cplus
c44af4eb
TT
10848 || cu->language == language_fortran || cu->language == language_d
10849 || cu->language == language_rust))
94af9270
KS
10850 {
10851 if (die_needs_namespace (die, cu))
10852 {
0d5cff50 10853 const char *prefix;
34a68019 10854 const char *canonical_name = NULL;
94af9270 10855
d7e74731
PA
10856 string_file buf;
10857
94af9270 10858 prefix = determine_prefix (die, cu);
94af9270
KS
10859 if (*prefix != '\0')
10860 {
f55ee35c
JK
10861 char *prefixed_name = typename_concat (NULL, prefix, name,
10862 physname, cu);
9a619af0 10863
d7e74731 10864 buf.puts (prefixed_name);
94af9270
KS
10865 xfree (prefixed_name);
10866 }
10867 else
d7e74731 10868 buf.puts (name);
94af9270 10869
98bfdba5
PA
10870 /* Template parameters may be specified in the DIE's DW_AT_name, or
10871 as children with DW_TAG_template_type_param or
10872 DW_TAG_value_type_param. If the latter, add them to the name
10873 here. If the name already has template parameters, then
10874 skip this step; some versions of GCC emit both, and
10875 it is more efficient to use the pre-computed name.
10876
10877 Something to keep in mind about this process: it is very
10878 unlikely, or in some cases downright impossible, to produce
10879 something that will match the mangled name of a function.
10880 If the definition of the function has the same debug info,
10881 we should be able to match up with it anyway. But fallbacks
10882 using the minimal symbol, for instance to find a method
10883 implemented in a stripped copy of libstdc++, will not work.
10884 If we do not have debug info for the definition, we will have to
10885 match them up some other way.
10886
10887 When we do name matching there is a related problem with function
10888 templates; two instantiated function templates are allowed to
10889 differ only by their return types, which we do not add here. */
10890
10891 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10892 {
10893 struct attribute *attr;
10894 struct die_info *child;
10895 int first = 1;
10896
10897 die->building_fullname = 1;
10898
10899 for (child = die->child; child != NULL; child = child->sibling)
10900 {
10901 struct type *type;
12df843f 10902 LONGEST value;
d521ce57 10903 const gdb_byte *bytes;
98bfdba5
PA
10904 struct dwarf2_locexpr_baton *baton;
10905 struct value *v;
10906
10907 if (child->tag != DW_TAG_template_type_param
10908 && child->tag != DW_TAG_template_value_param)
10909 continue;
10910
10911 if (first)
10912 {
d7e74731 10913 buf.puts ("<");
98bfdba5
PA
10914 first = 0;
10915 }
10916 else
d7e74731 10917 buf.puts (", ");
98bfdba5
PA
10918
10919 attr = dwarf2_attr (child, DW_AT_type, cu);
10920 if (attr == NULL)
10921 {
b98664d3 10922 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10923 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10924 continue;
10925 }
10926 type = die_type (child, cu);
10927
10928 if (child->tag == DW_TAG_template_type_param)
10929 {
c1ec8cea
TT
10930 c_print_type (type, "", &buf, -1, 0, cu->language,
10931 &type_print_raw_options);
98bfdba5
PA
10932 continue;
10933 }
10934
10935 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10936 if (attr == NULL)
10937 {
b98664d3 10938 complaint (_("template parameter missing "
3e43a32a 10939 "DW_AT_const_value"));
d7e74731 10940 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10941 continue;
10942 }
10943
10944 dwarf2_const_value_attr (attr, type, name,
10945 &cu->comp_unit_obstack, cu,
10946 &value, &bytes, &baton);
10947
10948 if (TYPE_NOSIGN (type))
10949 /* GDB prints characters as NUMBER 'CHAR'. If that's
10950 changed, this can use value_print instead. */
d7e74731 10951 c_printchar (value, type, &buf);
98bfdba5
PA
10952 else
10953 {
10954 struct value_print_options opts;
10955
10956 if (baton != NULL)
10957 v = dwarf2_evaluate_loc_desc (type, NULL,
10958 baton->data,
10959 baton->size,
10960 baton->per_cu);
10961 else if (bytes != NULL)
10962 {
10963 v = allocate_value (type);
10964 memcpy (value_contents_writeable (v), bytes,
10965 TYPE_LENGTH (type));
10966 }
10967 else
10968 v = value_from_longest (type, value);
10969
3e43a32a
MS
10970 /* Specify decimal so that we do not depend on
10971 the radix. */
98bfdba5
PA
10972 get_formatted_print_options (&opts, 'd');
10973 opts.raw = 1;
d7e74731 10974 value_print (v, &buf, &opts);
98bfdba5 10975 release_value (v);
98bfdba5
PA
10976 }
10977 }
10978
10979 die->building_fullname = 0;
10980
10981 if (!first)
10982 {
10983 /* Close the argument list, with a space if necessary
10984 (nested templates). */
d7e74731
PA
10985 if (!buf.empty () && buf.string ().back () == '>')
10986 buf.puts (" >");
98bfdba5 10987 else
d7e74731 10988 buf.puts (">");
98bfdba5
PA
10989 }
10990 }
10991
9c37b5ae 10992 /* For C++ methods, append formal parameter type
94af9270 10993 information, if PHYSNAME. */
6e70227d 10994
94af9270 10995 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10996 && cu->language == language_cplus)
94af9270
KS
10997 {
10998 struct type *type = read_type_die (die, cu);
10999
d7e74731 11000 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11001 &type_print_raw_options);
94af9270 11002
9c37b5ae 11003 if (cu->language == language_cplus)
94af9270 11004 {
60430eff
DJ
11005 /* Assume that an artificial first parameter is
11006 "this", but do not crash if it is not. RealView
11007 marks unnamed (and thus unused) parameters as
11008 artificial; there is no way to differentiate
11009 the two cases. */
94af9270
KS
11010 if (TYPE_NFIELDS (type) > 0
11011 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11012 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11013 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11014 0))))
d7e74731 11015 buf.puts (" const");
94af9270
KS
11016 }
11017 }
11018
d7e74731 11019 const std::string &intermediate_name = buf.string ();
94af9270
KS
11020
11021 if (cu->language == language_cplus)
34a68019 11022 canonical_name
322a8516 11023 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11024 &objfile->per_bfd->storage_obstack);
11025
11026 /* If we only computed INTERMEDIATE_NAME, or if
11027 INTERMEDIATE_NAME is already canonical, then we need to
11028 copy it to the appropriate obstack. */
322a8516 11029 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11030 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11031 intermediate_name);
34a68019
TT
11032 else
11033 name = canonical_name;
94af9270
KS
11034 }
11035 }
11036
11037 return name;
11038}
11039
0114d602
DJ
11040/* Return the fully qualified name of DIE, based on its DW_AT_name.
11041 If scope qualifiers are appropriate they will be added. The result
34a68019 11042 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11043 not have a name. NAME may either be from a previous call to
11044 dwarf2_name or NULL.
11045
9c37b5ae 11046 The output string will be canonicalized (if C++). */
0114d602
DJ
11047
11048static const char *
15d034d0 11049dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11050{
94af9270
KS
11051 return dwarf2_compute_name (name, die, cu, 0);
11052}
0114d602 11053
94af9270
KS
11054/* Construct a physname for the given DIE in CU. NAME may either be
11055 from a previous call to dwarf2_name or NULL. The result will be
11056 allocated on the objfile_objstack or NULL if the DIE does not have a
11057 name.
0114d602 11058
9c37b5ae 11059 The output string will be canonicalized (if C++). */
0114d602 11060
94af9270 11061static const char *
15d034d0 11062dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11063{
518817b3 11064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11065 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11066 int need_copy = 1;
11067
11068 /* In this case dwarf2_compute_name is just a shortcut not building anything
11069 on its own. */
11070 if (!die_needs_namespace (die, cu))
11071 return dwarf2_compute_name (name, die, cu, 1);
11072
73b9be8b 11073 mangled = dw2_linkage_name (die, cu);
900e11f9 11074
e98c9e7c
TT
11075 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11076 See https://github.com/rust-lang/rust/issues/32925. */
11077 if (cu->language == language_rust && mangled != NULL
11078 && strchr (mangled, '{') != NULL)
11079 mangled = NULL;
11080
900e11f9
JK
11081 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11082 has computed. */
791afaa2 11083 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11084 if (mangled != NULL)
900e11f9 11085 {
900e11f9 11086
59cc4834
JB
11087 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11088 {
11089 /* Do nothing (do not demangle the symbol name). */
11090 }
11091 else if (cu->language == language_go)
a766d390 11092 {
5e2db402
TT
11093 /* This is a lie, but we already lie to the caller new_symbol.
11094 new_symbol assumes we return the mangled name.
a766d390 11095 This just undoes that lie until things are cleaned up. */
a766d390
DE
11096 }
11097 else
11098 {
0eb876f5
JB
11099 /* Use DMGL_RET_DROP for C++ template functions to suppress
11100 their return type. It is easier for GDB users to search
11101 for such functions as `name(params)' than `long name(params)'.
11102 In such case the minimal symbol names do not match the full
11103 symbol names but for template functions there is never a need
11104 to look up their definition from their declaration so
11105 the only disadvantage remains the minimal symbol variant
11106 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11107 demangled.reset (gdb_demangle (mangled,
11108 (DMGL_PARAMS | DMGL_ANSI
11109 | DMGL_RET_DROP)));
a766d390 11110 }
900e11f9 11111 if (demangled)
791afaa2 11112 canon = demangled.get ();
900e11f9
JK
11113 else
11114 {
11115 canon = mangled;
11116 need_copy = 0;
11117 }
11118 }
11119
11120 if (canon == NULL || check_physname)
11121 {
11122 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11123
11124 if (canon != NULL && strcmp (physname, canon) != 0)
11125 {
11126 /* It may not mean a bug in GDB. The compiler could also
11127 compute DW_AT_linkage_name incorrectly. But in such case
11128 GDB would need to be bug-to-bug compatible. */
11129
b98664d3 11130 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11131 "(from linkage <%s>) - DIE at %s [in module %s]"),
11132 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11133 objfile_name (objfile));
900e11f9
JK
11134
11135 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11136 is available here - over computed PHYSNAME. It is safer
11137 against both buggy GDB and buggy compilers. */
11138
11139 retval = canon;
11140 }
11141 else
11142 {
11143 retval = physname;
11144 need_copy = 0;
11145 }
11146 }
11147 else
11148 retval = canon;
11149
11150 if (need_copy)
021887d8 11151 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11152
900e11f9 11153 return retval;
0114d602
DJ
11154}
11155
74921315
KS
11156/* Inspect DIE in CU for a namespace alias. If one exists, record
11157 a new symbol for it.
11158
11159 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11160
11161static int
11162read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11163{
11164 struct attribute *attr;
11165
11166 /* If the die does not have a name, this is not a namespace
11167 alias. */
11168 attr = dwarf2_attr (die, DW_AT_name, cu);
11169 if (attr != NULL)
11170 {
11171 int num;
11172 struct die_info *d = die;
11173 struct dwarf2_cu *imported_cu = cu;
11174
11175 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11176 keep inspecting DIEs until we hit the underlying import. */
11177#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11178 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11179 {
11180 attr = dwarf2_attr (d, DW_AT_import, cu);
11181 if (attr == NULL)
11182 break;
11183
11184 d = follow_die_ref (d, attr, &imported_cu);
11185 if (d->tag != DW_TAG_imported_declaration)
11186 break;
11187 }
11188
11189 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11190 {
b98664d3 11191 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11192 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11193 return 0;
11194 }
11195
11196 if (attr != NULL)
11197 {
11198 struct type *type;
9c541725 11199 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11200
9c541725 11201 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11202 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11203 {
11204 /* This declaration is a global namespace alias. Add
11205 a symbol for it whose type is the aliased namespace. */
11206 new_symbol (die, type, cu);
11207 return 1;
11208 }
11209 }
11210 }
11211
11212 return 0;
11213}
11214
22cee43f 11215/* Return the using directives repository (global or local?) to use in the
804d2729 11216 current context for CU.
22cee43f
PMR
11217
11218 For Ada, imported declarations can materialize renamings, which *may* be
11219 global. However it is impossible (for now?) in DWARF to distinguish
11220 "external" imported declarations and "static" ones. As all imported
11221 declarations seem to be static in all other languages, make them all CU-wide
11222 global only in Ada. */
11223
11224static struct using_direct **
804d2729 11225using_directives (struct dwarf2_cu *cu)
22cee43f 11226{
c24bdb02
KS
11227 if (cu->language == language_ada
11228 && cu->get_builder ()->outermost_context_p ())
11229 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11230 else
c24bdb02 11231 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11232}
11233
27aa8d6a
SW
11234/* Read the import statement specified by the given die and record it. */
11235
11236static void
11237read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11238{
518817b3 11239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11240 struct attribute *import_attr;
32019081 11241 struct die_info *imported_die, *child_die;
de4affc9 11242 struct dwarf2_cu *imported_cu;
27aa8d6a 11243 const char *imported_name;
794684b6 11244 const char *imported_name_prefix;
13387711
SW
11245 const char *canonical_name;
11246 const char *import_alias;
11247 const char *imported_declaration = NULL;
794684b6 11248 const char *import_prefix;
eb1e02fd 11249 std::vector<const char *> excludes;
13387711 11250
27aa8d6a
SW
11251 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11252 if (import_attr == NULL)
11253 {
b98664d3 11254 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11255 dwarf_tag_name (die->tag));
11256 return;
11257 }
11258
de4affc9
CC
11259 imported_cu = cu;
11260 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11261 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11262 if (imported_name == NULL)
11263 {
11264 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11265
11266 The import in the following code:
11267 namespace A
11268 {
11269 typedef int B;
11270 }
11271
11272 int main ()
11273 {
11274 using A::B;
11275 B b;
11276 return b;
11277 }
11278
11279 ...
11280 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11281 <52> DW_AT_decl_file : 1
11282 <53> DW_AT_decl_line : 6
11283 <54> DW_AT_import : <0x75>
11284 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11285 <59> DW_AT_name : B
11286 <5b> DW_AT_decl_file : 1
11287 <5c> DW_AT_decl_line : 2
11288 <5d> DW_AT_type : <0x6e>
11289 ...
11290 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11291 <76> DW_AT_byte_size : 4
11292 <77> DW_AT_encoding : 5 (signed)
11293
11294 imports the wrong die ( 0x75 instead of 0x58 ).
11295 This case will be ignored until the gcc bug is fixed. */
11296 return;
11297 }
11298
82856980
SW
11299 /* Figure out the local name after import. */
11300 import_alias = dwarf2_name (die, cu);
27aa8d6a 11301
794684b6
SW
11302 /* Figure out where the statement is being imported to. */
11303 import_prefix = determine_prefix (die, cu);
11304
11305 /* Figure out what the scope of the imported die is and prepend it
11306 to the name of the imported die. */
de4affc9 11307 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11308
f55ee35c
JK
11309 if (imported_die->tag != DW_TAG_namespace
11310 && imported_die->tag != DW_TAG_module)
794684b6 11311 {
13387711
SW
11312 imported_declaration = imported_name;
11313 canonical_name = imported_name_prefix;
794684b6 11314 }
13387711 11315 else if (strlen (imported_name_prefix) > 0)
12aaed36 11316 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11317 imported_name_prefix,
11318 (cu->language == language_d ? "." : "::"),
11319 imported_name, (char *) NULL);
13387711
SW
11320 else
11321 canonical_name = imported_name;
794684b6 11322
32019081
JK
11323 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11324 for (child_die = die->child; child_die && child_die->tag;
11325 child_die = sibling_die (child_die))
11326 {
11327 /* DWARF-4: A Fortran use statement with a “rename list” may be
11328 represented by an imported module entry with an import attribute
11329 referring to the module and owned entries corresponding to those
11330 entities that are renamed as part of being imported. */
11331
11332 if (child_die->tag != DW_TAG_imported_declaration)
11333 {
b98664d3 11334 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11335 "- DIE at %s [in module %s]"),
11336 sect_offset_str (child_die->sect_off),
11337 objfile_name (objfile));
32019081
JK
11338 continue;
11339 }
11340
11341 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11342 if (import_attr == NULL)
11343 {
b98664d3 11344 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11345 dwarf_tag_name (child_die->tag));
11346 continue;
11347 }
11348
11349 imported_cu = cu;
11350 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11351 &imported_cu);
11352 imported_name = dwarf2_name (imported_die, imported_cu);
11353 if (imported_name == NULL)
11354 {
b98664d3 11355 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11356 "imported name - DIE at %s [in module %s]"),
11357 sect_offset_str (child_die->sect_off),
11358 objfile_name (objfile));
32019081
JK
11359 continue;
11360 }
11361
eb1e02fd 11362 excludes.push_back (imported_name);
32019081
JK
11363
11364 process_die (child_die, cu);
11365 }
11366
804d2729 11367 add_using_directive (using_directives (cu),
22cee43f
PMR
11368 import_prefix,
11369 canonical_name,
11370 import_alias,
11371 imported_declaration,
11372 excludes,
11373 0,
11374 &objfile->objfile_obstack);
27aa8d6a
SW
11375}
11376
5230b05a
WT
11377/* ICC<14 does not output the required DW_AT_declaration on incomplete
11378 types, but gives them a size of zero. Starting with version 14,
11379 ICC is compatible with GCC. */
11380
9068261f 11381static bool
5230b05a
WT
11382producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11383{
11384 if (!cu->checked_producer)
11385 check_producer (cu);
11386
11387 return cu->producer_is_icc_lt_14;
11388}
11389
eb77c9df
AB
11390/* ICC generates a DW_AT_type for C void functions. This was observed on
11391 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11392 which says that void functions should not have a DW_AT_type. */
11393
11394static bool
11395producer_is_icc (struct dwarf2_cu *cu)
11396{
11397 if (!cu->checked_producer)
11398 check_producer (cu);
11399
11400 return cu->producer_is_icc;
11401}
11402
1b80a9fa
JK
11403/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11404 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11405 this, it was first present in GCC release 4.3.0. */
11406
9068261f 11407static bool
1b80a9fa
JK
11408producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11409{
11410 if (!cu->checked_producer)
11411 check_producer (cu);
11412
11413 return cu->producer_is_gcc_lt_4_3;
11414}
11415
d721ba37
PA
11416static file_and_directory
11417find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11418{
d721ba37
PA
11419 file_and_directory res;
11420
9291a0cd
TT
11421 /* Find the filename. Do not use dwarf2_name here, since the filename
11422 is not a source language identifier. */
d721ba37
PA
11423 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11424 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11425
d721ba37
PA
11426 if (res.comp_dir == NULL
11427 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11428 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11429 {
d721ba37
PA
11430 res.comp_dir_storage = ldirname (res.name);
11431 if (!res.comp_dir_storage.empty ())
11432 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11433 }
d721ba37 11434 if (res.comp_dir != NULL)
9291a0cd
TT
11435 {
11436 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11437 directory, get rid of it. */
d721ba37 11438 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11439
d721ba37
PA
11440 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11441 res.comp_dir = cp + 1;
9291a0cd
TT
11442 }
11443
d721ba37
PA
11444 if (res.name == NULL)
11445 res.name = "<unknown>";
11446
11447 return res;
9291a0cd
TT
11448}
11449
f4dc4d17
DE
11450/* Handle DW_AT_stmt_list for a compilation unit.
11451 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11452 COMP_DIR is the compilation directory. LOWPC is passed to
11453 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11454
11455static void
11456handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11457 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11458{
518817b3
SM
11459 struct dwarf2_per_objfile *dwarf2_per_objfile
11460 = cu->per_cu->dwarf2_per_objfile;
527f3840 11461 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11462 struct attribute *attr;
527f3840
JK
11463 struct line_header line_header_local;
11464 hashval_t line_header_local_hash;
527f3840
JK
11465 void **slot;
11466 int decode_mapping;
2ab95328 11467
f4dc4d17
DE
11468 gdb_assert (! cu->per_cu->is_debug_types);
11469
2ab95328 11470 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11471 if (attr == NULL)
11472 return;
11473
9c541725 11474 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11475
11476 /* The line header hash table is only created if needed (it exists to
11477 prevent redundant reading of the line table for partial_units).
11478 If we're given a partial_unit, we'll need it. If we're given a
11479 compile_unit, then use the line header hash table if it's already
11480 created, but don't create one just yet. */
11481
11482 if (dwarf2_per_objfile->line_header_hash == NULL
11483 && die->tag == DW_TAG_partial_unit)
2ab95328 11484 {
527f3840
JK
11485 dwarf2_per_objfile->line_header_hash
11486 = htab_create_alloc_ex (127, line_header_hash_voidp,
11487 line_header_eq_voidp,
11488 free_line_header_voidp,
11489 &objfile->objfile_obstack,
11490 hashtab_obstack_allocate,
11491 dummy_obstack_deallocate);
11492 }
2ab95328 11493
9c541725 11494 line_header_local.sect_off = line_offset;
527f3840
JK
11495 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11496 line_header_local_hash = line_header_hash (&line_header_local);
11497 if (dwarf2_per_objfile->line_header_hash != NULL)
11498 {
11499 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11500 &line_header_local,
11501 line_header_local_hash, NO_INSERT);
11502
11503 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11504 is not present in *SLOT (since if there is something in *SLOT then
11505 it will be for a partial_unit). */
11506 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11507 {
527f3840 11508 gdb_assert (*slot != NULL);
9a3c8263 11509 cu->line_header = (struct line_header *) *slot;
527f3840 11510 return;
dee91e82 11511 }
2ab95328 11512 }
527f3840
JK
11513
11514 /* dwarf_decode_line_header does not yet provide sufficient information.
11515 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11516 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11517 if (lh == NULL)
527f3840 11518 return;
4c8aa72d
PA
11519
11520 cu->line_header = lh.release ();
11521 cu->line_header_die_owner = die;
527f3840
JK
11522
11523 if (dwarf2_per_objfile->line_header_hash == NULL)
11524 slot = NULL;
11525 else
11526 {
11527 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11528 &line_header_local,
11529 line_header_local_hash, INSERT);
11530 gdb_assert (slot != NULL);
11531 }
11532 if (slot != NULL && *slot == NULL)
11533 {
11534 /* This newly decoded line number information unit will be owned
11535 by line_header_hash hash table. */
11536 *slot = cu->line_header;
4c8aa72d 11537 cu->line_header_die_owner = NULL;
527f3840
JK
11538 }
11539 else
11540 {
11541 /* We cannot free any current entry in (*slot) as that struct line_header
11542 may be already used by multiple CUs. Create only temporary decoded
11543 line_header for this CU - it may happen at most once for each line
11544 number information unit. And if we're not using line_header_hash
11545 then this is what we want as well. */
11546 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11547 }
11548 decode_mapping = (die->tag != DW_TAG_partial_unit);
11549 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11550 decode_mapping);
fff8551c 11551
2ab95328
TT
11552}
11553
95554aad 11554/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11555
c906108c 11556static void
e7c27a73 11557read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11558{
518817b3
SM
11559 struct dwarf2_per_objfile *dwarf2_per_objfile
11560 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11561 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11562 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11563 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11564 CORE_ADDR highpc = ((CORE_ADDR) 0);
11565 struct attribute *attr;
c906108c 11566 struct die_info *child_die;
e142c38c 11567 CORE_ADDR baseaddr;
6e70227d 11568
380618d6 11569 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11571
fae299cd 11572 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11573
11574 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11575 from finish_block. */
2acceee2 11576 if (lowpc == ((CORE_ADDR) -1))
c906108c 11577 lowpc = highpc;
3e29f34a 11578 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11579
d721ba37 11580 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11581
f4b8a18d
KW
11582 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11583 standardised yet. As a workaround for the language detection we fall
11584 back to the DW_AT_producer string. */
11585 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11586 cu->language = language_opencl;
11587
3019eac3
DE
11588 /* Similar hack for Go. */
11589 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11590 set_cu_language (DW_LANG_Go, cu);
11591
c24bdb02 11592 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11593
11594 /* Decode line number information if present. We do this before
11595 processing child DIEs, so that the line header table is available
11596 for DW_AT_decl_file. */
d721ba37 11597 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11598
11599 /* Process all dies in compilation unit. */
11600 if (die->child != NULL)
11601 {
11602 child_die = die->child;
11603 while (child_die && child_die->tag)
11604 {
11605 process_die (child_die, cu);
11606 child_die = sibling_die (child_die);
11607 }
11608 }
11609
11610 /* Decode macro information, if present. Dwarf 2 macro information
11611 refers to information in the line number info statement program
11612 header, so we can only read it if we've read the header
11613 successfully. */
0af92d60
JK
11614 attr = dwarf2_attr (die, DW_AT_macros, cu);
11615 if (attr == NULL)
11616 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11617 if (attr && cu->line_header)
11618 {
11619 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11620 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11621
43f3e411 11622 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11623 }
11624 else
11625 {
11626 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11627 if (attr && cu->line_header)
11628 {
11629 unsigned int macro_offset = DW_UNSND (attr);
11630
43f3e411 11631 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11632 }
11633 }
3019eac3
DE
11634}
11635
c24bdb02
KS
11636void
11637dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11638{
f4dc4d17
DE
11639 struct type_unit_group *tu_group;
11640 int first_time;
3019eac3 11641 struct attribute *attr;
9c541725 11642 unsigned int i;
0186c6a7 11643 struct signatured_type *sig_type;
3019eac3 11644
f4dc4d17 11645 gdb_assert (per_cu->is_debug_types);
0186c6a7 11646 sig_type = (struct signatured_type *) per_cu;
3019eac3 11647
c24bdb02 11648 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11649
f4dc4d17 11650 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11651 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11652 if (sig_type->type_unit_group == NULL)
c24bdb02 11653 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11654 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11655
11656 /* If we've already processed this stmt_list there's no real need to
11657 do it again, we could fake it and just recreate the part we need
11658 (file name,index -> symtab mapping). If data shows this optimization
11659 is useful we can do it then. */
43f3e411 11660 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11661
11662 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11663 debug info. */
fff8551c 11664 line_header_up lh;
f4dc4d17 11665 if (attr != NULL)
3019eac3 11666 {
9c541725 11667 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11668 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11669 }
11670 if (lh == NULL)
11671 {
11672 if (first_time)
c24bdb02 11673 start_symtab ("", NULL, 0);
f4dc4d17
DE
11674 else
11675 {
11676 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11677 gdb_assert (m_builder == nullptr);
804d2729 11678 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11679 m_builder.reset (new struct buildsym_compunit
11680 (COMPUNIT_OBJFILE (cust), "",
11681 COMPUNIT_DIRNAME (cust),
11682 compunit_language (cust),
11683 0, cust));
f4dc4d17 11684 }
f4dc4d17 11685 return;
3019eac3
DE
11686 }
11687
c24bdb02
KS
11688 line_header = lh.release ();
11689 line_header_die_owner = die;
3019eac3 11690
f4dc4d17
DE
11691 if (first_time)
11692 {
c24bdb02 11693 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11694
1fd60fc0
DE
11695 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11696 still initializing it, and our caller (a few levels up)
11697 process_full_type_unit still needs to know if this is the first
11698 time. */
11699
c24bdb02 11700 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11701 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11702 line_header->file_names.size ());
3019eac3 11703
c24bdb02 11704 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11705 {
c24bdb02 11706 file_entry &fe = line_header->file_names[i];
3019eac3 11707
c24bdb02
KS
11708 dwarf2_start_subfile (this, fe.name,
11709 fe.include_dir (line_header));
11710 buildsym_compunit *b = get_builder ();
11711 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11712 {
4c8aa72d
PA
11713 /* NOTE: start_subfile will recognize when it's been
11714 passed a file it has already seen. So we can't
11715 assume there's a simple mapping from
11716 cu->line_header->file_names to subfiles, plus
11717 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11718 b->get_current_subfile ()->symtab
11719 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11720 }
11721
c24bdb02 11722 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11723 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11724 }
11725 }
11726 else
3019eac3 11727 {
c24bdb02 11728 gdb_assert (m_builder == nullptr);
804d2729 11729 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11730 m_builder.reset (new struct buildsym_compunit
11731 (COMPUNIT_OBJFILE (cust), "",
11732 COMPUNIT_DIRNAME (cust),
11733 compunit_language (cust),
11734 0, cust));
f4dc4d17 11735
c24bdb02 11736 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11737 {
c24bdb02 11738 file_entry &fe = line_header->file_names[i];
f4dc4d17 11739
4c8aa72d 11740 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11741 }
3019eac3
DE
11742 }
11743
f4dc4d17
DE
11744 /* The main symtab is allocated last. Type units don't have DW_AT_name
11745 so they don't have a "real" (so to speak) symtab anyway.
11746 There is later code that will assign the main symtab to all symbols
11747 that don't have one. We need to handle the case of a symbol with a
11748 missing symtab (DW_AT_decl_file) anyway. */
11749}
3019eac3 11750
f4dc4d17
DE
11751/* Process DW_TAG_type_unit.
11752 For TUs we want to skip the first top level sibling if it's not the
11753 actual type being defined by this TU. In this case the first top
11754 level sibling is there to provide context only. */
3019eac3 11755
f4dc4d17
DE
11756static void
11757read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11758{
11759 struct die_info *child_die;
3019eac3 11760
f4dc4d17
DE
11761 prepare_one_comp_unit (cu, die, language_minimal);
11762
11763 /* Initialize (or reinitialize) the machinery for building symtabs.
11764 We do this before processing child DIEs, so that the line header table
11765 is available for DW_AT_decl_file. */
c24bdb02 11766 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11767
11768 if (die->child != NULL)
11769 {
11770 child_die = die->child;
11771 while (child_die && child_die->tag)
11772 {
11773 process_die (child_die, cu);
11774 child_die = sibling_die (child_die);
11775 }
11776 }
3019eac3
DE
11777}
11778\f
80626a55
DE
11779/* DWO/DWP files.
11780
11781 http://gcc.gnu.org/wiki/DebugFission
11782 http://gcc.gnu.org/wiki/DebugFissionDWP
11783
11784 To simplify handling of both DWO files ("object" files with the DWARF info)
11785 and DWP files (a file with the DWOs packaged up into one file), we treat
11786 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11787
11788static hashval_t
11789hash_dwo_file (const void *item)
11790{
9a3c8263 11791 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11792 hashval_t hash;
3019eac3 11793
a2ce51a0
DE
11794 hash = htab_hash_string (dwo_file->dwo_name);
11795 if (dwo_file->comp_dir != NULL)
11796 hash += htab_hash_string (dwo_file->comp_dir);
11797 return hash;
3019eac3
DE
11798}
11799
11800static int
11801eq_dwo_file (const void *item_lhs, const void *item_rhs)
11802{
9a3c8263
SM
11803 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11804 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11805
a2ce51a0
DE
11806 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11807 return 0;
11808 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11809 return lhs->comp_dir == rhs->comp_dir;
11810 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11811}
11812
11813/* Allocate a hash table for DWO files. */
11814
51ac9db5 11815static htab_up
ed2dc618 11816allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11817{
51ac9db5
SM
11818 auto delete_dwo_file = [] (void *item)
11819 {
11820 struct dwo_file *dwo_file = (struct dwo_file *) item;
11821
11822 delete dwo_file;
11823 };
11824
11825 return htab_up (htab_create_alloc_ex (41,
11826 hash_dwo_file,
11827 eq_dwo_file,
11828 delete_dwo_file,
11829 &objfile->objfile_obstack,
11830 hashtab_obstack_allocate,
11831 dummy_obstack_deallocate));
3019eac3
DE
11832}
11833
80626a55
DE
11834/* Lookup DWO file DWO_NAME. */
11835
11836static void **
ed2dc618
SM
11837lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11838 const char *dwo_name,
11839 const char *comp_dir)
80626a55
DE
11840{
11841 struct dwo_file find_entry;
11842 void **slot;
11843
11844 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11845 dwarf2_per_objfile->dwo_files
11846 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11847
0ac5b59e
DE
11848 find_entry.dwo_name = dwo_name;
11849 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11850 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11851 INSERT);
80626a55
DE
11852
11853 return slot;
11854}
11855
3019eac3
DE
11856static hashval_t
11857hash_dwo_unit (const void *item)
11858{
9a3c8263 11859 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11860
11861 /* This drops the top 32 bits of the id, but is ok for a hash. */
11862 return dwo_unit->signature;
11863}
11864
11865static int
11866eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11867{
9a3c8263
SM
11868 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11869 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11870
11871 /* The signature is assumed to be unique within the DWO file.
11872 So while object file CU dwo_id's always have the value zero,
11873 that's OK, assuming each object file DWO file has only one CU,
11874 and that's the rule for now. */
11875 return lhs->signature == rhs->signature;
11876}
11877
11878/* Allocate a hash table for DWO CUs,TUs.
11879 There is one of these tables for each of CUs,TUs for each DWO file. */
11880
11881static htab_t
11882allocate_dwo_unit_table (struct objfile *objfile)
11883{
11884 /* Start out with a pretty small number.
11885 Generally DWO files contain only one CU and maybe some TUs. */
11886 return htab_create_alloc_ex (3,
11887 hash_dwo_unit,
11888 eq_dwo_unit,
11889 NULL,
11890 &objfile->objfile_obstack,
11891 hashtab_obstack_allocate,
11892 dummy_obstack_deallocate);
11893}
11894
80626a55 11895/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11896
19c3d4c9 11897struct create_dwo_cu_data
3019eac3
DE
11898{
11899 struct dwo_file *dwo_file;
19c3d4c9 11900 struct dwo_unit dwo_unit;
3019eac3
DE
11901};
11902
19c3d4c9 11903/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11904
11905static void
19c3d4c9
DE
11906create_dwo_cu_reader (const struct die_reader_specs *reader,
11907 const gdb_byte *info_ptr,
11908 struct die_info *comp_unit_die,
11909 int has_children,
11910 void *datap)
3019eac3
DE
11911{
11912 struct dwarf2_cu *cu = reader->cu;
9c541725 11913 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11914 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11915 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11916 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11917 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11918
a084a2a6
AT
11919 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11920 if (!signature.has_value ())
3019eac3 11921 {
b98664d3 11922 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11923 " its dwo_id [in module %s]"),
9d8780f0 11924 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11925 return;
11926 }
11927
3019eac3 11928 dwo_unit->dwo_file = dwo_file;
a084a2a6 11929 dwo_unit->signature = *signature;
8a0459fd 11930 dwo_unit->section = section;
9c541725 11931 dwo_unit->sect_off = sect_off;
3019eac3
DE
11932 dwo_unit->length = cu->per_cu->length;
11933
b4f54984 11934 if (dwarf_read_debug)
9d8780f0
SM
11935 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11936 sect_offset_str (sect_off),
9c541725 11937 hex_string (dwo_unit->signature));
3019eac3
DE
11938}
11939
33c5cd75 11940/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11941 Note: This function processes DWO files only, not DWP files. */
3019eac3 11942
33c5cd75 11943static void
ed2dc618
SM
11944create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11945 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11946 htab_t &cus_htab)
3019eac3
DE
11947{
11948 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11949 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11950
33c5cd75
DB
11951 dwarf2_read_section (objfile, &section);
11952 info_ptr = section.buffer;
3019eac3
DE
11953
11954 if (info_ptr == NULL)
33c5cd75 11955 return;
3019eac3 11956
b4f54984 11957 if (dwarf_read_debug)
19c3d4c9
DE
11958 {
11959 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11960 get_section_name (&section),
11961 get_section_file_name (&section));
19c3d4c9 11962 }
3019eac3 11963
33c5cd75 11964 end_ptr = info_ptr + section.size;
3019eac3
DE
11965 while (info_ptr < end_ptr)
11966 {
11967 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11968 struct create_dwo_cu_data create_dwo_cu_data;
11969 struct dwo_unit *dwo_unit;
11970 void **slot;
11971 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11972
19c3d4c9
DE
11973 memset (&create_dwo_cu_data.dwo_unit, 0,
11974 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11975 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11976 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11977 per_cu.is_debug_types = 0;
33c5cd75
DB
11978 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11979 per_cu.section = &section;
c5ed0576 11980 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11981
11982 init_cutu_and_read_dies_no_follow (
11983 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11984 info_ptr += per_cu.length;
11985
11986 // If the unit could not be parsed, skip it.
11987 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11988 continue;
3019eac3 11989
33c5cd75
DB
11990 if (cus_htab == NULL)
11991 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11992
33c5cd75
DB
11993 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11994 *dwo_unit = create_dwo_cu_data.dwo_unit;
11995 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11996 gdb_assert (slot != NULL);
11997 if (*slot != NULL)
19c3d4c9 11998 {
33c5cd75
DB
11999 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12000 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12001
b98664d3 12002 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12003 " the entry at offset %s, signature %s"),
12004 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12005 hex_string (dwo_unit->signature));
19c3d4c9 12006 }
33c5cd75 12007 *slot = (void *)dwo_unit;
3019eac3 12008 }
3019eac3
DE
12009}
12010
80626a55
DE
12011/* DWP file .debug_{cu,tu}_index section format:
12012 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12013
d2415c6c
DE
12014 DWP Version 1:
12015
80626a55
DE
12016 Both index sections have the same format, and serve to map a 64-bit
12017 signature to a set of section numbers. Each section begins with a header,
12018 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12019 indexes, and a pool of 32-bit section numbers. The index sections will be
12020 aligned at 8-byte boundaries in the file.
12021
d2415c6c
DE
12022 The index section header consists of:
12023
12024 V, 32 bit version number
12025 -, 32 bits unused
12026 N, 32 bit number of compilation units or type units in the index
12027 M, 32 bit number of slots in the hash table
80626a55 12028
d2415c6c 12029 Numbers are recorded using the byte order of the application binary.
80626a55 12030
d2415c6c
DE
12031 The hash table begins at offset 16 in the section, and consists of an array
12032 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12033 order of the application binary). Unused slots in the hash table are 0.
12034 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12035
d2415c6c
DE
12036 The parallel table begins immediately after the hash table
12037 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12038 array of 32-bit indexes (using the byte order of the application binary),
12039 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12040 table contains a 32-bit index into the pool of section numbers. For unused
12041 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12042
73869dc2
DE
12043 The pool of section numbers begins immediately following the hash table
12044 (at offset 16 + 12 * M from the beginning of the section). The pool of
12045 section numbers consists of an array of 32-bit words (using the byte order
12046 of the application binary). Each item in the array is indexed starting
12047 from 0. The hash table entry provides the index of the first section
12048 number in the set. Additional section numbers in the set follow, and the
12049 set is terminated by a 0 entry (section number 0 is not used in ELF).
12050
12051 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12052 section must be the first entry in the set, and the .debug_abbrev.dwo must
12053 be the second entry. Other members of the set may follow in any order.
12054
12055 ---
12056
12057 DWP Version 2:
12058
12059 DWP Version 2 combines all the .debug_info, etc. sections into one,
12060 and the entries in the index tables are now offsets into these sections.
12061 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12062 section.
12063
12064 Index Section Contents:
12065 Header
12066 Hash Table of Signatures dwp_hash_table.hash_table
12067 Parallel Table of Indices dwp_hash_table.unit_table
12068 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12069 Table of Section Sizes dwp_hash_table.v2.sizes
12070
12071 The index section header consists of:
12072
12073 V, 32 bit version number
12074 L, 32 bit number of columns in the table of section offsets
12075 N, 32 bit number of compilation units or type units in the index
12076 M, 32 bit number of slots in the hash table
12077
12078 Numbers are recorded using the byte order of the application binary.
12079
12080 The hash table has the same format as version 1.
12081 The parallel table of indices has the same format as version 1,
12082 except that the entries are origin-1 indices into the table of sections
12083 offsets and the table of section sizes.
12084
12085 The table of offsets begins immediately following the parallel table
12086 (at offset 16 + 12 * M from the beginning of the section). The table is
12087 a two-dimensional array of 32-bit words (using the byte order of the
12088 application binary), with L columns and N+1 rows, in row-major order.
12089 Each row in the array is indexed starting from 0. The first row provides
12090 a key to the remaining rows: each column in this row provides an identifier
12091 for a debug section, and the offsets in the same column of subsequent rows
12092 refer to that section. The section identifiers are:
12093
12094 DW_SECT_INFO 1 .debug_info.dwo
12095 DW_SECT_TYPES 2 .debug_types.dwo
12096 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12097 DW_SECT_LINE 4 .debug_line.dwo
12098 DW_SECT_LOC 5 .debug_loc.dwo
12099 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12100 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12101 DW_SECT_MACRO 8 .debug_macro.dwo
12102
12103 The offsets provided by the CU and TU index sections are the base offsets
12104 for the contributions made by each CU or TU to the corresponding section
12105 in the package file. Each CU and TU header contains an abbrev_offset
12106 field, used to find the abbreviations table for that CU or TU within the
12107 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12108 be interpreted as relative to the base offset given in the index section.
12109 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12110 should be interpreted as relative to the base offset for .debug_line.dwo,
12111 and offsets into other debug sections obtained from DWARF attributes should
12112 also be interpreted as relative to the corresponding base offset.
12113
12114 The table of sizes begins immediately following the table of offsets.
12115 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12116 with L columns and N rows, in row-major order. Each row in the array is
12117 indexed starting from 1 (row 0 is shared by the two tables).
12118
12119 ---
12120
12121 Hash table lookup is handled the same in version 1 and 2:
12122
12123 We assume that N and M will not exceed 2^32 - 1.
12124 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12125
d2415c6c
DE
12126 Given a 64-bit compilation unit signature or a type signature S, an entry
12127 in the hash table is located as follows:
80626a55 12128
d2415c6c
DE
12129 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12130 the low-order k bits all set to 1.
80626a55 12131
d2415c6c 12132 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12133
d2415c6c
DE
12134 3) If the hash table entry at index H matches the signature, use that
12135 entry. If the hash table entry at index H is unused (all zeroes),
12136 terminate the search: the signature is not present in the table.
80626a55 12137
d2415c6c 12138 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12139
d2415c6c 12140 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12141 to stop at an unused slot or find the match. */
80626a55
DE
12142
12143/* Create a hash table to map DWO IDs to their CU/TU entry in
12144 .debug_{info,types}.dwo in DWP_FILE.
12145 Returns NULL if there isn't one.
12146 Note: This function processes DWP files only, not DWO files. */
12147
12148static struct dwp_hash_table *
ed2dc618
SM
12149create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12150 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12151{
12152 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12153 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12154 const gdb_byte *index_ptr, *index_end;
80626a55 12155 struct dwarf2_section_info *index;
73869dc2 12156 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12157 struct dwp_hash_table *htab;
12158
12159 if (is_debug_types)
12160 index = &dwp_file->sections.tu_index;
12161 else
12162 index = &dwp_file->sections.cu_index;
12163
12164 if (dwarf2_section_empty_p (index))
12165 return NULL;
12166 dwarf2_read_section (objfile, index);
12167
12168 index_ptr = index->buffer;
12169 index_end = index_ptr + index->size;
12170
12171 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12172 index_ptr += 4;
12173 if (version == 2)
12174 nr_columns = read_4_bytes (dbfd, index_ptr);
12175 else
12176 nr_columns = 0;
12177 index_ptr += 4;
80626a55
DE
12178 nr_units = read_4_bytes (dbfd, index_ptr);
12179 index_ptr += 4;
12180 nr_slots = read_4_bytes (dbfd, index_ptr);
12181 index_ptr += 4;
12182
73869dc2 12183 if (version != 1 && version != 2)
80626a55 12184 {
21aa081e 12185 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12186 " [in module %s]"),
21aa081e 12187 pulongest (version), dwp_file->name);
80626a55
DE
12188 }
12189 if (nr_slots != (nr_slots & -nr_slots))
12190 {
21aa081e 12191 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12192 " is not power of 2 [in module %s]"),
21aa081e 12193 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12194 }
12195
12196 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12197 htab->version = version;
12198 htab->nr_columns = nr_columns;
80626a55
DE
12199 htab->nr_units = nr_units;
12200 htab->nr_slots = nr_slots;
12201 htab->hash_table = index_ptr;
12202 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12203
12204 /* Exit early if the table is empty. */
12205 if (nr_slots == 0 || nr_units == 0
12206 || (version == 2 && nr_columns == 0))
12207 {
12208 /* All must be zero. */
12209 if (nr_slots != 0 || nr_units != 0
12210 || (version == 2 && nr_columns != 0))
12211 {
b98664d3 12212 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12213 " all zero [in modules %s]"),
12214 dwp_file->name);
12215 }
12216 return htab;
12217 }
12218
12219 if (version == 1)
12220 {
12221 htab->section_pool.v1.indices =
12222 htab->unit_table + sizeof (uint32_t) * nr_slots;
12223 /* It's harder to decide whether the section is too small in v1.
12224 V1 is deprecated anyway so we punt. */
12225 }
12226 else
12227 {
12228 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12229 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12230 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12231 /* Reverse map for error checking. */
12232 int ids_seen[DW_SECT_MAX + 1];
12233 int i;
12234
12235 if (nr_columns < 2)
12236 {
12237 error (_("Dwarf Error: bad DWP hash table, too few columns"
12238 " in section table [in module %s]"),
12239 dwp_file->name);
12240 }
12241 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12242 {
12243 error (_("Dwarf Error: bad DWP hash table, too many columns"
12244 " in section table [in module %s]"),
12245 dwp_file->name);
12246 }
04fd5eed
GB
12247 memset (ids, 255, sizeof_ids);
12248 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12249 for (i = 0; i < nr_columns; ++i)
12250 {
12251 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12252
12253 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12254 {
12255 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12256 " in section table [in module %s]"),
12257 id, dwp_file->name);
12258 }
12259 if (ids_seen[id] != -1)
12260 {
12261 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12262 " id %d in section table [in module %s]"),
12263 id, dwp_file->name);
12264 }
12265 ids_seen[id] = i;
12266 ids[i] = id;
12267 }
12268 /* Must have exactly one info or types section. */
12269 if (((ids_seen[DW_SECT_INFO] != -1)
12270 + (ids_seen[DW_SECT_TYPES] != -1))
12271 != 1)
12272 {
12273 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12274 " DWO info/types section [in module %s]"),
12275 dwp_file->name);
12276 }
12277 /* Must have an abbrev section. */
12278 if (ids_seen[DW_SECT_ABBREV] == -1)
12279 {
12280 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12281 " section [in module %s]"),
12282 dwp_file->name);
12283 }
12284 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12285 htab->section_pool.v2.sizes =
12286 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12287 * nr_units * nr_columns);
12288 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12289 * nr_units * nr_columns))
12290 > index_end)
12291 {
12292 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12293 " [in module %s]"),
12294 dwp_file->name);
12295 }
12296 }
80626a55
DE
12297
12298 return htab;
12299}
12300
12301/* Update SECTIONS with the data from SECTP.
12302
12303 This function is like the other "locate" section routines that are
12304 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12305 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12306
12307 The result is non-zero for success, or zero if an error was found. */
12308
12309static int
73869dc2
DE
12310locate_v1_virtual_dwo_sections (asection *sectp,
12311 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12312{
12313 const struct dwop_section_names *names = &dwop_section_names;
12314
12315 if (section_is_p (sectp->name, &names->abbrev_dwo))
12316 {
12317 /* There can be only one. */
049412e3 12318 if (sections->abbrev.s.section != NULL)
80626a55 12319 return 0;
049412e3 12320 sections->abbrev.s.section = sectp;
fd361982 12321 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12322 }
12323 else if (section_is_p (sectp->name, &names->info_dwo)
12324 || section_is_p (sectp->name, &names->types_dwo))
12325 {
12326 /* There can be only one. */
049412e3 12327 if (sections->info_or_types.s.section != NULL)
80626a55 12328 return 0;
049412e3 12329 sections->info_or_types.s.section = sectp;
fd361982 12330 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12331 }
12332 else if (section_is_p (sectp->name, &names->line_dwo))
12333 {
12334 /* There can be only one. */
049412e3 12335 if (sections->line.s.section != NULL)
80626a55 12336 return 0;
049412e3 12337 sections->line.s.section = sectp;
fd361982 12338 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12339 }
12340 else if (section_is_p (sectp->name, &names->loc_dwo))
12341 {
12342 /* There can be only one. */
049412e3 12343 if (sections->loc.s.section != NULL)
80626a55 12344 return 0;
049412e3 12345 sections->loc.s.section = sectp;
fd361982 12346 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12347 }
12348 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12349 {
12350 /* There can be only one. */
049412e3 12351 if (sections->macinfo.s.section != NULL)
80626a55 12352 return 0;
049412e3 12353 sections->macinfo.s.section = sectp;
fd361982 12354 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12355 }
12356 else if (section_is_p (sectp->name, &names->macro_dwo))
12357 {
12358 /* There can be only one. */
049412e3 12359 if (sections->macro.s.section != NULL)
80626a55 12360 return 0;
049412e3 12361 sections->macro.s.section = sectp;
fd361982 12362 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12363 }
12364 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12365 {
12366 /* There can be only one. */
049412e3 12367 if (sections->str_offsets.s.section != NULL)
80626a55 12368 return 0;
049412e3 12369 sections->str_offsets.s.section = sectp;
fd361982 12370 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12371 }
12372 else
12373 {
12374 /* No other kind of section is valid. */
12375 return 0;
12376 }
12377
12378 return 1;
12379}
12380
73869dc2
DE
12381/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12382 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12383 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12384 This is for DWP version 1 files. */
80626a55
DE
12385
12386static struct dwo_unit *
ed2dc618
SM
12387create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12388 struct dwp_file *dwp_file,
73869dc2
DE
12389 uint32_t unit_index,
12390 const char *comp_dir,
12391 ULONGEST signature, int is_debug_types)
80626a55
DE
12392{
12393 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12394 const struct dwp_hash_table *dwp_htab =
12395 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12396 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12397 const char *kind = is_debug_types ? "TU" : "CU";
12398 struct dwo_file *dwo_file;
12399 struct dwo_unit *dwo_unit;
73869dc2 12400 struct virtual_v1_dwo_sections sections;
80626a55 12401 void **dwo_file_slot;
80626a55
DE
12402 int i;
12403
73869dc2
DE
12404 gdb_assert (dwp_file->version == 1);
12405
b4f54984 12406 if (dwarf_read_debug)
80626a55 12407 {
73869dc2 12408 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12409 kind,
73869dc2 12410 pulongest (unit_index), hex_string (signature),
80626a55
DE
12411 dwp_file->name);
12412 }
12413
19ac8c2e 12414 /* Fetch the sections of this DWO unit.
80626a55
DE
12415 Put a limit on the number of sections we look for so that bad data
12416 doesn't cause us to loop forever. */
12417
73869dc2 12418#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12419 (1 /* .debug_info or .debug_types */ \
12420 + 1 /* .debug_abbrev */ \
12421 + 1 /* .debug_line */ \
12422 + 1 /* .debug_loc */ \
12423 + 1 /* .debug_str_offsets */ \
19ac8c2e 12424 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12425 + 1 /* trailing zero */)
12426
12427 memset (&sections, 0, sizeof (sections));
80626a55 12428
73869dc2 12429 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12430 {
12431 asection *sectp;
12432 uint32_t section_nr =
12433 read_4_bytes (dbfd,
73869dc2
DE
12434 dwp_htab->section_pool.v1.indices
12435 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12436
12437 if (section_nr == 0)
12438 break;
12439 if (section_nr >= dwp_file->num_sections)
12440 {
12441 error (_("Dwarf Error: bad DWP hash table, section number too large"
12442 " [in module %s]"),
12443 dwp_file->name);
12444 }
12445
12446 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12447 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12448 {
12449 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12450 " [in module %s]"),
12451 dwp_file->name);
12452 }
12453 }
12454
12455 if (i < 2
a32a8923
DE
12456 || dwarf2_section_empty_p (&sections.info_or_types)
12457 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12458 {
12459 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12460 " [in module %s]"),
12461 dwp_file->name);
12462 }
73869dc2 12463 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12464 {
12465 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12466 " [in module %s]"),
12467 dwp_file->name);
12468 }
12469
12470 /* It's easier for the rest of the code if we fake a struct dwo_file and
12471 have dwo_unit "live" in that. At least for now.
12472
12473 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12474 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12475 file, we can combine them back into a virtual DWO file to save space
12476 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12477 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12478
791afaa2
TT
12479 std::string virtual_dwo_name =
12480 string_printf ("virtual-dwo/%d-%d-%d-%d",
12481 get_section_id (&sections.abbrev),
12482 get_section_id (&sections.line),
12483 get_section_id (&sections.loc),
12484 get_section_id (&sections.str_offsets));
80626a55 12485 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12486 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12487 virtual_dwo_name.c_str (),
12488 comp_dir);
80626a55
DE
12489 /* Create one if necessary. */
12490 if (*dwo_file_slot == NULL)
12491 {
b4f54984 12492 if (dwarf_read_debug)
80626a55
DE
12493 {
12494 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12495 virtual_dwo_name.c_str ());
80626a55 12496 }
51ac9db5 12497 dwo_file = new struct dwo_file;
efba19b0
TT
12498 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12499 virtual_dwo_name);
0ac5b59e 12500 dwo_file->comp_dir = comp_dir;
80626a55
DE
12501 dwo_file->sections.abbrev = sections.abbrev;
12502 dwo_file->sections.line = sections.line;
12503 dwo_file->sections.loc = sections.loc;
12504 dwo_file->sections.macinfo = sections.macinfo;
12505 dwo_file->sections.macro = sections.macro;
12506 dwo_file->sections.str_offsets = sections.str_offsets;
12507 /* The "str" section is global to the entire DWP file. */
12508 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12509 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12510 there's no need to record it in dwo_file.
12511 Also, we can't simply record type sections in dwo_file because
12512 we record a pointer into the vector in dwo_unit. As we collect more
12513 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12514 for it, invalidating all copies of pointers into the previous
12515 contents. */
80626a55
DE
12516 *dwo_file_slot = dwo_file;
12517 }
12518 else
12519 {
b4f54984 12520 if (dwarf_read_debug)
80626a55
DE
12521 {
12522 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12523 virtual_dwo_name.c_str ());
80626a55 12524 }
9a3c8263 12525 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12526 }
80626a55
DE
12527
12528 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12529 dwo_unit->dwo_file = dwo_file;
12530 dwo_unit->signature = signature;
8d749320
SM
12531 dwo_unit->section =
12532 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12533 *dwo_unit->section = sections.info_or_types;
57d63ce2 12534 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12535
12536 return dwo_unit;
12537}
12538
73869dc2
DE
12539/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12540 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12541 piece within that section used by a TU/CU, return a virtual section
12542 of just that piece. */
12543
12544static struct dwarf2_section_info
ed2dc618
SM
12545create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12546 struct dwarf2_section_info *section,
73869dc2
DE
12547 bfd_size_type offset, bfd_size_type size)
12548{
12549 struct dwarf2_section_info result;
12550 asection *sectp;
12551
12552 gdb_assert (section != NULL);
12553 gdb_assert (!section->is_virtual);
12554
12555 memset (&result, 0, sizeof (result));
12556 result.s.containing_section = section;
dc4ccb6f 12557 result.is_virtual = true;
73869dc2
DE
12558
12559 if (size == 0)
12560 return result;
12561
12562 sectp = get_section_bfd_section (section);
12563
12564 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12565 bounds of the real section. This is a pretty-rare event, so just
12566 flag an error (easier) instead of a warning and trying to cope. */
12567 if (sectp == NULL
fd361982 12568 || offset + size > bfd_section_size (sectp))
73869dc2 12569 {
73869dc2
DE
12570 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12571 " in section %s [in module %s]"),
fd361982 12572 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12573 objfile_name (dwarf2_per_objfile->objfile));
12574 }
12575
12576 result.virtual_offset = offset;
12577 result.size = size;
12578 return result;
12579}
12580
12581/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12582 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12583 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12584 This is for DWP version 2 files. */
12585
12586static struct dwo_unit *
ed2dc618
SM
12587create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12588 struct dwp_file *dwp_file,
73869dc2
DE
12589 uint32_t unit_index,
12590 const char *comp_dir,
12591 ULONGEST signature, int is_debug_types)
12592{
12593 struct objfile *objfile = dwarf2_per_objfile->objfile;
12594 const struct dwp_hash_table *dwp_htab =
12595 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12596 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12597 const char *kind = is_debug_types ? "TU" : "CU";
12598 struct dwo_file *dwo_file;
12599 struct dwo_unit *dwo_unit;
12600 struct virtual_v2_dwo_sections sections;
12601 void **dwo_file_slot;
73869dc2
DE
12602 int i;
12603
12604 gdb_assert (dwp_file->version == 2);
12605
b4f54984 12606 if (dwarf_read_debug)
73869dc2
DE
12607 {
12608 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12609 kind,
12610 pulongest (unit_index), hex_string (signature),
12611 dwp_file->name);
12612 }
12613
12614 /* Fetch the section offsets of this DWO unit. */
12615
12616 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12617
12618 for (i = 0; i < dwp_htab->nr_columns; ++i)
12619 {
12620 uint32_t offset = read_4_bytes (dbfd,
12621 dwp_htab->section_pool.v2.offsets
12622 + (((unit_index - 1) * dwp_htab->nr_columns
12623 + i)
12624 * sizeof (uint32_t)));
12625 uint32_t size = read_4_bytes (dbfd,
12626 dwp_htab->section_pool.v2.sizes
12627 + (((unit_index - 1) * dwp_htab->nr_columns
12628 + i)
12629 * sizeof (uint32_t)));
12630
12631 switch (dwp_htab->section_pool.v2.section_ids[i])
12632 {
12633 case DW_SECT_INFO:
12634 case DW_SECT_TYPES:
12635 sections.info_or_types_offset = offset;
12636 sections.info_or_types_size = size;
12637 break;
12638 case DW_SECT_ABBREV:
12639 sections.abbrev_offset = offset;
12640 sections.abbrev_size = size;
12641 break;
12642 case DW_SECT_LINE:
12643 sections.line_offset = offset;
12644 sections.line_size = size;
12645 break;
12646 case DW_SECT_LOC:
12647 sections.loc_offset = offset;
12648 sections.loc_size = size;
12649 break;
12650 case DW_SECT_STR_OFFSETS:
12651 sections.str_offsets_offset = offset;
12652 sections.str_offsets_size = size;
12653 break;
12654 case DW_SECT_MACINFO:
12655 sections.macinfo_offset = offset;
12656 sections.macinfo_size = size;
12657 break;
12658 case DW_SECT_MACRO:
12659 sections.macro_offset = offset;
12660 sections.macro_size = size;
12661 break;
12662 }
12663 }
12664
12665 /* It's easier for the rest of the code if we fake a struct dwo_file and
12666 have dwo_unit "live" in that. At least for now.
12667
12668 The DWP file can be made up of a random collection of CUs and TUs.
12669 However, for each CU + set of TUs that came from the same original DWO
12670 file, we can combine them back into a virtual DWO file to save space
12671 (fewer struct dwo_file objects to allocate). Remember that for really
12672 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12673
791afaa2
TT
12674 std::string virtual_dwo_name =
12675 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12676 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12677 (long) (sections.line_size ? sections.line_offset : 0),
12678 (long) (sections.loc_size ? sections.loc_offset : 0),
12679 (long) (sections.str_offsets_size
12680 ? sections.str_offsets_offset : 0));
73869dc2 12681 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12682 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12683 virtual_dwo_name.c_str (),
12684 comp_dir);
73869dc2
DE
12685 /* Create one if necessary. */
12686 if (*dwo_file_slot == NULL)
12687 {
b4f54984 12688 if (dwarf_read_debug)
73869dc2
DE
12689 {
12690 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12691 virtual_dwo_name.c_str ());
73869dc2 12692 }
51ac9db5 12693 dwo_file = new struct dwo_file;
efba19b0
TT
12694 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12695 virtual_dwo_name);
73869dc2
DE
12696 dwo_file->comp_dir = comp_dir;
12697 dwo_file->sections.abbrev =
ed2dc618 12698 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12699 sections.abbrev_offset, sections.abbrev_size);
12700 dwo_file->sections.line =
ed2dc618 12701 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12702 sections.line_offset, sections.line_size);
12703 dwo_file->sections.loc =
ed2dc618 12704 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12705 sections.loc_offset, sections.loc_size);
12706 dwo_file->sections.macinfo =
ed2dc618 12707 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12708 sections.macinfo_offset, sections.macinfo_size);
12709 dwo_file->sections.macro =
ed2dc618 12710 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12711 sections.macro_offset, sections.macro_size);
12712 dwo_file->sections.str_offsets =
ed2dc618
SM
12713 create_dwp_v2_section (dwarf2_per_objfile,
12714 &dwp_file->sections.str_offsets,
73869dc2
DE
12715 sections.str_offsets_offset,
12716 sections.str_offsets_size);
12717 /* The "str" section is global to the entire DWP file. */
12718 dwo_file->sections.str = dwp_file->sections.str;
12719 /* The info or types section is assigned below to dwo_unit,
12720 there's no need to record it in dwo_file.
12721 Also, we can't simply record type sections in dwo_file because
12722 we record a pointer into the vector in dwo_unit. As we collect more
12723 types we'll grow the vector and eventually have to reallocate space
12724 for it, invalidating all copies of pointers into the previous
12725 contents. */
12726 *dwo_file_slot = dwo_file;
12727 }
12728 else
12729 {
b4f54984 12730 if (dwarf_read_debug)
73869dc2
DE
12731 {
12732 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12733 virtual_dwo_name.c_str ());
73869dc2 12734 }
9a3c8263 12735 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12736 }
73869dc2
DE
12737
12738 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12739 dwo_unit->dwo_file = dwo_file;
12740 dwo_unit->signature = signature;
8d749320
SM
12741 dwo_unit->section =
12742 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12743 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12744 is_debug_types
73869dc2
DE
12745 ? &dwp_file->sections.types
12746 : &dwp_file->sections.info,
12747 sections.info_or_types_offset,
12748 sections.info_or_types_size);
12749 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12750
12751 return dwo_unit;
12752}
12753
57d63ce2
DE
12754/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12755 Returns NULL if the signature isn't found. */
80626a55
DE
12756
12757static struct dwo_unit *
ed2dc618
SM
12758lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12759 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12760 ULONGEST signature, int is_debug_types)
80626a55 12761{
57d63ce2
DE
12762 const struct dwp_hash_table *dwp_htab =
12763 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12764 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12765 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12766 uint32_t hash = signature & mask;
12767 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12768 unsigned int i;
12769 void **slot;
870f88f7 12770 struct dwo_unit find_dwo_cu;
80626a55
DE
12771
12772 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12773 find_dwo_cu.signature = signature;
19ac8c2e
DE
12774 slot = htab_find_slot (is_debug_types
12775 ? dwp_file->loaded_tus
12776 : dwp_file->loaded_cus,
12777 &find_dwo_cu, INSERT);
80626a55
DE
12778
12779 if (*slot != NULL)
9a3c8263 12780 return (struct dwo_unit *) *slot;
80626a55
DE
12781
12782 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12783 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12784 {
12785 ULONGEST signature_in_table;
12786
12787 signature_in_table =
57d63ce2 12788 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12789 if (signature_in_table == signature)
12790 {
57d63ce2
DE
12791 uint32_t unit_index =
12792 read_4_bytes (dbfd,
12793 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12794
73869dc2
DE
12795 if (dwp_file->version == 1)
12796 {
ed2dc618
SM
12797 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12798 dwp_file, unit_index,
73869dc2
DE
12799 comp_dir, signature,
12800 is_debug_types);
12801 }
12802 else
12803 {
ed2dc618
SM
12804 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12805 dwp_file, unit_index,
73869dc2
DE
12806 comp_dir, signature,
12807 is_debug_types);
12808 }
9a3c8263 12809 return (struct dwo_unit *) *slot;
80626a55
DE
12810 }
12811 if (signature_in_table == 0)
12812 return NULL;
12813 hash = (hash + hash2) & mask;
12814 }
12815
12816 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12817 " [in module %s]"),
12818 dwp_file->name);
12819}
12820
ab5088bf 12821/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12822 Open the file specified by FILE_NAME and hand it off to BFD for
12823 preliminary analysis. Return a newly initialized bfd *, which
12824 includes a canonicalized copy of FILE_NAME.
80626a55 12825 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12826 SEARCH_CWD is true if the current directory is to be searched.
12827 It will be searched before debug-file-directory.
13aaf454
DE
12828 If successful, the file is added to the bfd include table of the
12829 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12830 If unable to find/open the file, return NULL.
3019eac3
DE
12831 NOTE: This function is derived from symfile_bfd_open. */
12832
192b62ce 12833static gdb_bfd_ref_ptr
ed2dc618
SM
12834try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12835 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12836{
24b9144d 12837 int desc;
9c02c129
DE
12838 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12839 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12840 to debug_file_directory. */
e0cc99a6 12841 const char *search_path;
9c02c129
DE
12842 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12843
e0cc99a6 12844 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12845 if (search_cwd)
12846 {
12847 if (*debug_file_directory != '\0')
e0cc99a6
TT
12848 {
12849 search_path_holder.reset (concat (".", dirname_separator_string,
12850 debug_file_directory,
12851 (char *) NULL));
12852 search_path = search_path_holder.get ();
12853 }
6ac97d4c 12854 else
e0cc99a6 12855 search_path = ".";
6ac97d4c 12856 }
9c02c129 12857 else
e0cc99a6 12858 search_path = debug_file_directory;
3019eac3 12859
24b9144d 12860 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12861 if (is_dwp)
12862 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12863
12864 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12865 desc = openp (search_path, flags, file_name,
3019eac3
DE
12866 O_RDONLY | O_BINARY, &absolute_name);
12867 if (desc < 0)
12868 return NULL;
12869
e0cc99a6
TT
12870 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12871 gnutarget, desc));
9c02c129
DE
12872 if (sym_bfd == NULL)
12873 return NULL;
192b62ce 12874 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12875
192b62ce
TT
12876 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12877 return NULL;
3019eac3 12878
13aaf454
DE
12879 /* Success. Record the bfd as having been included by the objfile's bfd.
12880 This is important because things like demangled_names_hash lives in the
12881 objfile's per_bfd space and may have references to things like symbol
12882 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12883 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12884
3019eac3
DE
12885 return sym_bfd;
12886}
12887
ab5088bf 12888/* Try to open DWO file FILE_NAME.
3019eac3
DE
12889 COMP_DIR is the DW_AT_comp_dir attribute.
12890 The result is the bfd handle of the file.
12891 If there is a problem finding or opening the file, return NULL.
12892 Upon success, the canonicalized path of the file is stored in the bfd,
12893 same as symfile_bfd_open. */
12894
192b62ce 12895static gdb_bfd_ref_ptr
ed2dc618
SM
12896open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12897 const char *file_name, const char *comp_dir)
3019eac3 12898{
80626a55 12899 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12900 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12901 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12902
12903 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12904
12905 if (comp_dir != NULL)
12906 {
b36cec19
PA
12907 char *path_to_try = concat (comp_dir, SLASH_STRING,
12908 file_name, (char *) NULL);
3019eac3
DE
12909
12910 /* NOTE: If comp_dir is a relative path, this will also try the
12911 search path, which seems useful. */
ed2dc618
SM
12912 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12913 path_to_try,
12914 0 /*is_dwp*/,
192b62ce 12915 1 /*search_cwd*/));
3019eac3
DE
12916 xfree (path_to_try);
12917 if (abfd != NULL)
12918 return abfd;
12919 }
12920
12921 /* That didn't work, try debug-file-directory, which, despite its name,
12922 is a list of paths. */
12923
12924 if (*debug_file_directory == '\0')
12925 return NULL;
12926
ed2dc618
SM
12927 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12928 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12929}
12930
80626a55
DE
12931/* This function is mapped across the sections and remembers the offset and
12932 size of each of the DWO debugging sections we are interested in. */
12933
12934static void
12935dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12936{
9a3c8263 12937 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12938 const struct dwop_section_names *names = &dwop_section_names;
12939
12940 if (section_is_p (sectp->name, &names->abbrev_dwo))
12941 {
049412e3 12942 dwo_sections->abbrev.s.section = sectp;
fd361982 12943 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12944 }
12945 else if (section_is_p (sectp->name, &names->info_dwo))
12946 {
049412e3 12947 dwo_sections->info.s.section = sectp;
fd361982 12948 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12949 }
12950 else if (section_is_p (sectp->name, &names->line_dwo))
12951 {
049412e3 12952 dwo_sections->line.s.section = sectp;
fd361982 12953 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12954 }
12955 else if (section_is_p (sectp->name, &names->loc_dwo))
12956 {
049412e3 12957 dwo_sections->loc.s.section = sectp;
fd361982 12958 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12959 }
12960 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12961 {
049412e3 12962 dwo_sections->macinfo.s.section = sectp;
fd361982 12963 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12964 }
12965 else if (section_is_p (sectp->name, &names->macro_dwo))
12966 {
049412e3 12967 dwo_sections->macro.s.section = sectp;
fd361982 12968 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12969 }
12970 else if (section_is_p (sectp->name, &names->str_dwo))
12971 {
049412e3 12972 dwo_sections->str.s.section = sectp;
fd361982 12973 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12974 }
12975 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12976 {
049412e3 12977 dwo_sections->str_offsets.s.section = sectp;
fd361982 12978 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12979 }
12980 else if (section_is_p (sectp->name, &names->types_dwo))
12981 {
12982 struct dwarf2_section_info type_section;
12983
12984 memset (&type_section, 0, sizeof (type_section));
049412e3 12985 type_section.s.section = sectp;
fd361982 12986 type_section.size = bfd_section_size (sectp);
fd5866f6 12987 dwo_sections->types.push_back (type_section);
80626a55
DE
12988 }
12989}
12990
ab5088bf 12991/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12992 by PER_CU. This is for the non-DWP case.
80626a55 12993 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12994
12995static struct dwo_file *
0ac5b59e
DE
12996open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12997 const char *dwo_name, const char *comp_dir)
3019eac3 12998{
ed2dc618 12999 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13000
fb1eb2f9 13001 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13002 if (dbfd == NULL)
13003 {
b4f54984 13004 if (dwarf_read_debug)
80626a55
DE
13005 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13006 return NULL;
13007 }
263db9a1 13008
51ac9db5 13009 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13010 dwo_file->dwo_name = dwo_name;
13011 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13012 dwo_file->dbfd = std::move (dbfd);
3019eac3 13013
fb1eb2f9 13014 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13015 &dwo_file->sections);
3019eac3 13016
ed2dc618
SM
13017 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13018 dwo_file->cus);
3019eac3 13019
263db9a1 13020 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13021 dwo_file->sections.types, dwo_file->tus);
3019eac3 13022
b4f54984 13023 if (dwarf_read_debug)
80626a55
DE
13024 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13025
263db9a1 13026 return dwo_file.release ();
3019eac3
DE
13027}
13028
80626a55 13029/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13030 size of each of the DWP debugging sections common to version 1 and 2 that
13031 we are interested in. */
3019eac3 13032
80626a55 13033static void
73869dc2
DE
13034dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13035 void *dwp_file_ptr)
3019eac3 13036{
9a3c8263 13037 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13038 const struct dwop_section_names *names = &dwop_section_names;
13039 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13040
80626a55 13041 /* Record the ELF section number for later lookup: this is what the
73869dc2 13042 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13043 gdb_assert (elf_section_nr < dwp_file->num_sections);
13044 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13045
80626a55
DE
13046 /* Look for specific sections that we need. */
13047 if (section_is_p (sectp->name, &names->str_dwo))
13048 {
049412e3 13049 dwp_file->sections.str.s.section = sectp;
fd361982 13050 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13051 }
13052 else if (section_is_p (sectp->name, &names->cu_index))
13053 {
049412e3 13054 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13055 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13056 }
13057 else if (section_is_p (sectp->name, &names->tu_index))
13058 {
049412e3 13059 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13060 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13061 }
13062}
3019eac3 13063
73869dc2
DE
13064/* This function is mapped across the sections and remembers the offset and
13065 size of each of the DWP version 2 debugging sections that we are interested
13066 in. This is split into a separate function because we don't know if we
13067 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13068
13069static void
13070dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13071{
9a3c8263 13072 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13073 const struct dwop_section_names *names = &dwop_section_names;
13074 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13075
13076 /* Record the ELF section number for later lookup: this is what the
13077 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13078 gdb_assert (elf_section_nr < dwp_file->num_sections);
13079 dwp_file->elf_sections[elf_section_nr] = sectp;
13080
13081 /* Look for specific sections that we need. */
13082 if (section_is_p (sectp->name, &names->abbrev_dwo))
13083 {
049412e3 13084 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13085 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13086 }
13087 else if (section_is_p (sectp->name, &names->info_dwo))
13088 {
049412e3 13089 dwp_file->sections.info.s.section = sectp;
fd361982 13090 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13091 }
13092 else if (section_is_p (sectp->name, &names->line_dwo))
13093 {
049412e3 13094 dwp_file->sections.line.s.section = sectp;
fd361982 13095 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13096 }
13097 else if (section_is_p (sectp->name, &names->loc_dwo))
13098 {
049412e3 13099 dwp_file->sections.loc.s.section = sectp;
fd361982 13100 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13101 }
13102 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13103 {
049412e3 13104 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13105 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13106 }
13107 else if (section_is_p (sectp->name, &names->macro_dwo))
13108 {
049412e3 13109 dwp_file->sections.macro.s.section = sectp;
fd361982 13110 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13111 }
13112 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13113 {
049412e3 13114 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13115 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13116 }
13117 else if (section_is_p (sectp->name, &names->types_dwo))
13118 {
049412e3 13119 dwp_file->sections.types.s.section = sectp;
fd361982 13120 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13121 }
13122}
13123
80626a55 13124/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13125
80626a55
DE
13126static hashval_t
13127hash_dwp_loaded_cutus (const void *item)
13128{
9a3c8263 13129 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13130
80626a55
DE
13131 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13132 return dwo_unit->signature;
3019eac3
DE
13133}
13134
80626a55 13135/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13136
80626a55
DE
13137static int
13138eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13139{
9a3c8263
SM
13140 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13141 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13142
80626a55
DE
13143 return dua->signature == dub->signature;
13144}
3019eac3 13145
80626a55 13146/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13147
80626a55
DE
13148static htab_t
13149allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13150{
13151 return htab_create_alloc_ex (3,
13152 hash_dwp_loaded_cutus,
13153 eq_dwp_loaded_cutus,
13154 NULL,
13155 &objfile->objfile_obstack,
13156 hashtab_obstack_allocate,
13157 dummy_obstack_deallocate);
13158}
3019eac3 13159
ab5088bf
DE
13160/* Try to open DWP file FILE_NAME.
13161 The result is the bfd handle of the file.
13162 If there is a problem finding or opening the file, return NULL.
13163 Upon success, the canonicalized path of the file is stored in the bfd,
13164 same as symfile_bfd_open. */
13165
192b62ce 13166static gdb_bfd_ref_ptr
ed2dc618
SM
13167open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13168 const char *file_name)
ab5088bf 13169{
ed2dc618
SM
13170 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13171 1 /*is_dwp*/,
192b62ce 13172 1 /*search_cwd*/));
6ac97d4c
DE
13173 if (abfd != NULL)
13174 return abfd;
13175
13176 /* Work around upstream bug 15652.
13177 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13178 [Whether that's a "bug" is debatable, but it is getting in our way.]
13179 We have no real idea where the dwp file is, because gdb's realpath-ing
13180 of the executable's path may have discarded the needed info.
13181 [IWBN if the dwp file name was recorded in the executable, akin to
13182 .gnu_debuglink, but that doesn't exist yet.]
13183 Strip the directory from FILE_NAME and search again. */
13184 if (*debug_file_directory != '\0')
13185 {
13186 /* Don't implicitly search the current directory here.
13187 If the user wants to search "." to handle this case,
13188 it must be added to debug-file-directory. */
ed2dc618
SM
13189 return try_open_dwop_file (dwarf2_per_objfile,
13190 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13191 0 /*search_cwd*/);
13192 }
13193
13194 return NULL;
ab5088bf
DE
13195}
13196
80626a55
DE
13197/* Initialize the use of the DWP file for the current objfile.
13198 By convention the name of the DWP file is ${objfile}.dwp.
13199 The result is NULL if it can't be found. */
a766d390 13200
400174b1 13201static std::unique_ptr<struct dwp_file>
ed2dc618 13202open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13203{
13204 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13205
82bf32bc
JK
13206 /* Try to find first .dwp for the binary file before any symbolic links
13207 resolving. */
6c447423
DE
13208
13209 /* If the objfile is a debug file, find the name of the real binary
13210 file and get the name of dwp file from there. */
d721ba37 13211 std::string dwp_name;
6c447423
DE
13212 if (objfile->separate_debug_objfile_backlink != NULL)
13213 {
13214 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13215 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13216
d721ba37 13217 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13218 }
13219 else
d721ba37
PA
13220 dwp_name = objfile->original_name;
13221
13222 dwp_name += ".dwp";
80626a55 13223
ed2dc618 13224 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13225 if (dbfd == NULL
13226 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13227 {
13228 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13229 dwp_name = objfile_name (objfile);
13230 dwp_name += ".dwp";
ed2dc618 13231 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13232 }
13233
80626a55
DE
13234 if (dbfd == NULL)
13235 {
b4f54984 13236 if (dwarf_read_debug)
d721ba37 13237 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13238 return std::unique_ptr<dwp_file> ();
3019eac3 13239 }
400174b1
TT
13240
13241 const char *name = bfd_get_filename (dbfd.get ());
13242 std::unique_ptr<struct dwp_file> dwp_file
13243 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13244
0a0f4c01 13245 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13246 dwp_file->elf_sections =
13247 OBSTACK_CALLOC (&objfile->objfile_obstack,
13248 dwp_file->num_sections, asection *);
13249
400174b1
TT
13250 bfd_map_over_sections (dwp_file->dbfd.get (),
13251 dwarf2_locate_common_dwp_sections,
13252 dwp_file.get ());
80626a55 13253
400174b1
TT
13254 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13255 0);
80626a55 13256
400174b1
TT
13257 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13258 1);
80626a55 13259
73869dc2 13260 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13261 if (dwp_file->cus && dwp_file->tus
13262 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13263 {
13264 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13265 pretty bizarre. We use pulongest here because that's the established
4d65956b 13266 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13267 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13268 " TU version %s [in DWP file %s]"),
13269 pulongest (dwp_file->cus->version),
d721ba37 13270 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13271 }
08302ed2
DE
13272
13273 if (dwp_file->cus)
13274 dwp_file->version = dwp_file->cus->version;
13275 else if (dwp_file->tus)
13276 dwp_file->version = dwp_file->tus->version;
13277 else
13278 dwp_file->version = 2;
73869dc2
DE
13279
13280 if (dwp_file->version == 2)
400174b1
TT
13281 bfd_map_over_sections (dwp_file->dbfd.get (),
13282 dwarf2_locate_v2_dwp_sections,
13283 dwp_file.get ());
73869dc2 13284
19ac8c2e
DE
13285 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13286 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13287
b4f54984 13288 if (dwarf_read_debug)
80626a55
DE
13289 {
13290 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13291 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13292 " %s CUs, %s TUs\n",
13293 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13294 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13295 }
13296
13297 return dwp_file;
3019eac3 13298}
c906108c 13299
ab5088bf
DE
13300/* Wrapper around open_and_init_dwp_file, only open it once. */
13301
13302static struct dwp_file *
ed2dc618 13303get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13304{
13305 if (! dwarf2_per_objfile->dwp_checked)
13306 {
ed2dc618
SM
13307 dwarf2_per_objfile->dwp_file
13308 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13309 dwarf2_per_objfile->dwp_checked = 1;
13310 }
400174b1 13311 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13312}
13313
80626a55
DE
13314/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13315 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13316 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13317 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13318 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13319
13320 This is called, for example, when wanting to read a variable with a
13321 complex location. Therefore we don't want to do file i/o for every call.
13322 Therefore we don't want to look for a DWO file on every call.
13323 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13324 then we check if we've already seen DWO_NAME, and only THEN do we check
13325 for a DWO file.
13326
1c658ad5 13327 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13328 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13329
3019eac3 13330static struct dwo_unit *
80626a55
DE
13331lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13332 const char *dwo_name, const char *comp_dir,
13333 ULONGEST signature, int is_debug_types)
3019eac3 13334{
ed2dc618 13335 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13336 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13337 const char *kind = is_debug_types ? "TU" : "CU";
13338 void **dwo_file_slot;
3019eac3 13339 struct dwo_file *dwo_file;
80626a55 13340 struct dwp_file *dwp_file;
cb1df416 13341
6a506a2d
DE
13342 /* First see if there's a DWP file.
13343 If we have a DWP file but didn't find the DWO inside it, don't
13344 look for the original DWO file. It makes gdb behave differently
13345 depending on whether one is debugging in the build tree. */
cf2c3c16 13346
ed2dc618 13347 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13348 if (dwp_file != NULL)
cf2c3c16 13349 {
80626a55
DE
13350 const struct dwp_hash_table *dwp_htab =
13351 is_debug_types ? dwp_file->tus : dwp_file->cus;
13352
13353 if (dwp_htab != NULL)
13354 {
13355 struct dwo_unit *dwo_cutu =
ed2dc618 13356 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13357 signature, is_debug_types);
80626a55
DE
13358
13359 if (dwo_cutu != NULL)
13360 {
b4f54984 13361 if (dwarf_read_debug)
80626a55
DE
13362 {
13363 fprintf_unfiltered (gdb_stdlog,
13364 "Virtual DWO %s %s found: @%s\n",
13365 kind, hex_string (signature),
13366 host_address_to_string (dwo_cutu));
13367 }
13368 return dwo_cutu;
13369 }
13370 }
13371 }
6a506a2d 13372 else
80626a55 13373 {
6a506a2d 13374 /* No DWP file, look for the DWO file. */
80626a55 13375
ed2dc618
SM
13376 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13377 dwo_name, comp_dir);
6a506a2d 13378 if (*dwo_file_slot == NULL)
80626a55 13379 {
6a506a2d
DE
13380 /* Read in the file and build a table of the CUs/TUs it contains. */
13381 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13382 }
6a506a2d 13383 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13384 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13385
6a506a2d 13386 if (dwo_file != NULL)
19c3d4c9 13387 {
6a506a2d
DE
13388 struct dwo_unit *dwo_cutu = NULL;
13389
13390 if (is_debug_types && dwo_file->tus)
13391 {
13392 struct dwo_unit find_dwo_cutu;
13393
13394 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13395 find_dwo_cutu.signature = signature;
9a3c8263
SM
13396 dwo_cutu
13397 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13398 }
33c5cd75 13399 else if (!is_debug_types && dwo_file->cus)
80626a55 13400 {
33c5cd75
DB
13401 struct dwo_unit find_dwo_cutu;
13402
13403 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13404 find_dwo_cutu.signature = signature;
13405 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13406 &find_dwo_cutu);
6a506a2d
DE
13407 }
13408
13409 if (dwo_cutu != NULL)
13410 {
b4f54984 13411 if (dwarf_read_debug)
6a506a2d
DE
13412 {
13413 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13414 kind, dwo_name, hex_string (signature),
13415 host_address_to_string (dwo_cutu));
13416 }
13417 return dwo_cutu;
80626a55
DE
13418 }
13419 }
2e276125 13420 }
9cdd5dbd 13421
80626a55
DE
13422 /* We didn't find it. This could mean a dwo_id mismatch, or
13423 someone deleted the DWO/DWP file, or the search path isn't set up
13424 correctly to find the file. */
13425
b4f54984 13426 if (dwarf_read_debug)
80626a55
DE
13427 {
13428 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13429 kind, dwo_name, hex_string (signature));
13430 }
3019eac3 13431
6656a72d
DE
13432 /* This is a warning and not a complaint because it can be caused by
13433 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13434 {
13435 /* Print the name of the DWP file if we looked there, helps the user
13436 better diagnose the problem. */
791afaa2 13437 std::string dwp_text;
43942612
DE
13438
13439 if (dwp_file != NULL)
791afaa2
TT
13440 dwp_text = string_printf (" [in DWP file %s]",
13441 lbasename (dwp_file->name));
43942612 13442
9d8780f0 13443 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13444 " [in module %s]"),
13445 kind, dwo_name, hex_string (signature),
791afaa2 13446 dwp_text.c_str (),
43942612 13447 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13448 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13449 }
3019eac3 13450 return NULL;
5fb290d7
DJ
13451}
13452
80626a55
DE
13453/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13454 See lookup_dwo_cutu_unit for details. */
13455
13456static struct dwo_unit *
13457lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13458 const char *dwo_name, const char *comp_dir,
13459 ULONGEST signature)
13460{
13461 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13462}
13463
13464/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13465 See lookup_dwo_cutu_unit for details. */
13466
13467static struct dwo_unit *
13468lookup_dwo_type_unit (struct signatured_type *this_tu,
13469 const char *dwo_name, const char *comp_dir)
13470{
13471 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13472}
13473
89e63ee4
DE
13474/* Traversal function for queue_and_load_all_dwo_tus. */
13475
13476static int
13477queue_and_load_dwo_tu (void **slot, void *info)
13478{
13479 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13480 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13481 ULONGEST signature = dwo_unit->signature;
13482 struct signatured_type *sig_type =
13483 lookup_dwo_signatured_type (per_cu->cu, signature);
13484
13485 if (sig_type != NULL)
13486 {
13487 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13488
13489 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13490 a real dependency of PER_CU on SIG_TYPE. That is detected later
13491 while processing PER_CU. */
13492 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13493 load_full_type_unit (sig_cu);
13494 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13495 }
13496
13497 return 1;
13498}
13499
13500/* Queue all TUs contained in the DWO of PER_CU to be read in.
13501 The DWO may have the only definition of the type, though it may not be
13502 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13503 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13504
13505static void
13506queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13507{
13508 struct dwo_unit *dwo_unit;
13509 struct dwo_file *dwo_file;
13510
13511 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13512 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13513 gdb_assert (per_cu->cu != NULL);
13514
13515 dwo_unit = per_cu->cu->dwo_unit;
13516 gdb_assert (dwo_unit != NULL);
13517
13518 dwo_file = dwo_unit->dwo_file;
13519 if (dwo_file->tus != NULL)
13520 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13521}
13522
3019eac3 13523/* Read in various DIEs. */
348e048f 13524
d389af10 13525/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13526 Inherit only the children of the DW_AT_abstract_origin DIE not being
13527 already referenced by DW_AT_abstract_origin from the children of the
13528 current DIE. */
d389af10
JK
13529
13530static void
13531inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13532{
13533 struct die_info *child_die;
791afaa2 13534 sect_offset *offsetp;
d389af10
JK
13535 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13536 struct die_info *origin_die;
13537 /* Iterator of the ORIGIN_DIE children. */
13538 struct die_info *origin_child_die;
d389af10 13539 struct attribute *attr;
cd02d79d
PA
13540 struct dwarf2_cu *origin_cu;
13541 struct pending **origin_previous_list_in_scope;
d389af10
JK
13542
13543 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13544 if (!attr)
13545 return;
13546
cd02d79d
PA
13547 /* Note that following die references may follow to a die in a
13548 different cu. */
13549
13550 origin_cu = cu;
13551 origin_die = follow_die_ref (die, attr, &origin_cu);
13552
13553 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13554 symbols in. */
13555 origin_previous_list_in_scope = origin_cu->list_in_scope;
13556 origin_cu->list_in_scope = cu->list_in_scope;
13557
edb3359d
DJ
13558 if (die->tag != origin_die->tag
13559 && !(die->tag == DW_TAG_inlined_subroutine
13560 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13561 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13562 sect_offset_str (die->sect_off),
13563 sect_offset_str (origin_die->sect_off));
d389af10 13564
791afaa2 13565 std::vector<sect_offset> offsets;
d389af10 13566
3ea89b92
PMR
13567 for (child_die = die->child;
13568 child_die && child_die->tag;
13569 child_die = sibling_die (child_die))
13570 {
13571 struct die_info *child_origin_die;
13572 struct dwarf2_cu *child_origin_cu;
13573
13574 /* We are trying to process concrete instance entries:
216f72a1 13575 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13576 it's not relevant to our analysis here. i.e. detecting DIEs that are
13577 present in the abstract instance but not referenced in the concrete
13578 one. */
216f72a1
JK
13579 if (child_die->tag == DW_TAG_call_site
13580 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13581 continue;
13582
c38f313d
DJ
13583 /* For each CHILD_DIE, find the corresponding child of
13584 ORIGIN_DIE. If there is more than one layer of
13585 DW_AT_abstract_origin, follow them all; there shouldn't be,
13586 but GCC versions at least through 4.4 generate this (GCC PR
13587 40573). */
3ea89b92
PMR
13588 child_origin_die = child_die;
13589 child_origin_cu = cu;
c38f313d
DJ
13590 while (1)
13591 {
cd02d79d
PA
13592 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13593 child_origin_cu);
c38f313d
DJ
13594 if (attr == NULL)
13595 break;
cd02d79d
PA
13596 child_origin_die = follow_die_ref (child_origin_die, attr,
13597 &child_origin_cu);
c38f313d
DJ
13598 }
13599
d389af10
JK
13600 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13601 counterpart may exist. */
c38f313d 13602 if (child_origin_die != child_die)
d389af10 13603 {
edb3359d
DJ
13604 if (child_die->tag != child_origin_die->tag
13605 && !(child_die->tag == DW_TAG_inlined_subroutine
13606 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13607 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13608 "different tags"),
9d8780f0
SM
13609 sect_offset_str (child_die->sect_off),
13610 sect_offset_str (child_origin_die->sect_off));
c38f313d 13611 if (child_origin_die->parent != origin_die)
b98664d3 13612 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13613 "different parents"),
9d8780f0
SM
13614 sect_offset_str (child_die->sect_off),
13615 sect_offset_str (child_origin_die->sect_off));
c38f313d 13616 else
791afaa2 13617 offsets.push_back (child_origin_die->sect_off);
d389af10 13618 }
d389af10 13619 }
791afaa2
TT
13620 std::sort (offsets.begin (), offsets.end ());
13621 sect_offset *offsets_end = offsets.data () + offsets.size ();
13622 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13623 if (offsetp[-1] == *offsetp)
b98664d3 13624 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13625 "to DIE %s as their abstract origin"),
13626 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13627
791afaa2 13628 offsetp = offsets.data ();
d389af10
JK
13629 origin_child_die = origin_die->child;
13630 while (origin_child_die && origin_child_die->tag)
13631 {
13632 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13633 while (offsetp < offsets_end
9c541725 13634 && *offsetp < origin_child_die->sect_off)
d389af10 13635 offsetp++;
b64f50a1 13636 if (offsetp >= offsets_end
9c541725 13637 || *offsetp > origin_child_die->sect_off)
d389af10 13638 {
adde2bff
DE
13639 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13640 Check whether we're already processing ORIGIN_CHILD_DIE.
13641 This can happen with mutually referenced abstract_origins.
13642 PR 16581. */
13643 if (!origin_child_die->in_process)
13644 process_die (origin_child_die, origin_cu);
d389af10
JK
13645 }
13646 origin_child_die = sibling_die (origin_child_die);
13647 }
cd02d79d 13648 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13649}
13650
c906108c 13651static void
e7c27a73 13652read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13653{
518817b3 13654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13656 struct context_stack *newobj;
c906108c
SS
13657 CORE_ADDR lowpc;
13658 CORE_ADDR highpc;
13659 struct die_info *child_die;
edb3359d 13660 struct attribute *attr, *call_line, *call_file;
15d034d0 13661 const char *name;
e142c38c 13662 CORE_ADDR baseaddr;
801e3a5b 13663 struct block *block;
edb3359d 13664 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13665 std::vector<struct symbol *> template_args;
34eaf542 13666 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13667
13668 if (inlined_func)
13669 {
13670 /* If we do not have call site information, we can't show the
13671 caller of this inlined function. That's too confusing, so
13672 only use the scope for local variables. */
13673 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13674 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13675 if (call_line == NULL || call_file == NULL)
13676 {
13677 read_lexical_block_scope (die, cu);
13678 return;
13679 }
13680 }
c906108c 13681
e142c38c
DJ
13682 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13683
94af9270 13684 name = dwarf2_name (die, cu);
c906108c 13685
e8d05480
JB
13686 /* Ignore functions with missing or empty names. These are actually
13687 illegal according to the DWARF standard. */
13688 if (name == NULL)
13689 {
b98664d3 13690 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13691 sect_offset_str (die->sect_off));
e8d05480
JB
13692 return;
13693 }
13694
13695 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13696 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13697 <= PC_BOUNDS_INVALID)
e8d05480 13698 {
ae4d0c03
PM
13699 attr = dwarf2_attr (die, DW_AT_external, cu);
13700 if (!attr || !DW_UNSND (attr))
b98664d3 13701 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13702 "for subprogram DIE at %s"),
13703 sect_offset_str (die->sect_off));
e8d05480
JB
13704 return;
13705 }
c906108c 13706
3e29f34a
MR
13707 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13708 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13709
34eaf542
TT
13710 /* If we have any template arguments, then we must allocate a
13711 different sort of symbol. */
13712 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13713 {
13714 if (child_die->tag == DW_TAG_template_type_param
13715 || child_die->tag == DW_TAG_template_value_param)
13716 {
e623cf5d 13717 templ_func = allocate_template_symbol (objfile);
cf724bc9 13718 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13719 break;
13720 }
13721 }
13722
c24bdb02 13723 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13724 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13725 (struct symbol *) templ_func);
4c2df51b 13726
81873cc8
TV
13727 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13728 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13729 cu->language);
13730
4cecd739
DJ
13731 /* If there is a location expression for DW_AT_frame_base, record
13732 it. */
e142c38c 13733 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13734 if (attr)
fe978cb0 13735 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13736
63e43d3a
PMR
13737 /* If there is a location for the static link, record it. */
13738 newobj->static_link = NULL;
13739 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13740 if (attr)
13741 {
224c3ddb
SM
13742 newobj->static_link
13743 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13744 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13745 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13746 }
13747
c24bdb02 13748 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13749
639d11d3 13750 if (die->child != NULL)
c906108c 13751 {
639d11d3 13752 child_die = die->child;
c906108c
SS
13753 while (child_die && child_die->tag)
13754 {
34eaf542
TT
13755 if (child_die->tag == DW_TAG_template_type_param
13756 || child_die->tag == DW_TAG_template_value_param)
13757 {
13758 struct symbol *arg = new_symbol (child_die, NULL, cu);
13759
f1078f66 13760 if (arg != NULL)
2f4732b0 13761 template_args.push_back (arg);
34eaf542
TT
13762 }
13763 else
13764 process_die (child_die, cu);
c906108c
SS
13765 child_die = sibling_die (child_die);
13766 }
13767 }
13768
d389af10
JK
13769 inherit_abstract_dies (die, cu);
13770
4a811a97
UW
13771 /* If we have a DW_AT_specification, we might need to import using
13772 directives from the context of the specification DIE. See the
13773 comment in determine_prefix. */
13774 if (cu->language == language_cplus
13775 && dwarf2_attr (die, DW_AT_specification, cu))
13776 {
13777 struct dwarf2_cu *spec_cu = cu;
13778 struct die_info *spec_die = die_specification (die, &spec_cu);
13779
13780 while (spec_die)
13781 {
13782 child_die = spec_die->child;
13783 while (child_die && child_die->tag)
13784 {
13785 if (child_die->tag == DW_TAG_imported_module)
13786 process_die (child_die, spec_cu);
13787 child_die = sibling_die (child_die);
13788 }
13789
13790 /* In some cases, GCC generates specification DIEs that
13791 themselves contain DW_AT_specification attributes. */
13792 spec_die = die_specification (spec_die, &spec_cu);
13793 }
13794 }
13795
c24bdb02 13796 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13797 /* Make a block for the local symbols within. */
c24bdb02 13798 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13799 cstk.static_link, lowpc, highpc);
801e3a5b 13800
df8a16a1 13801 /* For C++, set the block's scope. */
45280282
IB
13802 if ((cu->language == language_cplus
13803 || cu->language == language_fortran
c44af4eb
TT
13804 || cu->language == language_d
13805 || cu->language == language_rust)
4d4ec4e5 13806 && cu->processing_has_namespace_info)
195a3f6c
TT
13807 block_set_scope (block, determine_prefix (die, cu),
13808 &objfile->objfile_obstack);
df8a16a1 13809
801e3a5b
JB
13810 /* If we have address ranges, record them. */
13811 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13812
a60f3166 13813 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13814
34eaf542 13815 /* Attach template arguments to function. */
2f4732b0 13816 if (!template_args.empty ())
34eaf542
TT
13817 {
13818 gdb_assert (templ_func != NULL);
13819
2f4732b0 13820 templ_func->n_template_arguments = template_args.size ();
34eaf542 13821 templ_func->template_arguments
8d749320
SM
13822 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13823 templ_func->n_template_arguments);
34eaf542 13824 memcpy (templ_func->template_arguments,
2f4732b0 13825 template_args.data (),
34eaf542 13826 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13827
13828 /* Make sure that the symtab is set on the new symbols. Even
13829 though they don't appear in this symtab directly, other parts
13830 of gdb assume that symbols do, and this is reasonably
13831 true. */
8634679f 13832 for (symbol *sym : template_args)
3e1d3d8c 13833 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13834 }
13835
208d8187
JB
13836 /* In C++, we can have functions nested inside functions (e.g., when
13837 a function declares a class that has methods). This means that
13838 when we finish processing a function scope, we may need to go
13839 back to building a containing block's symbol lists. */
c24bdb02
KS
13840 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13841 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13842
921e78cf
JB
13843 /* If we've finished processing a top-level function, subsequent
13844 symbols go in the file symbol list. */
c24bdb02
KS
13845 if (cu->get_builder ()->outermost_context_p ())
13846 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13847}
13848
13849/* Process all the DIES contained within a lexical block scope. Start
13850 a new scope, process the dies, and then close the scope. */
13851
13852static void
e7c27a73 13853read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13854{
518817b3 13855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13857 CORE_ADDR lowpc, highpc;
13858 struct die_info *child_die;
e142c38c
DJ
13859 CORE_ADDR baseaddr;
13860
13861 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13862
13863 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13864 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13865 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13866 be nasty. Might be easier to properly extend generic blocks to
af34e669 13867 describe ranges. */
e385593e
JK
13868 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13869 {
13870 case PC_BOUNDS_NOT_PRESENT:
13871 /* DW_TAG_lexical_block has no attributes, process its children as if
13872 there was no wrapping by that DW_TAG_lexical_block.
13873 GCC does no longer produces such DWARF since GCC r224161. */
13874 for (child_die = die->child;
13875 child_die != NULL && child_die->tag;
13876 child_die = sibling_die (child_die))
13877 process_die (child_die, cu);
13878 return;
13879 case PC_BOUNDS_INVALID:
13880 return;
13881 }
3e29f34a
MR
13882 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13883 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13884
c24bdb02 13885 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13886 if (die->child != NULL)
c906108c 13887 {
639d11d3 13888 child_die = die->child;
c906108c
SS
13889 while (child_die && child_die->tag)
13890 {
e7c27a73 13891 process_die (child_die, cu);
c906108c
SS
13892 child_die = sibling_die (child_die);
13893 }
13894 }
3ea89b92 13895 inherit_abstract_dies (die, cu);
c24bdb02 13896 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13897
c24bdb02
KS
13898 if (*cu->get_builder ()->get_local_symbols () != NULL
13899 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13900 {
801e3a5b 13901 struct block *block
c24bdb02 13902 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13903 cstk.start_addr, highpc);
801e3a5b
JB
13904
13905 /* Note that recording ranges after traversing children, as we
13906 do here, means that recording a parent's ranges entails
13907 walking across all its children's ranges as they appear in
13908 the address map, which is quadratic behavior.
13909
13910 It would be nicer to record the parent's ranges before
13911 traversing its children, simply overriding whatever you find
13912 there. But since we don't even decide whether to create a
13913 block until after we've traversed its children, that's hard
13914 to do. */
13915 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13916 }
c24bdb02
KS
13917 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13918 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13919}
13920
216f72a1 13921/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13922
13923static void
13924read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13925{
518817b3 13926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13928 CORE_ADDR pc, baseaddr;
13929 struct attribute *attr;
13930 struct call_site *call_site, call_site_local;
13931 void **slot;
13932 int nparams;
13933 struct die_info *child_die;
13934
13935 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13936
216f72a1
JK
13937 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13938 if (attr == NULL)
13939 {
13940 /* This was a pre-DWARF-5 GNU extension alias
13941 for DW_AT_call_return_pc. */
13942 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13943 }
96408a79
SA
13944 if (!attr)
13945 {
b98664d3 13946 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13947 "DIE %s [in module %s]"),
13948 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13949 return;
13950 }
31aa7e4e 13951 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13952 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13953
13954 if (cu->call_site_htab == NULL)
13955 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13956 NULL, &objfile->objfile_obstack,
13957 hashtab_obstack_allocate, NULL);
13958 call_site_local.pc = pc;
13959 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13960 if (*slot != NULL)
13961 {
b98664d3 13962 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13963 "DIE %s [in module %s]"),
13964 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13965 objfile_name (objfile));
96408a79
SA
13966 return;
13967 }
13968
13969 /* Count parameters at the caller. */
13970
13971 nparams = 0;
13972 for (child_die = die->child; child_die && child_die->tag;
13973 child_die = sibling_die (child_die))
13974 {
216f72a1
JK
13975 if (child_die->tag != DW_TAG_call_site_parameter
13976 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13977 {
b98664d3 13978 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13979 "DW_TAG_call_site child DIE %s [in module %s]"),
13980 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13981 objfile_name (objfile));
96408a79
SA
13982 continue;
13983 }
13984
13985 nparams++;
13986 }
13987
224c3ddb
SM
13988 call_site
13989 = ((struct call_site *)
13990 obstack_alloc (&objfile->objfile_obstack,
13991 sizeof (*call_site)
13992 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13993 *slot = call_site;
13994 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13995 call_site->pc = pc;
13996
216f72a1
JK
13997 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13998 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13999 {
14000 struct die_info *func_die;
14001
14002 /* Skip also over DW_TAG_inlined_subroutine. */
14003 for (func_die = die->parent;
14004 func_die && func_die->tag != DW_TAG_subprogram
14005 && func_die->tag != DW_TAG_subroutine_type;
14006 func_die = func_die->parent);
14007
216f72a1
JK
14008 /* DW_AT_call_all_calls is a superset
14009 of DW_AT_call_all_tail_calls. */
96408a79 14010 if (func_die
216f72a1 14011 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14012 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14013 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14014 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14015 {
14016 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14017 not complete. But keep CALL_SITE for look ups via call_site_htab,
14018 both the initial caller containing the real return address PC and
14019 the final callee containing the current PC of a chain of tail
14020 calls do not need to have the tail call list complete. But any
14021 function candidate for a virtual tail call frame searched via
14022 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14023 determined unambiguously. */
14024 }
14025 else
14026 {
14027 struct type *func_type = NULL;
14028
14029 if (func_die)
14030 func_type = get_die_type (func_die, cu);
14031 if (func_type != NULL)
14032 {
14033 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14034
14035 /* Enlist this call site to the function. */
14036 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14037 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14038 }
14039 else
b98664d3 14040 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14041 "DIE %s [in module %s]"),
14042 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14043 }
14044 }
14045
216f72a1
JK
14046 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14047 if (attr == NULL)
14048 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14049 if (attr == NULL)
14050 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14051 if (attr == NULL)
216f72a1
JK
14052 {
14053 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14054 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14055 }
96408a79
SA
14056 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14057 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14058 /* Keep NULL DWARF_BLOCK. */;
14059 else if (attr_form_is_block (attr))
14060 {
14061 struct dwarf2_locexpr_baton *dlbaton;
14062
8d749320 14063 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14064 dlbaton->data = DW_BLOCK (attr)->data;
14065 dlbaton->size = DW_BLOCK (attr)->size;
14066 dlbaton->per_cu = cu->per_cu;
14067
14068 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14069 }
7771576e 14070 else if (attr_form_is_ref (attr))
96408a79 14071 {
96408a79
SA
14072 struct dwarf2_cu *target_cu = cu;
14073 struct die_info *target_die;
14074
ac9ec31b 14075 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14076 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14077 if (die_is_declaration (target_die, target_cu))
14078 {
7d45c7c3 14079 const char *target_physname;
9112db09
JK
14080
14081 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14082 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14083 if (target_physname == NULL)
9112db09 14084 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14085 if (target_physname == NULL)
b98664d3 14086 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14087 "physname, for referencing DIE %s [in module %s]"),
14088 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14089 else
7d455152 14090 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14091 }
14092 else
14093 {
14094 CORE_ADDR lowpc;
14095
14096 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14097 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14098 <= PC_BOUNDS_INVALID)
b98664d3 14099 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14100 "low pc, for referencing DIE %s [in module %s]"),
14101 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14102 else
3e29f34a
MR
14103 {
14104 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14105 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14106 }
96408a79
SA
14107 }
14108 }
14109 else
b98664d3 14110 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14111 "block nor reference, for DIE %s [in module %s]"),
14112 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14113
14114 call_site->per_cu = cu->per_cu;
14115
14116 for (child_die = die->child;
14117 child_die && child_die->tag;
14118 child_die = sibling_die (child_die))
14119 {
96408a79 14120 struct call_site_parameter *parameter;
1788b2d3 14121 struct attribute *loc, *origin;
96408a79 14122
216f72a1
JK
14123 if (child_die->tag != DW_TAG_call_site_parameter
14124 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14125 {
14126 /* Already printed the complaint above. */
14127 continue;
14128 }
14129
14130 gdb_assert (call_site->parameter_count < nparams);
14131 parameter = &call_site->parameter[call_site->parameter_count];
14132
1788b2d3
JK
14133 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14134 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14135 register is contained in DW_AT_call_value. */
96408a79 14136
24c5c679 14137 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14138 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14139 if (origin == NULL)
14140 {
14141 /* This was a pre-DWARF-5 GNU extension alias
14142 for DW_AT_call_parameter. */
14143 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14144 }
7771576e 14145 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14146 {
1788b2d3 14147 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14148
14149 sect_offset sect_off
14150 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14151 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14152 {
14153 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14154 binding can be done only inside one CU. Such referenced DIE
14155 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14156 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14157 "DW_TAG_call_site child DIE %s [in module %s]"),
14158 sect_offset_str (child_die->sect_off),
9c541725 14159 objfile_name (objfile));
d76b7dbc
JK
14160 continue;
14161 }
9c541725
PA
14162 parameter->u.param_cu_off
14163 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14164 }
14165 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14166 {
b98664d3 14167 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14168 "DW_TAG_call_site child DIE %s [in module %s]"),
14169 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14170 continue;
14171 }
24c5c679 14172 else
96408a79 14173 {
24c5c679
JK
14174 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14175 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14176 if (parameter->u.dwarf_reg != -1)
14177 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14178 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14179 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14180 &parameter->u.fb_offset))
14181 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14182 else
14183 {
b98664d3 14184 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14185 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14186 "DW_TAG_call_site child DIE %s "
24c5c679 14187 "[in module %s]"),
9d8780f0 14188 sect_offset_str (child_die->sect_off),
9c541725 14189 objfile_name (objfile));
24c5c679
JK
14190 continue;
14191 }
96408a79
SA
14192 }
14193
216f72a1
JK
14194 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14195 if (attr == NULL)
14196 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14197 if (!attr_form_is_block (attr))
14198 {
b98664d3 14199 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14200 "DW_TAG_call_site child DIE %s [in module %s]"),
14201 sect_offset_str (child_die->sect_off),
9c541725 14202 objfile_name (objfile));
96408a79
SA
14203 continue;
14204 }
14205 parameter->value = DW_BLOCK (attr)->data;
14206 parameter->value_size = DW_BLOCK (attr)->size;
14207
14208 /* Parameters are not pre-cleared by memset above. */
14209 parameter->data_value = NULL;
14210 parameter->data_value_size = 0;
14211 call_site->parameter_count++;
14212
216f72a1
JK
14213 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14214 if (attr == NULL)
14215 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14216 if (attr)
14217 {
14218 if (!attr_form_is_block (attr))
b98664d3 14219 complaint (_("No DW_FORM_block* DW_AT_call_data_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 else
14224 {
14225 parameter->data_value = DW_BLOCK (attr)->data;
14226 parameter->data_value_size = DW_BLOCK (attr)->size;
14227 }
14228 }
14229 }
14230}
14231
71a3c369
TT
14232/* Helper function for read_variable. If DIE represents a virtual
14233 table, then return the type of the concrete object that is
14234 associated with the virtual table. Otherwise, return NULL. */
14235
14236static struct type *
14237rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14238{
14239 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14240 if (attr == NULL)
14241 return NULL;
14242
14243 /* Find the type DIE. */
14244 struct die_info *type_die = NULL;
14245 struct dwarf2_cu *type_cu = cu;
14246
14247 if (attr_form_is_ref (attr))
14248 type_die = follow_die_ref (die, attr, &type_cu);
14249 if (type_die == NULL)
14250 return NULL;
14251
14252 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14253 return NULL;
14254 return die_containing_type (type_die, type_cu);
14255}
14256
14257/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14258
14259static void
14260read_variable (struct die_info *die, struct dwarf2_cu *cu)
14261{
14262 struct rust_vtable_symbol *storage = NULL;
14263
14264 if (cu->language == language_rust)
14265 {
14266 struct type *containing_type = rust_containing_type (die, cu);
14267
14268 if (containing_type != NULL)
14269 {
518817b3 14270 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14271
14272 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14273 struct rust_vtable_symbol);
14274 initialize_objfile_symbol (storage);
14275 storage->concrete_type = containing_type;
cf724bc9 14276 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14277 }
14278 }
14279
e4a62c65
TV
14280 struct symbol *res = new_symbol (die, NULL, cu, storage);
14281 struct attribute *abstract_origin
14282 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14283 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14284 if (res == NULL && loc && abstract_origin)
14285 {
14286 /* We have a variable without a name, but with a location and an abstract
14287 origin. This may be a concrete instance of an abstract variable
14288 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14289 later. */
14290 struct dwarf2_cu *origin_cu = cu;
14291 struct die_info *origin_die
14292 = follow_die_ref (die, abstract_origin, &origin_cu);
14293 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14294 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14295 }
71a3c369
TT
14296}
14297
43988095
JK
14298/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14299 reading .debug_rnglists.
14300 Callback's type should be:
14301 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14302 Return true if the attributes are present and valid, otherwise,
14303 return false. */
14304
14305template <typename Callback>
14306static bool
14307dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14308 Callback &&callback)
14309{
ed2dc618 14310 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14311 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14312 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14313 bfd *obfd = objfile->obfd;
43988095
JK
14314 /* Base address selection entry. */
14315 CORE_ADDR base;
14316 int found_base;
43988095 14317 const gdb_byte *buffer;
43988095
JK
14318 CORE_ADDR baseaddr;
14319 bool overflow = false;
14320
14321 found_base = cu->base_known;
14322 base = cu->base_address;
14323
14324 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14325 if (offset >= dwarf2_per_objfile->rnglists.size)
14326 {
b98664d3 14327 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14328 offset);
14329 return false;
14330 }
14331 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14332
14333 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14334
14335 while (1)
14336 {
7814882a
JK
14337 /* Initialize it due to a false compiler warning. */
14338 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14339 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14340 + dwarf2_per_objfile->rnglists.size);
14341 unsigned int bytes_read;
14342
14343 if (buffer == buf_end)
14344 {
14345 overflow = true;
14346 break;
14347 }
14348 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14349 switch (rlet)
14350 {
14351 case DW_RLE_end_of_list:
14352 break;
14353 case DW_RLE_base_address:
14354 if (buffer + cu->header.addr_size > buf_end)
14355 {
14356 overflow = true;
14357 break;
14358 }
14359 base = read_address (obfd, buffer, cu, &bytes_read);
14360 found_base = 1;
14361 buffer += bytes_read;
14362 break;
14363 case DW_RLE_start_length:
14364 if (buffer + cu->header.addr_size > buf_end)
14365 {
14366 overflow = true;
14367 break;
14368 }
14369 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14370 buffer += bytes_read;
14371 range_end = (range_beginning
14372 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14373 buffer += bytes_read;
14374 if (buffer > buf_end)
14375 {
14376 overflow = true;
14377 break;
14378 }
14379 break;
14380 case DW_RLE_offset_pair:
14381 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14382 buffer += bytes_read;
14383 if (buffer > buf_end)
14384 {
14385 overflow = true;
14386 break;
14387 }
14388 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14389 buffer += bytes_read;
14390 if (buffer > buf_end)
14391 {
14392 overflow = true;
14393 break;
14394 }
14395 break;
14396 case DW_RLE_start_end:
14397 if (buffer + 2 * cu->header.addr_size > buf_end)
14398 {
14399 overflow = true;
14400 break;
14401 }
14402 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14403 buffer += bytes_read;
14404 range_end = read_address (obfd, buffer, cu, &bytes_read);
14405 buffer += bytes_read;
14406 break;
14407 default:
b98664d3 14408 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14409 return false;
14410 }
14411 if (rlet == DW_RLE_end_of_list || overflow)
14412 break;
14413 if (rlet == DW_RLE_base_address)
14414 continue;
14415
14416 if (!found_base)
14417 {
14418 /* We have no valid base address for the ranges
14419 data. */
b98664d3 14420 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14421 return false;
14422 }
14423
14424 if (range_beginning > range_end)
14425 {
14426 /* Inverted range entries are invalid. */
b98664d3 14427 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14428 return false;
14429 }
14430
14431 /* Empty range entries have no effect. */
14432 if (range_beginning == range_end)
14433 continue;
14434
14435 range_beginning += base;
14436 range_end += base;
14437
14438 /* A not-uncommon case of bad debug info.
14439 Don't pollute the addrmap with bad data. */
14440 if (range_beginning + baseaddr == 0
14441 && !dwarf2_per_objfile->has_section_at_zero)
14442 {
b98664d3 14443 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14444 " [in module %s]"), objfile_name (objfile));
14445 continue;
14446 }
14447
14448 callback (range_beginning, range_end);
14449 }
14450
14451 if (overflow)
14452 {
b98664d3 14453 complaint (_("Offset %d is not terminated "
43988095
JK
14454 "for DW_AT_ranges attribute"),
14455 offset);
14456 return false;
14457 }
14458
14459 return true;
14460}
14461
14462/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14463 Callback's type should be:
14464 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14465 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14466
43988095 14467template <typename Callback>
43039443 14468static int
5f46c5a5 14469dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14470 Callback &&callback)
43039443 14471{
ed2dc618 14472 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14473 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14474 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14475 struct comp_unit_head *cu_header = &cu->header;
14476 bfd *obfd = objfile->obfd;
14477 unsigned int addr_size = cu_header->addr_size;
14478 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14479 /* Base address selection entry. */
14480 CORE_ADDR base;
14481 int found_base;
14482 unsigned int dummy;
d521ce57 14483 const gdb_byte *buffer;
ff013f42 14484 CORE_ADDR baseaddr;
43039443 14485
43988095
JK
14486 if (cu_header->version >= 5)
14487 return dwarf2_rnglists_process (offset, cu, callback);
14488
d00adf39
DE
14489 found_base = cu->base_known;
14490 base = cu->base_address;
43039443 14491
be391dca 14492 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14493 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14494 {
b98664d3 14495 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14496 offset);
14497 return 0;
14498 }
dce234bc 14499 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14500
e7030f15 14501 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14502
43039443
JK
14503 while (1)
14504 {
14505 CORE_ADDR range_beginning, range_end;
14506
14507 range_beginning = read_address (obfd, buffer, cu, &dummy);
14508 buffer += addr_size;
14509 range_end = read_address (obfd, buffer, cu, &dummy);
14510 buffer += addr_size;
14511 offset += 2 * addr_size;
14512
14513 /* An end of list marker is a pair of zero addresses. */
14514 if (range_beginning == 0 && range_end == 0)
14515 /* Found the end of list entry. */
14516 break;
14517
14518 /* Each base address selection entry is a pair of 2 values.
14519 The first is the largest possible address, the second is
14520 the base address. Check for a base address here. */
14521 if ((range_beginning & mask) == mask)
14522 {
28d2bfb9
AB
14523 /* If we found the largest possible address, then we already
14524 have the base address in range_end. */
14525 base = range_end;
43039443
JK
14526 found_base = 1;
14527 continue;
14528 }
14529
14530 if (!found_base)
14531 {
14532 /* We have no valid base address for the ranges
14533 data. */
b98664d3 14534 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14535 return 0;
14536 }
14537
9277c30c
UW
14538 if (range_beginning > range_end)
14539 {
14540 /* Inverted range entries are invalid. */
b98664d3 14541 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14542 return 0;
14543 }
14544
14545 /* Empty range entries have no effect. */
14546 if (range_beginning == range_end)
14547 continue;
14548
43039443
JK
14549 range_beginning += base;
14550 range_end += base;
14551
01093045
DE
14552 /* A not-uncommon case of bad debug info.
14553 Don't pollute the addrmap with bad data. */
14554 if (range_beginning + baseaddr == 0
14555 && !dwarf2_per_objfile->has_section_at_zero)
14556 {
b98664d3 14557 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14558 " [in module %s]"), objfile_name (objfile));
01093045
DE
14559 continue;
14560 }
14561
5f46c5a5
JK
14562 callback (range_beginning, range_end);
14563 }
14564
14565 return 1;
14566}
14567
14568/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14569 Return 1 if the attributes are present and valid, otherwise, return 0.
14570 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14571
14572static int
14573dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14574 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14575 struct partial_symtab *ranges_pst)
14576{
518817b3 14577 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14578 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14579 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14580 SECT_OFF_TEXT (objfile));
14581 int low_set = 0;
14582 CORE_ADDR low = 0;
14583 CORE_ADDR high = 0;
14584 int retval;
14585
14586 retval = dwarf2_ranges_process (offset, cu,
14587 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14588 {
9277c30c 14589 if (ranges_pst != NULL)
3e29f34a
MR
14590 {
14591 CORE_ADDR lowpc;
14592 CORE_ADDR highpc;
14593
79748972
TT
14594 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14595 range_beginning + baseaddr)
14596 - baseaddr);
14597 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14598 range_end + baseaddr)
14599 - baseaddr);
d320c2b5
TT
14600 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14601 lowpc, highpc - 1, ranges_pst);
3e29f34a 14602 }
ff013f42 14603
43039443
JK
14604 /* FIXME: This is recording everything as a low-high
14605 segment of consecutive addresses. We should have a
14606 data structure for discontiguous block ranges
14607 instead. */
14608 if (! low_set)
14609 {
14610 low = range_beginning;
14611 high = range_end;
14612 low_set = 1;
14613 }
14614 else
14615 {
14616 if (range_beginning < low)
14617 low = range_beginning;
14618 if (range_end > high)
14619 high = range_end;
14620 }
5f46c5a5
JK
14621 });
14622 if (!retval)
14623 return 0;
43039443
JK
14624
14625 if (! low_set)
14626 /* If the first entry is an end-of-list marker, the range
14627 describes an empty scope, i.e. no instructions. */
14628 return 0;
14629
14630 if (low_return)
14631 *low_return = low;
14632 if (high_return)
14633 *high_return = high;
14634 return 1;
14635}
14636
3a2b436a
JK
14637/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14638 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14639 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14640
3a2b436a 14641static enum pc_bounds_kind
af34e669 14642dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14643 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14644 struct partial_symtab *pst)
c906108c 14645{
518817b3
SM
14646 struct dwarf2_per_objfile *dwarf2_per_objfile
14647 = cu->per_cu->dwarf2_per_objfile;
c906108c 14648 struct attribute *attr;
91da1414 14649 struct attribute *attr_high;
af34e669
DJ
14650 CORE_ADDR low = 0;
14651 CORE_ADDR high = 0;
e385593e 14652 enum pc_bounds_kind ret;
c906108c 14653
91da1414
MW
14654 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14655 if (attr_high)
af34e669 14656 {
e142c38c 14657 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14658 if (attr)
91da1414 14659 {
31aa7e4e
JB
14660 low = attr_value_as_address (attr);
14661 high = attr_value_as_address (attr_high);
14662 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14663 high += low;
91da1414 14664 }
af34e669
DJ
14665 else
14666 /* Found high w/o low attribute. */
e385593e 14667 return PC_BOUNDS_INVALID;
af34e669
DJ
14668
14669 /* Found consecutive range of addresses. */
3a2b436a 14670 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14671 }
c906108c 14672 else
af34e669 14673 {
e142c38c 14674 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14675 if (attr != NULL)
14676 {
ab435259
DE
14677 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14678 We take advantage of the fact that DW_AT_ranges does not appear
14679 in DW_TAG_compile_unit of DWO files. */
14680 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14681 unsigned int ranges_offset = (DW_UNSND (attr)
14682 + (need_ranges_base
14683 ? cu->ranges_base
14684 : 0));
2e3cf129 14685
af34e669 14686 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14687 .debug_ranges section. */
2e3cf129 14688 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14689 return PC_BOUNDS_INVALID;
43039443 14690 /* Found discontinuous range of addresses. */
3a2b436a 14691 ret = PC_BOUNDS_RANGES;
af34e669 14692 }
e385593e
JK
14693 else
14694 return PC_BOUNDS_NOT_PRESENT;
af34e669 14695 }
c906108c 14696
48fbe735 14697 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14698 if (high <= low)
e385593e 14699 return PC_BOUNDS_INVALID;
c906108c
SS
14700
14701 /* When using the GNU linker, .gnu.linkonce. sections are used to
14702 eliminate duplicate copies of functions and vtables and such.
14703 The linker will arbitrarily choose one and discard the others.
14704 The AT_*_pc values for such functions refer to local labels in
14705 these sections. If the section from that file was discarded, the
14706 labels are not in the output, so the relocs get a value of 0.
14707 If this is a discarded function, mark the pc bounds as invalid,
14708 so that GDB will ignore it. */
72dca2f5 14709 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14710 return PC_BOUNDS_INVALID;
c906108c
SS
14711
14712 *lowpc = low;
96408a79
SA
14713 if (highpc)
14714 *highpc = high;
af34e669 14715 return ret;
c906108c
SS
14716}
14717
b084d499
JB
14718/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14719 its low and high PC addresses. Do nothing if these addresses could not
14720 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14721 and HIGHPC to the high address if greater than HIGHPC. */
14722
14723static void
14724dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14725 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14726 struct dwarf2_cu *cu)
14727{
14728 CORE_ADDR low, high;
14729 struct die_info *child = die->child;
14730
e385593e 14731 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14732 {
325fac50
PA
14733 *lowpc = std::min (*lowpc, low);
14734 *highpc = std::max (*highpc, high);
b084d499
JB
14735 }
14736
14737 /* If the language does not allow nested subprograms (either inside
14738 subprograms or lexical blocks), we're done. */
14739 if (cu->language != language_ada)
14740 return;
6e70227d 14741
b084d499
JB
14742 /* Check all the children of the given DIE. If it contains nested
14743 subprograms, then check their pc bounds. Likewise, we need to
14744 check lexical blocks as well, as they may also contain subprogram
14745 definitions. */
14746 while (child && child->tag)
14747 {
14748 if (child->tag == DW_TAG_subprogram
14749 || child->tag == DW_TAG_lexical_block)
14750 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14751 child = sibling_die (child);
14752 }
14753}
14754
fae299cd
DC
14755/* Get the low and high pc's represented by the scope DIE, and store
14756 them in *LOWPC and *HIGHPC. If the correct values can't be
14757 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14758
14759static void
14760get_scope_pc_bounds (struct die_info *die,
14761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14762 struct dwarf2_cu *cu)
14763{
14764 CORE_ADDR best_low = (CORE_ADDR) -1;
14765 CORE_ADDR best_high = (CORE_ADDR) 0;
14766 CORE_ADDR current_low, current_high;
14767
3a2b436a 14768 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14769 >= PC_BOUNDS_RANGES)
fae299cd
DC
14770 {
14771 best_low = current_low;
14772 best_high = current_high;
14773 }
14774 else
14775 {
14776 struct die_info *child = die->child;
14777
14778 while (child && child->tag)
14779 {
14780 switch (child->tag) {
14781 case DW_TAG_subprogram:
b084d499 14782 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14783 break;
14784 case DW_TAG_namespace:
f55ee35c 14785 case DW_TAG_module:
fae299cd
DC
14786 /* FIXME: carlton/2004-01-16: Should we do this for
14787 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14788 that current GCC's always emit the DIEs corresponding
14789 to definitions of methods of classes as children of a
14790 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14791 the DIEs giving the declarations, which could be
14792 anywhere). But I don't see any reason why the
14793 standards says that they have to be there. */
14794 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14795
14796 if (current_low != ((CORE_ADDR) -1))
14797 {
325fac50
PA
14798 best_low = std::min (best_low, current_low);
14799 best_high = std::max (best_high, current_high);
fae299cd
DC
14800 }
14801 break;
14802 default:
0963b4bd 14803 /* Ignore. */
fae299cd
DC
14804 break;
14805 }
14806
14807 child = sibling_die (child);
14808 }
14809 }
14810
14811 *lowpc = best_low;
14812 *highpc = best_high;
14813}
14814
801e3a5b
JB
14815/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14816 in DIE. */
380bca97 14817
801e3a5b
JB
14818static void
14819dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14820 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14821{
518817b3 14822 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14823 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14824 struct attribute *attr;
91da1414 14825 struct attribute *attr_high;
801e3a5b 14826
91da1414
MW
14827 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14828 if (attr_high)
801e3a5b 14829 {
801e3a5b
JB
14830 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14831 if (attr)
14832 {
31aa7e4e
JB
14833 CORE_ADDR low = attr_value_as_address (attr);
14834 CORE_ADDR high = attr_value_as_address (attr_high);
14835
14836 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14837 high += low;
9a619af0 14838
3e29f34a
MR
14839 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14840 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14841 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14842 }
14843 }
14844
14845 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14846 if (attr)
14847 {
ab435259
DE
14848 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14849 We take advantage of the fact that DW_AT_ranges does not appear
14850 in DW_TAG_compile_unit of DWO files. */
14851 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14852
14853 /* The value of the DW_AT_ranges attribute is the offset of the
14854 address range list in the .debug_ranges section. */
ab435259
DE
14855 unsigned long offset = (DW_UNSND (attr)
14856 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14857
2d5f09ec 14858 std::vector<blockrange> blockvec;
5f46c5a5
JK
14859 dwarf2_ranges_process (offset, cu,
14860 [&] (CORE_ADDR start, CORE_ADDR end)
14861 {
58fdfd2c
JK
14862 start += baseaddr;
14863 end += baseaddr;
5f46c5a5
JK
14864 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14865 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14866 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14867 blockvec.emplace_back (start, end);
5f46c5a5 14868 });
2d5f09ec
KB
14869
14870 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14871 }
14872}
14873
685b1105
JK
14874/* Check whether the producer field indicates either of GCC < 4.6, or the
14875 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14876
685b1105
JK
14877static void
14878check_producer (struct dwarf2_cu *cu)
60d5a603 14879{
38360086 14880 int major, minor;
60d5a603
JK
14881
14882 if (cu->producer == NULL)
14883 {
14884 /* For unknown compilers expect their behavior is DWARF version
14885 compliant.
14886
14887 GCC started to support .debug_types sections by -gdwarf-4 since
14888 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14889 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14890 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14891 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14892 }
b1ffba5a 14893 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14894 {
38360086
MW
14895 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14896 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14897 }
5230b05a 14898 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14899 {
14900 cu->producer_is_icc = true;
14901 cu->producer_is_icc_lt_14 = major < 14;
14902 }
c258c396
JD
14903 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14904 cu->producer_is_codewarrior = true;
685b1105
JK
14905 else
14906 {
14907 /* For other non-GCC compilers, expect their behavior is DWARF version
14908 compliant. */
60d5a603
JK
14909 }
14910
9068261f 14911 cu->checked_producer = true;
685b1105 14912}
ba919b58 14913
685b1105
JK
14914/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14915 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14916 during 4.6.0 experimental. */
14917
9068261f 14918static bool
685b1105
JK
14919producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14920{
14921 if (!cu->checked_producer)
14922 check_producer (cu);
14923
14924 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14925}
14926
c258c396
JD
14927
14928/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14929 with incorrect is_stmt attributes. */
14930
14931static bool
14932producer_is_codewarrior (struct dwarf2_cu *cu)
14933{
14934 if (!cu->checked_producer)
14935 check_producer (cu);
14936
14937 return cu->producer_is_codewarrior;
14938}
14939
60d5a603
JK
14940/* Return the default accessibility type if it is not overriden by
14941 DW_AT_accessibility. */
14942
14943static enum dwarf_access_attribute
14944dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14945{
14946 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14947 {
14948 /* The default DWARF 2 accessibility for members is public, the default
14949 accessibility for inheritance is private. */
14950
14951 if (die->tag != DW_TAG_inheritance)
14952 return DW_ACCESS_public;
14953 else
14954 return DW_ACCESS_private;
14955 }
14956 else
14957 {
14958 /* DWARF 3+ defines the default accessibility a different way. The same
14959 rules apply now for DW_TAG_inheritance as for the members and it only
14960 depends on the container kind. */
14961
14962 if (die->parent->tag == DW_TAG_class_type)
14963 return DW_ACCESS_private;
14964 else
14965 return DW_ACCESS_public;
14966 }
14967}
14968
74ac6d43
TT
14969/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14970 offset. If the attribute was not found return 0, otherwise return
14971 1. If it was found but could not properly be handled, set *OFFSET
14972 to 0. */
14973
14974static int
14975handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14976 LONGEST *offset)
14977{
14978 struct attribute *attr;
14979
14980 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14981 if (attr != NULL)
14982 {
14983 *offset = 0;
14984
14985 /* Note that we do not check for a section offset first here.
14986 This is because DW_AT_data_member_location is new in DWARF 4,
14987 so if we see it, we can assume that a constant form is really
14988 a constant and not a section offset. */
14989 if (attr_form_is_constant (attr))
14990 *offset = dwarf2_get_attr_constant_value (attr, 0);
14991 else if (attr_form_is_section_offset (attr))
14992 dwarf2_complex_location_expr_complaint ();
14993 else if (attr_form_is_block (attr))
14994 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14995 else
14996 dwarf2_complex_location_expr_complaint ();
14997
14998 return 1;
14999 }
15000
15001 return 0;
15002}
15003
c906108c
SS
15004/* Add an aggregate field to the field list. */
15005
15006static void
107d2387 15007dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15008 struct dwarf2_cu *cu)
6e70227d 15009{
518817b3 15010 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15011 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15012 struct nextfield *new_field;
15013 struct attribute *attr;
15014 struct field *fp;
15d034d0 15015 const char *fieldname = "";
c906108c 15016
7d0ccb61
DJ
15017 if (die->tag == DW_TAG_inheritance)
15018 {
be2daae6
TT
15019 fip->baseclasses.emplace_back ();
15020 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15021 }
15022 else
15023 {
be2daae6
TT
15024 fip->fields.emplace_back ();
15025 new_field = &fip->fields.back ();
7d0ccb61 15026 }
be2daae6 15027
c906108c
SS
15028 fip->nfields++;
15029
e142c38c 15030 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15031 if (attr)
15032 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15033 else
15034 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15035 if (new_field->accessibility != DW_ACCESS_public)
15036 fip->non_public_fields = 1;
60d5a603 15037
e142c38c 15038 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15039 if (attr)
15040 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15041 else
15042 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15043
15044 fp = &new_field->field;
a9a9bd0f 15045
e142c38c 15046 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15047 {
74ac6d43
TT
15048 LONGEST offset;
15049
a9a9bd0f 15050 /* Data member other than a C++ static data member. */
6e70227d 15051
c906108c 15052 /* Get type of field. */
e7c27a73 15053 fp->type = die_type (die, cu);
c906108c 15054
d6a843b5 15055 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15056
c906108c 15057 /* Get bit size of field (zero if none). */
e142c38c 15058 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15059 if (attr)
15060 {
15061 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15062 }
15063 else
15064 {
15065 FIELD_BITSIZE (*fp) = 0;
15066 }
15067
15068 /* Get bit offset of field. */
74ac6d43
TT
15069 if (handle_data_member_location (die, cu, &offset))
15070 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15071 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15072 if (attr)
15073 {
5e2b427d 15074 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15075 {
15076 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15077 additional bit offset from the MSB of the containing
15078 anonymous object to the MSB of the field. We don't
15079 have to do anything special since we don't need to
15080 know the size of the anonymous object. */
f41f5e61 15081 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15082 }
15083 else
15084 {
15085 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15086 MSB of the anonymous object, subtract off the number of
15087 bits from the MSB of the field to the MSB of the
15088 object, and then subtract off the number of bits of
15089 the field itself. The result is the bit offset of
15090 the LSB of the field. */
c906108c
SS
15091 int anonymous_size;
15092 int bit_offset = DW_UNSND (attr);
15093
e142c38c 15094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15095 if (attr)
15096 {
15097 /* The size of the anonymous object containing
15098 the bit field is explicit, so use the
15099 indicated size (in bytes). */
15100 anonymous_size = DW_UNSND (attr);
15101 }
15102 else
15103 {
15104 /* The size of the anonymous object containing
15105 the bit field must be inferred from the type
15106 attribute of the data member containing the
15107 bit field. */
15108 anonymous_size = TYPE_LENGTH (fp->type);
15109 }
f41f5e61
PA
15110 SET_FIELD_BITPOS (*fp,
15111 (FIELD_BITPOS (*fp)
15112 + anonymous_size * bits_per_byte
15113 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15114 }
15115 }
da5b30da
AA
15116 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15117 if (attr != NULL)
15118 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15119 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15120
15121 /* Get name of field. */
39cbfefa
DJ
15122 fieldname = dwarf2_name (die, cu);
15123 if (fieldname == NULL)
15124 fieldname = "";
d8151005
DJ
15125
15126 /* The name is already allocated along with this objfile, so we don't
15127 need to duplicate it for the type. */
15128 fp->name = fieldname;
c906108c
SS
15129
15130 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15131 pointer or virtual base class pointer) to private. */
e142c38c 15132 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15133 {
d48cc9dd 15134 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15135 new_field->accessibility = DW_ACCESS_private;
15136 fip->non_public_fields = 1;
15137 }
15138 }
a9a9bd0f 15139 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15140 {
a9a9bd0f
DC
15141 /* C++ static member. */
15142
15143 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15144 is a declaration, but all versions of G++ as of this writing
15145 (so through at least 3.2.1) incorrectly generate
15146 DW_TAG_variable tags. */
6e70227d 15147
ff355380 15148 const char *physname;
c906108c 15149
a9a9bd0f 15150 /* Get name of field. */
39cbfefa
DJ
15151 fieldname = dwarf2_name (die, cu);
15152 if (fieldname == NULL)
c906108c
SS
15153 return;
15154
254e6b9e 15155 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15156 if (attr
15157 /* Only create a symbol if this is an external value.
15158 new_symbol checks this and puts the value in the global symbol
15159 table, which we want. If it is not external, new_symbol
15160 will try to put the value in cu->list_in_scope which is wrong. */
15161 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15162 {
15163 /* A static const member, not much different than an enum as far as
15164 we're concerned, except that we can support more types. */
15165 new_symbol (die, NULL, cu);
15166 }
15167
2df3850c 15168 /* Get physical name. */
ff355380 15169 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15170
d8151005
DJ
15171 /* The name is already allocated along with this objfile, so we don't
15172 need to duplicate it for the type. */
15173 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15174 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15175 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15176 }
15177 else if (die->tag == DW_TAG_inheritance)
15178 {
74ac6d43 15179 LONGEST offset;
d4b96c9a 15180
74ac6d43
TT
15181 /* C++ base class field. */
15182 if (handle_data_member_location (die, cu, &offset))
15183 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15184 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15185 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15186 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15187 }
2ddeaf8a
TT
15188 else if (die->tag == DW_TAG_variant_part)
15189 {
15190 /* process_structure_scope will treat this DIE as a union. */
15191 process_structure_scope (die, cu);
15192
15193 /* The variant part is relative to the start of the enclosing
15194 structure. */
15195 SET_FIELD_BITPOS (*fp, 0);
15196 fp->type = get_die_type (die, cu);
15197 fp->artificial = 1;
15198 fp->name = "<<variant>>";
c8c81635
TT
15199
15200 /* Normally a DW_TAG_variant_part won't have a size, but our
15201 representation requires one, so set it to the maximum of the
15202 child sizes. */
15203 if (TYPE_LENGTH (fp->type) == 0)
15204 {
15205 unsigned max = 0;
15206 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15207 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15208 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15209 TYPE_LENGTH (fp->type) = max;
15210 }
2ddeaf8a
TT
15211 }
15212 else
15213 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15214}
15215
883fd55a
KS
15216/* Can the type given by DIE define another type? */
15217
15218static bool
15219type_can_define_types (const struct die_info *die)
15220{
15221 switch (die->tag)
15222 {
15223 case DW_TAG_typedef:
15224 case DW_TAG_class_type:
15225 case DW_TAG_structure_type:
15226 case DW_TAG_union_type:
15227 case DW_TAG_enumeration_type:
15228 return true;
15229
15230 default:
15231 return false;
15232 }
15233}
15234
15235/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15236
15237static void
883fd55a
KS
15238dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15239 struct dwarf2_cu *cu)
6e70227d 15240{
be2daae6
TT
15241 struct decl_field fp;
15242 memset (&fp, 0, sizeof (fp));
98751a41 15243
883fd55a 15244 gdb_assert (type_can_define_types (die));
98751a41 15245
883fd55a 15246 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15247 fp.name = dwarf2_name (die, cu);
15248 fp.type = read_type_die (die, cu);
98751a41 15249
c191a687
KS
15250 /* Save accessibility. */
15251 enum dwarf_access_attribute accessibility;
15252 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15253 if (attr != NULL)
15254 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15255 else
15256 accessibility = dwarf2_default_access_attribute (die, cu);
15257 switch (accessibility)
15258 {
15259 case DW_ACCESS_public:
15260 /* The assumed value if neither private nor protected. */
15261 break;
15262 case DW_ACCESS_private:
be2daae6 15263 fp.is_private = 1;
c191a687
KS
15264 break;
15265 case DW_ACCESS_protected:
be2daae6 15266 fp.is_protected = 1;
c191a687
KS
15267 break;
15268 default:
b98664d3 15269 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15270 }
15271
883fd55a 15272 if (die->tag == DW_TAG_typedef)
be2daae6 15273 fip->typedef_field_list.push_back (fp);
883fd55a 15274 else
be2daae6 15275 fip->nested_types_list.push_back (fp);
98751a41
JK
15276}
15277
c906108c
SS
15278/* Create the vector of fields, and attach it to the type. */
15279
15280static void
fba45db2 15281dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15282 struct dwarf2_cu *cu)
c906108c
SS
15283{
15284 int nfields = fip->nfields;
15285
15286 /* Record the field count, allocate space for the array of fields,
15287 and create blank accessibility bitfields if necessary. */
15288 TYPE_NFIELDS (type) = nfields;
15289 TYPE_FIELDS (type) = (struct field *)
be2daae6 15290 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15291
b4ba55a1 15292 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15293 {
15294 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15295
15296 TYPE_FIELD_PRIVATE_BITS (type) =
15297 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15298 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15299
15300 TYPE_FIELD_PROTECTED_BITS (type) =
15301 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15302 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15303
774b6a14
TT
15304 TYPE_FIELD_IGNORE_BITS (type) =
15305 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15306 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15307 }
15308
15309 /* If the type has baseclasses, allocate and clear a bit vector for
15310 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15311 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15312 {
be2daae6 15313 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15314 unsigned char *pointer;
c906108c
SS
15315
15316 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15317 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15318 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15319 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15320 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15321 }
15322
2ddeaf8a
TT
15323 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15324 {
15325 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15326
be2daae6 15327 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15328 {
be2daae6
TT
15329 struct nextfield &field = fip->fields[index];
15330
15331 if (field.variant.is_discriminant)
2ddeaf8a 15332 di->discriminant_index = index;
be2daae6 15333 else if (field.variant.default_branch)
2ddeaf8a
TT
15334 di->default_index = index;
15335 else
be2daae6 15336 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15337 }
15338 }
15339
be2daae6
TT
15340 /* Copy the saved-up fields into the field vector. */
15341 for (int i = 0; i < nfields; ++i)
c906108c 15342 {
be2daae6
TT
15343 struct nextfield &field
15344 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15345 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15346
be2daae6
TT
15347 TYPE_FIELD (type, i) = field.field;
15348 switch (field.accessibility)
c906108c 15349 {
c5aa993b 15350 case DW_ACCESS_private:
b4ba55a1 15351 if (cu->language != language_ada)
be2daae6 15352 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15353 break;
c906108c 15354
c5aa993b 15355 case DW_ACCESS_protected:
b4ba55a1 15356 if (cu->language != language_ada)
be2daae6 15357 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15358 break;
c906108c 15359
c5aa993b
JM
15360 case DW_ACCESS_public:
15361 break;
c906108c 15362
c5aa993b
JM
15363 default:
15364 /* Unknown accessibility. Complain and treat it as public. */
15365 {
b98664d3 15366 complaint (_("unsupported accessibility %d"),
be2daae6 15367 field.accessibility);
c5aa993b
JM
15368 }
15369 break;
c906108c 15370 }
be2daae6 15371 if (i < fip->baseclasses.size ())
c906108c 15372 {
be2daae6 15373 switch (field.virtuality)
c906108c 15374 {
c5aa993b
JM
15375 case DW_VIRTUALITY_virtual:
15376 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15377 if (cu->language == language_ada)
a73c6dcd 15378 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15379 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15380 break;
c906108c
SS
15381 }
15382 }
c906108c
SS
15383 }
15384}
15385
7d27a96d
TT
15386/* Return true if this member function is a constructor, false
15387 otherwise. */
15388
15389static int
15390dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15391{
15392 const char *fieldname;
fe978cb0 15393 const char *type_name;
7d27a96d
TT
15394 int len;
15395
15396 if (die->parent == NULL)
15397 return 0;
15398
15399 if (die->parent->tag != DW_TAG_structure_type
15400 && die->parent->tag != DW_TAG_union_type
15401 && die->parent->tag != DW_TAG_class_type)
15402 return 0;
15403
15404 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15405 type_name = dwarf2_name (die->parent, cu);
15406 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15407 return 0;
15408
15409 len = strlen (fieldname);
fe978cb0
PA
15410 return (strncmp (fieldname, type_name, len) == 0
15411 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15412}
15413
c906108c
SS
15414/* Add a member function to the proper fieldlist. */
15415
15416static void
107d2387 15417dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15418 struct type *type, struct dwarf2_cu *cu)
c906108c 15419{
518817b3 15420 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15421 struct attribute *attr;
c906108c 15422 int i;
be2daae6 15423 struct fnfieldlist *flp = nullptr;
c906108c 15424 struct fn_field *fnp;
15d034d0 15425 const char *fieldname;
f792889a 15426 struct type *this_type;
60d5a603 15427 enum dwarf_access_attribute accessibility;
c906108c 15428
b4ba55a1 15429 if (cu->language == language_ada)
a73c6dcd 15430 error (_("unexpected member function in Ada type"));
b4ba55a1 15431
2df3850c 15432 /* Get name of member function. */
39cbfefa
DJ
15433 fieldname = dwarf2_name (die, cu);
15434 if (fieldname == NULL)
2df3850c 15435 return;
c906108c 15436
c906108c 15437 /* Look up member function name in fieldlist. */
be2daae6 15438 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15439 {
27bfe10e 15440 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15441 {
15442 flp = &fip->fnfieldlists[i];
15443 break;
15444 }
c906108c
SS
15445 }
15446
be2daae6
TT
15447 /* Create a new fnfieldlist if necessary. */
15448 if (flp == nullptr)
c906108c 15449 {
be2daae6
TT
15450 fip->fnfieldlists.emplace_back ();
15451 flp = &fip->fnfieldlists.back ();
c906108c 15452 flp->name = fieldname;
be2daae6 15453 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15454 }
15455
be2daae6
TT
15456 /* Create a new member function field and add it to the vector of
15457 fnfieldlists. */
15458 flp->fnfields.emplace_back ();
15459 fnp = &flp->fnfields.back ();
3da10d80
KS
15460
15461 /* Delay processing of the physname until later. */
9c37b5ae 15462 if (cu->language == language_cplus)
be2daae6
TT
15463 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15464 die, cu);
3da10d80
KS
15465 else
15466 {
1d06ead6 15467 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15468 fnp->physname = physname ? physname : "";
15469 }
15470
c906108c 15471 fnp->type = alloc_type (objfile);
f792889a
DJ
15472 this_type = read_type_die (die, cu);
15473 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15474 {
f792889a 15475 int nparams = TYPE_NFIELDS (this_type);
c906108c 15476
f792889a 15477 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15478 of the method itself (TYPE_CODE_METHOD). */
15479 smash_to_method_type (fnp->type, type,
f792889a
DJ
15480 TYPE_TARGET_TYPE (this_type),
15481 TYPE_FIELDS (this_type),
15482 TYPE_NFIELDS (this_type),
15483 TYPE_VARARGS (this_type));
c906108c
SS
15484
15485 /* Handle static member functions.
c5aa993b 15486 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15487 member functions. G++ helps GDB by marking the first
15488 parameter for non-static member functions (which is the this
15489 pointer) as artificial. We obtain this information from
15490 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15491 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15492 fnp->voffset = VOFFSET_STATIC;
15493 }
15494 else
b98664d3 15495 complaint (_("member function type missing for '%s'"),
3da10d80 15496 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15497
15498 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15499 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15500 fnp->fcontext = die_containing_type (die, cu);
c906108c 15501
3e43a32a
MS
15502 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15503 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15504
15505 /* Get accessibility. */
e142c38c 15506 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15507 if (attr)
aead7601 15508 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15509 else
15510 accessibility = dwarf2_default_access_attribute (die, cu);
15511 switch (accessibility)
c906108c 15512 {
60d5a603
JK
15513 case DW_ACCESS_private:
15514 fnp->is_private = 1;
15515 break;
15516 case DW_ACCESS_protected:
15517 fnp->is_protected = 1;
15518 break;
c906108c
SS
15519 }
15520
b02dede2 15521 /* Check for artificial methods. */
e142c38c 15522 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15523 if (attr && DW_UNSND (attr) != 0)
15524 fnp->is_artificial = 1;
15525
7d27a96d
TT
15526 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15527
0d564a31 15528 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15529 function. For older versions of GCC, this is an offset in the
15530 appropriate virtual table, as specified by DW_AT_containing_type.
15531 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15532 to the object address. */
15533
e142c38c 15534 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15535 if (attr)
8e19ed76 15536 {
aec5aa8b 15537 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15538 {
aec5aa8b
TT
15539 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15540 {
15541 /* Old-style GCC. */
15542 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15543 }
15544 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15545 || (DW_BLOCK (attr)->size > 1
15546 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15547 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15548 {
aec5aa8b
TT
15549 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15550 if ((fnp->voffset % cu->header.addr_size) != 0)
15551 dwarf2_complex_location_expr_complaint ();
15552 else
15553 fnp->voffset /= cu->header.addr_size;
15554 fnp->voffset += 2;
15555 }
15556 else
15557 dwarf2_complex_location_expr_complaint ();
15558
15559 if (!fnp->fcontext)
7e993ebf
KS
15560 {
15561 /* If there is no `this' field and no DW_AT_containing_type,
15562 we cannot actually find a base class context for the
15563 vtable! */
15564 if (TYPE_NFIELDS (this_type) == 0
15565 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15566 {
b98664d3 15567 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15568 "function \"%s\" (offset %s)"),
15569 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15570 }
15571 else
15572 {
15573 fnp->fcontext
15574 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15575 }
15576 }
aec5aa8b 15577 }
3690dd37 15578 else if (attr_form_is_section_offset (attr))
8e19ed76 15579 {
4d3c2250 15580 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15581 }
15582 else
15583 {
4d3c2250
KB
15584 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15585 fieldname);
8e19ed76 15586 }
0d564a31 15587 }
d48cc9dd
DJ
15588 else
15589 {
15590 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15591 if (attr && DW_UNSND (attr))
15592 {
15593 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15594 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15595 "but the vtable offset is not specified"),
9d8780f0 15596 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15597 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15598 TYPE_CPLUS_DYNAMIC (type) = 1;
15599 }
15600 }
c906108c
SS
15601}
15602
15603/* Create the vector of member function fields, and attach it to the type. */
15604
15605static void
fba45db2 15606dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15607 struct dwarf2_cu *cu)
c906108c 15608{
b4ba55a1 15609 if (cu->language == language_ada)
a73c6dcd 15610 error (_("unexpected member functions in Ada type"));
b4ba55a1 15611
c906108c
SS
15612 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15613 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15614 TYPE_ALLOC (type,
15615 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15616
be2daae6 15617 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15618 {
be2daae6 15619 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15620 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15621
be2daae6
TT
15622 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15623 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15624 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15625 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15626
15627 for (int k = 0; k < nf.fnfields.size (); ++k)
15628 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15629 }
15630
be2daae6 15631 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15632}
15633
1168df01
JB
15634/* Returns non-zero if NAME is the name of a vtable member in CU's
15635 language, zero otherwise. */
15636static int
15637is_vtable_name (const char *name, struct dwarf2_cu *cu)
15638{
15639 static const char vptr[] = "_vptr";
15640
9c37b5ae
TT
15641 /* Look for the C++ form of the vtable. */
15642 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15643 return 1;
15644
15645 return 0;
15646}
15647
c0dd20ea 15648/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15649 functions, with the ABI-specified layout. If TYPE describes
15650 such a structure, smash it into a member function type.
61049d3b
DJ
15651
15652 GCC shouldn't do this; it should just output pointer to member DIEs.
15653 This is GCC PR debug/28767. */
c0dd20ea 15654
0b92b5bb
TT
15655static void
15656quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15657{
09e2d7c7 15658 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15659
15660 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15661 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15662 return;
c0dd20ea
DJ
15663
15664 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15665 if (TYPE_FIELD_NAME (type, 0) == NULL
15666 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15667 || TYPE_FIELD_NAME (type, 1) == NULL
15668 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15669 return;
c0dd20ea
DJ
15670
15671 /* Find the type of the method. */
0b92b5bb 15672 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15673 if (pfn_type == NULL
15674 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15675 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15676 return;
c0dd20ea
DJ
15677
15678 /* Look for the "this" argument. */
15679 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15680 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15681 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15682 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15683 return;
c0dd20ea 15684
09e2d7c7 15685 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15686 new_type = alloc_type (objfile);
09e2d7c7 15687 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15688 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15689 TYPE_VARARGS (pfn_type));
0b92b5bb 15690 smash_to_methodptr_type (type, new_type);
c0dd20ea 15691}
1168df01 15692
2b4424c3
TT
15693/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15694 appropriate error checking and issuing complaints if there is a
15695 problem. */
15696
15697static ULONGEST
15698get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15699{
15700 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15701
15702 if (attr == nullptr)
15703 return 0;
15704
15705 if (!attr_form_is_constant (attr))
15706 {
b98664d3 15707 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15708 " - DIE at %s [in module %s]"),
15709 sect_offset_str (die->sect_off),
15710 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15711 return 0;
15712 }
15713
15714 ULONGEST align;
15715 if (attr->form == DW_FORM_sdata)
15716 {
15717 LONGEST val = DW_SND (attr);
15718 if (val < 0)
15719 {
b98664d3 15720 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15721 " - DIE at %s [in module %s]"),
15722 sect_offset_str (die->sect_off),
15723 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15724 return 0;
15725 }
15726 align = val;
15727 }
15728 else
15729 align = DW_UNSND (attr);
15730
15731 if (align == 0)
15732 {
b98664d3 15733 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15734 " - DIE at %s [in module %s]"),
15735 sect_offset_str (die->sect_off),
15736 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15737 return 0;
15738 }
15739 if ((align & (align - 1)) != 0)
15740 {
b98664d3 15741 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15742 " - DIE at %s [in module %s]"),
15743 sect_offset_str (die->sect_off),
15744 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15745 return 0;
15746 }
15747
15748 return align;
15749}
15750
15751/* If the DIE has a DW_AT_alignment attribute, use its value to set
15752 the alignment for TYPE. */
15753
15754static void
15755maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15756 struct type *type)
15757{
15758 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15759 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15760 " - DIE at %s [in module %s]"),
15761 sect_offset_str (die->sect_off),
15762 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15763}
685b1105 15764
c906108c 15765/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15766 (definition) to create a type for the structure or union. Fill in
15767 the type's name and general properties; the members will not be
83655187
DE
15768 processed until process_structure_scope. A symbol table entry for
15769 the type will also not be done until process_structure_scope (assuming
15770 the type has a name).
c906108c 15771
c767944b
DJ
15772 NOTE: we need to call these functions regardless of whether or not the
15773 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15774 structure or union. This gets the type entered into our set of
83655187 15775 user defined types. */
c906108c 15776
f792889a 15777static struct type *
134d01f1 15778read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15779{
518817b3 15780 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15781 struct type *type;
15782 struct attribute *attr;
15d034d0 15783 const char *name;
c906108c 15784
348e048f
DE
15785 /* If the definition of this type lives in .debug_types, read that type.
15786 Don't follow DW_AT_specification though, that will take us back up
15787 the chain and we want to go down. */
45e58e77 15788 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15789 if (attr)
15790 {
ac9ec31b 15791 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15792
ac9ec31b 15793 /* The type's CU may not be the same as CU.
02142a6c 15794 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15795 return set_die_type (die, type, cu);
15796 }
15797
c0dd20ea 15798 type = alloc_type (objfile);
c906108c 15799 INIT_CPLUS_SPECIFIC (type);
93311388 15800
39cbfefa
DJ
15801 name = dwarf2_name (die, cu);
15802 if (name != NULL)
c906108c 15803 {
987504bb 15804 if (cu->language == language_cplus
c44af4eb
TT
15805 || cu->language == language_d
15806 || cu->language == language_rust)
63d06c5c 15807 {
15d034d0 15808 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15809
15810 /* dwarf2_full_name might have already finished building the DIE's
15811 type. If so, there is no need to continue. */
15812 if (get_die_type (die, cu) != NULL)
15813 return get_die_type (die, cu);
15814
e86ca25f 15815 TYPE_NAME (type) = full_name;
63d06c5c
DC
15816 }
15817 else
15818 {
d8151005
DJ
15819 /* The name is already allocated along with this objfile, so
15820 we don't need to duplicate it for the type. */
e86ca25f 15821 TYPE_NAME (type) = name;
63d06c5c 15822 }
c906108c
SS
15823 }
15824
15825 if (die->tag == DW_TAG_structure_type)
15826 {
15827 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15828 }
15829 else if (die->tag == DW_TAG_union_type)
15830 {
15831 TYPE_CODE (type) = TYPE_CODE_UNION;
15832 }
2ddeaf8a
TT
15833 else if (die->tag == DW_TAG_variant_part)
15834 {
15835 TYPE_CODE (type) = TYPE_CODE_UNION;
15836 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15837 }
c906108c
SS
15838 else
15839 {
4753d33b 15840 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15841 }
15842
0cc2414c
TT
15843 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15844 TYPE_DECLARED_CLASS (type) = 1;
15845
e142c38c 15846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15847 if (attr)
15848 {
155bfbd3
JB
15849 if (attr_form_is_constant (attr))
15850 TYPE_LENGTH (type) = DW_UNSND (attr);
15851 else
15852 {
15853 /* For the moment, dynamic type sizes are not supported
15854 by GDB's struct type. The actual size is determined
15855 on-demand when resolving the type of a given object,
15856 so set the type's length to zero for now. Otherwise,
15857 we record an expression as the length, and that expression
15858 could lead to a very large value, which could eventually
15859 lead to us trying to allocate that much memory when creating
15860 a value of that type. */
15861 TYPE_LENGTH (type) = 0;
15862 }
c906108c
SS
15863 }
15864 else
15865 {
15866 TYPE_LENGTH (type) = 0;
15867 }
15868
2b4424c3
TT
15869 maybe_set_alignment (cu, die, type);
15870
5230b05a 15871 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15872 {
5230b05a
WT
15873 /* ICC<14 does not output the required DW_AT_declaration on
15874 incomplete types, but gives them a size of zero. */
422b1cb0 15875 TYPE_STUB (type) = 1;
685b1105
JK
15876 }
15877 else
15878 TYPE_STUB_SUPPORTED (type) = 1;
15879
dc718098 15880 if (die_is_declaration (die, cu))
876cecd0 15881 TYPE_STUB (type) = 1;
a6c727b2
DJ
15882 else if (attr == NULL && die->child == NULL
15883 && producer_is_realview (cu->producer))
15884 /* RealView does not output the required DW_AT_declaration
15885 on incomplete types. */
15886 TYPE_STUB (type) = 1;
dc718098 15887
c906108c
SS
15888 /* We need to add the type field to the die immediately so we don't
15889 infinitely recurse when dealing with pointers to the structure
0963b4bd 15890 type within the structure itself. */
1c379e20 15891 set_die_type (die, type, cu);
c906108c 15892
7e314c57
JK
15893 /* set_die_type should be already done. */
15894 set_descriptive_type (type, die, cu);
15895
c767944b
DJ
15896 return type;
15897}
15898
2ddeaf8a
TT
15899/* A helper for process_structure_scope that handles a single member
15900 DIE. */
15901
15902static void
15903handle_struct_member_die (struct die_info *child_die, struct type *type,
15904 struct field_info *fi,
15905 std::vector<struct symbol *> *template_args,
15906 struct dwarf2_cu *cu)
15907{
15908 if (child_die->tag == DW_TAG_member
15909 || child_die->tag == DW_TAG_variable
15910 || child_die->tag == DW_TAG_variant_part)
15911 {
15912 /* NOTE: carlton/2002-11-05: A C++ static data member
15913 should be a DW_TAG_member that is a declaration, but
15914 all versions of G++ as of this writing (so through at
15915 least 3.2.1) incorrectly generate DW_TAG_variable
15916 tags for them instead. */
15917 dwarf2_add_field (fi, child_die, cu);
15918 }
15919 else if (child_die->tag == DW_TAG_subprogram)
15920 {
15921 /* Rust doesn't have member functions in the C++ sense.
15922 However, it does emit ordinary functions as children
15923 of a struct DIE. */
15924 if (cu->language == language_rust)
15925 read_func_scope (child_die, cu);
15926 else
15927 {
15928 /* C++ member function. */
15929 dwarf2_add_member_fn (fi, child_die, type, cu);
15930 }
15931 }
15932 else if (child_die->tag == DW_TAG_inheritance)
15933 {
15934 /* C++ base class field. */
15935 dwarf2_add_field (fi, child_die, cu);
15936 }
15937 else if (type_can_define_types (child_die))
15938 dwarf2_add_type_defn (fi, child_die, cu);
15939 else if (child_die->tag == DW_TAG_template_type_param
15940 || child_die->tag == DW_TAG_template_value_param)
15941 {
15942 struct symbol *arg = new_symbol (child_die, NULL, cu);
15943
15944 if (arg != NULL)
15945 template_args->push_back (arg);
15946 }
15947 else if (child_die->tag == DW_TAG_variant)
15948 {
15949 /* In a variant we want to get the discriminant and also add a
15950 field for our sole member child. */
15951 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15952
bde09ab7 15953 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15954 variant_child != NULL;
15955 variant_child = sibling_die (variant_child))
15956 {
15957 if (variant_child->tag == DW_TAG_member)
15958 {
15959 handle_struct_member_die (variant_child, type, fi,
15960 template_args, cu);
15961 /* Only handle the one. */
15962 break;
15963 }
15964 }
15965
15966 /* We don't handle this but we might as well report it if we see
15967 it. */
15968 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15969 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15970 " - DIE at %s [in module %s]"),
15971 sect_offset_str (child_die->sect_off),
15972 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15973
15974 /* The first field was just added, so we can stash the
15975 discriminant there. */
be2daae6 15976 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15977 if (discr == NULL)
be2daae6 15978 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15979 else
be2daae6 15980 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15981 }
15982}
15983
c767944b
DJ
15984/* Finish creating a structure or union type, including filling in
15985 its members and creating a symbol for it. */
15986
15987static void
15988process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15989{
518817b3 15990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15991 struct die_info *child_die;
c767944b
DJ
15992 struct type *type;
15993
15994 type = get_die_type (die, cu);
15995 if (type == NULL)
15996 type = read_structure_type (die, cu);
15997
2ddeaf8a
TT
15998 /* When reading a DW_TAG_variant_part, we need to notice when we
15999 read the discriminant member, so we can record it later in the
16000 discriminant_info. */
16001 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16002 sect_offset discr_offset;
3e1d3d8c 16003 bool has_template_parameters = false;
2ddeaf8a
TT
16004
16005 if (is_variant_part)
16006 {
16007 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16008 if (discr == NULL)
16009 {
16010 /* Maybe it's a univariant form, an extension we support.
16011 In this case arrange not to check the offset. */
16012 is_variant_part = false;
16013 }
16014 else if (attr_form_is_ref (discr))
16015 {
16016 struct dwarf2_cu *target_cu = cu;
16017 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16018
16019 discr_offset = target_die->sect_off;
16020 }
16021 else
16022 {
b98664d3 16023 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16024 " - DIE at %s [in module %s]"),
16025 sect_offset_str (die->sect_off),
16026 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16027 is_variant_part = false;
16028 }
16029 }
16030
e142c38c 16031 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16032 {
16033 struct field_info fi;
2f4732b0 16034 std::vector<struct symbol *> template_args;
c906108c 16035
639d11d3 16036 child_die = die->child;
c906108c
SS
16037
16038 while (child_die && child_die->tag)
16039 {
2ddeaf8a 16040 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16041
2ddeaf8a 16042 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16043 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16044
c906108c
SS
16045 child_die = sibling_die (child_die);
16046 }
16047
34eaf542 16048 /* Attach template arguments to type. */
2f4732b0 16049 if (!template_args.empty ())
34eaf542 16050 {
3e1d3d8c 16051 has_template_parameters = true;
34eaf542 16052 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16053 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16054 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16055 = XOBNEWVEC (&objfile->objfile_obstack,
16056 struct symbol *,
16057 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16058 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16059 template_args.data (),
34eaf542
TT
16060 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16061 * sizeof (struct symbol *)));
34eaf542
TT
16062 }
16063
c906108c
SS
16064 /* Attach fields and member functions to the type. */
16065 if (fi.nfields)
e7c27a73 16066 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16067 if (!fi.fnfieldlists.empty ())
c906108c 16068 {
e7c27a73 16069 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16070
c5aa993b 16071 /* Get the type which refers to the base class (possibly this
c906108c 16072 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16073 class from the DW_AT_containing_type attribute. This use of
16074 DW_AT_containing_type is a GNU extension. */
c906108c 16075
e142c38c 16076 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16077 {
e7c27a73 16078 struct type *t = die_containing_type (die, cu);
c906108c 16079
ae6ae975 16080 set_type_vptr_basetype (type, t);
c906108c
SS
16081 if (type == t)
16082 {
c906108c
SS
16083 int i;
16084
16085 /* Our own class provides vtbl ptr. */
16086 for (i = TYPE_NFIELDS (t) - 1;
16087 i >= TYPE_N_BASECLASSES (t);
16088 --i)
16089 {
0d5cff50 16090 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16091
1168df01 16092 if (is_vtable_name (fieldname, cu))
c906108c 16093 {
ae6ae975 16094 set_type_vptr_fieldno (type, i);
c906108c
SS
16095 break;
16096 }
16097 }
16098
16099 /* Complain if virtual function table field not found. */
16100 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16101 complaint (_("virtual function table pointer "
3e43a32a 16102 "not found when defining class '%s'"),
e86ca25f 16103 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16104 }
16105 else
16106 {
ae6ae975 16107 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16108 }
16109 }
f6235d4c 16110 else if (cu->producer
61012eef 16111 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16112 {
16113 /* The IBM XLC compiler does not provide direct indication
16114 of the containing type, but the vtable pointer is
16115 always named __vfp. */
16116
16117 int i;
16118
16119 for (i = TYPE_NFIELDS (type) - 1;
16120 i >= TYPE_N_BASECLASSES (type);
16121 --i)
16122 {
16123 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16124 {
ae6ae975
DE
16125 set_type_vptr_fieldno (type, i);
16126 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16127 break;
16128 }
16129 }
16130 }
c906108c 16131 }
98751a41
JK
16132
16133 /* Copy fi.typedef_field_list linked list elements content into the
16134 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16135 if (!fi.typedef_field_list.empty ())
98751a41 16136 {
be2daae6 16137 int count = fi.typedef_field_list.size ();
98751a41 16138
a0d7a4ff 16139 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16140 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16141 = ((struct decl_field *)
be2daae6
TT
16142 TYPE_ALLOC (type,
16143 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16144 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16145
be2daae6
TT
16146 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16147 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16148 }
c767944b 16149
883fd55a
KS
16150 /* Copy fi.nested_types_list linked list elements content into the
16151 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16152 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16153 {
be2daae6 16154 int count = fi.nested_types_list.size ();
883fd55a
KS
16155
16156 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16157 TYPE_NESTED_TYPES_ARRAY (type)
16158 = ((struct decl_field *)
be2daae6
TT
16159 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16160 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16161
be2daae6
TT
16162 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16163 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16164 }
c906108c 16165 }
63d06c5c 16166
bb5ed363 16167 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16168 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16169 cu->rust_unions.push_back (type);
0b92b5bb 16170
90aeadfc
DC
16171 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16172 snapshots) has been known to create a die giving a declaration
16173 for a class that has, as a child, a die giving a definition for a
16174 nested class. So we have to process our children even if the
16175 current die is a declaration. Normally, of course, a declaration
16176 won't have any children at all. */
134d01f1 16177
ca040673
DE
16178 child_die = die->child;
16179
90aeadfc
DC
16180 while (child_die != NULL && child_die->tag)
16181 {
16182 if (child_die->tag == DW_TAG_member
16183 || child_die->tag == DW_TAG_variable
34eaf542
TT
16184 || child_die->tag == DW_TAG_inheritance
16185 || child_die->tag == DW_TAG_template_value_param
16186 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16187 {
90aeadfc 16188 /* Do nothing. */
134d01f1 16189 }
90aeadfc
DC
16190 else
16191 process_die (child_die, cu);
134d01f1 16192
90aeadfc 16193 child_die = sibling_die (child_die);
134d01f1
DJ
16194 }
16195
fa4028e9
JB
16196 /* Do not consider external references. According to the DWARF standard,
16197 these DIEs are identified by the fact that they have no byte_size
16198 attribute, and a declaration attribute. */
16199 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16200 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16201 {
16202 struct symbol *sym = new_symbol (die, type, cu);
16203
16204 if (has_template_parameters)
16205 {
a776957c
TT
16206 struct symtab *symtab;
16207 if (sym != nullptr)
16208 symtab = symbol_symtab (sym);
16209 else if (cu->line_header != nullptr)
16210 {
16211 /* Any related symtab will do. */
16212 symtab
16213 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16214 }
16215 else
16216 {
16217 symtab = nullptr;
16218 complaint (_("could not find suitable "
16219 "symtab for template parameter"
16220 " - DIE at %s [in module %s]"),
16221 sect_offset_str (die->sect_off),
16222 objfile_name (objfile));
16223 }
16224
16225 if (symtab != nullptr)
16226 {
16227 /* Make sure that the symtab is set on the new symbols.
16228 Even though they don't appear in this symtab directly,
16229 other parts of gdb assume that symbols do, and this is
16230 reasonably true. */
16231 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16232 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16233 }
3e1d3d8c
TT
16234 }
16235 }
134d01f1
DJ
16236}
16237
55426c9d
JB
16238/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16239 update TYPE using some information only available in DIE's children. */
16240
16241static void
16242update_enumeration_type_from_children (struct die_info *die,
16243 struct type *type,
16244 struct dwarf2_cu *cu)
16245{
60f7655a 16246 struct die_info *child_die;
55426c9d
JB
16247 int unsigned_enum = 1;
16248 int flag_enum = 1;
16249 ULONGEST mask = 0;
55426c9d 16250
8268c778 16251 auto_obstack obstack;
55426c9d 16252
60f7655a
DE
16253 for (child_die = die->child;
16254 child_die != NULL && child_die->tag;
16255 child_die = sibling_die (child_die))
55426c9d
JB
16256 {
16257 struct attribute *attr;
16258 LONGEST value;
16259 const gdb_byte *bytes;
16260 struct dwarf2_locexpr_baton *baton;
16261 const char *name;
60f7655a 16262
55426c9d
JB
16263 if (child_die->tag != DW_TAG_enumerator)
16264 continue;
16265
16266 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16267 if (attr == NULL)
16268 continue;
16269
16270 name = dwarf2_name (child_die, cu);
16271 if (name == NULL)
16272 name = "<anonymous enumerator>";
16273
16274 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16275 &value, &bytes, &baton);
16276 if (value < 0)
16277 {
16278 unsigned_enum = 0;
16279 flag_enum = 0;
16280 }
16281 else if ((mask & value) != 0)
16282 flag_enum = 0;
16283 else
16284 mask |= value;
16285
16286 /* If we already know that the enum type is neither unsigned, nor
16287 a flag type, no need to look at the rest of the enumerates. */
16288 if (!unsigned_enum && !flag_enum)
16289 break;
55426c9d
JB
16290 }
16291
16292 if (unsigned_enum)
16293 TYPE_UNSIGNED (type) = 1;
16294 if (flag_enum)
16295 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16296}
16297
134d01f1
DJ
16298/* Given a DW_AT_enumeration_type die, set its type. We do not
16299 complete the type's fields yet, or create any symbols. */
c906108c 16300
f792889a 16301static struct type *
134d01f1 16302read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16303{
518817b3 16304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16305 struct type *type;
c906108c 16306 struct attribute *attr;
0114d602 16307 const char *name;
134d01f1 16308
348e048f
DE
16309 /* If the definition of this type lives in .debug_types, read that type.
16310 Don't follow DW_AT_specification though, that will take us back up
16311 the chain and we want to go down. */
45e58e77 16312 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16313 if (attr)
16314 {
ac9ec31b 16315 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16316
ac9ec31b 16317 /* The type's CU may not be the same as CU.
02142a6c 16318 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16319 return set_die_type (die, type, cu);
16320 }
16321
c906108c
SS
16322 type = alloc_type (objfile);
16323
16324 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16325 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16326 if (name != NULL)
e86ca25f 16327 TYPE_NAME (type) = name;
c906108c 16328
0626fc76
TT
16329 attr = dwarf2_attr (die, DW_AT_type, cu);
16330 if (attr != NULL)
16331 {
16332 struct type *underlying_type = die_type (die, cu);
16333
16334 TYPE_TARGET_TYPE (type) = underlying_type;
16335 }
16336
e142c38c 16337 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16338 if (attr)
16339 {
16340 TYPE_LENGTH (type) = DW_UNSND (attr);
16341 }
16342 else
16343 {
16344 TYPE_LENGTH (type) = 0;
16345 }
16346
2b4424c3
TT
16347 maybe_set_alignment (cu, die, type);
16348
137033e9
JB
16349 /* The enumeration DIE can be incomplete. In Ada, any type can be
16350 declared as private in the package spec, and then defined only
16351 inside the package body. Such types are known as Taft Amendment
16352 Types. When another package uses such a type, an incomplete DIE
16353 may be generated by the compiler. */
02eb380e 16354 if (die_is_declaration (die, cu))
876cecd0 16355 TYPE_STUB (type) = 1;
02eb380e 16356
0626fc76
TT
16357 /* Finish the creation of this type by using the enum's children.
16358 We must call this even when the underlying type has been provided
16359 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16360 update_enumeration_type_from_children (die, type, cu);
16361
0626fc76
TT
16362 /* If this type has an underlying type that is not a stub, then we
16363 may use its attributes. We always use the "unsigned" attribute
16364 in this situation, because ordinarily we guess whether the type
16365 is unsigned -- but the guess can be wrong and the underlying type
16366 can tell us the reality. However, we defer to a local size
16367 attribute if one exists, because this lets the compiler override
16368 the underlying type if needed. */
16369 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16370 {
16371 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16372 if (TYPE_LENGTH (type) == 0)
16373 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16374 if (TYPE_RAW_ALIGN (type) == 0
16375 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16376 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16377 }
16378
3d567982
TT
16379 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16380
f792889a 16381 return set_die_type (die, type, cu);
134d01f1
DJ
16382}
16383
16384/* Given a pointer to a die which begins an enumeration, process all
16385 the dies that define the members of the enumeration, and create the
16386 symbol for the enumeration type.
16387
16388 NOTE: We reverse the order of the element list. */
16389
16390static void
16391process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16392{
f792889a 16393 struct type *this_type;
134d01f1 16394
f792889a
DJ
16395 this_type = get_die_type (die, cu);
16396 if (this_type == NULL)
16397 this_type = read_enumeration_type (die, cu);
9dc481d3 16398
639d11d3 16399 if (die->child != NULL)
c906108c 16400 {
9dc481d3
DE
16401 struct die_info *child_die;
16402 struct symbol *sym;
16403 struct field *fields = NULL;
16404 int num_fields = 0;
15d034d0 16405 const char *name;
9dc481d3 16406
639d11d3 16407 child_die = die->child;
c906108c
SS
16408 while (child_die && child_die->tag)
16409 {
16410 if (child_die->tag != DW_TAG_enumerator)
16411 {
e7c27a73 16412 process_die (child_die, cu);
c906108c
SS
16413 }
16414 else
16415 {
39cbfefa
DJ
16416 name = dwarf2_name (child_die, cu);
16417 if (name)
c906108c 16418 {
f792889a 16419 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16420
16421 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16422 {
16423 fields = (struct field *)
16424 xrealloc (fields,
16425 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16426 * sizeof (struct field));
c906108c
SS
16427 }
16428
3567439c 16429 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16430 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16431 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16432 FIELD_BITSIZE (fields[num_fields]) = 0;
16433
16434 num_fields++;
16435 }
16436 }
16437
16438 child_die = sibling_die (child_die);
16439 }
16440
16441 if (num_fields)
16442 {
f792889a
DJ
16443 TYPE_NFIELDS (this_type) = num_fields;
16444 TYPE_FIELDS (this_type) = (struct field *)
16445 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16446 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16447 sizeof (struct field) * num_fields);
b8c9b27d 16448 xfree (fields);
c906108c 16449 }
c906108c 16450 }
134d01f1 16451
6c83ed52
TT
16452 /* If we are reading an enum from a .debug_types unit, and the enum
16453 is a declaration, and the enum is not the signatured type in the
16454 unit, then we do not want to add a symbol for it. Adding a
16455 symbol would in some cases obscure the true definition of the
16456 enum, giving users an incomplete type when the definition is
16457 actually available. Note that we do not want to do this for all
16458 enums which are just declarations, because C++0x allows forward
16459 enum declarations. */
3019eac3 16460 if (cu->per_cu->is_debug_types
6c83ed52
TT
16461 && die_is_declaration (die, cu))
16462 {
52dc124a 16463 struct signatured_type *sig_type;
6c83ed52 16464
c0f78cd4 16465 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16466 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16467 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16468 return;
16469 }
16470
f792889a 16471 new_symbol (die, this_type, cu);
c906108c
SS
16472}
16473
16474/* Extract all information from a DW_TAG_array_type DIE and put it in
16475 the DIE's type field. For now, this only handles one dimensional
16476 arrays. */
16477
f792889a 16478static struct type *
e7c27a73 16479read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16480{
518817b3 16481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16482 struct die_info *child_die;
7e314c57 16483 struct type *type;
c906108c 16484 struct type *element_type, *range_type, *index_type;
c906108c 16485 struct attribute *attr;
15d034d0 16486 const char *name;
a405673c 16487 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16488 unsigned int bit_stride = 0;
c906108c 16489
e7c27a73 16490 element_type = die_type (die, cu);
c906108c 16491
7e314c57
JK
16492 /* The die_type call above may have already set the type for this DIE. */
16493 type = get_die_type (die, cu);
16494 if (type)
16495 return type;
16496
dc53a7ad
JB
16497 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16498 if (attr != NULL)
a405673c
JB
16499 {
16500 int stride_ok;
9a49df9d
AB
16501 struct type *prop_type
16502 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16503
16504 byte_stride_prop
16505 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16506 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16507 prop_type);
a405673c
JB
16508 if (!stride_ok)
16509 {
b98664d3 16510 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16511 " - DIE at %s [in module %s]"),
16512 sect_offset_str (die->sect_off),
518817b3 16513 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16514 /* Ignore this attribute. We will likely not be able to print
16515 arrays of this type correctly, but there is little we can do
16516 to help if we cannot read the attribute's value. */
16517 byte_stride_prop = NULL;
16518 }
16519 }
dc53a7ad
JB
16520
16521 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16522 if (attr != NULL)
16523 bit_stride = DW_UNSND (attr);
16524
c906108c
SS
16525 /* Irix 6.2 native cc creates array types without children for
16526 arrays with unspecified length. */
639d11d3 16527 if (die->child == NULL)
c906108c 16528 {
46bf5051 16529 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16530 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16531 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16532 byte_stride_prop, bit_stride);
f792889a 16533 return set_die_type (die, type, cu);
c906108c
SS
16534 }
16535
791afaa2 16536 std::vector<struct type *> range_types;
639d11d3 16537 child_die = die->child;
c906108c
SS
16538 while (child_die && child_die->tag)
16539 {
16540 if (child_die->tag == DW_TAG_subrange_type)
16541 {
f792889a 16542 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16543
f792889a 16544 if (child_type != NULL)
a02abb62 16545 {
0963b4bd
MS
16546 /* The range type was succesfully read. Save it for the
16547 array type creation. */
791afaa2 16548 range_types.push_back (child_type);
a02abb62 16549 }
c906108c
SS
16550 }
16551 child_die = sibling_die (child_die);
16552 }
16553
16554 /* Dwarf2 dimensions are output from left to right, create the
16555 necessary array types in backwards order. */
7ca2d3a3 16556
c906108c 16557 type = element_type;
7ca2d3a3
DL
16558
16559 if (read_array_order (die, cu) == DW_ORD_col_major)
16560 {
16561 int i = 0;
9a619af0 16562
791afaa2 16563 while (i < range_types.size ())
dc53a7ad 16564 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16565 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16566 }
16567 else
16568 {
791afaa2 16569 size_t ndim = range_types.size ();
7ca2d3a3 16570 while (ndim-- > 0)
dc53a7ad 16571 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16572 byte_stride_prop, bit_stride);
7ca2d3a3 16573 }
c906108c 16574
f5f8a009
EZ
16575 /* Understand Dwarf2 support for vector types (like they occur on
16576 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16577 array type. This is not part of the Dwarf2/3 standard yet, but a
16578 custom vendor extension. The main difference between a regular
16579 array and the vector variant is that vectors are passed by value
16580 to functions. */
e142c38c 16581 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16582 if (attr)
ea37ba09 16583 make_vector_type (type);
f5f8a009 16584
dbc98a8b
KW
16585 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16586 implementation may choose to implement triple vectors using this
16587 attribute. */
16588 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16589 if (attr)
16590 {
16591 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16592 TYPE_LENGTH (type) = DW_UNSND (attr);
16593 else
b98664d3 16594 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16595 "than the total size of elements"));
dbc98a8b
KW
16596 }
16597
39cbfefa
DJ
16598 name = dwarf2_name (die, cu);
16599 if (name)
16600 TYPE_NAME (type) = name;
6e70227d 16601
2b4424c3
TT
16602 maybe_set_alignment (cu, die, type);
16603
0963b4bd 16604 /* Install the type in the die. */
7e314c57
JK
16605 set_die_type (die, type, cu);
16606
16607 /* set_die_type should be already done. */
b4ba55a1
JB
16608 set_descriptive_type (type, die, cu);
16609
7e314c57 16610 return type;
c906108c
SS
16611}
16612
7ca2d3a3 16613static enum dwarf_array_dim_ordering
6e70227d 16614read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16615{
16616 struct attribute *attr;
16617
16618 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16619
aead7601
SM
16620 if (attr)
16621 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16622
0963b4bd
MS
16623 /* GNU F77 is a special case, as at 08/2004 array type info is the
16624 opposite order to the dwarf2 specification, but data is still
16625 laid out as per normal fortran.
7ca2d3a3 16626
0963b4bd
MS
16627 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16628 version checking. */
7ca2d3a3 16629
905e0470
PM
16630 if (cu->language == language_fortran
16631 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16632 {
16633 return DW_ORD_row_major;
16634 }
16635
6e70227d 16636 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16637 {
16638 case array_column_major:
16639 return DW_ORD_col_major;
16640 case array_row_major:
16641 default:
16642 return DW_ORD_row_major;
16643 };
16644}
16645
72019c9c 16646/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16647 the DIE's type field. */
72019c9c 16648
f792889a 16649static struct type *
72019c9c
GM
16650read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16651{
7e314c57
JK
16652 struct type *domain_type, *set_type;
16653 struct attribute *attr;
f792889a 16654
7e314c57
JK
16655 domain_type = die_type (die, cu);
16656
16657 /* The die_type call above may have already set the type for this DIE. */
16658 set_type = get_die_type (die, cu);
16659 if (set_type)
16660 return set_type;
16661
16662 set_type = create_set_type (NULL, domain_type);
16663
16664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16665 if (attr)
16666 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16667
2b4424c3
TT
16668 maybe_set_alignment (cu, die, set_type);
16669
f792889a 16670 return set_die_type (die, set_type, cu);
72019c9c 16671}
7ca2d3a3 16672
0971de02
TT
16673/* A helper for read_common_block that creates a locexpr baton.
16674 SYM is the symbol which we are marking as computed.
16675 COMMON_DIE is the DIE for the common block.
16676 COMMON_LOC is the location expression attribute for the common
16677 block itself.
16678 MEMBER_LOC is the location expression attribute for the particular
16679 member of the common block that we are processing.
16680 CU is the CU from which the above come. */
16681
16682static void
16683mark_common_block_symbol_computed (struct symbol *sym,
16684 struct die_info *common_die,
16685 struct attribute *common_loc,
16686 struct attribute *member_loc,
16687 struct dwarf2_cu *cu)
16688{
518817b3
SM
16689 struct dwarf2_per_objfile *dwarf2_per_objfile
16690 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16691 struct objfile *objfile = dwarf2_per_objfile->objfile;
16692 struct dwarf2_locexpr_baton *baton;
16693 gdb_byte *ptr;
16694 unsigned int cu_off;
16695 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16696 LONGEST offset = 0;
16697
16698 gdb_assert (common_loc && member_loc);
16699 gdb_assert (attr_form_is_block (common_loc));
16700 gdb_assert (attr_form_is_block (member_loc)
16701 || attr_form_is_constant (member_loc));
16702
8d749320 16703 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16704 baton->per_cu = cu->per_cu;
16705 gdb_assert (baton->per_cu);
16706
16707 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16708
16709 if (attr_form_is_constant (member_loc))
16710 {
16711 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16712 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16713 }
16714 else
16715 baton->size += DW_BLOCK (member_loc)->size;
16716
224c3ddb 16717 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16718 baton->data = ptr;
16719
16720 *ptr++ = DW_OP_call4;
9c541725 16721 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16722 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16723 ptr += 4;
16724
16725 if (attr_form_is_constant (member_loc))
16726 {
16727 *ptr++ = DW_OP_addr;
16728 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16729 ptr += cu->header.addr_size;
16730 }
16731 else
16732 {
16733 /* We have to copy the data here, because DW_OP_call4 will only
16734 use a DW_AT_location attribute. */
16735 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16736 ptr += DW_BLOCK (member_loc)->size;
16737 }
16738
16739 *ptr++ = DW_OP_plus;
16740 gdb_assert (ptr - baton->data == baton->size);
16741
0971de02 16742 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16743 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16744}
16745
4357ac6c
TT
16746/* Create appropriate locally-scoped variables for all the
16747 DW_TAG_common_block entries. Also create a struct common_block
16748 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16749 is used to sepate the common blocks name namespace from regular
16750 variable names. */
c906108c
SS
16751
16752static void
e7c27a73 16753read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16754{
0971de02
TT
16755 struct attribute *attr;
16756
16757 attr = dwarf2_attr (die, DW_AT_location, cu);
16758 if (attr)
16759 {
16760 /* Support the .debug_loc offsets. */
16761 if (attr_form_is_block (attr))
16762 {
16763 /* Ok. */
16764 }
16765 else if (attr_form_is_section_offset (attr))
16766 {
16767 dwarf2_complex_location_expr_complaint ();
16768 attr = NULL;
16769 }
16770 else
16771 {
16772 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16773 "common block member");
16774 attr = NULL;
16775 }
16776 }
16777
639d11d3 16778 if (die->child != NULL)
c906108c 16779 {
518817b3 16780 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16781 struct die_info *child_die;
16782 size_t n_entries = 0, size;
16783 struct common_block *common_block;
16784 struct symbol *sym;
74ac6d43 16785
4357ac6c
TT
16786 for (child_die = die->child;
16787 child_die && child_die->tag;
16788 child_die = sibling_die (child_die))
16789 ++n_entries;
16790
16791 size = (sizeof (struct common_block)
16792 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16793 common_block
16794 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16795 size);
4357ac6c
TT
16796 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16797 common_block->n_entries = 0;
16798
16799 for (child_die = die->child;
16800 child_die && child_die->tag;
16801 child_die = sibling_die (child_die))
16802 {
16803 /* Create the symbol in the DW_TAG_common_block block in the current
16804 symbol scope. */
e7c27a73 16805 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16806 if (sym != NULL)
16807 {
16808 struct attribute *member_loc;
16809
16810 common_block->contents[common_block->n_entries++] = sym;
16811
16812 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16813 cu);
16814 if (member_loc)
16815 {
16816 /* GDB has handled this for a long time, but it is
16817 not specified by DWARF. It seems to have been
16818 emitted by gfortran at least as recently as:
16819 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16820 complaint (_("Variable in common block has "
0971de02 16821 "DW_AT_data_member_location "
9d8780f0
SM
16822 "- DIE at %s [in module %s]"),
16823 sect_offset_str (child_die->sect_off),
518817b3 16824 objfile_name (objfile));
0971de02
TT
16825
16826 if (attr_form_is_section_offset (member_loc))
16827 dwarf2_complex_location_expr_complaint ();
16828 else if (attr_form_is_constant (member_loc)
16829 || attr_form_is_block (member_loc))
16830 {
16831 if (attr)
16832 mark_common_block_symbol_computed (sym, die, attr,
16833 member_loc, cu);
16834 }
16835 else
16836 dwarf2_complex_location_expr_complaint ();
16837 }
16838 }
c906108c 16839 }
4357ac6c
TT
16840
16841 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16842 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16843 }
16844}
16845
0114d602 16846/* Create a type for a C++ namespace. */
d9fa45fe 16847
0114d602
DJ
16848static struct type *
16849read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16850{
518817b3 16851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16852 const char *previous_prefix, *name;
9219021c 16853 int is_anonymous;
0114d602
DJ
16854 struct type *type;
16855
16856 /* For extensions, reuse the type of the original namespace. */
16857 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16858 {
16859 struct die_info *ext_die;
16860 struct dwarf2_cu *ext_cu = cu;
9a619af0 16861
0114d602
DJ
16862 ext_die = dwarf2_extension (die, &ext_cu);
16863 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16864
16865 /* EXT_CU may not be the same as CU.
02142a6c 16866 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16867 return set_die_type (die, type, cu);
16868 }
9219021c 16869
e142c38c 16870 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16871
16872 /* Now build the name of the current namespace. */
16873
0114d602
DJ
16874 previous_prefix = determine_prefix (die, cu);
16875 if (previous_prefix[0] != '\0')
16876 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16877 previous_prefix, name, 0, cu);
0114d602
DJ
16878
16879 /* Create the type. */
19f392bc 16880 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16881
60531b24 16882 return set_die_type (die, type, cu);
0114d602
DJ
16883}
16884
22cee43f 16885/* Read a namespace scope. */
0114d602
DJ
16886
16887static void
16888read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16889{
518817b3 16890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16891 int is_anonymous;
9219021c 16892
5c4e30ca
DC
16893 /* Add a symbol associated to this if we haven't seen the namespace
16894 before. Also, add a using directive if it's an anonymous
16895 namespace. */
9219021c 16896
f2f0e013 16897 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16898 {
16899 struct type *type;
16900
0114d602 16901 type = read_type_die (die, cu);
e7c27a73 16902 new_symbol (die, type, cu);
5c4e30ca 16903
e8e80198 16904 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16905 if (is_anonymous)
0114d602
DJ
16906 {
16907 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16908
eb1e02fd 16909 std::vector<const char *> excludes;
804d2729 16910 add_using_directive (using_directives (cu),
22cee43f 16911 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16912 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16913 }
5c4e30ca 16914 }
9219021c 16915
639d11d3 16916 if (die->child != NULL)
d9fa45fe 16917 {
639d11d3 16918 struct die_info *child_die = die->child;
6e70227d 16919
d9fa45fe
DC
16920 while (child_die && child_die->tag)
16921 {
e7c27a73 16922 process_die (child_die, cu);
d9fa45fe
DC
16923 child_die = sibling_die (child_die);
16924 }
16925 }
38d518c9
EZ
16926}
16927
f55ee35c
JK
16928/* Read a Fortran module as type. This DIE can be only a declaration used for
16929 imported module. Still we need that type as local Fortran "use ... only"
16930 declaration imports depend on the created type in determine_prefix. */
16931
16932static struct type *
16933read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16934{
518817b3 16935 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16936 const char *module_name;
f55ee35c
JK
16937 struct type *type;
16938
16939 module_name = dwarf2_name (die, cu);
19f392bc 16940 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16941
f55ee35c
JK
16942 return set_die_type (die, type, cu);
16943}
16944
5d7cb8df
JK
16945/* Read a Fortran module. */
16946
16947static void
16948read_module (struct die_info *die, struct dwarf2_cu *cu)
16949{
16950 struct die_info *child_die = die->child;
530e8392
KB
16951 struct type *type;
16952
16953 type = read_type_die (die, cu);
16954 new_symbol (die, type, cu);
5d7cb8df 16955
5d7cb8df
JK
16956 while (child_die && child_die->tag)
16957 {
16958 process_die (child_die, cu);
16959 child_die = sibling_die (child_die);
16960 }
16961}
16962
38d518c9
EZ
16963/* Return the name of the namespace represented by DIE. Set
16964 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16965 namespace. */
16966
16967static const char *
e142c38c 16968namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16969{
16970 struct die_info *current_die;
16971 const char *name = NULL;
16972
16973 /* Loop through the extensions until we find a name. */
16974
16975 for (current_die = die;
16976 current_die != NULL;
f2f0e013 16977 current_die = dwarf2_extension (die, &cu))
38d518c9 16978 {
96553a0c
DE
16979 /* We don't use dwarf2_name here so that we can detect the absence
16980 of a name -> anonymous namespace. */
7d45c7c3 16981 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16982
38d518c9
EZ
16983 if (name != NULL)
16984 break;
16985 }
16986
16987 /* Is it an anonymous namespace? */
16988
16989 *is_anonymous = (name == NULL);
16990 if (*is_anonymous)
2b1dbab0 16991 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16992
16993 return name;
d9fa45fe
DC
16994}
16995
c906108c
SS
16996/* Extract all information from a DW_TAG_pointer_type DIE and add to
16997 the user defined type vector. */
16998
f792889a 16999static struct type *
e7c27a73 17000read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17001{
518817b3
SM
17002 struct gdbarch *gdbarch
17003 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17004 struct comp_unit_head *cu_header = &cu->header;
c906108c 17005 struct type *type;
8b2dbe47
KB
17006 struct attribute *attr_byte_size;
17007 struct attribute *attr_address_class;
17008 int byte_size, addr_class;
7e314c57
JK
17009 struct type *target_type;
17010
17011 target_type = die_type (die, cu);
c906108c 17012
7e314c57
JK
17013 /* The die_type call above may have already set the type for this DIE. */
17014 type = get_die_type (die, cu);
17015 if (type)
17016 return type;
17017
17018 type = lookup_pointer_type (target_type);
8b2dbe47 17019
e142c38c 17020 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17021 if (attr_byte_size)
17022 byte_size = DW_UNSND (attr_byte_size);
c906108c 17023 else
8b2dbe47
KB
17024 byte_size = cu_header->addr_size;
17025
e142c38c 17026 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17027 if (attr_address_class)
17028 addr_class = DW_UNSND (attr_address_class);
17029 else
17030 addr_class = DW_ADDR_none;
17031
2b4424c3
TT
17032 ULONGEST alignment = get_alignment (cu, die);
17033
17034 /* If the pointer size, alignment, or address class is different
17035 than the default, create a type variant marked as such and set
17036 the length accordingly. */
17037 if (TYPE_LENGTH (type) != byte_size
17038 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17039 && alignment != TYPE_RAW_ALIGN (type))
17040 || addr_class != DW_ADDR_none)
c906108c 17041 {
5e2b427d 17042 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17043 {
17044 int type_flags;
17045
849957d9 17046 type_flags = gdbarch_address_class_type_flags
5e2b427d 17047 (gdbarch, byte_size, addr_class);
876cecd0
TT
17048 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17049 == 0);
8b2dbe47
KB
17050 type = make_type_with_address_space (type, type_flags);
17051 }
17052 else if (TYPE_LENGTH (type) != byte_size)
17053 {
b98664d3 17054 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17055 }
2b4424c3
TT
17056 else if (TYPE_RAW_ALIGN (type) != alignment)
17057 {
b98664d3 17058 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17059 " - DIE at %s [in module %s]"),
17060 sect_offset_str (die->sect_off),
17061 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17062 }
6e70227d 17063 else
9a619af0
MS
17064 {
17065 /* Should we also complain about unhandled address classes? */
17066 }
c906108c 17067 }
8b2dbe47
KB
17068
17069 TYPE_LENGTH (type) = byte_size;
2b4424c3 17070 set_type_align (type, alignment);
f792889a 17071 return set_die_type (die, type, cu);
c906108c
SS
17072}
17073
17074/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17075 the user defined type vector. */
17076
f792889a 17077static struct type *
e7c27a73 17078read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17079{
17080 struct type *type;
17081 struct type *to_type;
17082 struct type *domain;
17083
e7c27a73
DJ
17084 to_type = die_type (die, cu);
17085 domain = die_containing_type (die, cu);
0d5de010 17086
7e314c57
JK
17087 /* The calls above may have already set the type for this DIE. */
17088 type = get_die_type (die, cu);
17089 if (type)
17090 return type;
17091
0d5de010
DJ
17092 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17093 type = lookup_methodptr_type (to_type);
7078baeb
TT
17094 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17095 {
518817b3
SM
17096 struct type *new_type
17097 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17098
17099 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17100 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17101 TYPE_VARARGS (to_type));
17102 type = lookup_methodptr_type (new_type);
17103 }
0d5de010
DJ
17104 else
17105 type = lookup_memberptr_type (to_type, domain);
c906108c 17106
f792889a 17107 return set_die_type (die, type, cu);
c906108c
SS
17108}
17109
4297a3f0 17110/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17111 the user defined type vector. */
17112
f792889a 17113static struct type *
4297a3f0
AV
17114read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17115 enum type_code refcode)
c906108c 17116{
e7c27a73 17117 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17118 struct type *type, *target_type;
c906108c
SS
17119 struct attribute *attr;
17120
4297a3f0
AV
17121 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17122
7e314c57
JK
17123 target_type = die_type (die, cu);
17124
17125 /* The die_type call above may have already set the type for this DIE. */
17126 type = get_die_type (die, cu);
17127 if (type)
17128 return type;
17129
4297a3f0 17130 type = lookup_reference_type (target_type, refcode);
e142c38c 17131 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17132 if (attr)
17133 {
17134 TYPE_LENGTH (type) = DW_UNSND (attr);
17135 }
17136 else
17137 {
107d2387 17138 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17139 }
2b4424c3 17140 maybe_set_alignment (cu, die, type);
f792889a 17141 return set_die_type (die, type, cu);
c906108c
SS
17142}
17143
cf363f18
MW
17144/* Add the given cv-qualifiers to the element type of the array. GCC
17145 outputs DWARF type qualifiers that apply to an array, not the
17146 element type. But GDB relies on the array element type to carry
17147 the cv-qualifiers. This mimics section 6.7.3 of the C99
17148 specification. */
17149
17150static struct type *
17151add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17152 struct type *base_type, int cnst, int voltl)
17153{
17154 struct type *el_type, *inner_array;
17155
17156 base_type = copy_type (base_type);
17157 inner_array = base_type;
17158
17159 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17160 {
17161 TYPE_TARGET_TYPE (inner_array) =
17162 copy_type (TYPE_TARGET_TYPE (inner_array));
17163 inner_array = TYPE_TARGET_TYPE (inner_array);
17164 }
17165
17166 el_type = TYPE_TARGET_TYPE (inner_array);
17167 cnst |= TYPE_CONST (el_type);
17168 voltl |= TYPE_VOLATILE (el_type);
17169 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17170
17171 return set_die_type (die, base_type, cu);
17172}
17173
f792889a 17174static struct type *
e7c27a73 17175read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17176{
f792889a 17177 struct type *base_type, *cv_type;
c906108c 17178
e7c27a73 17179 base_type = die_type (die, cu);
7e314c57
JK
17180
17181 /* The die_type call above may have already set the type for this DIE. */
17182 cv_type = get_die_type (die, cu);
17183 if (cv_type)
17184 return cv_type;
17185
2f608a3a
KW
17186 /* In case the const qualifier is applied to an array type, the element type
17187 is so qualified, not the array type (section 6.7.3 of C99). */
17188 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17189 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17190
f792889a
DJ
17191 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17192 return set_die_type (die, cv_type, cu);
c906108c
SS
17193}
17194
f792889a 17195static struct type *
e7c27a73 17196read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17197{
f792889a 17198 struct type *base_type, *cv_type;
c906108c 17199
e7c27a73 17200 base_type = die_type (die, cu);
7e314c57
JK
17201
17202 /* The die_type call above may have already set the type for this DIE. */
17203 cv_type = get_die_type (die, cu);
17204 if (cv_type)
17205 return cv_type;
17206
cf363f18
MW
17207 /* In case the volatile qualifier is applied to an array type, the
17208 element type is so qualified, not the array type (section 6.7.3
17209 of C99). */
17210 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17211 return add_array_cv_type (die, cu, base_type, 0, 1);
17212
f792889a
DJ
17213 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17214 return set_die_type (die, cv_type, cu);
c906108c
SS
17215}
17216
06d66ee9
TT
17217/* Handle DW_TAG_restrict_type. */
17218
17219static struct type *
17220read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17221{
17222 struct type *base_type, *cv_type;
17223
17224 base_type = die_type (die, cu);
17225
17226 /* The die_type call above may have already set the type for this DIE. */
17227 cv_type = get_die_type (die, cu);
17228 if (cv_type)
17229 return cv_type;
17230
17231 cv_type = make_restrict_type (base_type);
17232 return set_die_type (die, cv_type, cu);
17233}
17234
a2c2acaf
MW
17235/* Handle DW_TAG_atomic_type. */
17236
17237static struct type *
17238read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17239{
17240 struct type *base_type, *cv_type;
17241
17242 base_type = die_type (die, cu);
17243
17244 /* The die_type call above may have already set the type for this DIE. */
17245 cv_type = get_die_type (die, cu);
17246 if (cv_type)
17247 return cv_type;
17248
17249 cv_type = make_atomic_type (base_type);
17250 return set_die_type (die, cv_type, cu);
17251}
17252
c906108c
SS
17253/* Extract all information from a DW_TAG_string_type DIE and add to
17254 the user defined type vector. It isn't really a user defined type,
17255 but it behaves like one, with other DIE's using an AT_user_def_type
17256 attribute to reference it. */
17257
f792889a 17258static struct type *
e7c27a73 17259read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17260{
518817b3 17261 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17262 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17263 struct type *type, *range_type, *index_type, *char_type;
17264 struct attribute *attr;
17265 unsigned int length;
17266
e142c38c 17267 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17268 if (attr)
17269 {
17270 length = DW_UNSND (attr);
17271 }
17272 else
17273 {
0963b4bd 17274 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17275 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17276 if (attr)
17277 {
17278 length = DW_UNSND (attr);
17279 }
17280 else
17281 {
17282 length = 1;
17283 }
c906108c 17284 }
6ccb9162 17285
46bf5051 17286 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17287 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17288 char_type = language_string_char_type (cu->language_defn, gdbarch);
17289 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17290
f792889a 17291 return set_die_type (die, type, cu);
c906108c
SS
17292}
17293
4d804846
JB
17294/* Assuming that DIE corresponds to a function, returns nonzero
17295 if the function is prototyped. */
17296
17297static int
17298prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17299{
17300 struct attribute *attr;
17301
17302 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17303 if (attr && (DW_UNSND (attr) != 0))
17304 return 1;
17305
17306 /* The DWARF standard implies that the DW_AT_prototyped attribute
17307 is only meaninful for C, but the concept also extends to other
17308 languages that allow unprototyped functions (Eg: Objective C).
17309 For all other languages, assume that functions are always
17310 prototyped. */
17311 if (cu->language != language_c
17312 && cu->language != language_objc
17313 && cu->language != language_opencl)
17314 return 1;
17315
17316 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17317 prototyped and unprototyped functions; default to prototyped,
17318 since that is more common in modern code (and RealView warns
17319 about unprototyped functions). */
17320 if (producer_is_realview (cu->producer))
17321 return 1;
17322
17323 return 0;
17324}
17325
c906108c
SS
17326/* Handle DIES due to C code like:
17327
17328 struct foo
c5aa993b
JM
17329 {
17330 int (*funcp)(int a, long l);
17331 int b;
17332 };
c906108c 17333
0963b4bd 17334 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17335
f792889a 17336static struct type *
e7c27a73 17337read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17338{
518817b3 17339 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17340 struct type *type; /* Type that this function returns. */
17341 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17342 struct attribute *attr;
17343
e7c27a73 17344 type = die_type (die, cu);
7e314c57
JK
17345
17346 /* The die_type call above may have already set the type for this DIE. */
17347 ftype = get_die_type (die, cu);
17348 if (ftype)
17349 return ftype;
17350
0c8b41f1 17351 ftype = lookup_function_type (type);
c906108c 17352
4d804846 17353 if (prototyped_function_p (die, cu))
a6c727b2 17354 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17355
c055b101
CV
17356 /* Store the calling convention in the type if it's available in
17357 the subroutine die. Otherwise set the calling convention to
17358 the default value DW_CC_normal. */
17359 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17360 if (attr)
17361 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17362 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17363 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17364 else
17365 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17366
743649fd
MW
17367 /* Record whether the function returns normally to its caller or not
17368 if the DWARF producer set that information. */
17369 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17370 if (attr && (DW_UNSND (attr) != 0))
17371 TYPE_NO_RETURN (ftype) = 1;
17372
76c10ea2
GM
17373 /* We need to add the subroutine type to the die immediately so
17374 we don't infinitely recurse when dealing with parameters
0963b4bd 17375 declared as the same subroutine type. */
76c10ea2 17376 set_die_type (die, ftype, cu);
6e70227d 17377
639d11d3 17378 if (die->child != NULL)
c906108c 17379 {
bb5ed363 17380 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17381 struct die_info *child_die;
8072405b 17382 int nparams, iparams;
c906108c
SS
17383
17384 /* Count the number of parameters.
17385 FIXME: GDB currently ignores vararg functions, but knows about
17386 vararg member functions. */
8072405b 17387 nparams = 0;
639d11d3 17388 child_die = die->child;
c906108c
SS
17389 while (child_die && child_die->tag)
17390 {
17391 if (child_die->tag == DW_TAG_formal_parameter)
17392 nparams++;
17393 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17394 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17395 child_die = sibling_die (child_die);
17396 }
17397
17398 /* Allocate storage for parameters and fill them in. */
17399 TYPE_NFIELDS (ftype) = nparams;
17400 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17401 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17402
8072405b
JK
17403 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17404 even if we error out during the parameters reading below. */
17405 for (iparams = 0; iparams < nparams; iparams++)
17406 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17407
17408 iparams = 0;
639d11d3 17409 child_die = die->child;
c906108c
SS
17410 while (child_die && child_die->tag)
17411 {
17412 if (child_die->tag == DW_TAG_formal_parameter)
17413 {
3ce3b1ba
PA
17414 struct type *arg_type;
17415
17416 /* DWARF version 2 has no clean way to discern C++
17417 static and non-static member functions. G++ helps
17418 GDB by marking the first parameter for non-static
17419 member functions (which is the this pointer) as
17420 artificial. We pass this information to
17421 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17422
17423 DWARF version 3 added DW_AT_object_pointer, which GCC
17424 4.5 does not yet generate. */
e142c38c 17425 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17426 if (attr)
17427 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17428 else
9c37b5ae 17429 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17430 arg_type = die_type (child_die, cu);
17431
17432 /* RealView does not mark THIS as const, which the testsuite
17433 expects. GCC marks THIS as const in method definitions,
17434 but not in the class specifications (GCC PR 43053). */
17435 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17436 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17437 {
17438 int is_this = 0;
17439 struct dwarf2_cu *arg_cu = cu;
17440 const char *name = dwarf2_name (child_die, cu);
17441
17442 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17443 if (attr)
17444 {
17445 /* If the compiler emits this, use it. */
17446 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17447 is_this = 1;
17448 }
17449 else if (name && strcmp (name, "this") == 0)
17450 /* Function definitions will have the argument names. */
17451 is_this = 1;
17452 else if (name == NULL && iparams == 0)
17453 /* Declarations may not have the names, so like
17454 elsewhere in GDB, assume an artificial first
17455 argument is "this". */
17456 is_this = 1;
17457
17458 if (is_this)
17459 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17460 arg_type, 0);
17461 }
17462
17463 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17464 iparams++;
17465 }
17466 child_die = sibling_die (child_die);
17467 }
17468 }
17469
76c10ea2 17470 return ftype;
c906108c
SS
17471}
17472
f792889a 17473static struct type *
e7c27a73 17474read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17475{
518817b3 17476 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17477 const char *name = NULL;
3c8e0968 17478 struct type *this_type, *target_type;
c906108c 17479
94af9270 17480 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17481 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17482 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17483 set_die_type (die, this_type, cu);
3c8e0968
DE
17484 target_type = die_type (die, cu);
17485 if (target_type != this_type)
17486 TYPE_TARGET_TYPE (this_type) = target_type;
17487 else
17488 {
17489 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17490 spec and cause infinite loops in GDB. */
b98664d3 17491 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17492 "- DIE at %s [in module %s]"),
17493 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17494 TYPE_TARGET_TYPE (this_type) = NULL;
17495 }
f792889a 17496 return this_type;
c906108c
SS
17497}
17498
9b790ce7
UW
17499/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17500 (which may be different from NAME) to the architecture back-end to allow
17501 it to guess the correct format if necessary. */
17502
17503static struct type *
17504dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17505 const char *name_hint)
17506{
17507 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17508 const struct floatformat **format;
17509 struct type *type;
17510
17511 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17512 if (format)
17513 type = init_float_type (objfile, bits, name, format);
17514 else
77b7c781 17515 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17516
17517 return type;
17518}
17519
eb77c9df
AB
17520/* Allocate an integer type of size BITS and name NAME. */
17521
17522static struct type *
17523dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17524 int bits, int unsigned_p, const char *name)
17525{
17526 struct type *type;
17527
17528 /* Versions of Intel's C Compiler generate an integer type called "void"
17529 instead of using DW_TAG_unspecified_type. This has been seen on
17530 at least versions 14, 17, and 18. */
35ee2dc2
AB
17531 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17532 && strcmp (name, "void") == 0)
eb77c9df
AB
17533 type = objfile_type (objfile)->builtin_void;
17534 else
17535 type = init_integer_type (objfile, bits, unsigned_p, name);
17536
17537 return type;
17538}
17539
8bdc1658
AB
17540/* Initialise and return a floating point type of size BITS suitable for
17541 use as a component of a complex number. The NAME_HINT is passed through
17542 when initialising the floating point type and is the name of the complex
17543 type.
17544
17545 As DWARF doesn't currently provide an explicit name for the components
17546 of a complex number, but it can be helpful to have these components
17547 named, we try to select a suitable name based on the size of the
17548 component. */
17549static struct type *
17550dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17551 struct objfile *objfile,
17552 int bits, const char *name_hint)
17553{
17554 gdbarch *gdbarch = get_objfile_arch (objfile);
17555 struct type *tt = nullptr;
17556
35add35e
AB
17557 /* Try to find a suitable floating point builtin type of size BITS.
17558 We're going to use the name of this type as the name for the complex
17559 target type that we are about to create. */
1db455a7 17560 switch (cu->language)
8bdc1658 17561 {
1db455a7
AB
17562 case language_fortran:
17563 switch (bits)
17564 {
17565 case 32:
17566 tt = builtin_f_type (gdbarch)->builtin_real;
17567 break;
17568 case 64:
17569 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17570 break;
17571 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17572 case 128:
17573 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17574 break;
17575 }
8bdc1658 17576 break;
1db455a7
AB
17577 default:
17578 switch (bits)
17579 {
17580 case 32:
17581 tt = builtin_type (gdbarch)->builtin_float;
17582 break;
17583 case 64:
17584 tt = builtin_type (gdbarch)->builtin_double;
17585 break;
17586 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17587 case 128:
17588 tt = builtin_type (gdbarch)->builtin_long_double;
17589 break;
17590 }
8bdc1658
AB
17591 break;
17592 }
17593
35add35e
AB
17594 /* If the type we found doesn't match the size we were looking for, then
17595 pretend we didn't find a type at all, the complex target type we
17596 create will then be nameless. */
a12e5744 17597 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17598 tt = nullptr;
17599
8bdc1658
AB
17600 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17601 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17602}
17603
c906108c
SS
17604/* Find a representation of a given base type and install
17605 it in the TYPE field of the die. */
17606
f792889a 17607static struct type *
e7c27a73 17608read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17609{
518817b3 17610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17611 struct type *type;
17612 struct attribute *attr;
19f392bc 17613 int encoding = 0, bits = 0;
15d034d0 17614 const char *name;
c906108c 17615
e142c38c 17616 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17617 if (attr)
17618 {
17619 encoding = DW_UNSND (attr);
17620 }
e142c38c 17621 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17622 if (attr)
17623 {
19f392bc 17624 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17625 }
39cbfefa 17626 name = dwarf2_name (die, cu);
6ccb9162 17627 if (!name)
c906108c 17628 {
b98664d3 17629 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17630 }
6ccb9162
UW
17631
17632 switch (encoding)
c906108c 17633 {
6ccb9162
UW
17634 case DW_ATE_address:
17635 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17636 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17637 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17638 break;
17639 case DW_ATE_boolean:
19f392bc 17640 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17641 break;
17642 case DW_ATE_complex_float:
8bdc1658 17643 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17644 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17645 break;
17646 case DW_ATE_decimal_float:
19f392bc 17647 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17648 break;
17649 case DW_ATE_float:
9b790ce7 17650 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17651 break;
17652 case DW_ATE_signed:
eb77c9df 17653 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17654 break;
17655 case DW_ATE_unsigned:
3b2b8fea
TT
17656 if (cu->language == language_fortran
17657 && name
61012eef 17658 && startswith (name, "character("))
19f392bc
UW
17659 type = init_character_type (objfile, bits, 1, name);
17660 else
eb77c9df 17661 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17662 break;
17663 case DW_ATE_signed_char:
6e70227d 17664 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17665 || cu->language == language_pascal
17666 || cu->language == language_fortran)
19f392bc
UW
17667 type = init_character_type (objfile, bits, 0, name);
17668 else
eb77c9df 17669 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17670 break;
17671 case DW_ATE_unsigned_char:
868a0084 17672 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17673 || cu->language == language_pascal
c44af4eb
TT
17674 || cu->language == language_fortran
17675 || cu->language == language_rust)
19f392bc
UW
17676 type = init_character_type (objfile, bits, 1, name);
17677 else
eb77c9df 17678 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17679 break;
75079b2b 17680 case DW_ATE_UTF:
53e710ac
PA
17681 {
17682 gdbarch *arch = get_objfile_arch (objfile);
17683
17684 if (bits == 16)
17685 type = builtin_type (arch)->builtin_char16;
17686 else if (bits == 32)
17687 type = builtin_type (arch)->builtin_char32;
17688 else
17689 {
b98664d3 17690 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17691 bits);
eb77c9df 17692 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17693 }
17694 return set_die_type (die, type, cu);
17695 }
75079b2b
TT
17696 break;
17697
6ccb9162 17698 default:
b98664d3 17699 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17700 dwarf_type_encoding_name (encoding));
77b7c781 17701 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17702 break;
c906108c 17703 }
6ccb9162 17704
0114d602 17705 if (name && strcmp (name, "char") == 0)
876cecd0 17706 TYPE_NOSIGN (type) = 1;
0114d602 17707
2b4424c3
TT
17708 maybe_set_alignment (cu, die, type);
17709
f792889a 17710 return set_die_type (die, type, cu);
c906108c
SS
17711}
17712
80180f79
SA
17713/* Parse dwarf attribute if it's a block, reference or constant and put the
17714 resulting value of the attribute into struct bound_prop.
17715 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17716
17717static int
17718attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17719 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17720 struct type *default_type)
80180f79
SA
17721{
17722 struct dwarf2_property_baton *baton;
518817b3
SM
17723 struct obstack *obstack
17724 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17725
9a49df9d
AB
17726 gdb_assert (default_type != NULL);
17727
80180f79
SA
17728 if (attr == NULL || prop == NULL)
17729 return 0;
17730
17731 if (attr_form_is_block (attr))
17732 {
8d749320 17733 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17734 baton->property_type = default_type;
80180f79
SA
17735 baton->locexpr.per_cu = cu->per_cu;
17736 baton->locexpr.size = DW_BLOCK (attr)->size;
17737 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17738 baton->locexpr.is_reference = false;
80180f79
SA
17739 prop->data.baton = baton;
17740 prop->kind = PROP_LOCEXPR;
17741 gdb_assert (prop->data.baton != NULL);
17742 }
17743 else if (attr_form_is_ref (attr))
17744 {
17745 struct dwarf2_cu *target_cu = cu;
17746 struct die_info *target_die;
17747 struct attribute *target_attr;
17748
17749 target_die = follow_die_ref (die, attr, &target_cu);
17750 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17751 if (target_attr == NULL)
17752 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17753 target_cu);
80180f79
SA
17754 if (target_attr == NULL)
17755 return 0;
17756
df25ebbd 17757 switch (target_attr->name)
80180f79 17758 {
df25ebbd
JB
17759 case DW_AT_location:
17760 if (attr_form_is_section_offset (target_attr))
17761 {
8d749320 17762 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17763 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17764 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17765 prop->data.baton = baton;
17766 prop->kind = PROP_LOCLIST;
17767 gdb_assert (prop->data.baton != NULL);
17768 }
17769 else if (attr_form_is_block (target_attr))
17770 {
8d749320 17771 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17772 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17773 baton->locexpr.per_cu = cu->per_cu;
17774 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17775 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17776 baton->locexpr.is_reference = true;
df25ebbd
JB
17777 prop->data.baton = baton;
17778 prop->kind = PROP_LOCEXPR;
17779 gdb_assert (prop->data.baton != NULL);
17780 }
17781 else
17782 {
17783 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17784 "dynamic property");
17785 return 0;
17786 }
17787 break;
17788 case DW_AT_data_member_location:
17789 {
17790 LONGEST offset;
17791
17792 if (!handle_data_member_location (target_die, target_cu,
17793 &offset))
17794 return 0;
17795
8d749320 17796 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17797 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17798 target_cu);
df25ebbd
JB
17799 baton->offset_info.offset = offset;
17800 baton->offset_info.type = die_type (target_die, target_cu);
17801 prop->data.baton = baton;
17802 prop->kind = PROP_ADDR_OFFSET;
17803 break;
17804 }
80180f79
SA
17805 }
17806 }
17807 else if (attr_form_is_constant (attr))
17808 {
17809 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17810 prop->kind = PROP_CONST;
17811 }
17812 else
17813 {
17814 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17815 dwarf2_name (die, cu));
17816 return 0;
17817 }
17818
17819 return 1;
17820}
17821
9a49df9d
AB
17822/* Find an integer type the same size as the address size given in the
17823 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17824 is unsigned or not. */
17825
17826static struct type *
17827dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17828 bool unsigned_p)
17829{
17830 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17831 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17832 struct type *int_type;
17833
17834 /* Helper macro to examine the various builtin types. */
17835#define TRY_TYPE(F) \
17836 int_type = (unsigned_p \
17837 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17838 : objfile_type (objfile)->builtin_ ## F); \
17839 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17840 return int_type
17841
17842 TRY_TYPE (char);
17843 TRY_TYPE (short);
17844 TRY_TYPE (int);
17845 TRY_TYPE (long);
17846 TRY_TYPE (long_long);
17847
17848#undef TRY_TYPE
17849
17850 gdb_assert_not_reached ("unable to find suitable integer type");
17851}
17852
b86352cf
AB
17853/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17854 present (which is valid) then compute the default type based on the
17855 compilation units address size. */
17856
17857static struct type *
17858read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17859{
17860 struct type *index_type = die_type (die, cu);
17861
17862 /* Dwarf-2 specifications explicitly allows to create subrange types
17863 without specifying a base type.
17864 In that case, the base type must be set to the type of
17865 the lower bound, upper bound or count, in that order, if any of these
17866 three attributes references an object that has a type.
17867 If no base type is found, the Dwarf-2 specifications say that
17868 a signed integer type of size equal to the size of an address should
17869 be used.
17870 For the following C code: `extern char gdb_int [];'
17871 GCC produces an empty range DIE.
17872 FIXME: muller/2010-05-28: Possible references to object for low bound,
17873 high bound or count are not yet handled by this code. */
17874 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17875 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17876
17877 return index_type;
17878}
17879
a02abb62
JB
17880/* Read the given DW_AT_subrange DIE. */
17881
f792889a 17882static struct type *
a02abb62
JB
17883read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17884{
4c9ad8c2 17885 struct type *base_type, *orig_base_type;
a02abb62
JB
17886 struct type *range_type;
17887 struct attribute *attr;
729efb13 17888 struct dynamic_prop low, high;
4fae6e18 17889 int low_default_is_valid;
c451ebe5 17890 int high_bound_is_count = 0;
15d034d0 17891 const char *name;
d359392f 17892 ULONGEST negative_mask;
e77813c8 17893
b86352cf
AB
17894 orig_base_type = read_subrange_index_type (die, cu);
17895
4c9ad8c2
TT
17896 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17897 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17898 creating the range type, but we use the result of check_typedef
17899 when examining properties of the type. */
17900 base_type = check_typedef (orig_base_type);
a02abb62 17901
7e314c57
JK
17902 /* The die_type call above may have already set the type for this DIE. */
17903 range_type = get_die_type (die, cu);
17904 if (range_type)
17905 return range_type;
17906
729efb13
SA
17907 low.kind = PROP_CONST;
17908 high.kind = PROP_CONST;
17909 high.data.const_val = 0;
17910
4fae6e18
JK
17911 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17912 omitting DW_AT_lower_bound. */
17913 switch (cu->language)
6e70227d 17914 {
4fae6e18
JK
17915 case language_c:
17916 case language_cplus:
729efb13 17917 low.data.const_val = 0;
4fae6e18
JK
17918 low_default_is_valid = 1;
17919 break;
17920 case language_fortran:
729efb13 17921 low.data.const_val = 1;
4fae6e18
JK
17922 low_default_is_valid = 1;
17923 break;
17924 case language_d:
4fae6e18 17925 case language_objc:
c44af4eb 17926 case language_rust:
729efb13 17927 low.data.const_val = 0;
4fae6e18
JK
17928 low_default_is_valid = (cu->header.version >= 4);
17929 break;
17930 case language_ada:
17931 case language_m2:
17932 case language_pascal:
729efb13 17933 low.data.const_val = 1;
4fae6e18
JK
17934 low_default_is_valid = (cu->header.version >= 4);
17935 break;
17936 default:
729efb13 17937 low.data.const_val = 0;
4fae6e18
JK
17938 low_default_is_valid = 0;
17939 break;
a02abb62
JB
17940 }
17941
e142c38c 17942 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17943 if (attr)
9a49df9d 17944 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17945 else if (!low_default_is_valid)
b98664d3 17946 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17947 "- DIE at %s [in module %s]"),
17948 sect_offset_str (die->sect_off),
518817b3 17949 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17950
506f5c41
TV
17951 struct attribute *attr_ub, *attr_count;
17952 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17953 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17954 {
506f5c41 17955 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17956 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17957 {
c451ebe5
SA
17958 /* If bounds are constant do the final calculation here. */
17959 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17960 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17961 else
17962 high_bound_is_count = 1;
c2ff108b 17963 }
506f5c41
TV
17964 else
17965 {
17966 if (attr_ub != NULL)
17967 complaint (_("Unresolved DW_AT_upper_bound "
17968 "- DIE at %s [in module %s]"),
17969 sect_offset_str (die->sect_off),
17970 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17971 if (attr_count != NULL)
17972 complaint (_("Unresolved DW_AT_count "
17973 "- DIE at %s [in module %s]"),
17974 sect_offset_str (die->sect_off),
17975 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17976 }
e77813c8 17977 }
a02abb62 17978
4e962e74
TT
17979 LONGEST bias = 0;
17980 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17981 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17982 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17983
dbb9c2b1
JB
17984 /* Normally, the DWARF producers are expected to use a signed
17985 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17986 But this is unfortunately not always the case, as witnessed
17987 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17988 is used instead. To work around that ambiguity, we treat
17989 the bounds as signed, and thus sign-extend their values, when
17990 the base type is signed. */
6e70227d 17991 negative_mask =
d359392f 17992 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17993 if (low.kind == PROP_CONST
17994 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17995 low.data.const_val |= negative_mask;
17996 if (high.kind == PROP_CONST
17997 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17998 high.data.const_val |= negative_mask;
43bbcdc2 17999
4e962e74 18000 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18001
c451ebe5
SA
18002 if (high_bound_is_count)
18003 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18004
c2ff108b
JK
18005 /* Ada expects an empty array on no boundary attributes. */
18006 if (attr == NULL && cu->language != language_ada)
729efb13 18007 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18008
39cbfefa
DJ
18009 name = dwarf2_name (die, cu);
18010 if (name)
18011 TYPE_NAME (range_type) = name;
6e70227d 18012
e142c38c 18013 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18014 if (attr)
18015 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18016
2b4424c3
TT
18017 maybe_set_alignment (cu, die, range_type);
18018
7e314c57
JK
18019 set_die_type (die, range_type, cu);
18020
18021 /* set_die_type should be already done. */
b4ba55a1
JB
18022 set_descriptive_type (range_type, die, cu);
18023
7e314c57 18024 return range_type;
a02abb62 18025}
6e70227d 18026
f792889a 18027static struct type *
81a17f79
JB
18028read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18029{
18030 struct type *type;
81a17f79 18031
518817b3
SM
18032 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18033 NULL);
0114d602 18034 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18035
74a2f8ff
JB
18036 /* In Ada, an unspecified type is typically used when the description
18037 of the type is defered to a different unit. When encountering
18038 such a type, we treat it as a stub, and try to resolve it later on,
18039 when needed. */
18040 if (cu->language == language_ada)
18041 TYPE_STUB (type) = 1;
18042
f792889a 18043 return set_die_type (die, type, cu);
81a17f79 18044}
a02abb62 18045
639d11d3
DC
18046/* Read a single die and all its descendents. Set the die's sibling
18047 field to NULL; set other fields in the die correctly, and set all
18048 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18049 location of the info_ptr after reading all of those dies. PARENT
18050 is the parent of the die in question. */
18051
18052static struct die_info *
dee91e82 18053read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18054 const gdb_byte *info_ptr,
18055 const gdb_byte **new_info_ptr,
dee91e82 18056 struct die_info *parent)
639d11d3
DC
18057{
18058 struct die_info *die;
d521ce57 18059 const gdb_byte *cur_ptr;
639d11d3
DC
18060 int has_children;
18061
bf6af496 18062 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18063 if (die == NULL)
18064 {
18065 *new_info_ptr = cur_ptr;
18066 return NULL;
18067 }
93311388 18068 store_in_ref_table (die, reader->cu);
639d11d3
DC
18069
18070 if (has_children)
bf6af496 18071 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18072 else
18073 {
18074 die->child = NULL;
18075 *new_info_ptr = cur_ptr;
18076 }
18077
18078 die->sibling = NULL;
18079 die->parent = parent;
18080 return die;
18081}
18082
18083/* Read a die, all of its descendents, and all of its siblings; set
18084 all of the fields of all of the dies correctly. Arguments are as
18085 in read_die_and_children. */
18086
18087static struct die_info *
bf6af496 18088read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18089 const gdb_byte *info_ptr,
18090 const gdb_byte **new_info_ptr,
bf6af496 18091 struct die_info *parent)
639d11d3
DC
18092{
18093 struct die_info *first_die, *last_sibling;
d521ce57 18094 const gdb_byte *cur_ptr;
639d11d3 18095
c906108c 18096 cur_ptr = info_ptr;
639d11d3
DC
18097 first_die = last_sibling = NULL;
18098
18099 while (1)
c906108c 18100 {
639d11d3 18101 struct die_info *die
dee91e82 18102 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18103
1d325ec1 18104 if (die == NULL)
c906108c 18105 {
639d11d3
DC
18106 *new_info_ptr = cur_ptr;
18107 return first_die;
c906108c 18108 }
1d325ec1
DJ
18109
18110 if (!first_die)
18111 first_die = die;
c906108c 18112 else
1d325ec1
DJ
18113 last_sibling->sibling = die;
18114
18115 last_sibling = die;
c906108c 18116 }
c906108c
SS
18117}
18118
bf6af496
DE
18119/* Read a die, all of its descendents, and all of its siblings; set
18120 all of the fields of all of the dies correctly. Arguments are as
18121 in read_die_and_children.
18122 This the main entry point for reading a DIE and all its children. */
18123
18124static struct die_info *
18125read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18126 const gdb_byte *info_ptr,
18127 const gdb_byte **new_info_ptr,
bf6af496
DE
18128 struct die_info *parent)
18129{
18130 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18131 new_info_ptr, parent);
18132
b4f54984 18133 if (dwarf_die_debug)
bf6af496
DE
18134 {
18135 fprintf_unfiltered (gdb_stdlog,
18136 "Read die from %s@0x%x of %s:\n",
a32a8923 18137 get_section_name (reader->die_section),
bf6af496
DE
18138 (unsigned) (info_ptr - reader->die_section->buffer),
18139 bfd_get_filename (reader->abfd));
b4f54984 18140 dump_die (die, dwarf_die_debug);
bf6af496
DE
18141 }
18142
18143 return die;
18144}
18145
3019eac3
DE
18146/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18147 attributes.
18148 The caller is responsible for filling in the extra attributes
18149 and updating (*DIEP)->num_attrs.
18150 Set DIEP to point to a newly allocated die with its information,
18151 except for its child, sibling, and parent fields.
18152 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18153
d521ce57 18154static const gdb_byte *
3019eac3 18155read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18156 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18157 int *has_children, int num_extra_attrs)
93311388 18158{
b64f50a1 18159 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18160 struct abbrev_info *abbrev;
18161 struct die_info *die;
18162 struct dwarf2_cu *cu = reader->cu;
18163 bfd *abfd = reader->abfd;
18164
9c541725 18165 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18166 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18167 info_ptr += bytes_read;
18168 if (!abbrev_number)
18169 {
18170 *diep = NULL;
18171 *has_children = 0;
18172 return info_ptr;
18173 }
18174
685af9cd 18175 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18176 if (!abbrev)
348e048f
DE
18177 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18178 abbrev_number,
18179 bfd_get_filename (abfd));
18180
3019eac3 18181 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18182 die->sect_off = sect_off;
93311388
DE
18183 die->tag = abbrev->tag;
18184 die->abbrev = abbrev_number;
18185
3019eac3
DE
18186 /* Make the result usable.
18187 The caller needs to update num_attrs after adding the extra
18188 attributes. */
93311388
DE
18189 die->num_attrs = abbrev->num_attrs;
18190
18191 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18192 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18193 info_ptr);
93311388
DE
18194
18195 *diep = die;
18196 *has_children = abbrev->has_children;
18197 return info_ptr;
18198}
18199
3019eac3
DE
18200/* Read a die and all its attributes.
18201 Set DIEP to point to a newly allocated die with its information,
18202 except for its child, sibling, and parent fields.
18203 Set HAS_CHILDREN to tell whether the die has children or not. */
18204
d521ce57 18205static const gdb_byte *
3019eac3 18206read_full_die (const struct die_reader_specs *reader,
d521ce57 18207 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18208 int *has_children)
18209{
d521ce57 18210 const gdb_byte *result;
bf6af496
DE
18211
18212 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18213
b4f54984 18214 if (dwarf_die_debug)
bf6af496
DE
18215 {
18216 fprintf_unfiltered (gdb_stdlog,
18217 "Read die from %s@0x%x of %s:\n",
a32a8923 18218 get_section_name (reader->die_section),
bf6af496
DE
18219 (unsigned) (info_ptr - reader->die_section->buffer),
18220 bfd_get_filename (reader->abfd));
b4f54984 18221 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18222 }
18223
18224 return result;
3019eac3 18225}
433df2d4
DE
18226\f
18227/* Abbreviation tables.
3019eac3 18228
433df2d4 18229 In DWARF version 2, the description of the debugging information is
c906108c
SS
18230 stored in a separate .debug_abbrev section. Before we read any
18231 dies from a section we read in all abbreviations and install them
433df2d4
DE
18232 in a hash table. */
18233
18234/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18235
685af9cd
TT
18236struct abbrev_info *
18237abbrev_table::alloc_abbrev ()
433df2d4
DE
18238{
18239 struct abbrev_info *abbrev;
18240
685af9cd 18241 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18242 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18243
433df2d4
DE
18244 return abbrev;
18245}
18246
18247/* Add an abbreviation to the table. */
c906108c 18248
685af9cd
TT
18249void
18250abbrev_table::add_abbrev (unsigned int abbrev_number,
18251 struct abbrev_info *abbrev)
433df2d4
DE
18252{
18253 unsigned int hash_number;
18254
18255 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18256 abbrev->next = m_abbrevs[hash_number];
18257 m_abbrevs[hash_number] = abbrev;
433df2d4 18258}
dee91e82 18259
433df2d4
DE
18260/* Look up an abbrev in the table.
18261 Returns NULL if the abbrev is not found. */
18262
685af9cd
TT
18263struct abbrev_info *
18264abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18265{
433df2d4
DE
18266 unsigned int hash_number;
18267 struct abbrev_info *abbrev;
18268
18269 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18270 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18271
18272 while (abbrev)
18273 {
18274 if (abbrev->number == abbrev_number)
18275 return abbrev;
18276 abbrev = abbrev->next;
18277 }
18278 return NULL;
18279}
18280
18281/* Read in an abbrev table. */
18282
685af9cd 18283static abbrev_table_up
ed2dc618
SM
18284abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18285 struct dwarf2_section_info *section,
9c541725 18286 sect_offset sect_off)
433df2d4
DE
18287{
18288 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18289 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18290 const gdb_byte *abbrev_ptr;
c906108c
SS
18291 struct abbrev_info *cur_abbrev;
18292 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18293 unsigned int abbrev_form;
f3dd6933
DJ
18294 struct attr_abbrev *cur_attrs;
18295 unsigned int allocated_attrs;
c906108c 18296
685af9cd 18297 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18298
433df2d4 18299 dwarf2_read_section (objfile, section);
9c541725 18300 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18301 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18302 abbrev_ptr += bytes_read;
18303
f3dd6933 18304 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18305 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18306
0963b4bd 18307 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18308 while (abbrev_number)
18309 {
685af9cd 18310 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18311
18312 /* read in abbrev header */
18313 cur_abbrev->number = abbrev_number;
aead7601
SM
18314 cur_abbrev->tag
18315 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18316 abbrev_ptr += bytes_read;
18317 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18318 abbrev_ptr += 1;
18319
18320 /* now read in declarations */
22d2f3ab 18321 for (;;)
c906108c 18322 {
43988095
JK
18323 LONGEST implicit_const;
18324
22d2f3ab
JK
18325 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18326 abbrev_ptr += bytes_read;
18327 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18328 abbrev_ptr += bytes_read;
43988095
JK
18329 if (abbrev_form == DW_FORM_implicit_const)
18330 {
18331 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18332 &bytes_read);
18333 abbrev_ptr += bytes_read;
18334 }
18335 else
18336 {
18337 /* Initialize it due to a false compiler warning. */
18338 implicit_const = -1;
18339 }
22d2f3ab
JK
18340
18341 if (abbrev_name == 0)
18342 break;
18343
f3dd6933 18344 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18345 {
f3dd6933
DJ
18346 allocated_attrs += ATTR_ALLOC_CHUNK;
18347 cur_attrs
224c3ddb 18348 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18349 }
ae038cb0 18350
aead7601
SM
18351 cur_attrs[cur_abbrev->num_attrs].name
18352 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18353 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18354 = (enum dwarf_form) abbrev_form;
43988095 18355 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18356 ++cur_abbrev->num_attrs;
c906108c
SS
18357 }
18358
8d749320
SM
18359 cur_abbrev->attrs =
18360 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18361 cur_abbrev->num_attrs);
f3dd6933
DJ
18362 memcpy (cur_abbrev->attrs, cur_attrs,
18363 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18364
685af9cd 18365 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18366
18367 /* Get next abbreviation.
18368 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18369 always properly terminated with an abbrev number of 0.
18370 Exit loop if we encounter an abbreviation which we have
18371 already read (which means we are about to read the abbreviations
18372 for the next compile unit) or if the end of the abbreviation
18373 table is reached. */
433df2d4 18374 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18375 break;
18376 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18377 abbrev_ptr += bytes_read;
685af9cd 18378 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18379 break;
18380 }
f3dd6933
DJ
18381
18382 xfree (cur_attrs);
433df2d4 18383 return abbrev_table;
c906108c
SS
18384}
18385
72bf9492
DJ
18386/* Returns nonzero if TAG represents a type that we might generate a partial
18387 symbol for. */
18388
18389static int
18390is_type_tag_for_partial (int tag)
18391{
18392 switch (tag)
18393 {
18394#if 0
18395 /* Some types that would be reasonable to generate partial symbols for,
18396 that we don't at present. */
18397 case DW_TAG_array_type:
18398 case DW_TAG_file_type:
18399 case DW_TAG_ptr_to_member_type:
18400 case DW_TAG_set_type:
18401 case DW_TAG_string_type:
18402 case DW_TAG_subroutine_type:
18403#endif
18404 case DW_TAG_base_type:
18405 case DW_TAG_class_type:
680b30c7 18406 case DW_TAG_interface_type:
72bf9492
DJ
18407 case DW_TAG_enumeration_type:
18408 case DW_TAG_structure_type:
18409 case DW_TAG_subrange_type:
18410 case DW_TAG_typedef:
18411 case DW_TAG_union_type:
18412 return 1;
18413 default:
18414 return 0;
18415 }
18416}
18417
18418/* Load all DIEs that are interesting for partial symbols into memory. */
18419
18420static struct partial_die_info *
dee91e82 18421load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18422 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18423{
dee91e82 18424 struct dwarf2_cu *cu = reader->cu;
518817b3 18425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18426 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18427 unsigned int bytes_read;
5afb4e99 18428 unsigned int load_all = 0;
72bf9492
DJ
18429 int nesting_level = 1;
18430
18431 parent_die = NULL;
18432 last_die = NULL;
18433
7adf1e79
DE
18434 gdb_assert (cu->per_cu != NULL);
18435 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18436 load_all = 1;
18437
72bf9492
DJ
18438 cu->partial_dies
18439 = htab_create_alloc_ex (cu->header.length / 12,
18440 partial_die_hash,
18441 partial_die_eq,
18442 NULL,
18443 &cu->comp_unit_obstack,
18444 hashtab_obstack_allocate,
18445 dummy_obstack_deallocate);
18446
72bf9492
DJ
18447 while (1)
18448 {
685af9cd 18449 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18450
18451 /* A NULL abbrev means the end of a series of children. */
18452 if (abbrev == NULL)
18453 {
18454 if (--nesting_level == 0)
cd9983dd
YQ
18455 return first_die;
18456
72bf9492
DJ
18457 info_ptr += bytes_read;
18458 last_die = parent_die;
18459 parent_die = parent_die->die_parent;
18460 continue;
18461 }
18462
98bfdba5
PA
18463 /* Check for template arguments. We never save these; if
18464 they're seen, we just mark the parent, and go on our way. */
18465 if (parent_die != NULL
18466 && cu->language == language_cplus
18467 && (abbrev->tag == DW_TAG_template_type_param
18468 || abbrev->tag == DW_TAG_template_value_param))
18469 {
18470 parent_die->has_template_arguments = 1;
18471
18472 if (!load_all)
18473 {
18474 /* We don't need a partial DIE for the template argument. */
dee91e82 18475 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18476 continue;
18477 }
18478 }
18479
0d99eb77 18480 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18481 Skip their other children. */
18482 if (!load_all
18483 && cu->language == language_cplus
18484 && parent_die != NULL
18485 && parent_die->tag == DW_TAG_subprogram)
18486 {
dee91e82 18487 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18488 continue;
18489 }
18490
5afb4e99
DJ
18491 /* Check whether this DIE is interesting enough to save. Normally
18492 we would not be interested in members here, but there may be
18493 later variables referencing them via DW_AT_specification (for
18494 static members). */
18495 if (!load_all
18496 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18497 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18498 && abbrev->tag != DW_TAG_enumerator
18499 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18500 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18501 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18502 && abbrev->tag != DW_TAG_variable
5afb4e99 18503 && abbrev->tag != DW_TAG_namespace
f55ee35c 18504 && abbrev->tag != DW_TAG_module
95554aad 18505 && abbrev->tag != DW_TAG_member
74921315
KS
18506 && abbrev->tag != DW_TAG_imported_unit
18507 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18508 {
18509 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18510 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18511 continue;
18512 }
18513
6f06d47b
YQ
18514 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18515 abbrev);
cd9983dd 18516
48fbe735 18517 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18518
18519 /* This two-pass algorithm for processing partial symbols has a
18520 high cost in cache pressure. Thus, handle some simple cases
18521 here which cover the majority of C partial symbols. DIEs
18522 which neither have specification tags in them, nor could have
18523 specification tags elsewhere pointing at them, can simply be
18524 processed and discarded.
18525
18526 This segment is also optional; scan_partial_symbols and
18527 add_partial_symbol will handle these DIEs if we chain
18528 them in normally. When compilers which do not emit large
18529 quantities of duplicate debug information are more common,
18530 this code can probably be removed. */
18531
18532 /* Any complete simple types at the top level (pretty much all
18533 of them, for a language without namespaces), can be processed
18534 directly. */
18535 if (parent_die == NULL
cd9983dd
YQ
18536 && pdi.has_specification == 0
18537 && pdi.is_declaration == 0
18538 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18539 || pdi.tag == DW_TAG_base_type
18540 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18541 {
cd9983dd 18542 if (building_psymtab && pdi.name != NULL)
67547d89 18543 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18544 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18545 psymbol_placement::STATIC,
1762568f 18546 0, cu->language, objfile);
cd9983dd 18547 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18548 continue;
18549 }
18550
d8228535
JK
18551 /* The exception for DW_TAG_typedef with has_children above is
18552 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18553 type_name_or_error will error on such types later.
d8228535
JK
18554
18555 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18556 it could not find the child DIEs referenced later, this is checked
18557 above. In correct DWARF DW_TAG_typedef should have no children. */
18558
cd9983dd 18559 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18560 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18561 "- DIE at %s [in module %s]"),
cd9983dd 18562 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18563
72bf9492
DJ
18564 /* If we're at the second level, and we're an enumerator, and
18565 our parent has no specification (meaning possibly lives in a
18566 namespace elsewhere), then we can add the partial symbol now
18567 instead of queueing it. */
cd9983dd 18568 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18569 && parent_die != NULL
18570 && parent_die->die_parent == NULL
18571 && parent_die->tag == DW_TAG_enumeration_type
18572 && parent_die->has_specification == 0)
18573 {
cd9983dd 18574 if (pdi.name == NULL)
b98664d3 18575 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18576 else if (building_psymtab)
67547d89 18577 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18578 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18579 cu->language == language_cplus
75aedd27
TT
18580 ? psymbol_placement::GLOBAL
18581 : psymbol_placement::STATIC,
1762568f 18582 0, cu->language, objfile);
72bf9492 18583
cd9983dd 18584 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18585 continue;
18586 }
18587
cd9983dd 18588 struct partial_die_info *part_die
6f06d47b 18589 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18590
72bf9492
DJ
18591 /* We'll save this DIE so link it in. */
18592 part_die->die_parent = parent_die;
18593 part_die->die_sibling = NULL;
18594 part_die->die_child = NULL;
18595
18596 if (last_die && last_die == parent_die)
18597 last_die->die_child = part_die;
18598 else if (last_die)
18599 last_die->die_sibling = part_die;
18600
18601 last_die = part_die;
18602
18603 if (first_die == NULL)
18604 first_die = part_die;
18605
18606 /* Maybe add the DIE to the hash table. Not all DIEs that we
18607 find interesting need to be in the hash table, because we
18608 also have the parent/sibling/child chains; only those that we
18609 might refer to by offset later during partial symbol reading.
18610
18611 For now this means things that might have be the target of a
18612 DW_AT_specification, DW_AT_abstract_origin, or
18613 DW_AT_extension. DW_AT_extension will refer only to
18614 namespaces; DW_AT_abstract_origin refers to functions (and
18615 many things under the function DIE, but we do not recurse
18616 into function DIEs during partial symbol reading) and
18617 possibly variables as well; DW_AT_specification refers to
18618 declarations. Declarations ought to have the DW_AT_declaration
18619 flag. It happens that GCC forgets to put it in sometimes, but
18620 only for functions, not for types.
18621
18622 Adding more things than necessary to the hash table is harmless
18623 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18624 wasted time in find_partial_die, when we reread the compilation
18625 unit with load_all_dies set. */
72bf9492 18626
5afb4e99 18627 if (load_all
72929c62 18628 || abbrev->tag == DW_TAG_constant
5afb4e99 18629 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18630 || abbrev->tag == DW_TAG_variable
18631 || abbrev->tag == DW_TAG_namespace
18632 || part_die->is_declaration)
18633 {
18634 void **slot;
18635
18636 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18637 to_underlying (part_die->sect_off),
18638 INSERT);
72bf9492
DJ
18639 *slot = part_die;
18640 }
18641
72bf9492 18642 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18643 we have no reason to follow the children of structures; for other
98bfdba5
PA
18644 languages we have to, so that we can get at method physnames
18645 to infer fully qualified class names, for DW_AT_specification,
18646 and for C++ template arguments. For C++, we also look one level
18647 inside functions to find template arguments (if the name of the
18648 function does not already contain the template arguments).
bc30ff58
JB
18649
18650 For Ada, we need to scan the children of subprograms and lexical
18651 blocks as well because Ada allows the definition of nested
18652 entities that could be interesting for the debugger, such as
18653 nested subprograms for instance. */
72bf9492 18654 if (last_die->has_children
5afb4e99
DJ
18655 && (load_all
18656 || last_die->tag == DW_TAG_namespace
f55ee35c 18657 || last_die->tag == DW_TAG_module
72bf9492 18658 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18659 || (cu->language == language_cplus
18660 && last_die->tag == DW_TAG_subprogram
18661 && (last_die->name == NULL
18662 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18663 || (cu->language != language_c
18664 && (last_die->tag == DW_TAG_class_type
680b30c7 18665 || last_die->tag == DW_TAG_interface_type
72bf9492 18666 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18667 || last_die->tag == DW_TAG_union_type))
18668 || (cu->language == language_ada
18669 && (last_die->tag == DW_TAG_subprogram
18670 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18671 {
18672 nesting_level++;
18673 parent_die = last_die;
18674 continue;
18675 }
18676
18677 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18678 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18679
18680 /* Back to the top, do it again. */
18681 }
18682}
18683
6f06d47b
YQ
18684partial_die_info::partial_die_info (sect_offset sect_off_,
18685 struct abbrev_info *abbrev)
18686 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18687{
18688}
18689
35cc7ed7
YQ
18690/* Read a minimal amount of information into the minimal die structure.
18691 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18692
48fbe735
YQ
18693const gdb_byte *
18694partial_die_info::read (const struct die_reader_specs *reader,
18695 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18696{
dee91e82 18697 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18698 struct dwarf2_per_objfile *dwarf2_per_objfile
18699 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18700 unsigned int i;
c5aa993b 18701 int has_low_pc_attr = 0;
c906108c 18702 int has_high_pc_attr = 0;
91da1414 18703 int high_pc_relative = 0;
c906108c 18704
fd0a254f 18705 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18706 {
48fbe735
YQ
18707 struct attribute attr;
18708
fd0a254f 18709 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18710
18711 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18712 partial symbol table. */
c906108c
SS
18713 switch (attr.name)
18714 {
18715 case DW_AT_name:
48fbe735 18716 switch (tag)
71c25dea
TT
18717 {
18718 case DW_TAG_compile_unit:
95554aad 18719 case DW_TAG_partial_unit:
348e048f 18720 case DW_TAG_type_unit:
71c25dea
TT
18721 /* Compilation units have a DW_AT_name that is a filename, not
18722 a source language identifier. */
18723 case DW_TAG_enumeration_type:
18724 case DW_TAG_enumerator:
18725 /* These tags always have simple identifiers already; no need
18726 to canonicalize them. */
48fbe735 18727 name = DW_STRING (&attr);
71c25dea
TT
18728 break;
18729 default:
48fbe735
YQ
18730 {
18731 struct objfile *objfile = dwarf2_per_objfile->objfile;
18732
18733 name
18734 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18735 &objfile->per_bfd->storage_obstack);
18736 }
71c25dea
TT
18737 break;
18738 }
c906108c 18739 break;
31ef98ae 18740 case DW_AT_linkage_name:
c906108c 18741 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18742 /* Note that both forms of linkage name might appear. We
18743 assume they will be the same, and we only store the last
18744 one we see. */
48fbe735 18745 linkage_name = DW_STRING (&attr);
c906108c
SS
18746 break;
18747 case DW_AT_low_pc:
18748 has_low_pc_attr = 1;
48fbe735 18749 lowpc = attr_value_as_address (&attr);
c906108c
SS
18750 break;
18751 case DW_AT_high_pc:
18752 has_high_pc_attr = 1;
48fbe735 18753 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18754 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18755 high_pc_relative = 1;
c906108c
SS
18756 break;
18757 case DW_AT_location:
0963b4bd 18758 /* Support the .debug_loc offsets. */
8e19ed76
PS
18759 if (attr_form_is_block (&attr))
18760 {
48fbe735 18761 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18762 }
3690dd37 18763 else if (attr_form_is_section_offset (&attr))
8e19ed76 18764 {
4d3c2250 18765 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18766 }
18767 else
18768 {
4d3c2250
KB
18769 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18770 "partial symbol information");
8e19ed76 18771 }
c906108c 18772 break;
c906108c 18773 case DW_AT_external:
48fbe735 18774 is_external = DW_UNSND (&attr);
c906108c
SS
18775 break;
18776 case DW_AT_declaration:
48fbe735 18777 is_declaration = DW_UNSND (&attr);
c906108c
SS
18778 break;
18779 case DW_AT_type:
48fbe735 18780 has_type = 1;
c906108c
SS
18781 break;
18782 case DW_AT_abstract_origin:
18783 case DW_AT_specification:
72bf9492 18784 case DW_AT_extension:
48fbe735
YQ
18785 has_specification = 1;
18786 spec_offset = dwarf2_get_ref_die_offset (&attr);
18787 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18788 || cu->per_cu->is_dwz);
c906108c
SS
18789 break;
18790 case DW_AT_sibling:
18791 /* Ignore absolute siblings, they might point outside of
18792 the current compile unit. */
18793 if (attr.form == DW_FORM_ref_addr)
b98664d3 18794 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18795 else
b9502d3f 18796 {
48fbe735 18797 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18798 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18799 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18800
18801 if (sibling_ptr < info_ptr)
b98664d3 18802 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18803 else if (sibling_ptr > reader->buffer_end)
18804 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18805 else
48fbe735 18806 sibling = sibling_ptr;
b9502d3f 18807 }
c906108c 18808 break;
fa4028e9 18809 case DW_AT_byte_size:
48fbe735 18810 has_byte_size = 1;
fa4028e9 18811 break;
ff908ebf 18812 case DW_AT_const_value:
48fbe735 18813 has_const_value = 1;
ff908ebf 18814 break;
68511cec
CES
18815 case DW_AT_calling_convention:
18816 /* DWARF doesn't provide a way to identify a program's source-level
18817 entry point. DW_AT_calling_convention attributes are only meant
18818 to describe functions' calling conventions.
18819
18820 However, because it's a necessary piece of information in
0c1b455e
TT
18821 Fortran, and before DWARF 4 DW_CC_program was the only
18822 piece of debugging information whose definition refers to
18823 a 'main program' at all, several compilers marked Fortran
18824 main programs with DW_CC_program --- even when those
18825 functions use the standard calling conventions.
18826
18827 Although DWARF now specifies a way to provide this
18828 information, we support this practice for backward
18829 compatibility. */
68511cec 18830 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18831 && cu->language == language_fortran)
48fbe735 18832 main_subprogram = 1;
68511cec 18833 break;
481860b3
GB
18834 case DW_AT_inline:
18835 if (DW_UNSND (&attr) == DW_INL_inlined
18836 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18837 may_be_inlined = 1;
481860b3 18838 break;
95554aad
TT
18839
18840 case DW_AT_import:
48fbe735 18841 if (tag == DW_TAG_imported_unit)
36586728 18842 {
48fbe735
YQ
18843 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18844 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18845 || cu->per_cu->is_dwz);
18846 }
95554aad
TT
18847 break;
18848
0c1b455e 18849 case DW_AT_main_subprogram:
48fbe735 18850 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18851 break;
18852
05caa1d2
TT
18853 case DW_AT_ranges:
18854 {
18855 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18856 but that requires a full DIE, so instead we just
18857 reimplement it. */
18858 int need_ranges_base = tag != DW_TAG_compile_unit;
18859 unsigned int ranges_offset = (DW_UNSND (&attr)
18860 + (need_ranges_base
18861 ? cu->ranges_base
18862 : 0));
18863
18864 /* Value of the DW_AT_ranges attribute is the offset in the
18865 .debug_ranges section. */
18866 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18867 nullptr))
18868 has_pc_info = 1;
18869 }
18870 break;
18871
c906108c
SS
18872 default:
18873 break;
18874 }
18875 }
18876
10d06d82
TT
18877 /* For Ada, if both the name and the linkage name appear, we prefer
18878 the latter. This lets "catch exception" work better, regardless
18879 of the order in which the name and linkage name were emitted.
18880 Really, though, this is just a workaround for the fact that gdb
18881 doesn't store both the name and the linkage name. */
18882 if (cu->language == language_ada && linkage_name != nullptr)
18883 name = linkage_name;
18884
91da1414 18885 if (high_pc_relative)
48fbe735 18886 highpc += lowpc;
91da1414 18887
9373cf26
JK
18888 if (has_low_pc_attr && has_high_pc_attr)
18889 {
18890 /* When using the GNU linker, .gnu.linkonce. sections are used to
18891 eliminate duplicate copies of functions and vtables and such.
18892 The linker will arbitrarily choose one and discard the others.
18893 The AT_*_pc values for such functions refer to local labels in
18894 these sections. If the section from that file was discarded, the
18895 labels are not in the output, so the relocs get a value of 0.
18896 If this is a discarded function, mark the pc bounds as invalid,
18897 so that GDB will ignore it. */
48fbe735 18898 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18899 {
48fbe735 18900 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18901 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18902
b98664d3 18903 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18904 "for DIE at %s [in module %s]"),
48fbe735
YQ
18905 paddress (gdbarch, lowpc),
18906 sect_offset_str (sect_off),
9d8780f0 18907 objfile_name (objfile));
9373cf26
JK
18908 }
18909 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18910 else if (lowpc >= highpc)
9373cf26 18911 {
48fbe735 18912 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18913 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18914
b98664d3 18915 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18916 "for DIE at %s [in module %s]"),
48fbe735
YQ
18917 paddress (gdbarch, lowpc),
18918 paddress (gdbarch, highpc),
18919 sect_offset_str (sect_off),
9c541725 18920 objfile_name (objfile));
9373cf26
JK
18921 }
18922 else
48fbe735 18923 has_pc_info = 1;
9373cf26 18924 }
85cbf3d3 18925
c906108c
SS
18926 return info_ptr;
18927}
18928
72bf9492
DJ
18929/* Find a cached partial DIE at OFFSET in CU. */
18930
d590ff25
YQ
18931struct partial_die_info *
18932dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18933{
18934 struct partial_die_info *lookup_die = NULL;
6f06d47b 18935 struct partial_die_info part_die (sect_off);
72bf9492 18936
9a3c8263 18937 lookup_die = ((struct partial_die_info *)
d590ff25 18938 htab_find_with_hash (partial_dies, &part_die,
9c541725 18939 to_underlying (sect_off)));
72bf9492 18940
72bf9492
DJ
18941 return lookup_die;
18942}
18943
348e048f
DE
18944/* Find a partial DIE at OFFSET, which may or may not be in CU,
18945 except in the case of .debug_types DIEs which do not reference
18946 outside their CU (they do however referencing other types via
55f1336d 18947 DW_FORM_ref_sig8). */
72bf9492 18948
122cf0f2 18949static const struct cu_partial_die_info
9c541725 18950find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18951{
518817b3
SM
18952 struct dwarf2_per_objfile *dwarf2_per_objfile
18953 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18954 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18955 struct dwarf2_per_cu_data *per_cu = NULL;
18956 struct partial_die_info *pd = NULL;
72bf9492 18957
36586728 18958 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18959 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18960 {
d590ff25 18961 pd = cu->find_partial_die (sect_off);
5afb4e99 18962 if (pd != NULL)
fb816e8b 18963 return { cu, pd };
0d99eb77
DE
18964 /* We missed recording what we needed.
18965 Load all dies and try again. */
18966 per_cu = cu->per_cu;
5afb4e99 18967 }
0d99eb77
DE
18968 else
18969 {
18970 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18971 if (cu->per_cu->is_debug_types)
0d99eb77 18972 {
9d8780f0
SM
18973 error (_("Dwarf Error: Type Unit at offset %s contains"
18974 " external reference to offset %s [in module %s].\n"),
18975 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18976 bfd_get_filename (objfile->obfd));
18977 }
9c541725 18978 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18979 dwarf2_per_objfile);
72bf9492 18980
0d99eb77
DE
18981 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18982 load_partial_comp_unit (per_cu);
ae038cb0 18983
0d99eb77 18984 per_cu->cu->last_used = 0;
d590ff25 18985 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18986 }
5afb4e99 18987
dee91e82
DE
18988 /* If we didn't find it, and not all dies have been loaded,
18989 load them all and try again. */
18990
5afb4e99
DJ
18991 if (pd == NULL && per_cu->load_all_dies == 0)
18992 {
5afb4e99 18993 per_cu->load_all_dies = 1;
fd820528
DE
18994
18995 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18996 THIS_CU->cu may already be in use. So we can't just free it and
18997 replace its DIEs with the ones we read in. Instead, we leave those
18998 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18999 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19000 set. */
dee91e82 19001 load_partial_comp_unit (per_cu);
5afb4e99 19002
d590ff25 19003 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19004 }
19005
19006 if (pd == NULL)
19007 internal_error (__FILE__, __LINE__,
9d8780f0 19008 _("could not find partial DIE %s "
3e43a32a 19009 "in cache [from module %s]\n"),
9d8780f0 19010 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19011 return { per_cu->cu, pd };
72bf9492
DJ
19012}
19013
abc72ce4
DE
19014/* See if we can figure out if the class lives in a namespace. We do
19015 this by looking for a member function; its demangled name will
19016 contain namespace info, if there is any. */
19017
19018static void
19019guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19020 struct dwarf2_cu *cu)
19021{
19022 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19023 what template types look like, because the demangler
19024 frequently doesn't give the same name as the debug info. We
19025 could fix this by only using the demangled name to get the
19026 prefix (but see comment in read_structure_type). */
19027
19028 struct partial_die_info *real_pdi;
19029 struct partial_die_info *child_pdi;
19030
19031 /* If this DIE (this DIE's specification, if any) has a parent, then
19032 we should not do this. We'll prepend the parent's fully qualified
19033 name when we create the partial symbol. */
19034
19035 real_pdi = struct_pdi;
19036 while (real_pdi->has_specification)
fb816e8b 19037 {
122cf0f2
AB
19038 auto res = find_partial_die (real_pdi->spec_offset,
19039 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19040 real_pdi = res.pdi;
19041 cu = res.cu;
19042 }
abc72ce4
DE
19043
19044 if (real_pdi->die_parent != NULL)
19045 return;
19046
19047 for (child_pdi = struct_pdi->die_child;
19048 child_pdi != NULL;
19049 child_pdi = child_pdi->die_sibling)
19050 {
19051 if (child_pdi->tag == DW_TAG_subprogram
19052 && child_pdi->linkage_name != NULL)
19053 {
19054 char *actual_class_name
19055 = language_class_name_from_physname (cu->language_defn,
19056 child_pdi->linkage_name);
19057 if (actual_class_name != NULL)
19058 {
518817b3 19059 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19060 struct_pdi->name
021887d8
TT
19061 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19062 actual_class_name);
abc72ce4
DE
19063 xfree (actual_class_name);
19064 }
19065 break;
19066 }
19067 }
19068}
19069
52356b79
YQ
19070void
19071partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19072{
abc72ce4
DE
19073 /* Once we've fixed up a die, there's no point in doing so again.
19074 This also avoids a memory leak if we were to call
19075 guess_partial_die_structure_name multiple times. */
52356b79 19076 if (fixup_called)
abc72ce4
DE
19077 return;
19078
72bf9492
DJ
19079 /* If we found a reference attribute and the DIE has no name, try
19080 to find a name in the referred to DIE. */
19081
52356b79 19082 if (name == NULL && has_specification)
72bf9492
DJ
19083 {
19084 struct partial_die_info *spec_die;
72bf9492 19085
122cf0f2 19086 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19087 spec_die = res.pdi;
19088 cu = res.cu;
72bf9492 19089
52356b79 19090 spec_die->fixup (cu);
72bf9492
DJ
19091
19092 if (spec_die->name)
19093 {
52356b79 19094 name = spec_die->name;
72bf9492
DJ
19095
19096 /* Copy DW_AT_external attribute if it is set. */
19097 if (spec_die->is_external)
52356b79 19098 is_external = spec_die->is_external;
72bf9492
DJ
19099 }
19100 }
19101
19102 /* Set default names for some unnamed DIEs. */
72bf9492 19103
52356b79
YQ
19104 if (name == NULL && tag == DW_TAG_namespace)
19105 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19106
abc72ce4
DE
19107 /* If there is no parent die to provide a namespace, and there are
19108 children, see if we can determine the namespace from their linkage
122d1940 19109 name. */
abc72ce4 19110 if (cu->language == language_cplus
fd5866f6 19111 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19112 && die_parent == NULL
19113 && has_children
19114 && (tag == DW_TAG_class_type
19115 || tag == DW_TAG_structure_type
19116 || tag == DW_TAG_union_type))
19117 guess_partial_die_structure_name (this, cu);
abc72ce4 19118
53832f31
TT
19119 /* GCC might emit a nameless struct or union that has a linkage
19120 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19121 if (name == NULL
19122 && (tag == DW_TAG_class_type
19123 || tag == DW_TAG_interface_type
19124 || tag == DW_TAG_structure_type
19125 || tag == DW_TAG_union_type)
19126 && linkage_name != NULL)
53832f31
TT
19127 {
19128 char *demangled;
19129
52356b79 19130 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19131 if (demangled)
19132 {
96408a79
SA
19133 const char *base;
19134
19135 /* Strip any leading namespaces/classes, keep only the base name.
19136 DW_AT_name for named DIEs does not contain the prefixes. */
19137 base = strrchr (demangled, ':');
19138 if (base && base > demangled && base[-1] == ':')
19139 base++;
19140 else
19141 base = demangled;
19142
518817b3 19143 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19144 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19145 xfree (demangled);
19146 }
19147 }
19148
52356b79 19149 fixup_called = 1;
72bf9492
DJ
19150}
19151
a8329558 19152/* Read an attribute value described by an attribute form. */
c906108c 19153
d521ce57 19154static const gdb_byte *
dee91e82
DE
19155read_attribute_value (const struct die_reader_specs *reader,
19156 struct attribute *attr, unsigned form,
43988095 19157 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19158{
dee91e82 19159 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19160 struct dwarf2_per_objfile *dwarf2_per_objfile
19161 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19162 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19164 bfd *abfd = reader->abfd;
e7c27a73 19165 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19166 unsigned int bytes_read;
19167 struct dwarf_block *blk;
19168
aead7601 19169 attr->form = (enum dwarf_form) form;
a8329558 19170 switch (form)
c906108c 19171 {
c906108c 19172 case DW_FORM_ref_addr:
ae411497 19173 if (cu->header.version == 2)
4568ecf9 19174 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19175 else
4568ecf9
DE
19176 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19177 &cu->header, &bytes_read);
ae411497
TT
19178 info_ptr += bytes_read;
19179 break;
36586728
TT
19180 case DW_FORM_GNU_ref_alt:
19181 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19182 info_ptr += bytes_read;
19183 break;
ae411497 19184 case DW_FORM_addr:
e7c27a73 19185 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19186 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19187 info_ptr += bytes_read;
c906108c
SS
19188 break;
19189 case DW_FORM_block2:
7b5a2f43 19190 blk = dwarf_alloc_block (cu);
c906108c
SS
19191 blk->size = read_2_bytes (abfd, info_ptr);
19192 info_ptr += 2;
19193 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19194 info_ptr += blk->size;
19195 DW_BLOCK (attr) = blk;
19196 break;
19197 case DW_FORM_block4:
7b5a2f43 19198 blk = dwarf_alloc_block (cu);
c906108c
SS
19199 blk->size = read_4_bytes (abfd, info_ptr);
19200 info_ptr += 4;
19201 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19202 info_ptr += blk->size;
19203 DW_BLOCK (attr) = blk;
19204 break;
19205 case DW_FORM_data2:
19206 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19207 info_ptr += 2;
19208 break;
19209 case DW_FORM_data4:
19210 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19211 info_ptr += 4;
19212 break;
19213 case DW_FORM_data8:
19214 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19215 info_ptr += 8;
19216 break;
0224619f
JK
19217 case DW_FORM_data16:
19218 blk = dwarf_alloc_block (cu);
19219 blk->size = 16;
19220 blk->data = read_n_bytes (abfd, info_ptr, 16);
19221 info_ptr += 16;
19222 DW_BLOCK (attr) = blk;
19223 break;
2dc7f7b3
TT
19224 case DW_FORM_sec_offset:
19225 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19226 info_ptr += bytes_read;
19227 break;
c906108c 19228 case DW_FORM_string:
9b1c24c8 19229 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19230 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19231 info_ptr += bytes_read;
19232 break;
4bdf3d34 19233 case DW_FORM_strp:
36586728
TT
19234 if (!cu->per_cu->is_dwz)
19235 {
ed2dc618
SM
19236 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19237 abfd, info_ptr, cu_header,
36586728
TT
19238 &bytes_read);
19239 DW_STRING_IS_CANONICAL (attr) = 0;
19240 info_ptr += bytes_read;
19241 break;
19242 }
19243 /* FALLTHROUGH */
43988095
JK
19244 case DW_FORM_line_strp:
19245 if (!cu->per_cu->is_dwz)
19246 {
ed2dc618
SM
19247 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19248 abfd, info_ptr,
43988095
JK
19249 cu_header, &bytes_read);
19250 DW_STRING_IS_CANONICAL (attr) = 0;
19251 info_ptr += bytes_read;
19252 break;
19253 }
19254 /* FALLTHROUGH */
36586728
TT
19255 case DW_FORM_GNU_strp_alt:
19256 {
ed2dc618 19257 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19258 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19259 &bytes_read);
19260
ed2dc618
SM
19261 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19262 dwz, str_offset);
36586728
TT
19263 DW_STRING_IS_CANONICAL (attr) = 0;
19264 info_ptr += bytes_read;
19265 }
4bdf3d34 19266 break;
2dc7f7b3 19267 case DW_FORM_exprloc:
c906108c 19268 case DW_FORM_block:
7b5a2f43 19269 blk = dwarf_alloc_block (cu);
c906108c
SS
19270 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19271 info_ptr += bytes_read;
19272 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19273 info_ptr += blk->size;
19274 DW_BLOCK (attr) = blk;
19275 break;
19276 case DW_FORM_block1:
7b5a2f43 19277 blk = dwarf_alloc_block (cu);
c906108c
SS
19278 blk->size = read_1_byte (abfd, info_ptr);
19279 info_ptr += 1;
19280 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19281 info_ptr += blk->size;
19282 DW_BLOCK (attr) = blk;
19283 break;
19284 case DW_FORM_data1:
19285 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19286 info_ptr += 1;
19287 break;
19288 case DW_FORM_flag:
19289 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19290 info_ptr += 1;
19291 break;
2dc7f7b3
TT
19292 case DW_FORM_flag_present:
19293 DW_UNSND (attr) = 1;
19294 break;
c906108c
SS
19295 case DW_FORM_sdata:
19296 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19297 info_ptr += bytes_read;
19298 break;
19299 case DW_FORM_udata:
19300 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19301 info_ptr += bytes_read;
19302 break;
19303 case DW_FORM_ref1:
9c541725 19304 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19305 + read_1_byte (abfd, info_ptr));
c906108c
SS
19306 info_ptr += 1;
19307 break;
19308 case DW_FORM_ref2:
9c541725 19309 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19310 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19311 info_ptr += 2;
19312 break;
19313 case DW_FORM_ref4:
9c541725 19314 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19315 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19316 info_ptr += 4;
19317 break;
613e1657 19318 case DW_FORM_ref8:
9c541725 19319 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19320 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19321 info_ptr += 8;
19322 break;
55f1336d 19323 case DW_FORM_ref_sig8:
ac9ec31b 19324 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19325 info_ptr += 8;
19326 break;
c906108c 19327 case DW_FORM_ref_udata:
9c541725 19328 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19329 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19330 info_ptr += bytes_read;
19331 break;
c906108c 19332 case DW_FORM_indirect:
a8329558
KW
19333 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19334 info_ptr += bytes_read;
43988095
JK
19335 if (form == DW_FORM_implicit_const)
19336 {
19337 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19338 info_ptr += bytes_read;
19339 }
19340 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19341 info_ptr);
19342 break;
19343 case DW_FORM_implicit_const:
19344 DW_SND (attr) = implicit_const;
a8329558 19345 break;
336d760d 19346 case DW_FORM_addrx:
3019eac3
DE
19347 case DW_FORM_GNU_addr_index:
19348 if (reader->dwo_file == NULL)
19349 {
19350 /* For now flag a hard error.
19351 Later we can turn this into a complaint. */
19352 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19353 dwarf_form_name (form),
19354 bfd_get_filename (abfd));
19355 }
19356 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19357 info_ptr += bytes_read;
19358 break;
cf532bd1 19359 case DW_FORM_strx:
15f18d14
AT
19360 case DW_FORM_strx1:
19361 case DW_FORM_strx2:
19362 case DW_FORM_strx3:
19363 case DW_FORM_strx4:
3019eac3
DE
19364 case DW_FORM_GNU_str_index:
19365 if (reader->dwo_file == NULL)
19366 {
19367 /* For now flag a hard error.
19368 Later we can turn this into a complaint if warranted. */
19369 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19370 dwarf_form_name (form),
19371 bfd_get_filename (abfd));
19372 }
19373 {
15f18d14
AT
19374 ULONGEST str_index;
19375 if (form == DW_FORM_strx1)
19376 {
19377 str_index = read_1_byte (abfd, info_ptr);
19378 info_ptr += 1;
19379 }
19380 else if (form == DW_FORM_strx2)
19381 {
19382 str_index = read_2_bytes (abfd, info_ptr);
19383 info_ptr += 2;
19384 }
19385 else if (form == DW_FORM_strx3)
19386 {
19387 str_index = read_3_bytes (abfd, info_ptr);
19388 info_ptr += 3;
19389 }
19390 else if (form == DW_FORM_strx4)
19391 {
19392 str_index = read_4_bytes (abfd, info_ptr);
19393 info_ptr += 4;
19394 }
19395 else
19396 {
19397 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19398 info_ptr += bytes_read;
19399 }
342587c4 19400 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19401 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19402 }
19403 break;
c906108c 19404 default:
8a3fe4f8 19405 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19406 dwarf_form_name (form),
19407 bfd_get_filename (abfd));
c906108c 19408 }
28e94949 19409
36586728 19410 /* Super hack. */
7771576e 19411 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19412 attr->form = DW_FORM_GNU_ref_alt;
19413
28e94949
JB
19414 /* We have seen instances where the compiler tried to emit a byte
19415 size attribute of -1 which ended up being encoded as an unsigned
19416 0xffffffff. Although 0xffffffff is technically a valid size value,
19417 an object of this size seems pretty unlikely so we can relatively
19418 safely treat these cases as if the size attribute was invalid and
19419 treat them as zero by default. */
19420 if (attr->name == DW_AT_byte_size
19421 && form == DW_FORM_data4
19422 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19423 {
19424 complaint
b98664d3 19425 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19426 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19427 DW_UNSND (attr) = 0;
19428 }
28e94949 19429
c906108c
SS
19430 return info_ptr;
19431}
19432
a8329558
KW
19433/* Read an attribute described by an abbreviated attribute. */
19434
d521ce57 19435static const gdb_byte *
dee91e82
DE
19436read_attribute (const struct die_reader_specs *reader,
19437 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19438 const gdb_byte *info_ptr)
a8329558
KW
19439{
19440 attr->name = abbrev->name;
43988095
JK
19441 return read_attribute_value (reader, attr, abbrev->form,
19442 abbrev->implicit_const, info_ptr);
a8329558
KW
19443}
19444
0963b4bd 19445/* Read dwarf information from a buffer. */
c906108c
SS
19446
19447static unsigned int
a1855c1d 19448read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19449{
fe1b8b76 19450 return bfd_get_8 (abfd, buf);
c906108c
SS
19451}
19452
19453static int
a1855c1d 19454read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19455{
fe1b8b76 19456 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19457}
19458
19459static unsigned int
a1855c1d 19460read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19461{
fe1b8b76 19462 return bfd_get_16 (abfd, buf);
c906108c
SS
19463}
19464
21ae7a4d 19465static int
a1855c1d 19466read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19467{
19468 return bfd_get_signed_16 (abfd, buf);
19469}
19470
15f18d14
AT
19471static unsigned int
19472read_3_bytes (bfd *abfd, const gdb_byte *buf)
19473{
19474 unsigned int result = 0;
19475 for (int i = 0; i < 3; ++i)
19476 {
19477 unsigned char byte = bfd_get_8 (abfd, buf);
19478 buf++;
19479 result |= ((unsigned int) byte << (i * 8));
19480 }
19481 return result;
19482}
19483
c906108c 19484static unsigned int
a1855c1d 19485read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19486{
fe1b8b76 19487 return bfd_get_32 (abfd, buf);
c906108c
SS
19488}
19489
21ae7a4d 19490static int
a1855c1d 19491read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19492{
19493 return bfd_get_signed_32 (abfd, buf);
19494}
19495
93311388 19496static ULONGEST
a1855c1d 19497read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19498{
fe1b8b76 19499 return bfd_get_64 (abfd, buf);
c906108c
SS
19500}
19501
19502static CORE_ADDR
d521ce57 19503read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19504 unsigned int *bytes_read)
c906108c 19505{
e7c27a73 19506 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19507 CORE_ADDR retval = 0;
19508
107d2387 19509 if (cu_header->signed_addr_p)
c906108c 19510 {
107d2387
AC
19511 switch (cu_header->addr_size)
19512 {
19513 case 2:
fe1b8b76 19514 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19515 break;
19516 case 4:
fe1b8b76 19517 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19518 break;
19519 case 8:
fe1b8b76 19520 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19521 break;
19522 default:
8e65ff28 19523 internal_error (__FILE__, __LINE__,
e2e0b3e5 19524 _("read_address: bad switch, signed [in module %s]"),
659b0389 19525 bfd_get_filename (abfd));
107d2387
AC
19526 }
19527 }
19528 else
19529 {
19530 switch (cu_header->addr_size)
19531 {
19532 case 2:
fe1b8b76 19533 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19534 break;
19535 case 4:
fe1b8b76 19536 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19537 break;
19538 case 8:
fe1b8b76 19539 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19540 break;
19541 default:
8e65ff28 19542 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19543 _("read_address: bad switch, "
19544 "unsigned [in module %s]"),
659b0389 19545 bfd_get_filename (abfd));
107d2387 19546 }
c906108c 19547 }
64367e0a 19548
107d2387
AC
19549 *bytes_read = cu_header->addr_size;
19550 return retval;
c906108c
SS
19551}
19552
f7ef9339 19553/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19554 specification allows the initial length to take up either 4 bytes
19555 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19556 bytes describe the length and all offsets will be 8 bytes in length
19557 instead of 4.
19558
f7ef9339
KB
19559 An older, non-standard 64-bit format is also handled by this
19560 function. The older format in question stores the initial length
19561 as an 8-byte quantity without an escape value. Lengths greater
19562 than 2^32 aren't very common which means that the initial 4 bytes
19563 is almost always zero. Since a length value of zero doesn't make
19564 sense for the 32-bit format, this initial zero can be considered to
19565 be an escape value which indicates the presence of the older 64-bit
19566 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19567 greater than 4GB. If it becomes necessary to handle lengths
19568 somewhat larger than 4GB, we could allow other small values (such
19569 as the non-sensical values of 1, 2, and 3) to also be used as
19570 escape values indicating the presence of the old format.
f7ef9339 19571
917c78fc
MK
19572 The value returned via bytes_read should be used to increment the
19573 relevant pointer after calling read_initial_length().
c764a876 19574
613e1657
KB
19575 [ Note: read_initial_length() and read_offset() are based on the
19576 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19577 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19578 from:
19579
f7ef9339 19580 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19581
613e1657
KB
19582 This document is only a draft and is subject to change. (So beware.)
19583
f7ef9339 19584 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19585 determined empirically by examining 64-bit ELF files produced by
19586 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19587
19588 - Kevin, July 16, 2002
613e1657
KB
19589 ] */
19590
19591static LONGEST
d521ce57 19592read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19593{
fe1b8b76 19594 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19595
dd373385 19596 if (length == 0xffffffff)
613e1657 19597 {
fe1b8b76 19598 length = bfd_get_64 (abfd, buf + 4);
613e1657 19599 *bytes_read = 12;
613e1657 19600 }
dd373385 19601 else if (length == 0)
f7ef9339 19602 {
dd373385 19603 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19604 length = bfd_get_64 (abfd, buf);
f7ef9339 19605 *bytes_read = 8;
f7ef9339 19606 }
613e1657
KB
19607 else
19608 {
19609 *bytes_read = 4;
613e1657
KB
19610 }
19611
c764a876
DE
19612 return length;
19613}
dd373385 19614
c764a876
DE
19615/* Cover function for read_initial_length.
19616 Returns the length of the object at BUF, and stores the size of the
19617 initial length in *BYTES_READ and stores the size that offsets will be in
19618 *OFFSET_SIZE.
19619 If the initial length size is not equivalent to that specified in
19620 CU_HEADER then issue a complaint.
19621 This is useful when reading non-comp-unit headers. */
dd373385 19622
c764a876 19623static LONGEST
d521ce57 19624read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19625 const struct comp_unit_head *cu_header,
19626 unsigned int *bytes_read,
19627 unsigned int *offset_size)
19628{
19629 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19630
19631 gdb_assert (cu_header->initial_length_size == 4
19632 || cu_header->initial_length_size == 8
19633 || cu_header->initial_length_size == 12);
19634
19635 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19636 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19637
c764a876 19638 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19639 return length;
613e1657
KB
19640}
19641
19642/* Read an offset from the data stream. The size of the offset is
917c78fc 19643 given by cu_header->offset_size. */
613e1657
KB
19644
19645static LONGEST
d521ce57
TT
19646read_offset (bfd *abfd, const gdb_byte *buf,
19647 const struct comp_unit_head *cu_header,
891d2f0b 19648 unsigned int *bytes_read)
c764a876
DE
19649{
19650 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19651
c764a876
DE
19652 *bytes_read = cu_header->offset_size;
19653 return offset;
19654}
19655
19656/* Read an offset from the data stream. */
19657
19658static LONGEST
d521ce57 19659read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19660{
19661 LONGEST retval = 0;
19662
c764a876 19663 switch (offset_size)
613e1657
KB
19664 {
19665 case 4:
fe1b8b76 19666 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19667 break;
19668 case 8:
fe1b8b76 19669 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19670 break;
19671 default:
8e65ff28 19672 internal_error (__FILE__, __LINE__,
c764a876 19673 _("read_offset_1: bad switch [in module %s]"),
659b0389 19674 bfd_get_filename (abfd));
613e1657
KB
19675 }
19676
917c78fc 19677 return retval;
613e1657
KB
19678}
19679
d521ce57
TT
19680static const gdb_byte *
19681read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19682{
19683 /* If the size of a host char is 8 bits, we can return a pointer
19684 to the buffer, otherwise we have to copy the data to a buffer
19685 allocated on the temporary obstack. */
4bdf3d34 19686 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19687 return buf;
c906108c
SS
19688}
19689
d521ce57
TT
19690static const char *
19691read_direct_string (bfd *abfd, const gdb_byte *buf,
19692 unsigned int *bytes_read_ptr)
c906108c
SS
19693{
19694 /* If the size of a host char is 8 bits, we can return a pointer
19695 to the string, otherwise we have to copy the string to a buffer
19696 allocated on the temporary obstack. */
4bdf3d34 19697 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19698 if (*buf == '\0')
19699 {
19700 *bytes_read_ptr = 1;
19701 return NULL;
19702 }
d521ce57
TT
19703 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19704 return (const char *) buf;
4bdf3d34
JJ
19705}
19706
43988095
JK
19707/* Return pointer to string at section SECT offset STR_OFFSET with error
19708 reporting strings FORM_NAME and SECT_NAME. */
19709
d521ce57 19710static const char *
ed2dc618
SM
19711read_indirect_string_at_offset_from (struct objfile *objfile,
19712 bfd *abfd, LONGEST str_offset,
43988095
JK
19713 struct dwarf2_section_info *sect,
19714 const char *form_name,
19715 const char *sect_name)
19716{
ed2dc618 19717 dwarf2_read_section (objfile, sect);
43988095
JK
19718 if (sect->buffer == NULL)
19719 error (_("%s used without %s section [in module %s]"),
19720 form_name, sect_name, bfd_get_filename (abfd));
19721 if (str_offset >= sect->size)
19722 error (_("%s pointing outside of %s section [in module %s]"),
19723 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19724 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19725 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19726 return NULL;
43988095
JK
19727 return (const char *) (sect->buffer + str_offset);
19728}
19729
19730/* Return pointer to string at .debug_str offset STR_OFFSET. */
19731
19732static const char *
ed2dc618
SM
19733read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19734 bfd *abfd, LONGEST str_offset)
43988095 19735{
ed2dc618
SM
19736 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19737 abfd, str_offset,
43988095
JK
19738 &dwarf2_per_objfile->str,
19739 "DW_FORM_strp", ".debug_str");
19740}
19741
19742/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19743
19744static const char *
ed2dc618
SM
19745read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19746 bfd *abfd, LONGEST str_offset)
43988095 19747{
ed2dc618
SM
19748 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19749 abfd, str_offset,
43988095
JK
19750 &dwarf2_per_objfile->line_str,
19751 "DW_FORM_line_strp",
19752 ".debug_line_str");
c906108c
SS
19753}
19754
36586728
TT
19755/* Read a string at offset STR_OFFSET in the .debug_str section from
19756 the .dwz file DWZ. Throw an error if the offset is too large. If
19757 the string consists of a single NUL byte, return NULL; otherwise
19758 return a pointer to the string. */
19759
d521ce57 19760static const char *
ed2dc618
SM
19761read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19762 LONGEST str_offset)
36586728 19763{
ed2dc618 19764 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19765
19766 if (dwz->str.buffer == NULL)
19767 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19768 "section [in module %s]"),
00f93c44 19769 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19770 if (str_offset >= dwz->str.size)
19771 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19772 ".debug_str section [in module %s]"),
00f93c44 19773 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19774 gdb_assert (HOST_CHAR_BIT == 8);
19775 if (dwz->str.buffer[str_offset] == '\0')
19776 return NULL;
d521ce57 19777 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19778}
19779
43988095
JK
19780/* Return pointer to string at .debug_str offset as read from BUF.
19781 BUF is assumed to be in a compilation unit described by CU_HEADER.
19782 Return *BYTES_READ_PTR count of bytes read from BUF. */
19783
d521ce57 19784static const char *
ed2dc618
SM
19785read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19786 const gdb_byte *buf,
cf2c3c16
TT
19787 const struct comp_unit_head *cu_header,
19788 unsigned int *bytes_read_ptr)
19789{
19790 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19791
ed2dc618 19792 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19793}
19794
43988095
JK
19795/* Return pointer to string at .debug_line_str offset as read from BUF.
19796 BUF is assumed to be in a compilation unit described by CU_HEADER.
19797 Return *BYTES_READ_PTR count of bytes read from BUF. */
19798
19799static const char *
ed2dc618
SM
19800read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19801 bfd *abfd, const gdb_byte *buf,
43988095
JK
19802 const struct comp_unit_head *cu_header,
19803 unsigned int *bytes_read_ptr)
19804{
19805 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19806
ed2dc618
SM
19807 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19808 str_offset);
43988095
JK
19809}
19810
19811ULONGEST
d521ce57 19812read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19813 unsigned int *bytes_read_ptr)
c906108c 19814{
12df843f 19815 ULONGEST result;
ce5d95e1 19816 unsigned int num_read;
870f88f7 19817 int shift;
c906108c
SS
19818 unsigned char byte;
19819
19820 result = 0;
19821 shift = 0;
19822 num_read = 0;
c906108c
SS
19823 while (1)
19824 {
fe1b8b76 19825 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19826 buf++;
19827 num_read++;
12df843f 19828 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19829 if ((byte & 128) == 0)
19830 {
19831 break;
19832 }
19833 shift += 7;
19834 }
19835 *bytes_read_ptr = num_read;
19836 return result;
19837}
19838
12df843f 19839static LONGEST
d521ce57
TT
19840read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19841 unsigned int *bytes_read_ptr)
c906108c 19842{
4dd1b460 19843 ULONGEST result;
870f88f7 19844 int shift, num_read;
c906108c
SS
19845 unsigned char byte;
19846
19847 result = 0;
19848 shift = 0;
c906108c 19849 num_read = 0;
c906108c
SS
19850 while (1)
19851 {
fe1b8b76 19852 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19853 buf++;
19854 num_read++;
4dd1b460 19855 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19856 shift += 7;
19857 if ((byte & 128) == 0)
19858 {
19859 break;
19860 }
19861 }
77e0b926 19862 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19863 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19864 *bytes_read_ptr = num_read;
19865 return result;
19866}
19867
3019eac3
DE
19868/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19869 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19870 ADDR_SIZE is the size of addresses from the CU header. */
19871
19872static CORE_ADDR
ed2dc618
SM
19873read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19874 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19875{
19876 struct objfile *objfile = dwarf2_per_objfile->objfile;
19877 bfd *abfd = objfile->obfd;
19878 const gdb_byte *info_ptr;
19879
19880 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19881 if (dwarf2_per_objfile->addr.buffer == NULL)
19882 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19883 objfile_name (objfile));
3019eac3
DE
19884 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19885 error (_("DW_FORM_addr_index pointing outside of "
19886 ".debug_addr section [in module %s]"),
4262abfb 19887 objfile_name (objfile));
3019eac3
DE
19888 info_ptr = (dwarf2_per_objfile->addr.buffer
19889 + addr_base + addr_index * addr_size);
19890 if (addr_size == 4)
19891 return bfd_get_32 (abfd, info_ptr);
19892 else
19893 return bfd_get_64 (abfd, info_ptr);
19894}
19895
19896/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19897
19898static CORE_ADDR
19899read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19900{
518817b3
SM
19901 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19902 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19903}
19904
19905/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19906
19907static CORE_ADDR
d521ce57 19908read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19909 unsigned int *bytes_read)
19910{
518817b3 19911 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19912 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19913
19914 return read_addr_index (cu, addr_index);
19915}
19916
19917/* Data structure to pass results from dwarf2_read_addr_index_reader
19918 back to dwarf2_read_addr_index. */
19919
19920struct dwarf2_read_addr_index_data
19921{
19922 ULONGEST addr_base;
19923 int addr_size;
19924};
19925
19926/* die_reader_func for dwarf2_read_addr_index. */
19927
19928static void
19929dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19930 const gdb_byte *info_ptr,
3019eac3
DE
19931 struct die_info *comp_unit_die,
19932 int has_children,
19933 void *data)
19934{
19935 struct dwarf2_cu *cu = reader->cu;
19936 struct dwarf2_read_addr_index_data *aidata =
19937 (struct dwarf2_read_addr_index_data *) data;
19938
19939 aidata->addr_base = cu->addr_base;
19940 aidata->addr_size = cu->header.addr_size;
19941}
19942
19943/* Given an index in .debug_addr, fetch the value.
19944 NOTE: This can be called during dwarf expression evaluation,
19945 long after the debug information has been read, and thus per_cu->cu
19946 may no longer exist. */
19947
19948CORE_ADDR
19949dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19950 unsigned int addr_index)
19951{
ed2dc618 19952 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19953 struct dwarf2_cu *cu = per_cu->cu;
19954 ULONGEST addr_base;
19955 int addr_size;
19956
3019eac3
DE
19957 /* We need addr_base and addr_size.
19958 If we don't have PER_CU->cu, we have to get it.
19959 Nasty, but the alternative is storing the needed info in PER_CU,
19960 which at this point doesn't seem justified: it's not clear how frequently
19961 it would get used and it would increase the size of every PER_CU.
19962 Entry points like dwarf2_per_cu_addr_size do a similar thing
19963 so we're not in uncharted territory here.
19964 Alas we need to be a bit more complicated as addr_base is contained
19965 in the DIE.
19966
19967 We don't need to read the entire CU(/TU).
19968 We just need the header and top level die.
a1b64ce1 19969
3019eac3 19970 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19971 For now we skip this optimization. */
3019eac3
DE
19972
19973 if (cu != NULL)
19974 {
19975 addr_base = cu->addr_base;
19976 addr_size = cu->header.addr_size;
19977 }
19978 else
19979 {
19980 struct dwarf2_read_addr_index_data aidata;
19981
a1b64ce1
DE
19982 /* Note: We can't use init_cutu_and_read_dies_simple here,
19983 we need addr_base. */
58f0c718 19984 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19985 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19986 addr_base = aidata.addr_base;
19987 addr_size = aidata.addr_size;
19988 }
19989
ed2dc618
SM
19990 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19991 addr_size);
3019eac3
DE
19992}
19993
cf532bd1 19994/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 19995 This is only used by the Fission support. */
3019eac3 19996
d521ce57 19997static const char *
342587c4 19998read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19999{
ed2dc618 20000 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20001 struct dwarf2_per_objfile *dwarf2_per_objfile
20002 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20003 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20004 const char *objf_name = objfile_name (objfile);
3019eac3 20005 bfd *abfd = objfile->obfd;
73869dc2
DE
20006 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20007 struct dwarf2_section_info *str_offsets_section =
20008 &reader->dwo_file->sections.str_offsets;
d521ce57 20009 const gdb_byte *info_ptr;
3019eac3 20010 ULONGEST str_offset;
cf532bd1 20011 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20012
73869dc2
DE
20013 dwarf2_read_section (objfile, str_section);
20014 dwarf2_read_section (objfile, str_offsets_section);
20015 if (str_section->buffer == NULL)
57d63ce2 20016 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20017 " in CU at offset %s [in module %s]"),
20018 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20019 if (str_offsets_section->buffer == NULL)
57d63ce2 20020 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20021 " in CU at offset %s [in module %s]"),
20022 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20023 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20024 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20025 " section in CU at offset %s [in module %s]"),
20026 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20027 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20028 + str_index * cu->header.offset_size);
20029 if (cu->header.offset_size == 4)
20030 str_offset = bfd_get_32 (abfd, info_ptr);
20031 else
20032 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20033 if (str_offset >= str_section->size)
57d63ce2 20034 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20035 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20036 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20037 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20038}
20039
3019eac3
DE
20040/* Return the length of an LEB128 number in BUF. */
20041
20042static int
20043leb128_size (const gdb_byte *buf)
20044{
20045 const gdb_byte *begin = buf;
20046 gdb_byte byte;
20047
20048 while (1)
20049 {
20050 byte = *buf++;
20051 if ((byte & 128) == 0)
20052 return buf - begin;
20053 }
20054}
20055
c906108c 20056static void
e142c38c 20057set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20058{
20059 switch (lang)
20060 {
20061 case DW_LANG_C89:
76bee0cc 20062 case DW_LANG_C99:
0cfd832f 20063 case DW_LANG_C11:
c906108c 20064 case DW_LANG_C:
d1be3247 20065 case DW_LANG_UPC:
e142c38c 20066 cu->language = language_c;
c906108c 20067 break;
9c37b5ae 20068 case DW_LANG_Java:
c906108c 20069 case DW_LANG_C_plus_plus:
0cfd832f
MW
20070 case DW_LANG_C_plus_plus_11:
20071 case DW_LANG_C_plus_plus_14:
e142c38c 20072 cu->language = language_cplus;
c906108c 20073 break;
6aecb9c2
JB
20074 case DW_LANG_D:
20075 cu->language = language_d;
20076 break;
c906108c
SS
20077 case DW_LANG_Fortran77:
20078 case DW_LANG_Fortran90:
b21b22e0 20079 case DW_LANG_Fortran95:
f7de9aab
MW
20080 case DW_LANG_Fortran03:
20081 case DW_LANG_Fortran08:
e142c38c 20082 cu->language = language_fortran;
c906108c 20083 break;
a766d390
DE
20084 case DW_LANG_Go:
20085 cu->language = language_go;
20086 break;
c906108c 20087 case DW_LANG_Mips_Assembler:
e142c38c 20088 cu->language = language_asm;
c906108c
SS
20089 break;
20090 case DW_LANG_Ada83:
8aaf0b47 20091 case DW_LANG_Ada95:
bc5f45f8
JB
20092 cu->language = language_ada;
20093 break;
72019c9c
GM
20094 case DW_LANG_Modula2:
20095 cu->language = language_m2;
20096 break;
fe8e67fd
PM
20097 case DW_LANG_Pascal83:
20098 cu->language = language_pascal;
20099 break;
22566fbd
DJ
20100 case DW_LANG_ObjC:
20101 cu->language = language_objc;
20102 break;
c44af4eb
TT
20103 case DW_LANG_Rust:
20104 case DW_LANG_Rust_old:
20105 cu->language = language_rust;
20106 break;
c906108c
SS
20107 case DW_LANG_Cobol74:
20108 case DW_LANG_Cobol85:
c906108c 20109 default:
e142c38c 20110 cu->language = language_minimal;
c906108c
SS
20111 break;
20112 }
e142c38c 20113 cu->language_defn = language_def (cu->language);
c906108c
SS
20114}
20115
20116/* Return the named attribute or NULL if not there. */
20117
20118static struct attribute *
e142c38c 20119dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20120{
a48e046c 20121 for (;;)
c906108c 20122 {
a48e046c
TT
20123 unsigned int i;
20124 struct attribute *spec = NULL;
20125
20126 for (i = 0; i < die->num_attrs; ++i)
20127 {
20128 if (die->attrs[i].name == name)
20129 return &die->attrs[i];
20130 if (die->attrs[i].name == DW_AT_specification
20131 || die->attrs[i].name == DW_AT_abstract_origin)
20132 spec = &die->attrs[i];
20133 }
20134
20135 if (!spec)
20136 break;
c906108c 20137
f2f0e013 20138 die = follow_die_ref (die, spec, &cu);
f2f0e013 20139 }
c5aa993b 20140
c906108c
SS
20141 return NULL;
20142}
20143
348e048f
DE
20144/* Return the named attribute or NULL if not there,
20145 but do not follow DW_AT_specification, etc.
20146 This is for use in contexts where we're reading .debug_types dies.
20147 Following DW_AT_specification, DW_AT_abstract_origin will take us
20148 back up the chain, and we want to go down. */
20149
20150static struct attribute *
45e58e77 20151dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20152{
20153 unsigned int i;
20154
20155 for (i = 0; i < die->num_attrs; ++i)
20156 if (die->attrs[i].name == name)
20157 return &die->attrs[i];
20158
20159 return NULL;
20160}
20161
7d45c7c3
KB
20162/* Return the string associated with a string-typed attribute, or NULL if it
20163 is either not found or is of an incorrect type. */
20164
20165static const char *
20166dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20167{
20168 struct attribute *attr;
20169 const char *str = NULL;
20170
20171 attr = dwarf2_attr (die, name, cu);
20172
20173 if (attr != NULL)
20174 {
43988095 20175 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20176 || attr->form == DW_FORM_string
cf532bd1 20177 || attr->form == DW_FORM_strx
8fe0f950
AT
20178 || attr->form == DW_FORM_strx1
20179 || attr->form == DW_FORM_strx2
20180 || attr->form == DW_FORM_strx3
20181 || attr->form == DW_FORM_strx4
b3340438 20182 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20183 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20184 str = DW_STRING (attr);
20185 else
b98664d3 20186 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20187 "DIE at %s in module %s"),
20188 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20189 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20190 }
20191
20192 return str;
20193}
20194
a084a2a6
AT
20195/* Return the dwo name or NULL if not present. If present, it is in either
20196 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20197static const char *
20198dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20199{
20200 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20201 if (dwo_name == nullptr)
20202 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20203 return dwo_name;
20204}
20205
05cf31d1
JB
20206/* Return non-zero iff the attribute NAME is defined for the given DIE,
20207 and holds a non-zero value. This function should only be used for
2dc7f7b3 20208 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20209
20210static int
20211dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20212{
20213 struct attribute *attr = dwarf2_attr (die, name, cu);
20214
20215 return (attr && DW_UNSND (attr));
20216}
20217
3ca72b44 20218static int
e142c38c 20219die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20220{
05cf31d1
JB
20221 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20222 which value is non-zero. However, we have to be careful with
20223 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20224 (via dwarf2_flag_true_p) follows this attribute. So we may
20225 end up accidently finding a declaration attribute that belongs
20226 to a different DIE referenced by the specification attribute,
20227 even though the given DIE does not have a declaration attribute. */
20228 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20229 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20230}
20231
63d06c5c 20232/* Return the die giving the specification for DIE, if there is
f2f0e013 20233 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20234 containing the return value on output. If there is no
20235 specification, but there is an abstract origin, that is
20236 returned. */
63d06c5c
DC
20237
20238static struct die_info *
f2f0e013 20239die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20240{
f2f0e013
DJ
20241 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20242 *spec_cu);
63d06c5c 20243
edb3359d
DJ
20244 if (spec_attr == NULL)
20245 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20246
63d06c5c
DC
20247 if (spec_attr == NULL)
20248 return NULL;
20249 else
f2f0e013 20250 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20251}
c906108c 20252
527f3840
JK
20253/* Stub for free_line_header to match void * callback types. */
20254
20255static void
20256free_line_header_voidp (void *arg)
20257{
9a3c8263 20258 struct line_header *lh = (struct line_header *) arg;
527f3840 20259
fff8551c 20260 delete lh;
527f3840
JK
20261}
20262
fff8551c
PA
20263void
20264line_header::add_include_dir (const char *include_dir)
c906108c 20265{
27e0867f 20266 if (dwarf_line_debug >= 2)
fff8551c
PA
20267 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20268 include_dirs.size () + 1, include_dir);
27e0867f 20269
fff8551c 20270 include_dirs.push_back (include_dir);
debd256d 20271}
6e70227d 20272
fff8551c
PA
20273void
20274line_header::add_file_name (const char *name,
ecfb656c 20275 dir_index d_index,
fff8551c
PA
20276 unsigned int mod_time,
20277 unsigned int length)
debd256d 20278{
27e0867f
DE
20279 if (dwarf_line_debug >= 2)
20280 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20281 (unsigned) file_names.size () + 1, name);
27e0867f 20282
ecfb656c 20283 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20284}
6e70227d 20285
83769d0b 20286/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20287
20288static struct dwarf2_section_info *
20289get_debug_line_section (struct dwarf2_cu *cu)
20290{
20291 struct dwarf2_section_info *section;
518817b3
SM
20292 struct dwarf2_per_objfile *dwarf2_per_objfile
20293 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20294
20295 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20296 DWO file. */
20297 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20298 section = &cu->dwo_unit->dwo_file->sections.line;
20299 else if (cu->per_cu->is_dwz)
20300 {
ed2dc618 20301 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20302
20303 section = &dwz->line;
20304 }
20305 else
20306 section = &dwarf2_per_objfile->line;
20307
20308 return section;
20309}
20310
43988095
JK
20311/* Read directory or file name entry format, starting with byte of
20312 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20313 entries count and the entries themselves in the described entry
20314 format. */
20315
20316static void
ed2dc618
SM
20317read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20318 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20319 struct line_header *lh,
20320 const struct comp_unit_head *cu_header,
20321 void (*callback) (struct line_header *lh,
20322 const char *name,
ecfb656c 20323 dir_index d_index,
43988095
JK
20324 unsigned int mod_time,
20325 unsigned int length))
20326{
20327 gdb_byte format_count, formati;
20328 ULONGEST data_count, datai;
20329 const gdb_byte *buf = *bufp;
20330 const gdb_byte *format_header_data;
43988095
JK
20331 unsigned int bytes_read;
20332
20333 format_count = read_1_byte (abfd, buf);
20334 buf += 1;
20335 format_header_data = buf;
20336 for (formati = 0; formati < format_count; formati++)
20337 {
20338 read_unsigned_leb128 (abfd, buf, &bytes_read);
20339 buf += bytes_read;
20340 read_unsigned_leb128 (abfd, buf, &bytes_read);
20341 buf += bytes_read;
20342 }
20343
20344 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20345 buf += bytes_read;
20346 for (datai = 0; datai < data_count; datai++)
20347 {
20348 const gdb_byte *format = format_header_data;
20349 struct file_entry fe;
20350
43988095
JK
20351 for (formati = 0; formati < format_count; formati++)
20352 {
ecfb656c 20353 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20354 format += bytes_read;
43988095 20355
ecfb656c 20356 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20357 format += bytes_read;
ecfb656c
PA
20358
20359 gdb::optional<const char *> string;
20360 gdb::optional<unsigned int> uint;
20361
43988095
JK
20362 switch (form)
20363 {
20364 case DW_FORM_string:
ecfb656c 20365 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20366 buf += bytes_read;
20367 break;
20368
20369 case DW_FORM_line_strp:
ed2dc618
SM
20370 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20371 abfd, buf,
ecfb656c
PA
20372 cu_header,
20373 &bytes_read));
43988095
JK
20374 buf += bytes_read;
20375 break;
20376
20377 case DW_FORM_data1:
ecfb656c 20378 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20379 buf += 1;
20380 break;
20381
20382 case DW_FORM_data2:
ecfb656c 20383 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20384 buf += 2;
20385 break;
20386
20387 case DW_FORM_data4:
ecfb656c 20388 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20389 buf += 4;
20390 break;
20391
20392 case DW_FORM_data8:
ecfb656c 20393 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20394 buf += 8;
20395 break;
20396
20397 case DW_FORM_udata:
ecfb656c 20398 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20399 buf += bytes_read;
20400 break;
20401
20402 case DW_FORM_block:
20403 /* It is valid only for DW_LNCT_timestamp which is ignored by
20404 current GDB. */
20405 break;
20406 }
ecfb656c
PA
20407
20408 switch (content_type)
20409 {
20410 case DW_LNCT_path:
20411 if (string.has_value ())
20412 fe.name = *string;
20413 break;
20414 case DW_LNCT_directory_index:
20415 if (uint.has_value ())
20416 fe.d_index = (dir_index) *uint;
20417 break;
20418 case DW_LNCT_timestamp:
20419 if (uint.has_value ())
20420 fe.mod_time = *uint;
20421 break;
20422 case DW_LNCT_size:
20423 if (uint.has_value ())
20424 fe.length = *uint;
20425 break;
20426 case DW_LNCT_MD5:
20427 break;
20428 default:
b98664d3 20429 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20430 pulongest (content_type));
20431 }
43988095
JK
20432 }
20433
ecfb656c 20434 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20435 }
20436
20437 *bufp = buf;
20438}
20439
debd256d 20440/* Read the statement program header starting at OFFSET in
3019eac3 20441 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20442 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20443 Returns NULL if there is a problem reading the header, e.g., if it
20444 has a version we don't understand.
debd256d
JB
20445
20446 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20447 the returned object point into the dwarf line section buffer,
20448 and must not be freed. */
ae2de4f8 20449
fff8551c 20450static line_header_up
9c541725 20451dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20452{
d521ce57 20453 const gdb_byte *line_ptr;
c764a876 20454 unsigned int bytes_read, offset_size;
debd256d 20455 int i;
d521ce57 20456 const char *cur_dir, *cur_file;
3019eac3
DE
20457 struct dwarf2_section_info *section;
20458 bfd *abfd;
518817b3
SM
20459 struct dwarf2_per_objfile *dwarf2_per_objfile
20460 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20461
36586728 20462 section = get_debug_line_section (cu);
3019eac3
DE
20463 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20464 if (section->buffer == NULL)
debd256d 20465 {
3019eac3 20466 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20467 complaint (_("missing .debug_line.dwo section"));
3019eac3 20468 else
b98664d3 20469 complaint (_("missing .debug_line section"));
debd256d
JB
20470 return 0;
20471 }
20472
fceca515
DE
20473 /* We can't do this until we know the section is non-empty.
20474 Only then do we know we have such a section. */
a32a8923 20475 abfd = get_section_bfd_owner (section);
fceca515 20476
a738430d
MK
20477 /* Make sure that at least there's room for the total_length field.
20478 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20479 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20480 {
4d3c2250 20481 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20482 return 0;
20483 }
20484
fff8551c 20485 line_header_up lh (new line_header ());
debd256d 20486
9c541725 20487 lh->sect_off = sect_off;
527f3840
JK
20488 lh->offset_in_dwz = cu->per_cu->is_dwz;
20489
9c541725 20490 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20491
a738430d 20492 /* Read in the header. */
6e70227d 20493 lh->total_length =
c764a876
DE
20494 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20495 &bytes_read, &offset_size);
debd256d 20496 line_ptr += bytes_read;
3019eac3 20497 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20498 {
4d3c2250 20499 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20500 return 0;
20501 }
20502 lh->statement_program_end = line_ptr + lh->total_length;
20503 lh->version = read_2_bytes (abfd, line_ptr);
20504 line_ptr += 2;
43988095 20505 if (lh->version > 5)
cd366ee8
DE
20506 {
20507 /* This is a version we don't understand. The format could have
20508 changed in ways we don't handle properly so just punt. */
b98664d3 20509 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20510 return NULL;
20511 }
43988095
JK
20512 if (lh->version >= 5)
20513 {
20514 gdb_byte segment_selector_size;
20515
20516 /* Skip address size. */
20517 read_1_byte (abfd, line_ptr);
20518 line_ptr += 1;
20519
20520 segment_selector_size = read_1_byte (abfd, line_ptr);
20521 line_ptr += 1;
20522 if (segment_selector_size != 0)
20523 {
b98664d3 20524 complaint (_("unsupported segment selector size %u "
43988095
JK
20525 "in .debug_line section"),
20526 segment_selector_size);
20527 return NULL;
20528 }
20529 }
c764a876
DE
20530 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20531 line_ptr += offset_size;
debd256d
JB
20532 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20533 line_ptr += 1;
2dc7f7b3
TT
20534 if (lh->version >= 4)
20535 {
20536 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20537 line_ptr += 1;
20538 }
20539 else
20540 lh->maximum_ops_per_instruction = 1;
20541
20542 if (lh->maximum_ops_per_instruction == 0)
20543 {
20544 lh->maximum_ops_per_instruction = 1;
b98664d3 20545 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20546 "in `.debug_line' section"));
2dc7f7b3
TT
20547 }
20548
debd256d
JB
20549 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20550 line_ptr += 1;
20551 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20552 line_ptr += 1;
20553 lh->line_range = read_1_byte (abfd, line_ptr);
20554 line_ptr += 1;
20555 lh->opcode_base = read_1_byte (abfd, line_ptr);
20556 line_ptr += 1;
fff8551c 20557 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20558
20559 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20560 for (i = 1; i < lh->opcode_base; ++i)
20561 {
20562 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20563 line_ptr += 1;
20564 }
20565
43988095 20566 if (lh->version >= 5)
debd256d 20567 {
43988095 20568 /* Read directory table. */
ed2dc618
SM
20569 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20570 &cu->header,
b926417a 20571 [] (struct line_header *header, const char *name,
ecfb656c 20572 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20573 unsigned int length)
20574 {
b926417a 20575 header->add_include_dir (name);
fff8551c 20576 });
debd256d 20577
43988095 20578 /* Read file name table. */
ed2dc618
SM
20579 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20580 &cu->header,
b926417a 20581 [] (struct line_header *header, const char *name,
ecfb656c 20582 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20583 unsigned int length)
20584 {
b926417a 20585 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20586 });
43988095
JK
20587 }
20588 else
debd256d 20589 {
43988095
JK
20590 /* Read directory table. */
20591 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20592 {
20593 line_ptr += bytes_read;
fff8551c 20594 lh->add_include_dir (cur_dir);
43988095 20595 }
debd256d
JB
20596 line_ptr += bytes_read;
20597
43988095
JK
20598 /* Read file name table. */
20599 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20600 {
ecfb656c
PA
20601 unsigned int mod_time, length;
20602 dir_index d_index;
43988095
JK
20603
20604 line_ptr += bytes_read;
ecfb656c 20605 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20606 line_ptr += bytes_read;
20607 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20608 line_ptr += bytes_read;
20609 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20610 line_ptr += bytes_read;
20611
ecfb656c 20612 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20613 }
20614 line_ptr += bytes_read;
debd256d 20615 }
6e70227d 20616 lh->statement_program_start = line_ptr;
debd256d 20617
3019eac3 20618 if (line_ptr > (section->buffer + section->size))
b98664d3 20619 complaint (_("line number info header doesn't "
3e43a32a 20620 "fit in `.debug_line' section"));
debd256d 20621
debd256d
JB
20622 return lh;
20623}
c906108c 20624
c6da4cef
DE
20625/* Subroutine of dwarf_decode_lines to simplify it.
20626 Return the file name of the psymtab for included file FILE_INDEX
20627 in line header LH of PST.
20628 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20629 If space for the result is malloc'd, *NAME_HOLDER will be set.
20630 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20631
d521ce57 20632static const char *
c6da4cef
DE
20633psymtab_include_file_name (const struct line_header *lh, int file_index,
20634 const struct partial_symtab *pst,
c89b44cd
TT
20635 const char *comp_dir,
20636 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20637{
8c43009f 20638 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20639 const char *include_name = fe.name;
20640 const char *include_name_to_compare = include_name;
72b9f47f 20641 const char *pst_filename;
c6da4cef
DE
20642 int file_is_pst;
20643
8c43009f 20644 const char *dir_name = fe.include_dir (lh);
c6da4cef 20645
c89b44cd 20646 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20647 if (!IS_ABSOLUTE_PATH (include_name)
20648 && (dir_name != NULL || comp_dir != NULL))
20649 {
20650 /* Avoid creating a duplicate psymtab for PST.
20651 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20652 Before we do the comparison, however, we need to account
20653 for DIR_NAME and COMP_DIR.
20654 First prepend dir_name (if non-NULL). If we still don't
20655 have an absolute path prepend comp_dir (if non-NULL).
20656 However, the directory we record in the include-file's
20657 psymtab does not contain COMP_DIR (to match the
20658 corresponding symtab(s)).
20659
20660 Example:
20661
20662 bash$ cd /tmp
20663 bash$ gcc -g ./hello.c
20664 include_name = "hello.c"
20665 dir_name = "."
20666 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20667 DW_AT_name = "./hello.c"
20668
20669 */
c6da4cef
DE
20670
20671 if (dir_name != NULL)
20672 {
c89b44cd
TT
20673 name_holder->reset (concat (dir_name, SLASH_STRING,
20674 include_name, (char *) NULL));
20675 include_name = name_holder->get ();
c6da4cef 20676 include_name_to_compare = include_name;
c6da4cef
DE
20677 }
20678 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20679 {
c89b44cd
TT
20680 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20681 include_name, (char *) NULL));
20682 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20683 }
20684 }
20685
20686 pst_filename = pst->filename;
c89b44cd 20687 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20688 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20689 {
c89b44cd
TT
20690 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20691 pst_filename, (char *) NULL));
20692 pst_filename = copied_name.get ();
c6da4cef
DE
20693 }
20694
1e3fad37 20695 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20696
c6da4cef
DE
20697 if (file_is_pst)
20698 return NULL;
20699 return include_name;
20700}
20701
d9b3de22
DE
20702/* State machine to track the state of the line number program. */
20703
6f77053d 20704class lnp_state_machine
d9b3de22 20705{
6f77053d
PA
20706public:
20707 /* Initialize a machine state for the start of a line number
20708 program. */
804d2729
TT
20709 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20710 bool record_lines_p);
6f77053d 20711
8c43009f
PA
20712 file_entry *current_file ()
20713 {
20714 /* lh->file_names is 0-based, but the file name numbers in the
20715 statement program are 1-based. */
6f77053d
PA
20716 return m_line_header->file_name_at (m_file);
20717 }
20718
20719 /* Record the line in the state machine. END_SEQUENCE is true if
20720 we're processing the end of a sequence. */
20721 void record_line (bool end_sequence);
20722
7ab6656f
OJ
20723 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20724 nop-out rest of the lines in this sequence. */
6f77053d
PA
20725 void check_line_address (struct dwarf2_cu *cu,
20726 const gdb_byte *line_ptr,
7ab6656f 20727 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20728
20729 void handle_set_discriminator (unsigned int discriminator)
20730 {
20731 m_discriminator = discriminator;
20732 m_line_has_non_zero_discriminator |= discriminator != 0;
20733 }
20734
20735 /* Handle DW_LNE_set_address. */
20736 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20737 {
20738 m_op_index = 0;
20739 address += baseaddr;
20740 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20741 }
20742
20743 /* Handle DW_LNS_advance_pc. */
20744 void handle_advance_pc (CORE_ADDR adjust);
20745
20746 /* Handle a special opcode. */
20747 void handle_special_opcode (unsigned char op_code);
20748
20749 /* Handle DW_LNS_advance_line. */
20750 void handle_advance_line (int line_delta)
20751 {
20752 advance_line (line_delta);
20753 }
20754
20755 /* Handle DW_LNS_set_file. */
20756 void handle_set_file (file_name_index file);
20757
20758 /* Handle DW_LNS_negate_stmt. */
20759 void handle_negate_stmt ()
20760 {
20761 m_is_stmt = !m_is_stmt;
20762 }
20763
20764 /* Handle DW_LNS_const_add_pc. */
20765 void handle_const_add_pc ();
20766
20767 /* Handle DW_LNS_fixed_advance_pc. */
20768 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20769 {
20770 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20771 m_op_index = 0;
20772 }
20773
20774 /* Handle DW_LNS_copy. */
20775 void handle_copy ()
20776 {
20777 record_line (false);
20778 m_discriminator = 0;
20779 }
20780
20781 /* Handle DW_LNE_end_sequence. */
20782 void handle_end_sequence ()
20783 {
804d2729 20784 m_currently_recording_lines = true;
6f77053d
PA
20785 }
20786
20787private:
20788 /* Advance the line by LINE_DELTA. */
20789 void advance_line (int line_delta)
20790 {
20791 m_line += line_delta;
20792
20793 if (line_delta != 0)
20794 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20795 }
20796
804d2729
TT
20797 struct dwarf2_cu *m_cu;
20798
6f77053d
PA
20799 gdbarch *m_gdbarch;
20800
20801 /* True if we're recording lines.
20802 Otherwise we're building partial symtabs and are just interested in
20803 finding include files mentioned by the line number program. */
20804 bool m_record_lines_p;
20805
8c43009f 20806 /* The line number header. */
6f77053d 20807 line_header *m_line_header;
8c43009f 20808
6f77053d
PA
20809 /* These are part of the standard DWARF line number state machine,
20810 and initialized according to the DWARF spec. */
d9b3de22 20811
6f77053d 20812 unsigned char m_op_index = 0;
8c43009f 20813 /* The line table index (1-based) of the current file. */
6f77053d
PA
20814 file_name_index m_file = (file_name_index) 1;
20815 unsigned int m_line = 1;
20816
20817 /* These are initialized in the constructor. */
20818
20819 CORE_ADDR m_address;
20820 bool m_is_stmt;
20821 unsigned int m_discriminator;
d9b3de22
DE
20822
20823 /* Additional bits of state we need to track. */
20824
20825 /* The last file that we called dwarf2_start_subfile for.
20826 This is only used for TLLs. */
6f77053d 20827 unsigned int m_last_file = 0;
d9b3de22 20828 /* The last file a line number was recorded for. */
6f77053d 20829 struct subfile *m_last_subfile = NULL;
d9b3de22 20830
804d2729
TT
20831 /* When true, record the lines we decode. */
20832 bool m_currently_recording_lines = false;
d9b3de22
DE
20833
20834 /* The last line number that was recorded, used to coalesce
20835 consecutive entries for the same line. This can happen, for
20836 example, when discriminators are present. PR 17276. */
6f77053d
PA
20837 unsigned int m_last_line = 0;
20838 bool m_line_has_non_zero_discriminator = false;
8c43009f 20839};
d9b3de22 20840
6f77053d
PA
20841void
20842lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20843{
20844 CORE_ADDR addr_adj = (((m_op_index + adjust)
20845 / m_line_header->maximum_ops_per_instruction)
20846 * m_line_header->minimum_instruction_length);
20847 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20848 m_op_index = ((m_op_index + adjust)
20849 % m_line_header->maximum_ops_per_instruction);
20850}
d9b3de22 20851
6f77053d
PA
20852void
20853lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20854{
6f77053d
PA
20855 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20856 CORE_ADDR addr_adj = (((m_op_index
20857 + (adj_opcode / m_line_header->line_range))
20858 / m_line_header->maximum_ops_per_instruction)
20859 * m_line_header->minimum_instruction_length);
20860 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20861 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20862 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20863
6f77053d
PA
20864 int line_delta = (m_line_header->line_base
20865 + (adj_opcode % m_line_header->line_range));
20866 advance_line (line_delta);
20867 record_line (false);
20868 m_discriminator = 0;
20869}
d9b3de22 20870
6f77053d
PA
20871void
20872lnp_state_machine::handle_set_file (file_name_index file)
20873{
20874 m_file = file;
20875
20876 const file_entry *fe = current_file ();
20877 if (fe == NULL)
20878 dwarf2_debug_line_missing_file_complaint ();
20879 else if (m_record_lines_p)
20880 {
20881 const char *dir = fe->include_dir (m_line_header);
20882
c24bdb02 20883 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20884 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20885 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20886 }
20887}
20888
20889void
20890lnp_state_machine::handle_const_add_pc ()
20891{
20892 CORE_ADDR adjust
20893 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20894
20895 CORE_ADDR addr_adj
20896 = (((m_op_index + adjust)
20897 / m_line_header->maximum_ops_per_instruction)
20898 * m_line_header->minimum_instruction_length);
20899
20900 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20901 m_op_index = ((m_op_index + adjust)
20902 % m_line_header->maximum_ops_per_instruction);
20903}
d9b3de22 20904
a05a36a5
DE
20905/* Return non-zero if we should add LINE to the line number table.
20906 LINE is the line to add, LAST_LINE is the last line that was added,
20907 LAST_SUBFILE is the subfile for LAST_LINE.
20908 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20909 had a non-zero discriminator.
20910
20911 We have to be careful in the presence of discriminators.
20912 E.g., for this line:
20913
20914 for (i = 0; i < 100000; i++);
20915
20916 clang can emit four line number entries for that one line,
20917 each with a different discriminator.
20918 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20919
20920 However, we want gdb to coalesce all four entries into one.
20921 Otherwise the user could stepi into the middle of the line and
20922 gdb would get confused about whether the pc really was in the
20923 middle of the line.
20924
20925 Things are further complicated by the fact that two consecutive
20926 line number entries for the same line is a heuristic used by gcc
20927 to denote the end of the prologue. So we can't just discard duplicate
20928 entries, we have to be selective about it. The heuristic we use is
20929 that we only collapse consecutive entries for the same line if at least
20930 one of those entries has a non-zero discriminator. PR 17276.
20931
20932 Note: Addresses in the line number state machine can never go backwards
20933 within one sequence, thus this coalescing is ok. */
20934
20935static int
804d2729
TT
20936dwarf_record_line_p (struct dwarf2_cu *cu,
20937 unsigned int line, unsigned int last_line,
a05a36a5
DE
20938 int line_has_non_zero_discriminator,
20939 struct subfile *last_subfile)
20940{
c24bdb02 20941 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20942 return 1;
20943 if (line != last_line)
20944 return 1;
20945 /* Same line for the same file that we've seen already.
20946 As a last check, for pr 17276, only record the line if the line
20947 has never had a non-zero discriminator. */
20948 if (!line_has_non_zero_discriminator)
20949 return 1;
20950 return 0;
20951}
20952
804d2729
TT
20953/* Use the CU's builder to record line number LINE beginning at
20954 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20955
20956static void
d9b3de22
DE
20957dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20958 unsigned int line, CORE_ADDR address,
804d2729 20959 struct dwarf2_cu *cu)
252a6764
DE
20960{
20961 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20962
27e0867f
DE
20963 if (dwarf_line_debug)
20964 {
20965 fprintf_unfiltered (gdb_stdlog,
20966 "Recording line %u, file %s, address %s\n",
20967 line, lbasename (subfile->name),
20968 paddress (gdbarch, address));
20969 }
20970
804d2729 20971 if (cu != nullptr)
c24bdb02 20972 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20973}
20974
20975/* Subroutine of dwarf_decode_lines_1 to simplify it.
20976 Mark the end of a set of line number records.
d9b3de22 20977 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20978 If SUBFILE is NULL the request is ignored. */
20979
20980static void
20981dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20982 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20983{
27e0867f
DE
20984 if (subfile == NULL)
20985 return;
20986
20987 if (dwarf_line_debug)
20988 {
20989 fprintf_unfiltered (gdb_stdlog,
20990 "Finishing current line, file %s, address %s\n",
20991 lbasename (subfile->name),
20992 paddress (gdbarch, address));
20993 }
20994
804d2729 20995 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20996}
20997
6f77053d
PA
20998void
20999lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21000{
d9b3de22
DE
21001 if (dwarf_line_debug)
21002 {
21003 fprintf_unfiltered (gdb_stdlog,
21004 "Processing actual line %u: file %u,"
21005 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
21006 m_line, to_underlying (m_file),
21007 paddress (m_gdbarch, m_address),
21008 m_is_stmt, m_discriminator);
d9b3de22
DE
21009 }
21010
6f77053d 21011 file_entry *fe = current_file ();
8c43009f
PA
21012
21013 if (fe == NULL)
d9b3de22
DE
21014 dwarf2_debug_line_missing_file_complaint ();
21015 /* For now we ignore lines not starting on an instruction boundary.
21016 But not when processing end_sequence for compatibility with the
21017 previous version of the code. */
6f77053d 21018 else if (m_op_index == 0 || end_sequence)
d9b3de22 21019 {
8c43009f 21020 fe->included_p = 1;
c258c396 21021 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21022 {
c24bdb02 21023 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21024 || end_sequence)
d9b3de22 21025 {
804d2729
TT
21026 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21027 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21028 }
21029
21030 if (!end_sequence)
21031 {
804d2729 21032 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21033 m_line_has_non_zero_discriminator,
21034 m_last_subfile))
d9b3de22 21035 {
c24bdb02 21036 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21037 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21038 builder->get_current_subfile (),
6f77053d 21039 m_line, m_address,
804d2729 21040 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21041 }
c24bdb02 21042 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21043 m_last_line = m_line;
d9b3de22
DE
21044 }
21045 }
21046 }
21047}
21048
804d2729
TT
21049lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21050 line_header *lh, bool record_lines_p)
d9b3de22 21051{
804d2729 21052 m_cu = cu;
6f77053d
PA
21053 m_gdbarch = arch;
21054 m_record_lines_p = record_lines_p;
21055 m_line_header = lh;
d9b3de22 21056
804d2729 21057 m_currently_recording_lines = true;
d9b3de22 21058
d9b3de22
DE
21059 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21060 was a line entry for it so that the backend has a chance to adjust it
21061 and also record it in case it needs it. This is currently used by MIPS
21062 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21063 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21064 m_is_stmt = lh->default_is_stmt;
21065 m_discriminator = 0;
252a6764
DE
21066}
21067
6f77053d
PA
21068void
21069lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21070 const gdb_byte *line_ptr,
7ab6656f 21071 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21072{
7ab6656f
OJ
21073 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21074 the pc range of the CU. However, we restrict the test to only ADDRESS
21075 values of zero to preserve GDB's previous behaviour which is to handle
21076 the specific case of a function being GC'd by the linker. */
924c2928 21077
7ab6656f 21078 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21079 {
21080 /* This line table is for a function which has been
21081 GCd by the linker. Ignore it. PR gdb/12528 */
21082
518817b3 21083 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21084 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21085
b98664d3 21086 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21087 line_offset, objfile_name (objfile));
804d2729
TT
21088 m_currently_recording_lines = false;
21089 /* Note: m_currently_recording_lines is left as false until we see
21090 DW_LNE_end_sequence. */
924c2928
DE
21091 }
21092}
21093
f3f5162e 21094/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21095 Process the line number information in LH.
21096 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21097 program in order to set included_p for every referenced header. */
debd256d 21098
c906108c 21099static void
43f3e411
DE
21100dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21101 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21102{
d521ce57
TT
21103 const gdb_byte *line_ptr, *extended_end;
21104 const gdb_byte *line_end;
a8c50c1f 21105 unsigned int bytes_read, extended_len;
699ca60a 21106 unsigned char op_code, extended_op;
e142c38c 21107 CORE_ADDR baseaddr;
518817b3 21108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21109 bfd *abfd = objfile->obfd;
fbf65064 21110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21111 /* True if we're recording line info (as opposed to building partial
21112 symtabs and just interested in finding include files mentioned by
21113 the line number program). */
21114 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21115
21116 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21117
debd256d
JB
21118 line_ptr = lh->statement_program_start;
21119 line_end = lh->statement_program_end;
c906108c
SS
21120
21121 /* Read the statement sequences until there's nothing left. */
21122 while (line_ptr < line_end)
21123 {
6f77053d
PA
21124 /* The DWARF line number program state machine. Reset the state
21125 machine at the start of each sequence. */
804d2729 21126 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21127 bool end_sequence = false;
d9b3de22 21128
8c43009f 21129 if (record_lines_p)
c906108c 21130 {
8c43009f
PA
21131 /* Start a subfile for the current file of the state
21132 machine. */
21133 const file_entry *fe = state_machine.current_file ();
21134
21135 if (fe != NULL)
804d2729 21136 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21137 }
21138
a738430d 21139 /* Decode the table. */
d9b3de22 21140 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21141 {
21142 op_code = read_1_byte (abfd, line_ptr);
21143 line_ptr += 1;
9aa1fe7e 21144
debd256d 21145 if (op_code >= lh->opcode_base)
6e70227d 21146 {
8e07a239 21147 /* Special opcode. */
6f77053d 21148 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21149 }
21150 else switch (op_code)
c906108c
SS
21151 {
21152 case DW_LNS_extended_op:
3e43a32a
MS
21153 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21154 &bytes_read);
473b7be6 21155 line_ptr += bytes_read;
a8c50c1f 21156 extended_end = line_ptr + extended_len;
c906108c
SS
21157 extended_op = read_1_byte (abfd, line_ptr);
21158 line_ptr += 1;
21159 switch (extended_op)
21160 {
21161 case DW_LNE_end_sequence:
6f77053d
PA
21162 state_machine.handle_end_sequence ();
21163 end_sequence = true;
c906108c
SS
21164 break;
21165 case DW_LNE_set_address:
d9b3de22
DE
21166 {
21167 CORE_ADDR address
21168 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21169 line_ptr += bytes_read;
6f77053d
PA
21170
21171 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21172 lowpc - baseaddr, address);
6f77053d 21173 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21174 }
c906108c
SS
21175 break;
21176 case DW_LNE_define_file:
debd256d 21177 {
d521ce57 21178 const char *cur_file;
ecfb656c
PA
21179 unsigned int mod_time, length;
21180 dir_index dindex;
6e70227d 21181
3e43a32a
MS
21182 cur_file = read_direct_string (abfd, line_ptr,
21183 &bytes_read);
debd256d 21184 line_ptr += bytes_read;
ecfb656c 21185 dindex = (dir_index)
debd256d
JB
21186 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21187 line_ptr += bytes_read;
21188 mod_time =
21189 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21190 line_ptr += bytes_read;
21191 length =
21192 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21193 line_ptr += bytes_read;
ecfb656c 21194 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21195 }
c906108c 21196 break;
d0c6ba3d 21197 case DW_LNE_set_discriminator:
6f77053d
PA
21198 {
21199 /* The discriminator is not interesting to the
21200 debugger; just ignore it. We still need to
21201 check its value though:
21202 if there are consecutive entries for the same
21203 (non-prologue) line we want to coalesce them.
21204 PR 17276. */
21205 unsigned int discr
21206 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21207 line_ptr += bytes_read;
21208
21209 state_machine.handle_set_discriminator (discr);
21210 }
d0c6ba3d 21211 break;
c906108c 21212 default:
b98664d3 21213 complaint (_("mangled .debug_line section"));
debd256d 21214 return;
c906108c 21215 }
a8c50c1f
DJ
21216 /* Make sure that we parsed the extended op correctly. If e.g.
21217 we expected a different address size than the producer used,
21218 we may have read the wrong number of bytes. */
21219 if (line_ptr != extended_end)
21220 {
b98664d3 21221 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21222 return;
21223 }
c906108c
SS
21224 break;
21225 case DW_LNS_copy:
6f77053d 21226 state_machine.handle_copy ();
c906108c
SS
21227 break;
21228 case DW_LNS_advance_pc:
2dc7f7b3
TT
21229 {
21230 CORE_ADDR adjust
21231 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21232 line_ptr += bytes_read;
6f77053d
PA
21233
21234 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21235 }
c906108c
SS
21236 break;
21237 case DW_LNS_advance_line:
a05a36a5
DE
21238 {
21239 int line_delta
21240 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21241 line_ptr += bytes_read;
6f77053d
PA
21242
21243 state_machine.handle_advance_line (line_delta);
a05a36a5 21244 }
c906108c
SS
21245 break;
21246 case DW_LNS_set_file:
d9b3de22 21247 {
6f77053d 21248 file_name_index file
ecfb656c
PA
21249 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21250 &bytes_read);
d9b3de22 21251 line_ptr += bytes_read;
8c43009f 21252
6f77053d 21253 state_machine.handle_set_file (file);
d9b3de22 21254 }
c906108c
SS
21255 break;
21256 case DW_LNS_set_column:
0ad93d4f 21257 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21258 line_ptr += bytes_read;
21259 break;
21260 case DW_LNS_negate_stmt:
6f77053d 21261 state_machine.handle_negate_stmt ();
c906108c
SS
21262 break;
21263 case DW_LNS_set_basic_block:
c906108c 21264 break;
c2c6d25f
JM
21265 /* Add to the address register of the state machine the
21266 address increment value corresponding to special opcode
a738430d
MK
21267 255. I.e., this value is scaled by the minimum
21268 instruction length since special opcode 255 would have
b021a221 21269 scaled the increment. */
c906108c 21270 case DW_LNS_const_add_pc:
6f77053d 21271 state_machine.handle_const_add_pc ();
c906108c
SS
21272 break;
21273 case DW_LNS_fixed_advance_pc:
3e29f34a 21274 {
6f77053d 21275 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21276 line_ptr += 2;
6f77053d
PA
21277
21278 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21279 }
c906108c 21280 break;
9aa1fe7e 21281 default:
a738430d
MK
21282 {
21283 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21284 int i;
a738430d 21285
debd256d 21286 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21287 {
21288 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21289 line_ptr += bytes_read;
21290 }
21291 }
c906108c
SS
21292 }
21293 }
d9b3de22
DE
21294
21295 if (!end_sequence)
21296 dwarf2_debug_line_missing_end_sequence_complaint ();
21297
21298 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21299 in which case we still finish recording the last line). */
6f77053d 21300 state_machine.record_line (true);
c906108c 21301 }
f3f5162e
DE
21302}
21303
21304/* Decode the Line Number Program (LNP) for the given line_header
21305 structure and CU. The actual information extracted and the type
21306 of structures created from the LNP depends on the value of PST.
21307
21308 1. If PST is NULL, then this procedure uses the data from the program
21309 to create all necessary symbol tables, and their linetables.
21310
21311 2. If PST is not NULL, this procedure reads the program to determine
21312 the list of files included by the unit represented by PST, and
21313 builds all the associated partial symbol tables.
21314
21315 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21316 It is used for relative paths in the line table.
21317 NOTE: When processing partial symtabs (pst != NULL),
21318 comp_dir == pst->dirname.
21319
21320 NOTE: It is important that psymtabs have the same file name (via strcmp)
21321 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21322 symtab we don't use it in the name of the psymtabs we create.
21323 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21324 A good testcase for this is mb-inline.exp.
21325
527f3840
JK
21326 LOWPC is the lowest address in CU (or 0 if not known).
21327
21328 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21329 for its PC<->lines mapping information. Otherwise only the filename
21330 table is read in. */
f3f5162e
DE
21331
21332static void
21333dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21334 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21335 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21336{
518817b3 21337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21338 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21339
527f3840
JK
21340 if (decode_mapping)
21341 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21342
21343 if (decode_for_pst_p)
21344 {
21345 int file_index;
21346
21347 /* Now that we're done scanning the Line Header Program, we can
21348 create the psymtab of each included file. */
fff8551c 21349 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21350 if (lh->file_names[file_index].included_p == 1)
21351 {
c89b44cd 21352 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21353 const char *include_name =
c89b44cd
TT
21354 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21355 &name_holder);
c6da4cef 21356 if (include_name != NULL)
aaa75496
JB
21357 dwarf2_create_include_psymtab (include_name, pst, objfile);
21358 }
21359 }
cb1df416
DJ
21360 else
21361 {
21362 /* Make sure a symtab is created for every file, even files
21363 which contain only variables (i.e. no code with associated
21364 line numbers). */
c24bdb02
KS
21365 buildsym_compunit *builder = cu->get_builder ();
21366 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21367 int i;
cb1df416 21368
fff8551c 21369 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21370 {
8c43009f 21371 file_entry &fe = lh->file_names[i];
9a619af0 21372
804d2729 21373 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21374
c24bdb02 21375 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21376 {
c24bdb02 21377 builder->get_current_subfile ()->symtab
804d2729 21378 = allocate_symtab (cust,
c24bdb02 21379 builder->get_current_subfile ()->name);
43f3e411 21380 }
c24bdb02 21381 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21382 }
21383 }
c906108c
SS
21384}
21385
21386/* Start a subfile for DWARF. FILENAME is the name of the file and
21387 DIRNAME the name of the source directory which contains FILENAME
4d663531 21388 or NULL if not known.
c906108c
SS
21389 This routine tries to keep line numbers from identical absolute and
21390 relative file names in a common subfile.
21391
21392 Using the `list' example from the GDB testsuite, which resides in
21393 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21394 of /srcdir/list0.c yields the following debugging information for list0.c:
21395
c5aa993b 21396 DW_AT_name: /srcdir/list0.c
4d663531 21397 DW_AT_comp_dir: /compdir
357e46e7 21398 files.files[0].name: list0.h
c5aa993b 21399 files.files[0].dir: /srcdir
357e46e7 21400 files.files[1].name: list0.c
c5aa993b 21401 files.files[1].dir: /srcdir
c906108c
SS
21402
21403 The line number information for list0.c has to end up in a single
4f1520fb
FR
21404 subfile, so that `break /srcdir/list0.c:1' works as expected.
21405 start_subfile will ensure that this happens provided that we pass the
21406 concatenation of files.files[1].dir and files.files[1].name as the
21407 subfile's name. */
c906108c
SS
21408
21409static void
804d2729
TT
21410dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21411 const char *dirname)
c906108c 21412{
d521ce57 21413 char *copy = NULL;
4f1520fb 21414
4d663531 21415 /* In order not to lose the line information directory,
4f1520fb
FR
21416 we concatenate it to the filename when it makes sense.
21417 Note that the Dwarf3 standard says (speaking of filenames in line
21418 information): ``The directory index is ignored for file names
21419 that represent full path names''. Thus ignoring dirname in the
21420 `else' branch below isn't an issue. */
c906108c 21421
d5166ae1 21422 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21423 {
21424 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21425 filename = copy;
21426 }
c906108c 21427
c24bdb02 21428 cu->get_builder ()->start_subfile (filename);
4f1520fb 21429
d521ce57
TT
21430 if (copy != NULL)
21431 xfree (copy);
c906108c
SS
21432}
21433
804d2729
TT
21434/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21435 buildsym_compunit constructor. */
f4dc4d17 21436
c24bdb02
KS
21437struct compunit_symtab *
21438dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21439 CORE_ADDR low_pc)
f4dc4d17 21440{
c24bdb02 21441 gdb_assert (m_builder == nullptr);
43f3e411 21442
c24bdb02
KS
21443 m_builder.reset (new struct buildsym_compunit
21444 (per_cu->dwarf2_per_objfile->objfile,
21445 name, comp_dir, language, low_pc));
93b8bea4 21446
c24bdb02 21447 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21448
c24bdb02
KS
21449 get_builder ()->record_debugformat ("DWARF 2");
21450 get_builder ()->record_producer (producer);
f4dc4d17 21451
c24bdb02 21452 processing_has_namespace_info = false;
43f3e411 21453
c24bdb02 21454 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21455}
21456
4c2df51b
DJ
21457static void
21458var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21459 struct dwarf2_cu *cu)
4c2df51b 21460{
518817b3 21461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21462 struct comp_unit_head *cu_header = &cu->header;
21463
4c2df51b
DJ
21464 /* NOTE drow/2003-01-30: There used to be a comment and some special
21465 code here to turn a symbol with DW_AT_external and a
21466 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21467 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21468 with some versions of binutils) where shared libraries could have
21469 relocations against symbols in their debug information - the
21470 minimal symbol would have the right address, but the debug info
21471 would not. It's no longer necessary, because we will explicitly
21472 apply relocations when we read in the debug information now. */
21473
21474 /* A DW_AT_location attribute with no contents indicates that a
21475 variable has been optimized away. */
21476 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21477 {
f1e6e072 21478 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21479 return;
21480 }
21481
21482 /* Handle one degenerate form of location expression specially, to
21483 preserve GDB's previous behavior when section offsets are
336d760d
AT
21484 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21485 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21486
21487 if (attr_form_is_block (attr)
3019eac3
DE
21488 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21489 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21490 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21491 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21492 && (DW_BLOCK (attr)->size
21493 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21494 {
891d2f0b 21495 unsigned int dummy;
4c2df51b 21496
3019eac3
DE
21497 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21498 SYMBOL_VALUE_ADDRESS (sym) =
21499 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21500 else
21501 SYMBOL_VALUE_ADDRESS (sym) =
21502 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21503 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21504 fixup_symbol_section (sym, objfile);
21505 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21506 SYMBOL_SECTION (sym));
4c2df51b
DJ
21507 return;
21508 }
21509
21510 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21511 expression evaluator, and use LOC_COMPUTED only when necessary
21512 (i.e. when the value of a register or memory location is
21513 referenced, or a thread-local block, etc.). Then again, it might
21514 not be worthwhile. I'm assuming that it isn't unless performance
21515 or memory numbers show me otherwise. */
21516
f1e6e072 21517 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21518
f1e6e072 21519 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21520 cu->has_loclist = true;
4c2df51b
DJ
21521}
21522
c906108c
SS
21523/* Given a pointer to a DWARF information entry, figure out if we need
21524 to make a symbol table entry for it, and if so, create a new entry
21525 and return a pointer to it.
21526 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21527 used the passed type.
21528 If SPACE is not NULL, use it to hold the new symbol. If it is
21529 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21530
21531static struct symbol *
5e2db402
TT
21532new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21533 struct symbol *space)
c906108c 21534{
518817b3
SM
21535 struct dwarf2_per_objfile *dwarf2_per_objfile
21536 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21537 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21538 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21539 struct symbol *sym = NULL;
15d034d0 21540 const char *name;
c906108c
SS
21541 struct attribute *attr = NULL;
21542 struct attribute *attr2 = NULL;
e142c38c 21543 CORE_ADDR baseaddr;
e37fd15a
SW
21544 struct pending **list_to_add = NULL;
21545
edb3359d 21546 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21547
21548 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21549
94af9270 21550 name = dwarf2_name (die, cu);
c906108c
SS
21551 if (name)
21552 {
94af9270 21553 const char *linkagename;
34eaf542 21554 int suppress_add = 0;
94af9270 21555
34eaf542
TT
21556 if (space)
21557 sym = space;
21558 else
e623cf5d 21559 sym = allocate_symbol (objfile);
c906108c 21560 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21561
21562 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21563 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21564 linkagename = dwarf2_physname (name, die, cu);
21565 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21566
f55ee35c
JK
21567 /* Fortran does not have mangling standard and the mangling does differ
21568 between gfortran, iFort etc. */
21569 if (cu->language == language_fortran
b250c185 21570 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21571 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21572 dwarf2_full_name (name, die, cu),
29df156d 21573 NULL);
f55ee35c 21574
c906108c 21575 /* Default assumptions.
c5aa993b 21576 Use the passed type or decode it from the die. */
176620f1 21577 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21578 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21579 if (type != NULL)
21580 SYMBOL_TYPE (sym) = type;
21581 else
e7c27a73 21582 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21583 attr = dwarf2_attr (die,
21584 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21585 cu);
c906108c
SS
21586 if (attr)
21587 {
21588 SYMBOL_LINE (sym) = DW_UNSND (attr);
21589 }
cb1df416 21590
edb3359d
DJ
21591 attr = dwarf2_attr (die,
21592 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21593 cu);
cb1df416
DJ
21594 if (attr)
21595 {
ecfb656c 21596 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21597 struct file_entry *fe;
9a619af0 21598
ecfb656c
PA
21599 if (cu->line_header != NULL)
21600 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21601 else
21602 fe = NULL;
21603
21604 if (fe == NULL)
b98664d3 21605 complaint (_("file index out of range"));
8c43009f
PA
21606 else
21607 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21608 }
21609
c906108c
SS
21610 switch (die->tag)
21611 {
21612 case DW_TAG_label:
e142c38c 21613 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21614 if (attr)
3e29f34a
MR
21615 {
21616 CORE_ADDR addr;
21617
21618 addr = attr_value_as_address (attr);
21619 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21620 SYMBOL_VALUE_ADDRESS (sym) = addr;
21621 }
0f5238ed
TT
21622 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21623 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21624 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21625 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21626 break;
21627 case DW_TAG_subprogram:
21628 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21629 finish_block. */
f1e6e072 21630 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21631 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21632 if ((attr2 && (DW_UNSND (attr2) != 0))
21633 || cu->language == language_ada)
c906108c 21634 {
2cfa0c8d
JB
21635 /* Subprograms marked external are stored as a global symbol.
21636 Ada subprograms, whether marked external or not, are always
21637 stored as a global symbol, because we want to be able to
21638 access them globally. For instance, we want to be able
21639 to break on a nested subprogram without having to
21640 specify the context. */
c24bdb02 21641 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21642 }
21643 else
21644 {
e37fd15a 21645 list_to_add = cu->list_in_scope;
c906108c
SS
21646 }
21647 break;
edb3359d
DJ
21648 case DW_TAG_inlined_subroutine:
21649 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21650 finish_block. */
f1e6e072 21651 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21652 SYMBOL_INLINED (sym) = 1;
481860b3 21653 list_to_add = cu->list_in_scope;
edb3359d 21654 break;
34eaf542
TT
21655 case DW_TAG_template_value_param:
21656 suppress_add = 1;
21657 /* Fall through. */
72929c62 21658 case DW_TAG_constant:
c906108c 21659 case DW_TAG_variable:
254e6b9e 21660 case DW_TAG_member:
0963b4bd
MS
21661 /* Compilation with minimal debug info may result in
21662 variables with missing type entries. Change the
21663 misleading `void' type to something sensible. */
c906108c 21664 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21665 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21666
e142c38c 21667 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21668 /* In the case of DW_TAG_member, we should only be called for
21669 static const members. */
21670 if (die->tag == DW_TAG_member)
21671 {
3863f96c
DE
21672 /* dwarf2_add_field uses die_is_declaration,
21673 so we do the same. */
254e6b9e
DE
21674 gdb_assert (die_is_declaration (die, cu));
21675 gdb_assert (attr);
21676 }
c906108c
SS
21677 if (attr)
21678 {
e7c27a73 21679 dwarf2_const_value (attr, sym, cu);
e142c38c 21680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21681 if (!suppress_add)
34eaf542
TT
21682 {
21683 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21684 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21685 else
e37fd15a 21686 list_to_add = cu->list_in_scope;
34eaf542 21687 }
c906108c
SS
21688 break;
21689 }
e142c38c 21690 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21691 if (attr)
21692 {
e7c27a73 21693 var_decode_location (attr, sym, cu);
e142c38c 21694 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21695
21696 /* Fortran explicitly imports any global symbols to the local
21697 scope by DW_TAG_common_block. */
21698 if (cu->language == language_fortran && die->parent
21699 && die->parent->tag == DW_TAG_common_block)
21700 attr2 = NULL;
21701
caac4577
JG
21702 if (SYMBOL_CLASS (sym) == LOC_STATIC
21703 && SYMBOL_VALUE_ADDRESS (sym) == 0
21704 && !dwarf2_per_objfile->has_section_at_zero)
21705 {
21706 /* When a static variable is eliminated by the linker,
21707 the corresponding debug information is not stripped
21708 out, but the variable address is set to null;
21709 do not add such variables into symbol table. */
21710 }
21711 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21712 {
f55ee35c
JK
21713 /* Workaround gfortran PR debug/40040 - it uses
21714 DW_AT_location for variables in -fPIC libraries which may
21715 get overriden by other libraries/executable and get
21716 a different address. Resolve it by the minimal symbol
21717 which may come from inferior's executable using copy
21718 relocation. Make this workaround only for gfortran as for
21719 other compilers GDB cannot guess the minimal symbol
21720 Fortran mangling kind. */
21721 if (cu->language == language_fortran && die->parent
21722 && die->parent->tag == DW_TAG_module
21723 && cu->producer
28586665 21724 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21725 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21726
1c809c68
TT
21727 /* A variable with DW_AT_external is never static,
21728 but it may be block-scoped. */
804d2729 21729 list_to_add
c24bdb02
KS
21730 = ((cu->list_in_scope
21731 == cu->get_builder ()->get_file_symbols ())
21732 ? cu->get_builder ()->get_global_symbols ()
804d2729 21733 : cu->list_in_scope);
1c809c68 21734 }
c906108c 21735 else
e37fd15a 21736 list_to_add = cu->list_in_scope;
c906108c
SS
21737 }
21738 else
21739 {
21740 /* We do not know the address of this symbol.
c5aa993b
JM
21741 If it is an external symbol and we have type information
21742 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21743 The address of the variable will then be determined from
21744 the minimal symbol table whenever the variable is
21745 referenced. */
e142c38c 21746 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21747
21748 /* Fortran explicitly imports any global symbols to the local
21749 scope by DW_TAG_common_block. */
21750 if (cu->language == language_fortran && die->parent
21751 && die->parent->tag == DW_TAG_common_block)
21752 {
21753 /* SYMBOL_CLASS doesn't matter here because
21754 read_common_block is going to reset it. */
21755 if (!suppress_add)
21756 list_to_add = cu->list_in_scope;
21757 }
21758 else if (attr2 && (DW_UNSND (attr2) != 0)
21759 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21760 {
0fe7935b
DJ
21761 /* A variable with DW_AT_external is never static, but it
21762 may be block-scoped. */
804d2729 21763 list_to_add
c24bdb02
KS
21764 = ((cu->list_in_scope
21765 == cu->get_builder ()->get_file_symbols ())
21766 ? cu->get_builder ()->get_global_symbols ()
804d2729 21767 : cu->list_in_scope);
0fe7935b 21768
f1e6e072 21769 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21770 }
442ddf59
JK
21771 else if (!die_is_declaration (die, cu))
21772 {
21773 /* Use the default LOC_OPTIMIZED_OUT class. */
21774 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21775 if (!suppress_add)
21776 list_to_add = cu->list_in_scope;
442ddf59 21777 }
c906108c
SS
21778 }
21779 break;
21780 case DW_TAG_formal_parameter:
a60f3166
TT
21781 {
21782 /* If we are inside a function, mark this as an argument. If
21783 not, we might be looking at an argument to an inlined function
21784 when we do not have enough information to show inlined frames;
21785 pretend it's a local variable in that case so that the user can
21786 still see it. */
804d2729 21787 struct context_stack *curr
c24bdb02 21788 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21789 if (curr != nullptr && curr->name != nullptr)
21790 SYMBOL_IS_ARGUMENT (sym) = 1;
21791 attr = dwarf2_attr (die, DW_AT_location, cu);
21792 if (attr)
21793 {
21794 var_decode_location (attr, sym, cu);
21795 }
21796 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21797 if (attr)
21798 {
21799 dwarf2_const_value (attr, sym, cu);
21800 }
f346a30d 21801
a60f3166
TT
21802 list_to_add = cu->list_in_scope;
21803 }
c906108c
SS
21804 break;
21805 case DW_TAG_unspecified_parameters:
21806 /* From varargs functions; gdb doesn't seem to have any
21807 interest in this information, so just ignore it for now.
21808 (FIXME?) */
21809 break;
34eaf542
TT
21810 case DW_TAG_template_type_param:
21811 suppress_add = 1;
21812 /* Fall through. */
c906108c 21813 case DW_TAG_class_type:
680b30c7 21814 case DW_TAG_interface_type:
c906108c
SS
21815 case DW_TAG_structure_type:
21816 case DW_TAG_union_type:
72019c9c 21817 case DW_TAG_set_type:
c906108c 21818 case DW_TAG_enumeration_type:
f1e6e072 21819 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21820 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21821
63d06c5c 21822 {
9c37b5ae 21823 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21824 really ever be static objects: otherwise, if you try
21825 to, say, break of a class's method and you're in a file
21826 which doesn't mention that class, it won't work unless
21827 the check for all static symbols in lookup_symbol_aux
21828 saves you. See the OtherFileClass tests in
21829 gdb.c++/namespace.exp. */
21830
e37fd15a 21831 if (!suppress_add)
34eaf542 21832 {
c24bdb02 21833 buildsym_compunit *builder = cu->get_builder ();
804d2729 21834 list_to_add
c24bdb02 21835 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21836 && cu->language == language_cplus
c24bdb02 21837 ? builder->get_global_symbols ()
804d2729 21838 : cu->list_in_scope);
63d06c5c 21839
64382290 21840 /* The semantics of C++ state that "struct foo {
9c37b5ae 21841 ... }" also defines a typedef for "foo". */
64382290 21842 if (cu->language == language_cplus
45280282 21843 || cu->language == language_ada
c44af4eb
TT
21844 || cu->language == language_d
21845 || cu->language == language_rust)
64382290
TT
21846 {
21847 /* The symbol's name is already allocated along
21848 with this objfile, so we don't need to
21849 duplicate it for the type. */
21850 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21851 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21852 }
63d06c5c
DC
21853 }
21854 }
c906108c
SS
21855 break;
21856 case DW_TAG_typedef:
f1e6e072 21857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21858 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21859 list_to_add = cu->list_in_scope;
63d06c5c 21860 break;
c906108c 21861 case DW_TAG_base_type:
a02abb62 21862 case DW_TAG_subrange_type:
f1e6e072 21863 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21864 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21865 list_to_add = cu->list_in_scope;
c906108c
SS
21866 break;
21867 case DW_TAG_enumerator:
e142c38c 21868 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21869 if (attr)
21870 {
e7c27a73 21871 dwarf2_const_value (attr, sym, cu);
c906108c 21872 }
63d06c5c
DC
21873 {
21874 /* NOTE: carlton/2003-11-10: See comment above in the
21875 DW_TAG_class_type, etc. block. */
21876
804d2729 21877 list_to_add
c24bdb02 21878 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21879 && cu->language == language_cplus
c24bdb02 21880 ? cu->get_builder ()->get_global_symbols ()
804d2729 21881 : cu->list_in_scope);
63d06c5c 21882 }
c906108c 21883 break;
74921315 21884 case DW_TAG_imported_declaration:
5c4e30ca 21885 case DW_TAG_namespace:
f1e6e072 21886 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21887 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21888 break;
530e8392
KB
21889 case DW_TAG_module:
21890 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21891 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21892 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21893 break;
4357ac6c 21894 case DW_TAG_common_block:
f1e6e072 21895 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21896 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21897 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21898 break;
c906108c
SS
21899 default:
21900 /* Not a tag we recognize. Hopefully we aren't processing
21901 trash data, but since we must specifically ignore things
21902 we don't recognize, there is nothing else we should do at
0963b4bd 21903 this point. */
b98664d3 21904 complaint (_("unsupported tag: '%s'"),
4d3c2250 21905 dwarf_tag_name (die->tag));
c906108c
SS
21906 break;
21907 }
df8a16a1 21908
e37fd15a
SW
21909 if (suppress_add)
21910 {
21911 sym->hash_next = objfile->template_symbols;
21912 objfile->template_symbols = sym;
21913 list_to_add = NULL;
21914 }
21915
21916 if (list_to_add != NULL)
d3cb6808 21917 add_symbol_to_list (sym, list_to_add);
e37fd15a 21918
df8a16a1
DJ
21919 /* For the benefit of old versions of GCC, check for anonymous
21920 namespaces based on the demangled name. */
4d4ec4e5 21921 if (!cu->processing_has_namespace_info
94af9270 21922 && cu->language == language_cplus)
c24bdb02 21923 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21924 }
21925 return (sym);
21926}
21927
98bfdba5
PA
21928/* Given an attr with a DW_FORM_dataN value in host byte order,
21929 zero-extend it as appropriate for the symbol's type. The DWARF
21930 standard (v4) is not entirely clear about the meaning of using
21931 DW_FORM_dataN for a constant with a signed type, where the type is
21932 wider than the data. The conclusion of a discussion on the DWARF
21933 list was that this is unspecified. We choose to always zero-extend
21934 because that is the interpretation long in use by GCC. */
c906108c 21935
98bfdba5 21936static gdb_byte *
ff39bb5e 21937dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21938 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21939{
518817b3 21940 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21941 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21942 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21943 LONGEST l = DW_UNSND (attr);
21944
21945 if (bits < sizeof (*value) * 8)
21946 {
21947 l &= ((LONGEST) 1 << bits) - 1;
21948 *value = l;
21949 }
21950 else if (bits == sizeof (*value) * 8)
21951 *value = l;
21952 else
21953 {
224c3ddb 21954 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21955 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21956 return bytes;
21957 }
21958
21959 return NULL;
21960}
21961
21962/* Read a constant value from an attribute. Either set *VALUE, or if
21963 the value does not fit in *VALUE, set *BYTES - either already
21964 allocated on the objfile obstack, or newly allocated on OBSTACK,
21965 or, set *BATON, if we translated the constant to a location
21966 expression. */
21967
21968static void
ff39bb5e 21969dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21970 const char *name, struct obstack *obstack,
21971 struct dwarf2_cu *cu,
d521ce57 21972 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21973 struct dwarf2_locexpr_baton **baton)
21974{
518817b3 21975 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21976 struct comp_unit_head *cu_header = &cu->header;
c906108c 21977 struct dwarf_block *blk;
98bfdba5
PA
21978 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21979 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21980
21981 *value = 0;
21982 *bytes = NULL;
21983 *baton = NULL;
c906108c
SS
21984
21985 switch (attr->form)
21986 {
21987 case DW_FORM_addr:
336d760d 21988 case DW_FORM_addrx:
3019eac3 21989 case DW_FORM_GNU_addr_index:
ac56253d 21990 {
ac56253d
TT
21991 gdb_byte *data;
21992
98bfdba5
PA
21993 if (TYPE_LENGTH (type) != cu_header->addr_size)
21994 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21995 cu_header->addr_size,
98bfdba5 21996 TYPE_LENGTH (type));
ac56253d
TT
21997 /* Symbols of this form are reasonably rare, so we just
21998 piggyback on the existing location code rather than writing
21999 a new implementation of symbol_computed_ops. */
8d749320 22000 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22001 (*baton)->per_cu = cu->per_cu;
22002 gdb_assert ((*baton)->per_cu);
ac56253d 22003
98bfdba5 22004 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22005 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22006 (*baton)->data = data;
ac56253d
TT
22007
22008 data[0] = DW_OP_addr;
22009 store_unsigned_integer (&data[1], cu_header->addr_size,
22010 byte_order, DW_ADDR (attr));
22011 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22012 }
c906108c 22013 break;
4ac36638 22014 case DW_FORM_string:
93b5768b 22015 case DW_FORM_strp:
cf532bd1 22016 case DW_FORM_strx:
3019eac3 22017 case DW_FORM_GNU_str_index:
36586728 22018 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22019 /* DW_STRING is already allocated on the objfile obstack, point
22020 directly to it. */
d521ce57 22021 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22022 break;
c906108c
SS
22023 case DW_FORM_block1:
22024 case DW_FORM_block2:
22025 case DW_FORM_block4:
22026 case DW_FORM_block:
2dc7f7b3 22027 case DW_FORM_exprloc:
0224619f 22028 case DW_FORM_data16:
c906108c 22029 blk = DW_BLOCK (attr);
98bfdba5
PA
22030 if (TYPE_LENGTH (type) != blk->size)
22031 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22032 TYPE_LENGTH (type));
22033 *bytes = blk->data;
c906108c 22034 break;
2df3850c
JM
22035
22036 /* The DW_AT_const_value attributes are supposed to carry the
22037 symbol's value "represented as it would be on the target
22038 architecture." By the time we get here, it's already been
22039 converted to host endianness, so we just need to sign- or
22040 zero-extend it as appropriate. */
22041 case DW_FORM_data1:
3aef2284 22042 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22043 break;
c906108c 22044 case DW_FORM_data2:
3aef2284 22045 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22046 break;
c906108c 22047 case DW_FORM_data4:
3aef2284 22048 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22049 break;
c906108c 22050 case DW_FORM_data8:
3aef2284 22051 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22052 break;
22053
c906108c 22054 case DW_FORM_sdata:
663c44ac 22055 case DW_FORM_implicit_const:
98bfdba5 22056 *value = DW_SND (attr);
2df3850c
JM
22057 break;
22058
c906108c 22059 case DW_FORM_udata:
98bfdba5 22060 *value = DW_UNSND (attr);
c906108c 22061 break;
2df3850c 22062
c906108c 22063 default:
b98664d3 22064 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22065 dwarf_form_name (attr->form));
98bfdba5 22066 *value = 0;
c906108c
SS
22067 break;
22068 }
22069}
22070
2df3850c 22071
98bfdba5
PA
22072/* Copy constant value from an attribute to a symbol. */
22073
2df3850c 22074static void
ff39bb5e 22075dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22076 struct dwarf2_cu *cu)
2df3850c 22077{
518817b3 22078 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22079 LONGEST value;
d521ce57 22080 const gdb_byte *bytes;
98bfdba5 22081 struct dwarf2_locexpr_baton *baton;
2df3850c 22082
98bfdba5
PA
22083 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22084 SYMBOL_PRINT_NAME (sym),
22085 &objfile->objfile_obstack, cu,
22086 &value, &bytes, &baton);
2df3850c 22087
98bfdba5
PA
22088 if (baton != NULL)
22089 {
98bfdba5 22090 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22091 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22092 }
22093 else if (bytes != NULL)
22094 {
22095 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22096 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22097 }
22098 else
22099 {
22100 SYMBOL_VALUE (sym) = value;
f1e6e072 22101 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22102 }
2df3850c
JM
22103}
22104
c906108c
SS
22105/* Return the type of the die in question using its DW_AT_type attribute. */
22106
22107static struct type *
e7c27a73 22108die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22109{
c906108c 22110 struct attribute *type_attr;
c906108c 22111
e142c38c 22112 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22113 if (!type_attr)
22114 {
518817b3 22115 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22116 /* A missing DW_AT_type represents a void type. */
518817b3 22117 return objfile_type (objfile)->builtin_void;
c906108c 22118 }
348e048f 22119
673bfd45 22120 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22121}
22122
b4ba55a1
JB
22123/* True iff CU's producer generates GNAT Ada auxiliary information
22124 that allows to find parallel types through that information instead
22125 of having to do expensive parallel lookups by type name. */
22126
22127static int
22128need_gnat_info (struct dwarf2_cu *cu)
22129{
de4cb04a
JB
22130 /* Assume that the Ada compiler was GNAT, which always produces
22131 the auxiliary information. */
22132 return (cu->language == language_ada);
b4ba55a1
JB
22133}
22134
b4ba55a1
JB
22135/* Return the auxiliary type of the die in question using its
22136 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22137 attribute is not present. */
22138
22139static struct type *
22140die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22141{
b4ba55a1 22142 struct attribute *type_attr;
b4ba55a1
JB
22143
22144 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22145 if (!type_attr)
22146 return NULL;
22147
673bfd45 22148 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22149}
22150
22151/* If DIE has a descriptive_type attribute, then set the TYPE's
22152 descriptive type accordingly. */
22153
22154static void
22155set_descriptive_type (struct type *type, struct die_info *die,
22156 struct dwarf2_cu *cu)
22157{
22158 struct type *descriptive_type = die_descriptive_type (die, cu);
22159
22160 if (descriptive_type)
22161 {
22162 ALLOCATE_GNAT_AUX_TYPE (type);
22163 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22164 }
22165}
22166
c906108c
SS
22167/* Return the containing type of the die in question using its
22168 DW_AT_containing_type attribute. */
22169
22170static struct type *
e7c27a73 22171die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22172{
c906108c 22173 struct attribute *type_attr;
518817b3 22174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22175
e142c38c 22176 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22177 if (!type_attr)
22178 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22179 "[in module %s]"), objfile_name (objfile));
33ac96f0 22180
673bfd45 22181 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22182}
22183
ac9ec31b
DE
22184/* Return an error marker type to use for the ill formed type in DIE/CU. */
22185
22186static struct type *
22187build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22188{
518817b3
SM
22189 struct dwarf2_per_objfile *dwarf2_per_objfile
22190 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22191 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22192 char *saved;
ac9ec31b 22193
528e1572
SM
22194 std::string message
22195 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22196 objfile_name (objfile),
22197 sect_offset_str (cu->header.sect_off),
22198 sect_offset_str (die->sect_off));
efba19b0 22199 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22200
19f392bc 22201 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22202}
22203
673bfd45 22204/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22205 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22206 DW_AT_containing_type.
673bfd45
DE
22207 If there is no type substitute an error marker. */
22208
c906108c 22209static struct type *
ff39bb5e 22210lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22211 struct dwarf2_cu *cu)
c906108c 22212{
518817b3
SM
22213 struct dwarf2_per_objfile *dwarf2_per_objfile
22214 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22215 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22216 struct type *this_type;
22217
ac9ec31b
DE
22218 gdb_assert (attr->name == DW_AT_type
22219 || attr->name == DW_AT_GNAT_descriptive_type
22220 || attr->name == DW_AT_containing_type);
22221
673bfd45
DE
22222 /* First see if we have it cached. */
22223
36586728
TT
22224 if (attr->form == DW_FORM_GNU_ref_alt)
22225 {
22226 struct dwarf2_per_cu_data *per_cu;
9c541725 22227 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22228
ed2dc618
SM
22229 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22230 dwarf2_per_objfile);
9c541725 22231 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22232 }
7771576e 22233 else if (attr_form_is_ref (attr))
673bfd45 22234 {
9c541725 22235 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22236
9c541725 22237 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22238 }
55f1336d 22239 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22240 {
ac9ec31b 22241 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22242
ac9ec31b 22243 return get_signatured_type (die, signature, cu);
673bfd45
DE
22244 }
22245 else
22246 {
b98664d3 22247 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22248 " at %s [in module %s]"),
22249 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22250 objfile_name (objfile));
ac9ec31b 22251 return build_error_marker_type (cu, die);
673bfd45
DE
22252 }
22253
22254 /* If not cached we need to read it in. */
22255
22256 if (this_type == NULL)
22257 {
ac9ec31b 22258 struct die_info *type_die = NULL;
673bfd45
DE
22259 struct dwarf2_cu *type_cu = cu;
22260
7771576e 22261 if (attr_form_is_ref (attr))
ac9ec31b
DE
22262 type_die = follow_die_ref (die, attr, &type_cu);
22263 if (type_die == NULL)
22264 return build_error_marker_type (cu, die);
22265 /* If we find the type now, it's probably because the type came
3019eac3
DE
22266 from an inter-CU reference and the type's CU got expanded before
22267 ours. */
ac9ec31b 22268 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22269 }
22270
22271 /* If we still don't have a type use an error marker. */
22272
22273 if (this_type == NULL)
ac9ec31b 22274 return build_error_marker_type (cu, die);
673bfd45 22275
f792889a 22276 return this_type;
c906108c
SS
22277}
22278
673bfd45
DE
22279/* Return the type in DIE, CU.
22280 Returns NULL for invalid types.
22281
02142a6c 22282 This first does a lookup in die_type_hash,
673bfd45
DE
22283 and only reads the die in if necessary.
22284
22285 NOTE: This can be called when reading in partial or full symbols. */
22286
f792889a 22287static struct type *
e7c27a73 22288read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22289{
f792889a
DJ
22290 struct type *this_type;
22291
22292 this_type = get_die_type (die, cu);
22293 if (this_type)
22294 return this_type;
22295
673bfd45
DE
22296 return read_type_die_1 (die, cu);
22297}
22298
22299/* Read the type in DIE, CU.
22300 Returns NULL for invalid types. */
22301
22302static struct type *
22303read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22304{
22305 struct type *this_type = NULL;
22306
c906108c
SS
22307 switch (die->tag)
22308 {
22309 case DW_TAG_class_type:
680b30c7 22310 case DW_TAG_interface_type:
c906108c
SS
22311 case DW_TAG_structure_type:
22312 case DW_TAG_union_type:
f792889a 22313 this_type = read_structure_type (die, cu);
c906108c
SS
22314 break;
22315 case DW_TAG_enumeration_type:
f792889a 22316 this_type = read_enumeration_type (die, cu);
c906108c
SS
22317 break;
22318 case DW_TAG_subprogram:
22319 case DW_TAG_subroutine_type:
edb3359d 22320 case DW_TAG_inlined_subroutine:
f792889a 22321 this_type = read_subroutine_type (die, cu);
c906108c
SS
22322 break;
22323 case DW_TAG_array_type:
f792889a 22324 this_type = read_array_type (die, cu);
c906108c 22325 break;
72019c9c 22326 case DW_TAG_set_type:
f792889a 22327 this_type = read_set_type (die, cu);
72019c9c 22328 break;
c906108c 22329 case DW_TAG_pointer_type:
f792889a 22330 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22331 break;
22332 case DW_TAG_ptr_to_member_type:
f792889a 22333 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22334 break;
22335 case DW_TAG_reference_type:
4297a3f0
AV
22336 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22337 break;
22338 case DW_TAG_rvalue_reference_type:
22339 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22340 break;
22341 case DW_TAG_const_type:
f792889a 22342 this_type = read_tag_const_type (die, cu);
c906108c
SS
22343 break;
22344 case DW_TAG_volatile_type:
f792889a 22345 this_type = read_tag_volatile_type (die, cu);
c906108c 22346 break;
06d66ee9
TT
22347 case DW_TAG_restrict_type:
22348 this_type = read_tag_restrict_type (die, cu);
22349 break;
c906108c 22350 case DW_TAG_string_type:
f792889a 22351 this_type = read_tag_string_type (die, cu);
c906108c
SS
22352 break;
22353 case DW_TAG_typedef:
f792889a 22354 this_type = read_typedef (die, cu);
c906108c 22355 break;
a02abb62 22356 case DW_TAG_subrange_type:
f792889a 22357 this_type = read_subrange_type (die, cu);
a02abb62 22358 break;
c906108c 22359 case DW_TAG_base_type:
f792889a 22360 this_type = read_base_type (die, cu);
c906108c 22361 break;
81a17f79 22362 case DW_TAG_unspecified_type:
f792889a 22363 this_type = read_unspecified_type (die, cu);
81a17f79 22364 break;
0114d602
DJ
22365 case DW_TAG_namespace:
22366 this_type = read_namespace_type (die, cu);
22367 break;
f55ee35c
JK
22368 case DW_TAG_module:
22369 this_type = read_module_type (die, cu);
22370 break;
a2c2acaf
MW
22371 case DW_TAG_atomic_type:
22372 this_type = read_tag_atomic_type (die, cu);
22373 break;
c906108c 22374 default:
b98664d3 22375 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22376 dwarf_tag_name (die->tag));
c906108c
SS
22377 break;
22378 }
63d06c5c 22379
f792889a 22380 return this_type;
63d06c5c
DC
22381}
22382
abc72ce4
DE
22383/* See if we can figure out if the class lives in a namespace. We do
22384 this by looking for a member function; its demangled name will
22385 contain namespace info, if there is any.
22386 Return the computed name or NULL.
22387 Space for the result is allocated on the objfile's obstack.
22388 This is the full-die version of guess_partial_die_structure_name.
22389 In this case we know DIE has no useful parent. */
22390
22391static char *
22392guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22393{
22394 struct die_info *spec_die;
22395 struct dwarf2_cu *spec_cu;
22396 struct die_info *child;
518817b3 22397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22398
22399 spec_cu = cu;
22400 spec_die = die_specification (die, &spec_cu);
22401 if (spec_die != NULL)
22402 {
22403 die = spec_die;
22404 cu = spec_cu;
22405 }
22406
22407 for (child = die->child;
22408 child != NULL;
22409 child = child->sibling)
22410 {
22411 if (child->tag == DW_TAG_subprogram)
22412 {
73b9be8b 22413 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22414
7d45c7c3 22415 if (linkage_name != NULL)
abc72ce4
DE
22416 {
22417 char *actual_name
22418 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22419 linkage_name);
abc72ce4
DE
22420 char *name = NULL;
22421
22422 if (actual_name != NULL)
22423 {
15d034d0 22424 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22425
22426 if (die_name != NULL
22427 && strcmp (die_name, actual_name) != 0)
22428 {
22429 /* Strip off the class name from the full name.
22430 We want the prefix. */
22431 int die_name_len = strlen (die_name);
22432 int actual_name_len = strlen (actual_name);
22433
22434 /* Test for '::' as a sanity check. */
22435 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22436 && actual_name[actual_name_len
22437 - die_name_len - 1] == ':')
0cf9feb9 22438 name = obstack_strndup (
e3b94546 22439 &objfile->per_bfd->storage_obstack,
224c3ddb 22440 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22441 }
22442 }
22443 xfree (actual_name);
22444 return name;
22445 }
22446 }
22447 }
22448
22449 return NULL;
22450}
22451
96408a79
SA
22452/* GCC might emit a nameless typedef that has a linkage name. Determine the
22453 prefix part in such case. See
22454 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22455
a121b7c1 22456static const char *
96408a79
SA
22457anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22458{
22459 struct attribute *attr;
e6a959d6 22460 const char *base;
96408a79
SA
22461
22462 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22463 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22464 return NULL;
22465
7d45c7c3 22466 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22467 return NULL;
22468
73b9be8b 22469 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22470 if (attr == NULL || DW_STRING (attr) == NULL)
22471 return NULL;
22472
22473 /* dwarf2_name had to be already called. */
22474 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22475
22476 /* Strip the base name, keep any leading namespaces/classes. */
22477 base = strrchr (DW_STRING (attr), ':');
22478 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22479 return "";
22480
518817b3 22481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22482 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22483 DW_STRING (attr),
22484 &base[-1] - DW_STRING (attr));
96408a79
SA
22485}
22486
fdde2d81 22487/* Return the name of the namespace/class that DIE is defined within,
0114d602 22488 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22489
0114d602
DJ
22490 For example, if we're within the method foo() in the following
22491 code:
22492
22493 namespace N {
22494 class C {
22495 void foo () {
22496 }
22497 };
22498 }
22499
22500 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22501
0d5cff50 22502static const char *
e142c38c 22503determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22504{
518817b3
SM
22505 struct dwarf2_per_objfile *dwarf2_per_objfile
22506 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22507 struct die_info *parent, *spec_die;
22508 struct dwarf2_cu *spec_cu;
22509 struct type *parent_type;
a121b7c1 22510 const char *retval;
63d06c5c 22511
9c37b5ae 22512 if (cu->language != language_cplus
c44af4eb
TT
22513 && cu->language != language_fortran && cu->language != language_d
22514 && cu->language != language_rust)
0114d602
DJ
22515 return "";
22516
96408a79
SA
22517 retval = anonymous_struct_prefix (die, cu);
22518 if (retval)
22519 return retval;
22520
0114d602
DJ
22521 /* We have to be careful in the presence of DW_AT_specification.
22522 For example, with GCC 3.4, given the code
22523
22524 namespace N {
22525 void foo() {
22526 // Definition of N::foo.
22527 }
22528 }
22529
22530 then we'll have a tree of DIEs like this:
22531
22532 1: DW_TAG_compile_unit
22533 2: DW_TAG_namespace // N
22534 3: DW_TAG_subprogram // declaration of N::foo
22535 4: DW_TAG_subprogram // definition of N::foo
22536 DW_AT_specification // refers to die #3
22537
22538 Thus, when processing die #4, we have to pretend that we're in
22539 the context of its DW_AT_specification, namely the contex of die
22540 #3. */
22541 spec_cu = cu;
22542 spec_die = die_specification (die, &spec_cu);
22543 if (spec_die == NULL)
22544 parent = die->parent;
22545 else
63d06c5c 22546 {
0114d602
DJ
22547 parent = spec_die->parent;
22548 cu = spec_cu;
63d06c5c 22549 }
0114d602
DJ
22550
22551 if (parent == NULL)
22552 return "";
98bfdba5
PA
22553 else if (parent->building_fullname)
22554 {
22555 const char *name;
22556 const char *parent_name;
22557
22558 /* It has been seen on RealView 2.2 built binaries,
22559 DW_TAG_template_type_param types actually _defined_ as
22560 children of the parent class:
22561
22562 enum E {};
22563 template class <class Enum> Class{};
22564 Class<enum E> class_e;
22565
22566 1: DW_TAG_class_type (Class)
22567 2: DW_TAG_enumeration_type (E)
22568 3: DW_TAG_enumerator (enum1:0)
22569 3: DW_TAG_enumerator (enum2:1)
22570 ...
22571 2: DW_TAG_template_type_param
22572 DW_AT_type DW_FORM_ref_udata (E)
22573
22574 Besides being broken debug info, it can put GDB into an
22575 infinite loop. Consider:
22576
22577 When we're building the full name for Class<E>, we'll start
22578 at Class, and go look over its template type parameters,
22579 finding E. We'll then try to build the full name of E, and
22580 reach here. We're now trying to build the full name of E,
22581 and look over the parent DIE for containing scope. In the
22582 broken case, if we followed the parent DIE of E, we'd again
22583 find Class, and once again go look at its template type
22584 arguments, etc., etc. Simply don't consider such parent die
22585 as source-level parent of this die (it can't be, the language
22586 doesn't allow it), and break the loop here. */
22587 name = dwarf2_name (die, cu);
22588 parent_name = dwarf2_name (parent, cu);
b98664d3 22589 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22590 name ? name : "<unknown>",
22591 parent_name ? parent_name : "<unknown>");
22592 return "";
22593 }
63d06c5c 22594 else
0114d602
DJ
22595 switch (parent->tag)
22596 {
63d06c5c 22597 case DW_TAG_namespace:
0114d602 22598 parent_type = read_type_die (parent, cu);
acebe513
UW
22599 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22600 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22601 Work around this problem here. */
22602 if (cu->language == language_cplus
e86ca25f 22603 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22604 return "";
0114d602 22605 /* We give a name to even anonymous namespaces. */
e86ca25f 22606 return TYPE_NAME (parent_type);
63d06c5c 22607 case DW_TAG_class_type:
680b30c7 22608 case DW_TAG_interface_type:
63d06c5c 22609 case DW_TAG_structure_type:
0114d602 22610 case DW_TAG_union_type:
f55ee35c 22611 case DW_TAG_module:
0114d602 22612 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22613 if (TYPE_NAME (parent_type) != NULL)
22614 return TYPE_NAME (parent_type);
0114d602
DJ
22615 else
22616 /* An anonymous structure is only allowed non-static data
22617 members; no typedefs, no member functions, et cetera.
22618 So it does not need a prefix. */
22619 return "";
abc72ce4 22620 case DW_TAG_compile_unit:
95554aad 22621 case DW_TAG_partial_unit:
abc72ce4
DE
22622 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22623 if (cu->language == language_cplus
fd5866f6 22624 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22625 && die->child != NULL
22626 && (die->tag == DW_TAG_class_type
22627 || die->tag == DW_TAG_structure_type
22628 || die->tag == DW_TAG_union_type))
22629 {
22630 char *name = guess_full_die_structure_name (die, cu);
22631 if (name != NULL)
22632 return name;
22633 }
22634 return "";
3d567982
TT
22635 case DW_TAG_enumeration_type:
22636 parent_type = read_type_die (parent, cu);
22637 if (TYPE_DECLARED_CLASS (parent_type))
22638 {
e86ca25f
TT
22639 if (TYPE_NAME (parent_type) != NULL)
22640 return TYPE_NAME (parent_type);
3d567982
TT
22641 return "";
22642 }
22643 /* Fall through. */
63d06c5c 22644 default:
8176b9b8 22645 return determine_prefix (parent, cu);
63d06c5c 22646 }
63d06c5c
DC
22647}
22648
3e43a32a
MS
22649/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22650 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22651 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22652 an obconcat, otherwise allocate storage for the result. The CU argument is
22653 used to determine the language and hence, the appropriate separator. */
987504bb 22654
f55ee35c 22655#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22656
22657static char *
f55ee35c
JK
22658typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22659 int physname, struct dwarf2_cu *cu)
63d06c5c 22660{
f55ee35c 22661 const char *lead = "";
5c315b68 22662 const char *sep;
63d06c5c 22663
3e43a32a
MS
22664 if (suffix == NULL || suffix[0] == '\0'
22665 || prefix == NULL || prefix[0] == '\0')
987504bb 22666 sep = "";
45280282
IB
22667 else if (cu->language == language_d)
22668 {
22669 /* For D, the 'main' function could be defined in any module, but it
22670 should never be prefixed. */
22671 if (strcmp (suffix, "D main") == 0)
22672 {
22673 prefix = "";
22674 sep = "";
22675 }
22676 else
22677 sep = ".";
22678 }
f55ee35c
JK
22679 else if (cu->language == language_fortran && physname)
22680 {
22681 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22682 DW_AT_MIPS_linkage_name is preferred and used instead. */
22683
22684 lead = "__";
22685 sep = "_MOD_";
22686 }
987504bb
JJ
22687 else
22688 sep = "::";
63d06c5c 22689
6dd47d34
DE
22690 if (prefix == NULL)
22691 prefix = "";
22692 if (suffix == NULL)
22693 suffix = "";
22694
987504bb
JJ
22695 if (obs == NULL)
22696 {
3e43a32a 22697 char *retval
224c3ddb
SM
22698 = ((char *)
22699 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22700
f55ee35c
JK
22701 strcpy (retval, lead);
22702 strcat (retval, prefix);
6dd47d34
DE
22703 strcat (retval, sep);
22704 strcat (retval, suffix);
63d06c5c
DC
22705 return retval;
22706 }
987504bb
JJ
22707 else
22708 {
22709 /* We have an obstack. */
f55ee35c 22710 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22711 }
63d06c5c
DC
22712}
22713
c906108c
SS
22714/* Return sibling of die, NULL if no sibling. */
22715
f9aca02d 22716static struct die_info *
fba45db2 22717sibling_die (struct die_info *die)
c906108c 22718{
639d11d3 22719 return die->sibling;
c906108c
SS
22720}
22721
71c25dea
TT
22722/* Get name of a die, return NULL if not found. */
22723
15d034d0
TT
22724static const char *
22725dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22726 struct obstack *obstack)
22727{
22728 if (name && cu->language == language_cplus)
22729 {
2f408ecb 22730 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22731
2f408ecb 22732 if (!canon_name.empty ())
71c25dea 22733 {
2f408ecb 22734 if (canon_name != name)
efba19b0 22735 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22736 }
22737 }
22738
22739 return name;
c906108c
SS
22740}
22741
96553a0c
DE
22742/* Get name of a die, return NULL if not found.
22743 Anonymous namespaces are converted to their magic string. */
9219021c 22744
15d034d0 22745static const char *
e142c38c 22746dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22747{
22748 struct attribute *attr;
518817b3 22749 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22750
e142c38c 22751 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22752 if ((!attr || !DW_STRING (attr))
96553a0c 22753 && die->tag != DW_TAG_namespace
53832f31
TT
22754 && die->tag != DW_TAG_class_type
22755 && die->tag != DW_TAG_interface_type
22756 && die->tag != DW_TAG_structure_type
22757 && die->tag != DW_TAG_union_type)
71c25dea
TT
22758 return NULL;
22759
22760 switch (die->tag)
22761 {
22762 case DW_TAG_compile_unit:
95554aad 22763 case DW_TAG_partial_unit:
71c25dea
TT
22764 /* Compilation units have a DW_AT_name that is a filename, not
22765 a source language identifier. */
22766 case DW_TAG_enumeration_type:
22767 case DW_TAG_enumerator:
22768 /* These tags always have simple identifiers already; no need
22769 to canonicalize them. */
22770 return DW_STRING (attr);
907af001 22771
96553a0c
DE
22772 case DW_TAG_namespace:
22773 if (attr != NULL && DW_STRING (attr) != NULL)
22774 return DW_STRING (attr);
22775 return CP_ANONYMOUS_NAMESPACE_STR;
22776
907af001
UW
22777 case DW_TAG_class_type:
22778 case DW_TAG_interface_type:
22779 case DW_TAG_structure_type:
22780 case DW_TAG_union_type:
22781 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22782 structures or unions. These were of the form "._%d" in GCC 4.1,
22783 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22784 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22785 if (attr && DW_STRING (attr)
61012eef
GB
22786 && (startswith (DW_STRING (attr), "._")
22787 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22788 return NULL;
53832f31
TT
22789
22790 /* GCC might emit a nameless typedef that has a linkage name. See
22791 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22792 if (!attr || DW_STRING (attr) == NULL)
22793 {
df5c6c50 22794 char *demangled = NULL;
53832f31 22795
73b9be8b 22796 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22797 if (attr == NULL || DW_STRING (attr) == NULL)
22798 return NULL;
22799
df5c6c50
JK
22800 /* Avoid demangling DW_STRING (attr) the second time on a second
22801 call for the same DIE. */
22802 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22803 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22804
22805 if (demangled)
22806 {
e6a959d6 22807 const char *base;
96408a79 22808
53832f31 22809 /* FIXME: we already did this for the partial symbol... */
34a68019 22810 DW_STRING (attr)
021887d8
TT
22811 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22812 demangled);
53832f31
TT
22813 DW_STRING_IS_CANONICAL (attr) = 1;
22814 xfree (demangled);
96408a79
SA
22815
22816 /* Strip any leading namespaces/classes, keep only the base name.
22817 DW_AT_name for named DIEs does not contain the prefixes. */
22818 base = strrchr (DW_STRING (attr), ':');
22819 if (base && base > DW_STRING (attr) && base[-1] == ':')
22820 return &base[1];
22821 else
22822 return DW_STRING (attr);
53832f31
TT
22823 }
22824 }
907af001
UW
22825 break;
22826
71c25dea 22827 default:
907af001
UW
22828 break;
22829 }
22830
22831 if (!DW_STRING_IS_CANONICAL (attr))
22832 {
22833 DW_STRING (attr)
22834 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22835 &objfile->per_bfd->storage_obstack);
907af001 22836 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22837 }
907af001 22838 return DW_STRING (attr);
9219021c
DC
22839}
22840
22841/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22842 is none. *EXT_CU is the CU containing DIE on input, and the CU
22843 containing the return value on output. */
9219021c
DC
22844
22845static struct die_info *
f2f0e013 22846dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22847{
22848 struct attribute *attr;
9219021c 22849
f2f0e013 22850 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22851 if (attr == NULL)
22852 return NULL;
22853
f2f0e013 22854 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22855}
22856
fa9c3fa0
TT
22857/* A convenience function that returns an "unknown" DWARF name,
22858 including the value of V. STR is the name of the entity being
22859 printed, e.g., "TAG". */
22860
22861static const char *
22862dwarf_unknown (const char *str, unsigned v)
22863{
22864 char *cell = get_print_cell ();
22865 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22866 return cell;
22867}
22868
c906108c
SS
22869/* Convert a DIE tag into its string name. */
22870
f39c6ffd 22871static const char *
aa1ee363 22872dwarf_tag_name (unsigned tag)
c906108c 22873{
f39c6ffd
TT
22874 const char *name = get_DW_TAG_name (tag);
22875
22876 if (name == NULL)
fa9c3fa0 22877 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22878
22879 return name;
c906108c
SS
22880}
22881
22882/* Convert a DWARF attribute code into its string name. */
22883
f39c6ffd 22884static const char *
aa1ee363 22885dwarf_attr_name (unsigned attr)
c906108c 22886{
f39c6ffd
TT
22887 const char *name;
22888
c764a876 22889#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22890 if (attr == DW_AT_MIPS_fde)
22891 return "DW_AT_MIPS_fde";
22892#else
22893 if (attr == DW_AT_HP_block_index)
22894 return "DW_AT_HP_block_index";
c764a876 22895#endif
f39c6ffd
TT
22896
22897 name = get_DW_AT_name (attr);
22898
22899 if (name == NULL)
fa9c3fa0 22900 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22901
22902 return name;
c906108c
SS
22903}
22904
a084a2a6
AT
22905/* Convert a unit type to corresponding DW_UT name. */
22906
22907static const char *
22908dwarf_unit_type_name (int unit_type) {
22909 switch (unit_type)
22910 {
22911 case 0x01:
22912 return "DW_UT_compile (0x01)";
22913 case 0x02:
22914 return "DW_UT_type (0x02)";
22915 case 0x03:
22916 return "DW_UT_partial (0x03)";
22917 case 0x04:
22918 return "DW_UT_skeleton (0x04)";
22919 case 0x05:
22920 return "DW_UT_split_compile (0x05)";
22921 case 0x06:
22922 return "DW_UT_split_type (0x06)";
22923 case 0x80:
22924 return "DW_UT_lo_user (0x80)";
22925 case 0xff:
22926 return "DW_UT_hi_user (0xff)";
22927 default:
22928 return nullptr;
22929 }
22930}
22931
c906108c
SS
22932/* Convert a DWARF value form code into its string name. */
22933
f39c6ffd 22934static const char *
aa1ee363 22935dwarf_form_name (unsigned form)
c906108c 22936{
f39c6ffd
TT
22937 const char *name = get_DW_FORM_name (form);
22938
22939 if (name == NULL)
fa9c3fa0 22940 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22941
22942 return name;
c906108c
SS
22943}
22944
a121b7c1 22945static const char *
fba45db2 22946dwarf_bool_name (unsigned mybool)
c906108c
SS
22947{
22948 if (mybool)
22949 return "TRUE";
22950 else
22951 return "FALSE";
22952}
22953
22954/* Convert a DWARF type code into its string name. */
22955
f39c6ffd 22956static const char *
aa1ee363 22957dwarf_type_encoding_name (unsigned enc)
c906108c 22958{
f39c6ffd 22959 const char *name = get_DW_ATE_name (enc);
c906108c 22960
f39c6ffd 22961 if (name == NULL)
fa9c3fa0 22962 return dwarf_unknown ("ATE", enc);
c906108c 22963
f39c6ffd 22964 return name;
c906108c 22965}
c906108c 22966
f9aca02d 22967static void
d97bc12b 22968dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22969{
22970 unsigned int i;
22971
d97bc12b 22972 print_spaces (indent, f);
9d8780f0 22973 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22974 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22975 sect_offset_str (die->sect_off));
d97bc12b
DE
22976
22977 if (die->parent != NULL)
22978 {
22979 print_spaces (indent, f);
9d8780f0
SM
22980 fprintf_unfiltered (f, " parent at offset: %s\n",
22981 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22982 }
22983
22984 print_spaces (indent, f);
22985 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22986 dwarf_bool_name (die->child != NULL));
c906108c 22987
d97bc12b
DE
22988 print_spaces (indent, f);
22989 fprintf_unfiltered (f, " attributes:\n");
22990
c906108c
SS
22991 for (i = 0; i < die->num_attrs; ++i)
22992 {
d97bc12b
DE
22993 print_spaces (indent, f);
22994 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22995 dwarf_attr_name (die->attrs[i].name),
22996 dwarf_form_name (die->attrs[i].form));
d97bc12b 22997
c906108c
SS
22998 switch (die->attrs[i].form)
22999 {
c906108c 23000 case DW_FORM_addr:
336d760d 23001 case DW_FORM_addrx:
3019eac3 23002 case DW_FORM_GNU_addr_index:
d97bc12b 23003 fprintf_unfiltered (f, "address: ");
5af949e3 23004 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23005 break;
23006 case DW_FORM_block2:
23007 case DW_FORM_block4:
23008 case DW_FORM_block:
23009 case DW_FORM_block1:
56eb65bd
SP
23010 fprintf_unfiltered (f, "block: size %s",
23011 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23012 break;
2dc7f7b3 23013 case DW_FORM_exprloc:
56eb65bd
SP
23014 fprintf_unfiltered (f, "expression: size %s",
23015 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23016 break;
0224619f
JK
23017 case DW_FORM_data16:
23018 fprintf_unfiltered (f, "constant of 16 bytes");
23019 break;
4568ecf9
DE
23020 case DW_FORM_ref_addr:
23021 fprintf_unfiltered (f, "ref address: ");
23022 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23023 break;
36586728
TT
23024 case DW_FORM_GNU_ref_alt:
23025 fprintf_unfiltered (f, "alt ref address: ");
23026 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23027 break;
10b3939b
DJ
23028 case DW_FORM_ref1:
23029 case DW_FORM_ref2:
23030 case DW_FORM_ref4:
4568ecf9
DE
23031 case DW_FORM_ref8:
23032 case DW_FORM_ref_udata:
d97bc12b 23033 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23034 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23035 break;
c906108c
SS
23036 case DW_FORM_data1:
23037 case DW_FORM_data2:
23038 case DW_FORM_data4:
ce5d95e1 23039 case DW_FORM_data8:
c906108c
SS
23040 case DW_FORM_udata:
23041 case DW_FORM_sdata:
43bbcdc2
PH
23042 fprintf_unfiltered (f, "constant: %s",
23043 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23044 break;
2dc7f7b3
TT
23045 case DW_FORM_sec_offset:
23046 fprintf_unfiltered (f, "section offset: %s",
23047 pulongest (DW_UNSND (&die->attrs[i])));
23048 break;
55f1336d 23049 case DW_FORM_ref_sig8:
ac9ec31b
DE
23050 fprintf_unfiltered (f, "signature: %s",
23051 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23052 break;
c906108c 23053 case DW_FORM_string:
4bdf3d34 23054 case DW_FORM_strp:
43988095 23055 case DW_FORM_line_strp:
cf532bd1 23056 case DW_FORM_strx:
3019eac3 23057 case DW_FORM_GNU_str_index:
36586728 23058 case DW_FORM_GNU_strp_alt:
8285870a 23059 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23060 DW_STRING (&die->attrs[i])
8285870a
JK
23061 ? DW_STRING (&die->attrs[i]) : "",
23062 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23063 break;
23064 case DW_FORM_flag:
23065 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23066 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23067 else
d97bc12b 23068 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23069 break;
2dc7f7b3
TT
23070 case DW_FORM_flag_present:
23071 fprintf_unfiltered (f, "flag: TRUE");
23072 break;
a8329558 23073 case DW_FORM_indirect:
0963b4bd
MS
23074 /* The reader will have reduced the indirect form to
23075 the "base form" so this form should not occur. */
5f48f8f3 23076 fprintf_unfiltered (f,
3e43a32a 23077 "unexpected attribute form: DW_FORM_indirect");
a8329558 23078 break;
663c44ac
JK
23079 case DW_FORM_implicit_const:
23080 fprintf_unfiltered (f, "constant: %s",
23081 plongest (DW_SND (&die->attrs[i])));
23082 break;
c906108c 23083 default:
d97bc12b 23084 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23085 die->attrs[i].form);
d97bc12b 23086 break;
c906108c 23087 }
d97bc12b 23088 fprintf_unfiltered (f, "\n");
c906108c
SS
23089 }
23090}
23091
f9aca02d 23092static void
d97bc12b 23093dump_die_for_error (struct die_info *die)
c906108c 23094{
d97bc12b
DE
23095 dump_die_shallow (gdb_stderr, 0, die);
23096}
23097
23098static void
23099dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23100{
23101 int indent = level * 4;
23102
23103 gdb_assert (die != NULL);
23104
23105 if (level >= max_level)
23106 return;
23107
23108 dump_die_shallow (f, indent, die);
23109
23110 if (die->child != NULL)
c906108c 23111 {
d97bc12b
DE
23112 print_spaces (indent, f);
23113 fprintf_unfiltered (f, " Children:");
23114 if (level + 1 < max_level)
23115 {
23116 fprintf_unfiltered (f, "\n");
23117 dump_die_1 (f, level + 1, max_level, die->child);
23118 }
23119 else
23120 {
3e43a32a
MS
23121 fprintf_unfiltered (f,
23122 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23123 }
23124 }
23125
23126 if (die->sibling != NULL && level > 0)
23127 {
23128 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23129 }
23130}
23131
d97bc12b
DE
23132/* This is called from the pdie macro in gdbinit.in.
23133 It's not static so gcc will keep a copy callable from gdb. */
23134
23135void
23136dump_die (struct die_info *die, int max_level)
23137{
23138 dump_die_1 (gdb_stdlog, 0, max_level, die);
23139}
23140
f9aca02d 23141static void
51545339 23142store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23143{
51545339 23144 void **slot;
c906108c 23145
9c541725
PA
23146 slot = htab_find_slot_with_hash (cu->die_hash, die,
23147 to_underlying (die->sect_off),
b64f50a1 23148 INSERT);
51545339
DJ
23149
23150 *slot = die;
c906108c
SS
23151}
23152
b64f50a1
JK
23153/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23154 required kind. */
23155
23156static sect_offset
ff39bb5e 23157dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23158{
7771576e 23159 if (attr_form_is_ref (attr))
9c541725 23160 return (sect_offset) DW_UNSND (attr);
93311388 23161
b98664d3 23162 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23163 dwarf_form_name (attr->form));
9c541725 23164 return {};
c906108c
SS
23165}
23166
43bbcdc2
PH
23167/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23168 * the value held by the attribute is not constant. */
a02abb62 23169
43bbcdc2 23170static LONGEST
ff39bb5e 23171dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23172{
663c44ac 23173 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23174 return DW_SND (attr);
23175 else if (attr->form == DW_FORM_udata
23176 || attr->form == DW_FORM_data1
23177 || attr->form == DW_FORM_data2
23178 || attr->form == DW_FORM_data4
23179 || attr->form == DW_FORM_data8)
23180 return DW_UNSND (attr);
23181 else
23182 {
0224619f 23183 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23184 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23185 dwarf_form_name (attr->form));
23186 return default_value;
23187 }
23188}
23189
348e048f
DE
23190/* Follow reference or signature attribute ATTR of SRC_DIE.
23191 On entry *REF_CU is the CU of SRC_DIE.
23192 On exit *REF_CU is the CU of the result. */
23193
23194static struct die_info *
ff39bb5e 23195follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23196 struct dwarf2_cu **ref_cu)
23197{
23198 struct die_info *die;
23199
7771576e 23200 if (attr_form_is_ref (attr))
348e048f 23201 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23202 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23203 die = follow_die_sig (src_die, attr, ref_cu);
23204 else
23205 {
23206 dump_die_for_error (src_die);
23207 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23208 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23209 }
23210
23211 return die;
03dd20cc
DJ
23212}
23213
5c631832 23214/* Follow reference OFFSET.
673bfd45
DE
23215 On entry *REF_CU is the CU of the source die referencing OFFSET.
23216 On exit *REF_CU is the CU of the result.
23217 Returns NULL if OFFSET is invalid. */
f504f079 23218
f9aca02d 23219static struct die_info *
9c541725 23220follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23221 struct dwarf2_cu **ref_cu)
c906108c 23222{
10b3939b 23223 struct die_info temp_die;
f2f0e013 23224 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23225 struct dwarf2_per_objfile *dwarf2_per_objfile
23226 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23227
348e048f
DE
23228 gdb_assert (cu->per_cu != NULL);
23229
98bfdba5
PA
23230 target_cu = cu;
23231
3019eac3 23232 if (cu->per_cu->is_debug_types)
348e048f
DE
23233 {
23234 /* .debug_types CUs cannot reference anything outside their CU.
23235 If they need to, they have to reference a signatured type via
55f1336d 23236 DW_FORM_ref_sig8. */
9c541725 23237 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23238 return NULL;
348e048f 23239 }
36586728 23240 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23241 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23242 {
23243 struct dwarf2_per_cu_data *per_cu;
9a619af0 23244
9c541725 23245 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23246 dwarf2_per_objfile);
03dd20cc
DJ
23247
23248 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23249 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23250 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23251
10b3939b
DJ
23252 target_cu = per_cu->cu;
23253 }
98bfdba5
PA
23254 else if (cu->dies == NULL)
23255 {
23256 /* We're loading full DIEs during partial symbol reading. */
23257 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23258 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23259 }
c906108c 23260
f2f0e013 23261 *ref_cu = target_cu;
9c541725 23262 temp_die.sect_off = sect_off;
c24bdb02
KS
23263
23264 if (target_cu != cu)
23265 target_cu->ancestor = cu;
23266
9a3c8263 23267 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23268 &temp_die,
23269 to_underlying (sect_off));
5c631832 23270}
10b3939b 23271
5c631832
JK
23272/* Follow reference attribute ATTR of SRC_DIE.
23273 On entry *REF_CU is the CU of SRC_DIE.
23274 On exit *REF_CU is the CU of the result. */
23275
23276static struct die_info *
ff39bb5e 23277follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23278 struct dwarf2_cu **ref_cu)
23279{
9c541725 23280 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23281 struct dwarf2_cu *cu = *ref_cu;
23282 struct die_info *die;
23283
9c541725 23284 die = follow_die_offset (sect_off,
36586728
TT
23285 (attr->form == DW_FORM_GNU_ref_alt
23286 || cu->per_cu->is_dwz),
23287 ref_cu);
5c631832 23288 if (!die)
9d8780f0
SM
23289 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23290 "at %s [in module %s]"),
23291 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23292 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23293
5c631832
JK
23294 return die;
23295}
23296
9c541725 23297/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23298 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23299 dwarf2_locexpr_baton->data has lifetime of
23300 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23301
23302struct dwarf2_locexpr_baton
9c541725 23303dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23304 struct dwarf2_per_cu_data *per_cu,
23305 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23306 void *baton, bool resolve_abstract_p)
5c631832 23307{
918dd910 23308 struct dwarf2_cu *cu;
5c631832
JK
23309 struct die_info *die;
23310 struct attribute *attr;
23311 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23312 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23313 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23314
918dd910 23315 if (per_cu->cu == NULL)
58f0c718 23316 load_cu (per_cu, false);
918dd910 23317 cu = per_cu->cu;
cc12ce38
DE
23318 if (cu == NULL)
23319 {
23320 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23321 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23322 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23323 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23324 }
918dd910 23325
9c541725 23326 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23327 if (!die)
9d8780f0
SM
23328 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23329 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23330
23331 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23332 if (!attr && resolve_abstract_p
3360b6e7 23333 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23334 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23335 {
23336 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23337 CORE_ADDR baseaddr
23338 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23339 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23340
3360b6e7
TV
23341 for (const auto &cand_off
23342 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23343 {
3360b6e7
TV
23344 struct dwarf2_cu *cand_cu = cu;
23345 struct die_info *cand
23346 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23347 if (!cand
23348 || !cand->parent
e4a62c65
TV
23349 || cand->parent->tag != DW_TAG_subprogram)
23350 continue;
23351
23352 CORE_ADDR pc_low, pc_high;
23353 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23354 if (pc_low == ((CORE_ADDR) -1))
23355 continue;
23356 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23357 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23358 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23359 continue;
23360
23361 die = cand;
23362 attr = dwarf2_attr (die, DW_AT_location, cu);
23363 break;
23364 }
23365 }
23366
5c631832
JK
23367 if (!attr)
23368 {
e103e986
JK
23369 /* DWARF: "If there is no such attribute, then there is no effect.".
23370 DATA is ignored if SIZE is 0. */
5c631832 23371
e103e986 23372 retval.data = NULL;
5c631832
JK
23373 retval.size = 0;
23374 }
8cf6f0b1
TT
23375 else if (attr_form_is_section_offset (attr))
23376 {
23377 struct dwarf2_loclist_baton loclist_baton;
23378 CORE_ADDR pc = (*get_frame_pc) (baton);
23379 size_t size;
23380
23381 fill_in_loclist_baton (cu, &loclist_baton, attr);
23382
23383 retval.data = dwarf2_find_location_expression (&loclist_baton,
23384 &size, pc);
23385 retval.size = size;
23386 }
5c631832
JK
23387 else
23388 {
23389 if (!attr_form_is_block (attr))
9d8780f0 23390 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23391 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23392 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23393
23394 retval.data = DW_BLOCK (attr)->data;
23395 retval.size = DW_BLOCK (attr)->size;
23396 }
23397 retval.per_cu = cu->per_cu;
918dd910 23398
ed2dc618 23399 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23400
5c631832 23401 return retval;
348e048f
DE
23402}
23403
8b9737bf
TT
23404/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23405 offset. */
23406
23407struct dwarf2_locexpr_baton
23408dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23409 struct dwarf2_per_cu_data *per_cu,
23410 CORE_ADDR (*get_frame_pc) (void *baton),
23411 void *baton)
23412{
9c541725 23413 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23414
9c541725 23415 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23416}
23417
b6807d98
TT
23418/* Write a constant of a given type as target-ordered bytes into
23419 OBSTACK. */
23420
23421static const gdb_byte *
23422write_constant_as_bytes (struct obstack *obstack,
23423 enum bfd_endian byte_order,
23424 struct type *type,
23425 ULONGEST value,
23426 LONGEST *len)
23427{
23428 gdb_byte *result;
23429
23430 *len = TYPE_LENGTH (type);
224c3ddb 23431 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23432 store_unsigned_integer (result, *len, byte_order, value);
23433
23434 return result;
23435}
23436
23437/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23438 pointer to the constant bytes and set LEN to the length of the
23439 data. If memory is needed, allocate it on OBSTACK. If the DIE
23440 does not have a DW_AT_const_value, return NULL. */
23441
23442const gdb_byte *
9c541725 23443dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23444 struct dwarf2_per_cu_data *per_cu,
23445 struct obstack *obstack,
23446 LONGEST *len)
23447{
23448 struct dwarf2_cu *cu;
23449 struct die_info *die;
23450 struct attribute *attr;
23451 const gdb_byte *result = NULL;
23452 struct type *type;
23453 LONGEST value;
23454 enum bfd_endian byte_order;
e3b94546 23455 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23456
b6807d98 23457 if (per_cu->cu == NULL)
58f0c718 23458 load_cu (per_cu, false);
b6807d98 23459 cu = per_cu->cu;
cc12ce38
DE
23460 if (cu == NULL)
23461 {
23462 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23463 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23464 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23465 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23466 }
b6807d98 23467
9c541725 23468 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23469 if (!die)
9d8780f0
SM
23470 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23471 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23472
23473 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23474 if (attr == NULL)
23475 return NULL;
23476
e3b94546 23477 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23478 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23479
23480 switch (attr->form)
23481 {
23482 case DW_FORM_addr:
336d760d 23483 case DW_FORM_addrx:
b6807d98
TT
23484 case DW_FORM_GNU_addr_index:
23485 {
23486 gdb_byte *tem;
23487
23488 *len = cu->header.addr_size;
224c3ddb 23489 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23490 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23491 result = tem;
23492 }
23493 break;
23494 case DW_FORM_string:
23495 case DW_FORM_strp:
cf532bd1 23496 case DW_FORM_strx:
b6807d98
TT
23497 case DW_FORM_GNU_str_index:
23498 case DW_FORM_GNU_strp_alt:
23499 /* DW_STRING is already allocated on the objfile obstack, point
23500 directly to it. */
23501 result = (const gdb_byte *) DW_STRING (attr);
23502 *len = strlen (DW_STRING (attr));
23503 break;
23504 case DW_FORM_block1:
23505 case DW_FORM_block2:
23506 case DW_FORM_block4:
23507 case DW_FORM_block:
23508 case DW_FORM_exprloc:
0224619f 23509 case DW_FORM_data16:
b6807d98
TT
23510 result = DW_BLOCK (attr)->data;
23511 *len = DW_BLOCK (attr)->size;
23512 break;
23513
23514 /* The DW_AT_const_value attributes are supposed to carry the
23515 symbol's value "represented as it would be on the target
23516 architecture." By the time we get here, it's already been
23517 converted to host endianness, so we just need to sign- or
23518 zero-extend it as appropriate. */
23519 case DW_FORM_data1:
23520 type = die_type (die, cu);
23521 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23522 if (result == NULL)
23523 result = write_constant_as_bytes (obstack, byte_order,
23524 type, value, len);
23525 break;
23526 case DW_FORM_data2:
23527 type = die_type (die, cu);
23528 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23529 if (result == NULL)
23530 result = write_constant_as_bytes (obstack, byte_order,
23531 type, value, len);
23532 break;
23533 case DW_FORM_data4:
23534 type = die_type (die, cu);
23535 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23536 if (result == NULL)
23537 result = write_constant_as_bytes (obstack, byte_order,
23538 type, value, len);
23539 break;
23540 case DW_FORM_data8:
23541 type = die_type (die, cu);
23542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23543 if (result == NULL)
23544 result = write_constant_as_bytes (obstack, byte_order,
23545 type, value, len);
23546 break;
23547
23548 case DW_FORM_sdata:
663c44ac 23549 case DW_FORM_implicit_const:
b6807d98
TT
23550 type = die_type (die, cu);
23551 result = write_constant_as_bytes (obstack, byte_order,
23552 type, DW_SND (attr), len);
23553 break;
23554
23555 case DW_FORM_udata:
23556 type = die_type (die, cu);
23557 result = write_constant_as_bytes (obstack, byte_order,
23558 type, DW_UNSND (attr), len);
23559 break;
23560
23561 default:
b98664d3 23562 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23563 dwarf_form_name (attr->form));
23564 break;
23565 }
23566
23567 return result;
23568}
23569
7942e96e
AA
23570/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23571 valid type for this die is found. */
23572
23573struct type *
9c541725 23574dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23575 struct dwarf2_per_cu_data *per_cu)
23576{
23577 struct dwarf2_cu *cu;
23578 struct die_info *die;
23579
7942e96e 23580 if (per_cu->cu == NULL)
58f0c718 23581 load_cu (per_cu, false);
7942e96e
AA
23582 cu = per_cu->cu;
23583 if (!cu)
23584 return NULL;
23585
9c541725 23586 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23587 if (!die)
23588 return NULL;
23589
23590 return die_type (die, cu);
23591}
23592
8a9b8146
TT
23593/* Return the type of the DIE at DIE_OFFSET in the CU named by
23594 PER_CU. */
23595
23596struct type *
b64f50a1 23597dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23598 struct dwarf2_per_cu_data *per_cu)
23599{
9c541725 23600 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23601 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23602}
23603
ac9ec31b 23604/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23605 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23606 On exit *REF_CU is the CU of the result.
23607 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23608
23609static struct die_info *
ac9ec31b
DE
23610follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23611 struct dwarf2_cu **ref_cu)
348e048f 23612{
348e048f 23613 struct die_info temp_die;
c24bdb02 23614 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23615 struct die_info *die;
23616
ac9ec31b
DE
23617 /* While it might be nice to assert sig_type->type == NULL here,
23618 we can get here for DW_AT_imported_declaration where we need
23619 the DIE not the type. */
348e048f
DE
23620
23621 /* If necessary, add it to the queue and load its DIEs. */
23622
95554aad 23623 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23624 read_signatured_type (sig_type);
348e048f 23625
348e048f 23626 sig_cu = sig_type->per_cu.cu;
69d751e3 23627 gdb_assert (sig_cu != NULL);
9c541725
PA
23628 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23629 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23630 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23631 to_underlying (temp_die.sect_off));
348e048f
DE
23632 if (die)
23633 {
ed2dc618 23634 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23635 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23636
796a7ff8
DE
23637 /* For .gdb_index version 7 keep track of included TUs.
23638 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23639 if (dwarf2_per_objfile->index_table != NULL
23640 && dwarf2_per_objfile->index_table->version <= 7)
23641 {
23642 VEC_safe_push (dwarf2_per_cu_ptr,
23643 (*ref_cu)->per_cu->imported_symtabs,
23644 sig_cu->per_cu);
23645 }
23646
348e048f 23647 *ref_cu = sig_cu;
c24bdb02
KS
23648 if (sig_cu != cu)
23649 sig_cu->ancestor = cu;
23650
348e048f
DE
23651 return die;
23652 }
23653
ac9ec31b
DE
23654 return NULL;
23655}
23656
23657/* Follow signatured type referenced by ATTR in SRC_DIE.
23658 On entry *REF_CU is the CU of SRC_DIE.
23659 On exit *REF_CU is the CU of the result.
23660 The result is the DIE of the type.
23661 If the referenced type cannot be found an error is thrown. */
23662
23663static struct die_info *
ff39bb5e 23664follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23665 struct dwarf2_cu **ref_cu)
23666{
23667 ULONGEST signature = DW_SIGNATURE (attr);
23668 struct signatured_type *sig_type;
23669 struct die_info *die;
23670
23671 gdb_assert (attr->form == DW_FORM_ref_sig8);
23672
a2ce51a0 23673 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23674 /* sig_type will be NULL if the signatured type is missing from
23675 the debug info. */
23676 if (sig_type == NULL)
23677 {
23678 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23679 " from DIE at %s [in module %s]"),
23680 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23681 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23682 }
23683
23684 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23685 if (die == NULL)
23686 {
23687 dump_die_for_error (src_die);
23688 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23689 " from DIE at %s [in module %s]"),
23690 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23691 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23692 }
23693
23694 return die;
23695}
23696
23697/* Get the type specified by SIGNATURE referenced in DIE/CU,
23698 reading in and processing the type unit if necessary. */
23699
23700static struct type *
23701get_signatured_type (struct die_info *die, ULONGEST signature,
23702 struct dwarf2_cu *cu)
23703{
518817b3
SM
23704 struct dwarf2_per_objfile *dwarf2_per_objfile
23705 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23706 struct signatured_type *sig_type;
23707 struct dwarf2_cu *type_cu;
23708 struct die_info *type_die;
23709 struct type *type;
23710
a2ce51a0 23711 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23712 /* sig_type will be NULL if the signatured type is missing from
23713 the debug info. */
23714 if (sig_type == NULL)
23715 {
b98664d3 23716 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23717 " from DIE at %s [in module %s]"),
23718 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23719 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23720 return build_error_marker_type (cu, die);
23721 }
23722
23723 /* If we already know the type we're done. */
23724 if (sig_type->type != NULL)
23725 return sig_type->type;
23726
23727 type_cu = cu;
23728 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23729 if (type_die != NULL)
23730 {
23731 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23732 is created. This is important, for example, because for c++ classes
23733 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23734 type = read_type_die (type_die, type_cu);
23735 if (type == NULL)
23736 {
b98664d3 23737 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23738 " referenced from DIE at %s [in module %s]"),
23739 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23740 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23741 type = build_error_marker_type (cu, die);
23742 }
23743 }
23744 else
23745 {
b98664d3 23746 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23747 " from DIE at %s [in module %s]"),
23748 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23749 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23750 type = build_error_marker_type (cu, die);
23751 }
23752 sig_type->type = type;
23753
23754 return type;
23755}
23756
23757/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23758 reading in and processing the type unit if necessary. */
23759
23760static struct type *
ff39bb5e 23761get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23762 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23763{
23764 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23765 if (attr_form_is_ref (attr))
ac9ec31b
DE
23766 {
23767 struct dwarf2_cu *type_cu = cu;
23768 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23769
23770 return read_type_die (type_die, type_cu);
23771 }
23772 else if (attr->form == DW_FORM_ref_sig8)
23773 {
23774 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23775 }
23776 else
23777 {
518817b3
SM
23778 struct dwarf2_per_objfile *dwarf2_per_objfile
23779 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23780
b98664d3 23781 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23782 " at %s [in module %s]"),
23783 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23784 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23785 return build_error_marker_type (cu, die);
23786 }
348e048f
DE
23787}
23788
e5fe5e75 23789/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23790
23791static void
e5fe5e75 23792load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23793{
52dc124a 23794 struct signatured_type *sig_type;
348e048f 23795
f4dc4d17
DE
23796 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23797 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23798
6721b2ec
DE
23799 /* We have the per_cu, but we need the signatured_type.
23800 Fortunately this is an easy translation. */
23801 gdb_assert (per_cu->is_debug_types);
23802 sig_type = (struct signatured_type *) per_cu;
348e048f 23803
6721b2ec 23804 gdb_assert (per_cu->cu == NULL);
348e048f 23805
52dc124a 23806 read_signatured_type (sig_type);
348e048f 23807
6721b2ec 23808 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23809}
23810
dee91e82
DE
23811/* die_reader_func for read_signatured_type.
23812 This is identical to load_full_comp_unit_reader,
23813 but is kept separate for now. */
348e048f
DE
23814
23815static void
dee91e82 23816read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23817 const gdb_byte *info_ptr,
dee91e82
DE
23818 struct die_info *comp_unit_die,
23819 int has_children,
23820 void *data)
348e048f 23821{
dee91e82 23822 struct dwarf2_cu *cu = reader->cu;
348e048f 23823
dee91e82
DE
23824 gdb_assert (cu->die_hash == NULL);
23825 cu->die_hash =
23826 htab_create_alloc_ex (cu->header.length / 12,
23827 die_hash,
23828 die_eq,
23829 NULL,
23830 &cu->comp_unit_obstack,
23831 hashtab_obstack_allocate,
23832 dummy_obstack_deallocate);
348e048f 23833
dee91e82
DE
23834 if (has_children)
23835 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23836 &info_ptr, comp_unit_die);
23837 cu->dies = comp_unit_die;
23838 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23839
23840 /* We try not to read any attributes in this function, because not
9cdd5dbd 23841 all CUs needed for references have been loaded yet, and symbol
348e048f 23842 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23843 or we won't be able to build types correctly.
23844 Similarly, if we do not read the producer, we can not apply
23845 producer-specific interpretation. */
95554aad 23846 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23847}
348e048f 23848
3019eac3
DE
23849/* Read in a signatured type and build its CU and DIEs.
23850 If the type is a stub for the real type in a DWO file,
23851 read in the real type from the DWO file as well. */
dee91e82
DE
23852
23853static void
23854read_signatured_type (struct signatured_type *sig_type)
23855{
23856 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23857
3019eac3 23858 gdb_assert (per_cu->is_debug_types);
dee91e82 23859 gdb_assert (per_cu->cu == NULL);
348e048f 23860
58f0c718 23861 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23862 read_signatured_type_reader, NULL);
7ee85ab1 23863 sig_type->per_cu.tu_read = 1;
c906108c
SS
23864}
23865
c906108c
SS
23866/* Decode simple location descriptions.
23867 Given a pointer to a dwarf block that defines a location, compute
23868 the location and return the value.
23869
4cecd739
DJ
23870 NOTE drow/2003-11-18: This function is called in two situations
23871 now: for the address of static or global variables (partial symbols
23872 only) and for offsets into structures which are expected to be
23873 (more or less) constant. The partial symbol case should go away,
23874 and only the constant case should remain. That will let this
23875 function complain more accurately. A few special modes are allowed
23876 without complaint for global variables (for instance, global
23877 register values and thread-local values).
c906108c
SS
23878
23879 A location description containing no operations indicates that the
4cecd739 23880 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23881 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23882 callers will only want a very basic result and this can become a
21ae7a4d
JK
23883 complaint.
23884
23885 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23886
23887static CORE_ADDR
e7c27a73 23888decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23889{
518817b3 23890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23891 size_t i;
23892 size_t size = blk->size;
d521ce57 23893 const gdb_byte *data = blk->data;
21ae7a4d
JK
23894 CORE_ADDR stack[64];
23895 int stacki;
23896 unsigned int bytes_read, unsnd;
23897 gdb_byte op;
c906108c 23898
21ae7a4d
JK
23899 i = 0;
23900 stacki = 0;
23901 stack[stacki] = 0;
23902 stack[++stacki] = 0;
23903
23904 while (i < size)
23905 {
23906 op = data[i++];
23907 switch (op)
23908 {
23909 case DW_OP_lit0:
23910 case DW_OP_lit1:
23911 case DW_OP_lit2:
23912 case DW_OP_lit3:
23913 case DW_OP_lit4:
23914 case DW_OP_lit5:
23915 case DW_OP_lit6:
23916 case DW_OP_lit7:
23917 case DW_OP_lit8:
23918 case DW_OP_lit9:
23919 case DW_OP_lit10:
23920 case DW_OP_lit11:
23921 case DW_OP_lit12:
23922 case DW_OP_lit13:
23923 case DW_OP_lit14:
23924 case DW_OP_lit15:
23925 case DW_OP_lit16:
23926 case DW_OP_lit17:
23927 case DW_OP_lit18:
23928 case DW_OP_lit19:
23929 case DW_OP_lit20:
23930 case DW_OP_lit21:
23931 case DW_OP_lit22:
23932 case DW_OP_lit23:
23933 case DW_OP_lit24:
23934 case DW_OP_lit25:
23935 case DW_OP_lit26:
23936 case DW_OP_lit27:
23937 case DW_OP_lit28:
23938 case DW_OP_lit29:
23939 case DW_OP_lit30:
23940 case DW_OP_lit31:
23941 stack[++stacki] = op - DW_OP_lit0;
23942 break;
f1bea926 23943
21ae7a4d
JK
23944 case DW_OP_reg0:
23945 case DW_OP_reg1:
23946 case DW_OP_reg2:
23947 case DW_OP_reg3:
23948 case DW_OP_reg4:
23949 case DW_OP_reg5:
23950 case DW_OP_reg6:
23951 case DW_OP_reg7:
23952 case DW_OP_reg8:
23953 case DW_OP_reg9:
23954 case DW_OP_reg10:
23955 case DW_OP_reg11:
23956 case DW_OP_reg12:
23957 case DW_OP_reg13:
23958 case DW_OP_reg14:
23959 case DW_OP_reg15:
23960 case DW_OP_reg16:
23961 case DW_OP_reg17:
23962 case DW_OP_reg18:
23963 case DW_OP_reg19:
23964 case DW_OP_reg20:
23965 case DW_OP_reg21:
23966 case DW_OP_reg22:
23967 case DW_OP_reg23:
23968 case DW_OP_reg24:
23969 case DW_OP_reg25:
23970 case DW_OP_reg26:
23971 case DW_OP_reg27:
23972 case DW_OP_reg28:
23973 case DW_OP_reg29:
23974 case DW_OP_reg30:
23975 case DW_OP_reg31:
23976 stack[++stacki] = op - DW_OP_reg0;
23977 if (i < size)
23978 dwarf2_complex_location_expr_complaint ();
23979 break;
c906108c 23980
21ae7a4d
JK
23981 case DW_OP_regx:
23982 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23983 i += bytes_read;
23984 stack[++stacki] = unsnd;
23985 if (i < size)
23986 dwarf2_complex_location_expr_complaint ();
23987 break;
c906108c 23988
21ae7a4d
JK
23989 case DW_OP_addr:
23990 stack[++stacki] = read_address (objfile->obfd, &data[i],
23991 cu, &bytes_read);
23992 i += bytes_read;
23993 break;
d53d4ac5 23994
21ae7a4d
JK
23995 case DW_OP_const1u:
23996 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23997 i += 1;
23998 break;
23999
24000 case DW_OP_const1s:
24001 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24002 i += 1;
24003 break;
24004
24005 case DW_OP_const2u:
24006 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24007 i += 2;
24008 break;
24009
24010 case DW_OP_const2s:
24011 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24012 i += 2;
24013 break;
d53d4ac5 24014
21ae7a4d
JK
24015 case DW_OP_const4u:
24016 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24017 i += 4;
24018 break;
24019
24020 case DW_OP_const4s:
24021 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24022 i += 4;
24023 break;
24024
585861ea
JK
24025 case DW_OP_const8u:
24026 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24027 i += 8;
24028 break;
24029
21ae7a4d
JK
24030 case DW_OP_constu:
24031 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24032 &bytes_read);
24033 i += bytes_read;
24034 break;
24035
24036 case DW_OP_consts:
24037 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24038 i += bytes_read;
24039 break;
24040
24041 case DW_OP_dup:
24042 stack[stacki + 1] = stack[stacki];
24043 stacki++;
24044 break;
24045
24046 case DW_OP_plus:
24047 stack[stacki - 1] += stack[stacki];
24048 stacki--;
24049 break;
24050
24051 case DW_OP_plus_uconst:
24052 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24053 &bytes_read);
24054 i += bytes_read;
24055 break;
24056
24057 case DW_OP_minus:
24058 stack[stacki - 1] -= stack[stacki];
24059 stacki--;
24060 break;
24061
24062 case DW_OP_deref:
24063 /* If we're not the last op, then we definitely can't encode
24064 this using GDB's address_class enum. This is valid for partial
24065 global symbols, although the variable's address will be bogus
24066 in the psymtab. */
24067 if (i < size)
24068 dwarf2_complex_location_expr_complaint ();
24069 break;
24070
24071 case DW_OP_GNU_push_tls_address:
4aa4e28b 24072 case DW_OP_form_tls_address:
21ae7a4d
JK
24073 /* The top of the stack has the offset from the beginning
24074 of the thread control block at which the variable is located. */
24075 /* Nothing should follow this operator, so the top of stack would
24076 be returned. */
24077 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24078 address will be bogus in the psymtab. Make it always at least
24079 non-zero to not look as a variable garbage collected by linker
24080 which have DW_OP_addr 0. */
21ae7a4d
JK
24081 if (i < size)
24082 dwarf2_complex_location_expr_complaint ();
585861ea 24083 stack[stacki]++;
21ae7a4d
JK
24084 break;
24085
24086 case DW_OP_GNU_uninit:
24087 break;
24088
336d760d 24089 case DW_OP_addrx:
3019eac3 24090 case DW_OP_GNU_addr_index:
49f6c839 24091 case DW_OP_GNU_const_index:
3019eac3
DE
24092 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24093 &bytes_read);
24094 i += bytes_read;
24095 break;
24096
21ae7a4d
JK
24097 default:
24098 {
f39c6ffd 24099 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24100
24101 if (name)
b98664d3 24102 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24103 name);
24104 else
b98664d3 24105 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24106 op);
24107 }
24108
24109 return (stack[stacki]);
d53d4ac5 24110 }
3c6e0cb3 24111
21ae7a4d
JK
24112 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24113 outside of the allocated space. Also enforce minimum>0. */
24114 if (stacki >= ARRAY_SIZE (stack) - 1)
24115 {
b98664d3 24116 complaint (_("location description stack overflow"));
21ae7a4d
JK
24117 return 0;
24118 }
24119
24120 if (stacki <= 0)
24121 {
b98664d3 24122 complaint (_("location description stack underflow"));
21ae7a4d
JK
24123 return 0;
24124 }
24125 }
24126 return (stack[stacki]);
c906108c
SS
24127}
24128
24129/* memory allocation interface */
24130
c906108c 24131static struct dwarf_block *
7b5a2f43 24132dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24133{
8d749320 24134 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24135}
24136
c906108c 24137static struct die_info *
b60c80d6 24138dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24139{
24140 struct die_info *die;
b60c80d6
DJ
24141 size_t size = sizeof (struct die_info);
24142
24143 if (num_attrs > 1)
24144 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24145
b60c80d6 24146 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24147 memset (die, 0, sizeof (struct die_info));
24148 return (die);
24149}
2e276125
JB
24150
24151\f
24152/* Macro support. */
24153
233d95b5
JK
24154/* Return file name relative to the compilation directory of file number I in
24155 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24156 responsible for freeing it. */
233d95b5 24157
2e276125 24158static char *
233d95b5 24159file_file_name (int file, struct line_header *lh)
2e276125 24160{
6a83a1e6
EZ
24161 /* Is the file number a valid index into the line header's file name
24162 table? Remember that file numbers start with one, not zero. */
fff8551c 24163 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24164 {
8c43009f 24165 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24166
8c43009f
PA
24167 if (!IS_ABSOLUTE_PATH (fe.name))
24168 {
24169 const char *dir = fe.include_dir (lh);
24170 if (dir != NULL)
24171 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24172 }
24173 return xstrdup (fe.name);
6a83a1e6 24174 }
2e276125
JB
24175 else
24176 {
6a83a1e6
EZ
24177 /* The compiler produced a bogus file number. We can at least
24178 record the macro definitions made in the file, even if we
24179 won't be able to find the file by name. */
24180 char fake_name[80];
9a619af0 24181
8c042590
PM
24182 xsnprintf (fake_name, sizeof (fake_name),
24183 "<bad macro file number %d>", file);
2e276125 24184
b98664d3 24185 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24186 file);
2e276125 24187
6a83a1e6 24188 return xstrdup (fake_name);
2e276125
JB
24189 }
24190}
24191
233d95b5
JK
24192/* Return the full name of file number I in *LH's file name table.
24193 Use COMP_DIR as the name of the current directory of the
24194 compilation. The result is allocated using xmalloc; the caller is
24195 responsible for freeing it. */
24196static char *
24197file_full_name (int file, struct line_header *lh, const char *comp_dir)
24198{
24199 /* Is the file number a valid index into the line header's file name
24200 table? Remember that file numbers start with one, not zero. */
fff8551c 24201 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24202 {
24203 char *relative = file_file_name (file, lh);
24204
24205 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24206 return relative;
b36cec19
PA
24207 return reconcat (relative, comp_dir, SLASH_STRING,
24208 relative, (char *) NULL);
233d95b5
JK
24209 }
24210 else
24211 return file_file_name (file, lh);
24212}
24213
2e276125
JB
24214
24215static struct macro_source_file *
804d2729
TT
24216macro_start_file (struct dwarf2_cu *cu,
24217 int file, int line,
2e276125 24218 struct macro_source_file *current_file,
43f3e411 24219 struct line_header *lh)
2e276125 24220{
233d95b5
JK
24221 /* File name relative to the compilation directory of this source file. */
24222 char *file_name = file_file_name (file, lh);
2e276125 24223
2e276125 24224 if (! current_file)
abc9d0dc 24225 {
fc474241
DE
24226 /* Note: We don't create a macro table for this compilation unit
24227 at all until we actually get a filename. */
c24bdb02 24228 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24229
abc9d0dc
TT
24230 /* If we have no current file, then this must be the start_file
24231 directive for the compilation unit's main source file. */
fc474241
DE
24232 current_file = macro_set_main (macro_table, file_name);
24233 macro_define_special (macro_table);
abc9d0dc 24234 }
2e276125 24235 else
233d95b5 24236 current_file = macro_include (current_file, line, file_name);
2e276125 24237
233d95b5 24238 xfree (file_name);
6e70227d 24239
2e276125
JB
24240 return current_file;
24241}
24242
2e276125
JB
24243static const char *
24244consume_improper_spaces (const char *p, const char *body)
24245{
24246 if (*p == ' ')
24247 {
b98664d3 24248 complaint (_("macro definition contains spaces "
3e43a32a 24249 "in formal argument list:\n`%s'"),
4d3c2250 24250 body);
2e276125
JB
24251
24252 while (*p == ' ')
24253 p++;
24254 }
24255
24256 return p;
24257}
24258
24259
24260static void
24261parse_macro_definition (struct macro_source_file *file, int line,
24262 const char *body)
24263{
24264 const char *p;
24265
24266 /* The body string takes one of two forms. For object-like macro
24267 definitions, it should be:
24268
24269 <macro name> " " <definition>
24270
24271 For function-like macro definitions, it should be:
24272
24273 <macro name> "() " <definition>
24274 or
24275 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24276
24277 Spaces may appear only where explicitly indicated, and in the
24278 <definition>.
24279
24280 The Dwarf 2 spec says that an object-like macro's name is always
24281 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24282 the space when the macro's definition is the empty string.
2e276125
JB
24283
24284 The Dwarf 2 spec says that there should be no spaces between the
24285 formal arguments in a function-like macro's formal argument list,
24286 but versions of GCC around March 2002 include spaces after the
24287 commas. */
24288
24289
24290 /* Find the extent of the macro name. The macro name is terminated
24291 by either a space or null character (for an object-like macro) or
24292 an opening paren (for a function-like macro). */
24293 for (p = body; *p; p++)
24294 if (*p == ' ' || *p == '(')
24295 break;
24296
24297 if (*p == ' ' || *p == '\0')
24298 {
24299 /* It's an object-like macro. */
24300 int name_len = p - body;
3f8a7804 24301 char *name = savestring (body, name_len);
2e276125
JB
24302 const char *replacement;
24303
24304 if (*p == ' ')
24305 replacement = body + name_len + 1;
24306 else
24307 {
4d3c2250 24308 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24309 replacement = body + name_len;
24310 }
6e70227d 24311
2e276125
JB
24312 macro_define_object (file, line, name, replacement);
24313
24314 xfree (name);
24315 }
24316 else if (*p == '(')
24317 {
24318 /* It's a function-like macro. */
3f8a7804 24319 char *name = savestring (body, p - body);
2e276125
JB
24320 int argc = 0;
24321 int argv_size = 1;
8d749320 24322 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24323
24324 p++;
24325
24326 p = consume_improper_spaces (p, body);
24327
24328 /* Parse the formal argument list. */
24329 while (*p && *p != ')')
24330 {
24331 /* Find the extent of the current argument name. */
24332 const char *arg_start = p;
24333
24334 while (*p && *p != ',' && *p != ')' && *p != ' ')
24335 p++;
24336
24337 if (! *p || p == arg_start)
4d3c2250 24338 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24339 else
24340 {
24341 /* Make sure argv has room for the new argument. */
24342 if (argc >= argv_size)
24343 {
24344 argv_size *= 2;
224c3ddb 24345 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24346 }
24347
3f8a7804 24348 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24349 }
24350
24351 p = consume_improper_spaces (p, body);
24352
24353 /* Consume the comma, if present. */
24354 if (*p == ',')
24355 {
24356 p++;
24357
24358 p = consume_improper_spaces (p, body);
24359 }
24360 }
24361
24362 if (*p == ')')
24363 {
24364 p++;
24365
24366 if (*p == ' ')
24367 /* Perfectly formed definition, no complaints. */
24368 macro_define_function (file, line, name,
6e70227d 24369 argc, (const char **) argv,
2e276125
JB
24370 p + 1);
24371 else if (*p == '\0')
24372 {
24373 /* Complain, but do define it. */
4d3c2250 24374 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24375 macro_define_function (file, line, name,
6e70227d 24376 argc, (const char **) argv,
2e276125
JB
24377 p);
24378 }
24379 else
24380 /* Just complain. */
4d3c2250 24381 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24382 }
24383 else
24384 /* Just complain. */
4d3c2250 24385 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24386
24387 xfree (name);
24388 {
24389 int i;
24390
24391 for (i = 0; i < argc; i++)
24392 xfree (argv[i]);
24393 }
24394 xfree (argv);
24395 }
24396 else
4d3c2250 24397 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24398}
24399
cf2c3c16
TT
24400/* Skip some bytes from BYTES according to the form given in FORM.
24401 Returns the new pointer. */
2e276125 24402
d521ce57
TT
24403static const gdb_byte *
24404skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24405 enum dwarf_form form,
24406 unsigned int offset_size,
24407 struct dwarf2_section_info *section)
2e276125 24408{
cf2c3c16 24409 unsigned int bytes_read;
2e276125 24410
cf2c3c16 24411 switch (form)
2e276125 24412 {
cf2c3c16
TT
24413 case DW_FORM_data1:
24414 case DW_FORM_flag:
24415 ++bytes;
24416 break;
24417
24418 case DW_FORM_data2:
24419 bytes += 2;
24420 break;
24421
24422 case DW_FORM_data4:
24423 bytes += 4;
24424 break;
24425
24426 case DW_FORM_data8:
24427 bytes += 8;
24428 break;
24429
0224619f
JK
24430 case DW_FORM_data16:
24431 bytes += 16;
24432 break;
24433
cf2c3c16
TT
24434 case DW_FORM_string:
24435 read_direct_string (abfd, bytes, &bytes_read);
24436 bytes += bytes_read;
24437 break;
24438
24439 case DW_FORM_sec_offset:
24440 case DW_FORM_strp:
36586728 24441 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24442 bytes += offset_size;
24443 break;
24444
24445 case DW_FORM_block:
24446 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24447 bytes += bytes_read;
24448 break;
24449
24450 case DW_FORM_block1:
24451 bytes += 1 + read_1_byte (abfd, bytes);
24452 break;
24453 case DW_FORM_block2:
24454 bytes += 2 + read_2_bytes (abfd, bytes);
24455 break;
24456 case DW_FORM_block4:
24457 bytes += 4 + read_4_bytes (abfd, bytes);
24458 break;
24459
336d760d 24460 case DW_FORM_addrx:
cf2c3c16 24461 case DW_FORM_sdata:
cf532bd1 24462 case DW_FORM_strx:
cf2c3c16 24463 case DW_FORM_udata:
3019eac3
DE
24464 case DW_FORM_GNU_addr_index:
24465 case DW_FORM_GNU_str_index:
d521ce57 24466 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24467 if (bytes == NULL)
24468 {
24469 dwarf2_section_buffer_overflow_complaint (section);
24470 return NULL;
24471 }
cf2c3c16
TT
24472 break;
24473
663c44ac
JK
24474 case DW_FORM_implicit_const:
24475 break;
24476
cf2c3c16
TT
24477 default:
24478 {
b98664d3 24479 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24480 form, get_section_name (section));
cf2c3c16
TT
24481 return NULL;
24482 }
2e276125
JB
24483 }
24484
cf2c3c16
TT
24485 return bytes;
24486}
757a13d0 24487
cf2c3c16
TT
24488/* A helper for dwarf_decode_macros that handles skipping an unknown
24489 opcode. Returns an updated pointer to the macro data buffer; or,
24490 on error, issues a complaint and returns NULL. */
757a13d0 24491
d521ce57 24492static const gdb_byte *
cf2c3c16 24493skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24494 const gdb_byte **opcode_definitions,
24495 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24496 bfd *abfd,
24497 unsigned int offset_size,
24498 struct dwarf2_section_info *section)
24499{
24500 unsigned int bytes_read, i;
24501 unsigned long arg;
d521ce57 24502 const gdb_byte *defn;
2e276125 24503
cf2c3c16 24504 if (opcode_definitions[opcode] == NULL)
2e276125 24505 {
b98664d3 24506 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24507 opcode);
24508 return NULL;
24509 }
2e276125 24510
cf2c3c16
TT
24511 defn = opcode_definitions[opcode];
24512 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24513 defn += bytes_read;
2e276125 24514
cf2c3c16
TT
24515 for (i = 0; i < arg; ++i)
24516 {
aead7601
SM
24517 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24518 (enum dwarf_form) defn[i], offset_size,
f664829e 24519 section);
cf2c3c16
TT
24520 if (mac_ptr == NULL)
24521 {
24522 /* skip_form_bytes already issued the complaint. */
24523 return NULL;
24524 }
24525 }
757a13d0 24526
cf2c3c16
TT
24527 return mac_ptr;
24528}
757a13d0 24529
cf2c3c16
TT
24530/* A helper function which parses the header of a macro section.
24531 If the macro section is the extended (for now called "GNU") type,
24532 then this updates *OFFSET_SIZE. Returns a pointer to just after
24533 the header, or issues a complaint and returns NULL on error. */
757a13d0 24534
d521ce57
TT
24535static const gdb_byte *
24536dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24537 bfd *abfd,
d521ce57 24538 const gdb_byte *mac_ptr,
cf2c3c16
TT
24539 unsigned int *offset_size,
24540 int section_is_gnu)
24541{
24542 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24543
cf2c3c16
TT
24544 if (section_is_gnu)
24545 {
24546 unsigned int version, flags;
757a13d0 24547
cf2c3c16 24548 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24549 if (version != 4 && version != 5)
cf2c3c16 24550 {
b98664d3 24551 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24552 version);
24553 return NULL;
24554 }
24555 mac_ptr += 2;
757a13d0 24556
cf2c3c16
TT
24557 flags = read_1_byte (abfd, mac_ptr);
24558 ++mac_ptr;
24559 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24560
cf2c3c16
TT
24561 if ((flags & 2) != 0)
24562 /* We don't need the line table offset. */
24563 mac_ptr += *offset_size;
757a13d0 24564
cf2c3c16
TT
24565 /* Vendor opcode descriptions. */
24566 if ((flags & 4) != 0)
24567 {
24568 unsigned int i, count;
757a13d0 24569
cf2c3c16
TT
24570 count = read_1_byte (abfd, mac_ptr);
24571 ++mac_ptr;
24572 for (i = 0; i < count; ++i)
24573 {
24574 unsigned int opcode, bytes_read;
24575 unsigned long arg;
24576
24577 opcode = read_1_byte (abfd, mac_ptr);
24578 ++mac_ptr;
24579 opcode_definitions[opcode] = mac_ptr;
24580 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24581 mac_ptr += bytes_read;
24582 mac_ptr += arg;
24583 }
757a13d0 24584 }
cf2c3c16 24585 }
757a13d0 24586
cf2c3c16
TT
24587 return mac_ptr;
24588}
757a13d0 24589
cf2c3c16 24590/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24591 including DW_MACRO_import. */
cf2c3c16
TT
24592
24593static void
804d2729 24594dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24595 bfd *abfd,
d521ce57 24596 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24597 struct macro_source_file *current_file,
43f3e411 24598 struct line_header *lh,
cf2c3c16 24599 struct dwarf2_section_info *section,
36586728 24600 int section_is_gnu, int section_is_dwz,
cf2c3c16 24601 unsigned int offset_size,
8fc3fc34 24602 htab_t include_hash)
cf2c3c16 24603{
804d2729
TT
24604 struct dwarf2_per_objfile *dwarf2_per_objfile
24605 = cu->per_cu->dwarf2_per_objfile;
4d663531 24606 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24607 enum dwarf_macro_record_type macinfo_type;
24608 int at_commandline;
d521ce57 24609 const gdb_byte *opcode_definitions[256];
757a13d0 24610
cf2c3c16
TT
24611 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24612 &offset_size, section_is_gnu);
24613 if (mac_ptr == NULL)
24614 {
24615 /* We already issued a complaint. */
24616 return;
24617 }
757a13d0
JK
24618
24619 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24620 GDB is still reading the definitions from command line. First
24621 DW_MACINFO_start_file will need to be ignored as it was already executed
24622 to create CURRENT_FILE for the main source holding also the command line
24623 definitions. On first met DW_MACINFO_start_file this flag is reset to
24624 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24625
24626 at_commandline = 1;
24627
24628 do
24629 {
24630 /* Do we at least have room for a macinfo type byte? */
24631 if (mac_ptr >= mac_end)
24632 {
f664829e 24633 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24634 break;
24635 }
24636
aead7601 24637 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24638 mac_ptr++;
24639
cf2c3c16
TT
24640 /* Note that we rely on the fact that the corresponding GNU and
24641 DWARF constants are the same. */
132448f8
SM
24642 DIAGNOSTIC_PUSH
24643 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24644 switch (macinfo_type)
24645 {
24646 /* A zero macinfo type indicates the end of the macro
24647 information. */
24648 case 0:
24649 break;
2e276125 24650
0af92d60
JK
24651 case DW_MACRO_define:
24652 case DW_MACRO_undef:
24653 case DW_MACRO_define_strp:
24654 case DW_MACRO_undef_strp:
24655 case DW_MACRO_define_sup:
24656 case DW_MACRO_undef_sup:
2e276125 24657 {
891d2f0b 24658 unsigned int bytes_read;
2e276125 24659 int line;
d521ce57 24660 const char *body;
cf2c3c16 24661 int is_define;
2e276125 24662
cf2c3c16
TT
24663 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24664 mac_ptr += bytes_read;
24665
0af92d60
JK
24666 if (macinfo_type == DW_MACRO_define
24667 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24668 {
24669 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24670 mac_ptr += bytes_read;
24671 }
24672 else
24673 {
24674 LONGEST str_offset;
24675
24676 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24677 mac_ptr += offset_size;
2e276125 24678
0af92d60
JK
24679 if (macinfo_type == DW_MACRO_define_sup
24680 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24681 || section_is_dwz)
36586728 24682 {
ed2dc618
SM
24683 struct dwz_file *dwz
24684 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24685
ed2dc618
SM
24686 body = read_indirect_string_from_dwz (objfile,
24687 dwz, str_offset);
36586728
TT
24688 }
24689 else
ed2dc618
SM
24690 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24691 abfd, str_offset);
cf2c3c16
TT
24692 }
24693
0af92d60
JK
24694 is_define = (macinfo_type == DW_MACRO_define
24695 || macinfo_type == DW_MACRO_define_strp
24696 || macinfo_type == DW_MACRO_define_sup);
2e276125 24697 if (! current_file)
757a13d0
JK
24698 {
24699 /* DWARF violation as no main source is present. */
b98664d3 24700 complaint (_("debug info with no main source gives macro %s "
757a13d0 24701 "on line %d: %s"),
cf2c3c16
TT
24702 is_define ? _("definition") : _("undefinition"),
24703 line, body);
757a13d0
JK
24704 break;
24705 }
3e43a32a
MS
24706 if ((line == 0 && !at_commandline)
24707 || (line != 0 && at_commandline))
b98664d3 24708 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24709 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24710 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24711 line == 0 ? _("zero") : _("non-zero"), line, body);
24712
955b06fa 24713 if (body == NULL)
7bede828 24714 {
955b06fa
SDJ
24715 /* Fedora's rpm-build's "debugedit" binary
24716 corrupted .debug_macro sections.
24717
24718 For more info, see
24719 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24720 complaint (_("debug info gives %s invalid macro %s "
24721 "without body (corrupted?) at line %d "
24722 "on file %s"),
24723 at_commandline ? _("command-line") : _("in-file"),
24724 is_define ? _("definition") : _("undefinition"),
24725 line, current_file->filename);
7bede828 24726 }
955b06fa
SDJ
24727 else if (is_define)
24728 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24729 else
24730 {
0af92d60
JK
24731 gdb_assert (macinfo_type == DW_MACRO_undef
24732 || macinfo_type == DW_MACRO_undef_strp
24733 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24734 macro_undef (current_file, line, body);
24735 }
2e276125
JB
24736 }
24737 break;
24738
0af92d60 24739 case DW_MACRO_start_file:
2e276125 24740 {
891d2f0b 24741 unsigned int bytes_read;
2e276125
JB
24742 int line, file;
24743
24744 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24745 mac_ptr += bytes_read;
24746 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24747 mac_ptr += bytes_read;
24748
3e43a32a
MS
24749 if ((line == 0 && !at_commandline)
24750 || (line != 0 && at_commandline))
b98664d3 24751 complaint (_("debug info gives source %d included "
757a13d0
JK
24752 "from %s at %s line %d"),
24753 file, at_commandline ? _("command-line") : _("file"),
24754 line == 0 ? _("zero") : _("non-zero"), line);
24755
24756 if (at_commandline)
24757 {
0af92d60 24758 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24759 pass one. */
757a13d0
JK
24760 at_commandline = 0;
24761 }
24762 else
804d2729
TT
24763 current_file = macro_start_file (cu, file, line, current_file,
24764 lh);
2e276125
JB
24765 }
24766 break;
24767
0af92d60 24768 case DW_MACRO_end_file:
2e276125 24769 if (! current_file)
b98664d3 24770 complaint (_("macro debug info has an unmatched "
3e43a32a 24771 "`close_file' directive"));
2e276125
JB
24772 else
24773 {
24774 current_file = current_file->included_by;
24775 if (! current_file)
24776 {
cf2c3c16 24777 enum dwarf_macro_record_type next_type;
2e276125
JB
24778
24779 /* GCC circa March 2002 doesn't produce the zero
24780 type byte marking the end of the compilation
24781 unit. Complain if it's not there, but exit no
24782 matter what. */
24783
24784 /* Do we at least have room for a macinfo type byte? */
24785 if (mac_ptr >= mac_end)
24786 {
f664829e 24787 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24788 return;
24789 }
24790
24791 /* We don't increment mac_ptr here, so this is just
24792 a look-ahead. */
aead7601
SM
24793 next_type
24794 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24795 mac_ptr);
2e276125 24796 if (next_type != 0)
b98664d3 24797 complaint (_("no terminating 0-type entry for "
3e43a32a 24798 "macros in `.debug_macinfo' section"));
2e276125
JB
24799
24800 return;
24801 }
24802 }
24803 break;
24804
0af92d60
JK
24805 case DW_MACRO_import:
24806 case DW_MACRO_import_sup:
cf2c3c16
TT
24807 {
24808 LONGEST offset;
8fc3fc34 24809 void **slot;
a036ba48
TT
24810 bfd *include_bfd = abfd;
24811 struct dwarf2_section_info *include_section = section;
d521ce57 24812 const gdb_byte *include_mac_end = mac_end;
a036ba48 24813 int is_dwz = section_is_dwz;
d521ce57 24814 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24815
24816 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24817 mac_ptr += offset_size;
24818
0af92d60 24819 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24820 {
ed2dc618 24821 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24822
4d663531 24823 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24824
a036ba48 24825 include_section = &dwz->macro;
a32a8923 24826 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24827 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24828 is_dwz = 1;
24829 }
24830
24831 new_mac_ptr = include_section->buffer + offset;
24832 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24833
8fc3fc34
TT
24834 if (*slot != NULL)
24835 {
24836 /* This has actually happened; see
24837 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24838 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24839 ".debug_macro section"));
24840 }
24841 else
24842 {
d521ce57 24843 *slot = (void *) new_mac_ptr;
36586728 24844
804d2729 24845 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24846 include_mac_end, current_file, lh,
36586728 24847 section, section_is_gnu, is_dwz,
4d663531 24848 offset_size, include_hash);
8fc3fc34 24849
d521ce57 24850 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24851 }
cf2c3c16
TT
24852 }
24853 break;
24854
2e276125 24855 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24856 if (!section_is_gnu)
24857 {
24858 unsigned int bytes_read;
2e276125 24859
ac298888
TT
24860 /* This reads the constant, but since we don't recognize
24861 any vendor extensions, we ignore it. */
24862 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24863 mac_ptr += bytes_read;
24864 read_direct_string (abfd, mac_ptr, &bytes_read);
24865 mac_ptr += bytes_read;
2e276125 24866
cf2c3c16
TT
24867 /* We don't recognize any vendor extensions. */
24868 break;
24869 }
24870 /* FALLTHROUGH */
24871
24872 default:
24873 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24874 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24875 section);
24876 if (mac_ptr == NULL)
24877 return;
24878 break;
2e276125 24879 }
132448f8 24880 DIAGNOSTIC_POP
757a13d0 24881 } while (macinfo_type != 0);
2e276125 24882}
8e19ed76 24883
cf2c3c16 24884static void
09262596 24885dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24886 int section_is_gnu)
cf2c3c16 24887{
518817b3
SM
24888 struct dwarf2_per_objfile *dwarf2_per_objfile
24889 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24890 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24891 struct line_header *lh = cu->line_header;
24892 bfd *abfd;
d521ce57 24893 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24894 struct macro_source_file *current_file = 0;
24895 enum dwarf_macro_record_type macinfo_type;
24896 unsigned int offset_size = cu->header.offset_size;
d521ce57 24897 const gdb_byte *opcode_definitions[256];
8fc3fc34 24898 void **slot;
09262596
DE
24899 struct dwarf2_section_info *section;
24900 const char *section_name;
24901
24902 if (cu->dwo_unit != NULL)
24903 {
24904 if (section_is_gnu)
24905 {
24906 section = &cu->dwo_unit->dwo_file->sections.macro;
24907 section_name = ".debug_macro.dwo";
24908 }
24909 else
24910 {
24911 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24912 section_name = ".debug_macinfo.dwo";
24913 }
24914 }
24915 else
24916 {
24917 if (section_is_gnu)
24918 {
24919 section = &dwarf2_per_objfile->macro;
24920 section_name = ".debug_macro";
24921 }
24922 else
24923 {
24924 section = &dwarf2_per_objfile->macinfo;
24925 section_name = ".debug_macinfo";
24926 }
24927 }
cf2c3c16 24928
bb5ed363 24929 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24930 if (section->buffer == NULL)
24931 {
b98664d3 24932 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24933 return;
24934 }
a32a8923 24935 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24936
24937 /* First pass: Find the name of the base filename.
24938 This filename is needed in order to process all macros whose definition
24939 (or undefinition) comes from the command line. These macros are defined
24940 before the first DW_MACINFO_start_file entry, and yet still need to be
24941 associated to the base file.
24942
24943 To determine the base file name, we scan the macro definitions until we
24944 reach the first DW_MACINFO_start_file entry. We then initialize
24945 CURRENT_FILE accordingly so that any macro definition found before the
24946 first DW_MACINFO_start_file can still be associated to the base file. */
24947
24948 mac_ptr = section->buffer + offset;
24949 mac_end = section->buffer + section->size;
24950
24951 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24952 &offset_size, section_is_gnu);
24953 if (mac_ptr == NULL)
24954 {
24955 /* We already issued a complaint. */
24956 return;
24957 }
24958
24959 do
24960 {
24961 /* Do we at least have room for a macinfo type byte? */
24962 if (mac_ptr >= mac_end)
24963 {
24964 /* Complaint is printed during the second pass as GDB will probably
24965 stop the first pass earlier upon finding
24966 DW_MACINFO_start_file. */
24967 break;
24968 }
24969
aead7601 24970 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24971 mac_ptr++;
24972
24973 /* Note that we rely on the fact that the corresponding GNU and
24974 DWARF constants are the same. */
132448f8
SM
24975 DIAGNOSTIC_PUSH
24976 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24977 switch (macinfo_type)
24978 {
24979 /* A zero macinfo type indicates the end of the macro
24980 information. */
24981 case 0:
24982 break;
24983
0af92d60
JK
24984 case DW_MACRO_define:
24985 case DW_MACRO_undef:
cf2c3c16
TT
24986 /* Only skip the data by MAC_PTR. */
24987 {
24988 unsigned int bytes_read;
24989
24990 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24991 mac_ptr += bytes_read;
24992 read_direct_string (abfd, mac_ptr, &bytes_read);
24993 mac_ptr += bytes_read;
24994 }
24995 break;
24996
0af92d60 24997 case DW_MACRO_start_file:
cf2c3c16
TT
24998 {
24999 unsigned int bytes_read;
25000 int line, file;
25001
25002 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25003 mac_ptr += bytes_read;
25004 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25005 mac_ptr += bytes_read;
25006
804d2729 25007 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25008 }
25009 break;
25010
0af92d60 25011 case DW_MACRO_end_file:
cf2c3c16
TT
25012 /* No data to skip by MAC_PTR. */
25013 break;
25014
0af92d60
JK
25015 case DW_MACRO_define_strp:
25016 case DW_MACRO_undef_strp:
25017 case DW_MACRO_define_sup:
25018 case DW_MACRO_undef_sup:
cf2c3c16
TT
25019 {
25020 unsigned int bytes_read;
25021
25022 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25023 mac_ptr += bytes_read;
25024 mac_ptr += offset_size;
25025 }
25026 break;
25027
0af92d60
JK
25028 case DW_MACRO_import:
25029 case DW_MACRO_import_sup:
cf2c3c16 25030 /* Note that, according to the spec, a transparent include
0af92d60 25031 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25032 skip this opcode. */
25033 mac_ptr += offset_size;
25034 break;
25035
25036 case DW_MACINFO_vendor_ext:
25037 /* Only skip the data by MAC_PTR. */
25038 if (!section_is_gnu)
25039 {
25040 unsigned int bytes_read;
25041
25042 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25043 mac_ptr += bytes_read;
25044 read_direct_string (abfd, mac_ptr, &bytes_read);
25045 mac_ptr += bytes_read;
25046 }
25047 /* FALLTHROUGH */
25048
25049 default:
25050 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25051 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25052 section);
25053 if (mac_ptr == NULL)
25054 return;
25055 break;
25056 }
132448f8 25057 DIAGNOSTIC_POP
cf2c3c16
TT
25058 } while (macinfo_type != 0 && current_file == NULL);
25059
25060 /* Second pass: Process all entries.
25061
25062 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25063 command-line macro definitions/undefinitions. This flag is unset when we
25064 reach the first DW_MACINFO_start_file entry. */
25065
fc4007c9
TT
25066 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25067 htab_eq_pointer,
25068 NULL, xcalloc, xfree));
8fc3fc34 25069 mac_ptr = section->buffer + offset;
fc4007c9 25070 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25071 *slot = (void *) mac_ptr;
804d2729 25072 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25073 current_file, lh, section,
fc4007c9
TT
25074 section_is_gnu, 0, offset_size,
25075 include_hash.get ());
cf2c3c16
TT
25076}
25077
8e19ed76 25078/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25079 if so return true else false. */
380bca97 25080
8e19ed76 25081static int
6e5a29e1 25082attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25083{
25084 return (attr == NULL ? 0 :
25085 attr->form == DW_FORM_block1
25086 || attr->form == DW_FORM_block2
25087 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25088 || attr->form == DW_FORM_block
25089 || attr->form == DW_FORM_exprloc);
8e19ed76 25090}
4c2df51b 25091
c6a0999f
JB
25092/* Return non-zero if ATTR's value is a section offset --- classes
25093 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25094 You may use DW_UNSND (attr) to retrieve such offsets.
25095
25096 Section 7.5.4, "Attribute Encodings", explains that no attribute
25097 may have a value that belongs to more than one of these classes; it
25098 would be ambiguous if we did, because we use the same forms for all
25099 of them. */
380bca97 25100
3690dd37 25101static int
6e5a29e1 25102attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25103{
25104 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25105 || attr->form == DW_FORM_data8
25106 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25107}
25108
3690dd37
JB
25109/* Return non-zero if ATTR's value falls in the 'constant' class, or
25110 zero otherwise. When this function returns true, you can apply
25111 dwarf2_get_attr_constant_value to it.
25112
25113 However, note that for some attributes you must check
25114 attr_form_is_section_offset before using this test. DW_FORM_data4
25115 and DW_FORM_data8 are members of both the constant class, and of
25116 the classes that contain offsets into other debug sections
25117 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25118 that, if an attribute's can be either a constant or one of the
25119 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25120 taken as section offsets, not constants.
25121
25122 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25123 cannot handle that. */
380bca97 25124
3690dd37 25125static int
6e5a29e1 25126attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25127{
25128 switch (attr->form)
25129 {
25130 case DW_FORM_sdata:
25131 case DW_FORM_udata:
25132 case DW_FORM_data1:
25133 case DW_FORM_data2:
25134 case DW_FORM_data4:
25135 case DW_FORM_data8:
663c44ac 25136 case DW_FORM_implicit_const:
3690dd37
JB
25137 return 1;
25138 default:
25139 return 0;
25140 }
25141}
25142
7771576e
SA
25143
25144/* DW_ADDR is always stored already as sect_offset; despite for the forms
25145 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25146
25147static int
6e5a29e1 25148attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25149{
25150 switch (attr->form)
25151 {
25152 case DW_FORM_ref_addr:
25153 case DW_FORM_ref1:
25154 case DW_FORM_ref2:
25155 case DW_FORM_ref4:
25156 case DW_FORM_ref8:
25157 case DW_FORM_ref_udata:
25158 case DW_FORM_GNU_ref_alt:
25159 return 1;
25160 default:
25161 return 0;
25162 }
25163}
25164
3019eac3
DE
25165/* Return the .debug_loc section to use for CU.
25166 For DWO files use .debug_loc.dwo. */
25167
25168static struct dwarf2_section_info *
25169cu_debug_loc_section (struct dwarf2_cu *cu)
25170{
518817b3
SM
25171 struct dwarf2_per_objfile *dwarf2_per_objfile
25172 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25173
3019eac3 25174 if (cu->dwo_unit)
43988095
JK
25175 {
25176 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25177
43988095
JK
25178 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25179 }
25180 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25181 : &dwarf2_per_objfile->loc);
3019eac3
DE
25182}
25183
8cf6f0b1
TT
25184/* A helper function that fills in a dwarf2_loclist_baton. */
25185
25186static void
25187fill_in_loclist_baton (struct dwarf2_cu *cu,
25188 struct dwarf2_loclist_baton *baton,
ff39bb5e 25189 const struct attribute *attr)
8cf6f0b1 25190{
518817b3
SM
25191 struct dwarf2_per_objfile *dwarf2_per_objfile
25192 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25193 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25194
25195 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25196
25197 baton->per_cu = cu->per_cu;
25198 gdb_assert (baton->per_cu);
25199 /* We don't know how long the location list is, but make sure we
25200 don't run off the edge of the section. */
3019eac3
DE
25201 baton->size = section->size - DW_UNSND (attr);
25202 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25203 baton->base_address = cu->base_address;
f664829e 25204 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25205}
25206
4c2df51b 25207static void
ff39bb5e 25208dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25209 struct dwarf2_cu *cu, int is_block)
4c2df51b 25210{
518817b3
SM
25211 struct dwarf2_per_objfile *dwarf2_per_objfile
25212 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25213 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25214 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25215
3690dd37 25216 if (attr_form_is_section_offset (attr)
3019eac3 25217 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25218 the section. If so, fall through to the complaint in the
25219 other branch. */
3019eac3 25220 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25221 {
0d53c4c4 25222 struct dwarf2_loclist_baton *baton;
4c2df51b 25223
8d749320 25224 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25225
8cf6f0b1 25226 fill_in_loclist_baton (cu, baton, attr);
be391dca 25227
d00adf39 25228 if (cu->base_known == 0)
b98664d3 25229 complaint (_("Location list used without "
3e43a32a 25230 "specifying the CU base address."));
4c2df51b 25231
f1e6e072
TT
25232 SYMBOL_ACLASS_INDEX (sym) = (is_block
25233 ? dwarf2_loclist_block_index
25234 : dwarf2_loclist_index);
0d53c4c4
DJ
25235 SYMBOL_LOCATION_BATON (sym) = baton;
25236 }
25237 else
25238 {
25239 struct dwarf2_locexpr_baton *baton;
25240
8d749320 25241 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25242 baton->per_cu = cu->per_cu;
25243 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25244
25245 if (attr_form_is_block (attr))
25246 {
25247 /* Note that we're just copying the block's data pointer
25248 here, not the actual data. We're still pointing into the
6502dd73
DJ
25249 info_buffer for SYM's objfile; right now we never release
25250 that buffer, but when we do clean up properly this may
25251 need to change. */
0d53c4c4
DJ
25252 baton->size = DW_BLOCK (attr)->size;
25253 baton->data = DW_BLOCK (attr)->data;
25254 }
25255 else
25256 {
25257 dwarf2_invalid_attrib_class_complaint ("location description",
25258 SYMBOL_NATURAL_NAME (sym));
25259 baton->size = 0;
0d53c4c4 25260 }
6e70227d 25261
f1e6e072
TT
25262 SYMBOL_ACLASS_INDEX (sym) = (is_block
25263 ? dwarf2_locexpr_block_index
25264 : dwarf2_locexpr_index);
0d53c4c4
DJ
25265 SYMBOL_LOCATION_BATON (sym) = baton;
25266 }
4c2df51b 25267}
6502dd73 25268
9aa1f1e3
TT
25269/* Return the OBJFILE associated with the compilation unit CU. If CU
25270 came from a separate debuginfo file, then the master objfile is
25271 returned. */
ae0d2f24
UW
25272
25273struct objfile *
25274dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25275{
e3b94546 25276 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25277
25278 /* Return the master objfile, so that we can report and look up the
25279 correct file containing this variable. */
25280 if (objfile->separate_debug_objfile_backlink)
25281 objfile = objfile->separate_debug_objfile_backlink;
25282
25283 return objfile;
25284}
25285
96408a79
SA
25286/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25287 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25288 CU_HEADERP first. */
25289
25290static const struct comp_unit_head *
25291per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25292 struct dwarf2_per_cu_data *per_cu)
25293{
d521ce57 25294 const gdb_byte *info_ptr;
96408a79
SA
25295
25296 if (per_cu->cu)
25297 return &per_cu->cu->header;
25298
9c541725 25299 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25300
25301 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25302 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25303 rcuh_kind::COMPILE);
96408a79
SA
25304
25305 return cu_headerp;
25306}
25307
ae0d2f24
UW
25308/* Return the address size given in the compilation unit header for CU. */
25309
98714339 25310int
ae0d2f24
UW
25311dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25312{
96408a79
SA
25313 struct comp_unit_head cu_header_local;
25314 const struct comp_unit_head *cu_headerp;
c471e790 25315
96408a79
SA
25316 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25317
25318 return cu_headerp->addr_size;
ae0d2f24
UW
25319}
25320
9eae7c52
TT
25321/* Return the offset size given in the compilation unit header for CU. */
25322
25323int
25324dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25325{
96408a79
SA
25326 struct comp_unit_head cu_header_local;
25327 const struct comp_unit_head *cu_headerp;
9c6c53f7 25328
96408a79
SA
25329 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25330
25331 return cu_headerp->offset_size;
25332}
25333
25334/* See its dwarf2loc.h declaration. */
25335
25336int
25337dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25338{
25339 struct comp_unit_head cu_header_local;
25340 const struct comp_unit_head *cu_headerp;
25341
25342 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25343
25344 if (cu_headerp->version == 2)
25345 return cu_headerp->addr_size;
25346 else
25347 return cu_headerp->offset_size;
181cebd4
JK
25348}
25349
9aa1f1e3
TT
25350/* Return the text offset of the CU. The returned offset comes from
25351 this CU's objfile. If this objfile came from a separate debuginfo
25352 file, then the offset may be different from the corresponding
25353 offset in the parent objfile. */
25354
25355CORE_ADDR
25356dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25357{
e3b94546 25358 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25359
25360 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25361}
25362
9a49df9d
AB
25363/* Return a type that is a generic pointer type, the size of which matches
25364 the address size given in the compilation unit header for PER_CU. */
25365static struct type *
25366dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25367{
25368 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25369 struct type *void_type = objfile_type (objfile)->builtin_void;
25370 struct type *addr_type = lookup_pointer_type (void_type);
25371 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25372
25373 if (TYPE_LENGTH (addr_type) == addr_size)
25374 return addr_type;
25375
25376 addr_type
25377 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25378 return addr_type;
25379}
25380
43988095
JK
25381/* Return DWARF version number of PER_CU. */
25382
25383short
25384dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25385{
25386 return per_cu->dwarf_version;
25387}
25388
348e048f
DE
25389/* Locate the .debug_info compilation unit from CU's objfile which contains
25390 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25391
25392static struct dwarf2_per_cu_data *
9c541725 25393dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25394 unsigned int offset_in_dwz,
ed2dc618 25395 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25396{
25397 struct dwarf2_per_cu_data *this_cu;
25398 int low, high;
25399
ae038cb0 25400 low = 0;
b76e467d 25401 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25402 while (high > low)
25403 {
36586728 25404 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25405 int mid = low + (high - low) / 2;
9a619af0 25406
36586728 25407 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25408 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25409 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25410 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25411 high = mid;
25412 else
25413 low = mid + 1;
25414 }
25415 gdb_assert (low == high);
36586728 25416 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25417 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25418 {
36586728 25419 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25420 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25421 "offset %s [in module %s]"),
25422 sect_offset_str (sect_off),
ed2dc618 25423 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25424
9c541725
PA
25425 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25426 <= sect_off);
ae038cb0
DJ
25427 return dwarf2_per_objfile->all_comp_units[low-1];
25428 }
25429 else
25430 {
b76e467d 25431 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25432 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25433 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25434 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25435 return this_cu;
25436 }
25437}
25438
23745b47 25439/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25440
fcd3b13d
SM
25441dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25442 : per_cu (per_cu_),
9068261f
AB
25443 mark (false),
25444 has_loclist (false),
25445 checked_producer (false),
25446 producer_is_gxx_lt_4_6 (false),
25447 producer_is_gcc_lt_4_3 (false),
eb77c9df 25448 producer_is_icc (false),
9068261f 25449 producer_is_icc_lt_14 (false),
c258c396 25450 producer_is_codewarrior (false),
9068261f 25451 processing_has_namespace_info (false)
93311388 25452{
fcd3b13d
SM
25453 per_cu->cu = this;
25454}
25455
25456/* Destroy a dwarf2_cu. */
25457
25458dwarf2_cu::~dwarf2_cu ()
25459{
25460 per_cu->cu = NULL;
9816fde3
JK
25461}
25462
25463/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25464
25465static void
95554aad
TT
25466prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25467 enum language pretend_language)
9816fde3
JK
25468{
25469 struct attribute *attr;
25470
25471 /* Set the language we're debugging. */
25472 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25473 if (attr)
25474 set_cu_language (DW_UNSND (attr), cu);
25475 else
9cded63f 25476 {
95554aad 25477 cu->language = pretend_language;
9cded63f
TT
25478 cu->language_defn = language_def (cu->language);
25479 }
dee91e82 25480
7d45c7c3 25481 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25482}
25483
ae038cb0
DJ
25484/* Increase the age counter on each cached compilation unit, and free
25485 any that are too old. */
25486
25487static void
ed2dc618 25488age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25489{
25490 struct dwarf2_per_cu_data *per_cu, **last_chain;
25491
25492 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25493 per_cu = dwarf2_per_objfile->read_in_chain;
25494 while (per_cu != NULL)
25495 {
25496 per_cu->cu->last_used ++;
b4f54984 25497 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25498 dwarf2_mark (per_cu->cu);
25499 per_cu = per_cu->cu->read_in_chain;
25500 }
25501
25502 per_cu = dwarf2_per_objfile->read_in_chain;
25503 last_chain = &dwarf2_per_objfile->read_in_chain;
25504 while (per_cu != NULL)
25505 {
25506 struct dwarf2_per_cu_data *next_cu;
25507
25508 next_cu = per_cu->cu->read_in_chain;
25509
25510 if (!per_cu->cu->mark)
25511 {
fcd3b13d 25512 delete per_cu->cu;
ae038cb0
DJ
25513 *last_chain = next_cu;
25514 }
25515 else
25516 last_chain = &per_cu->cu->read_in_chain;
25517
25518 per_cu = next_cu;
25519 }
25520}
25521
25522/* Remove a single compilation unit from the cache. */
25523
25524static void
dee91e82 25525free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25526{
25527 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25528 struct dwarf2_per_objfile *dwarf2_per_objfile
25529 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25530
25531 per_cu = dwarf2_per_objfile->read_in_chain;
25532 last_chain = &dwarf2_per_objfile->read_in_chain;
25533 while (per_cu != NULL)
25534 {
25535 struct dwarf2_per_cu_data *next_cu;
25536
25537 next_cu = per_cu->cu->read_in_chain;
25538
dee91e82 25539 if (per_cu == target_per_cu)
ae038cb0 25540 {
fcd3b13d 25541 delete per_cu->cu;
dee91e82 25542 per_cu->cu = NULL;
ae038cb0
DJ
25543 *last_chain = next_cu;
25544 break;
25545 }
25546 else
25547 last_chain = &per_cu->cu->read_in_chain;
25548
25549 per_cu = next_cu;
25550 }
25551}
25552
dee91e82
DE
25553/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25554 We store these in a hash table separate from the DIEs, and preserve them
25555 when the DIEs are flushed out of cache.
25556
25557 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25558 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25559 or the type may come from a DWO file. Furthermore, while it's more logical
25560 to use per_cu->section+offset, with Fission the section with the data is in
25561 the DWO file but we don't know that section at the point we need it.
25562 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25563 because we can enter the lookup routine, get_die_type_at_offset, from
25564 outside this file, and thus won't necessarily have PER_CU->cu.
25565 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25566
dee91e82 25567struct dwarf2_per_cu_offset_and_type
1c379e20 25568{
dee91e82 25569 const struct dwarf2_per_cu_data *per_cu;
9c541725 25570 sect_offset sect_off;
1c379e20
DJ
25571 struct type *type;
25572};
25573
dee91e82 25574/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25575
25576static hashval_t
dee91e82 25577per_cu_offset_and_type_hash (const void *item)
1c379e20 25578{
9a3c8263
SM
25579 const struct dwarf2_per_cu_offset_and_type *ofs
25580 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25581
9c541725 25582 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25583}
25584
dee91e82 25585/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25586
25587static int
dee91e82 25588per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25589{
9a3c8263
SM
25590 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25591 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25592 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25593 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25594
dee91e82 25595 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25596 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25597}
25598
25599/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25600 table if necessary. For convenience, return TYPE.
25601
25602 The DIEs reading must have careful ordering to:
25603 * Not cause infite loops trying to read in DIEs as a prerequisite for
25604 reading current DIE.
25605 * Not trying to dereference contents of still incompletely read in types
25606 while reading in other DIEs.
25607 * Enable referencing still incompletely read in types just by a pointer to
25608 the type without accessing its fields.
25609
25610 Therefore caller should follow these rules:
25611 * Try to fetch any prerequisite types we may need to build this DIE type
25612 before building the type and calling set_die_type.
e71ec853 25613 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25614 possible before fetching more types to complete the current type.
25615 * Make the type as complete as possible before fetching more types. */
1c379e20 25616
f792889a 25617static struct type *
1c379e20
DJ
25618set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25619{
518817b3
SM
25620 struct dwarf2_per_objfile *dwarf2_per_objfile
25621 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25622 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25623 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25624 struct attribute *attr;
25625 struct dynamic_prop prop;
1c379e20 25626
b4ba55a1
JB
25627 /* For Ada types, make sure that the gnat-specific data is always
25628 initialized (if not already set). There are a few types where
25629 we should not be doing so, because the type-specific area is
25630 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25631 where the type-specific area is used to store the floatformat).
25632 But this is not a problem, because the gnat-specific information
25633 is actually not needed for these types. */
25634 if (need_gnat_info (cu)
25635 && TYPE_CODE (type) != TYPE_CODE_FUNC
25636 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25637 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25638 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25639 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25640 && !HAVE_GNAT_AUX_INFO (type))
25641 INIT_GNAT_SPECIFIC (type);
25642
3f2f83dd
KB
25643 /* Read DW_AT_allocated and set in type. */
25644 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25645 if (attr_form_is_block (attr))
25646 {
9a49df9d
AB
25647 struct type *prop_type
25648 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25649 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25650 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25651 }
25652 else if (attr != NULL)
25653 {
b98664d3 25654 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25655 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25656 sect_offset_str (die->sect_off));
3f2f83dd
KB
25657 }
25658
25659 /* Read DW_AT_associated and set in type. */
25660 attr = dwarf2_attr (die, DW_AT_associated, cu);
25661 if (attr_form_is_block (attr))
25662 {
9a49df9d
AB
25663 struct type *prop_type
25664 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25665 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25666 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25667 }
25668 else if (attr != NULL)
25669 {
b98664d3 25670 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25671 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25672 sect_offset_str (die->sect_off));
3f2f83dd
KB
25673 }
25674
3cdcd0ce
JB
25675 /* Read DW_AT_data_location and set in type. */
25676 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25677 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25678 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25679 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25680
dee91e82 25681 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25682 {
dee91e82
DE
25683 dwarf2_per_objfile->die_type_hash =
25684 htab_create_alloc_ex (127,
25685 per_cu_offset_and_type_hash,
25686 per_cu_offset_and_type_eq,
25687 NULL,
25688 &objfile->objfile_obstack,
25689 hashtab_obstack_allocate,
25690 dummy_obstack_deallocate);
f792889a 25691 }
1c379e20 25692
dee91e82 25693 ofs.per_cu = cu->per_cu;
9c541725 25694 ofs.sect_off = die->sect_off;
1c379e20 25695 ofs.type = type;
dee91e82
DE
25696 slot = (struct dwarf2_per_cu_offset_and_type **)
25697 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25698 if (*slot)
b98664d3 25699 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25700 sect_offset_str (die->sect_off));
8d749320
SM
25701 *slot = XOBNEW (&objfile->objfile_obstack,
25702 struct dwarf2_per_cu_offset_and_type);
1c379e20 25703 **slot = ofs;
f792889a 25704 return type;
1c379e20
DJ
25705}
25706
9c541725 25707/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25708 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25709
25710static struct type *
9c541725 25711get_die_type_at_offset (sect_offset sect_off,
673bfd45 25712 struct dwarf2_per_cu_data *per_cu)
1c379e20 25713{
dee91e82 25714 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25715 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25716
dee91e82 25717 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25718 return NULL;
1c379e20 25719
dee91e82 25720 ofs.per_cu = per_cu;
9c541725 25721 ofs.sect_off = sect_off;
9a3c8263
SM
25722 slot = ((struct dwarf2_per_cu_offset_and_type *)
25723 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25724 if (slot)
25725 return slot->type;
25726 else
25727 return NULL;
25728}
25729
02142a6c 25730/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25731 or return NULL if DIE does not have a saved type. */
25732
25733static struct type *
25734get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25735{
9c541725 25736 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25737}
25738
10b3939b
DJ
25739/* Add a dependence relationship from CU to REF_PER_CU. */
25740
25741static void
25742dwarf2_add_dependence (struct dwarf2_cu *cu,
25743 struct dwarf2_per_cu_data *ref_per_cu)
25744{
25745 void **slot;
25746
25747 if (cu->dependencies == NULL)
25748 cu->dependencies
25749 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25750 NULL, &cu->comp_unit_obstack,
25751 hashtab_obstack_allocate,
25752 dummy_obstack_deallocate);
25753
25754 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25755 if (*slot == NULL)
25756 *slot = ref_per_cu;
25757}
1c379e20 25758
f504f079
DE
25759/* Subroutine of dwarf2_mark to pass to htab_traverse.
25760 Set the mark field in every compilation unit in the
ae038cb0
DJ
25761 cache that we must keep because we are keeping CU. */
25762
10b3939b
DJ
25763static int
25764dwarf2_mark_helper (void **slot, void *data)
25765{
25766 struct dwarf2_per_cu_data *per_cu;
25767
25768 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25769
25770 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25771 reading of the chain. As such dependencies remain valid it is not much
25772 useful to track and undo them during QUIT cleanups. */
25773 if (per_cu->cu == NULL)
25774 return 1;
25775
10b3939b
DJ
25776 if (per_cu->cu->mark)
25777 return 1;
9068261f 25778 per_cu->cu->mark = true;
10b3939b
DJ
25779
25780 if (per_cu->cu->dependencies != NULL)
25781 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25782
25783 return 1;
25784}
25785
f504f079
DE
25786/* Set the mark field in CU and in every other compilation unit in the
25787 cache that we must keep because we are keeping CU. */
25788
ae038cb0
DJ
25789static void
25790dwarf2_mark (struct dwarf2_cu *cu)
25791{
25792 if (cu->mark)
25793 return;
9068261f 25794 cu->mark = true;
10b3939b
DJ
25795 if (cu->dependencies != NULL)
25796 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25797}
25798
25799static void
25800dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25801{
25802 while (per_cu)
25803 {
9068261f 25804 per_cu->cu->mark = false;
ae038cb0
DJ
25805 per_cu = per_cu->cu->read_in_chain;
25806 }
72bf9492
DJ
25807}
25808
72bf9492
DJ
25809/* Trivial hash function for partial_die_info: the hash value of a DIE
25810 is its offset in .debug_info for this objfile. */
25811
25812static hashval_t
25813partial_die_hash (const void *item)
25814{
9a3c8263
SM
25815 const struct partial_die_info *part_die
25816 = (const struct partial_die_info *) item;
9a619af0 25817
9c541725 25818 return to_underlying (part_die->sect_off);
72bf9492
DJ
25819}
25820
25821/* Trivial comparison function for partial_die_info structures: two DIEs
25822 are equal if they have the same offset. */
25823
25824static int
25825partial_die_eq (const void *item_lhs, const void *item_rhs)
25826{
9a3c8263
SM
25827 const struct partial_die_info *part_die_lhs
25828 = (const struct partial_die_info *) item_lhs;
25829 const struct partial_die_info *part_die_rhs
25830 = (const struct partial_die_info *) item_rhs;
9a619af0 25831
9c541725 25832 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25833}
25834
3c3bb058
AB
25835struct cmd_list_element *set_dwarf_cmdlist;
25836struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25837
25838static void
981a3fb3 25839set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25840{
b4f54984 25841 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25842 gdb_stdout);
ae038cb0
DJ
25843}
25844
25845static void
981a3fb3 25846show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25847{
b4f54984 25848 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25849}
25850
491144b5 25851bool dwarf_always_disassemble;
437afbb8 25852
437afbb8 25853static void
cd4fb1b2
SM
25854show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25855 struct cmd_list_element *c, const char *value)
9291a0cd 25856{
cd4fb1b2
SM
25857 fprintf_filtered (file,
25858 _("Whether to always disassemble "
25859 "DWARF expressions is %s.\n"),
25860 value);
9291a0cd
TT
25861}
25862
9291a0cd 25863static void
cd4fb1b2
SM
25864show_check_physname (struct ui_file *file, int from_tty,
25865 struct cmd_list_element *c, const char *value)
9291a0cd 25866{
cd4fb1b2
SM
25867 fprintf_filtered (file,
25868 _("Whether to check \"physname\" is %s.\n"),
25869 value);
9291a0cd
TT
25870}
25871
cd4fb1b2
SM
25872void
25873_initialize_dwarf2_read (void)
9291a0cd 25874{
cd4fb1b2
SM
25875 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25876Set DWARF specific variables.\n\
590042fc 25877Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25878 &set_dwarf_cmdlist, "maintenance set dwarf ",
25879 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25880
cd4fb1b2 25881 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25882Show DWARF specific variables.\n\
25883Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25884 &show_dwarf_cmdlist, "maintenance show dwarf ",
25885 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25886
cd4fb1b2
SM
25887 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25888 &dwarf_max_cache_age, _("\
25889Set the upper bound on the age of cached DWARF compilation units."), _("\
25890Show the upper bound on the age of cached DWARF compilation units."), _("\
25891A higher limit means that cached compilation units will be stored\n\
25892in memory longer, and more total memory will be used. Zero disables\n\
25893caching, which can slow down startup."),
25894 NULL,
25895 show_dwarf_max_cache_age,
25896 &set_dwarf_cmdlist,
25897 &show_dwarf_cmdlist);
156942c7 25898
cd4fb1b2
SM
25899 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25900 &dwarf_always_disassemble, _("\
25901Set whether `info address' always disassembles DWARF expressions."), _("\
25902Show whether `info address' always disassembles DWARF expressions."), _("\
25903When enabled, DWARF expressions are always printed in an assembly-like\n\
25904syntax. When disabled, expressions will be printed in a more\n\
25905conversational style, when possible."),
25906 NULL,
25907 show_dwarf_always_disassemble,
25908 &set_dwarf_cmdlist,
25909 &show_dwarf_cmdlist);
9291a0cd 25910
cd4fb1b2
SM
25911 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25912Set debugging of the DWARF reader."), _("\
25913Show debugging of the DWARF reader."), _("\
25914When enabled (non-zero), debugging messages are printed during DWARF\n\
25915reading and symtab expansion. A value of 1 (one) provides basic\n\
25916information. A value greater than 1 provides more verbose information."),
25917 NULL,
25918 NULL,
25919 &setdebuglist, &showdebuglist);
9291a0cd 25920
cd4fb1b2
SM
25921 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25922Set debugging of the DWARF DIE reader."), _("\
25923Show debugging of the DWARF DIE reader."), _("\
25924When enabled (non-zero), DIEs are dumped after they are read in.\n\
25925The value is the maximum depth to print."),
25926 NULL,
25927 NULL,
25928 &setdebuglist, &showdebuglist);
9291a0cd 25929
cd4fb1b2
SM
25930 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25931Set debugging of the dwarf line reader."), _("\
25932Show debugging of the dwarf line reader."), _("\
25933When enabled (non-zero), line number entries are dumped as they are read in.\n\
25934A value of 1 (one) provides basic information.\n\
25935A value greater than 1 provides more verbose information."),
25936 NULL,
25937 NULL,
25938 &setdebuglist, &showdebuglist);
437afbb8 25939
cd4fb1b2
SM
25940 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25941Set cross-checking of \"physname\" code against demangler."), _("\
25942Show cross-checking of \"physname\" code against demangler."), _("\
25943When enabled, GDB's internal \"physname\" code is checked against\n\
25944the demangler."),
25945 NULL, show_check_physname,
25946 &setdebuglist, &showdebuglist);
900e11f9 25947
e615022a
DE
25948 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25949 no_class, &use_deprecated_index_sections, _("\
25950Set whether to use deprecated gdb_index sections."), _("\
25951Show whether to use deprecated gdb_index sections."), _("\
25952When enabled, deprecated .gdb_index sections are used anyway.\n\
25953Normally they are ignored either because of a missing feature or\n\
25954performance issue.\n\
25955Warning: This option must be enabled before gdb reads the file."),
25956 NULL,
25957 NULL,
25958 &setlist, &showlist);
25959
f1e6e072
TT
25960 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25961 &dwarf2_locexpr_funcs);
25962 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25963 &dwarf2_loclist_funcs);
25964
25965 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25966 &dwarf2_block_frame_base_locexpr_funcs);
25967 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25968 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25969
25970#if GDB_SELF_TEST
25971 selftests::register_test ("dw2_expand_symtabs_matching",
25972 selftests::dw2_expand_symtabs_matching::run_test);
25973#endif
6502dd73 25974}
This page took 6.667689 seconds and 4 git commands to generate.